function [u,l,L]=qpsol1(H,f,A,b)
% QPSOL1 
% Syntax: [u,l,L]=qpsol1(H,f,A,b)
% Purpose: Illustration of an active set procedure for solving
% the QP problem: Minimize:        J(u)= u^T H u + 2 f^T,
% with respect to (subject to):    A u <= b.
% On input:
% H,f,A,b  - matrices for the above QP-problem.
% On output:
% u - The solution to the QP problem.
% l - m-dimensional vector of lagrange multipliers. Multipliers equal to zero
%     means that the corresponding inequality is inactive.
% L - m-dimensional vector of Lagrange mult., also with the negative values.
% Author: DDIR, 19.03.2001
[m,n]=size(A);       % m=number of inequalities, n=number of parameters in u.
l=zeros(m,1);L=l;    % Vector to store Lagrange multipliers for const. prob.      
for i=1:m
   A_i=A(i,:);       % 1. Get vectors for the i-th inequality.
   b_i=b(i);
   M=[H A_i';A_i 0]; % 2. Treat the i-th inequality as active and solve for 
   B=[-f;b_i];       % the Lagrange multiplier, li=lambda_i.
   x=inv(M)*B;
   l_i=x(n+1);       % The i-th Lagrange multiplier.
   L(i)=l_i;
   if l_i < 0;       % 3. Set negative Lagrange multipliers equal to zero.
      l_i=0; 
   end
   l(i)=l_i;   
end
u=-inv(H)*(f+A'*l);  % The minimizing solution.
%%% END QPSOL1
   
   