toby909 at gmail.com
2007-Feb-28 06:26 UTC
[R] D() and deriv() accessing components of returned expression
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) }