01 / 20

The Runge-Kutta
Method

A powerful numerical technique for solving ordinary differential equations when exact solutions are impossible: step forward with precision.

Differential Equations

Why Do We Need Numerical Methods?

Most real-world differential equations of the form \( \frac{dy}{dx} = f(x,y) \) cannot be solved exactly. When integration fails, we turn to numerical approximation: computing \( y \) values step by step across an interval.

Starting Simple: Euler's Method

The simplest numerical method is Euler's method. It approximates the next value by using a single slope evaluation at the current point:

\(x\)
\(y\)
\((x_0, y_0)\)
Error grows
True curve
Euler approx.

Euler's method is first-order accurate: the error is proportional to \( h \). The Runge-Kutta method improves this dramatically.

The Big Idea Behind Runge-Kutta

Instead of using one slope at the beginning of the interval (like Euler), the 4th-order Runge-Kutta method (RK4) samples the slope at four strategic points within each step and takes a weighted average.

\( k_1 \)
Slope at the start of the interval
\( k_2 \)
Slope at the midpoint using \( k_1 \)
\( k_3 \)
Slope at the midpoint using \( k_2 \)
\( k_4 \)
Slope at the end using \( k_3 \)

The RK4 Formulas

1
\( k_1 = h \cdot f(x_n,\; y_n) \)
2
\( k_2 = h \cdot f\!\left(x_n + \tfrac{h}{2},\; y_n + \tfrac{k_1}{2}\right) \)
3
\( k_3 = h \cdot f\!\left(x_n + \tfrac{h}{2},\; y_n + \tfrac{k_2}{2}\right) \)
4
\( k_4 = h \cdot f(x_n + h,\; y_n + k_3) \)

Visualizing the Four Slopes

\(x\)
\(y\)
\(x_n\)
\(x_n + h/2\)
\(x_n + h\)
\(k_1\)
\(k_2\)
\(k_3\)
\(k_4\)
\(y_{n+1}\)

Each slope builds on the previous one. The \( k_1 \) slope informs where we evaluate \( k_2 \), which informs \( k_3 \), which informs \( k_4 \). The final value \( y_{n+1} \) is found by adding a weighted average of these increments to \( y_n \): \( y_{n+1} = y_n + \frac{1}{6}(k_1 + 2k_2 + 2k_3 + k_4) \).

Why the 1-2-2-1 Weighting?

The coefficients in \( \frac{1}{6}(k_1 + 2k_2 + 2k_3 + k_4) \) are not arbitrary. They come from matching the method's Taylor series expansion to the true solution through fourth-order terms.

Simpson's Rule
The 1-2-2-1 pattern mirrors Simpson's 1/3 rule for numerical integration: endpoints count once, midpoints count twice.
Taylor Matching
These weights ensure that the error terms through \( h^4 \) cancel exactly, leaving a local truncation error of \( O(h^5) \).

Euler vs RK4: Key Differences

Euler's Method RK4
Slope evaluations per step 1 4
Order of accuracy \( O(h) \) \( O(h^4) \)
Local truncation error \( O(h^2) \) \( O(h^5) \)
Stability Poor for stiff problems Much better
Computation cost Low Moderate (\(4 \times\) Euler)
Practical use Teaching only Industry standard

How to Apply RK4

1
Identify \( f(x,y) \), the initial condition \( (x_0, y_0) \), the target \( x \)-value, and the step size \( h \).
2
Compute \( k_1 \): Evaluate \( h \cdot f(x_n, y_n) \) at the current point.
3
Compute \( k_2 \): Step half-way using \( k_1 \), then evaluate \( f \) there.
4
Compute \( k_3 \): Step half-way using \( k_2 \), then evaluate \( f \) there.
5
Compute \( k_4 \): Step fully using \( k_3 \), then evaluate \( f \) there.
6
Update: \( y_{n+1} = y_n + \frac{1}{6}(k_1 + 2k_2 + 2k_3 + k_4) \). Repeat until you reach the target.

