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.4440
1 0.2 1.2428 1.4428 1.6871 1.7115 1.9851
2 0.4 1.5836 1.9836 2.2820 2.3118 2.6460
3 0.6 2.0442 2.6442 3.0086 3.0451 3.4532
4 0.8 2.6510 3.4510 3.8961 3.9407 4.4392
5 1.0 3.4365

RK4 Result: y(1)3.4365

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

Error: |3.43653.4366|0.0001 (excellent accuracy 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.6406 0.0777 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(1))=0.05 1+0.05(0.05)=0.9975
k4,l4 (0+0.1(0.9975))=0.09975 1+0.1(0.05)=0.995

RK4 Updates:

$$x_1 = 1 + \frac{0.1}{6}\big(0 + 2(-0.05) + 2(-0.05) + (-0.09975)\big) = 1 - 0.00499583 = 0.99500417$$
$$y_1 = 0 + \frac{0.1}{6}\big(1 + 2(1) + 2(0.9975) + 0.995\big) = \frac{0.1}{6}(5.99) = 0.09983333$$

Result: (x1,y1)(0.99500417,0.09983333)

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

At t=0.1: (0.99500,0.09983)

Error: |Δx|1.4×109 and |Δy|8.3×108 — tiny! RK4 tracks circular motion almost exactly. Note that each stage must update both components before the next stage is evaluated; freezing l at its l1 value is the classic mistake and costs six digits of accuracy.

📝 Exam-Style Practice Problems

These are the kind of RK4 questions that appear on exams: one or two steps by hand, carried to full precision, with an accuracy comparison at the end. Keep at least eight decimal places in the intermediate slopes — RK4 is accurate enough that premature rounding, not the method, becomes your error. Click a problem to reveal the full solution.

Practice 1: Take one RK4 step for $y' = x + y$, $y(0) = 1$ with $h = 0.2$, and compare against Euler and the exact solution

Solution

Step 1: Write down the four slopes

$$k_1 = f(x_0, y_0),\quad k_2 = f\!\left(x_0 + \tfrac{h}{2},\, y_0 + \tfrac{h}{2}k_1\right),\quad k_3 = f\!\left(x_0 + \tfrac{h}{2},\, y_0 + \tfrac{h}{2}k_2\right),\quad k_4 = f(x_0 + h,\, y_0 + h k_3)$$

With $f(x,y) = x + y$, $(x_0,y_0) = (0,1)$, $h = 0.2$ and $\tfrac{h}{2} = 0.1$:

Step 2: Evaluate them in order

$$k_1 = f(0, 1) = 0 + 1 = 1$$ $$k_2 = f(0.1,\; 1 + 0.1(1)) = f(0.1,\, 1.1) = 0.1 + 1.1 = 1.2$$ $$k_3 = f(0.1,\; 1 + 0.1(1.2)) = f(0.1,\, 1.12) = 0.1 + 1.12 = 1.22$$ $$k_4 = f(0.2,\; 1 + 0.2(1.22)) = f(0.2,\, 1.244) = 0.2 + 1.244 = 1.444$$

Notice each slope feeds the next: $k_2$ uses $k_1$, $k_3$ uses $k_2$, $k_4$ uses $k_3$.

Step 3: Weighted average and update

$$k_1 + 2k_2 + 2k_3 + k_4 = 1 + 2.4 + 2.44 + 1.444 = 7.284$$ $$y_1 = 1 + \frac{0.2}{6}(7.284) = 1 + 0.2428 = 1.2428$$

Step 4: Compare

The exact solution is $y = 2e^{x} - x - 1$, so $y(0.2) = 1.24280552$.

Method ($h = 0.2$, one step)$y(0.2)$Error$f$-evaluations
Euler1.2000000$2.4\times10^{-2}$1
Improved Euler1.2400000$2.8\times10^{-3}$2
RK41.2428000$5.5\times10^{-6}$4
Exact1.2428055
$$\boxed{y(0.2) \approx 1.2428,\qquad \text{error} \approx 5.5\times10^{-6}}$$
Exam tip: four times the work of Euler buys roughly four thousand times the accuracy here. That ratio only improves as $h$ shrinks, because the errors scale as $h$ and $h^4$ respectively.
Practice 2: Take two RK4 steps for $y' = y - x^2 + 1$, $y(0) = 0.5$ with $h = 0.2$, and verify the fourth-order accuracy

Solution

Step 1: Record the exact solution first

$$y(x) = (x+1)^2 - \tfrac{1}{2}e^{x}$$

Check: $y(0) = 1 - 0.5 = 0.5$ ✓, and $y' = 2(x+1) - \tfrac12 e^x$, while $y - x^2 + 1 = (x^2+2x+1) - \tfrac12 e^x - x^2 + 1 = 2x + 2 - \tfrac12 e^x$ ✓

Step 2: First step, $x_0 = 0 \to x_1 = 0.2$

$$k_1 = f(0,\,0.5) = 0.5 - 0 + 1 = 1.5$$ $$k_2 = f(0.1,\; 0.5 + 0.1(1.5)) = f(0.1,\, 0.65) = 0.65 - 0.01 + 1 = 1.64$$ $$k_3 = f(0.1,\; 0.5 + 0.1(1.64)) = f(0.1,\, 0.664) = 0.664 - 0.01 + 1 = 1.654$$ $$k_4 = f(0.2,\; 0.5 + 0.2(1.654)) = f(0.2,\, 0.8308) = 0.8308 - 0.04 + 1 = 1.7908$$ $$y_1 = 0.5 + \frac{0.2}{6}\big(1.5 + 2(1.64) + 2(1.654) + 1.7908\big) = 0.5 + \frac{0.2}{6}(9.8788) = 0.82929333$$

