# Fails on the second iteration and NAN

my code:

``````function [e, e_rate, e_flag, ...
rc_sec,rp_sec, ...
a_ck, rou_ck, a_pk, rou_pk, ...
a_ce, rou_ce, a_ke, rou_ke  ...
] = ...
e_func(a_ck_iter, rou_ck_iter,a_pk_iter, rou_pk_iter,...
a_ce_iter, rou_ce_iter,a_ke_iter, rou_ke_iter,...
UE, PE, N, M, G_U, G_E, w_ini, rc_sec_iter, rp_sec_iter,e_iter, noise_maxpower, trans_maxpower, miu_t, miu_r,miu_e)

%  wc * wc'
WC = w_ini(1:N) * w_ini(1:N)';
%  wi * wi'
for i = 1 : UE
W(:,:,i) = w_ini(N*i+1:N*(i+1)) * w_ini(N*i+1:N*(i+1))';
end
% w1*w1' + w2*w2' + ...
WS = zeros(N,N);
for i = 1 : UE
WS = WS + W(:,:,i);
end
% diag(wc*wc' + w1*w1' + w2*w2' + ...)
WT = WC + WS;
DIAG_WT = diag(diag(WT));
AWGN_USER = (1 + miu_r) * noise_maxpower;
AWGN_PE      = (1 + miu_e) * noise_maxpower;

cvx_solver mosek
% cvx_save_prefs
cvx_begin

variable t
variable e(M) complex
variable a_ck(UE)
variable rou_ck(UE)
variable a_pk(UE)
variable rou_pk(UE)
variable a_ce
variable rou_ce
variable a_ke(UE)
variable rou_ke(UE)
variable deta_rc_sec(UE)
variable deta_rp_sec(UE)

expression constraint(UE * (2 + 2 + 1 + 2));
expression LEFT_CK(UE);
expression RIGHT_CK(UE);
for i = 1 : UE
A(:,:,i) = G_U(:,:,i) * WS * G_U(:,:,i)' + miu_r * G_U(:,:,i) * WT * G_U(:,:,i)' + miu_t * (1 + miu_r) * G_U(:,:,i) * DIAG_WT * G_U(:,:,i)';
LEFT_CK(i) = LEFT_CK(i) + AWGN_USER;
RIGHT_CK(i) = 2 * real(e_iter' * (G_U(:,:,i) * WC * G_U(:,:,i)') * [e;1]) / rou_ck_iter(i) - (e_iter' * (G_U(:,:,i) * WC * G_U(:,:,i)') * e_iter) * rou_ck(i) / rou_ck_iter(i)^2;
constraint((i -1 ) * 2 + 1) = exp(log(2)*a_ck(i)) - rou_ck(i) - 1;
constraint((i - 1) * 2 + 2) = LEFT_CK(i) - real(RIGHT_CK(i));
end

expression LEFT_PK(UE);
expression RIGHT_PK(UE);
bob = i * 2;
for i = 1 : UE
B(:,:,i) = G_U(:,:,i) * (WS-W(:,:,i)) * G_U(:,:,i)' + miu_r * G_U(:,:,i) * WT * G_U(:,:,i)' + miu_t * (1 + miu_r) * G_U(:,:,i) * DIAG_WT * G_U(:,:,i)';
LEFT_PK(i) = LEFT_PK(i) + AWGN_USER;
RIGHT_PK(i) = 2 * real(e_iter' * (G_U(:,:,i) * W(:,:,i) * G_U(:,:,i)') * [e;1]) / rou_pk_iter(i) - (e_iter' * (G_U(:,:,i) * W(:,:,i) * G_U(:,:,i)') * e_iter) * rou_pk(i) / rou_pk_iter(i)^2;
constraint(bob + (i - 1) * 2 + 1) = exp(log(2)*a_pk(i)) - rou_pk(i) - 1;
constraint(bob + (i - 1) * 2 + 2) = LEFT_PK(i) - real(RIGHT_PK(i));
end

