# Hello helper, I'm trying to run this code (energy optimization), but i get an error message :Index in position 1 exceeds array bounds (must not exceed 1).

5 views (last 30 days)
Jem Jem on 8 Aug 2022
Commented: Jem Jem on 11 Aug 2022
Hello helper, I'm trying to run this code (energy optimization), but i get an error message .What does "Index in position 1 exceeds array bounds (must not exceed 1)" mean? and how to solve this?
Here is code :
T=24;%time
M = 4;%grid
P=76;%generated power
E=12;%BES energy
K=16;%EV energy
Em=0.3;%BES minimal
Km=0.8;%EV minimal
x=ones(1,T);
y=ones(1,T);
q =ones(1,T);
for n = 2:T
for m =1:M
if abs(E(n,m) - E(n-1,m) <= E)%(6a)
if abs(K(n,m) - K(n-1,m) <= K)%(6b)
if Em(n,m) <= E(n,m) <= E%(6c)
if Km(n,m) <= K(n,m) <= K%(6d)
x(n,m) = P(n,m)+E(n,m)-E(n-1,m)+K(n,m)-K(n-1,m);%(6
y(n,m) = [min(P(n,m)+x(n,m)-E(n)-K(n,m)),0];
q(n,m) = [max(E(n,m)-Em(n,m)+K(n)-Km(n,m)+y(n,m)),0];%
end
end
end
end
end
end
figure()
plot(x,'--*r','Linewidth',2,'MarkerSize',8);hold on
plot(y,'--*g','Linewidth',2,'MarkerSize',8);hold on
plot(q,'--*b','Linewidth',2,'MarkerSize',8);hold on
grid on
hold off
##### 1 CommentShowHide None
Jem Jem on 9 Aug 2022
can not fixed it

Torsten on 8 Aug 2022
Edited: Torsten on 8 Aug 2022
You use P, E, K, Em and Km as 2-dimensional arrays, but they are only scalar values. Thus all of your if-statements and references to P as an array will throw an error.
Further, "double comparisons" are not possible in MATLAB.
Thus something like
if a <= b <= c
must be written as
if a <= b && b <= c
Jem Jem on 11 Aug 2022
Thanks