[R] D() and deriv() accessing components of returned expression

toby909 at gmail.com toby909 at gmail.com
Wed Feb 28 07:26:32 CET 2007


Hi All

I like the D() function since it directly gives me the derivative. D() however 
does not take multiple arguments like c("x1", "x2"), but deriv() does, but 
deriv() I am having trouble accessing the actual derivative function. So 
strange, its sitting direct in front of me but I cant access it:

 > deriv(fct, c("x", "c"))
expression({
     .value <- x^2 - 5 * x - 2 + 5 * c
     .grad <- array(0, c(length(.value), 2), list(NULL, c("x",
         "c")))
     .grad[, "x"] <- 2 * x - 5
     .grad[, "c"] <- 5
     attr(.value, "gradient") <- .grad
     .value
})

I want to plug in the derivative expression into an eval() function. I guess 
this is a more general question of how to access parts of a "list"?

Thanks for your hint.

Toby




de = 1
x = -4
D(fct, "x")
dfct = deriv(fct, c("x", "c"))
while (abs(de)>0.000000001) {
de = solve(-eval(fct),eval(dfct))
x = de+x
print(x)
}



More information about the R-help mailing list