Sei sulla pagina 1di 38

Numerical Solution of

Ordinary Differential
Equations
Introduction
•  Many physical processes may be described by
differential equations

d2y ∂2 u 2
2 ∂ u
! dy d 2 y d n y $
2
= −g 2
=c F # x, y, , 2 ..., n & = 0
dt ∂t ∂x 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. Runge-Kutta Methods
4. Adaptive RK Method (RKF45)
Euler’s Method
•  Also known as tangent-line method
•  First-order method

yn +1 = yn + h ⋅ f (xn , yn )
where y(x)

h = step size
x0 = a, y0 = b, xn = x0 + nh

x

x0
x1
x2

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

Analytical solution: y = ex + x2
Example 1

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

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
A

y(xn+1)
•  Multi-step method y(xn-1) y(xn)
– uses information
from more than one xn-1 xn xn+1
of the preceding
h h
points

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 (Heuns’
method)

yn+1 = yn + h ( k1 + k2 )
1
2
1
2

where
k1 = f ( xn , yn )
k2 = f ( xn+1, yn + hk1 )
Example 3
Solve Example 2 using 2nd-order Runge-
Kutta method with h=0.1.

Solution:
n=0:
k1 = f ( x0 , y0 ) = 1+ 0 − 0 2 = 1
k2 = f ( x1, y0 + hk1 ) = 1.1+ 2 ( 0.1) − 0.12 = 1.29
y1 = y0 + h ( 12 k1 + 12 k2 ) = 1+ 0.1* 12 (1+1.29 ) = 1.1145
n = 1:
Example 3
k1 = f ( x1, y1 ) = f ( 0.1,1.1145) = 1.3045
k2 = f ( x2 , y1 + hk1 ) = f ( 0.2, 1.1145 + 0.13045) = 1.60495
y2 = y1 + h ( 12 k1 + 12 k2 ) = 1.1145 + 0.1* 0.5 (1.3045 +1.60495) = 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 + h ( k1 + k2 + k3 + k4 )
1
6
1
3
1
3
1
6
where
k1 = f ( xn , yn )
k2 = f ( xn + 12 h, yn + 12 hk1 )
k3 = f ( xn + 12 h, yn + 12 hk2 )
k4 = f ( xn+1, yn + hk3 )
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
Explicit RK Formulas
yn+1 = yn + h ( A1k1 + A2 k2 +…+ Am km )
where
k1 = f ( xn , yn )
k2 = f ( xn + a2 h, yn + hb21k1 )
k3 = f ( xn + a3h, yn + h ( b31k1 + b32 k2 ))
!
km = f ( xn + am h, yn + h ( bm1k1 + bm2 k2 …+ bm,m−1km−1 ))
Explicit RK Formulas

Engelen-Mullges and Uhlig, Numerical Algorithms with C, Springer, © 1996.


Explicit RK Formulas

Engelen-Mullges and Uhlig, Numerical Algorithms with C, Springer, © 1996.


RK-Fehlberg method
Embedding Formula

Ø  Adaptive procedure to determine if the


proper step size h is being used

Ø  At each step, two different


approximations are made and compared:
²  If the 2 approximations are in close agreement, the
approximation is accepted
²  If they 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
yn+1 = yn + 135 6656
k1 + 12825 28561
k3 + 56430 k4
− 509 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) = β
Boundary Value Problem
Finite difference formulation

1.  Form a partition [a, b] using the points a = t0 < t1


< … <tN = b, where h = (b - a) / N and tj = a + jh
for j = 0, 1, …, N.

2.  Replace each term with finite difference


approximations
x (t j+1 ) − x (t j−1 ) x (t j+1 ) − 2x (t j ) + x (t j−1 )
x! (t ) = !!
x (t ) =
2h h2
Boundary Value Problem
Finite difference formulation

3. Introduce the notation qj = q(tj), pj = p(tj), and rj =


r(tj) then substitute approximation to the equation
x j+1 − 2x j + x j−1 " x j+1 − x j−1 %
2
= pj $ ' + q j x j + rj
h # 2h &

4. Rearranging terms
" −h % "h %
' j−1 ( j) j
2 2
$ p j −1 x + 2 + h q x + p −1
$ j ' j+1x = −h rj
# 2 & #2 &
for j = 1, 2, …, N-1, x0 = α and xN = β.
Boundary Value Problem
Finite difference formulation

