jacobian: jacobian function

View source: R/jacobian.R

jacobianR Documentation

jacobian function

Description

Creates a function that computes the jacobi-matrix of a function for one specific variable. Hereinafter the variable is called y. The derivative is calculated with respect to one of the arguments of the function. Subsequently, the variable is called x. The returned function can be called at any possible point of x.

Usage

jacobian(f, y, x, derivs = NULL, num_functions = NULL)

Arguments

f

A function

y

The variables to compute the derivatives of (the dependent variable). For example: df/dx

x

The variables to which respect the variables are calcualted (the independent variable). For example: df/dx

derivs

optional input defining own functions which should be used. See d() for details.

num_functions

optional input defining number of functions otherwise a squared matrix form is assumed.

Details

The function jacobian is intended for using it for functions accepting vectors (in case of x) and returns a vector (for y).
Mentionable, only integers are allowed for indexing the vectors. Moreover, only one element at the time can be changed. For instance, y[1] is permitted. In contrast, y[1.5] or y[variable] will throw an error.
As usually it is possible to define new variables. If x and/or y are found at the right side of the assignment operator the variable is replaced in all following lines. See the example below:
# Old code a <- x[1] b <- 3 y[1] <- a*b # New code b <- 3 y[1] <- a*3

Furthermore, it is possible to use if, else if, else blocks within the function. However, the dependent variable have to be located at the left side of the assignment operator. This restriction is necessary as variables found in previous lines are replaced in the following lines. # allowed code f <- function(x, t) { y <- numeric(2) y[1] <- 2*x[1]^3 if(t < 3) { y[2] <- x[2]^2 } else { y[2] <- x[2]^4 } return(y) } # not allowed code f <- function(x, t) { y <- numeric(2) y[1] <- 2*x[1]^3 a <- 0 if(t < 3) { a <- x[2]^2 } else { a <- x[2]^4 } y[2] <- a return(y) }

Value

A function that computes the jacobi-matrix of f. Notably, it expects the dame arguments as the input function f.

Examples

f <- function(x) {
   y <- numeric(2)
   y[1] <- x[1]^2 + sin(4)
   y[2] <- x[2]*7
   return(y)
}
jac <- dfdr::jacobian(f, y, x)
jac(c(1, 2))

mailund/dfdr documentation built on Feb. 25, 2023, 5:37 p.m.