5.4 — Runge-Kutta Methods

The workhorse of numerical differential equations

Learning Objectives

Motivation

Why Runge-Kutta? The RK4 method is considered the workhorse of numerical methods in science and engineering. It provides exceptional accuracy with manageable computational cost. You will encounter RK4 in:

  • Engineering simulations (structural mechanics, control systems)
  • Physics simulations (orbital mechanics, fluid dynamics)
  • Biology and medicine (pharmacokinetics, population dynamics)
  • Climate modeling and weather prediction

From Euler to Runge-Kutta

Euler's method uses a single slope evaluation at the beginning of each step, giving O(h) accuracy. Runge-Kutta methods use multiple slope evaluations at strategic points to achieve higher-order accuracy. The 4th-order method (RK4) uses four slope evaluations to achieve O(h4) accuracy.

The RK4 Formulas

Definition: Fourth-Order Runge-Kutta (RK4)

Given y'=f(x,y) with initial condition y(x0)=y0 and step size h, the RK4 step is:

$$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 + h\,k_3)$$
$$y_{n+1} = y_n + \tfrac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4)$$

Intuition Behind RK4

k1: Slope at the start of the interval: f(xn,yn)

k2: Slope at the midpoint using k1 to estimate the midpoint value

k3: Slope at the midpoint using k2 (a refined estimate)

k4: Slope at the end of the interval using k3

The final update uses a weighted average of these four slopes:

$$\text{weight} = \frac{1}{6}(1 + 2 + 2 + 1)$$

