# Why does matlab report an error: "Cannot use a value of type cvx as an index."?

hello, everyone！i first wrote a demo, and the cvx could work. When I integrate the cvx related code, it report an error: Cannot use a value of type cvx as an index.

So what does mean? and what can i do?

1 Like

clc, clear
opt = optimset(‘Display’, ‘none’);

dbstop if error

%% basic parameter settings
a = 0;
b = 0;
c = 0;

alpha = 3.6;
N_0 = -111;
P_i_V = 180;
P_k_V = 500;
rho = 6;
g_0 = 10;

vsender_num = 5;
lamda = 15;

cap_v = 1.5 * 10^9;
cap_u = 3.8 * 10^9;
cap_m = 5 * 10^9;

uav_max = 5;

T = 20;
tau = 0.1;

vs_x = zeros(vsender_num,T);

omega_v = zeros(vsender_num, T);
omega_u = zeros(vsender_num,T);
omega_m = zeros(vsender_num,T);

energy_v = zeros(vsender_num, T);
energy_u = zeros(vsender_num, T);
energy_m = zeros(vsender_num, T);

chosen_mode = zeros(vsender_num, T);
energy_res = zeros(vsender_num, T);
omega_res = zeros(vsender_num, T);

uav_xx = zeros(T,1);
uav_yy = zeros(T,1);

Q = zeros(vsender_num,T);
E_t = 88;
V = 1e+5;

% initial coordinate

for i = 1 : vsender_num
for j = 1 : T
vs_task(i,j) = 100 + randi([1,5]* 10); % D_i(t)
end
end

%% start
t = 1;
disp([’***** t=’, num2str(t), ‘时刻开始 *****’])

``````% coordinate
for i = 1 : vsender_num
vs_x(i,t) = vsender_x(i) + 15 * t;
end

vr_x(j,t) = vreceiver_x(j) + 18 * t;
end
``````

for t = 1:T

``````sender_nei = [];
sen_nei = [];
k = 1;
for i = 1:vsender_num

D(i,j) = round(sqrt((vs_x(t,i)-vr_x(t,j))^2 + (vsender_y(i)-vreceiver_y(j))^2));    % the distance of vehicles

end
end

D

dis = 0;

cvx_begin
variable uav_x(T,1);
variable uav_y(T,1);

for i = 1:vsender_num
dis = dis + square(uav_x(t,1) - vs_x(i,t)) +  square(uav_y(t,1) - vsender_y(i));
end

uav_z = sum( V .* dis + Q(i,t) .* dis )

minimize  ( uav_z );

subject to
uav_x(1,1) == 0; uav_x(T,1) == 100;
uav_y(1,1) == 0; uav_y(T,1) == 100;

0 <= uav_x <= 100;
0 <= uav_y <= 100;

for k = 2:T
square((uav_x(k,1) - uav_x(k-1,1))) + square(uav_y(k,1) - uav_y(k-1,1)) <= square(uav_max * t)
end

cvx_end

for i = 1:vsender_num

a_num = i;
if D(i,j) <= 50
b_num = 0;
for k = 1:5
sum = 0;
c_num = 0;
if k~= a_num
if D(k,j) <= 50
c_num = c_num + 1;
sender_nei = [sender_nei,D(k,j)];
k = k + 1;
b_num = c_num;
end
end
end

len = length(sender_nei);
sen_nei = sender_nei;

for d_num = 1:len
sum = sum + sen_nei(d_num);
end

gama(i,j) = (P_i_V * D(i,j)^(-alpha)) / (N_0 + P_k_V * sum);
beta(i,j) = rho * log2(1 + gama(i,j));

elseif D(i,j) > 50
omega_v(i,t) = Inf;
energy_v(i,t) = Inf;

sender_nei = [];
end
end

R = 100 * tand(37);
dis_v_u = sqrt( (vs_x(i,t) - uav_x(t,1))^2 + (vsender_y(i) - uav_y(t,1))^2 );
if dis_v_u <= R
disp('在覆盖范围内')

