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;
lam1 = -lam1; lam2 = -lam2; lam3 = -lam3;
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 SeDuMi: 37 variables (5 free), 30 equality constraints
------------------------------------------------------------------------
SeDuMi 1.1 by AdvOL, 2005 and Jos F. Sturm, 1998, 2001-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
Split 5 free variables
eqs m = 30, order n = 19, dim = 43, blocks = 5
nnz(A) = 213 + 0, nnz(ADA) = 774, nnz(L) = 402
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            1.56E+000 0.000
  1 : -2.37E-001 2.86E-001 0.000 0.1834 0.9000 0.9000   1.22  1  1  2.5E+000
  2 : -1.18E-001 1.49E-002 0.000 0.0523 0.9900 0.9900   1.08  1  1  1.8E-001
  3 : -8.60E-002 1.10E-003 0.114 0.0738 0.9900 0.9900   1.00  1  1  1.7E-002
  4 : -7.98E-002 4.16E-005 0.000 0.0377 0.9900 0.9900   1.00  1  1  7.1E-004
  5 : -7.98E-002 1.04E-006 0.000 0.0251 0.9900 0.9900   1.00  1  1  1.8E-005
  6 : -7.98E-002 9.07E-008 0.000 0.0870 0.9450 0.9463   1.00  1  1  1.2E-006
  7 : -7.98E-002 3.89E-009 0.374 0.0429 0.8848 0.9900   1.00  2  2  4.0E-008
  8 : -7.98E-002 1.00E-009 0.000 0.2572 0.9000 0.8104   1.00  2  2  1.0E-008

iter seconds digits       c*x               b*y
  8      0.1   Inf -7.9757737817e-002 -7.9757732568e-002
|Ax-b| =  1.6e-008, [Ay-c]_+ =  1.6E-009, |x|= 7.9e+000, |y|= 3.3e+000

Detailed timing (sec)
   Pre          IPM          Post
0.000E+000    5.007E-002    1.001E-002    
Max-norms: ||b||=1.316586e+000, ||c|| = 1.213838e+000,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 2.75851.
------------------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -0.895296
Done! 
------------------------------------------------------------------------
The duality gap is equal to 
    3.4935