# How to express this formulation in cvx?

``````clear all
H=100;
q0=[-100,100];
qF=[500,100];
ws=[0,0];
wd=[300,0];
we=[200,200];
gamma_0=10^9;
Pu_avg=10^-3;
Pu_max=39.8*10^-3;
Ps_avg=1;
Ps_max=398*10^-3;
threshold=10^-4;
V=3;
delta_t=2.5
Lmax=V*delta_t;
T=350;
N=T/delta_t;
q_x=linspace(q0(1),qF(1),N);
q_y=linspace(q0(2),qF(2),N);
q_last=[q_x;q_y]';
phi=3;
k=-psi(1);
e=2.718;

%% Ps
an=[];
bn=[];
dUD=[];
dUE=[];
for i=1:N
dUD(i)=sqrt(H^2+(q_x(i)-wd(1))^2+(q_x(i)-wd(2))^2);
end
for i=1:N
dUE(i)=sqrt(H^2+(q_x(i)-we(1))^2+(q_x(i)-we(2))^2);
end
dSD=sqrt((ws(1)-wd(1))^2+(ws(2)-wd(2))^2);
dSE=sqrt((ws(1)-we(1))^2+(ws(2)-we(2))^2);
for i=1:N
an(i)=((e^-k)*gamma_0*dSD^(-phi))/(1+(gamma_0*Pu_avg/dUD(i)^2));
end
for i=1:N
bn(i)=(gamma_0*dSE^(-phi))/(1+(gamma_0*Pu_avg/dUE(i)^2));
end
Ps=[];

mu=bisection(an,bn,N,Ps_avg,1e-8);
for i=1:N
if an(i)>bn(i)
Ps(i)=min(max(calPu(an(i),bn(i),mu),0),Ps_max);
else
Ps(i)=0;
end
end
Ps = Ps+1;%
cn=[];
db=[];
en=[];
fn=[];
for i=1:N
cn(i)=(e^-k)*gamma_0*Ps(i)*dSD^(-phi);
end
for i=1:N
dn(i)=gamma_0/dUD(i)^2;
end
for i=1:N
en(i)=gamma_0*Ps(i)*dSE^(-phi);
end
for i=1:N
fn(i)=gamma_0/dUE(i)^2;
end
Pu=[];
lamda=biscetion2(cn,dn,en,fn,N,Pu_avg,1e-6);
for i=1:N
res=calPs(cn(i),dn(i),en(i),fn(i),lamda);
fprintf('结果%f\n:',res);
Pu(i)=min(res,Pu_max);
end
Pu = Pu+1;%
for i=1:N
L_r(i,1)=dUD(i)^2+H^2;
M_r(i,1)=dUE(i)^2+H^2;
end

for i=1:10
cvx_begin quiet
variable Q(N,2)
variable M(N,1) nonnegative
variable L(N,1) nonnegative
sum=0;
for i=1:N
sum=sum+(log(1+cn(i)-cn(i)*gamma_0*Pu(i)*inv_pos(L(i)+(gamma_0*Pu(i))))/log(2)-...
en(i)*gamma_0*Pu(i)/(log(2)*((M_r(i)+gamma_0*Pu(i))*((en(i)+1)*M_r(i)+gamma_0*Pu(i))))*(M(i)-M_r(i)));
end
maximize (sum)
subject to
for i=2:N
(Q(i,1)-Q(i-1,1))^2+(Q(i,2)-Q(i-1,2))^2<=(V*delta_t)^2;
end
(Q(1,1)-q0(1,1))^2+(Q(1,2)-q0(1,2))^2<=(V*delta_t)^2;
Q(N,1)==qF(1,1);
Q(N,2)==qF(1,2);
for i=1:N
H^2+(Q(i,1)-we(1,1))^2+(Q(i,2)-we(1,2))^2<=M(i);
L(i)+norm(q_last(i,:))^2-2*(q_last(i,:)-wd(1,:))'*Q(i,:)-norm(wd)^2<=H^2;
end
cvx_end
for i=1:N
q_last(i,:)=Q(i,:);
L_r(i,:)=L(i,:);
M_r(i,:)=M(i,:);
end
%    i
end
``````

thank you. i have modified the code, but something wrong.

`cn(i), gamma_0,Pu(i)` all need to be nonnegative. I’m guessing that is not the case with the inputs you used.

yes, some variables are negative, i have got it, thank you.

Given that, perhaps your optimization problem is not convex, at least with that input data.