11/22/2006

10.6 二個桶子間之水位問題

不同水位之兩個桶子,其下用管路相通。當其開關打開後,兩桶之水位隨時間而變化,水位高著向水位低者流動,其流動速率與兩桶水位差成正比。設每公分之水位差之流動量為C[cm3],兩桶之斷面直徑分別為[D1、D2]cm,水位高度分別為[U1、U]cm,則在管路之流動量V為:


V=C*H*(U1-U2)
 U1=U1-V/A1
U2=U2+V/A2

式中,H為每次計算時之時段,A1A2分別為水桶之斷面積,U1、U2則隨著時間而變化。

程式設計


下面之程式係以上式為計算基礎,其輸入分三種方式,即使用預設資料、使用參數輸入與逐項輸入之方式:

預設資料:pail2pail
使用參數輸入:pail2pail(U,D,C,Delt)
逐項輸入:pail2pail(1)

程式內容



function pail2pail(U,D,C,Delt)
% pail2pail.m
% program to calculate waterflows between two pails.
% U1,U2:water levels for each pails, cm
% D1,D2:diameters of both pails, cm
% T:time elapsed through the experiment.
% C:flow coefficent, in cm^3/cm of water level difference.
% designed by D.S. Fon, BIME, NTU. Date:Nov. 26,2006
if nargin==0,
U1=20;U2=10;D1=5;D2=5;C=1;delt=1;
elseif U==1,
U1=inopt1('A. Input water level in Pail No.1,cm',[50 0 500]);
U2=inopt1('B. Input water level in Pail No.2,cm',[0 0 500]);
D1=inopt1('C. Input diameter of pail 1,cm',[5 0 500]);
D2=inopt1('D. Input diameter of pail 2,cm',[5 0 500]);
C=inopt1('E. Input flow coefficient,cm^3/cm',[1 0 10]);
delt=inopt1('F. Input time interval,seconds',[1 0.1 10]);
else
U1=U(1);U2=U(2);D1=D(1);D2=D(2);
end
A1=pi*D1^2/4;A2=pi*D2^2/4;
T=0;i=0;
clf;
disp(' Time Pail 1 Level Pail 2 Level');
disp('======== ============ ============');
while abs(U2-U1)>delt,
fprintf(1,'%8.3f %10.4f %10.4f\n',T,U1,U2);
T=T+delt;i=i+1;
vel=C*delt*(U1-U2);
U1=U1-vel/A1;U2=U2+vel/A2;
f1(i)=U1;f2(i)=U2;Tx(i)=T;
line(Tx(i),f1(i),'Marker','o');
line(Tx(i),f2(i),'Marker','s');
end
j=fix(i/3);
line(Tx,f1,'color','red');
line(Tx,f2,'color','blue');
line([0 Tx(end)],[(f1(1)+f2(1))/2 (f1(1)+f2(1))/2]);
text(Tx(j),f1(j),'\leftarrow---U1');
text(Tx(j),f2(j),'\leftarrow---U2');
xlabel('Time, seconds');
ylabel('Water levels, U1 & U2');

function [kk]=inopt1(A,s)
while 1
mtxt=['(',num2str(s(2)),'-',num2str(s(3)),')[',...
num2str(s(1)),']=='];
kk=input([A,mtxt]);
if isempty(kk), kk=s(1);end
if kk>=s(2)|kk<=s(3), return;end
disp('*****Exceeding the limit, please reinput*****')
end

執行範例



>> pail2pail(1)
A. Input water level in Pail No.1,cm(0-500)[50]==>
B. Input water level in Pail No.2,cm(0-500)[0]==>
C. Input diameter of pail 1,cm(0-500)[5]==>
D. Input diameter of pail 2,cm(0-500)[5]==>
E. Input flow coefficient,cm^3/cm(0-10)[1]==>
F. Input time interval,seconds(0.1-10)[1]==>
Time Pail 1 Level Pail 2 Level
======== ============ ============
0.000 50.0000 0.0000
1.000 47.4535 2.5465
2.000 45.1664 4.8336
3.000 43.1123 6.8877
4.000 41.2674 8.7326
5.000 39.6104 10.3896
6.000 38.1222 11.8778
7.000 36.7856 13.2144
8.000 35.5851 14.4149
9.000 34.5069 15.4931
10.000 33.5386 16.4614
11.000 32.6688 17.3312
12.000 31.8877 18.1123
13.000 31.1861 18.8139
14.000 30.5560 19.4440
15.000 29.9901 20.0099
16.000 29.4818 20.5182
17.000 29.0253 20.9747
18.000 28.6153 21.3847
19.000 28.2470 21.7530
20.000 27.9163 22.0837
21.000 27.6192 22.3808
22.000 27.3524 22.6476
23.000 27.1128 22.8872
24.000 26.8976 23.1024
25.000 26.7043 23.2957
26.000 26.5307 23.4693
27.000 26.3748 23.6252
28.000 26.2348 23.7652
29.000 26.1090 23.8910
30.000 25.9960 24.0040
31.000 25.8946 24.1054
32.000 25.8035 24.1965
33.000 25.7216 24.2784
34.000 25.6481 24.3519
35.000 25.5821 24.4179
36.000 25.5228 24.4772