5. The equation is a tridiagonal system

" 2 %( , ( ,
$ 2 + h q1 h2 p1 −1 '* x1 "h %
* ** −h 2 r1 + $ p1 +1'α *
$ − h p −1 2 + h 2 q '* x #2 & *
h
p2 −1 O * * *
$ 2 2 2 2
'* 2 * *
2
−h r2 *
$ ! '* ! ** ** ! *
$ − h2 p j −1 2 + h 2 q j h
p −1 '*) x - =) −h 2 rj
*
-
2 j j
$ '* * * *
$ ! '* ! !
* ** 2
*
*
$ ' −h rN−2
$ O − h2 pN−2 −1 2 + h 2 qN−2 h2 pN−2 −1 '* x N−2 * * *
* * * 2 " −h % *
$ −h rN−1 + $ pN−1 +1' β
# − h2 pN−1 −1 2 + h 2 qN−1 '&*+ x N−1 *. *+ # 2 & *.
BVP Example
Solve the boundary value problem

2t 2
!!
x (t ) = 2
x! (t ) − 2
x (t ) +1
1+ t 1+ t
with x(0) = 1.25 and x(4) = -0.95 over the interval
[0, 4].

Analytical solution:
x (t ) = 1.25 + 0.486089652t − 2.25t 2 + 2t arctan (t ) − 12 ln (1+ t 2 ) + 12 t 2 ln (1+ t 2 )
BVP Example
Numerical Solution
h=0.20 h=0.10 h=0.05 h=0.025 h=0.20 h=0.10 h=0.05 h=0.025
t x1 x2 x3 x4 x.exact e1 e2 e3 e4
0 1.25 1.25 1.25 1.25 1.25 0 0 0 0
0.2 1.314503 1.316645 1.317174 1.317306 1.31735 0.002847 0.000705 0.000176 4.42E-05
0.4 1.320607 1.325045 1.326141 1.326414 1.326505 0.005898 0.00146 0.000364 9.06E-05
0.6 1.272755 1.279533 1.281206 1.281623 1.281762 0.009007 0.002229 0.000556 0.000139
0.8 1.177399 1.186438 1.18867 1.189227 1.189412 0.012013 0.002974 0.000742 0.000185
1 1.042106 1.053226 1.055973 1.056658 1.056886 0.01478 0.00366 0.000913 0.000228
1.2 0.874878 0.887823 0.891023 0.891821 0.892086 0.017208 0.004263 0.001063 0.000265
1.4 0.683712 0.698181 0.701758 0.70265 0.702948 0.019236 0.004767 0.00119 0.000298
1.6 0.476372 0.492027 0.4959 0.496865 0.497187 0.020815 0.00516 0.001287 0.000322
1.8 0.260264 0.276749 0.280828 0.281846 0.282184 0.02192 0.005435 0.001356 0.000338
2 0.042398 0.059343 0.063537 0.064583 0.064931 0.022533 0.005588 0.001394 0.000348
2.2 -0.170616 -0.153592 -0.149378 -0.148327 -0.147977 0.022639 0.005615 0.001401 0.00035
2.4 -0.372557 -0.355841 -0.351702 -0.350669 -0.350325 0.022232 0.005516 0.001377 0.000344
2.6 -0.557565 -0.541546 -0.53758 -0.53659 -0.536261 0.021304 0.005285 0.001319 0.000329
2.8 -0.720114 -0.705188 -0.701492 -0.70057 -0.700262 0.019852 0.004926 0.00123 0.000308
3 -0.854988 -0.841551 -0.838223 -0.837393 -0.837116 0.017872 0.004435 0.001107 0.000277
3.2 -0.95725 -0.9457 -0.942839 -0.942125 -0.941888 0.015362 0.003812 0.000951 0.000237
3.4 -1.022221 -1.012958 -1.010663 -1.01009 -1.009899 0.012322 0.003059 0.000764 0.000191
3.6 -1.045457 -1.03888 -1.03725 -1.036844 -1.036708 0.008749 0.002172 0.000542 0.000136
3.8 -1.022727 -1.019238 -1.018373 -1.018158 -1.018086 0.004641 0.001152 0.000287 7.21E-05
4 -0.95 -0.95 -0.95 -0.95 -0.95 -2.23E-09 -2.23E-09 -2.23E-09 -2.23E-09 !

Potrebbero piacerti anche