Hi Jeremy,
One way to evaluate a function at multiple points is to write a for loop
that will evaluate your function for every point in ruler and store it in an
empty vector. Then you can plot the vector that your loop filled against
the ruler.
Cheers,
Jenn
On Wed, Feb 27, 2008 at 2:36 AM, Jeremy Hodgen <jeremy.hodgen at kcl.ac.uk>
wrote:
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
_______________________________________________
gov2001-l mailing list
gov2001-l at
lists.fas.harvard.edu
http://lists.fas.harvard.edu/mailman/listinfo/gov2001-l