Worked Example: \( \frac{dy}{dx} = y \)

Here \( f(x,y) = y \). The exact solution is \( y = e^x \), so the true answer is \( e^{0.1} \approx 1.1051709 \). Let's see how close RK4 gets in one step.

1
\( k_1 = 0.1 \cdot f(0, 1) = 0.1 \cdot 1 = 0.1 \)
2
\( k_2 = 0.1 \cdot f(0.05, 1.05) = 0.1 \cdot 1.05 = 0.105 \)
3
\( k_3 = 0.1 \cdot f(0.05, 1.0525) = 0.1 \cdot 1.0525 = 0.10525 \)
4
\( k_4 = 0.1 \cdot f(0.1, 1.10525) = 0.1 \cdot 1.10525 = 0.110525 \)

Example Result: Remarkable Accuracy

RK4 Result
\( 1.1051708 \)
Exact Value
\( e^{0.1} \approx 1.1051709 \)
Error
\( \approx 1 \times 10^{-7} \)

With just one step and \( h = 0.1 \), RK4 is accurate to 6 decimal places. Euler's method with the same step gives \( y_1 = 1.1 \), an error of \( 0.00517 \): about \(50,000 \times\) worse.

Example 2: \( \frac{dy}{dx} = x + y \)

1
\( k_1 = 0.1 \cdot f(0, 1) = 0.1(0 + 1) = 0.1 \)
2
\( k_2 = 0.1 \cdot f(0.05, 1.05) = 0.1(1.1) = 0.11 \)
3
\( k_3 = 0.1 \cdot f(0.05, 1.055) = 0.1(1.105) = 0.1105 \)
4
\( k_4 = 0.1 \cdot f(0.1, 1.1105) = 0.1(1.2105) = 0.12105 \)

Multi-Step Example: Building a Solution Curve

For \( \frac{dy}{dx} = -2xy \) with \( y(0) = 1 \) and \( h = 0.5 \), here are three RK4 steps compared to the exact solution \( y = e^{-x^2} \):

\(x\)
\(y\)
0
0.5
1.0
1.5
0
1.0
\(y(0) = 1\)
\(y(0.5) \approx 0.7786\)
\(y(1.0) \approx 0.3680\)
\(y(1.5) \approx 0.1117\)
Exact
RK4

Common Mistakes to Avoid

✕ Using \( k_1 \) Instead of \( k_2 \)
When computing \( k_3 \), you must use the most recent slope \( k_2 \), not \( k_1 \). Each \( k \) builds on the one before it.
✕ Forgetting the \( h \) Factor
Each \( k_i \) already includes the \( h \) multiplier. Don't multiply by \( h \) again in the final update formula.
✕ Wrong Midpoint Coordinates
For \( k_2 \) and \( k_3 \), the \( x \)-argument is \( x_n + \frac{h}{2} \), not \( x_n + h \). The full step is only for \( k_4 \).

Summary of Numerical Methods

Method Order Slope Evals Error / Step
Euler 1st 1 \( O(h^2) \)
Improved Euler (Heun) 2nd 2 \( O(h^3) \)
Midpoint Method 2nd 2 \( O(h^3) \)
RK4 (Classical) 4th 4 \( O(h^5) \)

Question 1 of 5

In the classical RK4 method, how many slope evaluations of \( f(x,y) \) are performed per step?

Question 2 of 5

What is the global truncation error order of the classical RK4 method?

Question 3 of 5

When computing \( k_3 \) in RK4, at what \( x \)-value is the function \( f \) evaluated?

Question 4 of 5

In the RK4 weighted average \( \frac{1}{6}(k_1 + 2k_2 + 2k_3 + k_4) \), why do \( k_2 \) and \( k_3 \) receive double weight?

Question 5 of 5

If you halve the step size \( h \) in RK4, by approximately what factor does the global error decrease?