Coding Using MATLAB
Coding Using MATLAB
o Examples #1-#5 in this lecture build on the examples from the quickstart CVX
webpage: http://web.cvxr.com/cvx/doc/quickstart.html
o Note: Examples #1-#5 are randomly generated for each test run; so it is expected to
obtain different answers than the sample ones shown on these slides.
o The following MATLAB code generates the problem data and the analytic problem
solution:
clc
clear all
%least-squares problem
m = 16; n = 8;
A = randn(m,n);
b = randn(m,1);
−1
x_ls = A\b; o 𝑥 = 𝐴𝑇 𝐴 𝐴𝑇 𝑏 is the analytic solution of the
unconstrained least-squares problem.
cvx_begin
variable x(n) o Declare optimization variable
minimize( norm(A*x-b) ) o Define objective function to minimize
cvx_end
o That is, upper and lower bounds on the optimization variable are added to the
optimization problem in Example #1.
o The following lines are added to the code when defining the problem parameters:
bnds = randn(n,2);
l = min( bnds, [], 2 );
u = max( bnds, [], 2 );
cvx_begin
variable x(n) o Declare optimization variable
minimize( norm(A*x-b) ) o Define objective function to minimize
subject to o Add problem constraints
l <= x <= u
cvx_end
check2 =
4.4170e-10
check2=min(x-l)
check3=min(u-x)
check3 =
2.8622e-10
check1 =
check1=max(abs(x-x_ls)) 1.3874
check4=min(x_ls-l)
check5=min(u-x_ls) check4 =
-0.6463
check5 =
-1.2353
ops = sdpsettings('solver','sdpt3','shift',1e-6,'verbose',1);
num. of constraints = 9
dim. of socp var = 17, num. of socp blk = 1
dim. of linear var = 16
*******************************************************************
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|1.6e+00|6.6e+00|1.6e+03| 7.958975e+01 0.000000e+00| 0:0:00| chol 1 1
1|0.856|0.944|2.2e-01|4.4e-01|1.6e+02| 6.037779e+01 -4.245208e+00| 0:0:00| chol 1 1
2|1.000|0.990|2.2e-06|1.2e-02|3.0e+01| 2.139144e+01 -6.874668e+00| 0:0:00| chol 1 1
3|0.862|0.952|1.4e-07|1.2e-03|4.0e+00|-1.815029e+00 -5.813998e+00| 0:0:00| chol 1 1
4|0.430|0.866|2.3e-07|2.3e-04|2.4e+00|-2.723919e+00 -5.147645e+00| 0:0:00| chol 1 1
number of iterations = 10
primal objective value = -4.73315546e+00
dual objective value = -4.73315551e+00
gap := trace(XZ) = 5.01e-08
relative gap = 4.79e-09
actual relative gap = 4.72e-09
rel. primal infeas (scaled problem) = 3.69e-12
rel. dual " " " = 6.70e-11
rel. primal infeas (unscaled problem) = 0.00e+00
rel. dual " " " = 0.00e+00
norm(X), norm(y), norm(Z) = 4.1e+00, 4.9e+00, 7.3e+00
norm(A), norm(b), norm(C) = 1.2e+01, 2.0e+00, 5.8e+00
Total CPU time (secs) = 0.34
CPU time per iteration = 0.03
termination code = 0
DIMACS: 3.7e-12 0.0e+00 1.3e-10 0.0e+00 4.7e-09 4.8e-09
-------------------------------------------------------------------
Dany Abou Jaoude Coding using MATLAB 13
Example #2
o Sample output continued:
yalmipversion: '20190425'
yalmiptime: 0.5992
solvertime: 0.5208
info: 'Successfully solved (SDPT3-4)'
problem: 0
o SEDUMI can also be chosen as the solver instead of SDPT3, and other solver options
may be specified. For example:
ops = sdpsettings('solver','sedumi','shift',1e-6,'verbose',0);
o The solutions obtained using CVX and YALMIP with the various solvers are very close.
o Note that, while an equivalent LP formulation to this problem is possible, we here solve for the convex
optimization problem as given.
o The tic/toc commands are added to time the solvers. (E.g., Elapsed time is 1.494506 seconds.)
o It is a insightful exercise to try to solve problems for increasing values of 𝑛 and 𝑚, and to see the
resulting effect on the total solution time.
tic
xyalmip=sdpvar(n,1);
obj=norm(A*xyalmip-b,Inf);
ops = sdpsettings('solver','sdpt3','verbose',0);
toc
m = 16; n = 8; cvx_begin
A = randn(m,n); variable x(n);
b = randn(m,1); minimize( norm(A*x-b) );
subject to
p = 4; C*x == d;
C = randn(p,n); norm(x,Inf) <= 1;
d = randn(p,1); cvx_end
Csts=[];
ops = sdpsettings('solver','sdpt3','verbose',1);
optimize(Csts,obj,ops)
xyalmip=value(xyalmip);
obj=value(obj);
o To obtain the minimum value of the objective function using CVX, check2=abs(cvx_optval-obj)
the command ‘cvx_optval’ is used. check2 =
6.7031e-08
cvx_begin
variable y(n);
minimize( norm(A*y-b) ); Error using cvxprob/newcnstr (line 192)
subject to Disciplined convex programming error:
C*y == d; Invalid constraint: {convex} == {real constant}
norm(y,Inf) == 1;
cvx_end
yyalmip=sdpvar(n,1);
obj=norm(A*yyalmip-b);
Csts=[];
Warning: Solver not applicable (sdpt3)
solvertime: 0
Csts = Csts + [C*yyalmip == d];
info: 'Solver not applicable (sdpt3)'
Csts = Csts + [norm(yyalmip,Inf) == 1];
problem: -4
yalmiptime: 0.3380
ops = sdpsettings('solver','sdpt3','verbose',1);
optimize(Csts,obj,ops)
for k = 1:length(gamma)
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);
end
plot( l1norm, l2norm );
xlabel( 'norm(x,1)' );
ylabel( 'norm(A*x-b)' );
grid on
subject to
n=3;
check1 =
11
check1=x(1,1) + 0*x(1,2) + 2*x(1,3) + 3*x(2,2) + 14*x(2,3) + 5*x(3,3)
check2 =
check2=0*x(1,1) + 4*x(1,2) + 16*x(1,3) + 6*x(2,2) + 0*x(2,3) + 4*x(3,3) 19.0000
check3 =
check3=eig(x) 0.0000
0.0000
1.8990
Csts=[];
Csts = Csts + [xyalmip(1,1) + 0*xyalmip(1,2) + 2*xyalmip(1,3) + 3*xyalmip(2,2) ...
+ 14*xyalmip(2,3) + 5*xyalmip(3,3)==11];
Csts = Csts + [0*xyalmip(1,1) + 4*xyalmip(1,2) + 16*xyalmip(1,3) + 6*xyalmip(2,2) ...
+ 0*xyalmip(2,3) + 4*xyalmip(3,3)==19];
Csts = Csts + [xyalmip>=0]; o The matrix 𝑥𝑦𝑎𝑙𝑚𝑖𝑝 is positive semidefinite.
ops = sdpsettings('solver','sdpt3','shift',1e-6,'verbose',1);
optimize(Csts,obj,ops)
xyalmip=value(xyalmip);
obj=value(obj);
obj =
13.9022
o If the matrix inequality is made strict, i.e., the following is used in the YALMIP code: Csts = Csts + [xyalmip>0];
then, the following warning line is received before the solution output:
Warning: Strict inequalities are not supported. A non-strict has been added instead')