Numerical Methods · Module 10 of 10
Numerical Solution of ODEs
Most differential equations cannot be solved by hand. Numerical solvers march the solution forward from its starting value, one step at a time. Euler is the idea in its simplest form; Runge-Kutta is the workhorse that gets it right.
Readiness check
This closing module marches ODEs forward. Tick only what you can do closed-notes.
- Read a first-order ODE dy/dx = f(x, y).
- Recall a slope as the rate of change.
- Evaluate a function of two variables.
- Recall the order O(hn) of an error.
- Recall what an initial condition specifies.
The core idea
An ODE solver steps a known starting value forward using the slope the equation provides. Euler takes one slope per step; Runge-Kutta samples several slopes within the step for far higher accuracy.
Euler: yi+1 = yi + h f(xi, yi)RK4: weighted average of four slopesEuler O(h), RK4 O(h4) globalAn initial-value problem gives a first-order ODE dy/dx = f(x, y) and a starting value y(x0). The solution is a curve, and the equation provides its slope at any point. Euler's method takes the simplest step: from the current point, follow the slope f(xi, yi) for a step h to the next point. It is easy but only first-order accurate, and its error accumulates, so the curve drifts. Heun's method improves it by averaging the slope at the start with a predicted slope at the end, a predictor-corrector that is second-order. The fourth-order Runge-Kutta method goes further, sampling four slopes across the step, at the start, twice in the middle, and at the end, and combining them in a weighted average; it is fourth-order accurate, the standard solver for smooth problems. Each step adds a local truncation error; these accumulate into a global error one order lower, so a method's global order is what matters over a full integration.
The skills, taught in order
Five skills build from the initial-value problem to Euler, Heun, Runge-Kutta, and the error that accumulates.
10.1 The initial-value problem
A first-order ODE dy/dx = f(x, y) with an initial condition y(x0) = y0 has a unique solution curve. The equation gives the slope everywhere; the solver's job is to trace the curve forward from the known starting point.
10.2 Euler's method
Euler's method steps yi+1 = yi + h f(xi, yi): follow the current slope for one step. It is the simplest solver and the clearest to understand, but its first-order accuracy means small steps are needed and error accumulates.
| Method | Slopes per step | Global order |
|---|---|---|
| Euler | 1 (at the start) | O(h) |
| Heun | 2 (start and predicted end) | O(h2) |
| Runge-Kutta 4 | 4 (across the step) | O(h4) |
More slope evaluations per step buy a higher order, so a larger step can be used for the same accuracy.
10.3 Heun's method
Heun's method predicts an end point with Euler, evaluates the slope there, and steps with the average of the start and end slopes. This predictor-corrector cancels the leading Euler error, making it second-order for one extra function evaluation.
10.4 The Runge-Kutta method
The classical fourth-order Runge-Kutta method evaluates four slopes, k1 at the start, k2 and k3 at the midpoint, and k4 at the end, then steps with the weighted average (k1 + 2k2 + 2k3 + k4)/6. Its fourth-order accuracy makes it the default for smooth systems.
10.5 Local and global error
Each step introduces a local truncation error; over many steps these accumulate into a global error one order lower than the local one. A method described by its global order, Euler O(h), RK4 O(h4), tells you how the total error shrinks as the step is reduced.
Engineering connection: every dynamic simulation, from a vehicle model to a chemical reactor, marches its governing ODEs with a Runge-Kutta or related solver.
Worked example 1: Euler's method
Solve dy/dx = x + y with y(0) = 1 using Euler's method with h = 0.1, for two steps. The exact solution is y = 2ex − x − 1.
- ProblemSolve the ODE by Euler for two steps of h = 0.1, as in Figure 1.
- Given / finddy/dx = x + y, y(0) = 1, h = 0.1. Find y(0.1) and y(0.2), with errors against the exact solution.
- AssumptionsConstant step; the slope is taken at the start of each step.
- Modelyi+1 = yi + h f(xi, yi), with f(x, y) = x + y.
- Equationsyi+1 = yi + h(xi + yi)exact: y = 2ex − x − 1
- SolveStep 1: y(0.1) = 1 + 0.1(0 + 1) = 1.1000 (exact 1.1103, error 0.93%). Step 2: y(0.2) = 1.1 + 0.1(0.1 + 1.1) = 1.1 + 0.12 = 1.2200 (exact 1.2428, error 1.84%).
- CheckThe estimates fall below the true values and the error grows from step to step, the accumulation expected of a first-order method on a concave-up curve.
- ConclusionEuler traces the solution but drifts: nearly 2 percent low after two steps. Smaller steps or a higher-order method would reduce the drift.
Worked example 2: the fourth-order Runge-Kutta method
Take one step of the same ODE dy/dx = x + y, y(0) = 1, with the classical fourth-order Runge-Kutta method and h = 0.1, and compare to Euler.
- ProblemTake one RK4 step of the ODE and compare to Euler, as in Figure 2.
- Given / finddy/dx = x + y, y(0) = 1, h = 0.1. Find y(0.1) by RK4 and its error.
- AssumptionsSmooth f; the four slope evaluations are exact.
- Modelk1 = f(x, y); k2 = f(x + h/2, y + h k1/2); k3 = f(x + h/2, y + h k2/2); k4 = f(x + h, y + h k3); yi+1 = yi + (h/6)(k1 + 2k2 + 2k3 + k4).
- Equationsk1 = 0 + 1 = 1k2 = 0.05 + 1.05 = 1.1; k3 = 0.05 + 1.055 = 1.105k4 = 0.1 + 1.1105 = 1.2105
- Solvey(0.1) = 1 + (0.1/6)(1 + 2(1.1) + 2(1.105) + 1.2105) = 1 + (0.1/6)(6.6205) = 1 + 0.110342 = 1.110342. Exact is 1.110342, error ≈ 0.0000002.
- CheckRK4 matches the exact value to six decimals in a single step, against Euler's 0.93 percent error, the dramatic gain of fourth-order accuracy.
- ConclusionRunge-Kutta delivers near-exact results where Euler drifts, for four slope evaluations per step. It is the standard solver because that trade is almost always worth it.
Misconceptions and diagnostics
| Mistake | Symptom | Diagnostic question | Correction |
|---|---|---|---|
| Euler with a large step | Solution drifts or goes unstable | "Is the step small enough?" | Reduce h or use a higher-order method. |
| Local vs global order confused | Accuracy over the run misjudged | "Is this the local or global order?" | Global error is one order below local. |
| Reusing the start slope in RK4 | Method collapses to Euler | "Did I evaluate all four slopes?" | Sample k1 through k4 at their own points. |
| Ignoring stability | A stiff system blows up | "Is the problem stiff?" | Stiff equations need an implicit or specialised solver. |
Practice ladder
For dy/dx = −y, y(0) = 1, take one Euler step with h = 0.1.
Show answer
y(0.1) = 1 + 0.1(−1) = 0.9. The exact value is e−0.1 = 0.905, so Euler is slightly low.
Take a second Euler step of that problem to find y(0.2).
Show answer
y(0.2) = 0.9 + 0.1(−0.9) = 0.81. The exact value is e−0.2 = 0.819, so the error grows.
If Euler has 1 percent error at h = 0.1, roughly what error would you expect at h = 0.05, and what about RK4?
Show answer
Euler is O(h), so halving h roughly halves the error to about 0.5 percent. RK4 is O(h4), so halving h cuts its error by a factor of sixteen.
You must simulate a vehicle dynamics model over a long run. Argue for a solver and step size, and how you would confirm the result is accurate.
What good work looks like
Use RK4 (or an adaptive Runge-Kutta) for its fourth-order accuracy, with a step small enough that halving it changes the result negligibly; confirm accuracy by a step-halving convergence check and, where possible, conservation or energy checks over the run.
Working with AI, and proving it yourself
Use AI as an examiner, not a solver
Portfolio task
Solve a real first-order ODE by Euler and by RK4, compare both to the exact solution, and show RK4's error falling far faster as you halve the step.
Retrieval and spaced review
Closed notes. Answer out loud, then reveal.
1. Write Euler's method.
yi+1 = yi + h f(xi, yi).
2. How does Heun's method improve on Euler?
It averages the start slope with a predicted end slope, reaching second order.
3. How many slopes does RK4 use, and how?
Four, combined as (k1 + 2k2 + 2k3 + k4)/6.
4. Give the global orders of Euler and RK4.
Euler O(h), RK4 O(h4).
5. How does global error relate to local error?
The global error is one order lower than the local truncation error.
Textbook mapping
This module follows Chapra and Canale, Numerical Methods for Engineers, 7th edition. Use these references to read further.
| Topic in this module | Where to read more |
|---|---|
| Euler's method and the initial-value problem | Chapra & Canale, Chapter 25 |
| Heun's method and Runge-Kutta | Chapra & Canale, Chapter 25 |
| Local and global error, stability | Chapra & Canale, Chapter 25 |
Chapter numbers refer to the 7th edition. The ODE solvers are standard, so any recent edition will align closely.