# Proper usage of GoTo to avoid infinite loop

4 views (last 30 days)
riya mathew on 27 Mar 2021
Answered: Les Beckham on 28 Mar 2021
function [Eb,Edump,diesel,t] = OUR_RUN_DIESEL_GENERATOR(Pp,Eb,Ebmax,uinv,Pl,t,Pg,Edump,Edch,Ech,diesel,Ebmin)
%^^^^^^^^^^^^^^^RUN DIESEL GENERATOR ^^^^^^^^^^^^^^
%LABEL RUN_DIESEL_GENERATOR
if Edch(t)<=((Pg*uinv+Pp(t))-(Pl(t)/uinv))
Eb(t)=Eb(t-1)+(Pg*uinv)+Pp(t)-((Pl(t)/uinv)*1);
if Eb(t)>Ebmax
Edump(t)=Eb(t)-Ebmax;
Eb(t)=Ebmax;
end
if Eb(t)<Ebmin
Edump(t)=0;
Eb(t)=Ebmin;
end
diesel(t)=Pg*uinv;
% return
end
end
While implementing a code on Particle Swarm Optimization to perform Techno-Economic Analysis of a Hybrid Renewable Energy (PV Diesel Battery) System, this function (OUR_RUN_DIESEL_GENERATOR) seems to be running in an infinite loop.
In lieu of this, any help regarding the usage of the statement would be highly appreciated.
##### 2 CommentsShowHide 1 older comment
riya mathew on 27 Mar 2021
We used a .m file that was available online as a reference to develop the code we require. The code pasted above is what was available.
When we run the pso_final_isitso.m file, it is giving us one vaue in an infinite loop. Please refer the image attached below.
We have attached all the .m files we used to run our code. Kindly share your insights to solve this issue. Your help would be highly appreciated.

Les Beckham on 28 Mar 2021
It appears that you are attempting to create a recursive algorithm. If so, replace "%JUMP TO RUN DIESEL GENERATOR!!!" with another call to OUR_RUN_DIESEL_GENERATOR. You probably need to update the t for the next iteration. You haven't provided any way to get out of the recursion, though. Maybe an else for your main if that exits?
I didn't look at the additional code you posted but these issues were immediately obvious from the top level code.