How do I get two while loops to keep repeating?
1 view (last 30 days)
Show older comments
I've got my first loop to (appear) to do what I want - I want it to calculate some flowrates (Q) until the height of the pumping chamber (hpc) = 0, then to enter my second nested while loop to allow refilling (rf) after pumping time (tp) is over. However, in this second nested while loop (for refilling) it does not stop at a height (hinitial) of 1.71 like I want it to! Has anyone got any guidance of how I can get it to do this? I want it to basically pump, refill, pump, refill etc. as a continuous cycle, with hinitial, hfinitial, tp, trf, Pt etc. all changing with each loop. Code is as follows; 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.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(1000,9);
t=1:1000;
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 hfinitial > 0
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((0 <= 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
end
0 Comments
Answers (1)
Sachin Kumar
on 28 Mar 2017
Instead of this statement : (0 <= hinitial < 1.71) %these kind of statements not supported in MATLAB. use ((0<=hinitial) && (hinitial<1.71)).
0 Comments
See Also
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!