Can anyone help?
I'm trying to define the function for qu2
This works fine for individual values of x:
g <- function(x){
if ((x < pi/2) & (x > -1*pi/2)) (y <- 10*sin(x)*cos(x)) else (y <-
sin(x)*cos(x))
return(y)
}
But when I input a vector (in order to draw the graph) like this:
ruler <- seq(-2*pi, 2*pi, by=.01)
g(ruler)
I get this error message:
Warning message:
In if ((x < pi/2) & (x > -1 * pi/2)) (y <- 10 * sin(x) * cos(x))
else (y <- sin(x) * :
the condition has length > 1 and only the first element will be used
It seems I've got too many conditions for 'if' when the input is a
vector. When I draw the graph of g(ruler)~ruler, I get sinxcosx on
the domain [-2pi, 2pi]. Any ideas on what I could do differently?
Thanks
Jeremy
Dr Jeremy Hodgen
Senior Lecturer in Mathematics Education
King's College London
Department of Education and Professional Studies
Franklin-Wilkins Building
Waterloo Bridge Wing
150 Stamford Street
London SE1 9NH
Tel: 020 7848 3102
Fax: 020 7848 3182
E-mail: jeremy.hodgen at kcl.ac.uk