lsqlin
Solve constrained linear leastsquares problems
Syntax
Description
Linear leastsquares solver with bounds or linear constraints.
Solves leastsquares curve fitting problems of the form
$$\underset{x}{\mathrm{min}}\frac{1}{2}{\Vert C\cdot xd\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 solverbased approach. For a discussion
of the two optimization approaches, see First Choose ProblemBased or SolverBased Approach.
minimizes with an initial point x
= lsqlin(C
,d
,A
,b
,Aeq
,beq
,lb
,ub
,x0
,options
)x0
and the optimization options
specified in options
. Use optimoptions
to set these options. If you do not want to include an initial
point, set the x0
argument to []
; however, a
nonempty x0
is required for the 'activeset'
algorithm.
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 2norm of the residual
resnorm =
$${\Vert C\cdot xd\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.
Examples
Least Squares with Linear Inequality Constraints
Find the x
that minimizes the norm of C*x  d
for an overdetermined problem with linear inequality constraints.
Specify the problem and constraints.
C = [0.9501 0.7620 0.6153 0.4057 0.2311 0.4564 0.7919 0.9354 0.6068 0.0185 0.9218 0.9169 0.4859 0.8214 0.7382 0.4102 0.8912 0.4447 0.1762 0.8936]; d = [0.0578 0.3528 0.8131 0.0098 0.1388]; A = [0.2027 0.2721 0.7467 0.4659 0.1987 0.1988 0.4450 0.4186 0.6037 0.0152 0.9318 0.8462]; b = [0.5251 0.2026 0.6721];
Call lsqlin
to solve the problem.
x = lsqlin(C,d,A,b)
Minimum found that satisfies the constraints. Optimization completed because the objective function is nondecreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
x = 4×1
0.1299
0.5757
0.4251
0.2438
Least Squares with Linear Constraints and Bounds
Find the x
that minimizes the norm of C*x  d
for an overdetermined problem with linear equality and inequality constraints and bounds.
Specify the problem and constraints.
C = [0.9501 0.7620 0.6153 0.4057 0.2311 0.4564 0.7919 0.9354 0.6068 0.0185 0.9218 0.9169 0.4859 0.8214 0.7382 0.4102 0.8912 0.4447 0.1762 0.8936]; d = [0.0578 0.3528 0.8131 0.0098 0.1388]; A =[0.2027 0.2721 0.7467 0.4659 0.1987 0.1988 0.4450 0.4186 0.6037 0.0152 0.9318 0.8462]; b =[0.5251 0.2026 0.6721]; Aeq = [3 5 7 9]; beq = 4; lb = 0.1*ones(4,1); ub = 2*ones(4,1);
Call lsqlin
to solve the problem.
x = lsqlin(C,d,A,b,Aeq,beq,lb,ub)
Minimum found that satisfies the constraints. Optimization completed because the objective function is nondecreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
x = 4×1
0.1000
0.1000
0.1599
0.4090
Linear Least Squares with Nondefault Options
This example shows how to use nondefault options for linear least squares.
Set options to use the 'interiorpoint'
algorithm and to give iterative display.
options = optimoptions('lsqlin','Algorithm','interiorpoint','Display','iter');
Set up a linear leastsquares problem.
C = [0.9501 0.7620 0.6153 0.4057 0.2311 0.4564 0.7919 0.9354 0.6068 0.0185 0.9218 0.9169 0.4859 0.8214 0.7382 0.4102 0.8912 0.4447 0.1762 0.8936]; d = [0.0578 0.3528 0.8131 0.0098 0.1388]; A = [0.2027 0.2721 0.7467 0.4659 0.1987 0.1988 0.4450 0.4186 0.6037 0.0152 0.9318 0.8462]; b = [0.5251 0.2026 0.6721];
Run the problem.
x = lsqlin(C,d,A,b,[],[],[],[],[],options)
Iter Resnorm Primal Infeas Dual Infeas Complementarity 0 6.545534e01 1.600492e+00 6.150431e01 1.000000e+00 1 6.545534e01 8.002458e04 3.075216e04 2.430833e01 2 1.757343e01 4.001229e07 1.537608e07 5.945636e02 3 5.619277e02 2.000615e10 2.036997e08 1.370933e02 4 2.587604e02 9.994783e14 1.006816e08 2.548273e03 5 1.868939e02 2.775558e17 2.955102e09 4.295807e04 6 1.764630e02 2.775558e17 1.237758e09 3.102850e05 7 1.758561e02 2.498002e16 1.645864e10 1.138719e07 8 1.758538e02 2.498002e16 2.399331e13 5.693290e11 Minimum found that satisfies the constraints. Optimization completed because the objective function is nondecreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
x = 4×1
0.1299
0.5757
0.4251
0.2438
Return All Outputs
Obtain and interpret all lsqlin
outputs.
Define a problem with linear inequality constraints and bounds. The problem is overdetermined because there are four columns in the C
matrix but five rows. This means the problem has four unknowns and five conditions, even before including the linear constraints and bounds.
C = [0.9501 0.7620 0.6153 0.4057 0.2311 0.4564 0.7919 0.9354 0.6068 0.0185 0.9218 0.9169 0.4859 0.8214 0.7382 0.4102 0.8912 0.4447 0.1762 0.8936]; d = [0.0578 0.3528 0.8131 0.0098 0.1388]; A = [0.2027 0.2721 0.7467 0.4659 0.1987 0.1988 0.4450 0.4186 0.6037 0.0152 0.9318 0.8462]; b = [0.5251 0.2026 0.6721]; lb = 0.1*ones(4,1); ub = 2*ones(4,1);
Set options to use the 'interiorpoint'
algorithm.
options = optimoptions('lsqlin','Algorithm','interiorpoint');
The 'interiorpoint'
algorithm does not use an initial point, so set x0
to []
.
x0 = [];
Call lsqlin
with all outputs.
[x,resnorm,residual,exitflag,output,lambda] = ...
lsqlin(C,d,A,b,[],[],lb,ub,x0,options)
Minimum found that satisfies the constraints. Optimization completed because the objective function is nondecreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
x = 4×1
0.1000
0.1000
0.2152
0.3502
resnorm = 0.1672
residual = 5×1
0.0455
0.0764
0.3562
0.1620
0.0784
exitflag = 1
output = struct with fields:
message: 'Minimum found that satisfies the constraints....'
algorithm: 'interiorpoint'
firstorderopt: 4.3374e11
constrviolation: 0
iterations: 6
linearsolver: 'dense'
cgiterations: []
lambda = struct with fields:
ineqlin: [3x1 double]
eqlin: [0x1 double]
lower: [4x1 double]
upper: [4x1 double]
Examine the nonzero Lagrange multiplier fields in more detail. First examine the Lagrange multipliers for the linear inequality constraint.
lambda.ineqlin
ans = 3×1
0.0000
0.2392
0.0000
Lagrange multipliers are nonzero exactly when the solution is on the corresponding constraint boundary. In other words, Lagrange multipliers are nonzero when the corresponding constraint is active. lambda.ineqlin(2)
is nonzero. This means that the second element in A*x
should equal the second element in b
, because the constraint is active.
[A(2,:)*x,b(2)]
ans = 1×2
0.2026 0.2026
Now examine the Lagrange multipliers for the lower and upper bound constraints.
lambda.lower
ans = 4×1
0.0409
0.2784
0.0000
0.0000
lambda.upper
ans = 4×1
0
0
0
0
The first two elements of lambda.lower
are nonzero. You see that x(1)
and x(2)
are at their lower bounds, 0.1
. All elements of lambda.upper
are essentially zero, and you see that all components of x
are less than their upper bound, 2
.
Return Warm Start Object
Create a warm start object so you can solve a modified problem quickly. Set options to turn off iterative display to support warm start.
rng default % For reproducibility options = optimoptions('lsqlin','Algorithm','activeset','Display','off'); n = 15; x0 = 5*rand(n,1); ws = optimwarmstart(x0,options);
Create and solve the first problem. Find the solution time.
r = 1:n1; % Index for making vectors v(n) = (1)^(n+1)/n; % Allocating the vector v v(r) =( 1).^(r+1)./r; C = gallery('circul',v); C = [C;C]; r = 1:2*n; d(r) = nr; lb = 5*ones(1,n); ub = 5*ones(1,n); tic [ws,fval,~,exitflag,output] = lsqlin(C,d,[],[],[],[],lb,ub,ws) toc
Elapsed time is 0.005117 seconds.
Add a linear constraint and solve again.
A = ones(1,n); b = 10; tic [ws,fval,~,exitflag,output] = lsqlin(C,d,A,b,[],[],lb,ub,ws) toc
Elapsed time is 0.001491 seconds.
Input Arguments
C
— Multiplier matrix
real matrix
Multiplier matrix, specified as a matrix of doubles. C
represents
the multiplier of the solution x
in the expression C*x 
d
. C
is M
byN
,
where M
is the number of equations, and N
is the
number of elements of x
.
Example: C = [1,4;2,5;7,8]
Data Types: single
 double
d
— Constant vector
real vector
Constant vector, specified as a vector of doubles. d
represents
the additive constant term in the expression C*x  d
.
d
is M
by1
, where
M
is the number of equations.
Example: d = [5;0;12]
Data Types: single
 double
A
— Linear inequality constraints
real matrix
Linear inequality constraints, specified as a real matrix. A
is
an M
byN
matrix, where M
is
the number of inequalities, and N
is the number
of variables (number of elements in x0
). For
large problems, pass A
as a sparse matrix.
A
encodes the M
linear
inequalities
A*x <= b
,
where x
is the column vector of N
variables x(:)
,
and b
is a column vector with M
elements.
For example, consider these inequalities:
x_{1} + 2x_{2} ≤
10
3x_{1} +
4x_{2} ≤ 20
5x_{1} +
6x_{2} ≤ 30,
Specify the inequalities by entering the following constraints.
A = [1,2;3,4;5,6]; b = [10;20;30];
Example: To specify that the x components sum to 1 or less, use A =
ones(1,N)
and b = 1
.
Data Types: single
 double
b
— Linear inequality constraints
real vector
Linear inequality constraints, specified as a real vector. b
is
an M
element vector related to the A
matrix.
If you pass b
as a row vector, solvers internally
convert b
to the column vector b(:)
.
For large problems, pass b
as a sparse vector.
b
encodes the M
linear
inequalities
A*x <= b
,
where x
is the column vector of N
variables x(:)
,
and A
is a matrix of size M
byN
.
For example, consider these inequalities:
x_{1}
+ 2x_{2} ≤
10
3x_{1}
+ 4x_{2} ≤
20
5x_{1}
+ 6x_{2} ≤
30.
Specify the inequalities by entering the following constraints.
A = [1,2;3,4;5,6]; b = [10;20;30];
Example: To specify that the x components sum to 1 or less, use A =
ones(1,N)
and b = 1
.
Data Types: single
 double
Aeq
— Linear equality constraints
real matrix
Linear equality constraints, specified as a real matrix. Aeq
is
an Me
byN
matrix, where Me
is
the number of equalities, and N
is the number of
variables (number of elements in x0
). For large
problems, pass Aeq
as a sparse matrix.
Aeq
encodes the Me
linear
equalities
Aeq*x = beq
,
where x
is the column vector of N
variables x(:)
,
and beq
is a column vector with Me
elements.
For example, consider these inequalities:
x_{1} + 2x_{2} +
3x_{3} = 10
2x_{1} +
4x_{2} + x_{3} =
20,
Specify the inequalities by entering the following constraints.
Aeq = [1,2,3;2,4,1]; beq = [10;20];
Example: To specify that the x components sum to 1, use Aeq = ones(1,N)
and
beq = 1
.
Data Types: single
 double
beq
— Linear equality constraints
real vector
Linear equality constraints, specified as a real vector. beq
is
an Me
element vector related to the Aeq
matrix.
If you pass beq
as a row vector, solvers internally
convert beq
to the column vector beq(:)
.
For large problems, pass beq
as a sparse vector.
beq
encodes the Me
linear
equalities
Aeq*x = beq
,
where x
is the column vector of N
variables
x(:)
, and Aeq
is a matrix of size
Me
byN
.
For example, consider these equalities:
x_{1}
+ 2x_{2} +
3x_{3} =
10
2x_{1}
+ 4x_{2} +
x_{3} =
20.
Specify the equalities by entering the following constraints.
Aeq = [1,2,3;2,4,1]; beq = [10;20];
Example: To specify that the x components sum to 1, use Aeq = ones(1,N)
and
beq = 1
.
Data Types: single
 double
lb
— Lower bounds
[]
(default)  real vector or array
Lower bounds, specified as a vector or array of doubles. lb
represents the lower bounds elementwise in
lb
≤ x
≤ ub
.
Internally, lsqlin
converts an array lb
to
the vector lb(:)
.
Example: lb = [0;Inf;4]
means x(1) ≥ 0
,
x(3) ≥ 4
.
Data Types: single
 double
ub
— Upper bounds
[]
(default)  real vector or array
Upper bounds, specified as a vector or array of doubles. ub
represents the upper bounds elementwise in
lb
≤ x
≤ ub
.
Internally, lsqlin
converts an array ub
to
the vector ub(:)
.
Example: ub = [Inf;4;10]
means x(2) ≤ 4
,
x(3) ≤ 10
.
Data Types: single
 double
x0
— Initial point
zeros(M,1)
where M
is the
number of equations (default)  real vector or array
Initial point for the solution process, specified as a real vector or array.
The
'interiorpoint'
algorithm does not usex0
.x0
is an optional argument for the'trustregionreflective'
algorithm. By default, this algorithm setsx0
to an allzero vector. If any of these components violate the bound constraints,lsqlin
resets the entire defaultx0
to a vector that satisfies the bounds. If any components of a nondefaultx0
violates the bounds,lsqlin
sets those components to satisfy the bounds.A nonempty
x0
is a required argument for the'activeset'
algorithm. If any components ofx0
violates the bounds,lsqlin
sets those components to satisfy the bounds.
Example: x0 = [4;3]
Data Types: single
 double
options
— Options for lsqlin
options created using optimoptions
 structure such as created by optimset
Options for lsqlin
, specified as the output of the optimoptions
function or as a structure such as created by
optimset
.
Some options are absent from the
optimoptions
display. These options appear in italics in the following
table. For details, see View Optimization Options.
All Algorithms
 Choose the algorithm:
The The
When the problem has no constraints,
If you have a large number of linear constraints and
not a large number of variables, try the For more information on choosing the algorithm, see Choosing the Algorithm. 
Diagnostics  Display diagnostic information about the function to be minimized or
solved. The choices are 
Display  Level of display returned to the command line.
The

MaxIterations  Maximum number of iterations allowed, a nonnegative integer. The
default value is For 
trustregionreflective
Algorithm Options
FunctionTolerance  Termination tolerance on the function value, a
nonnegative scalar. The default is For 
JacobianMultiplyFcn  Jacobian multiply
function, specified as a function handle. For largescale structured problems,
this function should compute the Jacobian matrix product
W = jmfun(Jinfo,Y,flag) where
In each case, See Jacobian Multiply Function with Linear Least Squares for an example. For 
MaxPCGIter  Maximum number of PCG (preconditioned conjugate
gradient) iterations, a positive scalar. The default is

OptimalityTolerance  Termination tolerance on the firstorder
optimality, a nonnegative scalar. The default is For 
PrecondBandWidth  Upper bandwidth of preconditioner for PCG
(preconditioned conjugate gradient). By default, diagonal preconditioning is
used (upper bandwidth of 0). For some problems, increasing the bandwidth
reduces the number of PCG iterations. Setting

SubproblemAlgorithm  Determines how the iteration step is
calculated. The default, 
TolPCG  Termination tolerance on the PCG
(preconditioned conjugate gradient) iteration, a positive scalar. The default
is 
TypicalX  Typical 
interiorpoint
Algorithm Options
ConstraintTolerance  Tolerance on the constraint violation, a nonnegative
scalar. The default is For

LinearSolver  Type of internal linear solver in algorithm:

OptimalityTolerance  Termination tolerance on the firstorder
optimality, a nonnegative scalar. The default is For 
StepTolerance  Termination tolerance on For

'activeset'
Algorithm Options
ConstraintTolerance  Tolerance on the constraint violation, a positive scalar. The default
value is For 
ObjectiveLimit  A tolerance (stopping criterion) that is a
scalar. If the objective function value goes below

OptimalityTolerance  Termination tolerance on the firstorder
optimality, a positive scalar. The default value is For 
StepTolerance  Termination tolerance on For 
SinglePrecision Code Generation
Algorithm  Must be 
ConstraintTolerance  Tolerance on the constraint violation, a positive scalar. The default value is For 
MaxIterations  Maximum number of iterations allowed, a nonnegative integer. The default value is 
ObjectiveLimit  A tolerance (stopping criterion) that is a scalar. If the objective function value goes below 
OptimalityTolerance  Termination tolerance on the firstorder optimality, a positive scalar. The default value is For 
StepTolerance  Termination tolerance on For 
problem
— Optimization problem
structure
Optimization problem, specified as a structure with the following fields.
 Matrix multiplier in the term C*x 
d 
 Additive constant in the term C*x 
d 
 Matrix for linear inequality constraints 
 Vector for linear inequality constraints 
 Matrix for linear equality constraints 
 Vector for linear equality constraints 
lb  Vector of lower bounds 
ub  Vector of upper bounds 
 Initial point for x 
 'lsqlin' 
 Options created with optimoptions 
Note
You cannot use warm start with the problem
argument.
Data Types: struct
ws
— Warm start object
object created using optimwarmstart
Warm start object, specified as an object created using optimwarmstart
. The warm start object contains the start point and
options, and optional data for memory size in code generation. See Warm Start Best Practices.
Example: ws = optimwarmstart(x0,options)
Output Arguments
x
— Solution
real vector
Solution, returned as a vector that minimizes the norm of C*xd
subject to all bounds and linear constraints.
wsout
— Solution warm start object
LsqlinWarmStart
object
Solution warm start object, returned as a LsqlinWarmStart
object.
The solution point is wsout.X
.
You can use wsout
as the input warm start object in a subsequent
lsqlin
call.
resnorm
— Objective value
real scalar
Objective value, returned as the scalar value
norm(C*xd)^2
.
residual
— Solution residuals
real vector
Solution residuals, returned as the vector C*xd
.
exitflag
— Algorithm stopping condition
integer
Algorithm stopping condition, returned as an integer identifying the reason the
algorithm stopped. The following lists the values of exitflag
and the
corresponding reasons lsqlin
stopped.
 Change in the residual was smaller than the specified
tolerance 
 Step size smaller than

 Function converged to a solution

 Number of iterations exceeded

 The problem is infeasible. Or, for the

3  The problem is unbounded. 
 Illconditioning prevents further optimization. 
 Unable to compute a step direction. 
The exit message for the interiorpoint
algorithm can give more
details on the reason lsqlin
stopped, such as exceeding a
tolerance. See Exit Flags and Exit Messages.
output
— Solution process summary
structure
Solution process summary, returned as a structure containing information about the optimization process.
 Number of iterations the solver took. 
 One of these algorithms:
For an unconstrained problem, 
 Constraint violation that is positive for
violated constraints (not returned for the

 Exit message. 
 Firstorder optimality at the solution. See FirstOrder Optimality Measure. 
linearsolver  Type of internal linear solver,

 Number of conjugate gradient iterations the
solver performed. Nonempty only for the

See Output Structures.
lambda
— Lagrange multipliers
structure
Lagrange multipliers, returned as a structure with the following fields.
 Lower bounds 
 Upper bounds 
 Linear inequalities 
 Linear equalities 
Tips
For problems with no constraints, consider using
mldivide
(matrix left division) orlsqminnorm
. When you have no constraints,lsqlin
returnsx = 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
'activeset'
algorithm. See Quadratic Programming with Many Linear Constraints.Better numerical results are likely if you specify equalities explicitly, using
Aeq
andbeq
, instead of implicitly, usinglb
andub
.The
trustregionreflective
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
isx0
and the outputsresnorm
andresidual
are[]
.You can solve some large structured problems, including those where the
C
matrix is too large to fit in memory, using thetrustregionreflective
algorithm with a Jacobian multiply function. For information, see trustregionreflective Algorithm Options.
Algorithms
TrustRegionReflective Algorithm
This method is a subspace trustregion method based on the interiorreflective Newton method described in [1]. Each iteration involves the approximate solution of a large linear system using the method of preconditioned conjugate gradients (PCG). See TrustRegionReflective Least Squares, and in particular Large Scale Linear Least Squares.
InteriorPoint Algorithm
The 'interiorpoint'
algorithm is based on the
quadprog
'interiorpointconvex'
algorithm. See Linear Least Squares: InteriorPoint or ActiveSet.
ActiveSet Algorithm
The 'activeset'
algorithm is based on the
quadprog
'activeset'
algorithm. For more information, see Linear Least Squares: InteriorPoint or ActiveSet and activeset quadprog Algorithm.
Unconstrained Problems
For unconstrained problems, lsqlin
performs a decomposition of the
C
coefficient matrix using decomposition
.
When lsqlin
detects that the C
matrix is
illconditioned, lsqlin
uses QR decomposition to solve the
problem.
References
[1] Coleman, T. F. and Y. Li. “A Reflective Newton Method for Minimizing a Quadratic Function Subject to Bounds on Some of the Variables,” SIAM Journal on Optimization, Vol. 6, Number 4, pp. 1040–1058, 1996.
[2] Gill, P. E., W. Murray, and M. H. Wright. Practical Optimization, Academic Press, London, UK, 1981.
Warm Start
A warm start object maintains a list of active constraints from the previous solved problem. The solver carries over as much active constraint information as possible to solve the current problem. If the previous problem is too different from the current one, no active set information is reused. In this case, the solver effectively executes a cold start in order to rebuild the list of active constraints.
Alternative Functionality
App
The Optimize Live Editor task provides a visual interface for lsqlin
.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
lsqlin
supports code generation using either thecodegen
(MATLAB Coder) function or the MATLAB^{®} Coder™ app. You must have a MATLAB Coder license to generate code.The target hardware must support standard doubleprecision floatingpoint computations or standard singleprecision floatingpoint computations.
Code generation targets do not use the same math kernel libraries as MATLAB solvers. Therefore, code generation solutions can vary from solver solutions, especially for poorly conditioned problems.
When solving unconstrained and underdetermined problems in MATLAB,
lsqlin
callsmldivide
, which returns a basic solution. In code generation, the returned solution has minimum norm, which usually differs.lsqlin
does not support theproblem
argument for code generation.[x,fval] = lsqlin(problem) % Not supported
All
lsqlin
input matrices such asA
,Aeq
,lb
, andub
must be full, not sparse. You can convert sparse matrices to full by using thefull
function.The
lb
andub
arguments must have the same number of entries as the number of columns inC
or must be empty[]
.If your target hardware does not support infinite bounds, use
optim.coder.infbound
.For advanced code optimization involving embedded processors, you also need an Embedded Coder^{®} license.
You must include options for
lsqlin
and specify them usingoptimoptions
. The options must include theAlgorithm
option, set to'activeset'
.options = optimoptions('lsqlin','Algorithm','activeset'); [x,fval,exitflag] = lsqlin(C,d,A,b,Aeq,beq,lb,ub,x0,options);
Code generation supports these options:
Algorithm
— Must be'activeset'
ConstraintTolerance
MaxIterations
ObjectiveLimit
OptimalityTolerance
StepTolerance
Generated code has limited error checking for options. The recommended way to update an option is to use
optimoptions
, not dot notation.opts = optimoptions('lsqlin','Algorithm','activeset'); opts = optimoptions(opts,'MaxIterations',1e4); % Recommended opts.MaxIterations = 1e4; % Not recommended
Do not load options from a file. Doing so can cause code generation to fail. Instead, create options in your code.
If you specify an option that is not supported, the option is typically ignored during code generation. For reliable results, specify only supported options.
Version History
Introduced before R2006aR2024b: Generate singleprecision code
You can generate code using lsqlin
for singleprecision floating
point hardware. For instructions, see SinglePrecision Code Generation.
R2024a: More robust handling of unconstrained problems
The solver now takes extra steps when solving an illconditioned unconstrained problem
with a square input matrix C
(the number of rows equals the number of
columns). The results can have improved accuracy and the likelihood of obtaining an
Inf
or NaN
result is decreased.
For unconstrained problems, the output.algorithm
field is now
'direct'
instead of the previous 'mldivide'
.
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
 América Latina (Español)
 Canada (English)
 United States (English)
Europe
 Belgium (English)
 Denmark (English)
 Deutschland (Deutsch)
 España (Español)
 Finland (English)
 France (Français)
 Ireland (English)
 Italia (Italiano)
 Luxembourg (English)
 Netherlands (English)
 Norway (English)
 Österreich (Deutsch)
 Portugal (English)
 Sweden (English)
 Switzerland
 United Kingdom (English)