How to get for/while loop to continue loop around?

4 views (last 30 days)
Just to explain my code - I want it to initially satisfy until the height in the feed tank (hfinitial > 0), but then it has the first for loop (for pumping) and the second for loop (for refill). I can only get this to go through each loop once. Can I get it to repeat this for loop a number of times? Do I need to use a different statement e.g. if/while? Code is as follows;
Pi=300000;
g=9.81;
RHO=1400;
Po=101325;
Dt=0.05;
At=pi*(Dt.^2)/4;
Df=6.096;
Af=pi*(Df.^2)/4;
Dpc=1;
Apc=pi*(Dpc.^2)/4;
Cd=0.975;
Cp=0.7;
hpc=1.71;
hinitial=1.71;
hfinitial=6.096;
%All details up to this point must be input by user
%Calculation of further initial conditions
Ptinitial=RHO*g*hfinitial;
Qiinitial=Cd*At*sqrt((2*(Pi+(RHO*g*hinitial)-Ptinitial)/RHO));
Qoinitial=(At/(sqrt(1-Cp)))*sqrt(2*((Pi-Po)/RHO));
QHinitial=Qoinitial-Qiinitial;
Qrfinitial=Cd*At*sqrt(2*g*(hfinitial-hinitial));
tpinitial=sqrt(2/g)*(Apc/(Cd*At))*(hpc/(2*sqrt(((Pi-Ptinitial)/(RHO*g))+(hpc/2))));
trfinitial=sqrt(2/g)*(Apc/(Cd*At))*(hpc/(2*sqrt(hfinitial-(hpc/2))));
%n is the number of loops of if statement
%Assigning an empty matrix for all values to be calculated
RFDoutput=zeros(117,9);
t=1:117;
RFDoutput(:,1)=t;
RFDoutput(1,1)=0;
RFDoutput(1,3)=hinitial;
RFDoutput(1,2)=hfinitial;
RFDoutput(1,4)=Ptinitial;
RFDoutput(1,5)=Qiinitial;
RFDoutput(1,6)=tpinitial;
RFDoutput(1,7)=0; %Qrf
RFDoutput(1,8)=QHinitial;
%Values recorded
t0=1;
t=1;
while((round(t0) <= t) && (t< round(t0+tpinitial))) & (0 < hinitial < 1.71) ; %could put as if/while statement, while tinitial=<t<tpinitial
RFDoutput(t+1,1)=t; %Input into matrix
hdecH(t)=Qiinitial/Af; %Input into matrix
hdecpc(t)=Qiinitial/Apc;
hnewH(t)=hfinitial-hdecH(t);
RFDoutput(t+1,2)=hnewH(t);
hnewpc(t)=hinitial-hdecpc(t);
RFDoutput(t+1,3)=hnewpc(t); %Input into matrix
hinitial=hnewpc(t);
hfinitial=hnewH(t);
Pt(t)=RHO*g*hnewH(t);
RFDoutput(t+1,4)=Pt(t); %Input into matrix
Qi(t)=Cd*At*sqrt(2*((Pi+(RHO*g*hnewpc(t))-Pt(t))/RHO));
RFDoutput(t+1,5)=Qi(t); %Input into matrix
Qinitial=Qi(t);
QH(t)=Qoinitial-Qi(t);
RFDoutput(t+1,8)=QH(t);
tpintitial(t)=sqrt(2/g)*(Apc/(Cd*At))*(hpc./(2*sqrt(((Pi-Pt(t))./(RHO*g))+(hpc./2))));
z=round(tpinitial)+1;
RFDoutput(t0:z,6)=round(tpinitial);
trfinitial(t)=sqrt(2/g)*(Apc/(Cd*At))*(hpc./(2*sqrt(hfinitial-(hpc./2))));
RFDoutput(t+1,9)=trfinitial(t);
trfinitial=trfinitial(t);
t=t+1;
end
while((hinitial < 1.71)&(round(tpinitial)+round(t0)+1 < t <= round(tpinitial)+round(t0)+1+round(trfinitial))) %hover over to see matri dims. Some are matrices some are 1 value??
RFDoutput(t+1,1)=t;
hdecH(t)=Qrfinitial/Af;
hnewH(t)=hfinitial-hdecH(t);
hfinitial=hnewH(t);
RFDoutput(t+1,2)=hnewH(t);
hincpc(t)=Qrfinitial/Apc;
hnewpc(t)=hinitial+hincpc(t);
hinitial=hnewpc(t);
RFDoutput(t+1,3)=hnewpc(t);
Pt(t)=RHO*g*hfinitial;
RFDoutput(t+1,4)=Pt(t);
Qrf(t)=Cd*At*sqrt(2*g*(hfinitial-hinitial));
RFDoutput(t+1,7)=Qrf(t);
tinitial=t;
t=t+1;
t0=tpinitial+t0+trfinitial+2;
end

Answers (1)

Jayaram Theegala
Jayaram Theegala on 5 Apr 2017
If you want to repeat both/one of your "while" loops, you can use "while" or "for" commands around your existing loops.
In other words, you can modify your MATLAB script as shown below:
for i = 1:<number of times you want to repeat>
< your while loop1>
< your while loop2>
end
If you want to learn more about nested loops in MATLAB, click on the following URL: https://www.tutorialspoint.com/matlab/matlab_nested_loops.htm
I hope this helps!

Categories

Find more on Loops and Conditional Statements 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!