Chapter 4
Numerical Solution of Ordinary
Differential Equations
2
Numerical Solution of Ordinary Differential Equations
①
initial value problems (IVPs)
②
boundary value problems (BVPs)
③
numerical solution of ordinary differential equations (ODEs)
④
differential-algebraic equations (DAEs)
the relevant Simulink tools and the differential equation editor (DEE) simulation interface will
be presented in this chapter.
04
3
Numerical Solution of Ordinary Differential Equations
4.1
Initial value problems for ordinary differential equations
4.1.1 The standard problem
dx1
= f1 ( x1 , x2 , K , xn , u , t ),
dt
dx2
= f 2 ( x1 , x2 , K , xn , u , t ),
dt
M
dxn
= f n ( x1 , x2 , K , xn , u , t ),
dt
continuous stirred-tank reactor
x1 (0) = x10
x2 (0) = x20
xn (0) = xn 0
04
4
Numerical Solution of Ordinary Differential Equations
dx1
x2
= − x1 + Da (1 − x1 ) exp
÷,
dt
1 + x2 / φ
x1 (0) = x10
dx2
x2
= −(1 + β ) x2 + BDa (1 − x1 ) exp
÷+ β u ,
dt
1 + x2 / φ
•
x2 (0) = x20
x1 and x2 represent the dimensionless reactant concentration and reactor temperature, respectively, while
the forcing function (input variable) u is the cooling water temperature,
•
u is the cooling water temperature,
•
The CSTR model parameters Da, φ, B, and β represent the Damköhler number, activation energy, heat of
reaction, and the heat transfer coefficient, respectively.
04
5
Numerical Solution of Ordinary Differential Equations
04
4.1.2 The MATLAB ODE solvers
Problem types
Non-stiff ODE
Commands
Algorithms (Shampine and Rechelt, 1997)
ode45
Explicit Runge-Kutta (4, 5) pair of Dormand-Prince
ode23
Explicit Runge-Kutta (2, 3) pair of Bogacki and Shampine
ode113
Variable order Adam-Bashforth-Moulton PECE solver
ode15s
Numerical differentiation formulas of order 1-5
ode23s
Modified Rosenbrock (2, 3) pair with a free interpolant
ode23t
Trapezoidal rule with a free interpolant
Stiff ODE
ode23tb
Implicit Runge-Kutta formula with trapezoidal rule and a backward differentiation formula of order
2.
6
Numerical Solution of Ordinary Differential Equations
04
[t, x]=ode45('odefun', tspan, xo)
Output
t
Description
The position vector of the independent variable
The system state output, which is a matrix of n columns. Its row number equals to the length of independent
x
variable t and the i-th column contains the evolutionary data of the i-th state with respect to the independent
variable t.
7
Numerical Solution of Ordinary Differential Equations
Input
04
Description
The file name of the ordinary differential equation, which is in the format of
odefun
function xdot=fun(t, x)
xdot= [...]; % list the ordinary differential equations one by one
The position of the independent variable t. If tspan is set to [to tf], indicating that only the beginning and the end of
the independent variable t are assigned, the middle points in between are automatically determined by the
tspan
program. Furthermore, if users intend to acquire the state values at some specific values of the independent
variable, tspan can be specified as [t0 t1 … tm], where ti is the desired inner location of the independent variable
satisfying the relation of t0 < t1 < …< tm (= tf).
x0
The vector of initial state values.
8
Numerical Solution of Ordinary Differential Equations
Example 4-1-1
Dynamic simulation of a CSTR
Da = 0.072, φ = 20, B = 8, and β = 0.3
u = 0,
x1(0) = 0.1 and x2(0) = 1.
Step 1: Create an ODE file named ex4_1fun.
────────────── ex4_1_1fun.m ──────────────
function xdot=fun(t, x)
% subroutine: ODE model of the CSTR
global Da phi B beta % declared as global variables
xdot=[-x(1)+Da*(1-x(1))*exp(x(2)/(1+x(2)/phi))
-(1+beta)*x(2)+B*Da*(1-x(1))*exp(x(2)/(1+x(2)/phi))]; % u=0
─────────────────────────────────────────────────
04
9
Numerical Solution of Ordinary Differential Equations
Step 2: Create the main program to solve the IVP ODE
─────────────── ex4_1_1.m ───────────────
%
% Example 4-1-1 Main program for solving CSTR dynamics
%
clear; close all; clc;
%
global Da phi B beta % declared as the global variables
%
Da=0.072; % Damköhler number
phi=20; % activation energy
B=8; % heat of reaction
beta=0.3; % heat transfer coefficient
x0=[0.1 1]'; % initial value of the system state
%
% Solving with ode45
%
[t, x]=ode45('ex4_1_1fun', [0 20], x0);
%
─────────────────────────────────────────────────
04
10
Numerical Solution of Ordinary Differential Equations
Step 2: Create the main program to solve the IVP ODE
─────────────── ex4_1_1.m ───────────────
% Resutls plotting
%
figure(1) % diagram of the system states
plot(t,x(:,1),t,x(:,2),'--')
%
title('diagram of the system states')
xlabel('time')
ylabel('dimensionless system states')
legend('reactant concentration', 'reactor temperature')
%
figure(2) % phase diagram
plot(x(:,1), x(:,2), '-o')
xlabel('dimensionless reactant concentration')
ylabel('dimensionless reactor temperature')
title('phase diagram')
─────────────────────────────────────────────────
04
11
Numerical Solution of Ordinary Differential Equations
Step 3:
>> ex4_1_1
04
12
Numerical Solution of Ordinary Differential Equations
04
13
Numerical Solution of Ordinary Differential Equations
────────────────── ex4_1_1b.m ─────────────────
function ex4_1_1b
%
% Example 4-1-1 CSTR dynamics - the effect of the initial values
%
clear; close all;
clc
%
global Da phi B beta % declared as global variables
%
Da=0.072; % Damköhler number
phi=20; % activation energy
B=8; % heat of reaction
beta=0.3; % heat transfer coefficient
%
% different initial values
%
x1=[0 0 0 0 0 0.6 0.6 0.6 0.6 0.5 0.5 0.5 0.5 0.2 0.3 0.3 0.3 …
0.3 0.3 0.9 0.9 0.9 0.9 0.9 0.6 0.6 0.6 0.6];
04
14
Numerical Solution of Ordinary Differential Equations
────────────────── ex4_1_1b.m ─────────────────
x2=[0.1 0.3 0.5 1 1.5 0.1 0.3 0.5 1 1.5 1.2 1.8 2 2 2.3 2.5 2.8 …
3 3.5 4 4.3 4.8 5 5.5 4 5 5.5 6];
N=length(x1);
%
figure(1) % phase diagram
hold on
%
for i=1: N
x0=[x1(i) x2(i)]'; % initial value
%
% ODE solutions
%
[t, x]=ode45(@fun, [0 20], x0);
%
% phase diagram plotting
%
plot(x(:,1), x(:,2))
end
hold off
04
15
Numerical Solution of Ordinary Differential Equations
────────────────── ex4_1_1b.m ─────────────────
xlabel('dimensionless reactant concentration')
ylabel('dimensionless reactant temperature')
title('phase diagram')
text(0.144,0.886, '\leftarrow A', 'FontSize', 18)
text(0.4472,2.7517, '\leftarrow B', 'FontSize', 18)
text(0.7646,4.7050, '\leftarrow C', 'FontSize', 18)
%
% ODE model of the CSTR
%
function xdot=fun(t, x)
global Da phi B beta % declared as global variables
xdot=[-x(1)+Da*(1-x(1))*exp(x(2)/(1+x(2)/phi))
-(1+beta)*x(2)+B*Da*(1-x(1))*exp(x(2)/(1+x(2)/phi))];
─────────────────────────────────────────────────
04
16
Numerical Solution of Ordinary Differential Equations
•
04
The whole phase diagram shows
that the CSTR has three
equilibrium points of which A
(0.1440, 0.8860), and C (0.7646,
4.7050) are stable equilibrium
points and B (0.4472, 2.7517) is
the unstable one.
17
Numerical Solution of Ordinary Differential Equations
4.1.3 Solving ODEs with MATLAB Simulink
Step 1: Enter the Simulink environment and open a new model file.
Step 2: In Fcn,
-u(1)+Da*(1-u(1))*exp(u(2)/(1+u(2)/phi))
Fcn1:
- (1+beta)*u(2)+B*Da*(1-u(1))*exp(u(2)/(1+u(2)/phi))
04
18
Numerical Solution of Ordinary Differential Equations
04
19
Numerical Solution of Ordinary Differential Equations
Step 3: Open
to key in the initial condition value.
04
20
Numerical Solution of Ordinary Differential Equations
Step 4: Create a subsystem for the constructed module.
04
21
Numerical Solution of Ordinary Differential Equations
Step 5:
Create the dialogue box.
04
22
Numerical Solution of Ordinary Differential Equations
Step 5:
Mask Edito
04
23
Numerical Solution of Ordinary Differential Equations
Step 6: Execute the simulation.
(i) open the dialogue box
04
24
Numerical Solution of Ordinary Differential Equations
Step 6: Execute the simulation.
(ii) Input the simulation time and select the numerical
method
04
25
Numerical Solution of Ordinary Differential Equations
Step 6: Execute the simulation.
(iii) Press
to simulate
04