Using Cvx within a for loop

Sir, I want to solve the optimization problem using cvx for a set of values using for loop which i have given below, but the result which I am getting is not same If I solve taking only one value at a time without using for loop. Why this is happening I do not understand. I will be grateful if someone help in this regard.

clc; clear all;
A0=[-7 3; 3 -10];
A1=[2 4; 4 -1];
x1=-3:1:3; y1=-4:1:4;
[X1, Y1]=meshgrid(x1,y1); Z2=complex(X1, Y1);
[k1, k2]=size(Z2);
O=zeros(2);
for i=1:k1
for j=1:k2
M=inv(A0+Z2(i,j)*A1);
G=[M'*M  Z2(i,j)*M'*M; conj(Z2(i,j))*M'*M abs(Z2(i,j))^2*M'*M];
H1= i*[M-M'  Z2(i,j)*M; -conj(Z2(i,j))*M' O];
H2= i*[O -M'; M  Z2(i,j)*M-conj(Z2(i,j))*M'];
cvx_begin
variable u;
variable v;
(minimize(lambda_max(G+u*H1+v*H2)));
cvx_end
        V1(i,j)= cvx_optval;
       end
end

Output:
V1 = [ 0.1142 0.0855 0.0617 0.0470 0.0496 0.0548 0.0581
0.1483 0.0958 0.0573 0.0437 0.0538 0.0625 0.0658
0.2452 0.1198 0.0496 0.0427 0.0659 0.0817 0.0801
0.7601 0.1846 0.0383 0.0407 0.1311 0.1430 0.1032
37.9737 0.2730 0.0313 0.0378 13.5474 0.2601 0.1193
0.7601 0.1846 0.0383 0.0407 0.1311 0.1430 0.1032
0.2452 0.1198 0.0496 0.0427 0.0659 0.0817 0.0801
0.1483 0.0958 0.0573 0.0437 0.0538 0.0625 0.0658
0.1142 0.0855 0.0617 0.0470 0.0496 0.0548 0.0581 ]
u, v in this case are coming as 0.

Please show the complete code, all output, including display of values of u and v after CVX concludes, for both

  1. use of for loop
  2. one case you are claiming is inconsistent with for loop result

clc; clear all;
A0=[-7 3; 3 -10];
A1=[2 4; 4 -1];
O=zeros(2);
lam =3+4i;
M=inv(A0+lamA1);
G=[M’M lamM’M; conj(lam)M’M abs(lam)^2M’M];
H1= i
[M-M’ lam
M; -conj(lam)M’ O];
H2= i
[O -M’; M lam
M-conj(lam)M’];
cvx_begin
variable u;
variable v;
(minimize(lambda_max(G+u
H1+v
H2)));
cvx_end
c1= cvx_optval
u
v

Output:
Calling SDPT3 4.0: 16 variables, 3 equality constraints
For improved efficiency, SDPT3 is solving the dual problem.

num. of constraints = 3
dim. of sdp var = 8, num. of sdp blk = 1


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|2.0e+01|2.7e+01|8.0e+02|-9.175664e-01 0.000000e+00| 0:0:00| chol 1 1
1|0.991|1.000|1.8e-01|1.0e-01|2.5e+01|-1.519214e-02 -1.824971e+01| 0:0:00| chol 1 1
2|1.000|1.000|8.0e-08|1.0e-02|1.2e+00|-6.794268e-03 -1.196760e+00| 0:0:00| chol 1 1
3|1.000|0.974|6.8e-08|1.2e-03|3.1e-02|-6.956998e-03 -3.696524e-02| 0:0:00| chol 1 1
4|1.000|1.000|1.2e-08|1.0e-04|1.2e-02|-1.695377e-02 -2.875992e-02| 0:0:00| chol 1 1
5|1.000|0.926|4.9e-11|1.7e-05|8.3e-04|-1.970072e-02 -2.051449e-02| 0:0:00| chol 1 1
6|0.964|1.000|6.6e-12|1.0e-06|2.4e-05|-2.029858e-02 -2.032232e-02| 0:0:00| chol 1 1
7|0.983|0.992|2.8e-11|7.6e-09|3.8e-07|-2.031774e-02 -2.031811e-02| 0:0:00| chol 1 1
8|1.000|1.000|1.2e-11|2.0e-12|2.8e-08|-2.031803e-02 -2.031805e-02| 0:0:00| chol 1 1
9|1.000|1.000|1.2e-12|2.3e-12|8.9e-10|-2.031805e-02 -2.031805e-02| 0:0:00|
stop: max(relative gap, infeasibilities) < 1.49e-08

