Main Content

Write Objective Function for Problem-Based Least Squares

To specify an objective function for problem-based least squares, write the objective either explicitly as a sum of squares or as the square of a norm of an expression. By explicitly using a least-squares formulation, you obtain the most appropriate and efficient solver for your problem. For example,

t = randn(10,1); % Data for the example
x = optimvar('x',10);

obj = sum((x - t).^2); % Explicit sum of squares

prob = optimproblem("Objective",obj);
% Check to see the default solver
opts = optimoptions(prob)
opts = 

  lsqlin options:
...

Equivalently, write the objective as a squared norm.

obj2 = norm(x-t)^2;
prob2 = optimproblem("Objective",obj2);
% Check to see the default solver
opts = optimoptions(prob2)
opts = 

  lsqlin options:
...

In contrast, expressing the objective as a mathematically equivalent expression gives a problem that the software interprets as a general quadratic problem.

obj3 = (x - t)'*(x - t); % Equivalent to a sum of squares,
                         % but not interpreted as a sum of squares
prob3 = optimproblem("Objective",obj3);
% Check to see the default solver
opts = optimoptions(prob3)
opts = 

  quadprog options:
...

Similarly, write nonlinear least-squares as a square of a norm or an explicit sums of squares of optimization expressions. This objective is an explicit sum of squares.

t = linspace(0,5); % Data for the example
A = optimvar('A');
r = optimvar('r');
expr = A*exp(r*t);
ydata = 3*exp(-2*t) + 0.1*randn(size(t));

obj4 = sum((expr - ydata).^2); % Explicit sum of squares

prob4 = optimproblem("Objective",obj4);
% Check to see the default solver
opts = optimoptions(prob4)
opts = 

  lsqnonlin options:
...

Equivalently, write the objective as a squared norm.

obj5 = norm(expr - ydata)^2; % norm squared
prob5 = optimproblem("Objective",obj5);
% Check to see the default solver
opts = optimoptions(prob5)
opts = 

  lsqnonlin options:
...

The most general form that the software interprets as a least-squares problem is a square of a norm or else a sum of expressions Rn of this form:

Rn=an+k1(k2(k3(...kjen2)))

  • en is any expression. If multidimensional, en should be squared term-by-term using .^2.

  • an is a scalar numeric value.

  • The kj are positive scalar numeric values.

  • Instead of multiplying by kj, you can instead divide by kj, which is equivalent to multiplying by 1/kj.

Each expression Rn must evaluate to a scalar, not a multidimensional value. For example,

x = optimvar('x',10,3,4);
y = optimvar('y',10,2);
t = randn(10,3,4); % Data for example
u = randn(10,2); % Data for example
a = randn; % Coefficient
k = abs(randn(5,1)); % Positive coefficients
% Explicit sums of squares:
R1 = a + k(1)*sum(k(2)*sum(k(3)*sum((x - t).^2,3)));
R2 = k(4)*sum(k(5)*sum((y - u).^2,2));
R3 = 1 + cos(x(1))^2;
prob = optimproblem('Objective',R1 + R2 + R3);
options = optimoptions(prob)
options = 

  lsqnonlin options:
...

Related Topics