Examples from the CVX User's guide
has_quadprog = exist( 'quadprog' );
has_quadprog = has_quadprog == 2 | has_quadprog == 3;
has_linprog = exist( 'linprog' );
has_linprog = has_linprog == 2 | has_linprog == 3;
rnstate = randn( 'state' ); randn( 'state', 1 );
s_quiet = cvx_quiet(true);
s_pause = cvx_pause(false);
cvx_clear; echo on
m = 16; n = 8;
A = randn(m,n);
b = randn(m,1);
x_ls = A \ b;
cvx_begin
variable x(n);
minimize( norm(A*x-b) );
cvx_end
echo off
disp( sprintf( '\nResults:\n--------\nnorm(A*x_ls-b): %6.4f\nnorm(A*x-b): %6.4f\ncvx_optval: %6.4f\ncvx_status: %s\n', norm(A*x_ls-b), norm(A*x-b), cvx_optval, cvx_status ) );
disp( 'Verify that x_ls == x:' );
disp( [ ' x_ls = [ ', sprintf( '%7.4f ', x_ls ), ']' ] );
disp( [ ' x = [ ', sprintf( '%7.4f ', x ), ']' ] );
disp( 'Residual vector:' );
disp( [ ' A*x-b = [ ', sprintf( '%7.4f ', A*x-b ), ']' ] );
disp( ' ' );
try, input( 'Press Enter/Return for the next example...' ); clc; catch, end
echo on
bnds = randn(n,2);
l = min( bnds, [] ,2 );
u = max( bnds, [], 2 );
if has_quadprog,
x_qp = quadprog( 2*A'*A, -2*A'*b, [], [], [], [], l, u );
else,
end
cvx_begin
variable x(n);
minimize( norm(A*x-b) );
subject to
x >= l;
x <= u;
cvx_end
echo off
if has_quadprog,
disp( sprintf( '\nResults:\n--------\nnorm(A*x_qp-b): %6.4f\nnorm(A*x-b): %6.4f\ncvx_optval: %6.4f\ncvx_status: %s\n', norm(A*x_qp-b), norm(A*x-b), cvx_optval, cvx_status ) );
disp( 'Verify that l <= x_qp == x <= u:' );
disp( [ ' l = [ ', sprintf( '%7.4f ', l ), ']' ] );
disp( [ ' x_qp = [ ', sprintf( '%7.4f ', x_qp ), ']' ] );
disp( [ ' x = [ ', sprintf( '%7.4f ', x ), ']' ] );
disp( [ ' u = [ ', sprintf( '%7.4f ', u ), ']' ] );
else,
disp( sprintf( '\nResults:\n--------\nnorm(A*x-b): %6.4f\ncvx_optval: %6.4f\ncvx_status: %s\n', norm(A*x-b), cvx_optval, cvx_status ) );
disp( 'Verify that l <= x <= u:' );
disp( [ ' l = [ ', sprintf( '%7.4f ', l ), ']' ] );
disp( [ ' x = [ ', sprintf( '%7.4f ', x ), ']' ] );
disp( [ ' u = [ ', sprintf( '%7.4f ', u ), ']' ] );
end
disp( 'Residual vector:' );
disp( [ ' A*x-b = [ ', sprintf( '%7.4f ', A*x-b ), ']' ] );
disp( ' ' );
try, input( 'Press Enter/Return for the next example...' ); clc; catch, end
echo on
if has_linprog,
f = [ zeros(n,1); 1 ];
Ane = [ +A, -ones(m,1) ; ...
-A, -ones(m,1) ];
bne = [ +b; -b ];
xt = linprog(f,Ane,bne);
x_lp = xt(1:n,:);
else,
end
cvx_begin
variable x(n);
minimize( norm(A*x-b,Inf) );
cvx_end
echo off
if has_linprog,
disp( sprintf( '\nResults:\n--------\nnorm(A*x_lp-b,Inf): %6.4f\nnorm(A*x-b,Inf): %6.4f\ncvx_optval: %6.4f\ncvx_status: %s\n', norm(A*x_lp-b,Inf), norm(A*x-b,Inf), cvx_optval, cvx_status ) );
disp( 'Verify that x_lp == x:' );
disp( [ ' x_lp = [ ', sprintf( '%7.4f ', x_lp ), ']' ] );
disp( [ ' x = [ ', sprintf( '%7.4f ', x ), ']' ] );
else,
disp( sprintf( '\nResults:\n--------\nnorm(A*x-b,Inf): %6.4f\ncvx_optval: %6.4f\ncvx_status: %s\n', norm(A*x-b,Inf), cvx_optval, cvx_status ) );
disp( 'Optimal vector:' );
disp( [ ' x = [ ', sprintf( '%7.4f ', x ), ']' ] );
end
disp( sprintf( 'Residual vector; verify that the peaks match the objective (%6.4f):', cvx_optval ) );
disp( [ ' A*x-b = [ ', sprintf( '%7.4f ', A*x-b ), ']' ] );
disp( ' ' );
try, input( 'Press Enter/Return for the next example...' ); clc; catch, end
echo on
if has_linprog,
f = [ zeros(n,1); ones(m,1); ones(m,1) ];
Aeq = [ A, -eye(m), +eye(m) ];
lb = [ -Inf*ones(n,1); zeros(m,1); zeros(m,1) ];
xzz = linprog(f,[],[], Aeq,b,lb,[]);
x_lp = xzz(1:n,:);
else,
end
cvx_begin
variable x(n);
minimize( norm(A*x-b,1) );
cvx_end
echo off
if has_linprog,
disp( sprintf( '\nResults:\n--------\nnorm(A*x_lp-b,1): %6.4f\nnorm(A*x-b,1): %6.4f\ncvx_optval: %6.4f\ncvx_status: %s\n', norm(A*x_lp-b,1), norm(A*x-b,1), cvx_optval, cvx_status ) );
disp( 'Verify that x_lp == x:' );
disp( [ ' x_lp = [ ', sprintf( '%7.4f ', x_lp ), ']' ] );
disp( [ ' x = [ ', sprintf( '%7.4f ', x ), ']' ] );
else,
disp( sprintf( '\nResults:\n--------\nnorm(A*x-b,1): %6.4f\ncvx_optval: %6.4f\ncvx_status: %s\n', norm(A*x-b,1), cvx_optval, cvx_status ) );
disp( 'Optimal vector:' );
disp( [ ' x = [ ', sprintf( '%7.4f ', x ), ']' ] );
end
disp( 'Residual vector; verify the presence of several zero residuals:' );
disp( [ ' A*x-b = [ ', sprintf( '%7.4f ', A*x-b ), ']' ] );
disp( ' ' );
try, input( 'Press Enter/Return for the next example...' ); clc; catch, end
echo on
k = 5;
cvx_begin
variable x(n);
minimize( norm_largest(A*x-b,k) );
cvx_end
echo off
temp = sort(abs(A*x-b));
disp( sprintf( '\nResults:\n--------\nnorm_largest(A*x-b,k): %6.4f\ncvx_optval: %6.4f\ncvx_status: %s\n', norm_largest(A*x-b,k), cvx_optval, cvx_status ) );
disp( 'Optimal vector:' );
disp( [ ' x = [ ', sprintf( '%7.4f ', x ), ']' ] );
disp( sprintf( 'Residual vector; verify a tie for %d-th place (%7.4f):', k, temp(end-k+1) ) );
disp( [ ' A*x-b = [ ', sprintf( '%7.4f ', A*x-b ), ']' ] );
disp( ' ' );
try, input( 'Press Enter/Return for the next example...' ); clc; catch, end
echo on
cvx_begin
variable x(n);
minimize( sum(huber(A*x-b)) );
cvx_end
echo off
disp( sprintf( '\nResults:\n--------\nsum(huber(A*x-b)): %6.4f\ncvx_optval: %6.4f\ncvx_status: %s\n', sum(huber(A*x-b)), cvx_optval, cvx_status ) );
disp( 'Optimal vector:' );
disp( [ ' x = [ ', sprintf( '%7.4f ', x ), ']' ] );
disp( 'Residual vector:' );
disp( [ ' A*x-b = [ ', sprintf( '%7.4f ', A*x-b ), ']' ] );
disp( ' ' );
try, input( 'Press Enter/Return for the next example...' ); clc; catch, end
echo on
p = 4;
C = randn(p,n);
d = randn(p,1);
cvx_begin
variable x(n);
minimize( norm(A*x-b) );
subject to
C*x == d;
norm(x,Inf) <= 1;
cvx_end
echo off
disp( sprintf( '\nResults:\n--------\nnorm(A*x-b): %6.4f\ncvx_optval: %6.4f\ncvx_status: %s\n', norm(A*x-b), cvx_optval, cvx_status ) );
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 ), ']' ] );
try, input( 'Press Enter/Return for the next example...' ); clc; catch, end
echo on
echo off
disp( ' ' );
disp( 'Generating tradeoff curve...' );
cvx_pause(false);
gamma = logspace( -2, 2, 20 );
l2norm = zeros(size(gamma));
l1norm = zeros(size(gamma));
fprintf( 1, ' gamma norm(x,1) norm(A*x-b)\n' );
fprintf( 1, '---------------------------------------\n' );
for k = 1:length(gamma),
fprintf( 1, '%8.4e', gamma(k) );
cvx_begin
variable x(n);
minimize( norm(A*x-b)+gamma(k)*norm(x,1) );
cvx_end
l1norm(k) = norm(x,1);
l2norm(k) = norm(A*x-b);
fprintf( 1, ' %8.4e %8.4e\n', l1norm(k), l2norm(k) );
end
plot( l1norm, l2norm );
xlabel( 'norm(x,1)' );
ylabel( 'norm(A*x-b)' );
grid
disp( 'Done. (Check out the graph!)' );
randn( 'state', rnstate );
cvx_quiet(s_quiet);
cvx_pause(s_pause);
Results:
--------
norm(A*x_ls-b): 2.0354
norm(A*x-b): 2.0354
cvx_optval: 2.0354
cvx_status: Solved
Verify that x_ls == x:
x_ls = [ -0.2628 0.8828 -0.0734 -1.0844 0.3249 -0.3330 0.0603 0.3802 ]
x = [ -0.2628 0.8828 -0.0734 -1.0844 0.3249 -0.3330 0.0603 0.3802 ]
Residual vector:
A*x-b = [ -0.3262 -0.0070 -0.9543 0.2447 -0.6418 -0.3426 -0.1870 0.2960 0.6024 -0.0440 0.6238 -0.7399 0.0849 0.9323 0.4799 -0.0763 ]
Results:
--------
norm(A*x-b): 4.3158
cvx_optval: 4.3158
cvx_status: Solved
Verify that l <= x <= u:
l = [ -0.9417 -1.0287 -0.2789 -0.9829 -2.0473 -3.0518 -2.3556 -0.5612 ]
x = [ -0.6355 0.3850 0.0819 -0.9829 0.6886 -1.1293 -0.2085 -0.0983 ]
u = [ -0.6355 0.3850 1.6414 0.0195 1.6208 -1.1293 -0.0485 0.3182 ]
Residual vector:
A*x-b = [ 0.2545 0.3802 0.3643 0.5763 -0.3124 1.0585 -0.4574 1.5770 0.7311 -2.0514 0.0611 -1.4060 -0.1811 2.6804 -0.0506 0.3074 ]
Results:
--------
norm(A*x-b,Inf): 0.7079
cvx_optval: 0.7079
cvx_status: Solved
Optimal vector:
x = [ -0.0944 0.8498 -0.1119 -1.1311 0.3804 -0.3017 0.2201 0.2488 ]
Residual vector; verify that the peaks match the objective (0.7079):
A*x-b = [ -0.0431 -0.0539 -0.7079 0.7079 -0.7079 -0.7079 -0.1800 0.5049 0.7079 -0.0040 0.7079 -0.7079 -0.1010 0.7079 0.7079 -0.2187 ]
Results:
--------
norm(A*x-b,1): 5.3359
cvx_optval: 5.3359
cvx_status: Solved
Optimal vector:
x = [ -0.3550 0.8934 -0.0375 -1.1827 0.1694 -0.3870 -0.2148 0.6712 ]
Residual vector; verify the presence of several zero residuals:
A*x-b = [ -0.7666 0.0129 -1.4977 0.0000 -0.5074 0.0000 -0.0000 0.0357 0.0000 0.0000 0.0299 -1.0842 -0.0000 1.4013 0.0000 -0.0000 ]
Results:
--------
norm_largest(A*x-b,k): 3.5394
cvx_optval: 3.5394
cvx_status: Solved
Optimal vector:
x = [ -0.0944 0.8498 -0.1119 -1.1311 0.3804 -0.3017 0.2201 0.2488 ]
Residual vector; verify a tie for 5-th place ( 0.7079):
A*x-b = [ -0.0431 -0.0539 -0.7079 0.7079 -0.7079 -0.7079 -0.1800 0.5049 0.7079 -0.0040 0.7079 -0.7079 -0.1010 0.7079 0.7079 -0.2187 ]
Results:
--------
sum(huber(A*x-b)): 4.1428
cvx_optval: 4.1428
cvx_status: Solved
Optimal vector:
x = [ -0.2628 0.8828 -0.0734 -1.0844 0.3249 -0.3330 0.0603 0.3802 ]
Residual vector:
A*x-b = [ -0.3262 -0.0070 -0.9543 0.2447 -0.6418 -0.3426 -0.1870 0.2960 0.6024 -0.0440 0.6238 -0.7399 0.0849 0.9323 0.4799 -0.0762 ]
Results:
--------
norm(A*x-b): 6.8098
cvx_optval: 6.8098
cvx_status: Solved
Optimal vector:
x = [ -1.0000 0.4013 1.0000 -0.2158 0.5613 0.3392 -0.4099 -0.4121 ]
Residual vector:
A*x-b = [ 0.4345 -0.5844 -0.7502 1.1046 -1.6866 2.4680 -2.4266 -1.8195 2.3077 1.0226 3.6275 0.5745 1.9436 0.5131 -1.4231 0.0129 ]
Equality constraints:
C*x = [ 0.0919 2.3710 -0.0087 2.1222 ]
d = [ 0.0919 2.3710 -0.0087 2.1222 ]
Generating tradeoff curve...
gamma norm(x,1) norm(A*x-b)
---------------------------------------
1.0000e-002 3.3796e+000 2.0355e+000
1.6238e-002 3.3658e+000 2.0357e+000
2.6367e-002 3.3433e+000 2.0362e+000
4.2813e-002 3.3068e+000 2.0374e+000
6.9519e-002 3.2472e+000 2.0408e+000
1.1288e-001 3.1498e+000 2.0497e+000
1.8330e-001 2.9878e+000 2.0737e+000
2.9764e-001 2.7617e+000 2.1280e+000
4.8329e-001 2.3445e+000 2.2924e+000
7.8476e-001 1.4082e+000 2.8895e+000
1.2743e+000 3.7699e-001 3.9509e+000
2.0691e+000 2.4222e-010 4.4813e+000
3.3598e+000 1.2158e-009 4.4813e+000
5.4556e+000 4.3395e-010 4.4813e+000
8.8587e+000 1.8988e-010 4.4813e+000
1.4384e+001 9.7038e-012 4.4813e+000
2.3357e+001 3.5491e-012 4.4813e+000
3.7927e+001 5.2747e-010 4.4813e+000
6.1585e+001 1.8694e-012 4.4813e+000
1.0000e+002 1.2580e-010 4.4813e+000
Done. (Check out the graph!)