```% 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 4.0: 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 linear var  =  3
*******************************************************************
SDPT3: Infeasible path-following algorithms
*******************************************************************
version  predcorr  gam  expon  scale_data
NT      1      0.000   1        0
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
0|0.000|0.000|2.0e+01|2.1e+00|5.5e+02| 1.197168e+02  0.000000e+00| 0:0:00| chol  1  1
1|1.000|0.687|1.3e-06|6.8e-01|9.6e+01| 8.394112e+00 -3.713950e+00| 0:0:00| chol  1  1
2|1.000|0.971|2.9e-06|2.3e-02|6.6e+00| 9.243206e-01 -3.202859e+00| 0:0:00| chol  1  1
3|0.836|1.000|1.4e-06|3.6e-04|1.0e+00|-2.412332e+00 -3.408622e+00| 0:0:00| chol  1  1
4|0.840|0.832|3.9e-07|9.2e-05|2.0e-01|-3.094383e+00 -3.291935e+00| 0:0:00| chol  1  1
5|0.927|1.000|9.6e-08|3.7e-06|3.0e-02|-3.204453e+00 -3.233858e+00| 0:0:00| chol  1  1
6|0.945|0.935|1.5e-08|6.0e-07|1.8e-03|-3.224662e+00 -3.226481e+00| 0:0:00| chol  1  1
7|0.934|0.905|9.5e-09|9.3e-08|1.4e-04|-3.225852e+00 -3.225989e+00| 0:0:00| chol  1  1
8|0.959|0.915|1.7e-09|9.8e-09|9.7e-06|-3.225934e+00 -3.225943e+00| 0:0:00| chol  1  1
9|0.990|0.988|9.7e-10|4.7e-10|4.4e-07|-3.225938e+00 -3.225939e+00| 0:0:00| chol  1  1
10|0.972|0.956|2.2e-10|6.8e-11|2.1e-08|-3.225938e+00 -3.225938e+00| 0:0:00|
stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
number of iterations   = 10
primal objective value = -3.22593846e+00
dual   objective value = -3.22593847e+00
gap := trace(XZ)       = 2.14e-08
relative gap           = 2.87e-09
actual relative gap    = 2.31e-09
rel. primal infeas (scaled problem)   = 2.18e-10
rel. dual     "        "       "      = 6.83e-11
rel. primal infeas (unscaled problem) = 0.00e+00
rel. dual     "        "       "      = 0.00e+00
norm(X), norm(y), norm(Z) = 1.0e+01, 1.5e+00, 1.9e+00
norm(A), norm(b), norm(C) = 1.5e+01, 7.9e+00, 7.3e+00
Total CPU time (secs)  = 0.12
CPU time per iteration = 0.01
termination code       =  0
DIMACS: 2.3e-10  0.0e+00  8.3e-11  0.0e+00  2.3e-09  2.9e-09
-------------------------------------------------------------------

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -0.895296

Done!
------------------------------------------------------------------------
The duality gap is equal to
1.2878e-08

```