% Boyd & Vandenberghe "Convex Optimization"
% Joelle Skaf - 01/29/06
% Updated to use GP mode by Almir Mutapcic 02/08/06
%
% Given a square matrix M, the goal is to find a vector (with dii > 0)
% such that ||DMD^{-1}||_F is minimized, where D = diag(d).
% The problem can be cast as an unconstrained geometric program:
%           minimize sqrt( sum_{i,j=1}^{n} Mij^2*di^2/dj^2 )
%

rs = randn( 'state' );
randn( 'state', 0 );

% matrix size (M is an n-by-n matrix)
n = 4;
M = randn(n,n);

% formulating the problem as a GP
cvx_begin gp
  variable d(n)
  minimize( sqrt( sum( sum( diag(d.^2)*(M.^2)*diag(d.^-2) ) ) ) )
  % Alternate formulation: norm( diag(d)*abs(M)*diag(1./d), 'fro' )
cvx_end

% displaying results
D = diag(d);
disp('The matrix D that minimizes ||DMD^{-1}||_F is: ');
disp(D);
disp('The minimium Frobenius norm achieved is: ');
disp(norm(D*M*inv(D),'fro'));
disp('while the Frobunius norm of the original matrix M is: ');
disp(norm(M,'fro'));
 
Successive approximation method to be employed.
   For improved efficiency, sedumi is solving the dual problem.
   sedumi will be called several times to refine the solution.
   Original size: 49 variables, 21 equality constraints
   16 exponentials add 128 variables, 80 equality constraints
-----------------------------------------------------------------
 Cones  |             Errors              |
Mov/Act | Centering  Exp cone   Poly cone | Status
--------+---------------------------------+---------
 16/ 16 | 8.409e+00  4.088e+00  9.320e-07 | Solved
 16/ 16 | 8.421e-01  5.135e-02  1.477e-07 | Solved
 16/ 16 | 7.510e-02  4.194e-04  5.395e-08 | Solved
  9/ 13 | 3.796e-03  6.881e-07  5.577e-08 | Solved
  1/ 11 | 2.079e-04  5.820e-08  5.563e-08 | Solved
  0/ 11 | 1.568e-05  5.681e-08  5.564e-08 | Solved
-----------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +3.25231
The matrix D that minimizes ||DMD^{-1}||_F is: 
    1.1160         0         0         0
         0    0.9369         0         0
         0         0    1.0000         0
         0         0         0    1.6706

The minimium Frobenius norm achieved is: 
    3.2523

while the Frobunius norm of the original matrix M is: 
    3.6126