# 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

## 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

## 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

## 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

## 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
``````