h_i_u = g_0 / ((1e+4) + ( (vs_x(i,t) - uav_x(t,1) )^2 + (vsender_y(i) - uav_y(t,1))^2 ) );
beta_u = cap_u * log2( 1 + ( P_i_V * h_i_u ) / ( cap_u * 1e-4));
energy_u(i,t) = (vs_task(i,t) * P_i_V / beta_u) + ((1900 * vs_task(i,t) * 40) / cap_u) + 0.5 * 9.65 * ( (vs_x(i,t) - uav_x(t,1) )^2 + (vsender_y(i) - uav_y(t,1))^2 );

else
disp('不在覆盖范围内')
omega_u(i,t) = Inf;
energy_u(i,t) = Inf;
end

disp(['t=', num2str(t), '时刻 进行V2M中'])
h_i_m = 1 /  ( (vs_x(i,t) - 50 )^2 + (vsender_y(i) - 50)^2 ) ;
beta_m = cap_m * log2( 1 + ( P_i_V * h_i_m ) / ( cap_m * 1e-4));
energy_m(i,t) = vs_task(i,t) * P_i_V / beta_m;

[~ , mode] = min([omega_v(i,t), omega_u(i,t), omega_m(i,t)]);
chosen_mode(i,t) = mode;

if chosen_mode(i,t) == 1
omega_res(i,t) = omega_v(i,t);
energy_res(i,t) = energy_v(i,t);
elseif chosen_mode(i,t) == 2
omega_res(i,t) = omega_u(i,t);
energy_res(i,t) = energy_u(i,t);
elseif chosen_mode(i,t) == 3
omega_res(i,t) = omega_m(i,t);
energy_res(i,t) = energy_m(i,t);
end

chosen_mode(i,t)
omega_res(i,t)
energy_res(i,t)

%   disp('Q(',num2str(i),',',num2str(t),'):');
Q(i,t+1) = max( (Q(i,t) - E_t) , 0) + energy_res(i,t)
end
% update the energy level and go to next time slot
t = t + 1;
``````

end

this is the error

I don’t see anything which should trigger the error you claim.
The following would trigger such an error:

``````variable i integer
variable x(3)
x(i)
``````

Can you make a simpler self-contained version which illustrates the error? Try looking at the individual pieces of the statement which triggers the error.

This isn’t necessarily an error, but looks suspect (even though it shouldn’t trigger an error message): `Q(i,t) ` uses index `i` after the for loop with i is over. Therefore, `Q(i,t)` is always `Q(vsender_num,t)`.

i made a simpler version, which could work, and didn;t have this error. Also, i set the Q(i,t) as the number, and the error appeared again. so, i’m confused now

this is the simpler version
clc, clear
opt = optimset(‘Display’, ‘none’);

dbstop if error

%% basic parameter settings
a = 0;
b = 0;
c = 0;

alpha = 3.6;
N_0 = -111;
P_i_V = 180;
P_k_V = 500;
rho = 6;
g_0 = 10;

vsender_num = 5;
lamda = 15;

cap_v = 1.5 * 10^9;
cap_u = 3.8 * 10^9;
cap_m = 5 * 10^9;

uav_max = 5;

T = 20;
tau = 0.1;

vs_x = zeros(vsender_num,T);

omega_v = zeros(vsender_num, T);
omega_u = zeros(vsender_num,T);
omega_m = zeros(vsender_num,T);

energy_v = zeros(vsender_num, T);
energy_u = zeros(vsender_num, T);
energy_m = zeros(vsender_num, T);

chosen_mode = zeros(vsender_num, T);
energy_res = zeros(vsender_num, T);
omega_res = zeros(vsender_num, T);

uav_xx = zeros(T,1);
uav_yy = zeros(T,1);

Q = zeros(vsender_num,T);
E_t = 88;
V = 1e+5;

% initial coordinate

for i = 1 : vsender_num
for j = 1 : T
vs_task(i,j) = 100 + randi([1,5]* 10); % D_i(t)
end
end

%% start
t = 1;

% coordinate
for i = 1 : vsender_num
vs_x(i,t) = vsender_x(i) + 15 * t;
end

