フィルターのクリア

I am unable to solve this array dimensional error can someone help please?

1 回表示 (過去 30 日間)
Grigori
Grigori 2024 年 2 月 26 日
編集済み: Torsten 2024 年 2 月 26 日
n error occurred while running the simulation and the simulation was terminated
Caused by:
Index exceeds array dimensions. Index value 2 exceeds valid range [1-1] for array 'P_battery'. Code generation does not support growing arrays via indexing except by using end+1.
Error in 'vanadiummicrogrid/MATLAB Function' (line 13)
P_battery(i) = P_battery;
Here is my code
function P_battery = EMS(netP, soc)
soc_max = 95; % percent
battery_max = .116; %kW
for i=1:length(netP)
if netP(i) > 0 && soc < soc_max
P_battery = min(netP(i), battery_max);
else
P_battery = max(netP(i), battery_max);
end
P_battery(i) = P_battery;
end
Also can someone tell me whether this program is structured well? I just need it so if the power exceeds the need of the load then the power reroutes to the battery. Thank you!

回答 (1 件)

Walter Roberson
Walter Roberson 2024 年 2 月 26 日
編集済み: Torsten 2024 年 2 月 26 日
function P_battery = EMS(netP, soc)
soc_max = 95; % percent
battery_max = .116; %kW
P_battery = zeros(1, length(netP));
for i=1:length(netP)
if netP(i) > 0 && soc < soc_max
P_bat = min(netP(i), battery_max);
else
P_bat = max(netP(i), battery_max);
end
P_battery(i) = P_bat;
end

Community Treasure Hunt

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

Start Hunting!

Translated by