# Minimal phase spectral factorization

    % A PSD matrix is found which minimizes a weighted trace while obtaining
    % fixed sums along the diagonals. Notice the use of a FOR loop to access
    % the diagonals of X. A later version of CVX will eliminate the need for
    % this by allowing the use of the SPDIAGS function in side models.
    % Nevertheless, this example provides an illustration of the use of
    % standard Matlab control statements to build models.
    %
    % Adapted from an example provided in the SeDuMi documentation.

    % Generate data
    b = [2; 0.2; -0.3];
    n = length( b );

    % Create and solve model
    cvx_begin sdp
        variable X( n, n ) symmetric
        dual variable y{n}
        minimize( ( n - 1 : -1 : 0 ) * diag( X ) );
        for k = 1 : n,
            sum( diag( X, k - 1 ) ) == b( k ) : y{k};
        end
        X >= 0;
    cvx_end
    y = [ y{:} ]';

    % Display resuls
    disp( 'The optimal point, X:' );
    disp( X )
    disp( 'The diagonal sums:' );
    disp( sum( spdiags( X, 0:n-1 ), 1 ) );
    disp( 'min( eig( X ) ) (should be nonnegative):' );
    disp( min( eig( X ) ) )
    disp( 'The optimal weighted trace:' );
    disp( ( n - 1 : -1 : 0 ) * diag( X ) );

    Calling SeDuMi: 6 variables (0 free), 3 equality constraints
    ------------------------------------------------------------------------
    SeDuMi 1.1 by AdvOL, 2005 and Jos F. Sturm, 1998, 2001-2003.
    Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
    eqs m = 3, order n = 4, dim = 10, blocks = 2
    nnz(A) = 6 + 0, nnz(ADA) = 9, nnz(L) = 6
     it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
      0 :            1.69E+001 0.000
      1 : -2.44E-001 3.91E+000 0.000 0.2312 0.9000 0.9000   1.39  1  1  2.0E+000
      2 :  1.86E-001 3.22E-001 0.000 0.0824 0.9900 0.9900   1.31  1  1  4.0E-001
      3 :  1.23E-001 1.12E-004 0.000 0.0003 0.9999 0.9999   0.99  1  1  2.6E-004
      4 :  1.23E-001 5.18E-006 0.000 0.0463 0.9900 0.9900   1.00  1  1  1.2E-005
      5 :  1.23E-001 2.36E-007 0.000 0.0455 0.9900 0.9900   1.00  1  1  6.8E-007
      6 :  1.23E-001 1.90E-008 0.327 0.0806 0.9900 0.9900   1.00  2  2  5.5E-008
      7 :  1.23E-001 8.16E-010 0.000 0.0429 0.9906 0.9900   0.99  2  2  2.2E-009

    iter seconds digits       c*x               b*y
      7      0.0   8.5  1.2273256559e-001  1.2273256524e-001
    |Ax-b| =  1.1e-010, [Ay-c]_+ =  1.4E-010, |x|= 2.0e+000, |y|= 7.6e-001

    Detailed timing (sec)
       Pre          IPM          Post
    1.001E-002    1.001E-002    0.000E+000
    Max-norms: ||b||=2, ||c|| = 2,
    Cholesky |add|=0, |skip| = 0, ||L.L|| = 9.99993.
    ------------------------------------------------------------------------
    Status: Solved
    Optimal value (cvx_optval): +0.122733
    The optimal point, X:
        0.0468   -0.0369   -0.3000
       -0.0369    0.0292    0.2369
       -0.3000    0.2369    1.9240

    The diagonal sums:
        2.0000    0.2000   -0.3000

    min( eig( X ) ) (should be nonnegative):
      4.4503e-011

    The optimal weighted trace:
        0.1227