number of iterations = 9
primal objective value = -2.03180456e-02
dual objective value = -2.03180465e-02
gap := trace(XZ) = 8.94e-10
relative gap = 8.59e-10
actual relative gap = 8.57e-10
rel. primal infeas (scaled problem) = 1.19e-12
rel. dual " " " = 2.31e-12
rel. primal infeas (unscaled problem) = 0.00e+00
rel. dual " " " = 0.00e+00
norm(X), norm(y), norm(Z) = 1.4e+00, 7.3e-01, 7.0e-02
norm(A), norm(b), norm© = 6.4e+00, 2.0e+00, 1.0e+00
Total CPU time (secs) = 0.33
CPU time per iteration = 0.04
termination code = 0
DIMACS: 1.2e-12 0.0e+00 2.4e-12 0.0e+00 8.6e-10 8.6e-10


Status: Solved
Optimal value (cvx_optval): +0.020318

c1 =

0.0203

u =

-0.1025

v =

-0.7258

This is the code by which we can solve the optimization problem for different points of lam. In the previous code V1(i, j) stores the values of the optimization problem for different Z2(i, j). Now if we calculate the optimization value individually by the code given here the values are not matching with the previous code. For example If we calculate using for loop at the last point of Z2(i,j), i.e, 3+4i we are getting the optimization value +0.0580751 in V1(i,j), but when we calculate individually by the code attached here we are getting the value as 0.0203, which is not matching. Why this is happening can you help sir.

You have not complied with my directions. I asked you to show the complete code and output combination. You did not.

I did not get a discrepancy when I tried it.

clc; clear all;
A0=[-7 3; 3 -10];
A1=[2 4; 4 -1];
[v, d] = eig(A0,-A1);
L2=diag(d);
plot(real(L2), imag(L2),’+’), axis([-5 5 -4 4]);
hold on
x1=-3:10/10:3; y1=-4:10/10:4;
[X1, Y1]=meshgrid(x1,y1); Z2=complex(X1, Y1);
[k1, k2]=size(Z2);
O=zeros(2);
for i=1:k1
for j=1:k2
M=inv(A0+Z2(i,j)*A1);
G=[M’*M Z2(i,j)*M’*M; conj(Z2(i,j))*M’M abs(Z2(i,j))^2M’M];
H1= i
[M-M’ Z2(i,j)M; -conj(Z2(i,j))M’ O];
H2= i
[O -M’; M Z2(i,j)M-conj(Z2(i,j))M’];
cvx_begin
variable u;
variable v;
(minimize(lambda_max(G+u
H1+v
H2)));
u
v
cvx_end
V1(i,j)= cvx_optval;
T1(i, j)= -0.5
log10(cvx_optval);
end
end
contour(X1, Y1, T1,[ 0.4839, 0.38, 0.2075, -0.0485 ]);
Output:
u =

cvx real affine expression (scalar)

v =

cvx real affine expression (scalar)

Calling SDPT3 4.0: 16 variables, 1 equality constraints
For improved efficiency, SDPT3 is solving the dual problem.

