Section 5.2.4: Solves a simple QCQP

% Boyd & Vandenberghe, "Convex Optimization"
% Joëlle Skaf - 08/23/05
%
% Solved a QCQP with 3 inequalities:
%           minimize    1/2 x'*P0*x + q0'*r + r0
%               s.t.    1/2 x'*Pi*x + qi'*r + ri <= 0   for i=1,2,3
% and verifies that strong duality holds.

% Input data
randn('state',13);
n = 6;
P0 = randn(n); P0 = P0'*P0 + eps*eye(n);
P1 = randn(n); P1 = P1'*P1;
P2 = randn(n); P2 = P2'*P2;
P3 = randn(n); P3 = P3'*P3;
q0 = randn(n,1); q1 = randn(n,1); q2 = randn(n,1); q3 = randn(n,1);
r0 = randn(1); r1 = randn(1); r2 = randn(1); r3 = randn(1);

fprintf(1,'Computing the optimal value of the QCQP and its dual... ');

cvx_begin
    variable x(n)
    dual variables lam1 lam2 lam3
    minimize( 0.5*quad_form(x,P0) + q0'*x + r0 )
    lam1: 0.5*quad_form(x,P1) + q1'*x + r1 <= 0;
    lam2: 0.5*quad_form(x,P2) + q2'*x + r2 <= 0;
    lam3: 0.5*quad_form(x,P3) + q3'*x + r3 <= 0;
cvx_end

obj1 = cvx_optval;
P_lam = P0 + lam1*P1 + lam2*P2 + lam3*P3;
q_lam = q0 + lam1*q1 + lam2*q2 + lam3*q3;
r_lam = r0 + lam1*r1 + lam2*r2 + lam3*r3;
obj2 = -0.5*q_lam'*inv(P_lam)*q_lam + r_lam;

fprintf(1,'Done! \n');

% Displaying results
disp('------------------------------------------------------------------------');
disp('The duality gap is equal to ');
disp(obj1-obj2)
Computing the optimal value of the QCQP and its dual...  
Calling SDPT3: 35 variables, 10 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints = 10
 dim. of socp   var  = 32,   num. of socp blk  =  4
 dim. of free   var  =  3 *** convert ublk to lblk
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
    NT      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      mean(obj)   cputime
-------------------------------------------------------------------
 0|0.000|0.000|2.0e+00|9.5e+00|2.0e+04| 3.922878e-09| 0:0:00| chol  1  1 
 1|1.000|0.987|8.3e-06|1.4e-01|8.0e+01| 1.606382e+00| 0:0:00| chol  1  1 
 2|0.990|0.978|7.0e-07|5.2e-03|9.9e-01|-4.476244e+00| 0:0:00| chol  1  1 
 3|0.956|0.936|2.1e-07|5.4e-04|4.3e-02|-4.693911e+00| 0:0:00| chol  1  1 
 4|0.942|0.938|3.7e-08|5.5e-05|2.4e-03|-4.712252e+00| 0:0:00| chol  1  1 
 5|0.907|0.921|1.4e-08|6.7e-06|2.3e-04|-4.714186e+00| 0:0:00| chol  1  1 
 6|0.906|0.911|4.5e-09|6.2e-07|2.3e-05|-4.714441e+00| 0:0:00| chol  1  1 
 7|0.981|0.962|5.7e-10|5.3e-08|1.5e-06|-4.714466e+00| 0:0:00| chol  1  1 
 8|0.977|0.949|5.1e-11|3.5e-09|8.5e-08|-4.714467e+00| 0:0:00|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   =  8
 primal objective value = -4.71446662e+00
 dual   objective value = -4.71446658e+00
 gap := trace(XZ)       = 8.47e-08
 relative gap           = 8.12e-09
 actual relative gap    = -4.00e-09
 rel. primal infeas     = 5.08e-11
 rel. dual   infeas     = 3.46e-09
 norm(X), norm(y), norm(Z) = 1.4e+01, 1.4e+00, 1.7e+00
 norm(A), norm(b), norm(C) = 2.8e+01, 1.1e+01, 1.4e+01
 Total CPU time (secs)  = 0.2  
 CPU time per iteration = 0.0  
 termination code       =  0
 DIMACS: 5.2e-11  0.0e+00  6.2e-09  0.0e+00  -4.0e-09  8.1e-09
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -0.895296
Done! 
------------------------------------------------------------------------
The duality gap is equal to 
  -6.0636e-08