# 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 solver = solvers(prob)

solver = "lsqlin"

Equivalently, write the objective as a squared norm.

obj2 = norm(x-t)^2; prob2 = optimproblem(Objective=obj2); solver2 = solvers(prob2)

solver2 = "lsqlin"

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); solver3 = solvers(prob3)

solver3 = "quadprog"

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); solver4 = solvers(prob4)

solver4 = "lsqnonlin"

Equivalently, write the objective as a squared norm.

```
obj5 = norm(expr - ydata)^2; % norm squared
prob5 = optimproblem(Objective=obj5);
solver5 = solvers(prob5)
```

solver5 = "lsqnonlin"

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:

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

$${e}_{n}$$ is any expression. If multidimensional, $${e}_{n}$$ should be squared term-by-term using

`.^2`

.$${a}_{n}$$ is a scalar numeric value.

The $${k}_{j}$$ are positive scalar numeric values.

Instead of multiplying by $${k}_{j}$$, you can divide by $${k}_{j}$$, which is equivalent to multiplying by $$1/{k}_{j}$$.

Each expression $${R}_{n}$$ 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; prob6 = optimproblem(Objective=R1 + R2 + R3); solver6 = solvers(prob6)

solver6 = "lsqnonlin"