Index in position 2 exceeds array bounds (must not exceed 1). I want to plot "ev_s" versus "time i"
1 view (last 30 days)
Show older comments
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 Comments
Chandler Hall
on 9 Nov 2022
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.
Answers (1)
Walter Roberson
on 10 Nov 2022
Moved: Walter Roberson
on 10 Nov 2022
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 Comments
Torsten
on 10 Nov 2022
E = 10;%EVs
t = 24; %hour
ev_c = repmat([26; 28; 24; 28; 24; 26; 28; 28; 26; 26],1,t)% EVs capacity
ev_L = repmat([18; 19; 16; 19; 16; 18; 19; 19; 18; 18],1,t)% EVs EVs load demand
ev_s = ev_c-ev_L
plot(1:t,ev_s)
grid on
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!