% 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 SDPT3 4.0: 10 variables, 1 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints =  1
 dim. of sdp    var  =  4,   num. of sdp  blk  =  1
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
   HKM      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|2.4e+01|4.8e+00|4.0e+02| 1.219270e+01  0.000000e+00| 0:0:00| chol  1  1 
 1|0.881|0.928|2.9e+00|3.9e-01|4.2e+01|-3.750658e+00 -1.667243e+00| 0:0:00| chol  1  1 
 2|0.802|0.388|5.7e-01|2.4e-01|5.6e+00|-3.433260e+01 -1.924193e+00| 0:0:00| chol  1  1 
 3|0.046|0.553|5.4e-01|1.1e-01|5.7e+01|-9.038621e+01 -2.395546e+01| 0:0:00| chol  1  1 
 4|0.290|1.000|3.8e-01|4.9e-05|1.2e+02|-7.186488e+01 -5.499101e+01| 0:0:00| chol  1  1 
 5|0.974|1.000|9.8e-03|4.9e-06|8.3e+00|-4.455226e+01 -4.975782e+01| 0:0:00| chol  1  1 
 6|1.000|1.000|1.1e-11|1.0e-03|2.5e+00|-4.673462e+01 -4.899832e+01| 0:0:00| chol  1  1 
 7|0.986|0.987|5.3e-12|1.4e-05|3.5e-02|-4.829693e+01 -4.832847e+01| 0:0:00| chol  1  1 
 8|0.988|0.989|1.9e-12|1.6e-07|4.1e-04|-4.831872e+01 -4.831909e+01| 0:0:00| chol  1  1 
 9|0.983|0.987|6.3e-13|2.0e-09|6.5e-06|-4.831898e+01 -4.831898e+01| 0:0:00| chol  1  1 
10|0.963|0.983|1.5e-14|3.6e-11|2.2e-07|-4.831898e+01 -4.831898e+01| 0:0:00|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 10
 primal objective value = -4.83189831e+01
 dual   objective value = -4.83189833e+01
 gap := trace(XZ)       = 2.16e-07
 relative gap           = 2.22e-09
 actual relative gap    = 2.13e-09
 rel. primal infeas (scaled problem)   = 1.49e-14
 rel. dual     "        "       "      = 3.61e-11
 rel. primal infeas (unscaled problem) = 0.00e+00
 rel. dual     "        "       "      = 0.00e+00
 norm(X), norm(y), norm(Z) = 1.1e+02, 2.6e+02, 4.8e+02
 norm(A), norm(b), norm(C) = 2.8e+00, 1.2e+00, 4.1e+00
 Total CPU time (secs)  = 0.28  
 CPU time per iteration = 0.03  
 termination code       =  0
 DIMACS: 1.5e-14  0.0e+00  6.0e-11  0.0e+00  2.1e-09  2.2e-09
-------------------------------------------------------------------
 
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +48.354
 
------------------------------------------------------------------------
The optimal t obtained is
  -48.3540