Runge-Kutta Method
The Runge-Kutta method (RK4) approximates the solution of y′ = f(x, y) one step at a time. Each step averages four slopes, k₁ to k₄, measured at the start of the step, twice at the midpoint and at the end: y_(n+1) = y_n + (h/6)(k₁ + 2k₂ + 2k₃ + k₄). It is far more accurate than Euler’s method.
Before this lesson: Euler’s Method, Slope Fields and Simpson’s Rule
The idea: a better average slope
Euler’s method walks along the tangent line at the start of each step. If the solution curves, that single slope is wrong for the rest of the step, and the error builds up.
RK4 looks ahead before it commits. It measures the slope at the start, uses it to peek at the midpoint, measures again at the midpoint with the improved estimate, and finally measures at the far end. Then it moves using a weighted average of those four slopes, counting the two midpoint slopes twice.
The weights 1, 2, 2, 1 over 6 are the same weights as Simpson’s rule. In fact, when f depends only on x, one RK4 step is exactly Simpson’s rule on that step.
Runge-Kutta method formula (RK4)
To solve y′ = f(x, y) with y(x₀) = y₀ and step size h, repeat these steps:
\begin{aligned} k_1 &= f(x_n,\ y_n) \\ k_2 &= f\!\left(x_n + \tfrac{h}{2},\ y_n + \tfrac{h}{2}k_1\right) \\ k_3 &= f\!\left(x_n + \tfrac{h}{2},\ y_n + \tfrac{h}{2}k_2\right) \\ k_4 &= f(x_n + h,\ y_n + hk_3) \\ y_{n+1} &= y_n + \tfrac{h}{6}\left(k_1 + 2k_2 + 2k_3 + k_4\right), \qquad x_{n+1} = x_n + h \end{aligned}| Slope | Where it is measured | What it uses |
|---|---|---|
| k₁ | Start of the step | The current point |
| k₂ | Midpoint | A half step along k₁ |
| k₃ | Midpoint again | A half step along k₂ |
| k₄ | End of the step | A full step along k₃ |
RK4 is a fourth-order method: the total error at a fixed x is roughly proportional to h⁴. Halving the step size cuts the error by a factor of about 16, compared with a factor of 2 for Euler’s method.
Runge-Kutta method examples
Example 1: One RK4 step
Use one step of RK4 with h = 0.1 to approximate y(0.1) for y′ = x + y, y(0) = 1.
k_1 = f(0,\ 1) = 0 + 1 = 1
Find the slope at the starting point (x₀, y₀) = (0, 1).
k_2 = f(0.05,\ 1 + 0.05(1)) = f(0.05,\ 1.05) = 1.1
Move half a step (h/2 = 0.05) along k₁, then add x + y there.
k_3 = f(0.05,\ 1 + 0.05(1.1)) = f(0.05,\ 1.055) = 1.105
Move half a step along k₂ instead, then evaluate f.
k_4 = f(0.1,\ 1 + 0.1(1.105)) = f(0.1,\ 1.1105) = 1.2105
Move a full step along k₃, then evaluate f.
k_1 + 2k_2 + 2k_3 + k_4 = 1 + 2.2 + 2.21 + 1.2105 = 6.6205
Form the weighted sum of the four slopes.
y_1 = 1 + \dfrac{0.1}{6}(6.6205) \approx 1.110342Multiply the sum by h/6 and add it to y₀.
Answer: y(0.1) ≈ 1.110342. This equation is first-order linear, and its exact solution y = 2e^x − x − 1 gives y(0.1) ≈ 1.1103418, so the error is about 0.0000002. Euler’s method would give 1.1, an error of about 0.01.
Example 2: Two steps to estimate e
Use RK4 with h = 0.5 on y′ = y, y(0) = 1, to approximate y(1) = e.
k_1 = f(0,\ 1) = 1
Step 1: since f(x, y) = y, each slope equals the y-value where it is measured.
k_2 = 1 + 0.25(1) = 1.25
Move half a step (h/2 = 0.25) along k₁.
k_3 = 1 + 0.25(1.25) = 1.3125
Move half a step along k₂.
k_4 = 1 + 0.5(1.3125) = 1.65625
Move a full step along k₃.
y_1 = 1 + \dfrac{0.5}{6}(1 + 2.5 + 2.625 + 1.65625)Substitute into the update formula.
y_1 = 1 + \dfrac{7.78125}{12} = 1.6484375Add the slopes and simplify 0.5/6 to 1/12.
k_1 = 1.6484375
Step 2 starts at (0.5, 1.6484375).
k_2 = 1.6484375 + 0.25(1.6484375) = 2.060546875
Move half a step along k₁.
k_3 = 1.6484375 + 0.25(2.060546875) = 2.16357421875
Move half a step along k₂.
k_4 = 1.6484375 + 0.5(2.16357421875) = 2.730224609375
Move a full step along k₃.
k_1 + 2k_2 + 2k_3 + k_4 = 12.826904296875
Form the weighted sum: 1.6484375 + 4.12109375 + 4.3271484375 + 2.730224609375.
y_2 = 1.6484375 + \dfrac{12.826904296875}{12} \approx 2.717346Multiply by h/6 = 1/12 and add to y₁.
Answer: y(1) ≈ 2.717346. The true value is e ≈ 2.718282, so the error is about 0.00094. Euler’s method with the same two steps gives 1.5 and then 2.25, an error of about 0.47.
Example 3: A nonlinear equation
Use one RK4 step with h = 0.2 to approximate y(0.2) for y′ = xy², y(0) = 1.
k_1 = f(0,\ 1) = 0 \cdot 1^2 = 0
Find the slope at the start.
k_2 = f(0.1,\ 1 + 0.1(0)) = 0.1(1)^2 = 0.1
Move half a step (h/2 = 0.1) along k₁ and evaluate xy².
k_3 = f(0.1,\ 1 + 0.1(0.1)) = 0.1(1.01)^2 = 0.10201
Move half a step along k₂ and evaluate.
k_4 = f(0.2,\ 1 + 0.2(0.10201)) = 0.2(1.020402)^2 \approx 0.208244
Move a full step along k₃ and evaluate.
k_1 + 2k_2 + 2k_3 + k_4 \approx 0 + 0.2 + 0.20402 + 0.208244 = 0.612264
Form the weighted sum.
y_1 = 1 + \dfrac{0.2}{6}(0.612264) \approx 1.020409Multiply by h/6 and add to y₀.
Answer: y(0.2) ≈ 1.020409. Solving the separable equation gives y = 2/(2 − x²), so y(0.2) = 2/1.96 ≈ 1.020408: RK4 is off by less than 0.000001.
Example 4: A second step, organized in a table
Continue Example 1 for one more step to approximate y(0.2), and compare RK4 with Euler’s method and the exact solution.
k_1 = f(0.1,\ 1.110342) \approx 1.210342
Start at (x₁, y₁) = (0.1, 1.110342) and add x + y.
k_2 = f(0.15,\ 1.110342 + 0.05(1.210342)) \approx f(0.15,\ 1.170859) \approx 1.320859
Move half a step along k₁ and evaluate.
k_3 = f(0.15,\ 1.110342 + 0.05(1.320859)) \approx f(0.15,\ 1.176385) \approx 1.326385
Move half a step along k₂ and evaluate.
k_4 = f(0.2,\ 1.110342 + 0.1(1.326385)) \approx f(0.2,\ 1.242980) \approx 1.442980
Move a full step along k₃ and evaluate.
k_1 + 2k_2 + 2k_3 + k_4 \approx 1.210342 + 2.641718 + 2.652770 + 1.442980 = 7.947810
Form the weighted sum.
y_2 = 1.110342 + \dfrac{0.1}{6}(7.947810) \approx 1.242805Multiply by h/6 and add to y₁.
| x | Euler (h = 0.1) | RK4 (h = 0.1) | Exact 2e^x - x - 1 |
|---|---|---|---|
| 0 | 1 | 1 | 1 |
| 0.1 | 1.1 | 1.110342 | 1.110342 |
| 0.2 | 1.22 | 1.242805 | 1.242806 |
Answer: y(0.2) ≈ 1.242805. RK4’s error is about 0.0000004, while Euler’s error is about 0.023, more than 50,000 times larger.
Example 5: A falling skydiver (word problem)
A skydiver’s speed v in m/s satisfies dv/dt = 10 − 0.01v², where the second term is air resistance, and v(0) = 0. Use one RK4 step with h = 1 second to estimate the speed after 1 second.
k_1 = 10 - 0.01(0)^2 = 10
Find the slope at t = 0, v = 0.
k_2 = 10 - 0.01(0 + 0.5(10))^2 = 10 - 0.01(5)^2 = 9.75
Move half a step (h/2 = 0.5) along k₁: v = 5.
k_3 = 10 - 0.01(0 + 0.5(9.75))^2 = 10 - 0.01(4.875)^2 \approx 9.762344
Move half a step along k₂: v = 4.875.
k_4 = 10 - 0.01(9.762344)^2 \approx 10 - 0.953034 = 9.046966
Move a full step along k₃: v = 9.762344.
k_1 + 2k_2 + 2k_3 + k_4 \approx 10 + 19.5 + 19.524688 + 9.046966 = 58.071654
Form the weighted sum.
v_1 = 0 + \dfrac{1}{6}(58.071654) \approx 9.678609Multiply by h/6 = 1/6 and add to v₀ = 0.
Answer: about 9.68 m/s after 1 second. Euler’s method would give 10 m/s, ignoring air resistance during the whole second. The exact solution, found with hyperbolic tangent, gives about 9.6795 m/s, so RK4 is off by only about 0.001 m/s even with a large step.
Common mistakes
| Mistake | Example of the error | Fix |
|---|---|---|
| Using the full step for k₂ or k₃ | k_2 = f(x_n + h,\ y_n + hk_1) | The two middle slopes use half steps: x + h/2 and y + (h/2)k. |
| Forgetting to move x | k_2 = f(x_n,\ y_n + \tfrac{h}{2}k_1) | k₂ and k₃ are measured at x + h/2, and k₄ at x + h. |
| Using k₁ again for k₃ | k_3 = f(x_n + \tfrac{h}{2},\ y_n + \tfrac{h}{2}k_1) | k₃ is built from k₂, and k₄ from k₃. |
| Wrong weights | Averaging the four slopes equally | Use (k₁ + 2k₂ + 2k₃ + k₄)/6. |
| Leaving out h in the update | y_1 = y_0 + \tfrac{1}{6}(k_1 + 2k_2 + 2k_3 + k_4) with h = 0.1 | Multiply by h/6, not 1/6 (they match only when h = 1). |
| Rounding too early | Rounding each k to one decimal place | Keep at least 6 significant digits until the final answer. |
Related methods
Euler, improved Euler and RK4
| Method | Slopes per step | Error at a fixed x |
|---|---|---|
| Euler’s method | 1 | About proportional to h |
| Improved Euler (Heun’s method) | 2 | About proportional to h² |
| RK4 | 4 | About proportional to h⁴ |
Improved Euler averages the slope at the start with the slope at the end of an Euler step: y_(n+1) = y_n + (h/2)(k₁ + k₂), where k₂ = f(x_n + h, y_n + hk₁). It is a second-order Runge-Kutta method.
Checking the order of RK4
For y′ = y on [0, 1], the RK4 error at x = 1 is about 0.00094 with h = 0.5, about 0.000072 with h = 0.25 and about 0.0000050 with h = 0.125. Each halving divides the error by about 13 to 14, and the ratio approaches 16 = 2⁴ as h shrinks.
When there is an exact solution
Most equations met in practice have no formula for their solution, which is why numerical methods matter. When one exists, as for separable and linear equations, use it to test your RK4 work.
Practice problems
- Use one RK4 step with h = 0.1 to approximate y(0.1) for y′ = 2y, y(0) = 1.
- Use one RK4 step with h = 0.2 to approximate y(0.2) for y′ = x − y, y(0) = 0.
- Use one RK4 step with h = 0.5 to approximate y(0.5) for y′ = −2xy, y(0) = 1.
- For y′ = y, y(0) = 1, compare one Euler step and one RK4 step with h = 1 against e.
- For y′ = y² − x, y(1) = 2 and h = 0.1, find k₁ and k₂.
- Use one RK4 step with h = 1 for y′ = 3x², y(0) = 0. Why is the answer exact?
- Use two RK4 steps with h = 0.5 to approximate y(1) for y′ = −y, y(0) = 1.
- RK4 with h = 0.1 gives an error of about 8 × 10⁻⁶ at x = 2. Estimate the error with h = 0.05.
- Use one RK4 step with h = 0.2 to approximate y(0.2) for y′ = 1 + y², y(0) = 0, and compare with tan 0.2.
- A cup of tea cools by dT/dt = −0.1(T − 20), with T in °C and t in minutes, and T(0) = 80. Use one RK4 step with h = 2 to estimate T(2).
Show answers
1.
k_1 = 2(1) = 2
Evaluate f(x, y) = 2y at y = 1.
k_2 = 2(1 + 0.05(2)) = 2(1.1) = 2.2
Move half a step along k₁.
k_3 = 2(1 + 0.05(2.2)) = 2(1.11) = 2.22
Move half a step along k₂.
k_4 = 2(1 + 0.1(2.22)) = 2(1.222) = 2.444
Move a full step along k₃.
y_1 = 1 + \dfrac{0.1}{6}(2 + 4.4 + 4.44 + 2.444) = 1 + \dfrac{0.1}{6}(13.284) = 1.2214Form the weighted sum and update.
Answer: y(0.1) ≈ 1.2214 (exact e^0.2 ≈ 1.221403)
2.
k_1 = 0 - 0 = 0
Evaluate x − y at (0, 0).
k_2 = f(0.1,\ 0) = 0.1
Move half a step along k₁.
k_3 = f(0.1,\ 0 + 0.1(0.1)) = 0.1 - 0.01 = 0.09
Move half a step along k₂.
k_4 = f(0.2,\ 0 + 0.2(0.09)) = 0.2 - 0.018 = 0.182
Move a full step along k₃.
y_1 = 0 + \dfrac{0.2}{6}(0 + 0.2 + 0.18 + 0.182) = \dfrac{0.2}{6}(0.562) \approx 0.018733Form the weighted sum and update.
Answer: y(0.2) ≈ 0.018733 (exact x − 1 + e^(−x) gives 0.018731)
3.
k_1 = -2(0)(1) = 0
Evaluate −2xy at (0, 1).
k_2 = -2(0.25)(1) = -0.5
Move half a step (0.25) along k₁: y = 1.
k_3 = -2(0.25)(1 - 0.125) = -0.4375
Move half a step along k₂: y = 1 + 0.25(−0.5) = 0.875.
k_4 = -2(0.5)(1 - 0.21875) = -0.78125
Move a full step along k₃: y = 1 + 0.5(−0.4375) = 0.78125.
y_1 = 1 + \dfrac{0.5}{6}(0 - 1 - 0.875 - 0.78125) = 1 - \dfrac{2.65625}{12} \approx 0.778646Form the weighted sum and update.
Answer: y(0.5) ≈ 0.778646 (exact e^(−0.25) ≈ 0.778801)
4.
y_1 = 1 + 1(1) = 2
Euler: add h times the starting slope.
k_1 = 1,\ k_2 = 1.5,\ k_3 = 1.75,\ k_4 = 2.75
RK4: k₂ = 1 + 0.5(1), k₃ = 1 + 0.5(1.5), k₄ = 1 + 1(1.75).
y_1 = 1 + \dfrac{1}{6}(1 + 3 + 3.5 + 2.75) = 1 + \dfrac{10.25}{6} \approx 2.708333Form the weighted sum and update.
Answer: Euler gives 2 (error about 0.718); RK4 gives about 2.708333 (error about 0.0099)
5.
k_1 = 2^2 - 1 = 3
Evaluate y² − x at (1, 2).
k_2 = f(1.05,\ 2 + 0.05(3)) = f(1.05,\ 2.15) = 4.6225 - 1.05 = 3.5725
Move half a step along k₁ and evaluate.
Answer: k₁ = 3 and k₂ = 3.5725
6.
k_1 = 3(0)^2 = 0,\quad k_2 = k_3 = 3(0.5)^2 = 0.75
f depends only on x, so k₂ = k₃ (both at x = 0.5).
k_4 = 3(1)^2 = 3
Evaluate at x = 1.
y_1 = 0 + \dfrac{1}{6}(0 + 1.5 + 1.5 + 3) = 1Form the weighted sum and update.
Answer: y(1) = 1, which is exact: here RK4 is Simpson’s rule for the integral of 3x² from 0 to 1, and Simpson’s rule is exact for polynomials of degree 3 or less
7.
k_1 = -1,\ k_2 = -0.75,\ k_3 = -0.8125,\ k_4 = -0.59375
Step 1: k₂ = −(1 − 0.25), k₃ = −(1 − 0.25(0.75)), k₄ = −(1 − 0.5(0.8125)).
y_1 = 1 + \dfrac{0.5}{6}(-4.71875) \approx 0.606771Weighted sum −1 − 1.5 − 1.625 − 0.59375 = −4.71875.
y_2 = y_1(0.606771) \approx 0.368171
Step 2: f is linear in y, so each k is y₁ times its step 1 value, and the update multiplies by the same factor again.
Answer: y(1) ≈ 0.368171 (exact e^(−1) ≈ 0.367879)
8.
\dfrac{8 \times 10^{-6}}{2^4} = \dfrac{8 \times 10^{-6}}{16}Halving h divides the RK4 error by about 2⁴ = 16.
= 5 \times 10^{-7}Divide.
Answer: about 5 × 10⁻⁷
9.
k_1 = 1 + 0^2 = 1
Evaluate 1 + y² at y = 0.
k_2 = 1 + (0.1)^2 = 1.01
Move half a step along k₁: y = 0.1.
k_3 = 1 + (0.101)^2 = 1.010201
Move half a step along k₂: y = 0.1(1.01) = 0.101.
k_4 = 1 + (0.2020402)^2 \approx 1.040820
Move a full step along k₃: y = 0.2(1.010201).
y_1 = \dfrac{0.2}{6}(1 + 2.02 + 2.020402 + 1.040820) \approx 0.202707Form the weighted sum and update.
Answer: y(0.2) ≈ 0.202707; the exact solution y = tan x gives tan 0.2 ≈ 0.202710
10.
k_1 = -0.1(80 - 20) = -6
Evaluate the slope at T = 80.
k_2 = -0.1(80 - 6 - 20) = -5.4
Move half a step (h/2 = 1) along k₁: T = 74.
k_3 = -0.1(80 - 5.4 - 20) = -5.46
Move half a step along k₂: T = 74.6.
k_4 = -0.1(80 - 10.92 - 20) = -4.908
Move a full step along k₃: T = 80 + 2(−5.46) = 69.08.
T_1 = 80 + \dfrac{2}{6}(-6 - 10.8 - 10.92 - 4.908) = 80 - \dfrac{32.628}{3} = 69.124Form the weighted sum and update.
Answer: T(2) ≈ 69.124 °C (the exact solution from Newton’s law of cooling gives about 69.124 °C)
Runge-Kutta method FAQ
What is the Runge-Kutta method?
It is a family of numerical methods for solving initial-value problems y′ = f(x, y). The most common one, RK4, moves from one point to the next using a weighted average of four slopes measured inside each step.
What is the RK4 formula?
With k₁ = f(x_n, y_n), k₂ = f(x_n + h/2, y_n + (h/2)k₁), k₃ = f(x_n + h/2, y_n + (h/2)k₂) and k₄ = f(x_n + h, y_n + hk₃), the update is y_(n+1) = y_n + (h/6)(k₁ + 2k₂ + 2k₃ + k₄).
Why is RK4 more accurate than Euler’s method?
Euler’s method uses only the slope at the start of each step. RK4 samples the slope across the whole step and combines the samples with Simpson’s rule weights, so its error shrinks like h⁴ instead of h.
What is the order of the Runge-Kutta method?
RK4 is fourth order: its total error at a fixed point is roughly proportional to h⁴, and the error made in a single step is roughly proportional to h⁵.
Summary
- RK4 solves y′ = f(x, y) step by step, like Euler’s method, but with four slopes per step.
- k₁ at the start, k₂ and k₃ at the midpoint, k₄ at the end, each built from the one before.
- Update: y_(n+1) = y_n + (h/6)(k₁ + 2k₂ + 2k₃ + k₄), the same weights as Simpson’s rule.
- The error is about proportional to h⁴: halving h cuts it by about 16.
- Keep plenty of digits in each k and organize the work in a table.
Previous lesson: Mixing Problems
Next lesson: Second-Order Homogeneous Linear Equations
Back to the Differential Equations Index