Main Content

Writing Vector and Matrix Objective Functions

What Are Vector and 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 how you 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

Jij(x)=Fi(x)xj.

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

For example, if

F(x)=[x12+x2x3sin(x1+2x23x3)],

then J(x) is

J(x)=[2x1x3x2cos(x1+2x23x3)2cos(x1+2x23x3)3cos(x1+2x23x3)].

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 the solver that your objective function includes a Jacobian, set the SpecifyObjectiveGradient option to true. For example:

options = optimoptions('lsqnonlin','SpecifyObjectiveGradient',true);

Jacobians of Matrix Functions

To define the Jacobian of a matrix F(x), change the matrix to a vector, column by column. For example, rewrite the matrix

F=[F11F12F21F22F31F32]

as a vector f

f=[F11F21F31F12F22F32].

The Jacobian of F is defined in terms of the Jacobian of f,

Jij=fixj.

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)=[x1x2x13+3x225x2x14x2/x14x22x13x24],

then the Jacobian of F is

J(x)=[x2x14x13502x23x126x2x2/x121/x13x124x23].

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=[x11x12x21x22],

then the gradient is defined in terms of the vector

x=[x11x21x12x22].

With

F=[F11F12F21F22F31F32],

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

Jij=fixj.

So, for example,

J(3,2)=f(3)x(2)=F31X21, and J(5,4)=f(5)x(4)=F22X22.

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.

Related Topics