Documentation

lsqnonneg

Solve nonnegative linear least-squares problem

Solve nonnegative least-squares curve fitting problems of the form

min x C x d 2 2 , where x 0.

Syntax

x = lsqnonneg(C,d)
x = lsqnonneg(C,d,options)
x = lsqnonneg(problem)
[x, resnorm残留]= lsqnonneg(___)
[x,resnorm,residual,exitflag,output] = lsqnonneg(___)
[x,resnorm,residual,exitflag,output,lambda] = lsqnonneg(___)

Description

example

x= lsqnonneg(C,d)returns the vectorxthat minimizesnorm(C*x-d)subject tox ≥ 0. ArgumentsCanddmust be real.

example

x= lsqnonneg(C,d,options)minimizes with the optimization options specified in the structureoptions. Useoptimsetto set these options.

x= lsqnonneg(problem)finds the minimum forproblem, whereproblemis a structure. Create theproblemargument by exporting a problem from Optimization app, as described inExporting Your Work.

example

[x,resnorm,residual] = lsqnonneg(___), for any previous syntax, additionally returns the value of the squared 2-norm of the residual,norm(C*x-d)^2, and returns the residuald-C*x.

[x,resnorm,residual,exitflag,output] = lsqnonneg(___)additionally returns a valueexitflagthat describes the exit condition oflsqnonneg, and a structureoutput与信息;n about the optimization process.

example

[x,resnorm,residual,exitflag,output,lambda] = lsqnonneg(___)additionally returns the Lagrange multiplier vectorlambda.

Examples

collapse all

Compute a nonnegative solution to a linear least-squares problem, and how to compare the result to the solution of an unconstrained problem.

Prepare aCmatrix anddvector for the problem.

C = [0.0372 0.2869 0.6861 0.7071 0.6233 0.6245 0.6344 0.6170]; d = [0.8587 0.1781 0.0747 0.8405];

Compute the constrained and unconstrained solutions.

x = lsqnonneg(C,d)
x =0 0.6929
xunc = C\d
xunc =-2.5627 3.1108

All entries inxare nonnegative, but some entries inxuncare negative.

Compute the norms of the residuals for the two solutions.

constrained_norm = norm(C*x - d)
constrained_norm = 0.9118
unconstrained_norm = norm(C*xunc - d)
unconstrained_norm = 0.6674

The unconstrained solution has a smaller residual norm because constraints can only increase a residual norm.

Set theDisplayoption to'final'to see output whenlsqnonnegfinishes.

Create the options.

options = optimset('Display','final');

Prepare aCmatrix anddvector for the problem.

C = [0.0372 0.2869 0.6861 0.7071 0.6233 0.6245 0.6344 0.6170]; d = [0.8587 0.1781 0.0747 0.8405];

Calllsqnonnegwith the options structure.

x = lsqnonneg(C,d,options);
Optimization terminated.

Calllsqnonnegwith outputs to obtain the solution, residual norm, and residual vector.

Prepare aCmatrix anddvector for the problem.

C = [0.0372 0.2869 0.6861 0.7071 0.6233 0.6245 0.6344 0.6170]; d = [0.8587 0.1781 0.0747 0.8405];

Obtain the solution and residual information.

[x, resnorm残留]= lsqnonneg(C,d)
x =0 0.6929
resnorm = 0.8315
residual =0.6599 -0.3119 -0.3580 0.4130

Verify that the returned residual norm is the square of the norm of the returned residual vector.

norm(residual)^2
ans = 0.8315

Request all output arguments to examine the solution and solution process afterlsqnonnegfinishes.

Prepare aCmatrix anddvector for the problem.

C = [0.0372 0.2869 0.6861 0.7071 0.6233 0.6245 0.6344 0.6170]; d = [0.8587 0.1781 0.0747 0.8405];

Solve the problem, requesting all output arguments.

[x,resnorm,residual,exitflag,output,lambda] = lsqnonneg(C,d)
x =0 0.6929
resnorm = 0.8315
residual =0.6599 -0.3119 -0.3580 0.4130
exitflag = 1
output =struct with fields:iterations: 1 algorithm: 'active-set' message: 'Optimization terminated.'
lambda =-0.1506 -0.0000

exitflagis1, indicating a correct solution.

x(1) = 0, and the correspondinglambda(1)0, showing the correct duality. Similarly,x(2) > 0, and the correspondinglambda(2) = 0.

Input Arguments

collapse all

Linear multiplier, specified as a real matrix. Represents the variableCin the problem

min x C x d 2 2 .

For compatibility, the number of rows ofCmust equal the length ofd.

Example:C = [1,2;3,-1;-4,4]

Data Types:double

Additive term, specified as a real vector. Represents the variabledin the problem

min x C x d 2 2 .

For compatibility, the length ofdmust equal the number of rows ofC.

Example:d = [1;-6;5]

Data Types:double

Optimization options, specified as a structure such asoptimsetreturns. You can useoptimsetto set or change the values of these fields in the options structure. SeeOptimization Options Referencefor detailed information.

Display

Level of display:

  • 'notify'(default) displays output only if the function does not converge.

  • 'off'or'none'displays no output.

  • 'final'displays just the final output.

TolX

Termination tolerance onx, a positive scalar. The default is10*eps*norm(C,1)*length(C). SeeTolerances and Stopping Criteria.

Example:options = optimset('Display','final')

Data Types:struct

Problem structure, specified as a structure with the following fields.

Field Name Entry

C

Real matrix

d

Real vector

solver

'lsqnonneg'

options

Options structure such as returned byoptimset

The simplest way to obtain aproblemstructure is to export the problem from the Optimization app.

Data Types:struct

Output Arguments

collapse all

Solution, returned as a real vector. The length ofxis the same as the length ofd.

Squared residual norm, returned as a nonnegative scalar. Equal tonorm(C*x-d)^2.

Residual, returned as a real vector. The residual isd - C*x.

Reasonlsqnonnegstopped, returned as an integer.

1

Function converged to a solutionx.

0

Number of iterations exceededoptions.MaxIter.

Information about the optimization process, returned as a structure with fields:

iterations

Number of iterations taken

algorithm

'active-set'

message

Exit message

Lagrange multipliers, returned as a real vector. The entries satisfy the complementarity conditionx'*lambda = 0. This meanslambda(i) < 0whenx(i)is approximately0, andlambda(i)is approximately0whenx(i) > 0.

Tips

  • For problems wheredhas length over 20,lsqlinmight be faster thanlsqnonneg. Whendhas length under 20,lsqnonnegis generally more efficient.

    To convert between the solvers whenChas more rows than columns (meaning the system is overdetermined),

    [x,resnorm,residual,exitflag,output,lambda] = lsqnonneg(C,d)

    相当于

    [m,n] = size(C); [x,resnorm,residual,exitflag,output,lambda_lsqlin] = ... lsqlin(C,d,-eye(n,n),zeros(n,1));

    The only difference is that the corresponding Lagrange multipliers have opposite signs:lambda = -lambda_lsqlin.ineqlin.

Algorithms

lsqnonneguses the algorithm described in[1]. The algorithm starts with a set of possible basis vectors and computes the associated dual vectorlambda. It then selects the basis vector corresponding to the maximum value inlambdato swap it out of the basis in exchange for another possible candidate. This continues untillambda ≤ 0.

References

[1] Lawson, C. L. and R. J. Hanson.Solving Least-Squares Problems. Upper Saddle River, NJ: Prentice Hall. 1974. Chapter 23, p. 161.

Extended Capabilities

Introduced before R2006a

Was this topic helpful?