Professional Documents
Culture Documents
CH E 351 Assignment 2B
CH E 351 Assignment 2B
File: KBS1
functionf=KBS4(x)
%KremserBrownSoudersequation
%Liquidrateisunknowni.e.solveforx=L
K=0.8;V=10;n=10;
x0=0.8;ynp1=0;xn=0.015;%Knownvalues
m=length(x);
fori=1:m
L=x(i);
S=L/K/V;
f(i)=(x0xn)/(x0ynp1/K)((1/S)^(n+1)(1/S))...
/((1/S)^(n+1)1);
end
File: Secant
functionr=secant(Fun,x,tol,trace)
%SECANTfindtherootofafunction"Fun"usingsecantscheme
%Funthenameoftheexternalfunction
%xvectoroflength2,(initialguesses)
%tolerrorcriterion
%traceprintintermediateresults
%
%Usagesecant('flash',[0,1])
%Hereflashisthenameoftheexternalfunction.
%[0,1]istheinitialguess
%Checkinputs
ifnargin<4,trace=0;end
ifnargin<3,tol=eps;trace=0;end
if(length(x)~=2)
error('Pleaseprovidetwoinitialguesses')
end
f=feval(Fun,x);%Funisassumedtoacceptavector
fori=1:100%Setmaxlimitoniterations
x3=x(1)f(1)*(x(2)x(1))/(f(2)f(1));%Update(step2)
f3=feval(Fun,x3);%Cal.f(x3)
%Keepthelasttwovalues
x(1)=x(2);f(1)=f(2);x(2)=x3;f(2)=f3;
ifabs(f3)<tol,r=x3;return;end%Checkforconvergence
iftrace,fprintf(1,'%3i%12.5f%12.5f\n',i,x3,f3);end
end
error('Exceededmaximumnumberofiterations')
File: Bisect
functionr=bisect(Fun,x,tol,trace)
%BISECTfindtherootofafunction"Fun"usingbisectionscheme
%Funthenameoftheexternalfunction
%xvectoroflength2,(initialguesses)
%tolerrorcriterion
%traceprintintermediateresults
%
%Usagebisect('flash',[0,1])
%Hereflashisthenameoftheexternalfunction.
%[0,1]istheinitialguess
%Checkinputs
ifnargin<4,trace=0;end
ifnargin<3,tol=eps;trace=0;end
if(length(x)~=2)
error('Pleaseprovidetwoinitialguesses')
end
f=feval(Fun,x);%Funisassumedtoacceptavector
if(prod(sign(f)))>0,%Checkifrootsarebracketed
error('Nosignchangenoroots')
end;
fori=1:100%Setmaxlimitoniterations
x3=(x(1)+x(2))/2;%Updatetheguess
f3=feval(Fun,x3);%Cal.f(x3)
%Checkifx2orx1shouldbediscarded
ifsign(f(1)*f3)<0,x(2)=x3;elsex(1)=x3;end;
ifabs(f3)<tol,r=x3;return;end%Checkforconvergence
iftrace,fprintf(1,'%3i%12.5f%12.5f\n',i,x3,f3);end
end
error('Exceededmaximumnubmerofiterations')
28.19668
-0.73829
28.18294
28.16922
4.49633
27.71803
27.28417
7.78169
26.37570
25.55444
13.31499
23.50875
22.14791
18.87720
20.24705
20.00221
19.96767
19.96875
19.96874
-0.00158
3.32385
-0.00158
-0.00158
0.08114
-0.00154
-0.00151
0.03093
-0.00143
-0.00134
0.00667
-0.00103
-0.00073
0.00052
-0.00011
-0.00001
0.00000
-0.00000
-0.00000
ans =
19.9687
>> bisect('KBS1',[10,30],eps,1)
1
20.00000
-0.00001
2
15.00000
0.00392
3
17.50000
0.00140
4
18.75000
0.00059
5
19.37500
0.00027
6
19.68750
0.00012
7
19.84375
0.00005
8
19.92188
0.00002
9
19.96094
0.00000
10
19.98047
-0.00000
11
19.97070
-0.00000
12
19.96582
0.00000
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
19.96826
19.96948
19.96887
19.96857
19.96872
19.96880
19.96876
19.96874
19.96875
19.96874
19.96875
19.96874
19.96874
19.96874
19.96874
19.96874
19.96874
19.96874
19.96874
19.96874
19.96874
19.96874
19.96874
19.96874
19.96874
19.96874
19.96874
19.96874
19.96874
19.96874
19.96874
19.96874
0.00000
-0.00000
-0.00000
0.00000
0.00000
-0.00000
-0.00000
0.00000
-0.00000
0.00000
-0.00000
-0.00000
0.00000
-0.00000
0.00000
-0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
-0.00000
0.00000
0.00000
-0.00000
0.00000
-0.00000
0.00000
0.00000
-0.00000
0.00000
-0.00000
ans =
19.9687
Question:2
File: KBS2
functionf=KBS2(x)
%KremserBrownSoudersequation
%Amountofgasesisunknowni.e.solveforx=V
K=0.8;L=10;n=10;
x0=0.8;ynp1=0;xn=0.01488;%Knownvalues
m=length(x);
fori=1:m
V=x(i);
S=L/K/V;
f(i)=(x0xn)/(x0ynp1/K)((1/S)^(n+1)(1/S))...
/((1/S)^(n+1)1);
end
>> bisect('KBS2',[5,20],eps,1)
1
12.50000
NaN
2
16.25000
-0.00087
3
14.37500
0.02247
4
15.31250
0.00844
5
15.78125
0.00330
6
16.01562
0.00110
7
16.13281
0.00009
8
16.19141
-0.00040
9
16.16211
-0.00016
10
16.14746
-0.00003
11
16.14014
0.00003
12
16.14380
-0.00000
13
16.14197
0.00001
14
16.14288
0.00000
15
16.14334
0.00000
16
16.14357
-0.00000
17
16.14346
0.00000
18
16.14351
-0.00000
19
16.14348
-0.00000
20
16.14347
0.00000
21
16.14348
0.00000
22
16.14348
-0.00000
23
16.14348
0.00000
24
16.14348
0.00000
25
16.14348
-0.00000
26
16.14348
-0.00000
27
16.14348
-0.00000
28
16.14348
0.00000
29
16.14348
0.00000
30
16.14348
0.00000
31
16.14348
-0.00000
32
16.14348
-0.00000
33
16.14348
0.00000
34
16.14348
0.00000
35
36
37
38
39
40
41
42
43
44
45
46
47
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
0.00000
-0.00000
0.00000
0.00000
0.00000
-0.00000
0.00000
-0.00000
-0.00000
-0.00000
-0.00000
0.00000
-0.00000
ans =
16.1435
>> bisect('KBS2',[5,30],eps,1)
1
17.50000
-0.00847
2
11.25000
0.12713
3
14.37500
0.02247
4
15.93750
0.00181
5
16.71875
-0.00424
6
16.32812
-0.00148
7
16.13281
0.00009
8
16.23047
-0.00071
9
16.18164
-0.00032
10
16.15723
-0.00011
11
16.14502
-0.00001
12
16.13892
0.00004
13
16.14197
0.00001
14
16.14349
-0.00000
15
16.14273
0.00001
16
16.14311
0.00000
17
16.14330
0.00000
18
16.14340
0.00000
19
16.14345
0.00000
20
16.14347
0.00000
21
16.14348
-0.00000
22
16.14348
0.00000
23
16.14348
0.00000
24
16.14348
-0.00000
25
16.14348
0.00000
26
16.14348
-0.00000
27
16.14348
0.00000
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
16.14348
-0.00000
0.00000
-0.00000
0.00000
-0.00000
0.00000
0.00000
0.00000
0.00000
-0.00000
0.00000
-0.00000
0.00000
0.00000
-0.00000
-0.00000
-0.00000
-0.00000
-0.00000
0.00000
ans =
16.1435
>> secant('KBS2',[5,20],eps,1)
1
19.61859
-0.01457
2
10.34093
0.17862
3
18.91885
-0.01316
4
18.33008
-0.01158
5
14.03145
0.02916
6
17.10824
-0.00654
7
16.54454
-0.00307
8
16.04528
0.00084
9
16.15262
-0.00008
10
16.14368
-0.00000
11
16.14348
0.00000
12
16.14348
-0.00000
ans = 16.1435
>> secant('KBS2',[5,30],eps,1)
1
29.22875
-0.01848
2 -538.10660
-0.01860
3 89549.18423
-0.01860
4
Inf
NaN
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN
97
98
99
100
NaN
NaN
NaN
NaN
NaN
NaN
NaN
NaN