How to get my while loop to skip into the next loop?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Apologies for the long code in advance. I'm unsure how to get my first while loop (within the initial while loop) to skip onto the next one. To give you a back ground, this is to represent pumping and refill of fluid transfer, so heigh (h) changes with time (t). I want it to pump for the time calculated (tp), and then once it reaches this, skip to refilling (for time trf). This should be done until the height in a feed tank = 0 (larger while loop). When I run it, it does not skip to the second while loop. Hence, this pump then refill cycle should continue to loop around a number of times. Any advice how I can get this? Code is below. Thanks in advance.
%Input initial conditions
Pi=300000;
g=9.81;
RHO=1400;
Po=101325
Dt=0.05;
At=pi*(Dt.^2)/4
Df=6;
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;
%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))))
%Assigning an empty matrix for all values to be calculated
RFDoutput=zeros(10000,9)
RFDoutput(:,1)=1:10000
t=RFDoutput(t,1)
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
tinitial=1
t=1
while hfinitial>0
while round(tinitial) <= RFDoutput(t,1) < round(tinitial+tpinitial) %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*hfinitial
RFDoutput(t+1,4)=Pt(t) %Input into matrix
Qi(t)=Cd*At*sqrt(2*((Pi+(RHO*g*hinitial)-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))))
RFDoutput(0:tpinitial+1,6)=tpinitial
trfinitial=t+tpinitial
t=t+1
end
while round(tpinitial+1) < RFDoutput(t,1) <= round(tpinitial+1+trfinitial)
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)
trfinitial(t)=sqrt(2/g)*(Apc/(Cd*At))*(hpc./(2*sqrt(hfinitial-(hpc./2))))
RFDoutput(t+1,9)=trfinitial(t)
tinitial=t
t=t+1
end
end
0 comentarios
Respuestas (1)
Geoff Hayes
el 17 de Mzo. de 2017
ranbo95 - your condition in your while loop seems suspect
while round(tinitial) <= RFDoutput(t,1) < round(tinitial+tpinitial)
I don't think that this is a valid statement and that you should be using
while round(tinitial) <= RFDoutput(t,1) && RFDoutput(t,1) < round(tinitial+tpinitial)
instead. Consider the following
4 < 3 < 2
what should the answer be? 4 is clearly not greater than 3 so this is false (0). But 0 is less than 2 so the above statement is true (1).
Contrast this with
4 < 3 && 3 < 2
which returns false (as expected).
Please clarify what your conditions should be. The same is true for the other inner while loop.
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!