Why do I get this error?I think there is no error in my writing, but NaN affects the operation of my program afterwards, how can I avoid it?

cvx_begin sdp
cvx_solver sdpt3
variable W_IRS(M,M,K) hermitian
W_sum=zeros(M,M);
for i=1:K
W_sum=W_sum+W_IRS(:,:,i);
end
C=0;
C1=0;
C2=0;
D1=0;
D1_tr=0;
D21=0;
D22=0;
D2=0;
D2_tr=0;
D2_sum=0;
for i=1:K
Bob_zi=real(trace(H_k(:,:,k)'U_preH_k(:,:,k)W_sum));
C1=(1/log(2))
(-log(Bob_zi+noisepow_Bob^2));
for j=1:L
Eve_mu=real(trace(H_l(:,:,j)'U_preH_l(:,:,j)(W_sum-W_IRS(:,:,i))));
C2=C2-1/L
(1/log(2))(log(Eve_mu+noisepow_Eve^2));
end
C=C+C1+C2;
C2=0;
end
for i=1:K
for k=[1:i-1,i+1:K]
D1_mu=trace(H_k(:,:,k)'U_preH_k(:,:,k)
(W_sum_pre-W_pre(:,:,k)));
D1_zi=H_k(:,:,k)‘U_preH_k(:,:,k);
D1=D1-1/log(2)(D1_zi/(D1_mu+noisepow_Bob^2));
end
D1_tr=D1_tr+real(trace(D1’W_IRS(:,:,i)));
D1=0;
end
for l=1:L
for i=1:K
D21_mu=L
trace(H_l(:,:,l)'U_preH_l(:,:,l)W_pre(:,:,i))+trace(H_l(:,:,l)'U_preH_l(:,:,l)(W_sum_pre-W_pre(:,:,i)));
D21_zi=L
H_l(:,:,l)‘U_preH_l(:,:,l);
D21=D21_zi/(D21_mu+noisepow_Eve^2);
for k=[1:i-1,i+1:K]
D22_mu=Ltrace(H_l(:,:,l)'U_preH_l(:,:,l)W_pre(:,:,k))+trace(H_l(:,:,l)'U_preH_l(:,:,l)(W_sum_pre-W_pre(:,:,k)));
D22_zi=H_l(:,:,l)'U_preH_l(:,:,l);
D22=D22+D22_zi/(D22_mu+noisepow_Eve^2);
end
D2=D21+D22;
D2_tr=D2_tr+real(trace((-1/(L
log(2))*D2)’*W_IRS(:,:,i)));
D22=0;
end
D2_sum=D2_sum+D2_tr;
D2_tr=0;
end
pow=0;
for i=1:K
pow=pow+trace(W_IRS(:,:,i));
end
minimize (C-D1_tr-D2_sum)
subject to
pow <= P;
for i=1:K
W_IRS(:,:,i) == hermitian_semidefinite(M);
end
cvx_end
cvx_status;
if (cvx_optval==inf) || (cvx_optval==-inf)
disp([’%%%%%% CVX SDP SCA error: ‘,num2str(cvx_status),’ %%%%%% ']);
inf_flag = 1;
break
end

W_sum=zeros(M,M);
for i=1:K
    W_sum=W_sum+W_IRS(:,:,i);
end
cvx_begin sdp
cvx_solver sdpt3
variable U(N+1,N+1) hermitian
C=0;
C1=0;
C2=0;
D1=0;
D1_tr=0;
D2=0;
D2_tr=0;
D2_sum=0;
for i=1:K
    Bob_zi=real(trace(H_k(:,:,k)*W_sum*H_k(:,:,k)'*U));
    C1=(1/log(2))*(-log(Bob_zi+noisepow_Bob^2));
    for j=1:L
        Eve_mu=real(trace(H_l(:,:,j)*(W_sum-W_IRS(:,:,i))*H_l(:,:,j)'*U));
        C2=C2-1/L*(1/log(2))*(log(Eve_mu++noisepow_Eve^2));
    end
    C=C+C1+C2;
    C2=0;
end
for i=1:K
    D1_mu=trace(H_k(:,:,i)*(W_sum-W_IRS(:,:,i))*H_k(:,:,i)'*U_pre);
    D1_zi=H_k(:,:,i)*(W_sum-W_IRS(:,:,i))*H_k(:,:,i)';
    D1=D1-1/log(2)*(D1_zi/(D1_mu+noisepow_Bob^2));
end
D1_tr=real(trace(D1'*U));
for l=1:L
    for i=1:K
    D2_mu=L*trace(H_l(:,:,l)*W_IRS(:,:,i)*H_l(:,:,l)'*U_pre)+trace(H_l(:,:,l)*(W_sum-W_IRS(:,:,i))*H_l(:,:,l)'*U_pre);
    D2_zi=L*H_l(:,:,l)*W_IRS(:,:,i)*H_l(:,:,l)'+H_l(:,:,l)*(W_sum-W_IRS(:,:,i))*H_l(:,:,l)';
    D2=D2-1/(L*log(2))*(D2_zi/(D2_mu+noisepow_Eve^2));
    end
    D2_tr=real(trace(D2'*U));
    D2_sum=D2_sum+D2_tr;
    D2_tr=0;
    D2=0;
end
minimize (C-D1_tr-D2_sum)
subject to
for n=1:N+1
    U(n,n) == 1;
end
U ==  hermitian_semidefinite(N+1);   
cvx_end  
cvx_status;
if (cvx_optval==inf) || (cvx_optval==-inf)
    disp(['%%%%%% CVX SDP U error:   ',num2str(cvx_status),'    %%%%%% ']);
    inf_flag = 1;
    break
end
[value_IRS]=calvalue(H_k,H_l,W_sum,W_IRS,U,noisepow_Bob,noisepow_Eve,K,L)
W_pre=W_IRS;
U_pre=U;
rate_pre=rate_cur
rate_cur=value_IRS![9WI4GMOEL3Y2LX185YT4$1|690x162](upload://oGde65x2mYyXUwObJeQ3uQwAdmg.png)
1 Like

When one CVX problem (outer for loop iteration) fails, the variable values are populated with NaN. When you use this variable value as input data for the next CVX problem, there is nan in a CVX expression, which results in the error.

If you have access to Mosek 9.x, use that with CVX 2.2 Otherwise, follow the directions at CVXQUAD: How to use CVXQUAD's Pade Approximant instead of CVX's unreliable Successive Approximation for GP mode, log, exp, entr, rel_entr, kl_div, log_det, det_rootn, exponential cone. CVXQUAD's Quantum (Matrix) Entropy & Matrix Log related functions . This will improves the chances that CVX solves a problem. However, unsafeguarded SCA is a very unreliable algorithm, and you may be better off using a non-convex solver available under YALMIP. This forum is filled with people who unsuccessfully tried to apply unsfaeguarded SCA.

Thank you!My cvx edition is cvx 2.2 and Mosek edition is 9.1.9.But I get above error,maybe I should check my formula.

Most likely you formulate a nasty problem that Mosek cannot solve. The solutions will be all NANs.

Showing us the log output may help verify that conclusion.

CVX Warning:
Models involving “log” or other functions in the log, exp, and entropy
family are solved using an experimental successive approximation method.
This method is slower and less reliable than the method CVX employs for
other models. Please see the section of the user’s guide entitled
The successive approximation method
for more details about the approach, and for instructions on how to
suppress this warning message in the future.

Successive approximation method to be employed.
SDPT3 will be called several times to refine the solution.
Original size: 220 variables, 19 equality constraints
9 exponentials add 72 variables, 45 equality constraints

Cones | Errors |
Mov/Act | Centering Exp cone Poly cone | Status
--------±--------------------------------±--------
9/ 9 | 6.035e+00 8.955e+00 0.000e+00 | Inaccurate/Solved
9/ 9 | 4.834e+00 3.864e+00 0.000e+00 | Inaccurate/Solved
9/ 9 | 3.115e+00 1.155e+00 0.000e+00 | Inaccurate/Solved
9/ 9 | 1.431e+00 1.940e-01 2.237e-07 | Solved
9/ 9 | 3.878e-01 1.275e-02 6.801e-08 | Solved
9/ 9 | 8.254e-01s 6.041e-02s 1.667e-07 | Inaccurate/Solved
9/ 9 | 3.333e-01 8.792e-03 0.000e+00 | Solved
9/ 9 | 9.433e-02 7.206e-04 1.554e-07 | Solved
9/ 9 | 4.394e-02 1.573e-04 2.566e-07 | Solved
9/ 9 | 1.562e-01s 1.964e-03s 2.755e-07 | Solved
9/ 9 | 3.708e-01s 1.164e-02s 1.216e-07 | Solved
9/ 9 | 6.319e-01s 3.470e-02s 1.175e-07 | Solved
9/ 9 | 1.643e-01 2.240e-03 1.145e-07 | Solved
9/ 9 | 2.415e-02 4.786e-05 1.131e-07 | Solved
9/ 9 | 3.416e-03 1.046e-06 1.130e-07 | Solved
0/ 9 | 4.888e-04 1.298e-07 1.130e-07 | Inaccurate/Solved
0/ 9 | 7.058e-05 1.135e-07 1.130e-07 | Inaccurate/Solved
0/ 9 | 1.023e-05 1.130e-07 1.130e-07 | Solved

Status: Solved
Optimal value (cvx_optval): +146.458

Successive approximation method to be employed.
SDPT3 will be called several times to refine the solution.
Original size: 703 variables, 44 equality constraints
9 exponentials add 72 variables, 45 equality constraints

Cones | Errors |
Mov/Act | Centering Exp cone Poly cone | Status
--------±--------------------------------±--------
9/ 9 | 6.055e+00 9.102e+00 1.437e-08 | Inaccurate/Solved
9/ 9 | 5.113e+00 4.645e+00 5.539e-08 | Inaccurate/Solved
9/ 9 | 3.134e+00 1.168e+00 1.664e-07 | Inaccurate/Solved
9/ 9 | 2.076e+00 4.417e-01 1.052e-07 | Inaccurate/Solved
9/ 9 | 4.589e-01 1.798e-02 1.436e-07 | Inaccurate/Solved
9/ 9 | 3.825e-01 1.240e-02 9.477e-08 | Inaccurate/Solved
9/ 9 | 6.633e-01s 3.380e-02s 2.566e-07 | Inaccurate/Solved
9/ 9 | 2.575e-01 5.549e-03 1.748e-07 | Solved
9/ 9 | 3.526e-02 1.019e-04 1.789e-07 | Solved
9/ 9 | 2.585e-03 6.705e-07 1.806e-07 | Solved
0/ 9 | 7.252e-04 1.990e-07 1.756e-07 | Inaccurate/Solved
0/ 9 | 1.107e-04 1.812e-07 1.805e-07 | Solved

Status: Solved
Optimal value (cvx_optval): +152.22

value_IRS =

-1.2451 - 0.0000i

rate_pre =

 0

rate_cur =

-1.2451 - 0.0000i

iteration =

 2

Successive approximation method to be employed.
SDPT3 will be called several times to refine the solution.
Original size: 220 variables, 19 equality constraints
9 exponentials add 72 variables, 45 equality constraints

Cones | Errors |
Mov/Act | Centering Exp cone Poly cone | Status
--------±--------------------------------±--------
0/ 5 | 3.500e+00 2.785e-10 2.785e-10 | Failed
0/ 5 | 1.750e+00 2.785e-10 0.000e+00 | Failed
0/ 5 | 8.750e-01 2.785e-10 0.000e+00 | Failed

Status: Failed
Optimal value (cvx_optval): NaN

Successive approximation method to be employed.
SDPT3 will be called several times to refine the solution.
Original size: 703 variables, 44 equality constraints
9 exponentials add 72 variables, 45 equality constraints

Cones | Errors |
Mov/Act | Centering Exp cone Poly cone | Status
--------±--------------------------------±--------
9/ 9 | 2.698e+00 4.369e-01 0.000e+00 | Solved
9/ 9 | 1.985e-01 2.591e-03 0.000e+00 | Solved
9/ 9 | 4.385e-03 1.256e-06 0.000e+00 | Solved
0/ 1 | 6.082e-05 8.971e-14 0.000e+00 | Solved

Status: Solved
Optimal value (cvx_optval): -8.0421

value_IRS =

-1.2451 - 0.0000i

rate_pre =

-1.2451 - 0.0000i

rate_cur =

-1.2451 - 0.0000i

iteration =

 3

Successive approximation method to be employed.
SDPT3 will be called several times to refine the solution.
Original size: 220 variables, 19 equality constraints
9 exponentials add 72 variables, 45 equality constraints

Cones | Errors |
Mov/Act | Centering Exp cone Poly cone | Status
--------±--------------------------------±--------
9/ 9 | 6.026e+00 8.893e+00 0.000e+00 | Inaccurate/Solved
9/ 9 | 4.822e+00 3.831e+00 0.000e+00 | Inaccurate/Solved
9/ 9 | 3.088e+00 1.126e+00 1.220e-07 | Inaccurate/Solved
9/ 9 | 2.693e+00 8.077e-01 5.989e-08 | Inaccurate/Solved
9/ 9 | 1.285e+00 1.541e-01 4.789e-08 | Solved
9/ 9 | 4.892e-01 2.049e-02 1.383e-07 | Solved
9/ 9 | 2.287e-01 4.366e-03 3.274e-08 | Solved
9/ 9 | 1.329e+00s 1.281e-01s 1.887e-07 | Inaccurate/Solved
9/ 9 | 7.339e-01 4.731e-02 1.190e-07 | Solved
9/ 9 | 3.972e-01 1.339e-02 7.297e-08 | Solved
9/ 9 | 4.435e-02 1.600e-04 5.264e-08 | Solved
9/ 9 | 1.133e-01s 1.037e-03s 5.801e-08 | Solved
9/ 9 | 1.176e+00s 1.017e-01s 1.103e-07 | Solved
9/ 9 | 6.288e-02 3.247e-04 9.533e-08 | Solved
9/ 9 | 6.532e-02s 3.463e-04s 1.120e-07 | Solved
9/ 9 | 6.611e-01s 3.810e-02s 4.377e-08 | Solved
9/ 9 | 5.865e-01 2.975e-02 4.360e-08 | Solved
9/ 9 | 1.015e-01 8.497e-04 4.464e-08 | Solved
9/ 9 | 1.690e-02 2.336e-05 4.457e-08 | Solved
9/ 9 | 2.836e-03 6.937e-07 4.455e-08 | Solved
6/ 9 | 4.747e-04 6.166e-08 4.455e-08 | Solved
0/ 9 | 8.005e-05 4.487e-08 4.455e-08 | Solved

Status: Solved
Optimal value (cvx_optval): +155.223

Successive approximation method to be employed.
SDPT3 will be called several times to refine the solution.
Original size: 703 variables, 44 equality constraints
9 exponentials add 72 variables, 45 equality constraints

Cones | Errors |
Mov/Act | Centering Exp cone Poly cone | Status
--------±--------------------------------±--------
9/ 9 | 6.069e+00 9.203e+00 4.540e-08 | Inaccurate/Solved
9/ 9 | 5.110e+00 4.636e+00 5.412e-08 | Inaccurate/Solved
9/ 9 | 3.050e+00 1.092e+00 3.235e-07 | Inaccurate/Solved
9/ 9 | 2.119e+00 4.626e-01 8.308e-08 | Inaccurate/Solved
9/ 9 | 6.230e-01 3.369e-02 1.404e-07 | Inaccurate/Solved
9/ 9 | 3.675e-02 1.100e-04 1.512e-07 | Inaccurate/Solved
9/ 9 | 6.103e-02s 3.025e-04s 1.662e-07 | Inaccurate/Solved
9/ 9 | 9.035e-02s 6.609e-04s 1.653e-07 | Solved
9/ 9 | 3.394e-01s 9.111e-03s 2.186e-07 | Solved
9/ 9 | 6.185e-01s 2.950e-02s 4.309e-07 | Solved
9/ 9 | 8.382e-01s 5.192e-02s 0.000e+00 | Inaccurate/Solved
9/ 9 | 1.818e+00s 3.281e-01s 6.313e-08 | Solved

Status: Failed
Optimal value (cvx_optval): NaN

value_IRS =

  NaN +    NaNi

rate_pre =

-1.2451 - 0.0000i

rate_cur =

  NaN +    NaNi

iteration =

 4

错误使用 * (line 55)
Disciplined convex programming error:
Invalid numeric values (NaNs) may not be used in CVX expressions.

Oh,there must be somewhere wrong,my objective function value is not change.

Even if Mosek is installed, the statement
cvx_solver sdpt3
causes sdpt3 to be used as the solver, which means that CVX’s Successive Approximation method is used. Therefore, remove that statement, or change it to
cvx_solver mosek
Then Mosek’s more reliable algorithm will be used. however, you are still subject to the perils of unsafeguarded SCA.

Oh,I have tried to use Mosek,but result is NaN immediately.It seems that there is something really wrong with my function.

Warning: A non-empty cvx problem already exists in this scope.
It is being overwritten.
In cvxprob (line 28)
In cvx_begin (line 41)
In SDR_noIRS (line 17)
In wy_main (line 78)

Name Status Version Location

Mosek selected,default 9.1.9 {cvx}\mosek\w64
SDPT3 4.0 {cvx}\sdpt3
SeDuMi 1.3.4 {cvx}\sedumi

Calling Mosek 9.1.9: 220 variables, 19 equality constraints

MOSEK Version 9.1.9 (Build date: 2019-11-21 11:34:40)
Copyright © MOSEK ApS, Denmark. WWW: mosek.com
Platform: Windows/64-X86

Problem
Name :
Objective sense : min
Type : CONIC (conic optimization problem)
Constraints : 19
Cones : 9
Scalar variables : 28
Matrix variables : 3
Integer variables : 0

Optimizer started.
Presolve started.
Linear dependency checker started.
Linear dependency checker terminated.
Eliminator started.
Freed constraints in eliminator : 0
Eliminator terminated.
Eliminator - tries : 1 time : 0.00
Lin. dep. - tries : 1 time : 0.01
Lin. dep. - number : 0
Presolve terminated. Time: 0.01
Problem
Name :
Objective sense : min
Type : CONIC (conic optimization problem)
Constraints : 19
Cones : 9
Scalar variables : 28
Matrix variables : 3
Integer variables : 0

Optimizer - threads : 4
Optimizer - solved problem : the primal
Optimizer - Constraints : 10
Optimizer - Cones : 9
Optimizer - Scalar variables : 28 conic : 27
Optimizer - Semi-definite variables: 3 scalarized : 408
Factor - setup time : 0.00 dense det. time : 0.00
Factor - ML order time : 0.00 GP order time : 0.00
Factor - nonzeros before factor : 55 after factor : 55
Factor - dense dim. : 0 flops : 3.15e+05
ITE PFEAS DFEAS GFEAS PRSTATUS POBJ DOBJ MU TIME
0 4.8e+01 1.3e+00 4.9e+01 0.00e+00 4.028377405e+01 -7.245918014e+00 1.0e+00 0.05
1 1.1e+01 3.0e-01 1.1e+01 -3.84e-01 2.214776496e+01 3.111158500e+00 2.3e-01 0.13
2 3.7e+00 1.0e-01 3.5e+00 1.03e-01 2.487232124e+01 1.402147327e+01 7.8e-02 0.13
3 5.4e-01 1.4e-02 4.1e-01 2.35e-01 2.629028423e+01 2.389465448e+01 1.1e-02 0.13
4 8.0e-02 2.2e-03 7.3e-02 -3.79e-02 3.215106849e+01 3.177800614e+01 1.7e-03 0.13
5 1.5e-02 4.2e-04 1.7e-02 -1.64e-01 3.829640700e+01 3.887872911e+01 3.2e-04 0.14
6 5.6e-03 1.5e-04 7.4e-03 -1.53e-01 4.335355948e+01 4.459236791e+01 1.2e-04 0.14
7 9.0e-04 2.4e-05 1.0e-03 5.96e-02 5.152747733e+01 5.264963253e+01 1.9e-05 0.14
8 2.0e-04 5.5e-06 2.2e-04 5.24e-02 5.773606296e+01 5.879357313e+01 4.3e-06 0.14
9 6.8e-05 1.8e-06 7.6e-05 -2.16e-02 6.256195622e+01 6.373181077e+01 1.4e-06 0.16
10 1.5e-05 4.1e-07 1.5e-05 8.75e-02 6.894259719e+01 6.993325679e+01 3.2e-07 0.16
11 5.0e-06 1.4e-07 5.7e-06 -6.90e-02 7.378314622e+01 7.505377399e+01 1.0e-07 0.16
12 1.2e-06 3.2e-08 1.2e-06 7.59e-02 8.011646711e+01 8.117815616e+01 2.5e-08 0.17
13 4.3e-07 1.2e-08 4.7e-07 -1.64e-02 8.459463430e+01 8.576182654e+01 9.0e-09 0.17
14 9.2e-08 2.5e-09 9.0e-08 9.77e-02 9.120243381e+01 9.213012184e+01 1.9e-09 0.17
15 2.2e-08 5.8e-10 2.3e-08 -3.10e-02 9.759478936e+01 9.871234323e+01 4.5e-10 0.17
16 4.8e-09 1.3e-10 4.8e-09 5.53e-02 1.041269206e+02 1.051126207e+02 9.9e-11 0.17
17 1.5e-09 4.0e-11 1.6e-09 -5.18e-02 1.093421596e+02 1.105298709e+02 3.1e-11 0.19
18 3.5e-10 9.3e-12 3.4e-10 1.05e-01 1.156228300e+02 1.165701580e+02 7.2e-12 0.19
19 1.1e-10 3.1e-12 1.2e-10 -5.19e-03 1.205431507e+02 1.216105315e+02 2.4e-12 0.20
20 2.6e-11 6.9e-13 2.4e-11 9.92e-02 1.269030394e+02 1.277747629e+02 5.4e-13 0.20
21 8.5e-12 2.3e-13 8.6e-12 -2.28e-02 1.318955286e+02 1.329143359e+02 1.8e-13 0.20
22 1.7e-12 4.5e-14 1.6e-12 7.45e-02 1.386221192e+02 1.394886785e+02 3.5e-14 0.20
23 3.6e-13 1.0e-14 3.9e-13 -5.86e-02 1.452733846e+02 1.464205792e+02 7.6e-15 0.20
24 9.0e-14 2.6e-15 8.6e-14 1.05e-01 1.512361627e+02 1.521380148e+02 1.9e-15 0.22
25 1.2e-14 2.0e-15 1.2e-14 8.16e-03 1.596434747e+02 1.606559825e+02 2.4e-16 0.22
26 4.2e-15 1.8e-15 4.6e-15 -7.37e-02 1.635167686e+02 1.646886870e+02 8.8e-17 0.22
27 1.5e-15 1.9e-15 1.7e-15 -1.16e-01 1.680898073e+02 1.693234920e+02 3.2e-17 0.22
28 2.5e-16 2.0e-15 2.9e-16 -5.92e-02 1.762334565e+02 1.775043299e+02 5.3e-18 0.25
29 7.1e-17 1.6e-15 8.1e-17 -2.13e-02 1.817843128e+02 1.830644915e+02 1.5e-18 0.25
30 8.3e-18 1.6e-15 9.7e-18 -3.63e-02 1.907064048e+02 1.920500027e+02 1.7e-19 0.25
31 3.1e-18 2.0e-15 4.2e-18 -2.50e-01 1.937813359e+02 1.956834266e+02 6.3e-20 0.25
32 2.0e-18 1.9e-15 2.9e-18 -2.96e-01 1.954878715e+02 1.975543744e+02 4.2e-20 0.25
33 4.6e-19 1.9e-15 7.1e-19 -1.89e-01 2.030721899e+02 2.054779966e+02 9.6e-21 0.25
Optimizer terminated. Time: 0.28

Interior-point solution summary
Problem status : ILL_POSED
Solution status : PRIMAL_ILLPOSED_CER
Dual. obj: 2.6495678988e-08 nrm: 3e+00 Viol. con: 0e+00 var: 2e-10 barvar: 1e-10 cones: 0e+00
Optimizer summary
Optimizer - time: 0.28
Interior-point - iterations : 33 time: 0.25
Basis identification - time: 0.00
Primal - iterations : 0 time: 0.00
Dual - iterations : 0 time: 0.00
Clean primal - iterations : 0 time: 0.00
Clean dual - iterations : 0 time: 0.00
Simplex - time: 0.00
Primal simplex - iterations : 0 time: 0.00
Dual simplex - iterations : 0 time: 0.00
Mixed integer - relaxations: 0 time: 0.00


Status: Failed
Optimal value (cvx_optval): NaN

When Mosek says ill posed then that means you have a numerically nasty problem. Maybe your problem is near infeasible.

If you are using SCA, then most likely you should do something else because that procedure generate
nasty sub problems in this case (and in many other cases).

OK.Thank you very much.