function x=NullSpaceMethod(A,C,b,d);
% NULLSPACEMETHOD solves a constrained least squares problem
%   x=NullSpaceMethod(A,C,b,d) solves the constrained least squares
%   problem ||Ax-b||=min s.t. Cx=d using the nullspace method.

[p n]=size(C);
[Q R]=qr(C');
y1=R(1:p,1:p)'\d;
x1=Q(:,1:p)*y1;
y2=(A*Q(:,p+1:n))\(b-A*x1);
x=x1+Q(:,p+1:n)*y2;