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


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

For example, if


then J(x) is


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)), ...

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


as a vector f


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


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


then the Jacobian of F is


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


then the gradient is defined in terms of the vector




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


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