%% Quadratic Program with Linear Equality Constraint
% Find the minimum of
% f(x) =
% \frac{1}{2}x_1^2 + x_2^2  - x_1x_2 -2x_1 -6x_2
% subject to the constraint
% x_1 + x_2 = 0.
% In quadprog syntax, this problem is to minimize
% f(x) = \frac{1}{2} x^T Hx + f^T x,
% where
% \begin{array}{l}
% H=\left\lbrack \begin{array}{cc}
% 1 & -1\\
% -1 & 2
% \end{array}\right\rbrack \\
% f=\left\lbrack \begin{array}{c}
% -2\\
% -6
% \end{array}\right\rbrack ,
% \end{array}
% subject to the linear constraint.
% To solve this problem, first enter the coefficient matrices.

H = [1 -1; -1 2]; 
f = [-2; -6];
Aeq = [1 1];
beq = 0;

% Call quadprog, entering [] for the inputs A and b.

[x,fval,exitflag,output,lambda] = ...
   quadprog(H,f,[],[],Aeq,beq);

% Examine the final point, function value, and exit flag.

x,fval,exitflag

% An exit flag of 1 means the result is a local minimum. Because H is a positive definite matrix, this problem is convex, so the minimum is a global minimum.
% Confirm that H is positive definite by checking its eigenvalues.

eig(H)

% Copyright 2018 The MathWorks, Inc.