Wize Scientific and Numerical Computing Textbook > Numerical Methods

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


dydt=f(t,y),y(t0)=y0\frac{dy}{dt} = f(t, y), \qquad y(t_0) = y_0


and produce approximations yn≈y(tn)y_n \approx y(t_n) at equally spaced times tn=t0+nht_n = t_0 + nh, where hh 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:


yn+1=yn+h f(tn,yn)y_{n+1} = y_n + h\, f(t_n, y_n)


Example: For dy/dt=0.3ydy/dt = 0.3y with y(0)=2y(0) = 2 and h=0.5h = 0.5, the starting slope is f(0,2)=0.6f(0, 2) = 0.6, so y1=2+0.5(0.6)=2.3y_1 = 2 + 0.5(0.6) = 2.3. The exact value is y(0.5)=2e0.15≈2.324y(0.5) = 2e^{0.15} \approx 2.324, so one step has already lost about 0.024.


A rising exact solution curve with three straight Euler steps of width h lying below it and the gap at the third step marked as error.


Each step makes a small local truncation error, proportional to h2h^2 for Euler. Reaching a fixed end time takes (tend−t0)/h(t_{\text{end}} - t_0)/h steps, so these errors accumulate into a global error proportional to hh. A method whose global error scales like hph^p has order of accuracy pp. Euler is first order: halving hh roughly halves the error and doubles the work.



Runge–Kutta methods: sample the slope more than once

Runge–Kutta methods evaluate ff 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:


k1=f(tn,yn),k2=f(tn+h, yn+hk1)yn+1=yn+h2(k1+k2)\begin{aligned} k_1 &= f(t_n, y_n), \qquad k_2 = f(t_n + h,\ y_n + h k_1)\\ y_{n+1} &= y_n + \tfrac{h}{2}(k_1 + k_2) \end{aligned}


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:


k1=f(tn,yn)k2=f(tn+h2, yn+h2k1)k3=f(tn+h2, yn+h2k2)k4=f(tn+h, yn+hk3)yn+1=yn+h6(k1+2k2+2k3+k4)\begin{aligned} k_1 &= f(t_n, y_n)\\ k_2 &= f\left(t_n + \tfrac{h}{2},\ y_n + \tfrac{h}{2}k_1\right)\\ k_3 &= f\left(t_n + \tfrac{h}{2},\ y_n + \tfrac{h}{2}k_2\right)\\ k_4 &= f(t_n + h,\ y_n + h k_3)\\ y_{n+1} &= y_n + \tfrac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4) \end{aligned}


Method

Slope evaluations per step

Global error

Error after halving hh

Euler

1

O(h)O(h)

about 12\tfrac{1}{2} as large

Heun (RK2)

2

O(h2)O(h^2)

about 14\tfrac{1}{4} as large

Classical RK4

4

O(h4)O(h^4)

about 116\tfrac{1}{16} as large



Step size, stability and adaptive solvers

Accuracy is not the only reason to choose hh carefully. Apply Euler to the decay equation dy/dt=−λydy/dt = -\lambda y with λ>0\lambda > 0: each step multiplies the solution by 1−hλ1 - h\lambda. The exact solution always decays, but Euler's estimates decay only when 0<hλ<20 < h\lambda < 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 hh 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 hh 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".


Python
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)y_{n+1} = y_n + h f(t_n, y_n); it is first order.
  • A method of order pp has global error proportional to hph^p, so halving hh divides the error by about 2p2^p.
  • RK4 uses four slope evaluations per step and is fourth order.
  • Euler on dy/dt=−λydy/dt = -\lambda y decays only when 0<hλ<20 < h\lambda < 2; stiff problems call for implicit methods.
  • Library solvers such as solve_ivp choose hh 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 TT, in °C, after tt minutes as


dTdt=−0.1 (T−20),T(0)=90\frac{dT}{dt} = -0.1\,(T - 20), \qquad T(0) = 90


where the cooling constant 0.1 min−10.1\ \text{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)f(t, T) = -0.1(T - 20); it happens not to depend on tt directly.


a) Use Euler's method with step size h=2h = 2 min to estimate T(4)T(4).


At t0=0t_0 = 0 the slope is f(0,90)=−0.1(90−20)f(0, 90) = -0.1(90 - 20), which is ________.


The first Euler step gives the temperature at t1=2t_1 = 2 min as T1=90+2(−7)T_1 = 90 + 2(-7), which is ________.


The slope there is f(2,76)=−0.1(76−20)=−5.6 ∘C/minf(2, 76) = -0.1(76 - 20) = -5.6\ ^\circ\text{C/min}, so