This weighting (Simpson's rule-like) gives extraordinary accuracy by sampling the slope in a strategic pattern.

Error Analysis

Theorem: RK4 Error

Local Truncation Error: O(h5)

Global Error: O(h4)

RK4 is a fourth-order method. To reduce error by a factor of 16, halve the step size h.

Comparison of Methods

Quick Reference: Comparing Numerical Methods

Method Order f Evaluations per Step Typical Use
Euler O(h) 1 Quick sketches, educational purposes
Improved Euler (RK2) O(h2) 2 Better accuracy, simple code
RK4 O(h4) 4 Standard default for production code

Step-by-Step RK4 Algorithm

  1. Set up: Define the differential equation y'=f(x,y), initial condition (x0,y0), and step size h.
  2. Compute k1: k1=f(xn,yn)
  3. Compute k2: k2=f(xn+h/2,yn+(h/2)k1)
  4. Compute k3: k3=f(xn+h/2,yn+(h/2)k2)
  5. Compute k4: k4=f(xn+h,yn+hk3)
  6. Update: yn+1=yn+(h/6)(k1+2k2+2k3+k4)
  7. Advance: xn+1=xn+h
  8. Repeat: Continue until you reach the desired endpoint.

Worked Examples

Problem: Solve y'=y, y(0)=1, with h=0.1. Find y(0.1).

Solution:

Setup: f(x,y)=y, x0=0, y0=1, h=0.1

Step Calculation Value
k1 f(0,1)=1 1
k2 f(0.05,1+0.05)=f(0.05,1.05)=1.05 1.05
k3 f(0.05,1+0.05(1.05))=1.0525 1.0525
k4 f(0.1,1+0.1(1.0525))=1.10525 1.10525

RK4 Update:

$$y_1 = 1 + \frac{0.1}{6}(1 + 2(1.05) + 2(1.0525) + 1.10525)$$
$$y_1 = 1 + \frac{0.1}{6}(6.31075) = 1 + 0.10517083... = 1.10517083...$$

Exact solution: y(x)=ex, so y(0.1)=e0.1=1.10517092...

Error: |1.105170831.10517092|8.5×108 (microscopic!)

Compare with Euler: Euler gives y_1 = 1 + 0.1(1) = 1.1 with error 0.005 — about 59,000 times larger!

Problem: Solve y'=x+y, y(0)=1, with h=0.2. Find y(1).

Solution:

We compute 5 steps, each with the full RK4 formula. Here's a summary table:

n xn yn k1 k2 k3 k4
0 0.0 1.0000 1.0000 1.2000 1.2200 1.4640
1 0.2 1.2427 1.4427 1.6875 1.7103 2.0080
2 0.4 1.5836 1.9836 2.2620 2.2888 2.6511
3 0.6 2.0533 2.6533 2.9795 3.0115 3.4470
4 0.8 2.6968 3.4968 3.8793 3.9185 4.4305
5 1.0 3.5681

RK4 Result: y(1)3.5681

Exact solution: y=2exx1, so y(1)=2e111=2e23.4366

Error: |3.56813.4366|0.1315 (very good for 5 steps with h=0.2)

Problem: Solve y'=2xy, y(0)=1, with h=0.25. Find y(1).

Solution:

This problem has an exact solution: y(x)=ex2, so y(1)=e10.36788

Using RK4 with only 4 steps (h=0.25):

Step x RK4 y Exact y Error
0 0.0 1.00000 1.00000 0.00000
1 0.25 0.93941 0.93941 0.00000
2 0.50 0.77848 0.77880 0.00032
3 0.75 0.55383 0.55432 0.00049
4 1.0 0.36779 0.36788 0.00009

Result: RK4 with just 4 steps gives y(1)0.36779, accurate to 4 decimal places! The error is only 0.00009.

Problem: Solve y'=y, y(0)=1 to x=1 with h=0.5. Compare three methods.

Solution:

Exact solution: y(1)=e2.71828

Method y(1) Error Functions Called
Euler 2.2500 0.4683 2
Improved Euler (RK2) 2.6875 0.0308 4
RK4 2.71735 0.00093 8
Exact 2.71828

Observation: RK4 with 8 function evaluations gives error 0.00093, while Euler with 2 evaluations gives error 0.4683 — RK4 is 500 times more accurate for the same computational work!

Problem: Solve the system:

$$x' = -y, \quad y' = x, \quad x(0) = 1, \quad y(0) = 0$$

with h=0.1. Compute one step.

Solution:

For systems, RK4 uses vector slopes. Let f_1(x,y)=y and f_2(x,y)=x.

At step 0: x0=1, y0=0

Slope k (for x) l (for y)
k1,l1 0=0 1
k2,l2 (0+0.05)=0.05 1
k3,l3 (0+0.05)=0.05 1
k4,l4 (0+0.1)=0.1 1

RK4 Updates:

$$x_1 = 1 + \frac{0.1}{6}(0 + 2(-0.05) + 2(-0.05) + (-0.1)) = 1 - 0.00833... \approx 0.99167$$
$$y_1 = 0 + \frac{0.1}{6}(1 + 2(1) + 2(1) + 1) = 0.1$$

Result: (x1,y1)(0.99167,0.1)

Exact solution: x(t)=cost, y(t)=sint (unit circle)

At t=0.1: (0.99500,0.09983)

Error: Tiny! RK4 perfectly captures circular motion.

Practice Problems

Test your understanding with these multiple-choice questions. Click a choice to check your answer.

Progress:

0 / 6 correct

Question 1: Computing k₁

For y'=x2+y, y(0)=1, what is k1?

Question 2: Understanding k₂

In RK4, k2 is evaluated at which point?

Question 3: The RK4 Weight

The RK4 update formula uses the weight h/6. What is the coefficient of k2 in this formula?

Question 4: Error Order of RK4

What is the global error order of the RK4 method?

Question 5: Comparing Methods

To reduce the global error by a factor of 256, by what factor should you decrease h in RK4?

Question 6: RK4 Applications

Which of the following is not a common application of RK4?

Quick Reference Card

RK4 Formula at a Glance

$$\begin{align} 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}(k_1 + 2k_2 + 2k_3 + k_4) \end{align}$$

Key Facts

  • Order: Fourth-order method, O(h4) global error
  • Function calls: 4 evaluations of f per step
  • Local truncation error: O(h5)
  • Halve h: Error is divided by 2^4=16
  • Stability: Good stability for most problems; region depends on the problem
  • Best used when: You need high accuracy without adaptive step control