# Why RRR, RRR1, RRR2,and RRR3 are not equal

When I set the optimization variable trx to be equal to the initial value tr, the value of RRR should be equal to RRR1, RRR2,and RRR3 , but why is the solution different?

clear all
Lmax=500;%2Km边界
T=100;%s无人机飞行时间
N=100;%无人机飞行时隙数

M=10;%IoT设备个数
sm=zeros(M,2);
sm(:,1)=randi([-Lmax/2,Lmax/2],1,M);
sm(:,2)=randi([-Lmax/2,Lmax/2],1,M);

%UAV参数
H=100;%飞行高度
Vmax=100;%m/s无人机飞行的最大速度
Vmin=3;%m/s无人机飞行的最小速度
amax=5;%m/s2无人机最大加速度
w1=9.26*10^(-4);
w2=2250;%无人机飞行因数
faimax1=pi/18;%固定翼无人机最大转向角
faimax2=pi;%旋转翼无人机最大转向角
tr=ones(N,2);%无人机飞行轨迹

for i=1:N
tr(i,1)=250cos((i-1)(2pi/N));
tr(i,2)=250
sin((i-1)(2pi/N));
end
trf=tr(1,:);

V=zeros(N,2);
VV=zeros(N,1);
for i=1:N-1
V(i,:)=(tr(i+1,:)-tr(i,:))./(T/N);
end
V(N,:)=(trf-tr(N,:))./(T/N);
Vf=V(1,:);

for i=1:N
VV(i,1)=sqrt(V(i,1)^2+V(i,2)^2);
end

a=zeros(N,2);
aa=zeros(N,1);
for i=1:N-1
a(i,:)=(V(i+1,:)-V(i,:))./(T/N);
end
a(N,:)=(Vf-V(N,:))./(T/N);
for i=1:N
aa(i,1)=sqrt(a(i,1)^2+a(i,2)^2);
end

Ef=225010^3;
g=9.8;
EE=zeros(N,1);
for i=1:N
EE(i,1)=(w1*(VV(i,1)^3)+(w2/VV(i,1))(1+(aa(i,1)^2)/g^2))(T/N);
end
Esum=sum(EE);
Ef=3*10^4;

for i=1:N-1
d(i,:)=tr(i+1,:)-tr(i,:);
end
d(N,:)=trf-tr(N,:);
yp=ones(N-1,1);
cosd=ones(N-1,1);
for i=2:N
yp(i-1)=sqrt(d(i-1,1)^2+d(i-1,2)^2)/sqrt(d(i,1)^2+d(i,2)^2);
end
for i=2:N
yp(i-1)=sqrt(d(i-1,1)^2+d(i-1,2)^2)/sqrt(d(i,1)^2+d(i,2)^2);
end
for i=2:N
cosd(i,1)=(d(i,:)*d(i-1,:)’)/(sqrt(d(i,1)^2+d(i,2)^2)*sqrt(d(i-1)^2+d(i-1,2)^2));
end

RRR=zeros(M,N);
dtr=zeros(M,N,2);
for i=1:M
for j=1:N
dtr(i,j,:)=tr(j,:)-sm(i,:);
RRR(i,j)=dtr(i,j,1)^2;
end
end

cvx_begin
%变量
variable trx(N+1,2);
variable deta(N,1);
%表达式
expression Vx(N+1,2);
expression ax(N,2);
expression Ex(N,1);
expression dx(N,2);
expression dtrx(M,N,2);
expression RRR1(M,N);
expression RRR2(M,N);
expression RRR3(M,N);
expression rudtrx(M,N,2);
expression chudtrx(M,N,2);

``````    for i=1:N
Vx(i,:)=(trx(i+1,:)-trx(i,:))./(T/N);
end
Vx(N+1,:)=Vx(1,:);

for i=1:N
ax(i,:)=(Vx(i+1,:)-Vx(i,:))./(T/N);
end

for i=1:N
end

for i=1:N
dx(i,:)=(trx(i+1,:)-trx(i,:));
end

for i=1:M
for j=1:N
dtrx(i,j,:)=(trx(j,:)-sm(i,:));
end
end
for i=1:M
for j=1:N
rudtrx(i,j,:)=dtrx(i,j,:);

RRR1(i,j)=dtrx(i,j,1)^2;
RRR3(i,j)=pow_pos(norm(dtrx(i,j,1)),2);

chudtrx(i,j,:)=dtrx(i,j,:);
end
end

%minimize sum(Ex)
subject to

trx(1:N,:)==tr;
trx(N+1,:)==trf;
for i=1:N
deta(i,1)>=Vmin;
Vx(i,1)^2+Vx(i,2)^2<=Vmax^2
V(i,1)^2+V(i,2)^2+2.*V(i,:)*(Vx(i,:)-V(i,:))'>=deta(i,1)^2
ax(i,1)^2+ax(i,2)^2<=amax^2;
end
sum(Ex)<=Ef;
for i=2:N
dx(i,:)*d(i-1,:)'+d(i,:)*dx(i-1,:)'-d(i,:)*d(i-1,:)'-0.5*(sum((dx(i,:)-d(i,:)).*(dx(i,:)-d(i,:))))-0.5*(sum((dx(i-1,:)-d(i-1,:)).*(dx(i-1,:)-d(i-1,:))))>=0.5*cos(faimax1)*(yp(i-1,1)*(dx(i,1)^2+dx(i,2)^2)+(1/yp(i-1,1))*(dx(i-1,1)^2+dx(i-1,2)^2));
end
``````

