Dear ExpeRts,
I have trouble implementing a function which computes the k-th derivative of a
specified function f and returns it as a function. I tried to adapt what I found
under ?deriv but could not get it to work. Here is how it should look like:
## specify the function
f <- function (x,alpha) x^alpha
## higher derivatives
DD <- function(expr, variable, order = 1) {
if(order < 1) stop("'order' must be >= 1")
if(order == 1) deriv(expr, variable)
else DD(deriv(expr, variable), variable, order - 1)
}
## compute the second derivative of f
f.prime.prime <- DD(f,"x",2)
## evaluate it at x=1 for alpha=0.5
f.prime.prime(1,0.5)
Many thanks,
Marius
## specify the function string
f.str <- "x^alpha"
## higher derivatives
DD <- function(f.str, x = 2, alpha=3,order = 1){
expr.s <- parse(text=f.str)
while(order>=1) {
expr.s <- D(expr.s,"x")
order <- order-1}
eval(expr.s)
}
compute
DD(f.str,x=1,alpha=0.5,order=1)
-----
A R learner.
--
View this message in context:
http://r.789695.n4.nabble.com/how-to-get-higher-derivatives-with-deriv-tp2306711p2307241.html
Sent from the R help mailing list archive at Nabble.com.
Okay, great. Thanks. Cheers, Marius -- View this message in context: http://r.789695.n4.nabble.com/how-to-get-higher-derivatives-with-deriv-tp2306711p2307709.html Sent from the R help mailing list archive at Nabble.com.