## Write Objective Function for Problem-Based Least Squares

To specify an objective function for problem-based least squares, write the objective explicitly as a sum of squares. 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:
...

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

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

...

Similarly, write nonlinear least-squares as explicit sums of squares of optimization expressions.

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

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

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

lsqnonlin options:
...

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

${R}_{n}={a}_{n}+{k}_{1}\sum \left({k}_{2}\sum \left({k}_{3}\sum \left(...{k}_{j}{e}_{n}^{2}\right)\right)\right)$

• 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.

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 + (fcn2optimexpr(@cos,x(1)))^2;
prob = optimproblem('Objective',R1 + R2 + R3);
options = optimoptions(prob)
options =

lsqnonlin options:
...

Watch now