Hi,

I am writing the below optimization function. I am optimizing the trajectory (c_q) while minimizing the energy (subq_E). After I get the optimized c_q values, I copy it into another variable q, and calculate the energy using the same formula that I gave in optimization code. (I also verified that the copying is proper c_q and q are exactly same).

Now, when I calculated the energy using the copied variable, the energy calculated is 3313.71. When I print the cvx_optval, it is 31.7816. How is it possible, Shouldn’t they both be same?

Am I missing something? Please help

I am pasting my code and output below.

cvx_begin quiet

variable c_q(M,N, 1,2);

```
expression subq_E;
subq_E = 0;
for m = M
for n = N-1
subq_E = subq_E + (w_2*0.5*Q*pow_pos(norm(reshape(c_q(m,n+1,:,:),[1 2])-reshape(c_q(m,n,:,:),[1 2])),2))/Delta;
end
end
minimize subq_E
for m = 1:M
c_q(m,1,:,:) == c_q(m,N,:,:); %C6
end
for m = 1:M
for n = 1:N-1
pow_pos(norm(reshape(c_q(m,n+1,:,:),[1 2]) - reshape(c_q(m,n,:,:),[1 2])),2) <= S_max^2; %C7
end
end
H_cottheta = H *cottheta;
for k = 1:K
for m = 1:M
for n = 1:N
a(k,m,n) * norm(reshape(c_q(m,n,:,:),[1 2]) -W_k(k,:),2) <= H_cottheta;
end
end
end
for n = 1:N
for m = 1:M
for j = m+1:M
- norm(reshape(q(m,n,:,:),[1 2])-reshape(q(j,n,:,:),[1 2]))^2 + 2*(reshape(q(m,n,:,:),[1 2])-reshape(q(j,n,:,:),[1 2])) * transpose(reshape(c_q(m,n,:,:),[1 2])-reshape(c_q(j,n,:,:),[1 2])) >= d_min^2;
end
end
end
```

cvx_end

q = c_q;

E_f5 = zeros(M,N);

for m =1: M

for n =1: N-1

E_f5(m,n) = (0.5*Q*pow_pos(norm(reshape(q(m,n+1,:,:),[1 2])-reshape(q(m,n,:,:),[1 2])),2))/Delta;

end

end

display(['The flight energy consumed: ’ num2str(w_2*sum(E_f5(:))) ‘.’])

display(['Sub5 is ’ cvx_status '. The cumulative objective is: ’ num2str(cvx_optval) ‘.’])

Output:

The flight energy consumed: 3313.171.

Sub5 is Solved. The cumulative objective is: 31.7816.