expression LEFT_CE;
expression RIGHT_CE;
tom = bob + i * 2;
POP_CORN = (G_E(:,:,1) * WS * G_E(:,:,1)' + miu_e * G_E(:,:,1) * WT * G_E(:,:,1)' + miu_t * (1 + miu_e) * G_E(:,:,1) * DIAG_WT * G_E(:,:,1)');
LEFT_CE = quad_over_lin(w_ini(1:N)' * G_E(:,:,1)' * [e;1] , rou_ce);
RIGHT_CE = 2 * real(e_iter' * POP_CORN * [e;1]) - e_iter' * POP_CORN * e_iter;
RIGHT_CE = RIGHT_CE + AWGN_PE;
constraint(tom + 1) = 1 + rou_ce - (2^a_ce_iter) * (1 + log(2) * (a_ce - a_ce_iter));
constraint(tom + 2) = LEFT_CE - real(RIGHT_CE);

expression LEFT_KE(UE);
expression RIGHT_KE(UE);
jerry = tom + 2;
for i = 1 : UE
COCA_COLA = (G_E(:,:,1) * WC * G_E(:,:,1)' + G_E(:,:,1) * (WS -W(:,:,i)) * G_E(:,:,1)' + miu_e * G_E(:,:,1) * WT * G_E(:,:,1)' + miu_t * (1 + miu_e) * G_E(:,:,1) * DIAG_WT * G_E(:,:,1)');

LEFT_KE(i) = quad_over_lin(w_ini(N*i+1:N*(i+1))' * G_E(:,:,1)' * [e;1] , rou_ke(i));
RIGHT_KE(i) = 2 * real(e_iter' * COCA_COLA * [e;1]) - e_iter' * COCA_COLA * e_iter;
RIGHT_KE(i) = RIGHT_KE(i) + AWGN_PE;
constraint(jerry + (i-1) * 2 + 1) = 1 + rou_ke(i) - (2^a_ke_iter(i)) * (1 + log(2) * (a_ke(i) - a_ke_iter(i)));
constraint(jerry + (i-1) * 2 + 2) = LEFT_KE(i) - real(RIGHT_KE(i));
end

expression bat(M);
for i = 1 : M
bat(i) = real(2 * real(e_iter(i)' * e(i)) - e_iter(i)' * e_iter(i) - 1);
end

punish = 0.001 * sum(bat);

maximize t + punish
subject to
real(constraint)<=0;
deta_rc_sec >= 0;
for i = 1 : UE
sum(rc_sec_iter + deta_rc_sec) <= a_ck(i) - a_ce;
end
deta_rp_sec >= 0;
for i = 1 : UE
rp_sec_iter(i) + deta_rp_sec(i) <= a_pk(i) - a_ke(i);
end
for i = 1 : UE
t  <= rc_sec_iter(i) + deta_rc_sec(i) + rp_sec_iter(i) + deta_rp_sec(i);
end
for i = 1 : M
abs(e(i)) <= 1;
end
cvx_end

if cvx_status(1) == 'S' || cvx_status(3) == 'a'
e_flag=1;
e_rate= t;
rc_sec = rc_sec_iter + deta_rc_sec;
rp_sec = rp_sec_iter + deta_rp_sec;
else
e_flag=0;
e_rate=0;
e =ones(M,1);
rc_sec = 0;
rp_sec = 0;
a_ck = 0;  rou_ck = 0; a_pk = 0;  rou_pk = 0;
a_ce = 0;  rou_ce = 0; a_ke = 0;  rou_ke = 0;
end
``````

Initial value of the second iteration：
w_ini：[7.821536513305144;4.857147214044837;3.229928731498855;2.159048879292589;0.319566135187890;0.188763447956598]
a_ck_iter：[2.412311953582665;2.387308638191314]
rou_ck_iter：[4.414149368022386;4.231804972481462]
a_ce_iter：9.234578309612607e-04
rou_ce_iter：6.402883441407603e-04
a_ke_iter：[0.005749652015342;4.647295573870919e-05]
rou_ke_iter：[0.003993251679489;3.221209737025443e-05]
a_pk_iter：[2.382036497372875;0.010046284505766]
rou_pk_iter：[4.212720671821359;0.006987856195515]
cvx information

``````Calling Mosek 9.1.9: 108 variables, 52 equality constraints
------------------------------------------------------------

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

Problem
Name                   :
Objective sense        : min
Type                   : CONIC (conic optimization problem)
Constraints            : 52
Cones                  : 21
Scalar variables       : 108
Matrix variables       : 0
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.00
Lin. dep.  - number                 : 0
Presolve terminated. Time: 0.06
Problem
Name                   :
Objective sense        : min
Type                   : CONIC (conic optimization problem)
Constraints            : 52
Cones                  : 21
Scalar variables       : 108
Matrix variables       : 0
Integer variables      : 0

Optimizer  - solved problem         : the primal
Optimizer  - Constraints            : 38
Optimizer  - Cones                  : 21
Optimizer  - Scalar variables       : 95                conic                  : 78
Optimizer  - Semi-definite variables: 0                 scalarized             : 0
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 : 485               after factor           : 511
Factor     - dense dim.             : 0                 flops                  : 3.39e+04
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME
0   2.5e+00  1.3e+00  1.4e+01  0.00e+00   0.000000000e+00   -1.322040801e+01  1.0e+00  0.13
1   9.8e-01  5.0e-01  4.2e+00  2.86e-01   2.862260641e-01   -6.241498296e+00  3.9e-01  0.34
2   2.3e-01  1.2e-01  5.9e-01  5.73e-01   2.844738315e-01   -1.548555358e+00  9.0e-02  0.34
3   1.0e-01  5.2e-02  1.7e-01  7.30e-01   8.884936453e-02   -7.630662992e-01  4.0e-02  0.36
4   4.2e-02  2.1e-02  5.0e-02  8.39e-01   3.934978891e-02   -3.388302640e-01  1.7e-02  0.38
5   7.8e-03  4.0e-03  4.2e-03  9.15e-01   -9.410744853e-03  -8.237378954e-02  3.1e-03  0.38
6   2.0e-03  1.0e-03  5.5e-04  1.01e+00   -1.692712855e-02  -3.525538640e-02  8.0e-04  0.39
7   4.8e-04  2.5e-04  6.7e-05  1.00e+00   -1.917175232e-02  -2.356871084e-02  1.9e-04  0.41
8   1.5e-04  7.6e-05  1.2e-05  9.68e-01   -1.974897272e-02  -2.111299496e-02  5.9e-05  0.41
9   2.8e-05  1.4e-05  8.4e-07  1.07e+00   -1.997054986e-02  -2.021776659e-02  1.1e-05  0.42
10  7.6e-06  3.9e-06  9.3e-08  1.34e+00   -2.000697730e-02  -2.006310827e-02  3.0e-06  0.42
11  3.0e-06  1.6e-06  2.4e-08  1.25e+00   -2.003045994e-02  -2.005119989e-02  1.2e-06  0.44
12  3.2e-07  1.7e-07  9.2e-10  1.01e+00   -2.005323362e-02  -2.005542001e-02  1.3e-07  0.45
13  1.4e-07  7.3e-08  4.1e-10  5.94e-01   -2.005245067e-02  -2.005383783e-02  5.7e-08  0.47
14  8.8e-08  3.4e-08  2.4e-10  -2.51e-01  -2.004937146e-02  -2.005045206e-02  2.7e-08  0.47
15  4.5e-08  1.8e-08  1.2e-10  -1.10e-01  -2.004600761e-02  -2.004671341e-02  1.4e-08  0.48
16  4.5e-08  1.8e-08  1.2e-10  -9.96e-02  -2.004600761e-02  -2.004671341e-02  1.4e-08  0.50
17  4.5e-08  1.8e-08  1.2e-10  -9.72e-02  -2.004600761e-02  -2.004671341e-02  1.4e-08  0.50
18  3.5e-08  8.8e-09  4.6e-11  5.30e-01   -2.005107611e-02  -2.005146300e-02  6.6e-09  0.52
19  1.1e-08  9.7e-10  5.8e-12  -2.06e-01  -2.004803699e-02  -2.004792089e-02  1.2e-09  0.53
20  6.7e-09  5.1e-10  3.1e-12  -1.48e-01  -2.004577881e-02  -2.004558883e-02  6.8e-10  0.55
21  6.7e-09  5.1e-10  3.1e-12  1.09e-01   -2.004577881e-02  -2.004558883e-02  6.8e-10  0.55
22  5.4e-09  2.7e-11  4.0e-14  1.08e+00   -2.004955185e-02  -2.004954092e-02  5.1e-10  0.56
23  5.0e-09  2.5e-11  4.3e-14  5.21e-02   -2.004940146e-02  -2.004938386e-02  4.7e-10  0.56
24  4.8e-09  2.5e-11  4.3e-14  5.30e-02   -2.004893128e-02  -2.004891217e-02  4.6e-10  0.59
25  4.7e-09  2.4e-11  4.3e-14  -2.74e-01  -2.004878727e-02  -2.004876727e-02  4.5e-10  0.59
26  4.1e-09  2.0e-10  3.4e-14  1.98e+00   -2.004602224e-02  -2.004595589e-02  3.8e-10  0.61
27  3.6e-09  1.8e-10  3.0e-14  -1.48e-01  -2.004505842e-02  -2.004498912e-02  3.4e-10  0.61
28  3.6e-09  1.8e-10  3.0e-14  1.97e-01   -2.004504493e-02  -2.004497567e-02  3.4e-10  0.64
29  3.6e-09  1.8e-10  3.0e-14  -3.86e-01  -2.004504493e-02  -2.004497567e-02  3.4e-10  0.64
30  3.6e-09  1.8e-10  3.0e-14  1.00e+00   -2.004504493e-02  -2.004497567e-02  3.4e-10  0.66
31  3.6e-09  1.8e-10  3.0e-14  1.00e+00   -2.004504445e-02  -2.004497519e-02  3.4e-10  0.67
Optimizer terminated. Time: 0.80

Interior-point solution summary
Problem status  : PRIMAL_AND_DUAL_FEASIBLE
Solution status : OPTIMAL
Primal.  obj: -2.0045044448e-02   nrm: 9e+02    Viol.  con: 9e-05    var: 0e+00    cones: 0e+00
Dual.    obj: -2.0044975189e-02   nrm: 5e+01    Viol.  con: 0e+00    var: 5e-09    cones: 0e+00
Optimizer summary
Optimizer                 -                        time: 0.80
Interior-point          - iterations : 32        time: 0.67
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: Solved
Optimal value (cvx_optval): +2.38633

Calling Mosek 9.1.9: 108 variables, 52 equality constraints
------------------------------------------------------------

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

Problem
Name                   :
Objective sense        : min
Type                   : CONIC (conic optimization problem)
Constraints            : 52
Cones                  : 21
Scalar variables       : 108
Matrix variables       : 0
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.03
Lin. dep.  - number                 : 0
Presolve terminated. Time: 0.06
Problem
Name                   :
Objective sense        : min
Type                   : CONIC (conic optimization problem)
Constraints            : 52
Cones                  : 21
Scalar variables       : 108
Matrix variables       : 0
Integer variables      : 0

Optimizer  - solved problem         : the primal
Optimizer  - Constraints            : 38
Optimizer  - Cones                  : 21
Optimizer  - Scalar variables       : 95                conic                  : 78
Optimizer  - Semi-definite variables: 0                 scalarized             : 0
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 : 485               after factor           : 511
Factor     - dense dim.             : 0                 flops                  : 3.39e+04
ITE PFEAS    DFEAS    GFEAS    PRSTATUS   POBJ              DOBJ              MU       TIME
0   2.5e+00  1.3e+00  1.4e+01  0.00e+00   0.000000000e+00   -1.322040801e+01  1.0e+00  0.13
1   9.8e-01  5.0e-01  4.2e+00  2.86e-01   2.863613140e-01   -6.240313907e+00  3.9e-01  0.34
2   2.3e-01  1.2e-01  5.9e-01  5.73e-01   2.844502251e-01   -1.543771426e+00  9.0e-02  0.34
3   1.0e-01  5.2e-02  1.7e-01  7.30e-01   8.848339507e-02   -7.604587294e-01  4.0e-02  0.36
4   4.2e-02  2.1e-02  5.0e-02  8.39e-01   3.917456071e-02   -3.380322650e-01  1.7e-02  0.38
5   7.9e-03  4.0e-03  4.2e-03  9.15e-01   -9.440625376e-03  -8.238834207e-02  3.1e-03  0.38
6   2.0e-03  1.0e-03  5.5e-04  1.01e+00   -1.692441860e-02  -3.525720214e-02  8.0e-04  0.39
7   4.8e-04  2.5e-04  6.7e-05  9.99e-01   -1.915754492e-02  -2.358191744e-02  1.9e-04  0.41
8   1.5e-04  7.7e-05  1.2e-05  9.51e-01   -1.972866212e-02  -2.112319692e-02  5.9e-05  0.41
9   2.5e-05  1.3e-05  8.0e-07  1.02e+00   -1.995911314e-02  -2.019381116e-02  1.0e-05  0.42
10  6.3e-06  3.2e-06  8.4e-08  1.16e+00   -1.999632116e-02  -2.004990879e-02  2.5e-06  0.44
11  2.0e-06  1.0e-06  1.3e-08  1.37e+00   -2.000395834e-02  -2.001803738e-02  7.9e-07  0.45
12  5.9e-07  3.0e-07  2.2e-09  1.09e+00   -2.001532606e-02  -2.001942930e-02  2.3e-07  0.47
13  2.0e-07  1.0e-07  5.8e-10  7.98e-01   -2.001693722e-02  -2.001860028e-02  8.1e-08  0.48
14  1.3e-07  6.9e-08  4.5e-10  -9.09e-03  -2.001464981e-02  -2.001626374e-02  6.6e-08  0.48
15  5.8e-08  3.0e-08  2.2e-10  -1.78e-01  -2.001006566e-02  -2.001110887e-02  2.9e-08  0.50
16  4.3e-08  2.2e-08  1.8e-10  -7.44e-01  -2.000794797e-02  -2.000882073e-02  1.9e-08  0.52
17  1.0e-08  4.9e-09  4.0e-11  -6.81e-02  -1.999952069e-02  -1.999958751e-02  4.1e-09  0.52
18  9.6e-09  4.5e-09  3.9e-11  -4.58e-01  -1.999962837e-02  -1.999965277e-02  3.8e-09  0.53
19  5.3e-09  1.4e-09  2.5e-11  -5.11e-01  -2.000072608e-02  -1.999904656e-02  1.2e-09  0.55
20  2.6e-09  6.3e-10  8.5e-12  5.62e-01   -1.999662910e-02  -1.999559788e-02  7.9e-10  0.55
21  1.6e-09  1.5e-10  6.1e-13  -9.81e-01  -1.897681043e-02  -1.848561853e-02  3.1e-12  0.56
22  6.6e-12  1.1e-10  2.6e-14  -1.00e+00  4.901862193e-01   7.350572286e-01   6.2e-15  0.58
23  1.4e-14  1.3e-10  2.4e-14  -1.00e+00  2.563096064e+02   3.793385085e+02   1.2e-17  0.58
Optimizer terminated. Time: 0.70

Interior-point solution summary
Problem status  : ILL_POSED
Solution status : PRIMAL_ILLPOSED_CER
Dual.    obj: 7.0516666284e-08    nrm: 1e+01    Viol.  con: 0e+00    var: 2e-10    cones: 0e+00
Optimizer summary
Optimizer                 -                        time: 0.70
Interior-point          - iterations : 23        time: 0.59
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
``````