# Hello everyone, I really need your help.Why can the results be solved in the first two iterations, but NaN appears in the third iteration? How can I solve this problem? Thank you everyone

clc;
clear all;
close all;
cvx_solver mosek
%轨迹初始化
T = 120; %mission period
delta = T/120; %elemental length
N = T/delta; %number of time slot
K=6;
% 用户在移动
% 用户一的初始轨迹
% xw1 = 700;%圆心
% yw1 = 100;
% theta = 0:2pi/(N-1):2pi;
% qw11 = xw1 + rw1cos(theta);
% qw21 = yw1 + rw1
sin(theta);
% qw11 = qw11’;
% qw21 = qw21’;
% w1 = [qw11,qw21];
%用户二的初始估计
% xw2 = 200;%圆心
% yw2 = 450;
% theta = 0:2pi/(N-1):2pi;
% qw12 = xw2 + rw2cos(theta);
% qw22 = yw2 + rw2
sin(theta);
% qw12 = qw12’;
% qw22 = qw22’;
% w2 = [qw12,qw22];
w1 = [700ones(N,1),100ones(N,1)];
w2 = [200ones(N,1),450ones(N,1)];
w3 = [-300ones(N,1),450ones(N,1)];
w4 = [-600ones(N,1),200ones(N,1)];
w5 = [-450ones(N,1),-450ones(N,1)];
w6 = [400ones(N,1),-500ones(N,1)];

% 恶意干扰在移动
% 干扰点的初始轨迹
Jx1 =0 ;%圆心
Jy1 = 0;
theta = 0:2pi/(N-1):2pi;
J11 = Jx1 + Jw1cos(theta);
J21 = Jy1 + Jw1
sin(theta);
J11 = J11’;
J21 = J21’;
J = [J11,J21];
% J = [0ones(N,1),0ones(N,1)];%恶意干扰信号位置

pm=0.01;%干扰功率
pmax=0.1;
B=10;
vmax = 30; %maximum speed
beta_db =-60; %单位距离下信道增益 dB
sigma_db = -148; %噪声强度dB
beta = 10^(beta_db/10);
sigma = 10^(sigma_db/10);
H = 100; %无人机高度
%初始轨迹
x11 = 0;%圆心
y11 = 0;
theta = 0:2pi/(N-1):2pi;
% 无人机轨迹
% q11 = x11 + r1cos(theta);
% q21 = y11 + r1
sin(theta);
% q11 = q11’;
% q21 = q21’;
% q1 = [q11,q21];
q1=[38ones(N,1),5ones(N,1)];
eachIterNum = 1;
for iterNum =1:1:3
%无人机与用户位置d_r,u
d11 = H^2+(q1(:,1)-w1(:,1)).^2+(q1(:,2)-w1(:,2)).^2;
d12 = H^2+(q1(:,1)-w2(:,1)).^2+(q1(:,2)-w2(:,2)).^2;
d13 = H^2+(q1(:,1)-w3(:,1)).^2+(q1(:,2)-w3(:,2)).^2;
d14 = H^2+(q1(:,1)-w4(:,1)).^2+(q1(:,2)-w4(:,2)).^2;
d15 = H^2+(q1(:,1)-w5(:,1)).^2+(q1(:,2)-w5(:,2)).^2;
d16 = H^2+(q1(:,1)-w6(:,1)).^2+(q1(:,2)-w6(:,2)).^2;
d= H^2+(q1(:,1)-J(:,1)).^2+(q1(:,2)-J(:,2)).^2;

g1=beta./d11;
g2=beta./d12;
g3=beta./d13;
g4=beta./d14;
g5=beta./d15;
g6=beta./d16;
gm=beta./d;

cvx_begin
% cvx_precision best
variable eta

variable p1(N)
variable p2(N)
variable p3(N)
variable p4(N)
variable p5(N)
variable p6(N)
maximize eta
subject to

sum(B*log(1+p1.*g1./(pm.gm(:)+sigma)))/T>=eta;
sum(B
log(1+p2.*g2./(pm.gm(:)+sigma)))/T>=eta;
sum(B
log(1+p3.*g3./(pm.gm(:)+sigma)))/T>=eta;
sum(B
log(1+p4.*g4./(pm.gm(:)+sigma)))/T>=eta;
sum(B
log(1+p5.*g5./(pm.gm(:)+sigma)))/T>=eta;
sum(B
log(1+p6.*g6./(pm.*gm(:)+sigma)))/T>=eta;

p1+p2+p3+p4+p5+p6<=pmax;

0.001<=p1;
0.001<=p2;
0.001<=p3;
0.001<=p4;
0.001<=p5;
0.001<=p6;

