```% 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}
dual variable Z
minimize( ( n - 1 : -1 : 0 ) * diag( X ) );
for k = 1 : n,
sum( diag( X, k - 1 ) ) == b( k ) : y{k};
end
X >= 0 : Z;
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 SDPT3 4.0: 6 variables, 3 equality constraints
For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

num. of constraints =  3
dim. of sdp    var  =  3,   num. of sdp  blk  =  1
*******************************************************************
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|6.9e-01|5.3e+00|3.0e+02| 2.000000e+01  0.000000e+00| 0:0:00| chol  1  1
1|1.000|1.000|2.5e-07|5.7e-02|1.9e+01| 1.882061e+01  2.306667e+00| 0:0:00| chol  1  1
2|0.921|1.000|1.4e-07|5.7e-03|1.5e+00| 4.040616e+00  2.524843e+00| 0:0:00| chol  1  1
3|1.000|0.859|8.8e-08|1.3e-03|3.7e-01| 4.105341e+00  3.748230e+00| 0:0:00| chol  1  1
4|0.983|0.985|1.9e-08|7.5e-05|6.0e-03| 3.880804e+00  3.875203e+00| 0:0:00| chol  1  1
5|0.988|0.988|4.9e-10|6.4e-06|7.0e-05| 3.877308e+00  3.877275e+00| 0:0:00| chol  1  1
6|0.986|0.988|6.1e-12|7.7e-08|9.2e-07| 3.877268e+00  3.877268e+00| 0:0:00| chol  1  1
7|1.000|1.000|3.3e-14|1.2e-12|3.7e-08| 3.877267e+00  3.877267e+00| 0:0:00|
stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
number of iterations   =  7
primal objective value =  3.87726745e+00
dual   objective value =  3.87726741e+00
gap := trace(XZ)       = 3.73e-08
relative gap           = 4.26e-09
actual relative gap    = 4.26e-09
rel. primal infeas (scaled problem)   = 3.29e-14
rel. dual     "        "       "      = 1.21e-12
rel. primal infeas (unscaled problem) = 0.00e+00
rel. dual     "        "       "      = 0.00e+00
norm(X), norm(y), norm(Z) = 2.4e+00, 1.9e+00, 2.0e+00
norm(A), norm(b), norm(C) = 3.8e+00, 3.2e+00, 3.1e+00
Total CPU time (secs)  = 0.10
CPU time per iteration = 0.01
termination code       =  0
DIMACS: 3.5e-14  0.0e+00  1.2e-12  0.0e+00  4.3e-09  4.3e-09
-------------------------------------------------------------------

------------------------------------------------------------
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):
5.7391e-09

The optimal weighted trace:
0.1227

```