Documentation

## Writing Vector and Matrix Objective Functions

### What Are Vector or Matrix Objective Functions?

Some solvers, such as `fsolve` and `lsqcurvefit`, have objective functions that are vectors or matrices. The main difference in usage between these types of objective functions and scalar objective functions is the way to write their derivatives. The first-order partial derivatives of a vector-valued or matrix-valued function is called a Jacobian; the first-order partial derivatives of a scalar function is called a gradient.

For information on complex-valued objective functions, see Complex Numbers in Optimization Toolbox Solvers.

### Jacobians of Vector Functions

If x is a vector of independent variables, and F(x) is a vector function, the Jacobian J(x) is

`${J}_{ij}\left(x\right)=\frac{\partial {F}_{i}\left(x\right)}{\partial {x}_{j}}.$`

If F has m components, and x has k components, J is an m-by-k matrix.

For example, if

`$F\left(x\right)=\left[\begin{array}{c}{x}_{1}^{2}+{x}_{2}{x}_{3}\\ \mathrm{sin}\left({x}_{1}+2{x}_{2}-3{x}_{3}\right)\end{array}\right],$`

then J(x) is

`$J\left(x\right)=\left[\begin{array}{ccc}2{x}_{1}& {x}_{3}& {x}_{2}\\ \mathrm{cos}\left({x}_{1}+2{x}_{2}-3{x}_{3}\right)& 2\mathrm{cos}\left({x}_{1}+2{x}_{2}-3{x}_{3}\right)& -3\mathrm{cos}\left({x}_{1}+2{x}_{2}-3{x}_{3}\right)\end{array}\right].$`

The function file associated with this example is:

```function [F jacF] = vectorObjective(x) F = [x(1)^2 + x(2)*x(3); sin(x(1) + 2*x(2) - 3*x(3))]; if nargout > 1 % need Jacobian jacF = [2*x(1),x(3),x(2); cos(x(1)+2*x(2)-3*x(3)),2*cos(x(1)+2*x(2)-3*x(3)), ... -3*cos(x(1)+2*x(2)-3*x(3))]; end```

To indicate to your solver that your objective function includes a Jacobian, set the `SpecifyObjectiveGradient` option to `true`. For example,

`options = optimptions('lsqnonlin','SpecifyObjectiveGradient',true);`

### Jacobians of Matrix Functions

The Jacobian of a matrix F(x) is defined by changing the matrix to a vector, column by column. For example, rewrite the matrix

`$F=\left[\begin{array}{cc}{F}_{11}& {F}_{12}\\ {F}_{21}& {F}_{22}\\ {F}_{31}& {F}_{32}\end{array}\right]$`

as a vector f:

`$f=\left[\begin{array}{c}{F}_{11}\\ {F}_{21}\\ {F}_{31}\\ {F}_{12}\\ {F}_{22}\\ {F}_{32}\end{array}\right].$`

The Jacobian of F is as the Jacobian of f,

`${J}_{ij}=\frac{\partial {f}_{i}}{\partial {x}_{j}}.$`

If F is an m-by-n matrix, and x is a k-vector, the Jacobian is an mn-by-k matrix.

For example, if

`$F\left(x\right)=\left[\begin{array}{cc}{x}_{1}{x}_{2}& {x}_{1}^{3}+3{x}_{2}^{2}\\ 5{x}_{2}-{x}_{1}^{4}& {x}_{2}/{x}_{1}\\ 4-{x}_{2}^{2}& {x}_{1}^{3}-{x}_{2}^{4}\end{array}\right],$`

then the Jacobian of F is

`$J\left(x\right)=\left[\begin{array}{cc}{x}_{2}& {x}_{1}\\ -4{x}_{1}^{3}& 5\\ 0& -2{x}_{2}\\ 3{x}_{1}^{2}& 6{x}_{2}\\ -{x}_{2}/{x}_{1}^{2}& 1/{x}_{1}\\ 3{x}_{1}^{2}& -4{x}_{2}^{3}\end{array}\right].$`

### Jacobians with Matrix-Valued Independent Variables

If x is a matrix, define the Jacobian of F(x) by changing the matrix x to a vector, column by column. For example, if

`$X=\left[\begin{array}{cc}{x}_{11}& {x}_{12}\\ {x}_{21}& {x}_{22}\end{array}\right],$`

then the gradient is defined in terms of the vector

`$x=\left[\begin{array}{c}{x}_{11}\\ {x}_{21}\\ {x}_{12}\\ {x}_{22}\end{array}\right].$`

With

`$F=\left[\begin{array}{cc}{F}_{11}& {F}_{12}\\ {F}_{21}& {F}_{22}\\ {F}_{31}& {F}_{32}\end{array}\right],$`

and with f the vector form of F as above, the Jacobian of F(X) is defined as the Jacobian of f(x):

`${J}_{ij}=\frac{\partial {f}_{i}}{\partial {x}_{j}}.$`

So, for example,

If F is an m-by-n matrix and x is a j-by-k matrix, then the Jacobian is an mn-by-jk matrix.

Watch now