Exercise 4.3: Solve a simple QP with inequality constraints

% From Boyd & Vandenberghe, "Convex Optimization"
% Joëlle Skaf - 09/26/05
%
% Solves the following QP with inequality constraints:
%           minimize    1/2x'*P*x + q'*x + r
%               s.t.    -1 <= x_i <= 1      for i = 1,2,3
% Also shows that the given x_star is indeed optimal

% Generate data
P = [13 12 -2; 12 17 6; -2 6 12];
q = [-22; -14.5; 13];
r = 1;
n = 3;
x_star = [1;1/2;-1];

% Construct and solve the model
fprintf(1,'Computing the optimal solution ...');
cvx_begin
    variable x(n)
    minimize ( (1/2)*quad_form(x,P) + q'*x + r)
    x >= -1;
    x <=  1;
cvx_end
fprintf(1,'Done! \n');

% Display results
disp('------------------------------------------------------------------------');
disp('The computed optimal solution is: ');
disp(x);
disp('The given optimal solution is: ');
disp(x_star);
Computing the optimal solution ... 
Calling SDPT3: 11 variables, 4 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints =  4
 dim. of socp   var  =  5,   num. of socp blk  =  1
 dim. of linear var  =  6
*******************************************************************
   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|9.8e-01|2.8e+00|1.2e+03| 1.028786e+02| 0:0:00| chol  1  1 
 1|1.000|1.000|2.9e-07|6.4e-02|1.5e+02| 9.371829e+01| 0:0:00| chol  1  1 
 2|0.905|0.870|1.2e-07|1.4e-02|1.5e+01| 4.175271e+01| 0:0:00| chol  1  1 
 3|0.954|1.000|3.7e-07|6.4e-04|6.2e+00| 3.937449e+01| 0:0:00| chol  1  1 
 4|1.000|0.889|3.9e-08|1.3e-04|1.2e+00| 3.842091e+01| 0:0:00| chol  1  1 
 5|0.878|1.000|6.6e-09|6.4e-06|3.0e-01| 3.816732e+01| 0:0:00| chol  1  1 
 6|1.000|0.973|1.2e-09|7.9e-07|1.7e-02| 3.812600e+01| 0:0:00| chol  1  1 
 7|0.974|0.965|3.5e-10|9.0e-08|5.5e-04| 3.812498e+01| 0:0:00| chol  1  1 
 8|0.987|0.985|1.2e-11|1.4e-09|7.6e-06| 3.812500e+01| 0:0:00| chol  1  1 
 9|1.000|0.994|1.1e-11|1.1e-11|1.1e-07| 3.812500e+01| 0:0:00|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   =  9
 primal objective value =  3.81250001e+01
 dual   objective value =  3.81249999e+01
 gap := trace(XZ)       = 1.08e-07
 relative gap           = 1.40e-09
 actual relative gap    = 1.39e-09
 rel. primal infeas     = 1.06e-11
 rel. dual   infeas     = 1.12e-11
 norm(X), norm(y), norm(Z) = 3.7e+01, 2.9e+00, 4.3e+00
 norm(A), norm(b), norm(C) = 7.1e+00, 5.2e+01, 4.2e+00
 Total CPU time (secs)  = 0.2  
 CPU time per iteration = 0.0  
 termination code       =  0
 DIMACS: 1.3e-11  0.0e+00  1.6e-11  0.0e+00  1.4e-09  1.4e-09
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -21.625
Done! 
------------------------------------------------------------------------
The computed optimal solution is: 
    1.0000
    0.5000
   -1.0000

The given optimal solution is: 
    1.0000
    0.5000
   -1.0000