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).

1 view (last 30 days)
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

Accepted Answer

Torsten
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
  6 Comments
Torsten
Torsten on 11 Aug 2022
Edited: Torsten on 11 Aug 2022
I repeat what I wrote before:
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.
To be precise: E, K, Em, Km and P must be arrays of size (TxM), not scalars of size (1x1).

Sign in to comment.

More Answers (0)

Categories

Find more on Linear Programming and Mixed-Integer Linear Programming 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!