% 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 sedumi: 50 variables, 25 equality constraints
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
Split 10 free variables
eqs m = 25, order n = 61, dim = 61, blocks = 1
nnz(A) = 540 + 0, nnz(ADA) = 625, nnz(L) = 325
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            4.79E+01 0.000
  1 :   1.39E+01 1.66E+01 0.000 0.3464 0.9000 0.9000   2.26  1  1  1.4E+00
  2 :   1.89E+01 3.34E+00 0.000 0.2008 0.9000 0.9000   1.25  1  1  3.2E-01
  3 :   2.03E+01 6.88E-01 0.000 0.2062 0.9000 0.9000   1.03  1  1  6.9E-02
  4 :   2.06E+01 1.58E-01 0.000 0.2292 0.9000 0.9000   1.01  1  1  1.6E-02
  5 :   2.06E+01 3.08E-02 0.000 0.1951 0.9000 0.9000   1.00  1  1  3.2E-03
  6 :   2.07E+01 1.23E-04 0.000 0.0040 0.9990 0.9989   1.00  1  1  
iter seconds digits       c*x               b*y
  6      0.0  15.3  2.0661712233e+01  2.0661712233e+01
|Ax-b| =   7.5e-15, [Ay-c]_+ =   2.0E-15, |x|=  1.1e+01, |y|=  7.5e+00

Detailed timing (sec)
   Pre          IPM          Post
0.000E+00    3.000E-02    0.000E+00    
Max-norms: ||b||=2.153708e+00, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.97849.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +20.6617
norm(A*x-b,1):
   ans   =   20.6617
Optimal vector:
   x     = [  0.1655  0.6782 -0.4228  0.9150 -0.0906  0.1216 -0.3164  0.1219 -0.6398 -0.6228 ]
Residual vector:
   A*x-b = [  0.4010  1.7552  1.9558  0.9247 -1.1188 -0.0000  1.5849 -3.1664  1.1300 -2.3871 -0.9826 -0.5744 -0.5122  1.5118 -2.0815 -0.5752  0.0000 -0.0000 -0.0000  0.0000 ]
Equality constraints:
   C*x   = [ -0.5847 -2.1537  1.3786  0.0757  1.2487 ]
   d     = [ -0.5847 -2.1537  1.3786  0.0757  1.2487 ]
Lagrange multiplier for C*x==d:
   y     = [ -1.5438 -5.6554  1.4361  0.3269 -1.8537 ]