شزﻮﻣآ Matlab: ﯽﻟﻮﻤﻌﻣ ﻞﯿﺴﻧاﺮﻔﯾد تﻻدﺎﻌﻣ ﻞﺣ) Odes (

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

8/2/2015

MATLAB

)(ODEs
1394

dsolve
ode functions

MATLAB

.

1394

1
8/2/2015


MATLAB
.
MATLAB )( dsolve .

syms x y
)'y=dsolve('Dy=y*x','x

>> syms x y
)'>> y=dsolve('Dy=y*x','y(1)=1','x
y ] [0,1 :
)]>> ezplot(y,[0,1
1394

>> syms x y
)'>> y=dsolve(D2y-Dy-6*y=0','x

>> syms x y
)'>>y=dsolve('D2y+2*Dy+2*y=0','y(0)=0','Dy(0)=-2','x
)>> ezplot(y
1394

2
8/2/2015

)'>> x = dsolve('Dx)^2 + x^2 = 1','x(0) = 0


])x = [-sin(t
])[ sin(t

;)'>> y = dsolve('D2y = cos(2*x) - y', 'y(0) = 1', 'Dy(0) = 0', 'x


)>> simplify(y
1394

)(dsolve
:

>>syms x y
[x,y]=dsolve('Dx-2*x-3*y=2*exp(2*t)','-
))x+Dy-4*y=3*exp(2*t

1394

3
8/2/2015

)'>> [f g] = dsolve('Df = 3*f+4*g', 'Dg = -4*f+3*g

1394

MATLAB

MATLAB
. .


ode23 -
.
ode23s stiff
.
ode23t
.
ode23tb
ode23s.

1394

4
8/2/2015

MATLAB


- ode45
.
ode113 -
. stiff
.
ode15s

.

1394

syms t y
f=@(t ,y)2*y-1
)ode45(f, [0,1], 1
)[t y]=ode45(f,[0 1],1

1394

5
8/2/2015

:
.
)'dsolve('Dy = 2*y-1, y(0)=1','t
= ans
exp(2*t)/2 + 1/2

;)[t,ya] = ode45(f,0:0.1:1,1
;)y = 1./2+1./2.*exp(2.*t
format long
][t,ya,y
= ans
0 1.000000000 1.0000000000
0.10000000 1.110701386 1.1107013790
0.20000000 1.245912367 1.2459123488
0.30000000 1.411059434 1.4110594001
0.40000000 1.612770519 1.6127704642
0.50000000 1.859140998 1.8591409142
0.60000000 2.160058585 2.1600584613
0.70000000 2.527600159 2.5275999834
0.80000000 2.976516458 2.9765162121
0.90000000 3.524824070 3.5248237322
1.00000000 4.194528508 4.1945280494

1394

: batch .

A + BC and C + BD

1394

6
8/2/2015

function f = batch_reactor(t, x);


f = zeros(size(x));
% extract concentrations from state vector
k1 = 1; k2 = 0.5; % set fixed parameters
cA = x(1); cB = x(2); cC = x(3); cD = x(4);
% compute rates of each reaction
r1 = k1*cA*cB; r2 = k2*cC*cB;
f(1) = -r1; % mole balance on A
f(2) = -r1 -r2; % mole balance on B
f(3) = r1 - r2; % mole balance on C
f(4) = r2; % mole balance on D
return;
1394

k1=1, k2=0.5, CA0=1, CB0=2


. 10
% set initial state
CA0 = 1; CB0 = 2; x0 = [CA0; CB0; 0; 0];
tend = 10; % set end time
% call ode45 to perform simulation
ode45(@batch_reactor, [0 tend], x0);
[t,x] = ode45(@batch_reactor, [0 tend], x0);

1394

7
8/2/2015

batch
(A + B C, C + B D).
1394

: batch
.

1394

8
8/2/2015

function F = batch_reactor(t, C)
CA=C(1);CB=C(2);CC=C(3);CD=C(4);CE=C(5);
k1=1.3; k2=1.4; k3=1.6;
F(1)=-k1*CA*CB-k3*CA*CD*CB;
F(2)=-k1*CA*CB-k2*CC*CB-k3*CA*CD*CB;
F(3)=k1*CA*CB-k2*CC*CB;
F(4)=k2*CC*CB-k3*CD*CA*CB;
F(5)=k3*CD*CA*CB;
F=F';
return;

C0=[1;2;0;0;0];
Ode45(@batch_reactor,[0 10],C0);
1394

1394

You might also like