randn('seed',0);
n = 10;
X0 = randn(n);
X0 = 0.5 * (X0 + X0');
[V,lam] = eig(X0);
fprintf(1,'Computing the analytical solution...');
pk_X0 = V*max(lam,0)*V';
fprintf(1,'Done! \n');
fprintf(1,'Computing the optimal solution by solving an SDP...');
cvx_begin sdp quiet
variable X(n,n) symmetric
minimize ( norm(X-X0,'fro') )
X >= 0;
cvx_end
fprintf(1,'Done! \n');
disp('-----------------------------------------------------------------');
disp('Verifying that the analytical solution and the solution obtained ');
disp('via CVX are equal by computing ||X_star - P_K(X0)||_F: ');
norm(X-pk_X0,'fro')
disp('Hence X_star and P_K(X0) are equal to working precision.');
Computing the analytical solution...Done!
Computing the optimal solution by solving an SDP...Done!
-----------------------------------------------------------------
Verifying that the analytical solution and the solution obtained
via CVX are equal by computing ||X_star - P_K(X0)||_F:
ans =
2.5435e-04
Hence X_star and P_K(X0) are equal to working precision.