# hessian

Hessian matrix of scalar function

## Description

example

hessian(f,v) finds the Hessian matrix of the scalar function f with respect to vector v in Cartesian coordinates.

If you do not specify v, then hessian(f) finds the Hessian matrix of the scalar function f with respect to a vector constructed from all symbolic variables found in f. The order of variables in this vector is defined by symvar.

## Examples

### Find Hessian Matrix of Scalar Function

Find the Hessian matrix of a function by using hessian. Then find the Hessian matrix of the same function as the Jacobian of the gradient of the function.

Find the Hessian matrix of this function of three variables:

syms x y z
f = x*y + 2*z*x;
hessian(f,[x,y,z])
ans =
[ 0, 1, 2]
[ 1, 0, 0]
[ 2, 0, 0]

Alternatively, compute the Hessian matrix of this function as the Jacobian of the gradient of that function:

ans =
[ 0, 1, 2]
[ 1, 0, 0]
[ 2, 0, 0]

## Input Arguments

collapse all

Scalar function, specified as symbolic expression or symbolic function.

Vector with respect to which you find Hessian matrix, specified as a symbolic vector. By default, v is a vector constructed from all symbolic variables found in f. The order of variables in this vector is defined by symvar.

If v is an empty symbolic object, such as sym([]), then hessian returns an empty symbolic object.

collapse all

### Hessian Matrix

The Hessian matrix of f(x) is the square matrix of the second partial derivatives of f(x).

$H\left(f\right)=\left[\begin{array}{cccc}\frac{{\partial }^{2}f}{\partial {x}_{1}^{2}}& \frac{{\partial }^{2}f}{\partial {x}_{1}\partial {x}_{2}}& \cdots & \frac{{\partial }^{2}f}{\partial {x}_{1}\partial {x}_{n}}\\ \frac{{\partial }^{2}f}{\partial {x}_{2}\partial {x}_{1}}& \frac{{\partial }^{2}f}{\partial {x}_{2}^{2}}& \cdots & \frac{{\partial }^{2}f}{\partial {x}_{2}\partial {x}_{n}}\\ ⋮& ⋮& \ddots & ⋮\\ \frac{{\partial }^{2}f}{\partial {x}_{n}\partial {x}_{1}}& \frac{{\partial }^{2}f}{\partial {x}_{n}\partial {x}_{2}}& \cdots & \frac{{\partial }^{2}f}{\partial {x}_{n}^{2}}\end{array}\right]$