Index in position 2 exceeds array bounds (must not exceed 1). I want to plot "ev_s" versus "time i"

1 次查看(过去 30 天)
Hello, I have 10 EVs, and each EV have different battery capacity and load demand.
I want to sell the EV energy surplus to the other EV claster at time t interval, which is
t = 1:1:24; %hour
E =10;%EVs
ev_c = [26; 28; 24; 28; 24; 26; 28; 28; 26; 26];% EVs capacity
ev_L = [18; 19; 16; 19; 16; 18; 19; 19; 18; 18];% EVs EVs load demand
for i =1:t
for j = 1:E
ev_s(i,j) = ev_c(i,j)-ev_L(i,j);% EV surplus
end
end
figure
plot(1:i,ev_s)% I want to plot ev_s versus time i
  4 个评论
Chandler Hall
Chandler Hall 2022-11-9
Instead of having ev_c and ev_L being constant arrays, you could implement them each as a function of two variables, time and EV# (1-10). Currently time plays no role at all.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2022-11-10
移动:Walter Roberson 2022-11-10
t = 1:1:24; %hour
that is a row vector.
for i =1:t
t is a row vector, so that is equivalent to
for i = 1 : (1:1:24)
but when you use the : operator with non-scalars, only the first element of the non-scalar is selected. So that code is equivalent to
for i = 1 : 1
If you want to use i to loop over the indices of t, then you should be using
for i = 1 : numel(t)
After the loop, i will be left as the last value it was assigned, which (after the modification) would be 24
plot(1:i,ev_s)%
i is the last value that it was assigned by the for loop, so that is plot(1:24, ev_s) where ev_s is 24 x 10 . That is defined: plot() will automatically notice that the number of columns of ev_s does not match the number of elements of 1:24 but that the number of rows does match, so plot() will automatically treat that as-if you had plot(1:i, ev_s.') which would produce 10 lines with 24 points along each line.
... after the fix for the for i loop. Without the fix, you go into the plot as plot(1:1, ev_s) and that is a mismatch on the sizes.
  9 个评论
Jem Jem
Jem Jem 2022-11-10
Thank you, this what I defined and updated, but for loop iteration is not working
clear all
clc
E =10;%EVs
t = 24; %hour
ev_c = zeros(E,t);
ev_L = zeros(E,t);
ev_c(:,t) = [26; 28; 24; 28; 24; 26; 28; 28; 26; 26];% EVs capacity
ev_L(:,t)= [18; 19; 16; 19; 16; 18; 19; 19; 18; 18];% EVs EVs load demand
for i = 1 : numel(t)
for j = 1:E %1 : 1:numel(t)
ev_s(j,i) = ev_c(j,i)-ev_L(j,i);
end
end
figure
plot(1:t,ev_s(1,:),'--*r','Linewidth',1);hold on
plot(1:t,ev_s(2,:),'--*b','Linewidth',1);hold on
plot(1:t,ev_s(3,:),'--*g','Linewidth',1);hold on
plot(1:t,ev_s(4,:),'--*k','Linewidth',1);hold on
plot(1:t,ev_s(5,:),'--*k','Linewidth',1);hold on
plot(1:t,ev_s(6,:),'--*k','Linewidth',1);hold on
plot(1:t,ev_s(7,:),'--*k','Linewidth',1);hold on
plot(1:t,ev_s(8,:),'--*k','Linewidth',1);hold on
plot(1:t,ev_s(9,:),'--*k','Linewidth',1);hold on
plot(1:t,ev_s(10,:),'--*k','Linewidth',1);hold on
grid on
hold off
Torsten
Torsten 2022-11-10
E = 10;%EVs
t = 24; %hour
ev_c = repmat([26; 28; 24; 28; 24; 26; 28; 28; 26; 26],1,t)% EVs capacity
ev_c = 10×24
26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26
ev_L = repmat([18; 19; 16; 19; 16; 18; 19; 19; 18; 18],1,t)% EVs EVs load demand
ev_L = 10×24
18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18
ev_s = ev_c-ev_L
ev_s = 10×24
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
plot(1:t,ev_s)
grid on

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by