% This script constructs a random equality-constrained norm minimization
% problem and solves it using CVX. You can also change p to +2 or +Inf
% to produce different results. Alternatively, you an replace
%     norm( A * x - b, p )
% with
%     norm_largest( A * x - b, 'largest', p )
% for 1 <= p <= 2 * n.

% Generate data
p = 1;
n = 10; m = 2*n; q=0.5*n;
A = randn(m,n);
b = randn(m,1);
C = randn(q,n);
d = randn(q,1);

% Create and solve problem
cvx_begin
   variable x(n)
   dual variable y
   minimize( norm( A * x - b, p ) )
   subject to
        y : C * x == d;
cvx_end

% Display results
disp( sprintf( 'norm(A*x-b,%g):', p ) );
disp( [ '   ans   =   ', sprintf( '%7.4f', norm(A*x-b,p) ) ] );
disp( 'Optimal vector:' );
disp( [ '   x     = [ ', sprintf( '%7.4f ', x ), ']' ] );
disp( 'Residual vector:' );
disp( [ '   A*x-b = [ ', sprintf( '%7.4f ', A*x-b ), ']' ] );
disp( 'Equality constraints:' );
disp( [ '   C*x   = [ ', sprintf( '%7.4f ', C*x ), ']' ] );
disp( [ '   d     = [ ', sprintf( '%7.4f ', d   ), ']' ] );
disp( 'Lagrange multiplier for C*x==d:' );
disp( [ '   y     = [ ', sprintf( '%7.4f ', y ), ']' ] );
 
Calling SDPT3: 50 variables, 25 equality constraints
------------------------------------------------------------

 num. of constraints = 25
 dim. of socp   var  = 40,   num. of socp blk  = 20
 dim. of free   var  = 10 *** convert ublk to lblk
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
    NT      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|8.3e-01|2.9e+01|1.5e+04| 8.471540e+01  0.000000e+00| 0:0:00| chol  1  1 
 1|1.000|0.964|3.1e-06|1.1e+00|2.8e+02| 1.464735e+02  1.231256e+01| 0:0:00| chol  1  1 
 2|0.961|0.990|6.9e-07|2.0e-02|1.0e+01| 2.225613e+01  1.265515e+01| 0:0:00| chol  1  1 
 3|0.935|0.790|2.0e-05|5.0e-03|1.8e+00| 1.789246e+01  1.620084e+01| 0:0:00| chol  1  1 
 4|0.862|0.455|2.3e-05|2.8e-03|7.7e-01| 1.736289e+01  1.665120e+01| 0:0:00| chol  1  1 
 5|1.000|0.442|1.3e-07|1.6e-03|3.8e-01| 1.725395e+01  1.690178e+01| 0:0:00| chol  1  1 
 6|1.000|0.824|3.0e-08|2.7e-04|6.4e-02| 1.722824e+01  1.716909e+01| 0:0:00| chol  1  1 
 7|0.988|0.777|5.9e-09|6.1e-05|1.4e-02| 1.722561e+01  1.721288e+01| 0:0:00| chol  1  1 
 8|1.000|0.283|1.3e-09|4.4e-05|9.4e-03| 1.722491e+01  1.721622e+01| 0:0:00| chol  1  1 
 9|1.000|0.280|4.7e-10|3.1e-05|6.7e-03| 1.722479e+01  1.721858e+01| 0:0:00| chol  1  1 
10|1.000|0.688|1.7e-10|9.8e-06|2.1e-03| 1.722470e+01  1.722275e+01| 0:0:00| chol  1  1 
11|0.993|0.866|4.6e-11|1.4e-05|3.7e-04| 1.722465e+01  1.722439e+01| 0:0:00| chol  1  1 
12|0.988|0.988|1.9e-12|2.5e-06|2.0e-05| 1.722465e+01  1.722465e+01| 0:0:00| chol  1  1 
13|1.000|0.944|1.5e-13|1.4e-07|1.2e-06| 1.722465e+01  1.722465e+01| 0:0:00| chol  1  1 
14|0.575|0.944|6.6e-14|8.1e-09|9.7e-08| 1.722465e+01  1.722465e+01| 0:0:00|
  stop: max(relative gap, infeasibilities) < 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 14
 primal objective value =  1.72246500e+01
 dual   objective value =  1.72246500e+01
 gap := trace(XZ)       = 9.66e-08
 relative gap           = 2.72e-09
 actual relative gap    = 1.18e-09
 rel. primal infeas     = 6.57e-14
 rel. dual   infeas     = 8.07e-09
 norm(X), norm(y), norm(Z) = 7.5e+00, 6.8e+00, 6.2e+00
 norm(A), norm(b), norm(C) = 2.4e+01, 5.8e+00, 5.5e+00
 Total CPU time (secs)  = 0.17  
 CPU time per iteration = 0.01  
 termination code       =  0
 DIMACS: 1.3e-13  0.0e+00  2.2e-08  0.0e+00  1.2e-09  2.7e-09
-------------------------------------------------------------------
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +17.2247
 
norm(A*x-b,1):
   ans   =   17.2246
Optimal vector:
   x     = [ -0.5253 -0.6745  0.3881 -0.3432  0.6278 -0.7627  0.0400  0.6648  1.0041 -0.4625 ]
Residual vector:
   A*x-b = [  0.0000 -0.0000 -0.8708 -1.0281 -0.0000  1.5043  0.0000  1.3248  0.2570 -2.1089  2.3600  0.7021 -1.2957  0.9965  0.1152  0.8386 -2.2360  0.0000 -1.1761 -0.4105 ]
Equality constraints:
   C*x   = [ -0.0798  0.7377  1.5352  1.2677  1.9951 ]
   d     = [ -0.0798  0.7377  1.5352  1.2677  1.9951 ]
Lagrange multiplier for C*x==d:
   y     = [  2.2526  0.2934  1.6808 -0.2579  4.4457 ]