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 examplex = optimvar('x',10); obj = sum((x - t).^2);% Explicit sum of squaresprob = optimproblem("Objective",obj);% Check to see the default solveropts = optimoptions(prob)
opts = lsqlin options: ...

Equivalently, write the objective as a squared norm.

methoda(型)^ 2 =标准;prob2 = optimproblem("Objective",obj2);% Check to see the default solveropts = 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 squaresprob3 = optimproblem("Objective",obj3);% Check to see the default solveropts = 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 exampleA = 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 squaresprob4 = optimproblem("Objective",obj4);% Check to see the default solveropts = optimoptions(prob4)
opts = lsqnonlin options: ...

Equivalently, write the objective as a squared norm.

obj5 = norm(expr - ydata)^2;% norm squaredprob5 = optimproblem("Objective",obj5);% Check to see the default solveropts = 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 expressionsRnof this form:

R n = a n + k 1 ( k 2 ( k 3 ( ... k j e n 2 ) ) )

  • enis any expression. If multidimensional,enshould be squared term-by-term using.^2.

  • anis a scalar numeric value.

  • Thekjare positive scalar numeric values.

Each expressionRnmust 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 exampleu = randn(10,2);% Data for examplea = randn;% Coefficientk = 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