cvx_end

## Calling SDPT3 4.0: 19746 variables, 8362 equality constraints For improved efficiency, SDPT3 is solving the dual problem.

num. of constraints = 8362
dim. of sdp var = 9984, num. of sdp blk = 4992
dim. of socp var = 2000, num. of socp blk = 1000
dim. of linear var = 2700
dim. of free var = 70 *** convert ublk to lblk

SDPT3: Infeasible path-following algorithms

## number of iterations = 56 primal objective value = 6.09575996e-09 dual objective value = 0.00000000e+00 gap := trace(XZ) = 1.06e-08 relative gap = 1.06e-08 actual relative gap = 6.10e-09 rel. primal infeas (scaled problem) = 1.05e-14 rel. dual " " " = 1.06e-12 rel. primal infeas (unscaled problem) = 0.00e+00 rel. dual " " " = 0.00e+00 norm(X), norm(y), norm(Z) = 3.3e-09, 1.7e+07, 1.7e+07 norm(A), norm(b), norm© = 4.6e+02, 1.0e+00, 1.0e+05 Total CPU time (secs) = 7.80 CPU time per iteration = 0.14 termination code = 0 DIMACS: 1.0e-14 0.0e+00 1.1e-11 0.0e+00 6.1e-09 1.1e-08

Status: Solved
Optimal value (cvx_optval): -6.09576e-09

RRR is defined outside CVX：

for i=1:M
for j=1:N
dtr(i,j,:)=tr(j,:)-sm(i,:);
RRR(i,j)=dtr(i,j,1)^2;
end
end

RRR1, RRR2 and RRR3 are defined in CVX：
for i=1:M
for j=1:N
dtrx(i,j,:)=(trx(j,:)-sm(i,:));
end
end

``````for i=1:M
for j=1:N
rudtrx(i,j,:)=dtrx(i,j,:);

RRR1(i,j)=dtrx(i,j,1)^2;
RRR3(i,j)=pow_pos(norm(dtrx(i,j,1)),2);

chudtrx(i,j,:)=dtrx(i,j,:);
end
end
``````

In addition, there is a constraint trx==tr in CVX

Your posts are not clear. You refer to a variable trx which does not appear in your program.

As for comparing two different programs, please show the programs, complete with defining input data for both formulations and the output they produce.

Note however, if a variable is set to a numerical value prior to cvx_begin, and it is declared as a variable inside CVX or used as an expression inside CVX, the CVX variable or expression will override any previous value, and it will be the same as if that variable had never been set to anything before cvx_begin.

Sorry, let me describe the problem again. The variable I optimized in the program is trx (N+1,2), and I set a constant tr (N, 2).

In CVX, I use the constraint trx (1: N,:)==tr to constrain the variable trx so that the first N values of trx are equal to tr.

Then
RRR1 (i, j, 1)=dtrx (i, j, 1) ^ 2 (dtrx (i, j,:))=trx (j,:)- sm (i,:))
calculated in CVX should be equal to
RRR (i, j, 1)=dtr (i, j, 1) ^ 2 (dtr (i, j,:)=tr (j,:))- sm (i,:)) (1<=j<=N).

But the result shows that dtrx=dtr, dtrx^ 2!= dtr^2(RRR1!=RRR).

clear all
con=2;
con2=con^2;
cvx_begin
variable x;
expression x2;
x2=x^2;
subject to
x==con;
x2<=5;
cvx_end

x=con,x^2!=con^2

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

num. of constraints = 1
dim. of sdp var = 2, num. of sdp blk = 1
dim. of linear var = 1

SDPT3: Infeasible path-following algorithms

## number of iterations = 7 primal objective value = 2.45272056e-09 dual objective value = 0.00000000e+00 gap := trace(XZ) = 2.45e-09 relative gap = 2.45e-09 actual relative gap = 2.45e-09 rel. primal infeas (scaled problem) = 0.00e+00 rel. dual " " " = 1.00e-12 rel. primal infeas (unscaled problem) = 0.00e+00 rel. dual " " " = 0.00e+00 norm(X), norm(y), norm(Z) = 9.3e-09, 6.8e-01, 5.3e+00 norm(A), norm(b), norm© = 2.4e+00, 1.0e+00, 6.8e+00 Total CPU time (secs) = 0.32 CPU time per iteration = 0.05 termination code = 0 DIMACS: 0.0e+00 0.0e+00 1.1e-12 0.0e+00 2.5e-09 2.5e-09

Status: Solved
Optimal value (cvx_optval): -2.45272e-09

Please show a complete MATLAB session which has all the variables and constraints you are talking about. You have not yet done so.

Thank you. The problem has been solved. There is a constraint that is too loose