Está en la página 1de 39

1

P3
P300

1. Matlab

1) Matlab
2) Matlab

1 Desktop Command Window


DesktopDesktop LayoutDefault
2) FileSet PathAdd Folder
Move to bottom
2. Matlab
1) help ginput ginput 50
hand_x hand_y
2) hand_x hand_y Hand.mat
3) clear load Hand.mat
plot

1) help ginput ginput [hand_x, hand_y]=ginput(50)

2) save hand hand_x hand_y


3) clearload handplot(hand_x,hand_y)
3.
1.84

p =

0.03L V

d 1.24 1000

L mV m/mind mm
L3000md45mmV1600m/min p
L=3000m

d=45mm

V=1600m/min

deltP=
6


L=3000;d=45;V=1600;
deltP=0.03*(V/1000)^1.84/d^1.24
disp('L=3000m d=45mm V=1600m/min')
disp('')
fprintf('\tdeltP=%.6f\n',deltP)
deltP=0.000635
4. Q m3/s

Q=

23/ 2 Dc5/ 2 g ( 0.5sin(2 ))3/ 2


8 sin (1 cos )5/ 2

g=9.8m/s2 Dc
d
Dc = (1 cos )
2
1) Script d2m60Q
2) Q d Q
d3m50 Q

1)
Theta=60*pi/180;
d=2;g=9.8;
Dc=d*(1-cos(Theta))/2;
Q=2^(3/2)*Dc^(5/2)*sqrt(g)*(Theta0.5*sin(2*Theta))^(3/2)/(8*sqrt(sin(Theta))*(1-cos(Theta))^(5/2))

Q=

0.5725

2)
function Q=QCal(d,Theta)
Theta=Theta*pi/180;
g=9.8;
Dc=d*(1-cos(Theta))/2;
Q=2^(3/2)*Dc^(5/2)*sqrt(g)*(Theta0.5*sin(2*Theta))^(3/2)/(8*sqrt(sin(Theta))*(1-cos(Theta))^(5/2));

>> Q1=QCal(3,50)
Q1 =

0.8171

5. plot sin t e t cos t t [0,3 ]

t=[0:0.1*pi:3*pi];
y1=sin(t);
y2=exp(-t).*cos(t);
plot(t,y1,'-r',t,y2,'.b')
title('The plots of sin(t) and exp(-t)*cos(t)')
legend('sin(t)','exp(-t)*cos(t)')
xlabel('t'),ylabel('function value')

6.
clf;
t=6*pi*(0:100)/100;y=1-exp(-0.3*t).*cos(0.7*t);

tt=t(find(abs(y-1)>0.05));

%0.05

ts=max(tt);

%tt

plot(t,y,'r-','LineWidth',3)
axis([-inf,6*pi,0.6,inf])
set(gca,'Xtick',[2*pi,4*pi,6*pi],'Ytick',[0.95,1,1.05,max(y)])
grid on
title('\it y = 1 - e^{ -\alphat}cos{\omegat}')
text(13.5,1.2,'\fontsize{12}{\alpha}=0.3')
text(13.5,1.1,'\fontsize{12}{\omega}=0.7')
hold on;
plot(ts,0.95,'bo','MarkerSize',10);
hold off
cell_string{1}='\fontsize{12}\uparrow';
cell_string{2}='\fontsize{16} \fontname{ {}';
cell_string{3}='\fontsize{6}

';

cell_string{4}=['\fontsize{14}\rmt_{s} = ' num2str(ts)];


text(ts,0.85,cell_string)
xlabel('\fontsize{14} \bft \rightarrow')
ylabel('\fontsize{14} \bfy \rightarrow')

y = 1 - e - t c os t
1.2843

=0.3
=0.7
1.05
1

0.95

t s = 9.6133

6.2832

12.5664

18.8496

clf;
t=6*pi*(0:100)/100;y=1-exp(-0.3*t).*cos(0.7*t);%
% 0.05

tt=t(find(abs(y-1)>0.05));

% tt

ts=max(tt);

plot(t,y,'r-','LineWidth',3) t y 3
axis([-inf,6*pi,0.6,inf])

[-inf 6*pi][0.6 inf]

set(gca,'Xtick',[2*pi,4*pi,6*pi],'Ytick',[0.95,1,1.05,max(y)]) x y
grid on

title('\it y = 1 - e^{ -\alphat}cos{\omegat}')

text(13.5,1.2,'\fontsize{12}{\alpha}=0.3')

text(13.5,1.1,'\fontsize{12}{\omega}=0.7')
hold on;

plot(ts,0.95,'bo','MarkerSize',10);

[ts,0.95] 10

hold off

cell_string{1}='\fontsize{12}\uparrow';

cell_string{2}='\fontsize{16} \fontname{}';
cell_string{3}='\fontsize{6}

';

cell_string{4}=['\fontsize{14}\rmt_{s} = ' num2str(ts)];


text(ts,0.85,cell_string)

[ts,0.85]

xlabel('\fontsize{14} \bft \rightarrow')

x 14 t

ylabel('\fontsize{14} \bfy \rightarrow')

y 14 y

2 P6
P611
1.
roots
>> c = [1 -2 -4 -7];
>> r=roots(c)
r=
3.6320

-0.8160 + 1.1232i
-0.8160 - 1.1232i
fzero
>> x = fzero(@(x)x^3-2*x^2-4*x-7,0)
x=
3.6320
roots
fzero
2.
10

-5

-10

