I tried to run the following MATLAB code, but I face the error say that Index in position 1 exceeds array bounds (must not exceed 1). Error in untitled (line 19)
1 view (last 30 days)
Show older comments
T = 24; % time horizon
MG = 2; % mini grid
PV = [72 72]; % PV power generation for each mini grid at time t interval
PL = [75.9 52]; % load demand power for each mini grid at time t interval
BES = [7 7]; % battery energy storage (BES) for each mini grid at time t interval
BES_max_dis = [3.6 3.6]; % discharged battery energy storage(BES) for each mini grid at time t interval
BES_min = [0.3 0.3]; % minimum battery energy storage(BES) state of charge for each mini grid at time t interval
EV = [0 0]; % electric vehicles (EVs) for each mini grid at time t interval
EV_capacity = [28 26]; % capacity of electric vehicle for each mini grid
EV_max_dis = [20 18]; % maximum discharged electric vehicles (EVs) for each mini grid at time t interval
EV_min = [0.35 0.3]; % minimum electric vehicles (EVs) state of charge for each mini grid at time t interval
SP_max = 10; % maximum PV surplus power for each mini grid at time t interval
SP = zeros(MG, T); % initialize SP
for m = 1:MG % loop through each mini grid
for t = 1:T % loop through each time interval
if (t <= size(PV,2) && t <= size(PL,2) && t <= size(BES,2) && t <= size(EV,2))
SP(m,t) = min((PV(m,t) - PL(m,t) - (BES(m,t) + EV(m,t))), SP_max); % calculate SP
end
end
end
% plot results
figure
plot(SP(1,:), 'b', 'LineWidth', 2)
hold on
plot(SP(2,:), 'r', 'LineWidth', 2)
xlabel('Time interval')
ylabel('PV surplus power (kW)')
legend('Mini grid 1', 'Mini grid 2')
title('PV Surplus Power')
0 Comments
Answers (1)
Sachin Hegde
on 21 Apr 2023
Edited: Sachin Hegde
on 21 Apr 2023
On the first look, i can see an issue in the line for SP(m,t) which is in the if loop.
In that line you have used on the right hand side for all valiables (m,t). where t countes till 24. So the matlab expects the values for PV,PL,BES,EV to have 24 columns but what you have defined earlier has only 2 columns. This is the reason for the error
On the right hand side you should use (1,m) instead of (m,t).
But still i do not understand the line of code. On the left hand side you want to create 2x24 matrix but on the right hand side you have all the terms adding to 1x2 vector. Please check if you have missed anything.
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!