Numerical Solution of Ordinary Differential Equations
Numerical Solution of Ordinary Differential Equations
Numerical Solution of Ordinary Differential Equations
Ordinary Differential
Equations
Introduction
• Many physical processes may be described by
differential equations
∂2 u 2
2 ∂ u
2
=c
∂t ∂x 2
2
d y ! dy d 2 y d n y $
=g F # x, y, , 2 ..., n & = 0
dt 2
" dx dx dx %
Numerical Solution
• “discretize” the problem and seek the numerical
values of the dependent variable at a discrete set
of values of the independent variable.
Initial Value Problem
Solve
dy
= f (x, y ) , y (a ) = b
dx
Methods:
1. Euler’s Method
2. Midpoint Rule
3. 2nd-order Runge-Kutta Method
4. Runge-Kutta-Fehlberg Method (RKF45)
Euler’s Method
• also known as tangent-line method
• First-order method
yn +1 = yn + h ⋅ f (xn , yn )
where
h = step size
x0 = a, y0 = b, xn = x0 + nh
Example 1
0
0 0.5 1 1.5
Midpoint Rule
Tangent line
• Second-order C
B y(x)
method
• Multi-step method A
yn +1 = yn −1 + (2h ) ⋅ f (xn , yn )
Example 2
Solve y’ = y + 2x – x2; y(0) = 1. using
Midpoint rule with h=0.1
yn+1 = yn + ⋅ (k1 + k2 )
1
2
where
k1 = h ⋅ f (xn , yn )
k 2 = h ⋅ f (xn +1 , yn + k1 )
Example 3
Solve Example 2 using 2nd-order Runge-
Kutta method with h=0.1.
Solution:
n = 0:
( )
k1 = h ⋅ f (x0 , y0 ) = 0.1 ⋅ 1 + 0 − 0 2 = 0.1
[ 2
]
k 2 = h ⋅ f (x1 , y0 + k1 ) = 0.1 ⋅ 1.1 + 2(0.1) − 0.1 = 0.129
y1 = y0 + 12 (k1 + k 2 ) = 1 + 0.5(0.1 + 0.129 ) = 1.1145
Example 3
n = 1:
k1 = h f (x1 , y1 ) = 0.1 f (0.1,1.1145) = 0.13045
k 2 = h f (x2 , y1 + k1 ) = 0.1 f (0.2, 1.1145 + 0.13045) = 0.160495
y2 = y1 + 12 (k1 + k 2 ) = 1.1145 + 0.5(0.13045 + 0.160495 ) = 1.2600
2nd-order
x Euler Midpoint Exact
RK
0.1 1.1000 1.11358 1.114500 1.11517
0.2 1.2290 1.26072 1.259973 1.26140
0.3 1.38790 1.43772 1.437570 1.43986
0.4 1.57769 1.65026 1.648564 1.65182
0.5 1.79946 1.89577 1.894364 1.89872
4 -order
th RK method
yn+1 = yn + ⋅ (k1 + 2k2 + 2k3 + k4 )
1
6
where
k1 = h ⋅ f (xn , yn )
k 2 = h ⋅ f (xn + h, y n +
1
2
) 1
k
2 1
k3 = h ⋅ f (xn + 2 h, yn + 2 k 2 )
1 1
k 4 = h ⋅ f (xn +1 , y n + k3 )
Example 4
Solve Example 2 using 4th-order Runge-
Kutta method with h=0.1.
Example 4
2nd-order 4th-order
x Euler Midpoint Exact
RK RK
0.1 1.1000 1.11358 1.11450 1.11517 1.11517
0.2 1.2290 1.26072 1.25997 1.26140 1.26140
0.3 1.38790 1.43772 1.43757 1.43986 1.43986
0.4 1.57769 1.65026 1.64856 1.65182 1.65182
0.5 1.79946 1.89577 1.89436 1.89872 1.89872
RK-Fehlberg method
Ø Adaptive procedure to determine if the
proper step size h is being used
Ø At each step, two different
approximations are made and compared:
1. If the 2 approximations are in close
agreement, the approximation is accepted
2. If the 2 answers do not agree to a specified
accuracy, the step size is reduced. If the
answers agree to more significant digits than
required, the step size is increased.
RKF45 method
Each step requires the evaluation of the
following six values:
k1 = h ⋅ f (xn , y n )
k 2 = h ⋅ f (xn + 14 h, y n + 14 k1 )
k3 = h ⋅ f (xn + 83 h, y n + 323 k1 + 329 k 2 )
k 4 = h ⋅ f (xn + 12
13
h , y n + 1932
k
2197 1
− 7200
k +
2197 2
7296
2197
k3 )
k5 = h ⋅ f (xn + h, yn + 439
k
216 1
− 8k 2 + 3680 k
513 3
− 845
k )
4104 4
dx dy
= f (t , x, y ) = g (t , x, y )
dt dt
with x(t0)=x0 and y(t0)=y0.
Numerical solution
The solution is a pair of differentiable functions x(t)
and y(t) with the property that when t, x(t) and
y(t) are substituted in f and g, the result is equal
to x’ and y’, respectively.
xn +1 = xn + h ⋅ f (t n , xn , yn )
yn +1 = yn + h ⋅ g (t n , xn , yn )
yn +1 = yn + 16 h ⋅ (g1 + 2 g 2 + 2 g 3 + g 4 )
Where
f1 = f (t n , xn , yn ) g1 = g (t n , xn , yn )
f 2 = f (t n + h2 , xn + h2 f1 , yn + h2 g1 ) g 2 = g (t n + h2 , xn + h2 f1 , yn + h2 g1 )
f 3 = f (t n + h2 , xn + h2 f 2 , yn + h2 g 2 ) g 3 = g (t n + h2 , xn + h2 f 2 , yn + h2 g 2 )
f 4 = f (t n + h, xn + hf 3 , yn + hg3 ) g 4 = g (t n + h, xn + hf 3 , yn + hg3 )
Higher-order D.Eqns.
Consider
m !x!(t ) + c x! (t ) + k x(t ) = g (t )
We can rewrite in the form
!x!(t ) = f (t , x(t ), x! (t ))
With
x(t0 ) = x0 & x! (t0 ) = y0
Higher-order D.Eqns.
The 2nd-order D.E. can be reformulated as a
system of two 1st-order equations if we
use the substitution
x! (t ) = y (t )
Then !x!(t ) = y! (t ) and the equation becomes:
x! (t ) = y x(t0 ) = x0
y! (t ) = f (t , x, y ) y(t0 ) = y0
Example 5
Solve
!x!(t ) + 4 x! (t ) + 5 x(t ) = 0
With x(0) = 3 and x! (0) = −5
Analytical Solution.
−2t −2t
x(t ) = 3e cos t + e sin t
Solution 5
The differential equation has the form
!x!(t ) = f (t , x, x! ) = −4 x! (t ) − 5 x(t )
Which can be reformulated as
x! = y x(0 ) = 3
y! = −4 y − 5 x y (0 ) = −5
Solution 5
Using RK4 method to solve the reformulated
problem over [0, 5] using N=50
(subintervals of width h=0.1.)
x f1 f2 f3 f4 x g1 g2 g3 g4 y exact
0 3 -5 3.000000
0.1 -5.0000 -4.7500 -4.7375 -4.4863 2.5256458 5.0000 5.2500 5.1375 5.3138 -4.4818542 2.525658
0.2 -4.4819 -4.2169 -4.2139 -3.9537 2.1040278 5.2992 5.3598 5.2814 5.2935 -3.9506000 2.104047
0.3 -3.9506 -3.6865 -3.6899 -3.4345 1.7350627 5.2823 5.2135 5.1612 5.0627 -3.4323615 1.735084
0.4 -3.4324 -3.1797 -3.1873 -2.9455 1.4165337 5.0541 4.9014 4.8688 4.7003 -2.9441160 1.416555
0.5 -2.9441 -2.7094 -2.7196 -2.4968 1.1448851 4.6938 4.4911 4.4729 4.2644 -2.4960126 1.144905
0.6 -2.4960 -2.2830 -2.2944 -2.0936 0.9158095 4.2596 4.0317 4.0240 3.7972 -2.0932070 0.915826
0.7 -2.0932 -1.9035 -1.9153 -1.7374 0.7246723 3.7938 3.5583 3.5580 3.3282 -1.7372961 0.724685
0.8 -1.7373 -1.5710 -1.5825 -1.4273 0.5668100 3.3258 3.0950 3.0996 2.8773 -1.4274260 0.566820
0.9 -1.4274 -1.2836 -1.2946 -1.1609 0.4377309 2.8757 2.6574 2.6651 2.4569 -1.1611345 0.437737
1 -1.1611 -1.0383 -1.0484 -0.9347 0.3332430 2.4559 2.2550 2.2645 2.0743 -0.9349829 0.333247
Boundary Value Problem
Consider the linear equation
x (t ) = p (t ) x (t ) + q (t ) x (t ) + r (t )
x(a) = α
x(b) = β