```% From Boyd & Vandenberghe, "Convex Optimization"
% Joëlle Skaf - 09/26/05
%
% Shows the equivalence of the following formulations:
% 1)        minimize    (Ax + b)'*inv(I + B*diag(x)*B')*(Ax + b)
%               s.t.    x >= 0
% 2)        minimize    (Ax + b)'*inv(I + B*Y*B')*(Ax + b)
%               s.t.    x >= 0
%                       Y = diag(x)
% 3)        minimize    v'*v + w'*inv(diag(x))*w
%               s.t.    v + Bw = Ax + b
%                       x >= 0
% 4)        minimize    v'*v + w'*inv(Y)*w
%               s.t.    Y = diag(x)
%                       v + Bw = Ax + b
%                       x >= 0

% Generate input data
randn('state',0);
m = 16; n = 8;
A = randn(m,n);
b = randn(m,1);
B = randn(m,n);

% Problem 1: original formulation
disp('Computing optimal solution for 1st formulation...');
cvx_begin
variable x1(n)
minimize( matrix_frac(A*x1 + b , eye(m) + B*diag(x1)*B') )
x1 >= 0;
cvx_end
opt1 = cvx_optval;

% Problem 2: original formulation (modified)
disp('Computing optimal solution for 2nd formulation...');
cvx_begin
variable x2(n)
variable Y(n,n) diagonal
minimize( matrix_frac(A*x2 + b , eye(m) + B*Y*B') )
x2 >= 0;
Y == diag(x2);
cvx_end
opt2 = cvx_optval;

% Problem 3: equivalent formulation (as given in the book)
disp('Computing optimal solution for 3rd formulation...');
cvx_begin
variables x3(n) w(n) v(m)
minimize( square_pos(norm(v)) + matrix_frac(w, diag(x3)) )
v + B*w == A*x3 + b;
x3 >= 0;
cvx_end
opt3 = cvx_optval;

% Problem 4: equivalent formulation (modified)
disp('Computing optimal solution for 4th formulation...');
cvx_begin
variables x4(n) w(n) v(m)
variable Y(n,n) diagonal
minimize( square_pos(norm(v)) + matrix_frac(w, Y) )
v + B*w == A*x4 + b;
x4 >= 0;
Y == diag(x4);
cvx_end
opt4 = cvx_optval;

% Display the results
disp('------------------------------------------------------------------------');
disp('The optimal value for each of the 4 formulations is: ');
[opt1 opt2 opt3 opt4]
disp('They should be equal!')
```
```Computing optimal solution for 1st formulation...

Calling sedumi: 161 variables, 9 equality constraints
For improved efficiency, sedumi is solving the dual problem.
------------------------------------------------------------
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 = 26, dim = 298, blocks = 2
nnz(A) = 1225 + 0, nnz(ADA) = 81, nnz(L) = 45
it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
0 :            3.39E+01 0.000
1 :  -2.62E+00 9.95E+00 0.000 0.2932 0.9000 0.9000   1.31  1  1  1.2E+01
2 :  -3.95E+00 3.75E+00 0.000 0.3767 0.9000 0.9000   1.25  1  1  4.0E+00
3 :  -4.65E+00 1.03E+00 0.000 0.2736 0.9000 0.9000   1.25  1  1  9.4E-01
4 :  -4.97E+00 3.95E-01 0.000 0.3851 0.9000 0.9000   0.95  1  1  3.8E-01
5 :  -5.14E+00 8.42E-02 0.000 0.2131 0.9000 0.9000   1.01  1  1  8.1E-02
6 :  -5.18E+00 6.78E-03 0.093 0.0806 0.9900 0.9900   1.02  1  1  6.4E-03
7 :  -5.18E+00 2.58E-04 0.363 0.0381 0.9901 0.9900   1.00  1  1  2.8E-04
8 :  -5.18E+00 1.09E-05 0.251 0.0421 0.9000 0.0000   1.00  1  1  5.8E-05
9 :  -5.18E+00 8.97E-07 0.000 0.0824 0.9121 0.9000   1.00  1  1  6.7E-06
10 :  -5.18E+00 1.19E-07 0.000 0.1321 0.9097 0.9000   1.00  1  1  9.9E-07
11 :  -5.18E+00 1.97E-08 0.000 0.1665 0.9111 0.9000   1.00  1  1  1.8E-07
12 :  -5.18E+00 1.16E-09 0.000 0.0588 0.9901 0.9900   1.00  1  1  1.1E-08

iter seconds digits       c*x               b*y
12      0.1   Inf -5.1824773412e+00 -5.1824772175e+00
|Ax-b| =   5.4e-09, [Ay-c]_+ =   1.9E-08, |x|=  6.9e+00, |y|=  5.2e+00

Detailed timing (sec)
Pre          IPM          Post
1.000E-02    6.000E-02    1.000E-02
Max-norms: ||b||=1, ||c|| = 2.976981e+00,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 3.52372.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +5.18248
Computing optimal solution for 2nd formulation...

Calling sedumi: 161 variables, 9 equality constraints
For improved efficiency, sedumi is solving the dual problem.
------------------------------------------------------------
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 = 26, dim = 298, blocks = 2
nnz(A) = 1225 + 0, nnz(ADA) = 81, nnz(L) = 45
it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
0 :            3.39E+01 0.000
1 :  -2.62E+00 9.95E+00 0.000 0.2932 0.9000 0.9000   1.31  1  1  1.2E+01
2 :  -3.95E+00 3.75E+00 0.000 0.3767 0.9000 0.9000   1.25  1  1  4.0E+00
3 :  -4.65E+00 1.03E+00 0.000 0.2736 0.9000 0.9000   1.25  1  1  9.4E-01
4 :  -4.97E+00 3.95E-01 0.000 0.3851 0.9000 0.9000   0.95  1  1  3.8E-01
5 :  -5.14E+00 8.42E-02 0.000 0.2131 0.9000 0.9000   1.01  1  1  8.1E-02
6 :  -5.18E+00 6.78E-03 0.093 0.0806 0.9900 0.9900   1.02  1  1  6.4E-03
7 :  -5.18E+00 2.58E-04 0.363 0.0381 0.9901 0.9900   1.00  1  1  2.8E-04
8 :  -5.18E+00 1.09E-05 0.251 0.0421 0.9000 0.0000   1.00  1  1  5.8E-05
9 :  -5.18E+00 8.97E-07 0.000 0.0824 0.9121 0.9000   1.00  1  1  6.7E-06
10 :  -5.18E+00 1.19E-07 0.000 0.1321 0.9097 0.9000   1.00  1  1  9.9E-07
11 :  -5.18E+00 1.97E-08 0.000 0.1665 0.9111 0.9000   1.00  1  1  1.8E-07
12 :  -5.18E+00 1.16E-09 0.000 0.0588 0.9901 0.9900   1.00  1  1  1.1E-08

iter seconds digits       c*x               b*y
12      0.1   Inf -5.1824773412e+00 -5.1824772175e+00
|Ax-b| =   5.4e-09, [Ay-c]_+ =   1.9E-08, |x|=  6.9e+00, |y|=  5.2e+00

Detailed timing (sec)
Pre          IPM          Post
1.000E-02    7.000E-02    1.000E-02
Max-norms: ||b||=1, ||c|| = 2.976981e+00,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 3.52372.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +5.18248
Computing optimal solution for 3rd formulation...

Calling sedumi: 75 variables, 54 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 = 54, order n = 24, dim = 113, blocks = 4
nnz(A) = 321 + 0, nnz(ADA) = 2740, nnz(L) = 1397
it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
0 :            4.11E+00 0.000
1 :   1.90E+00 1.88E+00 0.000 0.4561 0.9000 0.9000   0.91  1  1  6.7E+00
2 :   2.55E+00 4.95E-01 0.000 0.2638 0.9000 0.9000   1.40  1  1  1.5E+00
3 :   4.09E+00 1.29E-01 0.000 0.2612 0.9000 0.9000   1.02  1  1  3.8E-01
4 :   4.86E+00 3.41E-02 0.000 0.2635 0.9000 0.9000   0.97  1  1  1.0E-01
5 :   5.12E+00 6.90E-03 0.000 0.2024 0.9000 0.9000   1.00  1  1  2.0E-02
6 :   5.17E+00 5.29E-04 0.000 0.0766 0.8425 0.9000   0.99  1  1  4.4E-03
7 :   5.18E+00 9.99E-05 0.000 0.1891 0.9000 0.9000   1.00  1  1  8.4E-04
8 :   5.18E+00 1.89E-05 0.000 0.1891 0.9000 0.9000   1.00  1  1  1.6E-04
9 :   5.18E+00 3.75E-06 0.000 0.1982 0.9000 0.9000   1.00  1  1  3.2E-05
10 :   5.18E+00 7.68E-07 0.102 0.2050 0.9000 0.9000   1.00  1  1  6.5E-06
11 :   5.18E+00 1.61E-07 0.177 0.2097 0.9000 0.9000   1.00  1  1  1.4E-06
12 :   5.18E+00 3.41E-08 0.218 0.2117 0.9000 0.9000   1.00  1  1  2.9E-07
13 :   5.18E+00 7.29E-09 0.223 0.2138 0.9000 0.9000   1.00  1  1  6.1E-08
14 :   5.18E+00 1.54E-09 0.226 0.2112 0.9000 0.9000   1.00  2  2  1.3E-08

iter seconds digits       c*x               b*y
14      0.1   Inf  5.1824773736e+00  5.1824773780e+00
|Ax-b| =   2.4e-08, [Ay-c]_+ =   1.3E-09, |x|=  6.2e+00, |y|=  1.1e+01

Detailed timing (sec)
Pre          IPM          Post
0.000E+00    1.000E-01    1.000E-02
Max-norms: ||b||=1.488490e+00, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 282957.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +5.18248
Computing optimal solution for 4th formulation...

Calling sedumi: 75 variables, 54 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 = 54, order n = 24, dim = 113, blocks = 4
nnz(A) = 321 + 0, nnz(ADA) = 2740, nnz(L) = 1397
it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
0 :            4.11E+00 0.000
1 :   1.90E+00 1.88E+00 0.000 0.4561 0.9000 0.9000   0.91  1  1  6.7E+00
2 :   2.55E+00 4.95E-01 0.000 0.2638 0.9000 0.9000   1.40  1  1  1.5E+00
3 :   4.09E+00 1.29E-01 0.000 0.2612 0.9000 0.9000   1.02  1  1  3.8E-01
4 :   4.86E+00 3.41E-02 0.000 0.2635 0.9000 0.9000   0.97  1  1  1.0E-01
5 :   5.12E+00 6.90E-03 0.000 0.2024 0.9000 0.9000   1.00  1  1  2.0E-02
6 :   5.17E+00 5.29E-04 0.000 0.0766 0.8425 0.9000   0.99  1  1  4.4E-03
7 :   5.18E+00 9.99E-05 0.000 0.1891 0.9000 0.9000   1.00  1  1  8.4E-04
8 :   5.18E+00 1.89E-05 0.000 0.1891 0.9000 0.9000   1.00  1  1  1.6E-04
9 :   5.18E+00 3.75E-06 0.000 0.1982 0.9000 0.9000   1.00  1  1  3.2E-05
10 :   5.18E+00 7.68E-07 0.102 0.2050 0.9000 0.9000   1.00  1  1  6.5E-06
11 :   5.18E+00 1.61E-07 0.177 0.2097 0.9000 0.9000   1.00  1  1  1.4E-06
12 :   5.18E+00 3.41E-08 0.218 0.2115 0.9000 0.9000   1.00  1  1  2.9E-07
13 :   5.18E+00 7.27E-09 0.223 0.2134 0.9000 0.9000   1.00  1  1  6.1E-08
14 :   5.18E+00 1.49E-09 0.225 0.2054 0.9000 0.9000   1.00  1  2  1.3E-08

iter seconds digits       c*x               b*y
14      0.1   Inf  5.1824773737e+00  5.1824773781e+00
|Ax-b| =   2.4e-08, [Ay-c]_+ =   1.3E-09, |x|=  6.2e+00, |y|=  1.2e+01

Detailed timing (sec)
Pre          IPM          Post
1.000E-02    1.000E-01    0.000E+00
Max-norms: ||b||=1.488490e+00, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 318280.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +5.18248
------------------------------------------------------------------------
The optimal value for each of the 4 formulations is:

ans =

5.1825    5.1825    5.1825    5.1825

They should be equal!
```