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

MOSEK Version 9.1.9 (Build date: 2019-11-21 11:32:15)
Copyright (c) MOSEK ApS, Denmark. WWW: mosek.com
Platform: MACOSX/64-X86

Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 1               
  Cones                  : 0               
  Scalar variables       : 0               
  Matrix variables       : 1               
  Integer variables      : 0               

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator - tries                  : 2                 time                   : 0.00            
Lin. dep.  - tries                  : 1                 time                   : 0.00            
Lin. dep.  - number                 : 0               
Presolve terminated. Time: 0.00    
Problem
  Name                   :                 
  Objective sense        : min             
  Type                   : CONIC (conic optimization problem)
  Constraints            : 1               
  Cones                  : 0               
  Scalar variables       : 0               
  Matrix variables       : 1               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 1
Optimizer  - Cones                  : 0
Optimizer  - Scalar variables       : 0                 conic                  : 0               
Optimizer  - Semi-definite variables: 1                 scalarized             : 10              
Factor     - setup time             : 0.00              dense det. time        : 0.00            
Factor     - ML order time          : 0.00              GP order time          : 0.00            
Factor     - nonzeros before factor : 1                 after factor           : 1               
Factor     - dense dim.             : 0                 flops                  : 4.60e+02        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   2.7e+00  1.4e+00  2.2e+00  0.00e+00   1.219269963e+00   0.000000000e+00   1.0e+00  0.00  
1   3.3e-01  1.8e-01  4.8e-01  -7.50e-01  -2.576084821e+00  -5.931855625e-01  1.2e-01  0.01  
2   4.6e-02  2.5e-02  1.5e-01  -5.70e-01  -1.761476200e+01  -3.964947856e+00  1.7e-02  0.01  
3   3.8e-03  2.1e-03  2.7e-02  -7.63e-01  -9.737046770e+01  -2.654953039e+01  1.4e-03  0.01  
4   3.6e-04  1.9e-04  9.2e-04  3.13e-01   -5.883568002e+01  -4.942827837e+01  1.3e-04  0.01  
5   6.3e-06  3.4e-06  1.5e-06  1.09e+00   -4.847665768e+01  -4.839880108e+01  2.4e-06  0.01  
6   1.4e-08  7.6e-09  1.6e-10  1.00e+00   -4.831917654e+01  -4.831900204e+01  5.3e-09  0.01  
7   1.8e-09  1.0e-09  6.8e-12  1.00e+00   -4.831899541e+01  -4.831897371e+01  6.6e-10  0.01  
8   1.1e-10  3.9e-09  1.1e-13  1.00e+00   -4.831898380e+01  -4.831898242e+01  4.2e-11  0.01  
9   2.1e-12  1.7e-08  2.7e-16  1.00e+00   -4.831898330e+01  -4.831898328e+01  7.7e-13  0.01  
Optimizer terminated. Time: 0.02    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: -4.8318983303e+01   nrm: 7e+01    Viol.  con: 3e-10    barvar: 0e+00  
  Dual.    obj: -4.8318983278e+01   nrm: 3e+02    Viol.  con: 0e+00    barvar: 1e-10  
Optimizer summary
  Optimizer                 -                        time: 0.02    
    Interior-point          - iterations : 9         time: 0.01    
      Basis identification  -                        time: 0.00    
        Primal              - iterations : 0         time: 0.00    
        Dual                - iterations : 0         time: 0.00    
        Clean primal        - iterations : 0         time: 0.00    
        Clean dual          - iterations : 0         time: 0.00    
    Simplex                 -                        time: 0.00    
      Primal simplex        - iterations : 0         time: 0.00    
      Dual simplex          - iterations : 0         time: 0.00    
    Mixed integer           - relaxations: 0         time: 0.00    

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +48.354
 
------------------------------------------------------------------------
The optimal t obtained is
  -48.3540