Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Numerical Solution of Ordinary Differential Equations

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Numerical Solution of

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 %

• Solution is obtained by specifying additional


information
– Initial value problem
– Boundary value problem
Differential Equations
• Many differential equations encountered in
applications, and most nonlinear equations in
particular, are simply too difficult to find
solutions.

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

Solve y’ = y + 2x – x2; y(0) = 1.


Analytical solution:
y = Aex + x2 → y(0) = 1→ y = ex + x2
Numerical solution:
x h=0.5 h=0.1 h=0.02 y(x)
0.5 1.5000 1.7995 1.8778 1.8987
1.0 2.6250 3.4344 3.6578 3.7183
1.5 4.4375 6.1095 6.5975 6.7317
Example
6
analytical
h=0.02
4 h=0.10
h=0.50

0
0 0.5 1 1.5
Midpoint Rule
Tangent line
• Second-order C
B y(x)
method
• Multi-step method A

– uses information y(xn-1) y(xn) y(xn+1)


from more than
one of the xn-1 xn xn+1
preceding points
h h

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

Solution. To get started, carry out ten steps of


Euler’s method with h=0.01. The result of these
steps is the approximate solution 1.11358 at
x=0.1, which we now take as y1.
y 2 = y0 + 2h ⋅ f (x1 , y1 )
= 1 + 2(0.1)(1.11358 + 0.2 − 0.01) = 1.26072
y3 = y1 + 2h ⋅ f (x2 , y 2 ) = 1.43772
Example 2
Solution
x Euler Midpoint Exact
0.1 1.1000 1.11358 1.11517
0.2 1.2290 1.26072 1.26140
0.3 1.38790 1.43772 1.43986
0.4 1.57769 1.65026 1.65182
0.5 1.79946 1.89577 1.89872
2 -order
nd RK method
Also known as improved Euler step

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

k 6 = h ⋅ f (xn + 12 h, y n − 278 k1 + 2k 2 − 3544 k


2565 3
+ 1859
k
4104 4
− 11
k )
40 5
RKF45 method
Then an approximate solution of the IVP is
made using RK-method of order 4:
25 1408 2197 1
yn+1 = yn + k
216 1
+ k
2565 3
+ k
4101 4
− k
5 5
A better approximation is determined using
RK-method of order 5:
~ 16 6656 28561
yn+1 = yn + 135 k1 + 12825 k3 + 56430 k4
9
− 50 k5 + 552 k6
RKF45 method
The optimal step size sh can be determined
by multiplying the scalar s times the
current step size h. The scalar s is
14
⎛ ε ⋅ h ⎞
s = ⎜ ~ ⎟
⎜ 2 y − y ⎟
⎝ n+1 n +1 ⎠
where ε is the specified error control
tolerance
Systems of Differential
Equations
Consider the initial value problem

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.

A numerical solution to Eq. 1 over the


interval a ≤ t ≤ b is found by considering
the differentials
dx = f (t , x, y )dt dy = g (t , x, y )dt
Euler’s Method
The differentials dt = tn+1 - tn, dx = xn+1 - xn
and dy = yn+1 - yn are substituted to get

xn +1 = xn + h ⋅ f (t n , xn , yn )
yn +1 = yn + h ⋅ g (t n , xn , yn )

where tn+1 = tn + h and h = (b - a)/N .


RK4
xn +1 = xn + h ⋅ ( f1 + 2 f 2 + 2 f 3 + f 4 )
1
6

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 )

Over [a, b] with the following conditions:

x(a) = α
x(b) = β

You might also like