Numerical Methods Notes
Numerical Methods Notes
In this course there are many cases where you will need to find the solution of a differential
equation. Previously you will have come across simple differential equations such as;
dy
= y ' = - k r × y 2 (1)
dx
These simple equations can be solved easily.
dy
= y ' = - k r × y 2
dx
dy
= - kr × dx
y 2
y 1
1 x 1
òy 2
dy = - kr ò dx
x 0
y 0
which has the solution
1 1
- = kr × ( x1 - x 0 ) (2)
y1 y0
Therefore given any four pieces of information (e.g. y0, y1 and k and x0) we can determine the fifth
(e.g. x1). As we increase the complexity of the system direct integration becomes problematic and
alternative methods are necessary to solve such problems.
Numerical methods for integration
As stated above the evaluation of a definite integral such as
b
by formal methods is often difficult or impossible, even if the function f(x) has a relatively simple
analytical form. For such cases, and for the more general integration problem where we have a few
values of f(x) available at distinct base‐points arguments xi, i=0, 1,…,n, some other approach is
required. An obvious alternative is to find a function g(x) that is both a suitable approximation of f(x)
and simple to integrate formally. Then Eq. (3) can be estimated as
b
One possibility is to apply interpolation polynomials to the known points of the function, and then
carry out the integration of the polynomials. In the figure below, such a procedure is illustrated.
Here, the function f(x) is shown by the solid curve, and some discrete points at equally spaced
intervals are indicated by dots. Now, if we apply a polynomial to the discrete points, we may come
up with an approximation of the function f(x) indicated by the dashed curve.
Note that even though the polynomial fits exactly at the 5 discrete points, the area under the dashed
line will not be the same as the area under the solid line. However, often a positive error in one
segment may cancel a negative error in another segment, so that the total error may be small. This is
why integration often is called a smoothing process. Text books on numerical integration often focus
on formulas for approximation. This is not surprising since there are so many possibilities for
selecting the degree of interpolation polynomial, spacing between points, and location of points.
Normally, we classify the methods into two groups, one that uses equally spaced points (Newton‐
Cotes formulas), and another that uses unequally spaced points (Gaussian quadrature formulas). We
will consider some of the simplest and most common methods in the first group, namely the
trapezoidal method and the Simpson’s formula.
Trapezoidal formula
The method is illustrated in the figure below. Here, the exact function, f(x), is indicated by the solid
line, and the first order polynomial approximation (straight line) is indicated by the dashed line,
passing through two points on the function, at x0 and x1.
The equation for a straight line is of course
y = mx + C
f ( x1 ) - f ( x 0 )
f ( x0 ) = × x0 + C
x1 - x0
f ( x1 ) - f ( x 0 )
\ C = f ( x0 ) - × x 0
x1 - x0
æ f ( x1 ) - f ( x0 ) ö é f ( x1 ) - f ( x 0 ) ù
p1 ( x) = ç ÷ × x + ê f ( x0 ) - × x 0 ú
è x1 - x0 ø ë x1 - x0 û
Which simplifies to;
( x - x 0 )
p1 ( x) = f ( x0 ) + [ f ( x1 ) - f ( x o ) ] (5)
( x1 - x0 )
Integration of this approximation of the curve may then be carried out
x x
b 1
ìï
1
( x - x 0 ) üï
òa f ( x ) dx » p
òx 1 ( x ) dx = ò í f ( x0 ) + [ f ( x1 ) - f ( xo ) ] ý dx
0 x î
0
ï ( x1 - x
0 ) ïþ (6)
1
= ( x1 - x0 ) [ f ( x1 ) + f ( x0 ) ]
2
Now, consider the integration over a series of intervals, as illustrated with 5 intervals in the figure
below. The function is approximated with straight lines between pairs of points.
We may apply Eq. (6) to each sub‐interval, and take the sum of the sub‐areas to get the total area
under the function from a to b:
1
Ai = ( xi - xi-1 ) [ f ( xi ) + f ( xi -1 ) ] (7)
2
b n
1 n
ò f ( x )dx » å Ai = i =1
å ( xi - xi-1 ) [ f ( xi ) + f ( xi -1 ) ]
2 i =1
(8)
a
Simpson’s formula
The Simpson’s formula may then be derived as:
a + 2 h
h
ò f ( x) dx » [ f (a ) + 4 f (a + h) + f (a + 2h) ] (9)
a 3
For an integration interval [a,b], subdivided into n intervals, as illustrated with 6 intervals in the
figure below, a parabola is fitted to pairs of intervals.
The total integral over the 6 intervals, by application of Eq. (9) three times, is
b
h
ò f ( x)dx » 3 [ f (a) + 4 f ( x ) + f ( x ) ]
a
1 2
h
+ [ f ( x2 ) + 4 f ( x3 ) + f ( x 4 ) ]
3
h
+ [ f ( x4 ) + 4 f ( x5 ) + f (b ) ]
3
h
= é f (a ) + 4 ( f ( x1 ) + f ( x3 ) + f ( x5 ) ) + 2 ( f ( x2 ) + f ( x4 ) ) + f (b) ùû
3 ë
In general, for n intervals, Simpson’s formula becomes:
ò f ( x ) dx » ê
3 ëê +2 ( f ( a + 2 h) + ... + f (b - 2 h ) ) + f (b )
ú
a ûú (10)
where the interval length h=(b‐a)/(n).
Example:
dx
= -0.3 × x 2
dt
Solution:
1
\ f ( x ) =
-0.3 x 2
You can now solve this equation for 7 steps or 6 intervals as follows
From Eq (2) the correct answer is 3 which indicates a rather large error in the solution. You should
notice in the solutions for f(x) the large difference in the final two values i.e. those at x5 and x6. This
is plotted in the accompanying Excel file (integration.xlsx). If this problem was solved between 10
and 4 the correct solution would yield 0.5 while the Trapezoidal and the Simpson’s rule give 0.508
changes. In such cases either the number of steps can be increased or the problem can be split e.g.
integrate from 10 to 4 and then from 4 to 1.
Numerical methods for differentiation
Apart from integration we will often need to solve differential equations such as;
dy
= y ' = f ( x, y )
dx
One of the simplest methods for approximating a solution for such problems is shown in the figure
below;
p 3 (x)
(x 0 ,y 0 )
(x 1 ,y 1 )
(x 2 ,y 2 )
y=f(x)
Slope =
Dy ( y1 - y 0 )
=
Dx ( x1 - x0 )
(x 3 ,y 3 )
(x 3 ,y true )
x
dC A
= - kr C A
dt
An approximation of the above curve is also shown whereby we have used the slope at position
(x0,y0) to find (x1,y1). We can then use this point to recalculate the slope and find (x2,y2). This
procedure can be repeated until we find the final position i.e. (x3,y3). Therefore, if we know the initial
conditions we can move forward in finite difference steps until the final condition is reached. The
final value of y, i.e. y3 however is different from the real value of y at this x position.
The slope at position (x0,y0) is determined from the rate equation (as rate = slope)
dy ( y - y )
= f ( x0 , y 0 ) = 1 0
dx ( x1 - x0 ) (11)
y1 = y0 + f ( x0 , y0 ) × ( x1 - x0 )
Rewriting to a more general form gives us
dy
= y ' = f ( x, y )
dx
y ( x0 ) = y0
1
yi +1 = yi + ( Dy1 + 2Dy2 + 2 Dy3 + Dy 4 )
6
xi +1 = xi + D x (13)
Where
Dy1 = Dx × f ( xi , y i )
æ Dx Dy ö
Dy2 = Dx × f ç xi + , y i + 1 ÷
è 2 2 ø
æ Dx Dy ö (14)
Dy3 = Dx × f ç xi + , y i + 2 ÷
è 2 2 ø
Dy4 = Dx × f ( xi + Dx, yi + Dy3 )
Thus in this case, the next value (yi+1) is determined by the present value (yi) plus the product of the
size of the interval (Δx) and a weighted average of slope:
· Δy1 is the slope at the beginning of the interval;
· Δy2 is the slope at the midpoint of the interval, using slope Δy1;
· Δy3 is again the slope at the midpoint, but now using the slope Δy2 to determine the y‐value;
· Δy4 is the slope at the end of the interval, with its y‐value determined using Δy3.
Example:
For the rate equation;
dy
= -0.3 × y 2
dx
Euler’s method
Eq. 12 becomes;
Solving this gives the following results;
I X yi yi+1
0 0 10 4
1 0.2 4 3.04
2 0.4 3.04 2.485504
3 0.6 2.485504 2.11484
Runge‐Kutta
(
Dy1 = 0.2 × -0.3 × y n 2 )
2
æ æ Dy 1 ö ö
Dy2 = 0.2 × ç -0.3 × ç y n + ÷ ÷
ç è 2 ø ÷ø
è
2
æ æ Dy 2 ö ö
Dy3 = 0.2 × ç -0.3 × ç y n + ÷ ÷÷
ç è 2 ø ø
è
(
Dy4 = 0.2 × -0.3 × ( yn + D y3 )
2
)
and
1
yn +1 = yn + ( Dy1 + 2Dy2 + 2 Dy3 + Dy 4 )
6
xn +1 = xn + 0.2
The above procedure can then be applied to the problem which yields the following results.
Comparison of each method
12
10
8 Real
RK
6
y
Simple
4
2
0
0 0.5 1 1.5 2 2.5 3
t
Extension of numerical methods for solving systems of ordinary differential equations.
Consider the sequential reaction
A ® B ® C
If each of these reactions was elementary then we could state that the following three rate
equations apply.
dA
= - k1 × A
dt
dB
= k1 × A - k2 × B
dt
dC
= k2 × B
dt
Hence three equations are used to describe the system and each of these must be determined
simultaneously to solve the problem. This is a series of ordinary differential equations (ODEs) and in
this section we will show how the numerical methods described above can be used in such cases.
For the general case we will consider two coupled ODE’s;
dy
= y ' = f ( x, y , z )
dx
dz
= z ' = g ( x, y , z )
dx (16)
These equations are solved simultaneously using the current values of x, y and z to generate the
solution at the next interval.
Similarly the Runge‐Kutta method extends to;
1
yi +1 = yi + ( Dy1 + 2Dy2 + 2 Dy3 + Dy 4 )
6
1
zi +1 = zi + ( Dz1 + 2Dz 2 + 2 Dz3 + Dz 4 )
6
xi +1 = xi + D x (18)
where
Dy1 = Dx × f ( xi , yi , z i )
Dz1 = Dx × g ( xi , yi , z i )
æ Dx Dy Dz ö
Dy2 = Dx × f ç xi + , yi + 1 , z i + 1 ÷
è 2 2 2 ø
æ Dx Dy Dz ö
Dz 2 = Dx × g ç xi + , yi + 1 , z i + 1 ÷
è 2 2 2 ø
æ Dx Dy Dz ö
Dy3 = Dx × f ç xi + , yi + 2 , z i + 2 ÷
è 2 2 2 ø
æ Dx Dy Dz ö
Dz3 = Dx × g ç xi + , yi + 2 , z i + 2 ÷
è 2 2 2 ø
Consider the following system of two coupled ODE’s
dy
= y ' = z, y (0) = y 0 = 0
dx
dz
= z ' = 4 y, z (0) = z 0 = 50
dx
Runge‐Kutta method becomes
1
yi +1 = yi + ( Dy1 + 2Dy2 + 2 Dy3 + Dy 4 )
6
1
zi +1 = zi + ( Dz1 + 2Dz 2 + 2 Dz3 + Dz 4 )
6
xi +1 = xi + D x
where
Dy1 = Dx × z
Dz1 = Dx × 4 × y
æ Dz ö
Dy2 = Dx × ç z i + 1 ÷
è 2 ø
æ Dy ö
Dz2 = Dx × 4 × ç y i + 1 ÷
è 2 ø
æ Dz ö
Dy3 = Dx × ç z i + 2 ÷
è 2 ø
æ Dy ö
Dz3 = Dx × 4 × ç y i + 2 ÷
è 2 ø
Euler’s Method
i X yi zi
0 0 0 50
1 0.25 12.5 50
2 0.5 25 62.5
3 0.75 40.625 87.5
4 1 62.5 128.125
Runge‐Kutta
i X yi zi
0 0 0 50
1 0.25 13.02083 56.38021
2 0.5 29.36469 77.13793
3 0.75 53.19975 117.5693
4 1 90.60526 187.988
Again the Runge‐Kutta method gives the more correct result.
Solving ODE’s in Matlab
% Simple example of using ODE45
function [C]=ODEexample;
xspan = [0:0.25:1]; % x increment
C0 = [0;50]; % initial vlaues
[x,C]=ode45(@f,xspan,C0); % solve the problem
plot(x,C(:,1),'r',x,C(:,2),'b'); % plot the solution
%
function dydx = f(x,C)
dydx = [C(2)
4*C(1)]; % individual rate equations
%
You can see the three input arguments i.e.
· xspan = [0:0.25:1]; i.e. (0, 0.25, 5, 0.75, 1)
· C0 = [0; 50]; i.e. y0, z0
· dydx = [C(2) ; 4*C(1)]; i.e. z and 4y
More information on using ode45 can be found in the file “usingode45.pdf”.
Consider the following second order ODE and initial conditions;
d 2 y dy
2
+ A( x) + B ( x) y + C ( x ) = 0
dx dx
Where y(x0) = a
dy
= b
dx x 0
In the above equation we can make the following substitution:
dy
z =
dx
Then the differential becomes
dz
+ A( x ) z + B ( x ) y + C ( x ) = 0
dx
Rearranging both equations, we obtain
dz
= - A( x ) z - B ( x) y - C ( x )
dx
dy
= z
dx
Now the second order ODE has been converted into a set of two coupled first order ODE’s which can
be integrated using the previously described methods.
Example:
The differential equations which describe the trajectory of an object are:
2 2
d 2 x dx æ dx ö æ dy ö
m 2 = - k ç ÷ +ç ÷
dt dt è dt ø è dt ø
2 2
d 2 y dy æ dx ö æ dy ö
m 2 = - k ç ÷ + ç ÷ - ma g
dt dt è dt ø è dt ø
Using the initial conditions;
ì x = 0
ï y = 3( m )
ï
ï
t = 0 í dx = V0 cos q (ms -1 )
ï dt
ï dy -1
ï = V0 sin q (ms )
î dt
Convert the trajectory equations into a system of first order ODE’s
Solution
Let
dx
z 1 =
dt
dy
z 2 =
dt
Then the equations become
dz 1 k 2 2
= - z1 ( z1 ) + ( z 2 )
dt m
dz 2 k 2 2
= - z2 ( z1 ) + ( z2 ) - ma g
dt m
dx
= z 1
dt
dy
= z 2
dt
Using the initial conditions would allow us to solve the problem.
Initial value Partial differential equations
Linear initial value partial differential equations are often encountered in chemical engineering
problems. These equations are of the form;
¶ 2Y ¶ 2Y ¶ 2 Y æ ¶Y ¶Y ö
A 2
+ B + C 2
= f ç x, y , Y , , ÷ (20)
¶x ¶x¶y ¶y è ¶x ¶y ø
For example consider the temperature distribution through a solid wall. Initially the wall has a
uniform temperature equal to that of the outside environment, T0. Then at some time, the inside
wall is raised to Ti. Over time a temperature distribution through the wall is reached as shown in the
figure below.
Temp profile
T i
@ t = ¥
Temp profile
@ 0 < t < ¥
T 0
0 L
x
The energy balance on a differential element inside the wall is given as;
¶T ¶ 2 T
r C p = k 2 (21)
¶t ¶ x
and the boundary conditions are given by
Note that the temperature is a function of time (t) and the spatial location (x); i.e., there are two
independent variables. Therefore, the differential equation is a partial differential equation.
Several methods are available for solving such problems. Here we will use one of the simplest which
is known as the explicit method.
f(x i+1 )
f(x i )
f(x i‐1 )
Δx Δx
The second derivative can then be calculated as;
f ( xi ) - 2 f ( xi +1 ) + f ( x i + 2 )
f ''( x i ) = (23)
D x 2
We will not go into the proof here but higher orders of Δx approximations are more accurate. In this
case the central derivatives become;
f ( xi +1 ) - f ( x i -1 )
f '( x i ) =
2 D x
f ( xi +1 ) - 2 f ( xi ) + f ( x i -1 )
f ''( x i ) = (24)
D x 2
Using the above we can represent the problem
¶T k ¶ 2 T
= as a two dimensional grid with finite Δx and Δt axis as shown.
¶t r C p ¶ x 2
Node values along
here are unknown
Node values along
here are known
Δt j th location
Δx
i th location
Specified initial conditions
Specified boundary conditions
The lines along the x axis represent x0, x1, …, xi, xi+1 etc, while those along the t axis are t0, t1, …, tj, tj+1
etc. Each node (i.e. the points where the lines cross) represent a temperature at that point i.e. Ti,j.
The points Ti‐1,j , Ti,j , Ti+1,j and Ti,j+1 are also marked in the above figure.
And the first order time derivative is given by the forward difference approximation;
Putting these approximations into our equation gives
Which we can rearrange to give;
k Dt
Ti , j +1 = Ti , j + (T - 2 Ti, j + T i -1, j )
r C p D x 2 i +1, j
(28)
Example:
Consider the above heat transfer problem where
¶T m 2 ¶ 2 T
= 0.005 and
¶t s ¶ x 2
L = 0.5
Determine the temperature profile through the wall at t = 2, 4, 6, 8, and 10 s.
Solution:
0.005 m 2 Dt
Ti , j +1 = Ti , j +
s D x 2
(Ti +1, j - 2 Ti, j + T i-1, j )
Using the initial conditions we can set up an Excel spreadsheet to solve the equation for each of the
times specified.
Δx 0.0625
Δt 0.25
x
Time 0 0.0625 0.125 0.1875 0.25 0.3125 0.375 0.4375 0.5
0 0 0 0 0 0 0 0 0 25
0.25 0 0 0 0 0 0 0 8 25
0.5 0 0 0 0 0 0 2.56 10.88 25
0.75 0 0 0 0 0 0.8192 4.4032 12.736 25
1 0 0 0 0 0.2621 1.7039 5.9228 13.994 25
1.25 0 0 0 0.0839 0.6396 2.5926 7.1555 14.933 25
1.5 0 0 0.0268 0.2349 1.0867 3.4278 8.1842 15.666 25
1.75 0 0.0086 0.0848 0.4409 1.5633 4.2007 9.0562 16.259 25
2 0 0.0302 0.1744 0.6861 2.0481 4.9105 9.8072 16.751 25
Plotting this data for the times specified gives;
25
t = 0 s
t = 2 s
20
t = 4 s
Temperature
t = 6 s
t = 8 s
15
t = 10 s
10
5
0
0.0 0.1 0.2 0.3 0.4 0.5
Distance
qconv = – hA(T‐Ta),
where h is the convective cooling coefficient and A is the surface area of the mass. The energy E
stored in the mass is
E = mCT,
where C is the specific heat. From an energy balance, the rate of change of E must equal the rate of
cooling due to convection (qconv). Thus
dT hA
= - (T - T a ) , T(0) = T0
dt mC
Consider a sphere of radius r =1.0 cm made of an alloy for which ρ = 3000 kg/m 3 and C = 1000 J/(kg
K). If h = 500 J/(s m 2 K), T(0) = 500 °C and Ta = 50 °C, calculate T(t) for t = 0 to 10 s.
Q2. For heat loss by radiation the following equation applies;
dT A es 4
dt
=-
mC
(
T - T a 4 , ) T(0) = T0
Q3. The annual population of two species competing for the same food supply can be modelled by
the pair of ODEs
dN 1
= N 1 ( A 1 - B 1 N 1 - C 1 N 2 ), N 1 ( 0 ) = N 1 , 0
dt
dN 2
= N 2 ( A 2 - B 2 N 2 - C 2 N 1 ), N 2 ( 0 ) = N 2 , 0
dt
r = k CA 2
the unsteady‐state mole balance on A in the liquid film is given as
¶C A ¶ 2 C A
= DAB - kC A 2
¶ t dx 2
where
dC
x = L = 0
dx
For the following data, determine the concentration profile of A at t = 0.50 seconds:
L = 0.3 cm
CA0 = 1000 mol m ‐3