%% Convert Quadratic Programming Problem to Second-Order Cone Program
% This example show how to convert a positive semidefinite quadratic programming problem to the second-order cone form used by the coneprog solver. A quadratic programming problem has the form
% \min_x \frac{1}{2}x^THx + f^Tx,
% possibly subject to bounds and linear constraints. coneprog solves problems in the form
% \min_x f^Tx
% such that
% \|A_{\rm sc}x - b_{\rm sc}\| \le d_{\rm sc}x -\gamma,
% possibly subject to bounds and linear constraints.
% To convert a quadratic program to coneprog form, first calculate the matrix square root of the matrix H. Assuming that H is a symmetric positive semidefinite matrix, the command
% A = sqrtm(H);
% returns a positive semidefinite matrix A such that A'*A = A*A = H. Therefore,
% x^THx = x^TA^TAx = (Ax)^TAx =\|Ax\|^2.
% Modify the form of the quadratic program as follows:
% \min_x \frac{1}{2}x^THx + f^Tx = -\frac{1}{2} + \min_{x,t}\left [(t+1/2)+f^Tx\right ]
% where t satisfies the constraint
% t+1/2\ge \frac{1}{2}x^THx.
% Extend the control variable x to u, which includes t as its last element:
% u=\left\lbrack \begin{array}{c}
% x\\
% t
% \end{array}\right\rbrack.
% Extend the second-order cone constraint matrices and vectors as follows:
% A_{\textrm{sc}} =\left\lbrack \begin{array}{cc}
% A & 0\\
% 0 & 1
% \end{array}\right\rbrack
% b_{\textrm{sc}} =\left\lbrack \begin{array}{c}
% 0\\
% \vdots \\
% 0
% \end{array}\right\rbrack
% d_{\textrm{sc}} =\left\lbrack \begin{array}{c}
% 0\\
% \vdots \\
% 0\\
% 1
% \end{array}\right\rbrack
% \gamma =-1.
% Extend the coefficient vector f as well:
% f_{\textrm{sc}} =\left\lbrack \begin{array}{c}
% f\\
% 1
% \end{array}\right\rbrack.
% In terms of the new variables, the quadratic programming problem becomes
% \min_u \frac{1}{2}u^TA_{\rm sc}u + f_{\rm sc}^Tu
% = -1/2 + \min_u \left [ 1/2 + f_{\rm sc}^T u\right ]
% where
% u({\rm end)+1/2\ge \frac{1}{2}u^TA_{\rm sc}u.
% This quadratic constraint becomes a cone constraint through the following calculation, which uses the earlier definitions of A_{\rm sc}, d_{\rm sc}, and \gamma:
% \frac{1}{2}u^TA_{\rm sc}u = \frac{1}{2} \|Hx\|^2 \le t + \frac 1 2
% \|Hx\|^2 \le 2t+1
% \|Hx\|^2 + t^2 \le t^2+ 2t+1 = (t+1)^2
% \sqrt{\|Hx\|^2 + t^2 } \le | t+1| = \pm (t+1)
% \|u\| \le \pm(t-\gamma )
% \|u\| \le \pm(d_{\rm sc} -\gamma).
% The quadratic program has the same solution as the corresponding cone program. The only difference is the added term -1/2 in the cone program.
%% Numerical Example
% The quadprog documentation gives this example.

H = [1,-1,1
    -1,2,-2
    1,-2,4];
f = [-7;-12;-15];
lb = zeros(3,1);
ub = ones(size(lb));
Aineq = [1,1,1];
bineq = 3;
[xqp fqp] = quadprog(H,f,Aineq,bineq,[],[],lb,ub)

% Referring to the description at the beginning of this example, specify the second-order cone constraint variables, and then call the coneprog function.

Asc = sqrtm(H);
Asc((end+1),(end+1)) = 1;
d = [zeros(size(f(:)));1];
gamma = -1;
b = zeros(size(d));
qp = secondordercone(Asc,b,d,gamma);
Aq = Aineq;
Aq(:,(end+1)) = 0;
lb(end+1) = -Inf;
ub(end+1) = Inf;
[u,fval,eflag] = coneprog([f(:);1],qp,Aq,bineq,[],[],lb,ub)

% The first three elements of the cone solution u are equal to the elements of the quadratic programming solution xqp, to display precision:

disp([xqp,u(1:(end-1))])

% The returned quadratic function value fqp is the returned cone value minus 1/2 when 2t+1 is positive, or plus 1/2 when 2t+1 is negative.

disp([fqp-sign(2*u(end)+1)*1/2 fval])

% Copyright 2020 The MathWorks, Inc.