Message-ID: <1280506046834-2308143.post@n4.nabble.com>
Date: 2010-07-30T16:07:26Z
From: Wu Gong
Subject: how to get higher derivatives with "deriv"
In-Reply-To: <CF4CB0A0-76C9-4BCC-9ADF-45C052CFA859@gmail.com>
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.