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

num. of constraints =  6
dim. of sdp    var  =  4,   num. of sdp  blk  =  1
dim. of linear var  =  8
*******************************************************************
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|5.0e-01|8.7e+00|1.2e+03| 5.000000e+01  0.000000e+00| 0:0:00| chol  1  1
1|1.000|0.963|4.7e-07|4.0e-01|8.6e+01| 3.764390e+01  1.045278e-01| 0:0:00| chol  1  1
2|0.930|0.962|2.4e-06|2.4e-02|1.2e+01| 9.519382e+00  5.545477e-02| 0:0:00| chol  1  1
3|0.979|1.000|1.4e-07|9.0e-04|6.4e-01| 6.821346e-01  4.804692e-02| 0:0:00| chol  1  1
4|1.000|1.000|5.8e-08|9.0e-05|1.3e-01| 3.354711e-01  2.021144e-01| 0:0:00| chol  1  1
5|0.945|0.990|4.5e-09|9.9e-06|6.7e-03| 2.357452e-01  2.290535e-01| 0:0:00| chol  1  1
6|0.891|1.000|9.7e-09|9.0e-07|9.1e-04| 2.306737e-01  2.297716e-01| 0:0:00| chol  1  1
7|1.000|1.000|5.5e-09|9.2e-08|8.9e-05| 2.299788e-01  2.298904e-01| 0:0:00| chol  1  1
8|0.966|0.983|8.2e-10|2.6e-09|2.9e-06| 2.299114e-01  2.299086e-01| 0:0:00| chol  1  1
9|1.000|1.000|3.7e-09|1.6e-10|6.1e-07| 2.299095e-01  2.299089e-01| 0:0:00| chol  1  1
10|1.000|1.000|8.4e-12|2.5e-10|3.0e-08| 2.299091e-01  2.299091e-01| 0:0:00| chol  1  1
11|1.000|1.000|4.0e-13|1.7e-12|5.0e-10| 2.299091e-01  2.299091e-01| 0:0:00|
stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
number of iterations   = 11
primal objective value =  2.29909084e-01
dual   objective value =  2.29909083e-01
gap := trace(XZ)       = 4.98e-10
relative gap           = 3.41e-10
actual relative gap    = 3.35e-10
rel. primal infeas (scaled problem)   = 3.97e-13
rel. dual     "        "       "      = 1.68e-12
rel. primal infeas (unscaled problem) = 0.00e+00
rel. dual     "        "       "      = 0.00e+00
norm(X), norm(y), norm(Z) = 2.8e+00, 5.7e-01, 2.8e+00
norm(A), norm(b), norm(C) = 5.5e+00, 2.0e+00, 3.8e+00
Total CPU time (secs)  = 0.17
CPU time per iteration = 0.02
termination code       =  0
DIMACS: 4.0e-13  0.0e+00  3.2e-12  0.0e+00  3.4e-10  3.4e-10
-------------------------------------------------------------------

------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +0.229909

Done!
Solving the lower bound SDP ...
Calling SDPT3 4.0: 18 variables, 6 equality constraints
For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

num. of constraints =  6
dim. of sdp    var  =  4,   num. of sdp  blk  =  1
dim. of linear var  =  8
*******************************************************************
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|5.0e-01|8.7e+00|1.2e+03| 5.000000e+01  0.000000e+00| 0:0:00| chol  1  1
1|1.000|0.965|4.6e-07|3.9e-01|8.5e+01| 3.764990e+01  1.075756e-01| 0:0:00| chol  1  1
2|0.927|1.000|2.3e-06|9.0e-03|9.9e+00| 9.200175e+00  1.305318e-01| 0:0:00| chol  1  1
3|0.897|1.000|2.9e-07|9.0e-04|1.0e+00| 1.182476e+00  1.678502e-01| 0:0:00| chol  1  1
4|1.000|0.662|4.2e-08|3.6e-04|6.2e-01| 9.449252e-01  3.321156e-01| 0:0:00| chol  1  1
5|0.933|1.000|4.1e-09|9.0e-06|4.2e-02| 4.233944e-01  3.816576e-01| 0:0:00| chol  1  1
6|0.963|1.000|1.1e-09|9.0e-07|4.5e-03| 3.962889e-01  3.917716e-01| 0:0:00| chol  1  1
7|0.960|0.992|9.3e-10|9.7e-08|2.6e-04| 3.930398e-01  3.927797e-01| 0:0:00| chol  1  1
8|0.966|0.985|6.9e-10|1.1e-08|8.3e-06| 3.928279e-01  3.928197e-01| 0:0:00| chol  1  1
9|1.000|1.000|2.2e-09|1.4e-10|1.6e-06| 3.928215e-01  3.928199e-01| 0:0:00| chol  1  1
10|1.000|1.000|2.1e-11|2.1e-10|2.3e-08| 3.928203e-01  3.928203e-01| 0:0:00|
stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
number of iterations   = 10
primal objective value =  3.92820340e-01
dual   objective value =  3.92820317e-01
gap := trace(XZ)       = 2.32e-08
relative gap           = 1.30e-08
actual relative gap    = 1.25e-08
rel. primal infeas (scaled problem)   = 2.14e-11
rel. dual     "        "       "      = 2.08e-10
rel. primal infeas (unscaled problem) = 0.00e+00
rel. dual     "        "       "      = 0.00e+00
norm(X), norm(y), norm(Z) = 2.2e+00, 5.8e-01, 2.8e+00
norm(A), norm(b), norm(C) = 5.5e+00, 2.0e+00, 3.8e+00
Total CPU time (secs)  = 0.13
CPU time per iteration = 0.01
termination code       =  0
DIMACS: 2.1e-11  0.0e+00  4.0e-10  0.0e+00  1.2e-08  1.3e-08
-------------------------------------------------------------------

------------------------------------------------------------
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.8433   -0.3928
0.6000    1.0000    0.3322    0.5000
0.8433    0.3322    1.0000   -0.5311
-0.3928    0.5000   -0.5311    1.0000

1.0000    0.7127    0.8000    0.2299
0.7127    1.0000    0.3120    0.5827
0.8000    0.3120    1.0000   -0.4000
0.2299    0.5827   -0.4000    1.0000

```