vr_x(j,t) = vreceiver_x(j) + 18 * t;
end

``````for t = 1:T

dis = 0;

cvx_begin
variable uav_x(T,1);
variable uav_y(T,1);

for i = 1:vsender_num
dis = dis + square(uav_x(t,1) - vs_x(i,t)) +  square(uav_y(t,1) - vsender_y(i));
end

uav_z = sum( V .* dis  + Q(i,t) * dis )

minimize  ( uav_z );

subject to

uav_x(1,1) == 0; uav_x(T,1) == 100;
uav_y(1,1) == 0; uav_y(T,1) == 100;

0 <= uav_x <= 100;
0 <= uav_y <= 100;

for k = 2:T
square((uav_x(k,1) - uav_x(k-1,1))) + square(uav_y(k,1) - uav_y(k-1,1)) <= square(uav_max * t)
end

cvx_end

uav_x
uav_y

Q(i,t+1) = max( (Q(i,t) - 1) , 5) + 10;

t = t + 1;
end``````

Can you make a self-contained version illustrating the error? Your example does not contain the input data.

clc, clear
opt = optimset(‘Display’, ‘none’);

dbstop if error

%% basic parameter settings
a = 0;
b = 0;
c = 0;

alpha = 3.6;
N_0 = -111;
P_i_V = 180;
P_k_V = 500;
rho = 6;
g_0 = 10;

vsender_num = 5;
lamda = 15;

cap_v = 1.5 * 10^9;
cap_u = 3.8 * 10^9;
cap_m = 5 * 10^9;

uav_max = 5;

T = 20;
tau = 0.1;

vs_x = zeros(vsender_num,T);

omega_v = zeros(vsender_num, T);
omega_u = zeros(vsender_num,T);
omega_m = zeros(vsender_num,T);

energy_v = zeros(vsender_num, T);
energy_u = zeros(vsender_num, T);
energy_m = zeros(vsender_num, T);

chosen_mode = zeros(vsender_num, T);
energy_res = zeros(vsender_num, T);
omega_res = zeros(vsender_num, T);

uav_xx = zeros(T,1);
uav_yy = zeros(T,1);

Q = zeros(vsender_num,T);
E_t = 88;
V = 1e+5;

% initial coordinate
vsender_x = [1,5,23,67,4];
vsender_y = [0,5,10,15,20];

for i = 1 : vsender_num
for j = 1 : T
vs_task(i,j) = 100 + randi([1,5]* 10); % D_i(t)
end
end

%% start
t = 1;
while t <= T
disp([’***** t=’, num2str(t), ‘时刻开始 *****’])

% coordinate
for i = 1 : vsender_num
vs_x(i,t) = vsender_x(i) + 15 * t;
end

vr_x(j,t) = vreceiver_x(j) + 18 * t;
end

sender_nei = [];
sen_nei = [];
k = 1;
for i = 1:vsender_num

``````    D(i,j) = round(sqrt((vs_x(t,i)-vr_x(t,j))^2 + (vsender_y(i)-vreceiver_y(j))^2));    % the distance of vehicles

end
``````

end

D

% Step 1 compute the trajectory of UAV

disp([’***** t=’, num2str(t), ‘时刻开始计算无人机的轨迹 *****’])

dis = 0;

cvx_begin
variable uav_x(T,1);
variable uav_y(T,1);

``````for i = 1:vsender_num
dis = dis + square(uav_x(t,1) - vs_x(i,t)) +  square(uav_y(t,1) - vsender_y(i));
end

uav_z = sum( V .* dis + Q(i,t) .* dis )

minimize  ( uav_z );

subject to
uav_x(1,1) == 0; uav_x(T,1) == 100;
uav_y(1,1) == 0; uav_y(T,1) == 100;

0 <= uav_x <= 100;
0 <= uav_y <= 100;

for k = 2:T
square((uav_x(k,1) - uav_x(k-1,1))) + square(uav_y(k,1) - uav_y(k-1,1)) <= square(uav_max * t)
end
``````

