Hi Peter,

Here is my homework :)

DD <- function(f,order=1){
        f.str <- "body(f)"
        while(order>=1) {
        f.str <- paste('D(',f.str,',"x")',sep="")
        order <- order-1}
        ddf <- f
        body(ddf) <- eval(parse(text=f.str))
        ddf}

f <- function(x,alpha) x^alpha
DD(f)(2,3)
g <- function(x,alpha) x^alpha+2*x^(alpha-1)
DD(g,2)(2,3)

It would be better to handle expressions directly, but I can only paste
strings. The reason why I didn't prefer using body() is that the derivative
only works on a mathematical function not a R function. The body(f) would
cause error when function is constructed by "{}".
 

-----
A R learner.
-- 
View this message in context: 
http://r.789695.n4.nabble.com/how-to-get-higher-derivatives-with-deriv-tp2306711p2308143.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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