Calculating Jacobian and Hessian Matrices in R
Code
To calculate the Jacobian matrix and Hessian matrix in R, you use the jacobian()
and hessian()
functions from the numDeriv
package.
install.packages("numDeriv")
library(numDeriv)
f <- function(v) {c(v[1]^2 + v[2]^2 - 1,
sin(pi*v[1]/2) + v[2]^3)}
g <- function(v) {(v[1])^3+(v[2])^2}
jacobian(f, c(1,1))
hessian(g, c(1,1))
The results of executing the code are as follows. The top is the result of substituting into the Jacobian matrix of , and the bottom is the result of substituting into the Hessian matrix of .
In fact, since the Jacobian matrix of is , was obtained, and since the Hessian matrix of is , was obtained.