Professional Documents
Culture Documents
Odes Bvps
Odes Bvps
methods for
IVPs and
BVPs
Walter
Mudzimbabwe
IVPs
BVPs
Numerical methods for IVPs and BVPs
Walter Mudzimbabwe
Ordinary differential equations
Numerical
methods for
IVPs and
BVPs
Walter
Mudzimbabwe
Numerical
methods for
It is the simplest.
IVPs and
BVPs
Choose step size h and y (x0 ) = y0 to generate y (x1 ), y (x2 ), . . .
Walter
by sequence yi , i = 1, 2, . . .. Note xi = x0 + ih.
Mudzimbabwe
Taylor’s expansion
IVPs 1
BVPs
y (x + h) = y (x) + hy 0 (x) + h2 y 00 (x) + · · ·
2!
Since y 0 (x) = f (x, y ) then y 0 (xi ) = f (xi , yi ), therefore
1
y (x + h) = y (x) + hf (xi , yi ) + h2 fx (xi , yi ) + · · ·
2!
Truncate after the term in h and use notation y (xi ) = yi then
yi+1 = yi + hf (xi , yi ).
The truncation error is O(h2 ):
h2 00
E= y (ξ), ξ ∈ [xi , xi+1 ].
2! i
Example
Numerical
methods for
Apply the Euler’s method to solve the simple equation:
IVPs and
BVPs dy
= x + y , y (0) = 1
Walter
Mudzimbabwe
dx
with h = 0.1 (Exercise: Solve the equation analytically and
IVPs
show that the analytic solution is y = 2e x − x − 1.)
BVPs
Solution:
Here f (xi , yi ) = xi + yi . With h = 0.1, and y0 = 1 so
y1 = y0 + hf (x0 , y0 ) = 1 + 0.1(0 + 1) = 1.1
y2 = y1 + hf (x1 , y1 ) = 1.1 + 0.1(0.1 + 1.1) = 1.220
y3 = y2 + hf (x2 , y2 ) = 1.220 + 0.1(0.2 + 1.220) = 1.362
y4 = y3 + hf (x3 , y3 ) = 1.362 + 0.1(0.3 + 1.362) = 1.528
So
y (0.1) = 1.1, y (0.2) = 1.220, y (0.3) = 1.362, y (0.4) = 1.528.
Midpoint Method
Numerical
methods for
Euler method: yi+1 = yi + hf (xi , yi ).
IVPs and
BVPs
The Euler method assumes that y 0 (xi ) = f (xi , yi ) is the same
Walter
for the whole interval [xi , xi+1 ].
Mudzimbabwe
The midpoint uses Euler method to find y at the midpoint of
IVPs [xi , xi+1 ] ie.,
BVPs h
yi+ 1 = yi + f (xi , yi )
2 2
This is then used to find
y 0 (xi+ 1 ) = f (xi+ 1 , yi+ 1 )
2 2 2
This derivative is then used for the whole interval [xi , xi+1 ].
So the Euler method becames
yi+1 = yi + hy 0 (xi+ 1 ) = yi + hf (xi+ 1 , yi+ 1 ).
2 2 2
Numerical
methods for
Apply the midpoint rule solve the equation:
IVPs and
BVPs dy
= x + y , y (0) = 1
Walter dx
Mudzimbabwe
with h = 0.1.
IVPs Solution:
BVPs
Here f (xi , yi ) = xi + yi . With h = 0.1, and y0 = 1 also
h h
yi+1 = yi + hf (xi+ 1 , yi+ 1 ) = hf (xi + , yi + f (xi , yi )).
2 2 2 2
h h
y1 = y0 + hf (x0 + , y0 + f (x0 , y0 ))
2 2
= 1 + 0.1f (0 + 0.05, 1 + 0.05f (0, 1)) = 1.110
h h
y2 = y1 + hf (x1 + , y1 + f (x1 , y1 ))
2 2
= 1.110 + 0.1f (0.1 + 0.05, 1.110 + 0.05f (0.1, 1.110))
= 1.24205
Error of Midpoint rule
Numerical
methods for
IVPs and
BVPs
Walter
Mudzimbabwe
IVPs
BVPs
The truncation error is O(h3 ):
h3 000
E =− y (ξ), ξ ∈ [xi , xi+1 ].
12 i
Runge-Kutta Methods
Numerical
methods for
IVPs and
The general form of the Runge-Kutta method is:
BVPs
φ = w1 k1 + w2 k2 + · · · + wn kn (3)
where the k’s are constants and the w ’s are weights and
w1 + w2 + · · · + wn = 1
Second order Runge-Kutta Methods
Numerical
methods for
The second order R-K method has the form:
IVPs and
BVPs yi+1 = yi + (w1 k1 + w2 k2 ), (4)
Walter
Mudzimbabwe where
IVPs k1 = hf (xi , yi ) (5)
BVPs h k1
k2 = hf (xi + , yi + ), (6)
2 2
and the weights w1 + w2 = 1.
If w1 = 1, then w2 = 0 and we have Euler’s method.
If w1 = 0, then w2 = 1 we have the mipoint rule:
k1
yi+1 = yi + hf (xi + h2 , yi + 2 ), (7)
If w1 = w2 = 1/2, then we have:
1 h h k1
yi+1 = yi + (k1 +k2 ) = yi + f (xi , yi ) + f xi + , yi + ,
2 2 2 2
called Heun’s method.
Fourth Order Runge-Kutta Methods
Numerical
methods for
IVPs and
The classical fourth order R-K method has the form:
BVPs
Walter
1
Mudzimbabwe yi+1 = yi + (k1 + 2k2 + 2k3 + k4 ), (8)
6
IVPs
where
BVPs
k1 = hf (xi , yi ) (9)
h k1
k2 = hf (xi + , yi + ) (10)
2 2
h k2
k3 = hf (xi + , yi + ) (11)
2 2
k4 = hf (xi + h, yi + k3 ), (12)
Numerical
methods for
Solve
IVPs and
BVPs
y 0 = x + y , y (0) = 1.
Walter using 4th order Runge-Kutta method. Compare your results
Mudzimbabwe
with those obtained from Euler’s method, midpoint method
IVPs
and the actual value. Determine y (0.1) and y (0.2) only.
BVPs
The solution using Runge-Kutta is obtained as follows:
For y1 :
k1 = hf (xi , yi )
= 0.1(0 + 1) = 0.1
h k1
k2 = hf (xi + , yi + )
2 2
0.1 0.1
= 0.1 0+ + 1+ = 0.11
2 2
Example cnt’d
Numerical
methods for
IVPs and
BVPs h k2
k3 = hf xi + , yi +
Walter 2 2
Mudzimbabwe
0.1 0.11
IVPs = 0.1 0+ + 1+ = 0.1105
2 2
BVPs
k4 = hf (xi + h, yi + k3 )
= 0.1((0 + 0.1) + (1 + 0.1105)) = 0.1211
and therefore:
1
y1 = y0 + (0.1 + 2(0.11) + 2(0.1105) + 0.1211) = 1.1103
6
A similar computation yields
1
y2 = 1.1103+ (0.1210+2(0.1321)+2(0.1326)+0.1443 = 1.2428
6
Therefore y (0.1) = 1.1103 and y (0.2) = 1.2428
Comparison of all the methods so far
Numerical
methods for
IVPs and
BVPs
Walter
Mudzimbabwe
IVPs A table for all the approximate solutions using the required
BVPs methods is:
xi Euler Midpoint 4th Order RK Actual value
0.1 1.1000000 1.1100000 1.1103417 1.1103418
0.2 1.2300000 1.2420500 1.2428052 1.2428055
Systems of First Order ODEs
Numerical
methods for So far we have solved a single first order ODE for y (x).
IVPs and
BVPs A nth order system of first order initial value problems can be
Walter expressed in the form:
Mudzimbabwe
IVPs dy1
= f1 (x, y1 , y2 , · · · , yn ), y1 (x0 ) = α1
BVPs dx
dy2
= f2 (x, y1 , y2 , · · · , yn ), y2 (x0 ) = α2
dx
..
.
dyn
= fn (x, y1 , y2 , · · · , yn ), yn (x0 ) = αn ,
dx
All the methods we have seen can used to solve first order
systems of IVPs.
We seek n solutions y1 , y2 , . . . , yn each with an intial condition
yk (xi ); k = 1, . . . , n at the points xi , i = 1, 2. . . ..
4th order Runge-Kutta for systems of ODEs
Numerical
methods for
Consider the system of two equations:
IVPs and
BVPs dy
Walter
= f (x, y , z), y (0) = y0 (13)
Mudzimbabwe
dx
dz
IVPs = g (x, y , z), z(0) = z0 . (14)
dx
BVPs
Let y = y1 , z = y2 , f = f1 , and g = f2 . The fourth order R-K
method would be applied as follows. For each j = 1, 2
corresponding to solutions yj,i , compute
Numerical
methods for
IVPs and So for the system:
BVPs
Walter dy
Mudzimbabwe = f (x, y , z), y (0) = y0 (19)
dx
IVPs
dz
BVPs = g (x, y , z), z(0) = z0 . (20)
dx
we finally have,
1
yi+1 = y1,i+1 = y1,i + (k1,1 + 2k2,1 + 2k3,1 + k4,1 ) (21)
6
1
zi+1 = y2,i+1 = y2,i + (k1,2 + 2k2,2 + 2k3,2 + k4,2 ).(22)
6
Note that we must calculate
k1,1 , k1,2 , k2,1 , k2,2 , k3,1 , k3,2 , k4,1 , k4,2 in that order.
Converting an nth Order ODE to a System of First
Order ODEs
Numerical
methods for
IVPs and
Consider the general second order IVP
BVPs
IVPs If we let
BVPs z = y 0, z 0 = y 00
then the original ODE can now be written as
Numerical
methods for
IVPs and
BVPs
Walter
Mudzimbabwe
A linear second order boundary value problem (BVP) is
(
IVPs
y 00 (x) + p(x)y 0 (x) + q(x)y (x) = r (x)
BVPs
y (a) = α, y (b) = β
Numerical
methods for
Here we solve only a linear second order BVP:
IVPs and (
BVPs
y 00 (x) + p(x)y 0 (x) + q(x)y (x) = r (x)
Walter
Mudzimbabwe y (a) = α, y (b) = β
IVPs
First subdivide [a, b] into N subintervals with size h. So
BVPs
b−a
h= , xi = a + ih, i = 0, 1, · · · , N.
N
The finite difference method for (BVPs) consists of replacing
derivatives in the BVP by difference approximations. For
example:
y (xi+1 ) − y (xi−1 )
y 0 (xi ) ≈
2h
y (x i+1 ) − 2y (xi ) + y (xi−1 )
y 00 (xi ) ≈
h2
Finite difference for (BVPs) ctd
Numerical
methods for
IVPs and
BVPs
Walter
Mudzimbabwe Substituting these approximations in the BVP we get:
IVPs
h h
BVPs 1 − pi yi−1 +(−2+h qi )yi + 1 + pi yi+1 = h2 ri , (25)
2
2 2
where i = 1, 2, · · · , N − 1, y0 = α, yN = β and
Numerical
methods for
IVPs and
BVPs
b1 c1 y1 d 1 − a1 α
Walter
Mudzimbabwe a2 b2 c2 0 y2 d2
IVPs
a3 b3 c3 y3 d3
= ,
.. .. .. .. ..
BVPs
. . .
.
.
0 an−2 bn−2 cn−2 yN−2 dN−2
an−1 bn−1 yN−1 dN−1 − cN−1 β
where
h h
ai = 1 − pi , bi = −2 + h2 qi , ci = 1 + pi , di = h 2 r i ,
2 2
for i = 1, 2, · · · , N − 1.
This is a tridiagonal system using Gaussian elimination, LU
factorisation etc.
Example of BVP using difference approximations
Numerical
methods for
Solve the second order BVP:
IVPs and (
BVPs
y 00 (x) + (x + 1)y 0 (x) − 2y (x) = (1 − x 2 )e −x
Walter ,
Mudzimbabwe y (0) = −1, y (1) = 0
IVPs
BVPs
using h = 0.2. Compare the approximate solution with exact
solution y = (x − 1)e −x .
Solution: Here
Numerical
methods for
IVPs and
BVPs
Walter
Mudzimbabwe
IVPs
The resulting system of equations is
BVPs
−2.08 1.12 0 0 y1 0.91143926
0.86 −2.08 1.14 0 y2 = 0.02252275
0 0.84 −2.08 1.16 y3 0.01404958
0 0 0.82 −2.08 y4 0.00647034
Comparison of exact and difference approximation
Numerical
methods for x Difference solution Exact solution
IVPs and
BVPs 0.0 -1.00000000 -1.0000000
Walter
Mudzimbabwe
0.2 -0.65413043 -0.65498460
0.4 -0.40102860 -0.40219203
IVPs
0.6 -0.21847768 -0.21952465
BVPs
0.8 -0.08924136 -0.08986579
1.0 0.00000000 0.00000000