% From Boyd & Vandenberghe, "Convex Optimization"
% Joëlle Skaf - 09/26/05
%
% Finds the optimal t that would maximize c*t while still having A - t*B
% positive semidefinite by solving the following SDP:
%           minimize    c*t
%               s.t.    t*B <= A
% c can either be a positive or negative real number

% Generate input data
randn('state',0);
n = 4;
A = randn(n); A = 0.5*(A'+A); %A = A'*A;
B = randn(n); B = B'*B;
% can modify the value of c (>0 or <0)
c = -1;

% Create and solve the model
cvx_begin sdp
    variable t
    minimize ( c*t )
    A >= t * B;
cvx_end

% Display results
disp('------------------------------------------------------------------------');
disp('The optimal t obtained is');
disp(t);
 
Calling sedumi: 10 variables, 9 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 = 9, order n = 5, dim = 17, blocks = 2
nnz(A) = 18 + 0, nnz(ADA) = 81, nnz(L) = 45
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            7.41E+00 0.000
  1 :   1.50E+00 1.38E+00 0.000 0.1864 0.9000 0.9000   0.26  1  1  1.9E+00
  2 :   6.02E+00 2.83E-01 0.000 0.2051 0.9000 0.9000  -0.08  1  1  1.1E+00
  3 :   4.20E+01 2.00E-02 0.000 0.0706 0.9900 0.9900  -0.62  1  1  5.4E-01
  4 :   6.03E+01 1.87E-03 0.000 0.0937 0.9900 0.9900  -0.13  1  1  1.0E-01
  5 :   4.85E+01 7.65E-05 0.444 0.0409 0.9900 0.9900   0.98  1  1  4.1E-03
  6 :   4.81E+01 4.33E-07 0.000 0.0057 0.9769 0.9900   0.99  1  1  9.4E-05
  7 :   4.81E+01 2.01E-08 0.214 0.0464 0.9900 0.9585   1.00  1  1  4.4E-06
  8 :   4.81E+01 6.35E-10 0.000 0.0316 0.9900 0.9900   1.00  2  2  1.4E-07
  9 :   4.81E+01 3.38E-11 0.065 0.0532 0.9900 0.9900   1.00  2  2  7.4E-09

iter seconds digits       c*x               b*y
  9      0.0   Inf  4.8119383092e+01  4.8119383403e+01
|Ax-b| =   1.5e-08, [Ay-c]_+ =   1.2E-09, |x|=  4.8e+02, |y|=  1.2e+02

Detailed timing (sec)
   Pre          IPM          Post
0.000E+00    5.000E-02    0.000E+00    
Max-norms: ||b||=2.368443e+00, ||c|| = 5.424573e-01,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 176.719.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +48.354
------------------------------------------------------------------------
The optimal t obtained is
  -48.3540