How can I derive a function and then calculate its maximum in R

I've tried using the functions D() and deriv() to derive the function, but the output is an expression so I can't use the method optimize() to get the maximum of the derivative.

Upvotes: 0

Views: 121

Answers (1)

Kra.P
Kra.P

Reputation: 15123

Try using Deriv::Deriv() function instead. It returns function, not expression. For a simple example,

    f <- function(x) x^2
    Deriv(f)

result is

    function (x) 
    2 * x

Now you can optimize() that derivative

Upvotes: 1

Related Questions