Differential equations are composed of an unknown function and its derivatives. The equation is called an ordinary differential equation (ODE) when this unknown function involves only one inde- pendent variable and partial differential equation (PDE) when more than one independent variable is involved.
2.7.1 initial-ValuE ProblEms
The most basic method for finding the approximate solution of a 1st-order ODE is Euler’s method. Consider a 1st-order ODE, (dy/dt) = y′ = f(t, y), with the initial condition y(t0) = y0 on an
interval [t0, tn]. We find the approximate solutions y1, y2, …, yn at the points t1 = t0 + h, t2 = t0 + 2h, …, tn = t0 + nh by computing
yi+1= +yi hf t y
( )
i, iwhere h = (tn − t0)/n. In the midpoint method, the approximate values of solutions are given by yi+1=yi-1+2hf t y
( )
i, iThe well-known Runge–Kutta methods are based on the determination of a sequence of approxi- mations to the change yi+1 − yi followed by a combination of these to compute a new value of y.
These methods give results that match the higher-order Taylor series expansions, while an evalua- tion of derivatives of f(t, y) is not required. The optimal 2nd-order Runge–Kutta method is given by
k1=hf t y
( )
i, ik2 hf ti 2h yi k1
3 2
= æ + +3
èç ö
ø÷ ,
yi+1= +yi 14
(
k1+3k2)
and the classic 4th-order Runge–Kutta method is given by k1=hf t y
( )
i, ik2 hf ti 1h yi k1
2 1
= æ + +2
èç ö
ø÷ ,
k3 hf ti 1h yi k2
2 1
= æ + +2
èç ö
ø÷ ,
k4=hf t
(
i+h y, i+k3)
yi+1= +yi 16
(
k1+2k2+2k3+k4)
An nth-order ODE can be transformed into a set of 1st-order ODEs. The general form of an nth- order differential equation can be expressed as
f t y dy dt
d y dt
d y
dt f t y y y y
n n
, , , , , , , , , , n
2 2
1 1
¼ 1
æ
èç ö
ø÷ =
(
¢ ¢¢¼)
- -
( )- ==d y = ( ) dt y
n n
n
The state function can be defined as
s t
y t y t y t
y t
s t s t
n
( )
=( ) ( )
( ) ( )
é
ë êê êê êê ê
ù
û úú úú úú ú
=
( ) (
¢
¢¢
( )-
1
1 2
))
( ) ( )
é
ë êê êê êê
ù
û úú úú úú s t s tn 3
If we substitute these relations into the nth-order differential equation, we have the equivalent sys- tem of simultaneous ordinary differential equations, which can be represented by
ds t dt
y t y t y t y t
y t
n
( )
=( ) ( )
( ) ( )
é
ë êê êê êê ê
ù
û úú úú úú ú
=
( )
¢
¢¢
¢
( ) ( )
3
¢¢¢
¢
( ) ( )
( )
é
ë êê êê êê ê
ù
û úú úú úú ú
=
( )
( )
( )-
y t y t f t y y y
s t s
n 3
1
2
… , , , ,
33 4
1 2
t s t
f t s t s t s tn
( ) ( )
( ) ( ) ( )
( )
é
ë êê êê êê ê
ù
û úú úú úú ú
…
, , , ,
The solution of this system requires that n initial conditions be known at the starting value of t.
MATLAB® provides many built-in functions for solving ordinary differential equations. In order to use them, the given differential equation should be transformed into the equivalent system of 1st- order differential equations. Table 2.21 presents some of the built-in functions.
MATLAB’s built-in functions for solving ODEs can be called in a number of different ways.
A simple representation of the syntax is
[t, y] = solver(odefun,tspan,y0,options)
where solver is the name of MATLAB’s built-in function, odefun is the name of the function defin- ing the system of 1st-order ODEs to be solved, tspan is the span of independent variable, y0 is the vector of initial values, and options is a data structure created with the odeset function to control features of the solution.
2.7.2 stiff systEm
A stiff system involves both rapidly changing components and the slowly changing ones. In some cases, the rapidly varying components die away quickly and the solution becomes dominated by the slowly varying components.
MATLAB® provides a number of built-in functions for solving stiff systems of ODEs. Table 2.22 shows some of these solvers.
TABLE 2.21
Some of MATLAB®’s Built-In Functions for Solving ODE
Functions Solution Method
ode23 Runge–Kutta lower order (2nd and 3rd order) ode45 Runge–Kutta higher order (4th and 5th order)
ode113 Adams–Bashforth–Moulton
TABLE 2.22
Some of MATLAB®’s Built-In Functions for Stiff Systems
Functions Solution Method
ode23s Modified 2nd-order Rosenbrock
ode23t Trapezoidal rule
ode23tb Implicit Runge–Kutta method
ode15s Variable-order implicit multistep method
Example 2.33: Solution of an ODE Solve
dy
dt =e-t, y
( )
0 = -1 from t = 0 to 1.Solution
A function can be created to hold the given differential equation:
function dy = expfn(t,y) dy = exp(-t);
end
The following script employs the built-in ode45 function to integrate expfn and generate a plot of the solution as shown in Figure 2.12:
>> tspan = [0 1]; y0 = -1;
>> [t y] = ode45(@expfn, tspan, y0);
>> plot(t,y), grid, xlabel('t'), ylabel('y(t)')
Example 2.34: van der Pol Equation The van der Pol equation can be expressed as
d y
dt y dy
dt y
2 1
2 12 1
1 1 0
-m
(
-)
+ =This equation can be transformed into a set of 1st-order differential equations as follows:
dy dt1=y2
–10 –0.9 –0.8 –0.7 –0.6 –0.5 –0.4 –0.3
0.1 0.2 0.3 0.4 0.5 t
y (t)
0.6 0.7 0.8 0.9 1
FIGURE 2.12 Plot of the solution of (dy/dt) = e−t, y(0) = −1.
dy
dt2 y1 y y12
1 2
= - +m
(
-)
Plot the changes of y1 and y2 with respect to time t from t = 0 to 25. μ = 1 and the initial condi- tions are y1(0) = y2(0) = 1.
Solution
First, create a function to hold the van der Pol equation:
function dy = vdpeqn(t,y, mu)
% y(1) = y1, y(2) = y2
dy = [y(2); -y(1) + mu*(1-y(1)^2)*y(2)];
end
The following script employs the built-in ode45 function to integrate vdpeqn and generate a plot of the solution as shown in Figure 2.13:
>> mu = 1; tspan = [0 25]; y0 = [1 1];
>> [t y] = ode45(@vdpeqn, tspan, y0, [], mu);
>> plot(t,y(:,1),t,y(:,2),':'), legend('y_1','y_2'), xlabel('t'), ylabel('y')
Example 2.35: Well-Mixed Tanks
Figure 2.14 shows a series of three well-mixed tanks. From mass balance equations, we have dV
dt q m q V dx
dt q x x mx V dx
dt q x x V dx
1 0 1 1 1
0 0 1 1 2 2
1 1 2 3
= + - , =
(
-)
- , =(
-)
, 33 2 2 3dt =q x
(
-x)
where xi (i = 1, 2, 3) is the concentration (mol/liter) of the solution contained in the tank i. Under normal steady-state operation, m is maintained at 0 and qi(1, 2, 3) is kept constant. At a certain time (t = 0), m is suddenly increased to 12 liter/min. Plot the concentrations in the three tanks from t = 0 to 2. The initial conditions are x0 = 0.15 mol/liter and q0 = 15 liter/min, and the initial volume of each tank is 20 liter.
–3 –2 –1
y 0
y1 y2
t 1
2 3
0 5 10 15 20 25
FIGURE 2.13 Plot of the solution of the van der Pol equation.
Solution
When m = 0, V1 = V2 = V3 = 20 liter and qi (i = 1, 2, 3) is constant (qi = 15 liter/min). Thus, V2 and V3 are constant and independent with changes in m. Substituting the given data, we have
dx dt
x V
dx
dt x x dx
dt x x dV
d
1 1
1
2 1 2 3
2 3 1
2 25 27 15
20
15
= . - , =
(
-)
, = 20(
-)
,tt =12
The system of differential equations can be defined as a function file where y1 = x1, y2 = x2, y3 = x3, and y4 = V1:
function dy = cstm(t,y) dy = [(2.25-27*y(1))./y(4);
15*(y(1)-y(2))/20;
15*(y(2)-y(3))/20;
12];
end
The following script employs the ode45 solver to integrate cstm and generate a plot of the solution as shown in Figure 2.15:
>> tspan = [0 2]; y0 = [0.15 0.15 0.15 20];
>> [t y] = ode45(@cstm, tspan, y0);
V1 V2 V3
1 2 3
x1 x0
x2 x3
q0
q1 q2 q3
m
FIGURE 2.14 A series of three well-mixed tanks.
0
x (t)
0.09 0.1 0.11 0.12 0.13 0.14 0.15
0.2 x1 x2 x3
0.4 0.6 0.8 1 t (min)
1.2 1.4 1.6 1.8 2
FIGURE 2.15 Concentration changes in mixed tanks.
>> plot(t,y(:,1),t,y(:,2),'.-',t,y(:,3),':'), xlabel('t(min)'), ylabel('x(t)')
>> legend('x_1', 'x_2', 'x_3')
Example 2.36: Stiff Differential Equation5
A biological process involving the growth of a biomass from substrate can be represented as dB
dt kBS K S
dS dt
kBS
= K S
+ = -
, 0 75. +
where B and S are the biomass and substrate concentrations, respectively. Solve these differential equations from t = 0 to 20. At t = 0, S and B are 5 and 0.05, respectively. The reaction kinetics are k = 0.3 and K = 0.000001.
Solution
This system is a typical stiff system and we can use the built-in functions for stiff equations.
A function can be created to hold the given differential equations:
function dy = biom(t,y,k,K)
% y(1): B, y(2): S
dy = [k*y(1)*y(2)/(K + y(2));
-0.75*k*y(1)*y(2)/(K + y(2))];
end
Since the concentration of the substrate cannot be zero, we provide reduced error tolerance (10−8) to the solver as an option. The following script employs the ode15s solver to integrate biom and generate a plot of the solution. We can use other solvers such as ode23s, ode23t, or ode23tb to solve the stiff system. The following code generates the plot of change of substrate and biomass concentrations as shown in Figure 2.16
>> tspan = [0 20]; y0 = [0.05 5]; k = 0.3; K = 1e-6;
>> options = odeset('RelTol',1e-8,'AbsTol',[1e-8 1e-8]);
>> [t y] = ode15s(@biom, tspan, y0, options,k,K);
>> plot(t,y(:,1),t,y(:,2),':'), xlabel('t(min)'), ylabel('y(t)')
>> legend('B(t)', 'S(t)','location','best')
–10 0 1 2 3 4 5 6 7
2 4 6 8 10
y (t)
B(t)S(t)
t (min)
12 14 16 18 20
FIGURE 2.16 Change of substrate and biomass concentrations.
Example 2.37: Fluidized Packed Bed Catalytic Reactor6 The irreversible gas phase catalytic reaction
A®B
is to be carried out in a fluidized packed bed reactor. Material and energy balances for this reac- tor yield
dP
dt =Pe- +P H Pg
(
p-P)
dT
d T T H Te T p T H TW W T t = - +
(
-)
+(
-)
dP d
H
A P P K
p g
t =
{
- p(
1+) }
dT d
H
C T T FKP
p T
p p
t =
{ (
-)
+}
K exp
Tp
= ´ æ -
èçç ö
ø÷÷
6 10-4 20 7 1000 .
where
T (°R) is the temperature of the reactant P (atm) is the partial pressure of the reactant
Tp (°R) is the temperature of the reactant at the surface of the catalyst Pp (atm) is the partial pressure of the reactant at the surface of the catalyst K is the rate constant (dimensionless)
τ is time (dimensionless)
and the subscript e is the inlet condition
The parameters and constants used in the model equations are
H T H H T F A
C
g= e = T= W = W = = =
=
320 600 266 67 1 6 720 8000 0 17142
2
, , . , . , , , . ,
005 74. , Pe=0 1.
Solve the differential equations from τ = 0 to 1500 and plot the changes of dependent variables.
Initial conditions are P(0) = 0.1, T(0) = 600, Pp = 0, and Tp = 761.
Solution
First, create the function to hold differential equations:
function dy = catfb(t,y,g)
% y(1): P, y(2): T, y(3): Pp, y(4): Tp
A = g.A; C = g.C; F = g.F; Hg = g.Hg; Ht = g.Ht; Hw = g.Hw;
Pe = g.Pe; Te = g.Te; Tw = g.Tw;
K = 6e-4 * exp(20.7 - 15000/y(4));
dy = [Pe - y(1) + Hg*(y(3) - y(1));
Te - y(2) + Ht*(y(4) - y(2)) + Hw*(Tw - y(2));
Hg*(y(1) - y(3)*(1 + K))/A;
Ht*((y(2) - y(4)) + F*K*y(3))/C];
end
The following script employs the built-in ode15s function for stiff systems to integrate catfb and generate plots of the solutions as shown in Figures 2.17 and 2.18. The parameters and con- stants are fed into the function by using the structure:
>> g.A=0.17142; g.C=205.74; g.F=8000; g.Hg=320; g.Ht=266.67; g.Hw=1.6;
>> g.Pe=0.1; g.Te=600; g.Tw=720;
>> tspan = [0 1500]; Yinit = [0.1 600 0 761];
>> options = odeset('RelTol',1e-8,'AbsTol',[1e-8 1e-8 1e-8 1e-8]);
>> [t Y] = ode15s(@catfb, tspan, Yinit,options,g);
>> figure(1), plot(t,Y(:,1),t,Y(:,3),':'), xlabel('\tau'), ylabel('Press.'), legend('P','P_p')
>> figure(2), plot(t,Y(:,2),t,Y(:,4),':'), xlabel('\tau'), ylabel('Temp.'), legend('T','T_p')
0 500
0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1
1000 1500
τ
Press.
P Pp
FIGURE 2.17 Change of reaction pressure.
0 500
600 650 700 750 800 850 900 950
1000 1500
τ
Temp.
TTp
FIGURE 2.18 Change of reaction temperature.
2.7.3 boundary-ValuE ProblEms
Problems where the value of an unknown function, or its derivative, is constrained at two different points are known as boundary-value problems. MATLAB® provides the built-in function bvp4c to solve boundary-value problems. bvp4c produces a solution that is continuous on [a, b] and has a continuous first derivative there. A simple expression of the syntax for bvp4c is
sol = bvp4c(odefun,bcfun,solinit)
where odefun is a function handle that evaluates the differential equations, bcfun is a function handle that computes the residual in the boundary conditions, and solinit is a structure containing the initial guess for a solution. The procedure to use bvp4c consists of four steps:
Step 1: Transform the set of differential equations into a system of 1st-order differential equations, and create a function to hold the equation system (let’s call it bcprob.m). It can have the form
dy = bcprob(x,y)
For a scalar x and a column vector y, bcprob(x,y) returns a column vector, dy, representing f(x,y).
Step 2: Arrange the boundary conditions in the form of f(y(a), y(b)) = 0 and save it as a function (let’s call it bcval.m). For two-point boundary-value conditions of the form bc(y(a),y(b)), bcfun can have the form
res = bcval(ya,yb)
where ya and yb are column vectors corresponding to y(a) and y(b). The output res is a column vector.
Step 3: Initialize the value of the dependent variable y. We assume y values at several points in [a b] (let’s call it initsol).
Step 4: Using the functions and initial values, call the built-in function bvp4c. The function bvp4c returns a structure (let’s call it result), which has several fields as shown in Table 2.23.
As an example, we now solve the boundary-value problem defined by
¢¢ + =
( )
=( )
= -y y 0, y 0 0, y 4 2 by using the built-in function bvp4c.7
Step 1: We need to rewrite the differential equation as a system of two 1st-order ordinary differential equations:
¢ = =¢ ¢ = ¢¢= - = - z1 y z2, z2 y y z1
TABLE 2.23
Fields of the Structure Generated by bvp4c
Field Description
result.x Values of the independent variables selected by bvp4c result.y Approximation to y(x) at the mesh points of x result.yp Approximation to y′(x) at the mesh points of x result.stats Computational cost statistics
result.solver bvp4c
where z1 = y and z2 = y′. Create a function to hold these equations (let’s call it zode.m):
function dz = zode(x,z) dz = [z(2); -abs(z(1))];
Step 2: The boundary conditions can be rearranged as y(0) = 0, y(4) + 2 = 0 or z1(0) = 0, z1(4) + 2 = 0. Create a function to hold these conditions (let’s call it zobc.m):
function res = zobc(za,zb) res = [za(1); zb(1)+2];
Step 3: The range of the independent variable x is x = [a b] = [0 4]. Divide this range into 10 subintervals and initialize the values of dependent variable y at these points. Let z1(x) = y(x) = 1 and z2(x) = y′(x) = 0 (i.e., [z1 z2] = [1 0]). These conditions can be fed into the func- tion bvpinit as
zinit = bvpinit(linspace(0,4,10),[1 0]);
Step 4: Solve the problem using bvp4c:
sol = bvp4c(@zode,@zobc,zinit);
The structure sol returned by bvp4c has the following fields:
sol =
solver: 'bvp4c'
x: [1x20 double]
y: [2x20 double]
yp: [2x20 double]
stats: [1x1 struct]
We can evaluate the numerical solution at 100 equally spaced points and plot y(x) as
x=linspace(0,4,100); y=deval(sol,x); plot(x,y(1,:)), xlabel('x'), ylabel('y') The plot of solutions of two-point BVP is shown in Figure 2.19.
0 0.5 1 1.5 2
x
2.5 3 3.5 4
–2 –1.5 –1 –0.5 0
y
0.5 1 1.5 2 2.5
FIGURE 2.19 Plot of solutions of a two-point BVP.
Example 2.38: Boundary-Value Problem
Solve the equation
x d y dx y
2 2
2-6 =0
The range of x is 1 ≤x ≤ 2, and the boundary conditions are y(1) = 1, y(2) = 1.
Solution
The differential equation can be rearranged as dz
dx z dz dx
z x
1 2 2 1
2
= , =6
where z1 = y and z2 = dy/dx. We can see that z1(1) = y(1) = 1, z1(2) = y(2) = 1.
Step 1: Create a function that holds differential equations z1¢ =z2 and z¢ =2 6 / (let’s call it z x1
bcprob).
function deset = bcprob(x,z) deset = [z(2); 6*z(1)./x.^2];
end
Step 2: The boundary conditions y(1) = 1 and y(2) = 1 can be rewritten as y(1) − 1 = 0 and y(2) − 1 = 0 or z1(1) − 1 = 0 and z2(1) − 1 = 0. Denote the boundary condition at a = 1 as z1(1) = z1(a) = za and the condition at b = 2 as z1(2) = z1(b) = zb. We can create the function that holds these boundary conditions as
function bcset = bcval(za,zb) bcset = [za(1)-1; zb(1)-1];
end
Step 3: Suppose that the interval [a b] = [1 2] is divided into 10 subintervals and that the value of the dependent variable y is constant as 1 at all subinterval points. Since z1(x) = 1 and z x2
( )
= ¢ =z1 0 at the interval [1 2], the initial guesses can be written as [z1(x) z2(x)] = [1 0]:>> initsol = bvpinit(linspace(1,2,10), [1 0]);
Step 4: Solve the equation by calling the function bvp4c as
>> res = bvp4c(@bcprob, @bcval, initsol);
The following scripts show the procedure and generate the plot shown in Figure 2.20:
>> initsol = bvpinit(linspace(1,2,10), [1 0]);
>> res = bvp4c(@bcprob, @bcval, initsol);
>> x = linspace(1,2); y = deval(res,x);
>> plot(x,y(1,:)), xlabel('x'), ylabel('y')
deval(res,x) evaluates values of the solution structure res at x. The structure res returned by bvp4c has the following fields:
>> res res =
solver: 'bvp4c'
x: [1 1.1111 1.2222 1.3333 1.4444 1.5556 1.6667 1.7778 1.8889 2]
y: [2x10 double]
yp: [2x10 double]
stats: [1x1 struct]