Step 3: Second step, $x_1 = 0.2 \to x_2 = 0.4$

$$k_1 = f(0.2,\, 0.82929333) = 0.82929333 - 0.04 + 1 = 1.78929333$$ $$k_2 = f(0.3,\; 0.82929333 + 0.1(1.78929333)) = f(0.3,\, 1.00822267) = 1.91822267$$ $$k_3 = f(0.3,\; 0.82929333 + 0.1(1.91822267)) = f(0.3,\, 1.02111560) = 1.93111560$$ $$k_4 = f(0.4,\; 0.82929333 + 0.2(1.93111560)) = f(0.4,\, 1.21551645) = 2.05551645$$ $$y_2 = 0.82929333 + \frac{0.2}{6}\big(1.78929333 + 2(1.91822267) + 2(1.93111560) + 2.05551645\big) = 1.21407621$$

Step 4: Errors

$n$$x_n$$y_n$ (RK4)$y(x_n)$ exactError
00.00.500000000.500000000
10.20.829293330.82929862$5.3\times10^{-6}$
20.41.214076211.21408765$1.1\times10^{-5}$

Halving to $h = 0.1$ would shrink these by a factor of about $2^4 = 16$, not $2$ — that is what “fourth order” means.

$$\boxed{y(0.4) \approx 1.21407621,\qquad \text{error} \approx 1.1\times10^{-5}}$$
Practice 3 (system): Convert $y'' + 2y' + 2y = 0$, $y(0) = 1$, $y'(0) = 0$ to a first-order system and take one RK4 step with $h = 0.1$

Solution

Step 1: Convert to a system

Let $u = y$ and $v = y'$. Then

$$u' = v = f(t,u,v),\qquad v' = -2v - 2u = g(t,u,v),\qquad u(0) = 1,\; v(0) = 0$$

Step 2: Vector RK4 — every stage advances both components together

$$k_i \text{ for } u,\qquad \ell_i \text{ for } v,\qquad \begin{aligned} k_1 &= f(t_0,u_0,v_0), & \ell_1 &= g(t_0,u_0,v_0)\\ k_2 &= f\!\left(t_0+\tfrac h2, u_0+\tfrac h2 k_1, v_0+\tfrac h2 \ell_1\right), & \ell_2 &= g\!\left(t_0+\tfrac h2, u_0+\tfrac h2 k_1, v_0+\tfrac h2 \ell_1\right) \end{aligned}$$

and similarly for the third and fourth stages. Never update $u$ using $k$'s while leaving $v$ frozen — both must move at every stage.

Step 3: Evaluate the stages ($h/2 = 0.05$)

Stage$(u,v)$ used$k_i = v$$\ell_i = -2v - 2u$
1$(1,\,0)$$0$$-2$
2$(1 + 0.05(0),\; 0 + 0.05(-2)) = (1,\,-0.1)$$-0.1$$-1.8$
3$(1 + 0.05(-0.1),\; 0 + 0.05(-1.8)) = (0.995,\,-0.09)$$-0.09$$-1.81$
4$(1 + 0.1(-0.09),\; 0 + 0.1(-1.81)) = (0.991,\,-0.181)$$-0.181$$-1.62$

Step 4: Update both components

$$u_1 = 1 + \frac{0.1}{6}\big(0 + 2(-0.1) + 2(-0.09) + (-0.181)\big) = 1 + \frac{0.1}{6}(-0.561) = 0.99065$$ $$v_1 = 0 + \frac{0.1}{6}\big(-2 + 2(-1.8) + 2(-1.81) + (-1.62)\big) = \frac{0.1}{6}(-10.84) = -0.18066667$$

Step 5: Compare with the exact solution

The characteristic roots are $-1 \pm i$, so $y = e^{-t}(\cos t + \sin t)$ satisfies both initial conditions, and $y' = -2e^{-t}\sin t$.

$$y(0.1) = 0.99065001,\qquad y'(0.1) = -0.18066602$$ $$\boxed{u_1 = 0.99065\ \ (\text{error } 1.1\times10^{-8}),\qquad v_1 = -0.18066667\ \ (\text{error } 6.4\times10^{-7})}$$
Exam tip: for an $m$-component system RK4 needs $4m$ scalar evaluations per step, but the weights $\tfrac16(1,2,2,1)$ and the $\tfrac h2$ offsets are identical to the scalar case. Only the bookkeeping changes.

Practice Problems

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

Progress:

0 / 6 correct

Q1: Computing $k_1$ — for $y' = x^2 + y$ with $y(0) = 1$ and $h = 0.2$, what is $k_1$?

Q2: Understanding $k_2$ — at which point is the second RK4 slope evaluated?

Q3: In $y_{n+1} = y_n + \tfrac{h}{6}\left(k_1 + 2k_2 + 2k_3 + k_4\right)$, what fraction of the total weight does $k_2$ carry?

Q4: What is the local truncation error of RK4 on a single step?

Q5: Take one RK4 step for $y' = -2y$, $y(0) = 1$ with $h = 0.5$. What is $y_1$?

Q6: To reduce the global error of RK4 by a factor of about $256$, by what factor should you decrease $h$?

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