-15
-10

-8

-6

-4

-2

10

>> fplot(@(x) sin(x)+x-1, [-10 10], 0.01)


>> fzero(@(x)sin(x)+x-1, 1)
ans =
0.5110

3.
>> fplot(@(x) sin(x)-4*x-exp(x), [-50 10], 0.01)
0.5

x 10

-0.5

-1

-1.5

-2

-2.5
-50

-40

-30

-20

-10

>> fzero(@(x) sin(x)-4*x-exp(x), 10)

10

ans =
-0.2569
4.
function Chap2xiti4
x0 = [0 0];
x = fsolve(@fun, x0)
function y = fun(x)
y(1) = x(1) - 0.7*sin(x(1)) - 0.2*cos(x(2));
y(2) = x(2) - 0.7*cos(x(1)) - 0.2*sin(x(2));

>> chap2xiti4
Optimization terminated: first-order optimality is less than options.TolFun.
x=
0.4442

0.7715

5.
function Chap2xiti5
x0 = [0 0];
x = fsolve(@fun, x0)
function y = fun(x)
y(1) = x(1)^2 - x(2) - 1;
y(2) = (x(1)-1)^2 + (x(2)-1)^2 - 0.25;

>> chap2ti5
Optimization terminated: first-order optimality is less than options.TolFun.
x=
1.2523

0.5683

6.
>> L1=~(A>0)
L1 =
1
>> L2=~A>0
L2 =

0
0
0
0
0
7.
function Chap2xiti7
element_for = fun_for
element_while = fun_while

>> L3=~A
L3 =
0

>> L4=A>-2&A<1
L4 =
0

>> L5=A==B&L1
L5 =

function result = fun_for()


a(1) = 1;
a(2) = 1;
for k=1:1:10000
a(k+2) = a(k) + a(k+1);
if a(k+2) > 10000
result = a(k+2);
return
end
end
function result = fun_while()
k = 1;
a(1) = 1;
a(2) = 1;
a(3) = a(1) + a(2);
while a(k+2)<=10000
k = k + 1;
a(k+2) = a(k) + a(k+1);
end
result = a(k+2);


element_for =
10946
element_while =
10946
8.
function Chap2xiti8
x0 = [0 0];
x = fsolve(@fun, x0)
function y = fun(x)
y(1) = x(1) + x(2) -36;
y(2) = 2*x(1) + 4*x(2) -100;
>> chap3xiti8
Optimization terminated: first-order optimality is less than options.TolFun.
x=
22.0000

14.0000

9.
function [y, z] = chap2xiti9
x(1) = 0;
N = 201;
for m = 1:5:N
l = int8(m/5+1);
y(l,1) = m-1;
y(l,2) = x(m);
for n = m:m+4
x(n+1) = x(n)^2+0.25;
end
end
n = 1;
while n <= N
x(n+1) = x(n)^2+0.25;
if mod(n-1,5)==0
l=int8(n/5+1);
z(l,1) = n-1;
z(l,2) = x(n);
end
n = n + 1;
end
plot(y(:,1),y(:,2))

xlabel('n')
ylabel('x_n')

0.5
0.45
0.4
0.35

0.25

0.3

0.2
0.15
0.1
0.05
0

20

40

60

80

100
n

120

140

160

180

200

roots xn
>> roots([1 -1 0.25])
ans =
0.5000
0.5000
xn 0.5
fzero
>> fzero(@(x) x^2-x+0.25,0.4)
Exiting fzero: aborting search for an interval containing a sign change
because NaN or Inf function value encountered during search.
(Function value at -1.37296e+154 is Inf.)
Check function or try again with a different starting value.
ans =
NaN
10.
function Chap2xiti10
x0 = [0];
x = fzero(@fun, x0)
function y = fun(x)
y = (0.3+x)*(0.1+x)-148.4*(1-x)*(1.8-x);
>> chap2xiti10
x=
0.9884

11.
function Chap2xiti11
x0 = [0];
x = fzero(@fun, x0)
function y = fun(x)
T0 = 450;
A = 250;
B = 10000;
C = exp(20);
tau = 0.25;
T = T0 + A*x;
y = C*(1-x)^2*tau*exp(-B/T) - x;
>> chap2xiti11
x=
0.0408
12
function Chap2xiti11
D0 = [1];
D = fsolve(@fun, D0)
function y = fun(D)
rou = 964;
G = 6.67;
L = 10;
e = 5E-6;
dp = 15E3;
k =1.8;
n =0.64;
K = 1.48;
u = 4*G/rou/(pi*D*D);
Re = D^n*u^(2-n)*rou/8^(n-1)/K;
f = 16/Re;
Le = k*D/4/f;
y = D - (2*f*(L+Le)/rou/dp*(4*G/pi)^2)^0.2;

>> chap2xiti12
Optimization terminated: first-order optimality is less than options.TolFun.
D=
0.0865

P89

1.
1) magic(5)
2) eye(4)
3) ones(4,3)
4) zeros(3,4)
5)rand(2,4)
2.

3.

rand('state',0)
x=rand(1,5)
a1=x(3)
a2=x([1 2 5])
a3=x(1:3)
a4=x(3:end)
a5=x(3:-1:1)
a6=x(find(x>0.5))
x([1 4])=[0 0];x

4.

A=[2 6 5 22 2; 1 6 3 8 94; 13 4 5 7 21];