T2=76+2(−5.6)=64.8 ∘CT_2 = 76 + 2(-5.6) = 64.8\ ^\circ\text{C}


Euler's method estimates T(4)≈64.8T(4) \approx 64.8 °C.


b) Use one step of the classical fourth-order Runge–Kutta method (RK4) with h=4h = 4 min to estimate T(4)T(4).


RK4 samples four slopes across the single step from t=0t = 0 to t=4t = 4 min.

  • k1=f(0,90)=−7 ∘C/mink_1 = f(0, 90) = -7\ ^\circ\text{C/min}, the slope at the start.
  • For k2k_2, step half-way using k1k_1: T=90+2(−7)=76T = 90 + 2(-7) = 76, so k2k_2 is ________.
  • For k3k_3, step half-way using k2k_2: T=90+2(−5.6)=78.8T = 90 + 2(-5.6) = 78.8, so k3k_3 is ________.
  • For k4k_4, take a full step using k3k_3: T=90+4(−5.88)=66.48T = 90 + 4(-5.88) = 66.48, so k4k_4 is ________.

The weighted average slope is (k1+2k2+2k3+k4)/6=−34.608/6(k_1 + 2k_2 + 2k_3 + k_4)/6 = -34.608/6, which is ________. Then


T(4)≈90+4(−5.768)=66.928 ∘CT(4) \approx 90 + 4(-5.768) = 66.928\ ^\circ\text{C}


RK4 estimates T(4)≈66.93T(4) \approx 66.93 °C.


c) Solve the equation exactly, then find the error of each estimate at t=4t = 4 min.


The equation is separable. With u=T−20u = T - 20 it becomes du/dt=−0.1udu/dt = -0.1u, so u=70e−0.1tu = 70e^{-0.1t} and


T(t)=20+70e−0.1tT(t) = 20 + 70e^{-0.1t}


At t=4t = 4 min, e−0.4e^{-0.4} is ________, so T(4)=20+70(0.670320)=66.9224T(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: 66.9224−64.8≈2.12 ∘CRK4: 66.928−66.9224≈0.0056 ∘C\begin{aligned} \text{Euler: } & 66.9224 - 64.8 \approx 2.12\ ^\circ\text{C}\\ \text{RK4: } & 66.928 - 66.9224 \approx 0.0056\ ^\circ\text{C} \end{aligned}


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=4t = 4 min if the step size is halved to h=1h = 1 min, then check your prediction.


For a first-order method the global error is roughly proportional to hh, so halving hh should roughly halve the error: about 2.12/2≈1.062.12/2 \approx 1.06 °C.


To check, notice that each Euler step multiplies T−20T - 20 by 1−0.1h=0.91 - 0.1h = 0.9. Four steps give T−20=70(0.9)4T - 20 = 70(0.9)^4, where (0.9)4(0.9)^4 is ________. So T(4)≈20+45.927=65.927T(4) \approx 20 + 45.927 = 65.927 °C, and the error is 66.9224−65.927≈1.0066.9224 - 65.927 \approx 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→0h \to 0.

Match each term used in the numerical solution of ordinary differential equations with the description that fits it.

Match each term
A.

A problem whose fast-decaying components force explicit methods to take tiny steps to stay stable

B.

The error introduced in one step when that step starts from exact data

C.

The fixed increment in the independent variable between successive approximations

D.

The power p for which the accumulated error scales like h raised to the power p

E.

A differential equation together with the value of the unknown function at a starting time

F.

The accumulated difference between the exact and computed solutions at the end of the interval

Initial value problem

Local truncation error

Global truncation error

Step size

Stiff equation

Order of accuracy

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A quantity yy obeys dydt=2t−y\dfrac{dy}{dt} = 2t - y with y(0)=1y(0) = 1. Apply Euler's method with step size h=0.5h = 0.5 and complete the table, where the slope column is f(tn,yn)=2tn−ynf(t_n, y_n) = 2t_n - y_n evaluated on each row. Enter exact decimal values.

ntySlope 2t - y
001
10.5
21
31.5not needed

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

After an intravenous dose, a drug's plasma concentration CC (in mg/L) falls according to dCdt=−0.5C\dfrac{dC}{dt} = -0.5C, with tt in hours and C(0)=4C(0) = 4 mg/L. Take one step of Heun's method (the improved Euler method) with h=0.4h = 0.4 h to estimate C(0.4)C(0.4). Report each of the following to two decimal places: the starting slope k1k_1 in mg/L per hour, the Euler predictor C∗C^* in mg/L, the end-of-step slope k2k_2 in mg/L per hour, and the Heun estimate C1C_1 in mg/L.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice