Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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 %
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
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
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
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) = β
Boundary Value Problem
Finite difference formulation
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
" 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 !