num. of constraints = 1
dim. of sdp var = 8, num. of sdp blk = 1


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|2.0e+01|2.7e+01|8.0e+02|-8.182702e-01 0.000000e+00| 0:0:00| chol 1 1
1|1.000|1.000|2.2e-15|1.0e-01|1.9e+01|-2.051232e-02 -1.895945e+01| 0:0:00| chol 1 1
2|1.000|0.987|0.0e+00|1.1e-02|2.4e-01|-2.057049e-02 -2.544382e-01| 0:0:00| chol 1 1
3|1.000|0.874|0.0e+00|2.3e-03|3.2e-02|-2.531525e-02 -5.586228e-02| 0:0:00| chol 1 1
4|0.759|1.000|5.6e-17|1.0e-04|1.4e-02|-4.910598e-02 -6.342016e-02| 0:0:00| chol 1 1
5|1.000|0.972|5.6e-17|1.2e-05|3.2e-04|-5.277825e-02 -5.308830e-02| 0:0:00| chol 1 1
6|0.989|0.989|5.6e-17|1.1e-06|3.7e-06|-5.290087e-02 -5.290369e-02| 0:0:00| chol 1 1
7|1.000|0.993|1.1e-16|7.4e-09|5.5e-08|-5.290226e-02 -5.290231e-02| 0:0:00| chol 1 1
8|1.000|0.995|0.0e+00|3.8e-11|7.1e-10|-5.290229e-02 -5.290229e-02| 0:0:00|
stop: max(relative gap, infeasibilities) < 1.49e-08

number of iterations = 8
primal objective value = -5.29022896e-02
dual objective value = -5.29022903e-02
gap := trace(XZ) = 7.05e-10
relative gap = 6.38e-10
actual relative gap = 6.12e-10
rel. primal infeas (scaled problem) = 0.00e+00
rel. dual " " " = 3.84e-11
rel. primal infeas (unscaled problem) = 0.00e+00
rel. dual " " " = 0.00e+00
norm(X), norm(y), norm(Z) = 1.4e+00, 5.3e-02, 5.7e-02
norm(A), norm(b), norm© = 2.4e+00, 2.0e+00, 1.0e+00
Total CPU time (secs) = 0.07
CPU time per iteration = 0.01
termination code = 0
DIMACS: 0.0e+00 0.0e+00 3.9e-11 0.0e+00 6.1e-10 6.4e-10


Status: Solved
Optimal value (cvx_optval): +0.0548478

u =

cvx real affine expression (scalar)

v =

cvx real affine expression (scalar)

Calling SDPT3 4.0: 16 variables, 1 equality constraints
For improved efficiency, SDPT3 is solving the dual problem.

num. of constraints = 1
dim. of sdp var = 8, num. of sdp blk = 1


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|2.0e+01|2.7e+01|8.0e+02|-8.571028e-01 0.000000e+00| 0:0:00| chol 1 1
1|1.000|1.000|2.2e-15|1.0e-01|1.9e+01|-2.148831e-02 -1.896028e+01| 0:0:00| chol 1 1
2|1.000|0.987|0.0e+00|1.1e-02|2.4e-01|-2.155177e-02 -2.581920e-01| 0:0:00| chol 1 1
3|1.000|0.868|1.1e-16|2.3e-03|3.5e-02|-2.674349e-02 -5.955357e-02| 0:0:00| chol 1 1
4|0.758|1.000|0.0e+00|1.0e-04|1.5e-02|-5.245160e-02 -6.769491e-02| 0:0:00| chol 1 1
5|1.000|0.973|0.0e+00|1.2e-05|3.4e-04|-5.635014e-02 -5.667809e-02| 0:0:00| chol 1 1
6|0.989|0.989|0.0e+00|1.1e-06|3.9e-06|-5.647791e-02 -5.648092e-02| 0:0:00| chol 1 1
7|1.000|0.993|1.1e-16|7.4e-09|5.8e-08|-5.647936e-02 -5.647941e-02| 0:0:00| chol 1 1
8|1.000|0.995|1.1e-16|3.9e-11|7.5e-10|-5.647939e-02 -5.647939e-02| 0:0:00|
stop: max(relative gap, infeasibilities) < 1.49e-08

number of iterations = 8
primal objective value = -5.64793878e-02
dual objective value = -5.64793885e-02
gap := trace(XZ) = 7.47e-10
relative gap = 6.71e-10
actual relative gap = 6.45e-10
rel. primal infeas (scaled problem) = 1.11e-16
rel. dual " " " = 3.88e-11
rel. primal infeas (unscaled problem) = 0.00e+00
rel. dual " " " = 0.00e+00
norm(X), norm(y), norm(Z) = 1.4e+00, 5.6e-02, 6.1e-02
norm(A), norm(b), norm© = 2.4e+00, 2.0e+00, 1.0e+00
Total CPU time (secs) = 0.06
CPU time per iteration = 0.01
termination code = 0
DIMACS: 1.1e-16 0.0e+00 4.0e-11 0.0e+00 6.5e-10 6.7e-10


