Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Solve constrained linear least-squares problems

Linear least-squares solver with bounds or linear constraints.

Solves least-squares curve fitting problems of the form

$$\underset{x}{\mathrm{min}}\frac{1}{2}{\Vert C\cdot x-d\Vert}_{2}^{2}\text{suchthat}\{\begin{array}{c}A\cdot x\le b,\\ Aeq\cdot x=beq,\\ lb\le x\le ub.\end{array}$$

`lsqlin`

applies only to the solver-based approach. For a discussion
of the two optimization approaches, see First Choose Problem-Based or Solver-Based Approach.

`x = lsqlin(C,d,A,b)`

`x = lsqlin(C,d,A,b,Aeq,beq,lb,ub)`

`x = lsqlin(C,d,A,b,Aeq,beq,lb,ub,x0,options)`

`x = lsqlin(problem)`

```
[x,resnorm,residual,exitflag,output,lambda]
= lsqlin(___)
```

finds the
minimum for `x`

= lsqlin(`problem`

)`problem`

, where `problem`

is a structure.
Create the `problem`

structure by exporting a problem from Optimization
app, as described in Exporting Your Work. Or create a
`problem`

structure from an `OptimizationProblem`

object
by using `prob2struct`

.

`[`

, for any input arguments described
above, returns:`x`

,`resnorm`

,`residual`

,`exitflag`

,`output`

,`lambda`

]
= lsqlin(___)

The squared 2-norm of the residual

`resnorm =`

$${\Vert C\cdot x-d\Vert}_{2}^{2}$$The residual

`residual = C*x - d`

A value

`exitflag`

describing the exit conditionA structure

`output`

containing information about the optimization processA structure

`lambda`

containing the Lagrange multipliersThe factor ½ in the definition of the problem affects the values in the

`lambda`

structure.

For problems with no constraints, you can use

`mldivide`

(matrix left division). When you have no constraints,`lsqlin`

returns`x = C\d`

.Because the problem being solved is always convex,

`lsqlin`

finds a global, although not necessarily unique, solution.Better numerical results are likely if you specify equalities explicitly, using

`Aeq`

and`beq`

, instead of implicitly, using`lb`

and`ub`

.The

`trust-region-reflective`

algorithm does not allow equal upper and lower bounds. Use another algorithm for this case.If the specified input bounds for a problem are inconsistent, the output

`x`

is`x0`

and the outputs`resnorm`

and`residual`

are`[]`

.You can solve some large structured problems, including those where the

`C`

matrix is too large to fit in memory, using the`trust-region-reflective`

algorithm with a Jacobian multiply function. For information, see trust-region-reflective Algorithm Options.