a1=A(7);
fprintf('\nThe 7th element is %2d\n',a1)
a2=max(max(abs(A)));
fprintf('\nThe maximum element of the matrix is %2d\n',a2)
[R V]=find(A==a2);
fprintf('\nThe maximun element locates at %2dth Row and %2dth colum\n',R,V)
L=abs(A)>10;
X=A(L)
5.
1) >> A=[1 3 5; 2 4 6];
>> B=[8 1 6; 3 5 7; 4 9 2];
>> C=[16 2 3 13; 5 11 10 8; 9 7 6 12; 4 14 15 1];
2) >>diag(B)

ans =
8
5
2
diag(C)
ans =
16
11
6
1
3) >> triu(C)
ans =
16
2
0
11
0
0
0
0
4) >> tril(C)

3
10
6
0

13
8
12
1

0
0
6
15

0
0
0
1

10

10
6

8
12

ans =
16
5
9
4

0
11
7
14

6.
1)
>> C(2,:)
ans =
5
11
2)
>> C(:,4)
ans =
13
8
12
1
3) >> C(2:3,:)
ans =
5
11
9
7
4)

>> A(:)
ans =
1
2
3
4
5
6
5)
>> A(2:5)
ans =
2
3
4
5
7.
>> A=[1 1 0;1 0 1; 0 1 1];
>> B=[1 3 5;2 4 6];
>> rank(A)
ans =
3
>> rank(B)
ans =
2
>> pinv(A)
ans =
0.5000
0.5000 -0.5000
0.5000 -0.5000
0.5000
-0.5000
0.5000
0.5000
>> pinv(B)
ans =
-1.3333
1.0833
-0.3333
0.3333
0.6667 -0.4167
8.
1)
>> A=[1 1 0;1 0 1; 0 1 1];
>> B=[1 2 3;4 5 6; 7 8 0];
>> [V,D] = eig(A)
V=
0.4082 -0.7071
-0.8165
0.0000
0.4082
0.7071
D=
-1.0000
0
0
1.0000
0
0
>> [V,D] = eig(B)

0.5774
0.5774
0.5774
0
0
2.0000

V=
-0.2998 -0.7471 -0.2763
-0.7075
0.6582 -0.3884
-0.6400 -0.0931
0.8791
D=
12.1229
0
0
0 -0.3884
0
0
0 -5.7345
2)
>> [U S V]=svd(A)
U=
-0.5774
-0.5774
-0.5774

0.4082
0.4082
-0.8165

0.7071
-0.7071
0

S=
2.0000
0
0

0
1.0000
0

0
0
1.0000

V=
-0.5774
-0.5774
-0.5774

0.8165
-0.4082
-0.4082

0
0.7071
-0.7071

>> [U S V]=svd(B)
U=
-0.2304
-0.6073
-0.7604

-0.3961
-0.6552
0.6433

-0.8889
0.4493
-0.0896

S=
13.2015
0
0

0
5.4388
0

0
0
0.3760

V=
-0.6046
-0.7257
-0.3284

0.2733
0.1982
-0.9413

0.7482
-0.6589
0.0784

3)
>> [L U P]=lu(A)
L=
1
1
0

0
1
-1

0
0
1

1
0
0

1
-1
0

0
1
2

1
0
0

0
1
0

0
0
1

U=

P=

>> [Q R]=qr(A)
Q=
-0.7071
-0.7071
0

0.4082
-0.4082
0.8165

-0.5774
0.5774
0.5774

-1.4142
0
0

-0.7071 -0.7071
1.2247
0.4082
0
1.1547

R=

9.
>> A=[0.23 0.57 0.20;0 0.93 0.07;0.90 0 0.10];
>> b = [0.27 0.60 0.13];

>> w=A'\b';
>> W = 1000*w
W=
416.8937
389.6458
193.4605
10.
>> A=[12/31 5/31 14/31;24/45 7/45 14/45;72/93 7/93 14/93];
>> b=[0.615 0.124 0.261];
>> w = A'\b'
w=
0.1008
0.4990
0.4002
11.
C6H5NO2
72+5+14+32=123
C6H7N
72+7+14 = 93
C3H7NO 36+7+14+16=73
C2H6O
24+6+16 = 46
>> A=[72 5 14 32;72 7 14 0;36 7 14 16;24 6 0 16];
>> A(1,:)=A(1,:)/123;
>> A(2,:)=A(2,:)/93;
>> A(3,:)=A(3,:)/73;
>> A(4,:)=A(4,:)/46;
>> b=[0.5778 0.0792 0.1123 0.2307];
>> w = A'\b'
w=
0.3868
0.1511
0.2374
0.2249
12.
>> A=[

1.5020
0.0261
0.0342
0.340
>> b=[0.1013
0.09943
0.2194
0.03396];
>> w = A\b
w=
0.0641
0.0825

0
0.0514 0.0408
0
1.1516 0.0820
2.532
0.0355 0.2933
0
0.0684 0.3470];

0.0836
0.0186
13.

1)
0.07 D1 + 0.18B1 + 0.15 D 2 + 0.24 B 2 = 0.15 70
0.04 D1 + 0.24 B1 + 0.10 D 2 + 0.65 B 2 = 0.25 70

0.054 D1 + 0.42 B1 + 0.54 D 2 + 0.10 B2 = 0.40 70


0.035 D1 + 0.16 B1 + 0.21D 2 + 0.01B 2 = 0.20 70

