how to get rid of while loop running infinitely in battery sizing coding?

1 view (last 30 days)
I need to size the battery based on available power from my solar and wind. I need to check the SOC of the battery for every hour and comparing with the energy availability and demand. If my Pgeneration is less than demand, battery has to discharge when SOC is greater than SOC min. If my Pgeneration is greater than the demand, battery has to charge when SOC is lesser than SOC max. IN these condition, if the SOC is not greater or less than limits, then I have to increase the battery capacity and again I need to check the SOC from the begining hour. So I used while loop here. But the while loop is running infinitely. Couldnt get whether I made a mistake in coding or in the battery concept.
while i <= 24
SOC_max = Ebmax;
SOC_min = (1-DoD)*Ebmax;
if (PV_tot(i)+Pwind_tot(i))- P_load(i) > 0
if SOC(i)< SOC_max
SOC(i+1) = (1-d)*SOC(i) + eff_RT*((PV_tot(i)+Pwind_tot(i))- P_load(i))*i;
disp('for the Pgen greater than Pload')
disp(SOC(i))
disp('the PV tot for iteration in Pgen greater than Pload loop')
i = i+1;
else
Ebmax = Ebmax + 1000;
SOC(i+1) = (1-d)*SOC(i) + eff_RT*((PV_tot(i)+Pwind_tot(i))- P_load(i))*i;
disp('for the Pgen greater than Pload,altered Ebmax is')
disp(Ebmax)
i = 1;
end
else (PV_tot(i)+Pwind_tot(i))- P_load(i) < 0
if SOC(i)> SOC_min
SOC(i+1) = (1-d)*SOC(i) + eff_RT*((PV_tot(i)+Pwind_tot(i))- P_load(i))*i;
disp('for the Pgen lesser than Pload')
disp(SOC(i))
i=i+1;
else
Ebmax = Ebmax + 1000;
SOC(i+1) = (1-d)*SOC(i) + eff_RT*((PV_tot(i)+Pwind_tot(i))- P_load(i))*i;
disp('for the Pgen lesser than Pload,altered Ebmax is')
disp(Ebmax)
i = 1;
end
end
end

Answers (2)

Cris LaPierre
Cris LaPierre on 28 May 2020
I suggest using the debugger to set some breakpoints and inspect the value of i. I suspect it keeps finding conditions that keep resetting the counter to 1. When a breakpoint is reached, you will have the opportunity to inspect your variable values and determine if your code is behaving as you expect.
  3 Comments
Cris LaPierre
Cris LaPierre on 3 Jun 2020
I understand that. My suggestion was to use the debugger to inspect when that condition is applied, as it may be happening more often than desired.

Sign in to comment.


Image Analyst
Image Analyst on 12 Jun 2020
Edited: Image Analyst on 12 Jun 2020
You need a failsafe. Evidently i never exceeds 24 so you might check why that is. In the meantime, to prevent infinite loops, use a failsafe, such as a loop counter:
maxIterations = 1000000; % Whatever you think will be WAY more than you ever need.
loopCounter = 1;
while i <= 24 && loopCounter <= maxIterations
% Your code....
% At end of loop, increment the loop counter.
loopCounter = loopCounter + 1;
end
if loopCounter > maxIterations
warningMessage = sprintf('Loop ended early after %d iterations with i = %d.', loopCounter-1, i);
uiwait(warndlg(warningMessage));
end

Categories

Find more on Propulsion and Power Systems 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!