Hi:

Try the following:

f <- function(x) 5*cos(2*x)-2*x*sin(2*x)
curve(f, -5, 5)
abline(0, 0, lty = 'dotted')

This shows rather clearly that your function has multiple roots, which isn't
surprising given that it's a linear combination of sines and cosines. To
find a specific root numerically, use function uniroot on f, as follows:

> uniroot(f, c(0, 2))
$root
[1] 0.6569286

$f.root
[1] -0.0001196119

$iter
[1] 6

$estim.prec
[1] 6.103516e-05

This catches the root that lies between x = 0 and x = 2. If you want to find
a set of roots, you can try a loop. Fortunately, since the function is even,
you really only need to find the roots on one side of zero, since the ones
on the other side are the same with opposite sign.

lo <- seq(0, 4.5, by = 1.5)
hi <-  seq(1.5, 6, by = 1.5)
roots <- numeric(length(lo))

for(i in seq_along(lo)) roots[i] <- uniroot(f, c(lo[i], hi[i]))$root
roots

See ?uniroot for other options and tolerance settings.

HTH,
Dennis

On Wed, Aug 11, 2010 at 6:21 PM, TGS <cran.questi...@gmail.com> wrote:

> How would I numerically find the x value where the derivative of the
> function below is zero?
>
> x <- seq(1,2, by = .01)
> y <- 5*cos(2*x)-2*x*sin(2*x)
> plot(x,abs(y), type = "l", ylab = "|y|")
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to