>> A=[0.07 0.18 0.15 0.24;


0.04 0.24 0.10 0.65;
0.54 0.42 0.54 0.10;
0.35 0.16 0.21 0.01];
>> B=[0.15*70; 0.25*70; 0.40*70; 0.20*70];
>> x=A\B
x =[ 26.250017.5000 8.7500 17.5000]
14.

NH3 O2

NO H2O N2 NO2

4 5 4 6 0
4 3 0 6 2

4 0 6 6 5
A=
0 1 2 0 0
0
1 2 0 1

0 2 0 0 1

0
0
0

2
0

2
>> brank(A) b3
15.

function Chap3demo7
A=[-4 1 0 0 0 0;1 -4 1 0 0 0; 0 1 -4 1 0 0;0 0 1 -4 1 0; 0 0 0 1 -4 1;0 0 0 0 1 -4];
D=diag(diag(A)); U=-triu(A,1); L=-tril(A,-1);
x0=0*ones(1,6)';
b=[-27 -15 -15 -15 -15 -15]';
%Jacobian Iteration
BJ=D\(L+U); gJ=D\b; y=BJ*x0+gJ;
n=1;
while norm(y-x0)>=1e-6
x0=y;

y=BJ*x0+gJ;
n=n+1;
end
disp('The solution of Jacobian iteration are')
fprintf('\nn=%3d\n',n)
disp('y='),disp(y')
%Gauss-Seidel
BG=(D-L)\U;gG=(D-L)\b;y=BG*x0+gG;
n=1;
while norm(y-x0)>=1e-8
x0=y;
y=BG*x0+gG;
n=n+1;
end
disp('The solution of Gauss-Seidel iteration are')
fprintf('\nn=%3d\n',n)
disp('y='),disp(y')
%SOR
omiga=1.08;BS=(D-omiga*L)\((1-omiga)*D+omiga*U);gS=omiga*((D-omiga*L)\b);
y=BS*x0+gG;
n=1;
while norm(y-x0)>=1e-8
x0=y;
y=BS*x0+gS;
n=n+1;
end
disp('The solution of SOR iteration are')
fprintf('\nn=%3d\n',n)
disp('y='),disp(y')

The solution of Jacobian iteration are


n= 22
y= 8.7032

7.8128

7.5479

7.3789

6.9677

5.4919

7.3789

6.9677

5.4919

7.3789

6.9677

5.4919

The solution of Gauss-Seidel iteration are


n=5
y=8.7032

7.8128

7.5479

The solution of SOR iteration are


n= 11
y= 8.7032

7.8128

7.5479

09
P1
P109

1. x=0,1,2,3,,10ysinx
xi0,0.15,0.30,0.45,,10 yi x = 0,2,4,,10, y=sinx,

% xiti0401
x=0:10;
y=sin(x);
xi=0:0.15:10;
y1=interp1(x,y,xi,'linear'),
y2=interp1(x,y,xi,'nearest'),
y3=interp1(x,y,xi,'cubic'),
y4=interp1(x,y,xi,'spline')

2.
2. polyfit
x

0.5

1.0

1.5

2.0

2.5

3.0

1.75

2.45

3.81

4.80

8.00

8.60

% xiti0402
x=0.5:0.5:3;
y=[1.75 2.45 3.81 4.80 8.00 8.60];
p1=polyfit(x,y,1);
p2=polyfit(x,y,2);
p3=polyfit(x,y,3);
y1=polyval(p1,x)
y2=polyval(p2,x)
y3=polyval(p3,x)

2.
2. 10.13 10 3 kN / m 2 372K
K
T/K

p (10 3 kN / m 2 )

341

9.7981

0.0848

13.324

0.0897

9.0078

0.0762

13.355

0.0807

360

(W /( m K )

T/K

379
413

p (10 3 kN / m 2 )

(W /( m K )

9.7981

0.0696

14.277

0.0753

9.6563

00611.

12.463

0.0651

:8

(Ti , P2 i 1 ) , (Ti , P2 i ) , i = 1, 2, 3, 4 (T * , P * )
K T K P
T = Ti ( P2 i 1 , K 2 i 1 ) , ( P2 i , K 2 i ) K P

K (Ti , P * ) K 2 i 1

P * P2 i
P * P2 i 1
+ K 2i
, i = 1, 2, 3, 4
P2 i 1 P2 i
P2 i P2 i 1

K (Ti , P * ) i = 1, 2, 3, 4
4 T L a g ra n g e

K (T * , P * ) K (T 1 , P * )

(T
( T1

T 2 ) (T T 3 ) (T T 4 )
T 2 ) (T 1 T 3 )(T 1 T 4 )

( T T1 ) (T T 3 ) (T T 4 )
( T 2 T 1 ) (T 2 T 3 ) (T 2 T 4 )
T T
T T
T T
(T 3 , P * ) ((T T 1 ))((T T2 ))((T T4 ))
3
1
3
2
3
4
T

T
T

T
T

T
(T 4 , P * ) ((T T 1 ))((T T2 ))((T T3 ))
4
1
4
2
3
3

+ K (T 2 , P * )
+K
+K

K (T * , P *

% xiti0403
P1=[9.7981 13.324];K1=[0.0848,0.0897];
k1=interp1(P1,K1,10.13);
P2=[9.0078 13.355];K2=[0.0726,0.0807];
k2=interp1(P2,K2,10.13);
P3=[9.7981 14.277];K3=[0.0696,0.0753];
k3=interp1(P3,K3,10.13);
P4=[9.6563 12.463];K4=[0.0611,0.0651];
k4=interp1(P4,K4,10.13);
T=[341 360 379 413];
k=[k1,k2,k3,k4];
K=lagrange(T,k,372)

K=
0.0713

2 K T p
T = [341 341 360 360 379 379 413 413];
p = [9.7981 13.324 9.0078 13.355 9.7981 14.277 9.6563 12.463];
K = [0.0848 0.0897 0.0762 0.0807 0.0696 0.0753 0.0611 0.0651];
X = [ones(8,1) T' p'];
[b,BINT,R,RINT] = regress(K',X);
% b = X\lambda';

K0 = [1 372 10.13]*b

K0 =
0.0742

C
4. t t
t / min

Ct /( g / l )

0.2

0.6

1.0

2.0

5.0

10.0

5.19

3.77

2.30

1.57

0.8

0.25

0.094

C
t = 0.5 min t = 6 min t
% xiti0404
t=[0 0.2 0.6 1.0 2.0 5.0 10.0];
Ct=[5.19 3.77 2.30 1.57 0.8 0.25 0.094];
Ct1=spline(t,Ct,0.5),
Ct2=spline(t,Ct,6)

Ct1 =
2.5576
Ct2 =
0.2598

5.
5. Nu Re
Re

3520

6050

8400

9970

12520

14810

15900

18080

Nu

11.6

18.1

23.5

26.9

32.2

36.8

39.0

43.2

1 Nu Re
b

2 Nu = a Re a b
1

50
45
40
35
30
25
20
15
10
5
0
0

5000

10000

45

15000

20000

y = ax b
2

y = ax b

Y = ln y , X = ln x

Y = ln a + bX

NU = ln Nu, RE = ln Re

NU = ln a + bRE

3
% xiti0405
Re=[3520 6050 8400 9970 12520 14810 15900 18080];
Nu=[11.6 18.1 23.5 26.9 32.2 36.8 39.0 43.2];
RE=log(Re); NU=log(Nu) ;
p=polyfit(RE,NU,1)

p =
0.8011

-4.0852

b=0.8011, ln a =-4.0852
a=exp(-4.0852) = 0.0168
Nu Re Nu = 0.0168Re 0.8011

6.
6. k T

T/K

363

373

383

393

403

k 10 2 / m in 1

0.718

1.376

2.701

5.221

9.718

k T
k T

k = k0 exp( E / RT )
k0 E

E
RT
1
E
y=lnkx= a = ln k0b = ,
T
R
y = a + bx
ln k = ln k0

% xiti0406
T=[363 373 383 393 403];
k=[0.718 1.376 2.701 5.221 9.718];
y=log(k); x=1./T ;
p=polyfit(x,y,1)

p =
1.0e+003 *
-9.5707
0.0260
k0 =exp(26)E = 1.987.*95707,
k0 =1.9573e+011, E =1.9018e+004
k = 1.9573 1011 exp(

9570.7
)
T

7.
7.

ln P = a + bT + c / T + d ln T P T

T /K

373.15 393.25

P / atm

425.55 453.65
5

10

486.25 507.75

524.25 537.85

549.65

20

40

60

30

50

1
, f 4 = ln T , y = ln p
T
y=af1 +bf2 + cf3 + df 4
f1 = 1, f 2 = T , f3 =

% xiti0407
T=[373.15 393.25 425.55 453.65 486.25 507.75 524.25 537.85 549.65];
P=[1 2 5

10 20 30 40 50 60];

f2=T;f3=1./T;f4=log(T);y=log(P);
x=[ones(9,1),f2',f3',f4'];

format long;
q=regress(y',x)

>> q =
1.0e+003 *
0.13681042565960
0.00001960482307
-9.71003592452062
-0.01994254997923
a=136.8104, b=0.0196, c=-9710.0359, d=-19.9425

5
1.

25
P1
P125
e 0.5t sin(t + / 6)dt cumsumtrapzquadquadl

0.9008407878

% xiti0501
t=0:3*pi;
y=xiti0501f(t);
format long;
z1=cumsum(y);
z1(end)
z2=trapz(t,y)
z3=quad('xiti0501f',0,3*pi)
z4=quadl('xiti0501f',0,3*pi)

xiti0501f
function y=xiti0501f(t)
y=exp(-0.5.*t).*sin(t+pi./6);

z1 =
1.100053110185653
z2 =
0.850601117281238
z3 =
0.900840811006463
z4 =
0.900840787756463
quadl
2. 1000C 0.032
0.0018k/mol, t L
t/min

30

60

120

180

0.451

0.633

0.783

0.842

dL/dt

% xiti0502
t=[0
30 60 120 180];
L=[0
0.451 0.633 0.783

0.842];

dt=diff(t); t

dL=diff(L); L
q=dL./dt
q

q =
0.0150

0.0061

0.0025

0.0010

3. F(t)
t
t/min

F(t)

t/min

F(t)

0.7570

0.1

0.0197

10

0.8679

0.2

0.0392

15

0.9621

0.1821

20

0.9996

0.3299

25

1.000

0.6361

30

1.000

E(t)

% xiti0503
t=[0 0.1 0.2 1 2 5 7 10 15 20 25 30];
F=[0 0.0197 0.0392 0.1821 0.3299 0.6361 0.7570 0.8679 0.9621 0.9996 1.000
1.000];
cs=csapi(t,F); %

pp=fnder(cs); %
dF=fnval(pp,t); % t
disp('')
disp([t;dF])

:
Columns 1 through 8
0
0.1000
0.2000
1.0000
0.1974
0.1963
0.1934
0.1639
Columns 9 through 12
15.0000 20.0000 25.0000
0.0121
0.0030 -0.0014

2.0000
0.1338

5.0000
0.0743

7.0000
0.0488

10.0000
0.0278

30.0000
0.0029

t2

4.
4. t1 t2

C p /( J / mol K )

Q = C p dt
t1

Cp

t (o C )
t (o C )

25

100

150

200

250

300

350

400

450

500

Cp /(J / mol K)

40.5

45.6

48.3

51.4

55.3

56.4

58.9

60.1

63.2

64.9

1mol 250C 5000C


% xiti0504
t=[25 100 150 200 250 300 350 400 450 500];
Cp=[40.5 45.6 48.3 51.4 55.3 56.4 58.9 60.1 63.2 64.9];
Q=trapz(t,Cp)

Q =
2.5671e+004
5.
P(atm)

0.0

20.0

40.0

60.0

80.0

100.0

120.0

1.0

0.98654

0.97420

0.96297

0.95286

0.94387

0.93599

ln =

p
0

z 1
dp
p

pz

p z

% xiti0505
p=[0.0 20.0
z=[1.0

40.0

60.0

80.0

100.0

120.0];

0.98654 0.97420 0.96297 0.95286 0.94387 0.93599];

sp=spline(p,z); % spline

%
pplot=linspace(p(1),p(end),100);
zplot=fnval(sp,pplot);
plot(p,z,'*',pplot,zplot,'-');
h=quad(@xiti0505f,0.1,120,[],[],sp); %
H=exp(h)

xiti0505f
function f=xiti0505f(p,sp)
z=fnval(sp,p);
f=(z-1)./p;
H =
0.9287
function xiti0505
h=quad(@xiti0505f,0.0,120); %
H=exp(h)
function f=xiti0505f(p0)
p=[0.0 20.0
z=[1.0

40.0

60.0

80.0

100.0

120.0];

0.98654 0.97420 0.96297 0.95286 0.94387 0.93599];

% sp=csaps(p,z); %
sp=spline(p,z); %spline
%
pplot=linspace(p(1),p(end),100);
zplot=fnval(sp,pplot);
plot(p,z,'*',pplot,zplot,'-');
z0=fnval(sp,p0);
f=(z0-1)./p0;

47
P1
P147

1.
1)

d2y
= 1 + (1 + x 2 ) y , y (0) = 1, y '(0) = 3
dx 2

function CACE6_11
ode45(@odefun,[0 10],[1 3])
function dy=odefun(x,y)
dy(1)=y(2);
dy(2)=1+(1+x.^2).*y(1);
dy=[dy(1);dy(2)];

d3y
= y, y (0) = 1, y '(0) = 0, y "(0) = 0
dx 3

function CACE6_12
ode45(@odefun,[0 10],[1 0 0])
function dy=odefun(x,y)
dy(1)=y(2);
dy(2)=y(3);
dy(3)=-y(1);
dy=[dy(1);dy(2);dy(3)];

3) x 3

d3y
d2y
dy

2
y
3 = 3e2 x , y(1) = 1, y '(1) = 10, y "(1) = 30, x [1,1.5]
3
2
dx
dx
dx

function CACE6_13
ode45(@odefun,[1 1.5],[1 10 30])
function dy=odefun(x,y)
dy(1)=y(2);
dy(2)=y(3);
dy(3)=(2*y(3)+3*y(2)+3*exp(2*x))./(x.^3);
dy=[dy(1);dy(2);dy(3)];

2.
x' = x 3 y , x(0) = 1
0<t<30

y ' = x y 3 , y (0) = 0.5

function Excercise6_2
tspan=[0 30];
y0=[1 0.5];
ode45(@odefun,tspan,y0)
title('')
legend('x','y')
figure
opts=odeset('Outputfcn','odephas2');
sol=ode45(@odefun,tspan,y0,opts);
title('');
t=[1.5 2 2.5];
yt=deval(sol,t);
fprintf('\nThe solutions at t=1.5 2 2.5
are\t%.f\t%.f\t%.f\trespectively\n',yt(1),yt(2),yt(3))
function f=odefun(t,y)
dy1=-y(1).^3-y(2);
dy2=y(1)-y(2).^3;
f=[dy1;dy2];
1

1
x
y

0.8

0.6
0.5
0.4

0.2
0
0

-0.2

-0.5

10

15

20

25

30

-0.4
-0.5

0.5

3. x" = (2 / t ) x'+ (2 / t 2 ) x + (10 cos(ln(t ))) / t 2 x(1)=1x(3)=3 t=1.522.5


x x

function Excercise4_2
solinit=bvpinit(linspace(1,3,10),[1;1]);
sol=bvp4c(@odefun,@bcfun,solinit);
plot(sol.x,sol.y(1,:),'b-')
xlabel('t');
ylabel('x');
t=[1.5 2 2.5];
xt=deval(sol,t);
fprintf('\nThe solutions at t=1.5 2 2.5
are\t%.4f\t%.4f\t%.4f\trespectively\n',xt(1,1),xt(1,2),xt(1,3))
function f=odefun(t,x)

dx1dt=x(2);
dx2dt=(-2/t).*x(2)+(2/t.^2).*x(1)+10*cos(log(t))./t.^2;
f=[dx1dt;dx2dt];
function bc=bcfun(xa,xb)
bc=[xa(1)-1;xb(1)-3];

The solutions at t=1.5 2 2.5 are

0.5224 1.1664 2.0609 respectively

2.5

1.5

0.5

1.2

1.4

1.6

1.8

2
t

2.2

2.4

2.6

2.8

4.
:
1 d 2 x A dx A

+ rA = 0
Pem d 2
d

= 0, x A

1 dx A
=0
Pem d

= 1,

dx A
=0
d

Pem = 2, rA = 2(1-xA)2 xA

y=

dx A

dx A
=y
d
dy
= 2( y 2(1 x A ) 2 )
d

= 0, x A
= 1,

y
=0
2
y=0

MATLAB bvp4c

dx A
= 2(1 x A ) 2
d

= 0, x A = 0
MATLAB ode45
MATLAB

function dispersion
%
global Pe
Pe = 2;
a = 0;
b = 1;
solinit = bvpinit(linspace(a,b),[0 0]);
sol = bvp4c(@dispersion,@bcfun,solinit);
conversion1 = sol.y(1,end)

%
%

%
xA0 = 0;
[x,yp] = ode45(@plug,[a b],xA0);
conversion2 = yp(end)
%
plot(sol.x,sol.y(1,:),'b-',x,yp,'k-')
xlabel('dimensionless distance')
ylabel('conversion')
legend('dispersion model','plug model')
function dydx = dispersion(x,y)
global Pe
dy1dx = y(2);
dy2dx = Pe*(y(2)-2*(1-y(1))^2);
dydx = [dy1dx;dy2dx];

function bc = bcfun(ya,yb)
bc = [ya(1)-ya(2)/2;yb(2)];
function dypdx = plug(x,yp)
dypdx = 2*(1-yp)^2;

0.5680
0.6667 0+
0.2894

5.
5 cm 100 cm
C6H5CH3T+H2H C6H6B+CH4H
3 kmol/hr 1.15 800K

rT = kcTcH0.5 mol/(dm3s)

k = 3.5109exp(-50900/RT) (dm3/mol)0.5/hr
R = 8.314 J/(molK)

298K [CP = A+BT+CT 2+DT 3 (J/mol/K)]


Hf,298J/mol

5.003104

-24.355

27.143

8.298104

-33.917

-7.486104

0.5124

-2.76510-4 4.91110-8

9.27310-3

-1.3810-5

0.4743
5.21210-2

19.251

7.64510-9

-3.01710-4

7.1310-8

1.19710-5

-1.13110-8

50900

dcT
= 3.5 109 e 8.314 T cT cH0.5 / 3600 mol/(dm3s)
dz
50900

dcH
= 3.5 109 e 8.314 T cT cH0.5 / 3600 mol/(dm3s)
dz
50900

dc
u B = 3.5 109 e 8.314 T cT cH0.5 / 3600 mol/(dm3s)
dz
50900

dc
u M = 3.5 109 e 8.314 T cT cH0.5 / 3600 mol/(dm3s)
dz

50900

dT
= ( H ) 3.5 109 e 8.314 T cT cH0.5 / 3600 J/(dm3s)
dz

ucP

cP

c p = ( Ai + Bi T + Ci T 2 + Di T 3 ) xi (

298
)
22.4 T

J / dm 3

H T
T

H = ( H

o
fB

+H

o
fM

o
fT

o
fH

)+

(A + BT + CT

+ DT 3 )dT

298

z=0

cT = 1.0/(0.082800)/2.15 = 7.0910-3

cB = cM = 0

T = 800

function pfreactor
%
global cpA cpB cpC cpD dcpA dcpB dcpC dcpD

cH = 1.15 cT = 8.1510-3

J / mol

%
cpA = [-24.355 27.143 -33.917 19.251];
cpB = [0.5124 9.273e-3 0.4743 5.212e-2];
cpC = [-2.765e-4 -1.38e-5 -3.017e-4 1.197e-5];
cpD = [4.911e-8 7.645e-9 7.13e-8 -1.131e-8];
%
dcpA = cpA(3)+cpA(4)-cpA(1)-cpA(2);
dcpB = cpB(3)+cpB(4)-cpB(1)-cpB(2);
dcpC = cpC(3)+cpC(4)-cpC(1)-cpC(2);
dcpD = cpD(3)+cpD(4)-cpD(1)-cpD(2);
%
T0 = 800;
ct0 = 1.0/(0.082*T0);
f1 = ct0/2.15;
f2 = f1*1.15;
f0 = [f1,f2,0,0,T0]';
l0 = 0;

l1 = 10*(1+eps);

[l,f] = ode45(@pfr,[l0 l1],f0);


%
plot(l,f(:,1),l,f(:,2),l,f(:,3))
xlabel('length(dm)')
ylabel('concentration(mol/liter)')
legend('toluene','hydrogen','benzene & methane')
figure
%
plot(l,f(:,5))
xlabel('length(dm)')
ylabel('Temperature(K)')
%
mu = 1 - f(end,1)/f(1,1)
function yp = pfr(l,f)
%
global cpA cpB cpC cpD dcpA dcpB dcpC dcpD
As = pi*0.5^2/4;
qv = 3000*22.4*f(5)/(273*3600);
u = qv/As;
sumf = 0;
for i = 1:4
sumf = sumf+f(i);
end
for i = 1:4
x(i) = f(i)/sumf;
end
cp = cpmix(f(5),x);
cp = cp*273/(22.4*f(5));

rheat = heatr(f(5));
k = 3.5e9*exp(-50900/(8.314*f(5)))/3600;

ra = k*f(1)*f(2)^0.5;
yp(1) = -ra/u;
yp(2) = -ra/u;
yp(3) = ra/u;
yp(4) = ra/u;
yp(5) = -rheat*ra/cp/u;
yp = yp';
function cp = cpmix(T,x)
%
global cpA cpB cpC cpD dcpA dcpB dcpC dcpD
cp = sum((cpA+cpB*T+cpC*T^2+cpD*T^3).*x);
function dheat = heatr(T)
%
global cpA cpB cpC cpD dcpA dcpB dcpC dcpD
hf = [5.003e4 0 8.298e4 -7.486e4];
hr298 = hf(3)+hf(4)-hf(1)-hf(2);
dhr = quad(@deltah,298,T);
dheat = hr298+dhr;
function y = deltah(T)
%
global cpA cpB cpC cpD dcpA dcpB dcpC dcpD
y = dcpA+dcpB*T+dcpC*T.^2+dcpD*T.^3;

0.0070902 mol/dm3 0.00076034 mol/dm3

xA =

0.0070902 0.0007604
= 89.42%
0.0070902

6.
B 180~260
X C, D E
Ea

rkC k = k 0 e RT k015.780521010k023.923171012k03
1.64254104k046.264108Ea1=124670Ea2=150386Ea3=77954Ea4=111528

CA=1kmol/m3 0 B 224.6
1)2)

dC A
= (k1 + k 2 )C A
dt
dC B
= k1C A k 3 C B
dt
dC C
= k 2 C A k 4 CC
dt
dC D
= k 3C B k 5 C D
dt
dC E
= k 4 CC + k5 C D
dt

function Exc6_6
T = 224.6 + 273.15; % Reactor temperature, Kelvin
R = 8.31434;

% Gas constant, kJ/kmol K

% Arrhenius constant, 1/s


k0 = [5.78052E+10 3.92317E+12 1.64254E+4 6.264E+8];
% Activation energy, kJ/kmol
Ea = [124670 150386 77954 111528];
% Initial concentration:C0(i), kmol/m^3
C0 = [1 0 0 0 0];
tspan = [0 1e4];
opt=odeset('reltol',1e-4,'outputfcn','odephas2','outputsel',[1;2]);
[t,C] = ode45(@MassEquations, tspan, C0,opt,k0,Ea,R,T);
xlabel('A'),ylabel('B'),title('The Relationship between A and B')
% plot
figure
plot(t,C(:,1),'r-',t,C(:,2),'k:',t,C(:,3),'b-.',t,C(:,4),'k--');
xlabel('Time (s)');
ylabel('Concentration (kmol/m^3)');

legend('A','B','C','D')
CBmax = max(C(:,2));

% CBmax: the maximum concentration of B, kmol/m^3

yBmax = CBmax/C0(1)

% yBmax: the maximum yield of B

index = find(C(:,2)==CBmax);
t_opt = t(index)

% t_opt: the optimum batch time, s

% -----------------------------------------------------------------function dCdt = MassEquations(t,C,k0,Ea,R,T)


% Reaction rate constants, 1/s
k = k0.*exp(-Ea/(R*T));
k(5) = 2.16667E-04;
% Reaction rates, kmoles/m3 s
rA = -(k(1)+k(2))*C(1);
rB = k(1)*C(1)-k(3)*C(2);
rC = k(2)*C(1)-k(4)*C(3);
rD = k(3)*C(2)-k(5)*C(4);
rE = k(4)*C(3)+k(5)*C(4);
% Mass balances
dCdt = [rA; rB; rC; rD; rE];

7. 5mm 700K
k4s-1D=0.05cm2/s

d 2 C 2 dC
f (C )
0<r<1
+
=2
2
dr
r dr
CS

dC
(0) = 0, C (1) = 1
dr

C r Cs Thiele
1

= rP

k
=
D

0
1

function Cha5demo12
global fai Cs
Cs = 1;
rp = 5e-3/2;
k = 4;
D = 0.05e-4;

% m
% 1/s
% m^2/s

fai = rp*sqrt(k/D) % fai: Thiele


a = 0;

f (C )r dr
f (C )r dr
2

b = 1;
solinit = bvpinit(linspace(a,b,100),[0 0]);
sol = bvp4c(@ODEs,@BCfun,solinit);
plot(sol.x,sol.y(1,:),'b-')
xlabel('Dimensionless Radius')
ylabel('Dimensionless Concentration')
I1 = quadl(@IntFunc1,a,b,[],[],sol.x,sol.y(1,:))
I2 = quadl(@IntFunc2,a,b)
eta = I1/I2
fprintf('\t: = %.4f\n',eta)
% -----------------------------------------------------------------function dydr = ODEs(r,y)
global fai Cs
dy1dr = y(2);
if r == 0
dy2dr = 0;
else
dy2dr = fai^2*y(1)/Cs - 2/r*y(2);
end
dydr = [dy1dr; dy2dr];
% -----------------------------------------------------------------function bc = BCfun(ya,yb)
bc = [yb(1)-1; ya(2)];
% -----------------------------------------------------------------function f = IntFunc1(r,ri,yi)
f = spline(ri,yi,r) .* r.^2;
% -----------------------------------------------------------------function f = IntFunc2(r)
global Cs
f = Cs * r.^2;

También podría gustarte