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
        Ex(i,1)=(w1*pow_pos((Vx(i,1)^2+Vx(i,2)^2),1.5)+w2*pow_p(deta(i,1),-1)+(w2/g^2)*quad_over_lin(ax(i,1),deta(i,1))+(w2/g^2)*quad_over_lin(ax(i,2),deta(i,1)))*(T/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;
            RRR2(i,j)=quad_over_lin(dtrx(i,j,1),1);
            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


version predcorr gam expon scale_data
HKM 1 0.000 1 0
it pstep dstep pinfeas dinfeas gap prim-obj dual-obj cputime

0|0.000|0.000|2.7e+03|3.5e+00|4.9e+08| 1.046267e+07 0.000000e+00| 0:0:00| spchol 1 1
1|0.013|0.011|2.7e+03|3.4e+00|1.4e+08| 1.047325e+07 0.000000e+00| 0:0:00| spchol 1 1
2|0.102|0.113|2.4e+03|3.0e+00|7.2e+07| 9.284514e+06 0.000000e+00| 0:0:01| spchol 1 1
3|0.054|0.181|2.3e+03|2.5e+00|5.8e+07| 8.618119e+06 0.000000e+00| 0:0:01| spchol 1 1
4|0.262|0.271|1.7e+03|1.8e+00|5.0e+07| 5.451794e+06 0.000000e+00| 0:0:01| spchol 1 1
5|0.101|0.352|1.5e+03|1.2e+00|4.6e+07| 4.447180e+06 0.000000e+00| 0:0:01| spchol 1 1
6|0.175|0.152|1.2e+03|1.0e+00|4.3e+07| 2.619607e+06 0.000000e+00| 0:0:01| spchol 1 1
7|0.174|0.132|1.0e+03|8.7e-01|4.2e+07| 4.205816e+05 0.000000e+00| 0:0:01| spchol 1 1
8|0.075|0.235|9.4e+02|6.7e-01|4.1e+07|-3.512918e+05 0.000000e+00| 0:0:01| spchol 1 1
9|0.109|0.175|8.4e+02|5.5e-01|3.9e+07|-1.605833e+06 0.000000e+00| 0:0:02| spchol 1 1
10|0.301|0.296|5.9e+02|3.9e-01|3.5e+07|-5.013743e+06 0.000000e+00| 0:0:02| spchol 1 1
11|0.225|0.294|4.5e+02|2.7e-01|3.2e+07|-8.127126e+06 0.000000e+00| 0:0:02| spchol 1 1
12|0.158|0.116|3.8e+02|2.4e-01|3.2e+07|-1.146585e+07 0.000000e+00| 0:0:02| spchol 1 1
13|0.118|0.131|3.4e+02|2.1e-01|3.3e+07|-1.492824e+07 0.000000e+00| 0:0:02| spchol 1 1
14|0.071|0.189|3.1e+02|1.7e-01|3.3e+07|-1.754167e+07 0.000000e+00| 0:0:02| spchol 1 1
15|0.127|0.165|2.7e+02|1.4e-01|3.4e+07|-2.175964e+07 0.000000e+00| 0:0:02| spchol 1 1
16|0.138|0.277|2.4e+02|1.0e-01|3.6e+07|-2.270745e+07 0.000000e+00| 0:0:03| spchol 1 1
17|0.102|0.186|2.1e+02|8.4e-02|3.8e+07|-2.387293e+07 0.000000e+00| 0:0:03| spchol 1 1
18|0.237|0.413|1.6e+02|4.9e-02|3.7e+07|-2.258853e+07 0.000000e+00| 0:0:03| spchol 1 1
19|0.097|0.253|1.5e+02|3.7e-02|3.8e+07|-2.167469e+07 0.000000e+00| 0:0:03| spchol 1 1
20|0.412|0.466|8.6e+01|2.0e-02|3.0e+07|-1.653419e+07 0.000000e+00| 0:0:03| spchol 1 1
21|0.353|0.551|5.6e+01|8.8e-03|2.5e+07|-1.252655e+07 0.000000e+00| 0:0:03| spchol 1 1
22|0.312|0.581|3.8e+01|3.7e-03|2.2e+07|-6.798785e+06 0.000000e+00| 0:0:03| spchol 1 1
23|0.306|0.397|2.7e+01|2.2e-03|1.8e+07|-3.164786e+06 0.000000e+00| 0:0:03| spchol 1 1
24|0.269|0.489|1.9e+01|1.1e-03|1.6e+07|-7.981628e+05 0.000000e+00| 0:0:04| spchol 1 1
25|0.315|0.676|1.3e+01|3.7e-04|1.4e+07| 8.408077e+05 0.000000e+00| 0:0:04| spchol 1 1
26|0.487|0.701|6.8e+00|1.1e-04|8.7e+06| 1.613387e+06 0.000000e+00| 0:0:04| spchol 1 1
27|0.510|0.205|3.3e+00|8.7e-05|5.0e+06| 1.466766e+06 0.000000e+00| 0:0:04| spchol 1 1
28|0.855|0.405|4.9e-01|5.2e-05|1.3e+06| 7.953964e+05 0.000000e+00| 0:0:04| spchol 1 1
29|0.903|0.542|4.7e-02|2.4e-05|2.5e+05| 1.959864e+05 0.000000e+00| 0:0:04| spchol 1 1
30|0.886|0.567|5.4e-03|1.9e-05|5.3e+04| 4.109667e+04 0.000000e+00| 0:0:04| spchol 1 1
31|0.886|0.688|6.1e-04|1.5e-05|1.2e+04| 9.787547e+03 0.000000e+00| 0:0:04| spchol 1 1
32|0.765|0.697|1.4e-04|1.2e-05|4.4e+03| 3.463386e+03 0.000000e+00| 0:0:05| spchol 1 1
33|0.964|0.933|5.2e-06|3.7e-06|3.4e+02| 2.835815e+02 0.000000e+00| 0:0:05| spchol 1 1
34|0.965|0.958|1.8e-07|1.2e-06|1.9e+01| 1.736906e+01 0.000000e+00| 0:0:05| spchol 1 1
35|0.986|0.984|2.6e-09|5.6e-08|3.2e-01| 3.160846e-01 0.000000e+00| 0:0:05| spchol 1 1
36|0.989|0.989|3.0e-11|1.8e-09|3.7e-03| 3.583359e-03 0.000000e+00| 0:0:05| spchol 1 1
37|0.985|0.739|5.2e-11|3.1e-10|1.8e-03| 1.056756e-03 0.000000e+00| 0:0:05| spchol 1 1
38|0.831|0.943|3.1e-11|2.8e-11|1.0e-03| 5.821731e-04 0.000000e+00| 0:0:05| spchol 1 1
39|0.896|0.944|1.5e-10|9.0e-12|5.5e-04| 3.135636e-04 0.000000e+00| 0:0:06| spchol 1 1
40|0.892|0.944|1.7e-10|1.0e-11|2.9e-04| 1.663209e-04 0.000000e+00| 0:0:06| spchol 1 1
41|0.892|0.944|1.3e-10|1.5e-11|1.5e-04| 8.811515e-05 0.000000e+00| 0:0:06| spchol 1 1
42|0.892|0.944|7.5e-11|2.2e-11|8.1e-05| 4.657339e-05 0.000000e+00| 0:0:06| spchol 1 1
43|0.891|0.944|3.5e-11|1.6e-11|4.3e-05| 2.456654e-05 0.000000e+00| 0:0:06| spchol 1 1
44|0.892|0.944|1.8e-11|8.0e-12|2.3e-05| 1.296322e-05 0.000000e+00| 0:0:06| spchol 1 1
45|0.892|0.944|9.8e-12|4.0e-12|1.2e-05| 6.849169e-06 0.000000e+00| 0:0:06| spchol 1 1
46|0.892|0.944|5.6e-12|2.2e-12|6.3e-06| 3.619884e-06 0.000000e+00| 0:0:06| spchol 1 1
47|0.892|0.944|3.0e-12|1.2e-12|3.3e-06| 1.912403e-06 0.000000e+00| 0:0:07| spchol 1 1
48|0.892|0.944|1.6e-12|1.1e-12|1.8e-06| 1.009915e-06 0.000000e+00| 0:0:07| spchol 1 1
49|0.892|0.944|8.3e-13|1.1e-12|9.3e-07| 5.332086e-07 0.000000e+00| 0:0:07| spchol 1 1
50|0.892|0.944|4.4e-13|1.1e-12|4.9e-07| 2.815109e-07 0.000000e+00| 0:0:07| spchol 1 1
51|0.892|0.944|2.4e-13|1.1e-12|2.6e-07| 1.486257e-07 0.000000e+00| 0:0:07| spchol 1 1
52|0.892|0.944|1.3e-13|1.1e-12|1.4e-07| 7.846729e-08 0.000000e+00| 0:0:07| spchol 1 1
53|0.892|0.944|7.0e-14|1.1e-12|7.2e-08| 4.142649e-08 0.000000e+00| 0:0:07| spchol 1 1
54|0.892|0.944|3.7e-14|1.1e-12|3.8e-08| 2.187075e-08 0.000000e+00| 0:0:08| spchol 1 1
55|0.892|0.944|2.0e-14|1.1e-12|2.0e-08| 1.154640e-08 0.000000e+00| 0:0:08| spchol 1 1
56|0.892|0.944|1.0e-14|1.1e-12|1.1e-08| 6.095760e-09 0.000000e+00| 0:0:08|
stop: max(relative gap, infeasibilities) < 1.49e-08

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;
        RRR2(i,j)=quad_over_lin(dtrx(i,j,1),1);
        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

image
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


version predcorr gam expon scale_data
HKM 1 0.000 1 0
it pstep dstep pinfeas dinfeas gap prim-obj dual-obj cputime

0|0.000|0.000|0.0e+00|2.1e+00|3.0e+02| 6.000000e+01 0.000000e+00| 0:0:00| chol 1 1
1|1.000|0.965|8.9e-16|9.8e-02|3.2e+01| 2.402206e+01 0.000000e+00| 0:0:00| chol 1 1
2|0.612|1.000|8.9e-16|2.5e-03|7.3e+00| 6.905672e+00 0.000000e+00| 0:0:00| chol 1 1
3|0.988|1.000|0.0e+00|2.5e-04|1.2e-01| 1.166803e-01 0.000000e+00| 0:0:00| chol 1 1
4|0.989|1.000|0.0e+00|2.5e-05|1.3e-03| 1.300208e-03 0.000000e+00| 0:0:00| chol 1 1
5|0.989|1.000|0.0e+00|2.5e-06|1.4e-05| 1.430648e-05 0.000000e+00| 0:0:00| chol 1 1
6|0.989|1.000|0.0e+00|1.0e-12|1.6e-07| 1.572460e-07 0.000000e+00| 0:0:00| chol 1 1
7|0.995|1.000|0.0e+00|1.0e-12|2.5e-09| 2.452721e-09 0.000000e+00| 0:0:00|
stop: max(relative gap, infeasibilities) < 1.49e-08

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