EXAMPLES OF SYMBOLIC DIFFERENTIATION AND MINIMIZATION IN R. Taking symbolic derivatives with "D". > D(quote(a^3),"a") 3 * a^2 > D(quote(b^3*a^3),"a") b^3 * (3 * a^2) > D(D(quote(b^3*a^3),"a"),"b") 3 * b^2 * (3 * a^2) Using "eval" to get the value of a symbolic expression. > eval(D(D(quote(b^3*a^3),"a"),"b"),list(a=10,b=1)) [1] 900 > k <- function (a) eval(D(quote(a^3),"a")) > k(100) [1] 30000 Minimizing a function with "nlm". > f <- function (x, a) sum((x-a)^2) > nlm (function (x) f(x,c(1,2,3)), c(10,-10,20)) $minimum [1] 3.499991e-12 $estimate [1] 0.9999995 1.9999990 2.9999985 $gradient [1] -9.529044e-13 3.554712e-12 1.573408e-12 $code [1] 1 $iterations [1] 2 > g <- function (d) nlm (function (x) f(x,d), rep(0,length(d))) > g(10:20) $minimum [1] 3.405039e-26 $estimate [1] 10 11 12 13 14 15 16 17 18 19 20 $gradient [1] -1.598721e-13 -2.842171e-14 -1.065814e-13 -1.882939e-13 -5.329078e-14 [6] 7.815982e-14 0.000000e+00 -7.815972e-14 4.973801e-14 1.847411e-13 [11] 1.065815e-13 $code [1] 1 $iterations [1] 2 Using "deriv" to get a function for evaluating derivatives. > deriv(quote(x^2),"x",func=T) function (x) { .value <- x^2 .grad <- array(0, c(length(.value), 1), list(NULL, c("x"))) .grad[, "x"] <- 2 * x attr(.value, "gradient") <- .grad .value } > deriv(quote(x^2),"x",func=T,hessian=T) function (x) { .value <- x^2 .grad <- array(0, c(length(.value), 1), list(NULL, c("x"))) .hessian <- array(0, c(length(.value), 1, 1), list(NULL, c("x"), c("x"))) .grad[, "x"] <- 2 * x .hessian[, "x", "x"] <- 2 attr(.value, "gradient") <- .grad attr(.value, "hessian") <- .hessian .value } > nlm(deriv(quote((x-5)^2),"x",func=T,hessian=T),0) $minimum [1] 7.888609e-31 $estimate [1] 5 $gradient [1] -1.776357e-15 $code [1] 1 $iterations [1] 1