%% Solve Quadratic Programming Problem Using Active-Set Solver
% Find the values of x that minimize
% f\left( x \right) = 0.5x_1^2 + x_2^2 - {x_1}{x_2} - 2{x_1} - 6{x_2},
% subject to the constraints
% \begin{array}{l}
% {x_1} \ge 0\\
% {x_2} \ge 0\\
% {x_1} + {x_2} \le 2\\
%  - {x_1} + 2{x_2} \le 2\\
% 2{x_1} + {x_2} \le 3.
% \end{array}
%
% Specify the Hessian matrix and linear multiplier vector for the objective function.

H = [1 -1; -1 2];
f = [-2; -6];

%
% Specify the inequality constraint parameters.

A = [-1 0; 0 -1; 1 1; -1 2; 2 1];
b = [0; 0; 2; 2; 3];

%
% Define Aeq and beq to indicate that there are no equality constraints.

n = length(f);
Aeq = zeros(0,n);
beq = zeros(0,1);

%
% Create a default option set for mpcActiveSetSolver.

opt = mpcActiveSetOptions;

%
% To cold start the solver, define all inequality constraints as inactive.

iA0 = false(size(b));

%
% Solve the QP problem.

[x,exitflag] = mpcActiveSetSolver(H,f,A,b,Aeq,beq,iA0,opt);

%
% Examine the solution x.

x

%
% When solving the QP problem, you can also determine which inequality constraints are active for the solution.

[x,exitflag,iA,lambda] = mpcActiveSetSolver(H,f,A,b,Aeq,beq,iA0,opt);

%
% Check the active inequality constraints. An active inequality constraint is at equality for the optimal solution.

iA

%
% There is a single active inequality constraint. View the Lagrange multiplier for this constraint.

lambda.ineqlin(1)

% Copyright 2015-2019 The MathWorks, Inc.