4th-Order Runge-Kutta Method
4th-Order Runge-Kutta Method
Introduction
1
3/11/2021
Simpson’s rule
f ( x ) dx f ( a )( b − a )
a
y ( 0) = 1
f ( t 0 , y0 )
f ( t0 + h, y0 + hs0 )
y (1) ( t ) = −0.2 y ( t ) − sin ( t ) − 0.1
y ( 0) = 1
2
3/11/2021
• Without justification,
4th-order Runge-Kutta says to proceed as follows:
s0 f ( tk , yk )
s1 f ( tk + 12 h, yk + 12 hs0 )
s2 f ( tk + 12 h, yk + 12 hs1 )
s3 f ( tk + h, yk + hs2 )
s0 + 2s1 + 2s2 + s3
yk +1 yk + h
6
y ( 0) = 1
3
3/11/2021
s0 ← f (0, 1) = –1 y ( 0) = 1
s1 ← f (0.2, 1 + 0.2s0) = –0.8
s2 ← f (0.2, 1 + 0.2s1) = –0.84
s3 ← f (0.4, 1 + 0.4s2) = –0.664
e −0.4 = 0.6703200460356393
s0 + 2s1 + 2s2 + s3
y1 y0 + h
6
= 1 + 0.4
( −1) + 2 ( −0.8 ) + 2 ( −0.84 ) + ( −0.664 )
= 1 + 0.4 ( −0.824 )
6 7
= 0.6704
7
4
3/11/2021
10
10
5
3/11/2021
Implementation
std::tuple<double *, double *, double *> rk4(
double f( double t, double y ), std::pair<double, double> t_rng, double y0,
unsigned int n
) {
double h{ (t_rng.second - t_rng.first)/n };
ts[0] = t_rng.first;
ys[0] = y0;
dys[0] = f( ts[0], ys[0] );
11
0.006737946999085467 12
12
6
3/11/2021
13
Comparison
14
7
3/11/2021
Comparison
y(5) = 0.00682130435141457
Euler 1024 0.006655931188587414 0.00008202 1024
Heun 43 0.006737946999085467 –0.00008336 86
RK4 8 0.006810674597968526 –0.00007273 32
y(5) = 0.1552495456267901
Euler 1024 0.152997481619969 0.002252 1024
Heun 16 0.153866775462848 –0.002317 32
RK4 3 0.153866775462848 0.0013827 12
15
15
Summary
16
16
8
3/11/2021
References
[1] https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods
17
17
Acknowledgments
None so far.
18
18
9
3/11/2021
Colophon
These slides were prepared using the Cambria typeface. Mathematical equations
use Times New Roman, and source code is presented using Consolas.
Mathematical equations are prepared in MathType by Design Science, Inc.
Examples may be formulated and checked using Maple by Maplesoft, Inc.
The photographs of flowers and a monarch butter appearing on the title slide and
accenting the top of each other slide were taken at the Royal Botanical Gardens in
October of 2017 by Douglas Wilhelm Harder. Please see
https://www.rbg.ca/
for more information.
19
19
Disclaimer
These slides are provided for the ECE 204 Numerical methods
course taught at the University of Waterloo. The material in it
reflects the author’s best judgment in light of the information
available to them at the time of preparation. Any reliance on these
course slides by any party for any other purpose are the
responsibility of such parties. The authors accept no responsibility
for damages, if any, suffered by any party as a result of decisions
made or actions based on these course slides for any other purpose
than that for which it was intended.
20
20
10