www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/NonnegativeLinearLeastSquaresExample.m

    %% Nonnegative Linear Least Squares
% Compute a nonnegative solution to a linear least-squares problem, and how
% to compare the result to the solution of an unconstrained problem.
%%
% Prepare a |C| matrix and |d| vector for the problem |min ||C*x - d|| |.
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)
%%
xunc = C\d
%%
% All entries in |x| are nonnegative, but some entries in |xunc| are
% negative.
%
% Compute the norms of the residuals for the two solutions.
constrained_norm = norm(C*x - d)
%%
unconstrained_norm = norm(C*xunc - d)
%%
% The unconstrained solution has a smaller residual norm because
% constraints can only increase a residual norm.