Skip to content
Prev 385501 / 398506 Next

Solving derivates, getting the minimum of a function, and helpful documentation of the deriv function

On Sat, 29 Aug 2020 21:15:56 +0000
"Sorkin, John" <jsorkin at som.umaryland.edu> wrote:

            
Quadratic function???
It causes deriv() to return a *function* rather than an *expression*.
Couple of things that you could play around with.

y <- expression(-0.0263*B + 0.0010*B^2)
z <- deriv(y,"B",func=TRUE)
f <- function(x,z){as.vector(attr(z(x),"gradient"))}

(1) uniroot(f,c(5,15),z=z)$root
# 13.15 --- right answer!!! :-)

(2) library(polynom) # You may need to install this package.
    p <- poly.calc(x=1:2,y=f(1:2,z=z))
    polyroot(p)
# 13.15+0i You can get rid of the extraneous imaginary part
# by using Re(polyroot(p))

HTH

cheers,

Rolf

P.S. It's irritating the way that one has to fiddle about in order to
get a function that returns the value of the derivative, rather than the
value of the function being differentiated!

R.