% Solution proposal for Exercise 4, task 1a).
% Solving a constrained QP problem with the Active set method

% Active set method:
%  1. if the corresponding Lagrangian multiplier, li <0, then the 
%  ith inequality, ai'*u \leq bi, is inactive. Hence, put li=0.
%  2. if the langrange multiplier is positive, li>=0, then the inequality
%  is active.
%  3. Store all, m, Langrange multipliers, li for i=1,2,..,m in a vector.
%  and compute the solution.

% Task a)
% Given matrices in QP problem with inequality constraints
H=[3,1;1,2]; f=[-1;-0.5];
A=[1,0;-1,0]; b=[0.2;0];

% 1) Checking the first inequality, it it is actice ore not.
a1=A(1,:)'; b1=b(1);
l_1=-(b1+a1'*inv(H)*f)/(a1'*inv(H)*a1)
if l_1 <=0
   l_1=0;
end


% 2)  Cheking the 2nd inequality, if it is actice ore not.
a2=A(2,:)'; b2=b(2);
l_2=-(b2+a2'*inv(H)*f)/(a2'*inv(H)*a2)
if l_2 <=0
   l_2=0;
end

% The solution to the constrained QP problem.
l=[l_1;l_2]
u=-inv(H)*(f+A'*l)

% Cecking the results with the MATLAB function quadprog.
u_qp=quadprog(H,f,A,b)