lsqnonneg

Least squares with non-negativity constraints.

Category: Mathematics > Linear Algebra

Syntax

x = lsqnonneg(C, d)

Minimizes norm(C*x - d) subject to x >= 0.

[x, resnorm] = lsqnonneg(C, d)

Also returns the squared residual norm.

Description

Unconstrained least squares may give negative coefficients, which have no meaning for quantities like concentrations or amounts. lsqnonneg uses an active-set method: it keeps a set of variables free and holds the rest at zero, moving variables between the sets until the optimality conditions hold.

The solution is unique when C has full column rank.

Input arguments

C
Matrix of the system.
d
Target vector.

Output arguments

x
Non-negative solution vector.
resnorm
norm(C*x - d)^2.

Example

A coefficient pinned at zero

C = [1 0; 0 1; 1 1];
d = [1; -1; 0.5];
[x, r] = lsqnonneg(C, d)
Output
x = 2×1

    0.7500
         0
r = 1.1250

See also

lscov mldivide lsqminnorm fminsearch