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 sedumi: 11 variables, 4 equality constraints
   For improved efficiency, sedumi is solving the dual problem.
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 4, order n = 9, dim = 12, blocks = 2
nnz(A) = 16 + 0, nnz(ADA) = 16, nnz(L) = 10
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            7.22E+01 0.000
  1 :   1.10E+01 1.72E+01 0.000 0.2376 0.9000 0.9000   2.11  1  1  1.1E+00
  2 :   9.99E+00 4.77E+00 0.000 0.2778 0.9000 0.9000   1.15  1  1  3.8E-01
  3 :   1.04E+01 1.25E+00 0.000 0.2626 0.9000 0.9000   0.96  1  1  1.2E-01
  4 :   1.03E+01 2.96E-01 0.000 0.2368 0.9000 0.9000   0.96  1  1  3.2E-02
  5 :   1.03E+01 6.69E-02 0.000 0.2256 0.9000 0.9000   0.98  1  1  7.7E-03
  6 :   1.03E+01 6.03E-03 0.098 0.0902 0.9900 0.9901   0.99  1  1  7.3E-04
  7 :   1.03E+01 1.14E-04 0.000 0.0190 0.9901 0.9900   1.00  1  1  1.5E-05
  8 :   1.03E+01 2.96E-08 0.146 0.0003 0.9999 0.9999   1.00  1  1  4.3E-09

iter seconds digits       c*x               b*y
  8      0.1   8.4  1.0317307761e+01  1.0317307722e+01
|Ax-b| =   4.0e-09, [Ay-c]_+ =   1.4E-09, |x|=  3.7e+01, |y|=  3.1e+00

Detailed timing (sec)
   Pre          IPM          Post
0.000E+00    5.000E-02    0.000E+00    
Max-norms: ||b||=22, ||c|| = 2,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 2.44525.
------------------------------------------------------------
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