Info

此问题已关闭。 请重新打开它进行编辑或回答。

For loop terminates randomly after 70 lines?

1 次查看(过去 30 天)
ranbo95
ranbo95 2017-3-18
关闭: MATLAB Answer Bot 2021-8-20
Apologies for the long code in advance. 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 suddenly terminates at line 70 with the message 'Matrix dimensions must agree'. Seems quite random given that it runs the code perfectly for quite some time? Also, any ideas how to get it to continue this loop of pump and refill? Thanks!
%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(10000,9)
t=1:10000
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
for t=round(t0):round(t0+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)=sqrt(2/g)*(Apc/(Cd*At))*(hpc./(2*sqrt(hfinitial-(hpc./2))))
RFDoutput(t+1,9)=trfinitial(t)
t=t+1
end
for t=round(tpinitial+t0+1):round(tpinitial+t0+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)
tinitial=t
t=t+1
t0=tpinitial+t0+trfinitial+2
end
end
  1 个评论
John D'Errico
John D'Errico 2017-3-18
NO. That is NOT random. What you describe is arbitrary. Somewhere, you have made an error.
I would suggest that first, you learn to use semi-colons at the end of your lines. This code will dump god-awful amounts of useless crap to the command window without them.
Next, you should learn to use the debugger. Just put this code inside a function, then tell the debugger to stop when it sees an error.
dbstop if error
Finally, I would recommend that you read the warning messages that come out.

回答(1 个)

Image Analyst
Image Analyst 2017-3-18
Why are you setting t=t+1 inside of the for loop when t is your for loop iterator? That's a major no-no. Delete those unnecessary lines.
Next trfinitial is an array, not a single scalar number. So round(tpinitial+t0+1+trfinitial) will also be an array - not what you want as the final ending value in a for loop. Not sure what trfinitial you want, so I'll leave it up to you to fix it.

此问题已关闭。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by