Numerical Methods
Numerical Methods
Engineering Department
2023-2024 Fall Semester
Numerical Methods
Week-15: Lecture Notes
•Ordinary Differential Equations
•Euler’s method
•Improvements of Euler’s method
•Runge-Kutta methods
•Systems of Equations
•Adaptive Runge-Kutta Methods
1
Ordinary Differential Equations
Part- 7
2
Some ODE Examples:
• Swinging pendulum:
Runge-Kutta Methods
Chapter 25
3
Recall that the method was of the general form:
New value = old value + (slope×step size)
or, in mathematical terms, yi+1 = yi + ϕh.
This formula can be
applied step by step to
compute out into the
future and, hence,
trace out the trajectory
of the solution.
Euler’s Method
• The first derivative provides a direct estimate of the
slope at xi
φ = f ( xi , yi )
where f(xi,yi) is the differential equation evaluated at
xi and yi. This estimate can be substituted into the
equation:
yi +1 = yi + f ( xi , yi )h
• A new value of y is predicted using the slope to
extrapolate linearly over the step size h.
• The Euler method is a first-order numerical procedure
for solving ODEs with a given initial value. It is the
simplest Runge–Kutta method. 8
4
Example:
10
5
Error Analysis for Euler’s Method/
• Numerical solutions of ODEs involves two types of
error:
– Truncation error
• Local truncation error
f ′( xi , yi ) 2
Ea = h
2!
Ea = O ( h 2 )
• Propagated truncation error
– The sum of the two is the total or global truncation error
– Round-off errors
11
6
13
14
7
Heun’s Method/
• One method to improve the estimate of the slope
involves the determination of two derivatives for the
interval:
– At the initial point
– At the end point
• The two derivatives are then averaged to obtain an
improved estimate of the slope for the entire interval.
Predictor : yi0+1 = yi + f ( xi , yi )h
f ( xi , yi ) + f ( xi +1 , yi0+1 )
Corrector : yi +1 = yi + h
2
15
16
8
Heun’s Method is a predictor-corrector method.
• Corrector step can be used more than once to get better estimates
for yi+1.
Predictor: y0i+1= yi+ f(xi, yi) h
Corrector: y1i+1= yi+ [ f(xi, yi) + f(xi+1, y0i+1)]/2 * h
Corrector: y2i+1= yi+ [ f(xi, yi) + f(xi+1, y1i+1)]/2 * h … continue
until the error falls below the tolerance
• If f=f(x) only, than the predictor step is not required. Corrector step
becomes
f ( xi ) + f ( xi +1 )
yi +1 = yi + h
2
Note the similarity between the above formula and the trapezoidal
Rule.
• Heun’s Method is 2nd order accurate. It can obtain exact results
when the solution y(x) is quadratic.
• It has a global error of O(h2) and local error of O(h3).
17
Example:
80
70
60
50
y
40
20
10
0
0 0.5 1 1.5 2 2.5 3 3.5 4
x
M-file: p7_ch25_ex2.m 18
9
Example: Repeat the previous example (Euler Meth.) by applying
Heun’s method. Compare the results.
M-file: p7_ch25_ex3.m
19
10
yi +1 = yi + f ( xi +1/ 2 , yi +1/ 2 )h
21
Graphical depiction of the midpoint method.
M-file: p7_ch25_ex4.m
22
11
Example: Repeat the same example by applying Midpoint
method. Compare the results.
M-file: p7_ch25_ex5.m
23
k3 = f ( xi + p2 h, yi + q21k1h + q22 k2 h)
kn = f ( xi + pn −1h, yi + qn −1k1h + qn −1,2 k2 h + + qn −1,n −1kn −1h)
24
12
• k’s are recurrence functions. Because each k is a functional
evaluation, this recurrence makes RK methods efficient for
computer calculations.
• Various types of RK methods can be devised by employing
different number of terms in the increment function as
specified by n.
• First order RK method with n=1 is in fact Euler’s method.
• Once n is chosen, values of a’s, p’s, and q’s are evaluated by
setting general equation equal to terms in a Taylor series
expansion.
yi +1 = yi + (a1k1 + a2 k 2 )h
k1 = f ( x i , yi )
k 2 = f ( xi + p1h, yi + q11k1h)
25
26
13
• Because we can choose an infinite number of values
for a2, there are an infinite number of second-order RK
methods.
• Every version would yield exactly the same results if
the solution to ODE were quadratic, linear, or a
constant.
• However, they yield different results if the solution is
more complicated (typically the case).
• Three of the most commonly used methods are:
– Heun Method with a Single Corrector (a2=1/2)
– The Midpoint Method (a2=1)
– Ralston’s Method (a2=2/3)
27
28
14
Third Order Runge-Kutta Methods
• For n=3, a derivation similar to the one for the second-order method can be performed.
• It gives: six equations with eight unknowns. Therefore, values for 2 of the unknowns
must be specified a priori. One solution:
• Note that if the derivative is a function of x only, this third-order method reduces to
Simpson’s 1/3 rule.
• 3rd-order RK methods have local and global errors of O(h4) and O(h3), respectively.
29
30
15
Fourth Order Runge-Kutta Methods
31
32
16
Systems of Equations
• Many practical problems in engineering and science
require the solution of a system of simultaneous ordinary
differential equations rather than a single equation:
dy1
= f1 ( x, y1 , y2 , , yn )
dx
dy2
= f 2 ( x, y1 , y2 , , yn )
dx
dyn
= f n ( x, y1 , y2 , , yn )
dx
34
17
Step-Size Control/
• The strategy is to increase the step size if the error is too small
and decrease it if the error is too large. Press et al. (1992) have
suggested the following criterion to accomplish this:
α
Δ new
hnew = h present
Δ present
35
36
18
(a) A bell-shaped forcing function
that induces an abrupt change
in the solution of an ODE.
37
38
19