Numerical Solutions of Ordinary Differential Equations
Euler and Runge–Kutta Methods for Initial Value Problems
Most differential equations that come out of real models, such as drag on a falling object, a reacting chemical mixture or a predator–prey population, have no closed-form solution. The numerical solution of ordinary differential equations sidesteps this by building the solution one small step at a time. You start from an initial value problem
and produce approximations yn≈y(tn) at equally spaced times tn=t0+nh, where h is the step size.
Euler's method: follow the tangent
The differential equation tells you the slope of the solution at any point. Euler's method uses the slope at the start of a step as if it held across the whole step:
Example: For dy/dt=0.3y with y(0)=2 and h=0.5, the starting slope is f(0,2)=0.6, so y1=2+0.5(0.6)=2.3. The exact value is y(0.5)=2e0.15≈2.324, so one step has already lost about 0.024.
Each step makes a small local truncation error, proportional to h2 for Euler. Reaching a fixed end time takes (tend−t0)/h steps, so these errors accumulate into a global error proportional to h. A method whose global error scales like hp has order of accuracy p. Euler is first order: halving h roughly halves the error and doubles the work.
Runge–Kutta methods: sample the slope more than once
Runge–Kutta methods evaluate f at several points inside the step and combine those slopes into a better average. Heun's method, a second-order Runge–Kutta method, takes an Euler predictor step, measures the slope at its end and averages the two slopes:
The classical fourth-order method, RK4, samples the slope four times: once at the start, twice at the midpoint and once at the end, then weights the midpoint slopes double:
Method | Slope evaluations per step | Global error | Error after halving h |
|---|---|---|---|
Euler | 1 | O(h) | about 21 as large |
Heun (RK2) | 2 | O(h2) | about 41 as large |
Classical RK4 | 4 | O(h4) | about 161 as large |
Step size, stability and adaptive solvers
Accuracy is not the only reason to choose h carefully. Apply Euler to the decay equation dy/dt=−λy with λ>0: each step multiplies the solution by 1−hλ. The exact solution always decays, but Euler's estimates decay only when 0<hλ<2. A larger step makes the estimates oscillate with growing amplitude, no matter how carefully you do the arithmetic. Problems with some very fast-decaying components are called stiff; for them the stability limit, not accuracy, dictates the step size of explicit methods such as Euler and RK4.
In practice you rarely fix h by hand. SciPy's solve_ivp uses adaptive step control: its default method, RK45, computes fourth- and fifth-order estimates from the same slope evaluations, treats their difference as an error estimate, and shrinks or grows h to keep that estimate within the tolerances rtol and atol. For stiff problems, switch to an implicit method such as method="Radau" or method="BDF".
from scipy.integrate import solve_ivp
sol = solve_ivp(lambda t, y: 0.3 * y, (0, 10), [2.0], rtol=1e-6, atol=1e-9)
print(sol.t[-1], sol.y[0, -1])What to remember
- Euler's method: yn+1=yn+hf(tn,yn); it is first order.
- A method of order p has global error proportional to hp, so halving h divides the error by about 2p.
- RK4 uses four slope evaluations per step and is fourth order.
- Euler on dy/dt=−λy decays only when 0<hλ<2; stiff problems call for implicit methods.
- Library solvers such as solve_ivp choose h adaptively to meet the tolerances you set.
Example: Cooling a cup of coffee with Euler and RK4
A cup of coffee at 90 °C is left in a room held at 20 °C. Newton's law of cooling models its temperature T, in °C, after t minutes as
where the cooling constant 0.1 min−1 was measured for this cup. You want the temperature after 4 minutes, and you will compare two numerical methods with the exact answer. Throughout, f(t,T)=−0.1(T−20); it happens not to depend on t directly.
a) Use Euler's method with step size h=2 min to estimate T(4).
At t0=0 the slope is f(0,90)=−0.1(90−20), which is ________.
The first Euler step gives the temperature at t1=2 min as T1=90+2(−7), which is ________.
The slope there is f(2,76)=−0.1(76−20)=−5.6 ∘C/min, so
Euler's method estimates T(4)≈64.8 °C.
b) Use one step of the classical fourth-order Runge–Kutta method (RK4) with h=4 min to estimate T(4).
RK4 samples four slopes across the single step from t=0 to t=4 min.
- k1=f(0,90)=−7 ∘C/min, the slope at the start.
- For k2, step half-way using k1: T=90+2(−7)=76, so k2 is ________.
- For k3, step half-way using k2: T=90+2(−5.6)=78.8, so k3 is ________.
- For k4, take a full step using k3: T=90+4(−5.88)=66.48, so k4 is ________.
The weighted average slope is (k1+2k2+2k3+k4)/6=−34.608/6, which is ________. Then
RK4 estimates T(4)≈66.93 °C.
c) Solve the equation exactly, then find the error of each estimate at t=4 min.
The equation is separable. With u=T−20 it becomes du/dt=−0.1u, so u=70e−0.1t and
At t=4 min, e−0.4 is ________, so T(4)=20+70(0.670320)=66.9224 °C.
The difference between the exact value and a numerical estimate at the end of the interval is the global error:
Euler used two slope evaluations and RK4 used four, yet RK4's error is about 380 times smaller. Compare methods by error per slope evaluation, not per step: on smooth problems like this one, the extra evaluations of RK4 pay for themselves many times over.
d) Euler's method is first order. Predict its error at t=4 min if the step size is halved to h=1 min, then check your prediction.
For a first-order method the global error is roughly proportional to h, so halving h should roughly halve the error: about 2.12/2≈1.06 °C.
To check, notice that each Euler step multiplies T−20 by 1−0.1h=0.9. Four steps give T−20=70(0.9)4, where (0.9)4 is ________. So T(4)≈20+45.927=65.927 °C, and the error is 66.9224−65.927≈1.00 °C.
The error fell from 2.12 °C to 1.00 °C, a factor of about 2.1, close to the predicted factor of 2. That is the signature of a first-order method; the small mismatch arises because the proportionality is exact only in the limit h→0.
A quantity y obeys dtdy=2t−y with y(0)=1. Apply Euler's method with step size h=0.5 and complete the table, where the slope column is f(tn,yn)=2tn−yn evaluated on each row. Enter exact decimal values.
| n | t | y | Slope 2t - y |
| 0 | 0 | 1 | |
| 1 | 0.5 | ||
| 2 | 1 | ||
| 3 | 1.5 | not needed |
After an intravenous dose, a drug's plasma concentration C (in mg/L) falls according to dtdC=−0.5C, with t in hours and C(0)=4 mg/L. Take one step of Heun's method (the improved Euler method) with h=0.4 h to estimate C(0.4). Report each of the following to two decimal places: the starting slope k1 in mg/L per hour, the Euler predictor C∗ in mg/L, the end-of-step slope k2 in mg/L per hour, and the Heun estimate C1 in mg/L.