cvx_end

uav_x
uav_y

disp([’##### cvx求解结束 #####’])

for i = 1:vsender_num

``````% V2V transmission
disp(['t=', num2str(t), '时刻 进行V2V中'])
a_num = i;
if D(i,j) <= 50
b_num = 0;
for k = 1:5
sum = 0;
c_num = 0;
if k~= a_num
if D(k,j) <= 50
c_num = c_num + 1;
sender_nei = [sender_nei,D(k,j)];
k = k + 1;
b_num = c_num;
end
end
end

len = length(sender_nei);
sen_nei = sender_nei;

for d_num = 1:len
sum = sum + sen_nei(d_num);
end

gama(i,j) = (P_i_V * D(i,j)^(-alpha)) / (N_0 + P_k_V * sum);
beta(i,j) = rho * log2(1 + gama(i,j));

elseif D(i,j) > 50
omega_v(i,t) = Inf;
energy_v(i,t) = Inf;

sender_nei = [];
end
end

% V2U transmission

disp(['t=', num2str(t), '时刻 进行V2U中'])
R = 100 * tand(37);     %无人机最大覆盖范围的半径
dis_v_u = sqrt( (vs_x(i,t) - uav_x(t,1))^2 + (vsender_y(i) - uav_y(t,1))^2 );

if dis_v_u <= R

disp('在覆盖范围内')

h_i_u = g_0 / ((1e+4) + ( (vs_x(i,t) - uav_x(t,1) )^2 + (vsender_y(i) - uav_y(t,1))^2 ) );
beta_u = cap_u * log2( 1 + ( P_i_V * h_i_u ) / ( cap_u * 1e-4));
energy_u(i,t) = (vs_task(i,t) * P_i_V / beta_u) + ((1900 * vs_task(i,t) * 40) / cap_u) + 0.5 * 9.65 * ( (vs_x(i,t) - uav_x(t,1) )^2 + (vsender_y(i) - uav_y(t,1))^2 );

else
disp('不在覆盖范围内')
omega_u(i,t) = Inf;
energy_u(i,t) = Inf;
end

% V2M transmission
disp(['t=', num2str(t), '时刻 进行V2M中'])
h_i_m = 1 /  ( (vs_x(i,t) - 50 )^2 + (vsender_y(i) - 50)^2 ) ;
beta_m = cap_m * log2( 1 + ( P_i_V * h_i_m ) / ( cap_m * 1e-4));
energy_m(i,t) = vs_task(i,t) * P_i_V / beta_m;

[~ , mode] = min([omega_v(i,t), omega_u(i,t), omega_m(i,t)]);
chosen_mode(i,t) = mode;

if chosen_mode(i,t) == 1
omega_res(i,t) = omega_v(i,t);
energy_res(i,t) = energy_v(i,t);
elseif chosen_mode(i,t) == 2
omega_res(i,t) = omega_u(i,t);
energy_res(i,t) = energy_u(i,t);
elseif chosen_mode(i,t) == 3
omega_res(i,t) = omega_m(i,t);
energy_res(i,t) = energy_m(i,t);
end

chosen_mode(i,t)
omega_res(i,t)
energy_res(i,t)

Q(i,t+1) = max( (Q(i,t) - E_t) , 0) + energy_res(i,t)
``````

end
% update the energy level and go to next time slot
t = t + 1;
end

I executed the CvX program once, and there were no errore. The problem was reported to be infeasible. I didn’t do anything after that.

oh,no. i still have this error. Maybe i lost some important configuration file.
So do u know, why does this error appear? How can i deal with it?

I have no idea why you are getting the error. Perhaps something is corrupted or there are name conflicts. All I can suggest is

1. Try using a new MATLAB session if you haven’t already (sometimes a MATLAB session gets corrupted)

2. Reinstall CVX, being sure to use CVX 2.2, and not CVX 3.0beta. Make sure all CVX directories are removed from the MATLAB path prior to reinstallation, which you should do in a new MATLAB session.

ok. i will have a try. Thank u a lot!!!