The workhorse of numerical differential equations
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:
Euler's method uses a single slope evaluation at the beginning of each step, giving 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 accuracy.
Definition: Fourth-Order Runge-Kutta (RK4)
Given with initial condition and step size , the RK4 step is:
: Slope at the start of the interval:
: Slope at the midpoint using to estimate the midpoint value
: Slope at the midpoint using (a refined estimate)
: Slope at the end of the interval using
The final update uses a weighted average of these four slopes:
This weighting (Simpson's rule-like) gives extraordinary accuracy by sampling the slope in a strategic pattern.
Theorem: RK4 Error
Local Truncation Error:
Global Error:
RK4 is a fourth-order method. To reduce error by a factor of 16, halve the step size .
Quick Reference: Comparing Numerical Methods
| Method | Order | Evaluations per Step | Typical Use |
|---|---|---|---|
| Euler | 1 | Quick sketches, educational purposes | |
| Improved Euler (RK2) | 2 | Better accuracy, simple code | |
| RK4 | 4 | Standard default for production code |
Problem: Solve , , with . Find .
Setup: , , ,
| Step | Calculation | Value |
|---|---|---|
| 1 | ||
| 1.05 | ||
| 1.0525 | ||
| 1.10525 |
RK4 Update:
Exact solution: , so
Error: (microscopic!)
Compare with Euler: Euler gives with error — about 59,000 times larger!
Problem: Solve , , with . Find .
We compute 5 steps, each with the full RK4 formula. Here's a summary table:
| 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:
Exact solution: , so
Error: (excellent accuracy for 5 steps with )
Problem: Solve , , with . Find .
This problem has an exact solution: , so
Using RK4 with only 4 steps ():
| Step | RK4 | Exact | 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 , accurate to 4 decimal places! The error is only .
Problem: Solve , to with . Compare three methods.
Exact solution:
| Method | 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 , while Euler with 2 evaluations gives error — RK4 is 500 times more accurate for the same computational work!
Problem: Solve the system:
with . Compute one step.
For systems, RK4 uses vector slopes. Let and .
At step 0: ,
| Slope | (for ) | (for ) |
|---|---|---|
RK4 Updates:
Result:
Exact solution: , (unit circle)
At :
Error: and — tiny! RK4 tracks circular motion almost exactly. Note that each stage must update both components before the next stage is evaluated; freezing at its value is the classic mistake and costs six digits of accuracy.
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.
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 |
|---|---|---|---|
| Euler | 1.2000000 | $2.4\times10^{-2}$ | 1 |
| Improved Euler | 1.2400000 | $2.8\times10^{-3}$ | 2 |
| RK4 | 1.2428000 | $5.5\times10^{-6}$ | 4 |
| Exact | 1.2428055 | — | — |
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)$ exact | Error |
|---|---|---|---|---|
| 0 | 0.0 | 0.50000000 | 0.50000000 | 0 |
| 1 | 0.2 | 0.82929333 | 0.82929862 | $5.3\times10^{-6}$ |
| 2 | 0.4 | 1.21407621 | 1.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}}$$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})}$$Test your understanding with these multiple-choice questions. Click a choice to check your answer.
0 / 6 correct
RK4 Formula at a Glance
Key Facts