Main Content

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}$$

**Note**

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

finds the minimum for `x`

= lsqlin(`problem`

)`problem`

, a structure described in `problem`

. Create the `problem`

structure using dot notation
or the `struct`

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

`[`

starts `wsout`

,`resnorm`

,`residual`

,`exitflag`

,`output`

,`lambda`

]
= lsqlin(`C`

,`d`

,`A`

,`b`

,`Aeq`

,`beq`

,`lb`

,`ub`

,`ws`

)`lsqlin`

from the data in the warm start object
`ws`

, using the options in `ws`

. The returned argument
`wsout`

contains the solution point in `wsout.X`

. By
using `wsout`

as the initial warm start object in a subsequent solver
call, `lsqlin`

can work faster.

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.If your problem has many linear constraints and few variables, try using the

`'active-set'`

algorithm. See Quadratic Programming with Many Linear Constraints.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.

The **Optimize** Live Editor task provides a visual interface for `lsqlin`

.

`mldivide`

| `lsqnonneg`

| `quadprog`

| Optimize | `optimwarmstart`