[R] how to get higher derivatives with "deriv"

Wu Gong wg2f at mtmail.mtsu.edu
Fri Jul 30 18:07:26 CEST 2010


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.



More information about the R-help mailing list