Вы находитесь на странице: 1из 19

ODE Solver

Dept. of Environmental Engineering


ITB
Lecture 9 Outline

• ODE solvers
– chemical reactor example
– ode45 function

• “Function” functions
– feval command
– RMS voltage/numerical integration example

• eval command

2
Design Problem
• Many chemical reactions are
“exothermic” i.e. they release heat as
they proceed.
• You are designing a chemical reactor in
which chemical A is converted into
chemical B.
• As part of the design you need to know
the concentration of chemical A and the
temperature of the reactor as a function
of time.
3
Mathematical Model
• The behaviour of the chemical reactor
can be described by two differential
equations:
C  E / RT
C(t) = concentration of A

 kCe T(t) = temperature

t h, k, E, R = constants

T  E / RT
 hkCe
t
4
Solution Process
• The coupling between concentration and
temperature makes these equations
tough to solve analytically.
• Numerical methods exist to solve these
equations.
– Can write your own script files to
implement.
– Matlab also has these methods
implemented as part of its “toolbox”.

5
Systems of ODEs
• The reactor problem is an example of a
system of ODEs.
– Multiple variables.
– Multiple ODEs governing their interaction.

y1
 f1 (t , y1 , y2 )
t
two ODEs two variables
y2
 f 2 (t , y1 , y2 )
t
6
Reactor Example: Exercise 1
• Fill in the table below showing the
correspondence between the variables
and functions in the “generic” system of
ODEs and the reactor case.
y1 C

y2 T

t t

f1 kCe-E/RT
f2 khCe-E/RT

7
Developing a Matlab Solution
• We will use a Matlab function called
ode45 to solve the system of ODEs
representing the chemical reactor.

[T,Y] = ODE45(ODEFUN,TSPAN,Y0)
where TSPAN = [T0 TFINAL]
output arrays of Y0 = [Y10 Y20] user defined
t and y values
Matlab function
evaluating f1 and
time domain
initial conditions f2
for solution
8
Matlab Implementation
• We’ll start by writing the function defining
the right hand side (RHS) of the ODEs.
function [ode] = reactorfunc(t,y) Function takes inputs
% set up constants of
Comments – important!
a time value (t) and
E = 2500; an array
% the activation energy in J/mol Define of reactor
constants in
R = 8.314; % the gas constant in J/(mol K)
state variables
reactor at that
equations.
time (concentration
k = - 0.1; % constant in s^-1
and temperature).
h = -10; % constant in (K L)/mol

% work out RHS of ODEs


Calculate RHS values.
% store in an array to be returned
ode(1) = k*y(1)*exp(-E/R/y(2)); y(1) is concentration.
ode(2) = h*k*y(1)*exp(-E/R/y(2)); y(2) is temperature.
9
Filename: reactorfunc.m
Matlab Implementation

• Now we’ll write a main script file to call


ode45.
% set up initial conditions
Y0(1) = 1.0; % initial concentration of chemical A
Y0(2) = 300.0; % initial temperature in Kelvin

% set up time range


tspan(1) = 0.0; % start time, s
tspan(2) = 150.0; % end time, s

% call ODE solver Filename: reactor.m


[t,y] = ode45(@reactorfunc, tspan, Y0)

10
@reactorfunc ?
@ symbol means the
function input is another
function, not a variable.

11
Results
• When the reactor.m script file is run it
outputs two arrays:
t= y= concentration of A
0 1.0000 300.0000
1.3688 0.9510 300.4904
2.7376 0.9043 300.9574 reactor temperature
4.1063 0.8598 301.4022
5.4751 0.8174 301.8256
9.2251 0.7115 302.8846
12.9751 0.6191 303.8092
16.7251 0.5385 304.6155
… … 12
Results
• Plotting commands:
% plot concentration versus time
plot(t,y(:,1))
xlabel('Time, s')
ylabel('Concentration of chemical A')
title('Concentration of A in batch reactor')

figure
% plot temperature versus time
plot(t,y(:,2))
xlabel('Time, s')
ylabel('Temperature, K')
title('Temperature in batch reactor') 13
Results
Concentration of A in batch reactor
1

0.9

0.8
Concentration of chemical A

0.7

0.6

0.5

0.4

0.3
Temperature in batch reactor
0.2 310

0.1 309

0 308
0 50 100 150
Time, s 307

Temperature, K 306

305

304

303

302

301

300
0 50 100 14150
Time, s
“Function” functions
• ode45 is an example of a “function”
function, i.e. a function which takes
another function as an input.

• If you are going to write your own


“function” functions you will need to use
the feval command to evaluate the
function supplied as an input.

15
Electrical Engineering Example
• Voltage in an electrical circuit often vary
with time. It is often convenient for
electric engineers to define an “average”
voltage. The “average” which is typically
used is “root mean square” voltage.

T
1
 
2
VRMS V (t ) dt
T0
16
Solution Approach
• Will we assume V(t) is such that the
integral can not be done analytically.
• Will we do a simple numerical
integration using the midpoint rule.
T
f(t)
 f (t )dt   f dt
0 i
i

f2 f6
f1
f3
f5
f4

dt
17
0 t T
Matlab Implementation
function [RMS] = RMSVoltage(voltage,T,n)
dt = T/n; % calculate dt by dividing total time by
% number of panels
t = linspace(0+dt/2.0,T-dt/2.0,n); % define points in time to evaluate voltage
Vt = feval(voltage,t); % evaluate the function voltage function
% at each point in time
VtSquared = Vt.*Vt; % square voltage values (note dot operator)
RMS = sum(VtSquared)*dt; % area of each panel equals VtSquared times dt,
% sum these areas
RMS = sqrt(1.0/T*RMS); % finally divide by T and take square root
% to calculate RMS

Filename: RMSVoltage.m

18
Matlab Implementation

• We can use our Matlab function to


calculate the RMS voltage of any voltage
we supply.

• For example:
function [V] = voltageExample(t)
usage from Matlab command line
V = sqrt(t);

Filename: voltageExample.m
>> RMSVoltage(@voltageExample,10,20)
user supplied function

19

Вам также может понравиться