cvx_end
optvalue1(iterNum) = cvx_optval;
eachOptValue(eachIterNum) = cvx_optval;
eachIterNum = eachIterNum + 1;

q1s=q1;
for cntTra=1:1:1
d11 = H^2+(q1s(:,1)-w1(:,1)).^2+(q1s(:,2)-w1(:,2)).^2;
d12 = H^2+(q1s(:,1)-w2(:,1)).^2+(q1s(:,2)-w2(:,2)).^2;
d13 = H^2+(q1s(:,1)-w3(:,1)).^2+(q1s(:,2)-w3(:,2)).^2;
d14 = H^2+(q1s(:,1)-w4(:,1)).^2+(q1s(:,2)-w4(:,2)).^2;
d15 = H^2+(q1s(:,1)-w5(:,1)).^2+(q1s(:,2)-w5(:,2)).^2;
d16 = H^2+(q1s(:,1)-w6(:,1)).^2+(q1s(:,2)-w6(:,2)).^2;
d= H^2+(q1s(:,1)-J(:,1)).^2+(q1s(:,2)-J(:,2)).^2;

Is=sigma+(beta.*pm)./d;%初始可行解
Ls1=d11./(beta.*p1);
Ls2=d12./(beta.*p2);
Ls3=d13./(beta.*p3);
Ls4=d14./(beta.*p4);
Ls5=d15./(beta.*p5);
Ls6=d16./(beta.*p6);

A1=-1./(Ls1+B*Ls1.^2.Is);%泰勒展开系数
A2=-1./(Ls2+B
Ls2.^2.Is);
A3=-1./(Ls3+B
Ls3.^2.Is);
A4=-1./(Ls4+B
Ls4.^2.Is);
A5=-1./(Ls5+B
Ls5.^2.Is);
A6=-1./(Ls6+B
Ls6.^2.*Is);

C1=-1./(Is+B*Is.^2.Ls1);%泰勒展开系数
C2=-1./(Is+B
Is.^2.Ls2);
C3=-1./(Is+B
Is.^2.Ls3);
C4=-1./(Is+B
Is.^2.Ls4);
C5=-1./(Is+B
Is.^2.Ls5);
C6=-1./(Is+B
Is.^2.*Ls6);

cvx_begin

variable q1(N,2)
variable L1(N)
variable L2(N)
variable L3(N)
variable L4(N)
variable L5(N)
variable L6(N)
variable I(N)

variable eta
variable m(N)
expression qu1(N,1)

maximize eta

subject to
sum(Blog(1+1./(Ls1(:).Is(:)))-abs(A1).(L1(:)-Ls1(:))-abs(C1).(I(:)-Is(:)))/T>=eta;%（1-1）
sum(Blog(1+1./(Ls2(:).Is(:)))-abs(A2).(L2(:)-Ls2(:))-abs(C2).(I(:)-Is(:)))/T>=eta;
sum(Blog(1+1./(Ls3(:).Is(:)))-abs(A3).(L3(:)-Ls3(:))-abs(C3).(I(:)-Is(:)))/T>=eta;
sum(Blog(1+1./(Ls4(:).Is(:)))-abs(A4).(L4(:)-Ls4(:))-abs(C4).(I(:)-Is(:)))/T>=eta;
sum(Blog(1+1./(Ls5(:).Is(:)))-abs(A5).(L5(:)-Ls5(:))-abs(C5).(I(:)-Is(:)))/T>=eta;
sum(Blog(1+1./(Ls6(:).Is(:)))-abs(A6).(L6(:)-Ls6(:))-abs(C6).(I(:)-Is(:)))/T>=eta;

for i=1:1:N

H^2+sum_square_abs(q1(i,:)-w1(i,:))<=beta.*p1.*L1(i)%（1-2）
H^2+sum_square_abs(q1(i,:)-w2(i,:))<=beta.*p2.*L2(i);
H^2+sum_square_abs(q1(i,:)-w3(i,:))<=beta.*p3.*L3(i);
H^2+sum_square_abs(q1(i,:)-w4(i,:))<=beta.*p4.*L4(i);
H^2+sum_square_abs(q1(i,:)-w5(i,:))<=beta.*p5.*L5(i);
H^2+sum_square_abs(q1(i,:)-w6(i,:))<=beta.*p6.*L6(i);

pm*beta.*inv_pos(m(i))+sigma<=I(i)%(1-3)

end

for i=1:1:N
qu1(i)=2*q1s(i,1)q1(i,1)-q1s(i,1)^2+J(i,1)^2-2J(i,1)q1(i,1)+2q1s(i,2)q1(i,2)-q1s(i,2)^2+J(i,2)^2-2J(i,2)*q1(i,2)+H^2;%一阶泰勒
end

