Ordinary Differential Equations
Ordinary Differential Equations
EQUATIONS
NUMERICAL ANALYSIS
Euler’s method
• The first derivative provides a direct estimate of the slope at xi:
f = f ( xi , yi )
where f(xi, yi) is the differential equation evaluated at xi and yi.
• The formula of Euler’s (or the Euler-Cauchy or the point-slope) method:
yi +1 = yi + f ( xi , yi ) h
Example 1
dy
Use Euler’s method to solve: = f ( x, y ) = -2 x 3 + 12 x 2 - 20 x + 8.5
dx
With a step size of h = 0.5. The initial condition at x = 0 is y = 1.
SOLUTION
• Applying Euler’s method: y ( 0.5) = y ( 0 ) + f ( 0,1) ´ 0.5
• Initial condition: y ( 0 ) = 1
• The slope estimate at x = 0 is: f ( 0,1) = -2 ´ 03 + 12 ´ 02 - 20 ´ 0 + 8.5 = 8.5
• Approximate value using Euler’s method:
y ( 0.5) = y ( 0 ) + f ( 0,1) ´ 0.5 = 1 + 8.5 ´ 0.5 = 5.25
Improvement of Euler’s method
• The slope at the beginning of the interval: yi¢ = f ( xi , yi )
• The slope at the end of the interval:
( )
yi¢+1 = f xi +1 , yi0+1 = f ( xi +1 , yi + f ( xi , yi ) h )
• Formula of an improvement of Euler’s method (Heun’s method):
yi +1 = yi +
(
f ( xi , yi ) + f xi +1 , yi0+1 )h
2
Example 2
Use Heun’s method to solve: y ¢ = 4e0.8 x - 0.5 y
with a step size h = 1 and the initial condition at x = 0 is y = 2.
SOLUTION
• The slope at the beginning of the interval is:
y0¢ = f ( x0 , y0 ) = 4e0.8´0 - 0.5 ´ 2 = 3
Þ y10 = y0 + f ( x0 , y0 ) h = 2 + 3 ´ 1 = 5
• The slope at the end of the interval is:
( )
y1¢ = f x1 , y10 = 4e0.8´1 - 0.5 ´ 5 = 6.402164
• Applying Heun’s method:
y ¢ + y1¢ 3 + 6.402164
y1 = y0 + 0 h =2+ ´ 1 = 6.701082
2 2
Runge-Kutta methods
• Runge-Kutta (RK) methods achieve the accuracy of a Taylor series approach
without requiring the calculation of higher derivatives.
yi +1 = yi + f ( xi , yi , h ) h
where ϕ(xi, yi, h) is called an increment function, which can be interpreted as a
representative slope over the interval.
ì k1 = f ( xi , yi )
ï
ï k2 = f ( xi + p1h, yi + q11k1h )
n
ï
f = å ai ki ; where í k3 = f ( xi + p2 h, yi + q21k1h + q22 k2 h )
i =1 ï !
ï
ïk =
î n (
f xi + pn -1h, yi + qn -1,1k1h + qn -1, 2 k2 h + " + qn -1, n -1kn -1h )
where the a’s, p’s and q’s are constants. Notice that the k’s are recurrence
relationships. That is, k1 appears in the equation for k2, which appears in the
equation for k3, and so forth.
2nd order Runge-Kutta methods
• Heun method with a single corrector (a2 = 1/2):
æ1 1 ö ìïk1 = f ( xi , yi )
yi +1 = yi + ç k1 + k2 ÷ h ; where í
è2 2 ø ïîk2 = f ( xi + h, yi + k1h )
• The midpoint method (a2 = 1):
ìk1 = f ( xi , yi )
ï
yi +1 = yi + k2 h ; where í æ 1 1 ö
ï k 2 = f ç xi + h , yi + k1h ÷
î è 2 2 ø
• Ralston’s method (a2 = 2/3):
ìk1 = f ( xi , yi )
æ 1 2 ö ï
yi +1 = yi + ç k1 + k2 ÷ h ; where í æ 3 3 ö
è3 3 ø k
ï 2 = f ç ix + h , yi + k h
1 ÷
î è 4 4 ø
Example 3
Use the midpoint method to solve:
f ( x, y ) = -2 x 3 + 12 x 2 - 20 x + 8.5
with a step size h = 0.5 and the initial condition at x = 0 is y = 1.
SOLUTION
• Calculating k1 and k2:
k1 = -2 ´ 03 + 12 ´ 02 - 20 ´ 0 + 8.5 = 8.5
k2 = -2 ´ 0.253 + 12 ´ 0.252 - 20 ´ 0.25 + 8.5 = 4.21875
• Applying the midpoint method:
y ( 0.5) = y ( 0 ) + k2 h = 1 + 4.21875 ´ 0.5 = 3.109375
Example 3 (cont.)
• Comparison of the true solution with numerical solutions using three second-
order RK methods:
3rd & 4th order Runge-Kutta methods
• Third-order Runge-Kutta method:
ì k1 = f ( xi , yi )
ï
1 ï æ 1 1 ö
yi +1 = yi + ( k1 + 4k2 + k3 ) h ; where ík2 = f ç xi + h, yi + k1h ÷
6 ï è 2 2 ø
ï k3 = f ( xi + h, yi - k1h + 2k2 h )
î
• Fourth-order Runge-Kutta method:
ì k1 = f ( xi , yi )
ï
ïk = æ 1 1 ö
f ç xi + h, yi + k1h ÷
1 ïï 2 è 2 2 ø
yi +1 = yi + ( k1 + 2k2 + 2k3 + k4 ) h ; where í
6 ïk = æ 1 1 ö
f ç xi + h, yi + k2 h ÷
ï 3 è 2 2 ø
ï
ïî k4 = f ( xi + h, yi + k3h )
Example 4
Use the fourth-order RK method to integrate:
f ( x, y ) = -2 x 3 + 12 x 2 - 20 x + 8.5
Using a step size h = 0.5 and the initial condition at x = 0 is y = 1.
SOLUTION
k1 = -2 ´ 03 + 12 ´ 02 - 20 ´ 0 + 8.5 = 8.5
k2 = k3 = -2 ´ 0.253 + 12 ´ 0.252 - 20 ´ 0.25 + 8.5 = 4.21875
k4 = -2 ´ 0.53 + 12 ´ 0.52 - 20 ´ 0.5 + 8.5 = 1.25
• Applying fourth-order RK method:
1
y ( 0.5) = y ( 0 ) + ( k1 + 2k2 + 2k3 + k4 ) h
6
1
= 1 + (8.5 + 2 ´ 4.21875 + 2 ´ 4.21875 + 1.25) ´ 0.5
6
= 3.21875
Example 5
Use the classical fourth-order RK method to integrate: f ( x, y ) = 4e0.8 x - 0.5 y
using h = 0.5 with y(0) = 2.
SOLUTION
k1 = 4e0.8´0 - 0.5 ´ 2 = 3
k2 = 4e0.8´0.25 - 0.5 ´ 2.75 = 3.510611
k3 = 4e0.8´0.25 - 0.5 ´ 2.877653 = 3.446785
k4 = 4e0.8´0.5 - 0.5 ´ 3.723393 = 4.105602