% 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, 7 equality constraints
------------------------------------------------------------
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 = 7, order n = 9, dim = 12, blocks = 2
nnz(A) = 17 + 0, nnz(ADA) = 33, nnz(L) = 20
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            2.75E+00 0.000
  1 :  -3.71E+01 7.86E-01 0.000 0.2860 0.9000 0.9000   2.64  1  1  8.3E-01
  2 :  -5.38E+01 1.87E-01 0.000 0.2381 0.9000 0.9000   1.14  1  1  1.9E-01
  3 :  -5.78E+01 4.88E-02 0.000 0.2607 0.9000 0.9000   0.95  1  1  5.2E-02
  4 :  -5.88E+01 1.20E-02 0.000 0.2464 0.9000 0.9000   0.95  1  1  1.3E-02
  5 :  -5.90E+01 2.77E-03 0.000 0.2306 0.9000 0.9000   0.97  1  1  3.0E-03
  6 :  -5.91E+01 4.56E-04 0.000 0.1647 0.9015 0.9000   0.99  1  1  5.7E-04
  7 :  -5.91E+01 4.96E-06 0.000 0.0109 0.9901 0.9900   1.00  1  1  1.3E-05
  8 :  -5.91E+01 2.30E-10 0.000 0.0000 0.9999 0.9969   1.00  1  1  2.8E-08
  9 :  -5.91E+01 1.37E-11 0.000 0.0596 0.9450 0.9475   1.00  2  2  1.6E-09

iter seconds digits       c*x               b*y
  9      0.1   Inf -5.9124999997e+01 -5.9124999919e+01
|Ax-b| =   5.9e-10, [Ay-c]_+ =   1.8E-08, |x|=  4.2e+00, |y|=  3.0e+01

Detailed timing (sec)
   Pre          IPM          Post
1.000E-02    5.000E-02    1.000E-02    
Max-norms: ||b||=2, ||c|| = 4.268278e+01,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.631.
------------------------------------------------------------
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