Status: Solved
Optimal value (cvx_optval): +0.0580751
Second code:
clc; clear all;
A0=[-7 3; 3 -10];
A1=[2 4; 4 -1];
O=zeros(2);
lam =3+4i;
M=inv(A0+lamA1);
G=[M’M lamM’M; conj(lam)M’M abs(lam)^2M’M];
H1= i
[M-M’ lam
M; -conj(lam)M’ O];
H2= i
[O -M’; M lam
M-conj(lam)M’];
cvx_begin
variable u;
variable v;
(minimize(lambda_max(G+u
H1+v
H2)));
cvx_end
c1= cvx_optval
err1 = (cvx_optval)^(-1/2)
Output:

Calling SDPT3 4.0: 16 variables, 3 equality constraints
For improved efficiency, SDPT3 is solving the dual problem.

num. of constraints = 3
dim. of sdp var = 8, num. of sdp blk = 1


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|2.0e+01|2.7e+01|8.0e+02|-9.175664e-01 0.000000e+00| 0:0:00| chol 1 1
1|0.991|1.000|1.8e-01|1.0e-01|2.5e+01|-1.519214e-02 -1.824971e+01| 0:0:00| chol 1 1
2|1.000|1.000|8.0e-08|1.0e-02|1.2e+00|-6.794268e-03 -1.196760e+00| 0:0:00| chol 1 1
3|1.000|0.974|6.8e-08|1.2e-03|3.1e-02|-6.956998e-03 -3.696524e-02| 0:0:00| chol 1 1
4|1.000|1.000|1.2e-08|1.0e-04|1.2e-02|-1.695377e-02 -2.875992e-02| 0:0:00| chol 1 1
5|1.000|0.926|4.9e-11|1.7e-05|8.3e-04|-1.970072e-02 -2.051449e-02| 0:0:00| chol 1 1
6|0.964|1.000|6.6e-12|1.0e-06|2.4e-05|-2.029858e-02 -2.032232e-02| 0:0:00| chol 1 1
7|0.983|0.992|2.8e-11|7.6e-09|3.8e-07|-2.031774e-02 -2.031811e-02| 0:0:00| chol 1 1
8|1.000|1.000|1.2e-11|2.0e-12|2.8e-08|-2.031803e-02 -2.031805e-02| 0:0:00| chol 1 1
9|1.000|1.000|1.2e-12|2.3e-12|8.9e-10|-2.031805e-02 -2.031805e-02| 0:0:00|
stop: max(relative gap, infeasibilities) < 1.49e-08

number of iterations = 9
primal objective value = -2.03180456e-02
dual objective value = -2.03180465e-02
gap := trace(XZ) = 8.94e-10
relative gap = 8.59e-10
actual relative gap = 8.57e-10
rel. primal infeas (scaled problem) = 1.19e-12
rel. dual " " " = 2.31e-12
rel. primal infeas (unscaled problem) = 0.00e+00
rel. dual " " " = 0.00e+00
norm(X), norm(y), norm(Z) = 1.4e+00, 7.3e-01, 7.0e-02
norm(A), norm(b), norm© = 6.4e+00, 2.0e+00, 1.0e+00
Total CPU time (secs) = 0.30
CPU time per iteration = 0.03
termination code = 0
DIMACS: 1.2e-12 0.0e+00 2.4e-12 0.0e+00 8.6e-10 8.6e-10


Status: Solved
Optimal value (cvx_optval): +0.020318

c1 =

0.0203

err1 =

7.0155

This is what I am getting after running the codes. Please help.

Have you shown the values of G, H1, H2` are the same between the runs you are comparing? If they are not the same, the discrepancy has nothing to with CVX, and is purely a matter of 'regular" MATLAB.