%% Find an extremum of a linear equality-constrained quadratic function (aka QP problem)
% Find an extremum (minimum or maximum) of the function
% f(\mathrm x)  = x_1+x_2^2+x_2x_3+2x_3^2
% subject to x_1+x_2+x_3=1.
% This is called a quadratic programming (QP) problem.
%% Finding the matrices for the QP formulation
% We consider the standard QP problem formulation
% minimize \frac{1}{2}\mathrm x^T\mathrm Q\mathrm x + \mathrm r^T\mathrm x
% subject to Ax = b

Q = [0 0 0; 0 2 1; 0 1 4]
r = [1; 0; 0]
A = [1 1 1]
b = 1

%
%% The KKT linear system (Lagrange multiplier approach)

K = [Q A'; A 0]
c = [-r; b]
tic, xd = K\c, toc
xopt = xd(1:3)
dopt = xd(4)

%
%% Sufficiency (projected Hessian)
% Strictly speaking redundant here because there are no other higher order terms in the cost function

eig(Q)

%

Z = null(A)
Z'*Q*Z
eig(ans)

%
% We can also compute the value of the cost function at the stationary point

fopt = 1/2*xopt'*Q*xopt + r'*xopt

%
%% Using quadprog from Optimization Toolbox for Matlab
% See https://www.mathworks.com/help/optim/ug/quadprog.html

tic, xqp = quadprog(Q,r,[],[],A,b), toc

%
%% Using CVX
% Dowload from http://cvxr.com/cvx/download/

cvx_begin
    variable x(3)
    minimize( 1/2*quad_form(x,Q)+r'*x )
    subject to
        A*x == b
cvx_end
xcvx = x