m<=qu1;%1-4
m>=0;%(1-5)
q1(1, == [38,5];
q1(N, == [38,5];
for i = 1:1:N-1
norm(q1(i+1,:)-q1(i,:))<=vmax*delta;
end

cvx_end
q1s=q1;

optValueTrajectory(cntTra) = cvx_optval;
end
optvalue2(iterNum) = cvx_optval;
eachOptValue(eachIterNum) = cvx_optval;
eachIterNum = eachIterNum + 1;

end

## Calling Mosek 9.1.9: 90484 variables, 2158 equality constraints For improved efficiency, Mosek is solving the dual problem.

MOSEK Version 9.1.9 (Build date: 2019-11-21 11:34:40)
Platform: Windows/64-X86

MOSEK warning 62: The A matrix contains a large value of -1.1e+10 in constraint ‘’ (747) at variable ‘’ (2).
MOSEK warning 62: The A matrix contains a large value of -1.4e+10 in constraint ‘’ (748) at variable ‘’ (2).
MOSEK warning 62: The A matrix contains a large value of -1.8e+10 in constraint ‘’ (749) at variable ‘’ (2).
MOSEK warning 62: The A matrix contains a large value of -2.2e+10 in constraint ‘’ (750) at variable ‘’ (2).
MOSEK warning 62: The A matrix contains a large value of -2.7e+10 in constraint ‘’ (751) at variable ‘’ (2).
MOSEK warning 62: The A matrix contains a large value of -3.2e+10 in constraint ‘’ (752) at variable ‘’ (2).
MOSEK warning 62: The A matrix contains a large value of -3.7e+10 in constraint ‘’ (753) at variable ‘’ (2).
MOSEK warning 62: The A matrix contains a large value of -4.3e+10 in constraint ‘’ (754) at variable ‘’ (2).
MOSEK warning 62: The A matrix contains a large value of -4.9e+10 in constraint ‘’ (755) at variable ‘’ (2).
MOSEK warning 62: The A matrix contains a large value of -5.6e+10 in constraint ‘’ (756) at variable ‘’ (2).
Warning number 62 is disabled.
MOSEK warning 710: #120 (nearly) zero elements are specified in sparse col ‘’ (2) of matrix ‘A’.
MOSEK warning 710: #120 (nearly) zero elements are specified in sparse col ‘’ (3) of matrix ‘A’.
MOSEK warning 710: #120 (nearly) zero elements are specified in sparse col ‘’ (4) of matrix ‘A’.
MOSEK warning 710: #120 (nearly) zero elements are specified in sparse col ‘’ (5) of matrix ‘A’.
MOSEK warning 710: #120 (nearly) zero elements are specified in sparse col ‘’ (6) of matrix ‘A’.
MOSEK warning 710: #120 (nearly) zero elements are specified in sparse col ‘’ (7) of matrix ‘A’.
Problem
Name :
Objective sense : min
Type : CONIC (conic optimization problem)
Constraints : 2158
Cones : 959
Scalar variables : 90484
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.03
Problem
Name :
Objective sense : min
Type : CONIC (conic optimization problem)
Constraints : 2158
Cones : 959
Scalar variables : 90484
Matrix variables : 0
Integer variables : 0

Optimizer - solved problem : the primal
Optimizer - Constraints : 1197
Optimizer - Cones : 959
Optimizer - Scalar variables : 3831 conic : 3585
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 : 1.10e+04 after factor : 1.27e+04
Factor - dense dim. : 0 flops : 7.96e+05
ITE PFEAS DFEAS GFEAS PRSTATUS POBJ DOBJ MU TIME
0 1.0e+00 1.7e+06 4.3e+06 0.00e+00 4.278868337e+06 0.000000000e+00 1.0e+00 0.05
1 2.3e-01 3.8e+05 2.1e+06 -1.00e+00 4.264174489e+06 -1.429970182e+04 2.3e-01 0.13
2 9.1e-02 1.5e+05 1.3e+06 -1.00e+00 4.169061951e+06 -1.084017253e+05 9.1e-02 0.13
3 7.2e-03 1.2e+04 3.6e+05 -9.99e-01 2.767425110e+06 -1.484111759e+06 7.2e-03 0.13
4 8.7e-04 1.4e+03 1.2e+05 -9.91e-01 -1.609033013e+06 -5.752624609e+06 8.7e-04 0.14
5 1.8e-04 3.0e+02 5.4e+04 -9.62e-01 -8.039466782e+06 -1.195892980e+07 1.8e-04 0.14
6 4.0e-05 6.7e+01 2.3e+04 -9.01e-01 -1.456032929e+07 -1.803770651e+07 4.0e-05 0.14
7 1.4e-05 2.3e+01 1.2e+04 -7.70e-01 -1.678760490e+07 -1.974647638e+07 1.4e-05 0.14
8 2.1e-06 3.6e+00 2.6e+03 -5.68e-01 -9.710781650e+06 -1.110558980e+07 2.1e-06 0.14
9 1.0e-06 1.7e+00 9.3e+02 2.93e-01 -4.429027282e+06 -5.222233243e+06 1.0e-06 0.16
10 1.6e-07 2.7e-01 6.1e+01 6.65e-01 -5.585465870e+05 -7.041928431e+05 1.6e-07 0.16
11 3.6e-09 6.0e-03 1.9e-01 9.74e-01 -1.355210214e+04 -1.681145708e+04 3.6e-09 0.16
12 7.5e-12 1.2e-05 1.8e-05 1.00e+00 -1.873613910e+01 -2.548712876e+01 7.5e-12 0.16
13 3.8e-13 6.2e-07 2.1e-07 9.93e-01 8.014859919e+00 7.672628120e+00 3.8e-13 0.17
14 9.8e-14 1.6e-07 3.1e-08 9.09e-01 9.416719464e+00 9.322685386e+00 9.8e-14 0.17
15 4.6e-14 7.6e-08 1.1e-08 8.20e-01 9.849365646e+00 9.800882218e+00 4.6e-14 0.17
16 1.9e-14 3.2e-08 3.2e-09 8.27e-01 1.006563543e+01 1.004350991e+01 1.9e-14 0.19
17 7.5e-15 1.2e-08 9.1e-10 7.75e-01 1.023199126e+01 1.022214186e+01 7.5e-15 0.19
18 2.6e-15 4.3e-09 2.1e-10 7.76e-01 1.032114747e+01 1.031733771e+01 2.6e-15 0.19
19 8.6e-14 4.3e-09 2.0e-10 8.00e-01 1.032288874e+01 1.031916770e+01 2.5e-15 0.20
20 1.0e-13 4.3e-09 2.0e-10 7.95e-01 1.032388688e+01 1.032021537e+01 2.5e-15 0.20
21 1.0e-13 4.2e-09 1.9e-10 7.99e-01 1.032495023e+01 1.032133199e+01 2.4e-15 0.20
22 1.0e-13 4.3e-09 1.9e-10 7.99e-01 1.032607302e+01 1.032250970e+01 2.4e-15 0.22
23 1.1e-13 4.4e-09 1.9e-10 7.94e-01 1.032621204e+01 1.032265570e+01 2.4e-15 0.22
24 1.0e-13 4.3e-09 1.8e-10 7.99e-01 1.032732776e+01 1.032382525e+01 2.3e-15 0.22
25 1.1e-13 4.3e-09 1.8e-10 7.99e-01 1.032746558e+01 1.032396975e+01 2.3e-15 0.23
26 1.1e-13 4.3e-09 1.8e-10 7.99e-01 1.032773944e+01 1.032425670e+01 2.3e-15 0.23
27 1.1e-13 4.1e-09 1.7e-10 7.99e-01 1.032995143e+01 1.032657574e+01 2.2e-15 0.23
28 1.1e-13 4.1e-09 1.7e-10 7.95e-01 1.033022318e+01 1.032686086e+01 2.2e-15 0.23
29 1.1e-13 4.0e-09 1.7e-10 7.95e-01 1.033024008e+01 1.032687865e+01 2.2e-15 0.25
30 1.1e-13 4.1e-09 1.7e-10 7.99e-01 1.033024886e+01 1.032688754e+01 2.2e-15 0.25
31 1.1e-13 4.0e-09 1.7e-10 7.99e-01 1.033133818e+01 1.032802866e+01 2.2e-15 0.25
32 1.1e-13 4.0e-09 1.7e-10 7.99e-01 1.033135529e+01 1.032804637e+01 2.2e-15 0.27
33 1.1e-13 3.9e-09 1.6e-10 7.95e-01 1.033189712e+01 1.032861419e+01 2.1e-15 0.27
34 1.1e-13 4.0e-09 1.6e-10 7.99e-01 1.033243799e+01 1.032918073e+01 2.1e-15 0.27
35 1.1e-13 4.0e-09 1.6e-10 8.00e-01 1.033297808e+01 1.032974642e+01 2.1e-15 0.28
36 1.1e-13 3.9e-09 1.6e-10 8.00e-01 1.033405813e+01 1.033087785e+01 2.1e-15 0.28
37 1.1e-13 3.9e-09 1.6e-10 8.00e-01 1.033419222e+01 1.033101853e+01 2.1e-15 0.28
38 1.1e-13 3.8e-09 1.5e-10 8.00e-01 1.033446129e+01 1.033129991e+01 2.1e-15 0.30
39 1.1e-13 3.9e-09 1.5e-10 8.00e-01 1.033472750e+01 1.033157903e+01 2.0e-15 0.30
40 1.1e-13 3.9e-09 1.5e-10 8.01e-01 1.033479319e+01 1.033164850e+01 2.0e-15 0.30
41 1.1e-13 3.7e-09 1.5e-10 8.01e-01 1.033492580e+01 1.033178726e+01 2.0e-15 0.31
42 1.1e-13 3.7e-09 1.5e-10 8.01e-01 1.033505825e+01 1.033192560e+01 2.0e-15 0.31
43 1.1e-13 3.6e-09 1.5e-10 7.97e-01 1.033532327e+01 1.033220300e+01 2.0e-15 0.31
44 1.1e-13 3.7e-09 1.5e-10 8.01e-01 1.033558516e+01 1.033247723e+01 2.0e-15 0.33
45 1.1e-13 3.7e-09 1.5e-10 8.01e-01 1.033663342e+01 1.033357508e+01 2.0e-15 0.33
46 1.1e-13 3.7e-09 1.4e-10 8.02e-01 1.033765485e+01 1.033464413e+01 1.9e-15 0.33
47 1.1e-13 3.8e-09 1.4e-10 7.98e-01 1.033790394e+01 1.033490522e+01 1.9e-15 0.34
48 1.1e-13 3.8e-09 1.4e-10 7.99e-01 1.033790836e+01 1.033490928e+01 1.9e-15 0.34
49 1.1e-13 3.8e-09 1.4e-10 7.99e-01 1.033790836e+01 1.033490928e+01 1.9e-15 0.34
50 1.1e-13 3.8e-09 1.4e-10 9.55e-01 1.033790836e+01 1.033490928e+01 1.9e-15 0.36
51 1.0e-13 3.9e-09 1.2e-10 9.86e-01 1.033943293e+01 1.033669539e+01 1.8e-15 0.36
52 1.0e-13 4.0e-09 1.2e-10 9.87e-01 1.034009264e+01 1.033746517e+01 1.7e-15 0.36
53 1.1e-13 3.9e-09 1.1e-10 9.88e-01 1.034027109e+01 1.033767395e+01 1.7e-15 0.38
54 1.1e-13 4.0e-09 1.1e-10 9.88e-01 1.034034692e+01 1.033776319e+01 1.7e-15 0.38
55 1.1e-13 4.1e-09 1.1e-10 9.87e-01 1.034036987e+01 1.033778934e+01 1.7e-15 0.38
56 1.1e-13 3.9e-09 1.1e-10 9.88e-01 1.034037908e+01 1.033780061e+01 1.7e-15 0.39
57 1.1e-13 3.9e-09 1.1e-10 9.88e-01 1.034037908e+01 1.033780061e+01 1.7e-15 0.39
58 1.1e-13 4.0e-09 1.1e-10 9.93e-01 1.034039960e+01 1.033782497e+01 1.7e-15 0.39
59 1.1e-13 4.1e-09 1.1e-10 9.93e-01 1.034040501e+01 1.033783106e+01 1.7e-15 0.41
60 1.1e-13 4.0e-09 1.1e-10 9.93e-01 1.034040798e+01 1.033783410e+01 1.7e-15 0.41
61 1.1e-13 4.0e-09 1.1e-10 9.93e-01 1.034040893e+01 1.033783486e+01 1.7e-15 0.42
62 1.1e-13 4.0e-09 1.1e-10 9.93e-01 1.034040893e+01 1.033783486e+01 1.7e-15 0.42
63 1.1e-13 4.0e-09 1.1e-10 9.93e-01 1.034040893e+01 1.033783486e+01 1.7e-15 0.42
Optimizer terminated. Time: 0.48

Interior-point solution summary
Problem status : UNKNOWN
Solution status : UNKNOWN
Primal. obj: 1.0340408928e+01 nrm: 2e+11 Viol. con: 2e-04 var: 8e-10 cones: 0e+00
Dual. obj: 1.0337834860e+01 nrm: 2e+15 Viol. con: 0e+00 var: 2e-03 cones: 0e+00
Optimizer summary
Optimizer - time: 0.48
Interior-point - iterations : 64 time: 0.44
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