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.

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

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

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

For example, if

$$F(x)=\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(x)=\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);

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(x)=\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(x)=\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].$$

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,

$$J(3,2)=\frac{\partial f(3)}{\partial x(2)}=\frac{\partial {F}_{31}}{\partial {X}_{21}},\text{and}J(5,4)=\frac{\partial f(5)}{\partial x(4)}=\frac{\partial {F}_{22}}{\partial {X}_{22}}.$$

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.