% Boyd & Vandenberghe "Convex Optimization"
% Joelle Skaf - 10/09/05
%
% Let C be a correlation matrix. Given lower and upper bounds on
% some of the angles (or correlation coeff.), find the maximum and minimum
% possible values of rho_14 by solving 2 SDP's
%           minimize/maximize   rho_14
%                        s.t.   C >=0
%                               0.6 <= rho_12 <=  0.9
%                               0.8 <= rho_13 <=  0.9
%                               0.5 <= rho_24 <=  0.7
%                              -0.8 <= rho_34 <= -0.4

n = 4;

% Upper bound SDP
fprintf(1,'Solving the upper bound SDP ...');

cvx_begin sdp
    variable C1(n,n) symmetric
    maximize ( C1(1,4) )
    C1 >= 0;
    diag(C1) == ones(n,1);
    C1(1,2) >= 0.6;
    C1(1,2) <= 0.9;
    C1(1,3) >= 0.8;
    C1(1,3) <= 0.9;
    C1(2,4) >= 0.5;
    C1(2,4) <= 0.7;
    C1(3,4) >= -0.8;
    C1(3,4) <= -0.4;
cvx_end

fprintf(1,'Done! \n');

% Lower bound SDP
fprintf(1,'Solving the lower bound SDP ...');

cvx_begin sdp
    variable C2(n,n) symmetric
    minimize ( C2(1,4) )
    C2 >= 0;
    diag(C2) == ones(n,1);
    C2(1,2) >= 0.6;
    C2(1,2) <= 0.9;
    C2(1,3) >= 0.8;
    C2(1,3) <= 0.9;
    C2(2,4) >= 0.5;
    C2(2,4) <= 0.7;
    C2(3,4) >= -0.8;
    C2(3,4) <= -0.4;
cvx_end

fprintf(1,'Done! \n');
% Displaying results
disp('--------------------------------------------------------------------------------');
disp(['The minimum and maximum values of rho_14 are: ' num2str(C2(1,4)) ' and ' num2str(C1(1,4))]);
disp('with corresponding correlation matrices: ');
disp(C2)
disp(C1)
Solving the upper bound SDP ... 
Calling Mosek 9.1.9: 18 variables, 6 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            : 6               
  Cones                  : 0               
  Scalar variables       : 8               
  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 - tries                  : 1                 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            : 6               
  Cones                  : 0               
  Scalar variables       : 8               
  Matrix variables       : 1               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 6
Optimizer  - Cones                  : 0
Optimizer  - Scalar variables       : 8                 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 : 21                after factor           : 21              
Factor     - dense dim.             : 0                 flops                  : 5.29e+02        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   1.0e+00  8.0e-01  5.0e+00  0.00e+00   4.000000000e+00   0.000000000e+00   1.0e+00  0.00  
1   2.2e-01  1.8e-01  6.8e-01  5.68e-01   1.601847865e+00   5.752271707e-01   2.2e-01  0.01  
2   7.2e-02  5.8e-02  1.0e-01  1.20e+00   6.556609770e-01   3.423728347e-01   7.2e-02  0.01  
3   7.0e-03  5.6e-03  2.5e-03  1.50e+00   2.625263863e-01   2.385818161e-01   7.0e-03  0.01  
4   8.2e-05  6.5e-05  3.2e-06  1.05e+00   2.303212228e-01   2.300505955e-01   8.2e-05  0.01  
5   2.5e-06  2.0e-06  1.7e-08  9.99e-01   2.299221058e-01   2.299139040e-01   2.5e-06  0.01  
6   5.3e-08  4.2e-08  5.3e-11  1.00e+00   2.299093602e-01   2.299091858e-01   5.3e-08  0.01  
7   5.8e-09  4.7e-09  1.9e-12  1.00e+00   2.299091122e-01   2.299090929e-01   5.8e-09  0.01  
8   8.5e-10  5.2e-09  1.1e-13  1.00e+00   2.299090875e-01   2.299090847e-01   8.5e-10  0.01  
Optimizer terminated. Time: 0.01    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: 2.2990908751e-01    nrm: 2e+00    Viol.  con: 7e-10    var: 0e+00    barvar: 0e+00  
  Dual.    obj: 2.2990908467e-01    nrm: 1e+00    Viol.  con: 0e+00    var: 3e-10    barvar: 4e-09  
Optimizer summary
  Optimizer                 -                        time: 0.01    
    Interior-point          - iterations : 8         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): +0.229909
 
Done! 
Solving the lower bound SDP ... 
Calling Mosek 9.1.9: 18 variables, 6 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            : 6               
  Cones                  : 0               
  Scalar variables       : 8               
  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 - tries                  : 1                 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            : 6               
  Cones                  : 0               
  Scalar variables       : 8               
  Matrix variables       : 1               
  Integer variables      : 0               

Optimizer  - threads                : 8               
Optimizer  - solved problem         : the primal      
Optimizer  - Constraints            : 6
Optimizer  - Cones                  : 0
Optimizer  - Scalar variables       : 8                 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 : 21                after factor           : 21              
Factor     - dense dim.             : 0                 flops                  : 5.29e+02        
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME  
0   1.0e+00  8.0e-01  5.0e+00  0.00e+00   4.000000000e+00   0.000000000e+00   1.0e+00  0.00  
1   2.2e-01  1.8e-01  6.7e-01  5.68e-01   1.623017452e+00   5.938125873e-01   2.2e-01  0.01  
2   5.2e-02  4.2e-02  4.7e-02  1.36e+00   6.610632060e-01   4.381337029e-01   5.2e-02  0.01  
3   5.0e-03  4.0e-03  1.2e-03  1.45e+00   4.138204992e-01   3.969081187e-01   5.0e-03  0.01  
4   1.0e-05  8.3e-06  1.0e-07  1.06e+00   3.928604383e-01   3.928251029e-01   1.0e-05  0.01  
5   3.3e-07  2.6e-07  5.7e-10  1.00e+00   3.928217350e-01   3.928206256e-01   3.3e-07  0.01  
6   3.6e-08  2.9e-08  2.1e-11  1.00e+00   3.928204884e-01   3.928203659e-01   3.6e-08  0.01  
7   1.8e-09  1.1e-09  2.3e-13  1.00e+00   3.928203313e-01   3.928203253e-01   1.8e-09  0.01  
Optimizer terminated. Time: 0.02    


Interior-point solution summary
  Problem status  : PRIMAL_AND_DUAL_FEASIBLE
  Solution status : OPTIMAL
  Primal.  obj: 3.9282033134e-01    nrm: 1e+00    Viol.  con: 1e-09    var: 9e-11    barvar: 0e+00  
  Dual.    obj: 3.9282032532e-01    nrm: 1e+00    Viol.  con: 0e+00    var: 5e-10    barvar: 9e-10  
Optimizer summary
  Optimizer                 -                        time: 0.02    
    Interior-point          - iterations : 7         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): -0.39282
 
Done! 
--------------------------------------------------------------------------------
The minimum and maximum values of rho_14 are: -0.39282 and 0.22991
with corresponding correlation matrices: 
    1.0000    0.6000    0.8542   -0.3928
    0.6000    1.0000    0.3042    0.5000
    0.8542    0.3042    1.0000   -0.5751
   -0.3928    0.5000   -0.5751    1.0000

    1.0000    0.7447    0.8000    0.2299
    0.7447    1.0000    0.3306    0.6013
    0.8000    0.3306    1.0000   -0.4000
    0.2299    0.6013   -0.4000    1.0000