[R] Using uniroot() with output from deriv() or D()

Hans W Borchers hwborchers at googlemail.com
Sat Feb 26 15:10:34 CET 2011


> I need to find the root of the second derivative of many curves and do not
> want to cut and paste the expression results from the deriv() or D()
> functions every time.  Below is an example.  What I need to do is refer to
> "fn2nd" in the uniroot() function, but when I try something like
> uniroot(fn2nd,c(0,1)) I get an error, so I have resorted to pasting in the
> expression, but this is highly inefficient.
> 
> Thanks,  J
> 
> [...]

What is so wrong with using

    r <- uniroot(function(x) eval(fn2nd, list(x=x)),
            interval=c(0, 1), tol=0.0001)

(I thought you were almost there) or even

    fn2nd_fun <- function(x) eval(fn2nd, list(x=x))
    ex <- seq(from=0, to=1, length.out = 1000)
    y1 <- fn2nd_fun(ex)
    ...
    r <- uniroot(fn2nd_fun, interval=c(0, 1), tol=0.0001)

--Hans Werner

> r$root
> abline(h=0, col = "red")
> abline(v=r$root, col = "green")
> arrows(0.6, -2, r$root,0, length = 0.1, angle = 30, code = 2, col = "red")
> text(0.765,-2.3,paste("b = ",r$root,sep=""))
>



More information about the R-help mailing list