Numerical Methods for Ordinary Differential Equations

Interactive Lecture

Euler Method, Runge-Kutta Methods, Error Analysis, and Stability

by Dr Arnaldo Delli Carri

Introduction to ODE Numerical Methods

Many engineering problems involve ordinary differential equations (ODEs) that cannot be solved analytically. Numerical methods provide approximate solutions by discretising the continuous problem into a sequence of algebraic calculations.

Numerical ODE solvers approximate the solution by taking small steps in time, using the differential equation to predict how the solution evolves. The accuracy and stability of these methods depend on the step size and the mathematical properties of the algorithm.

The Initial Value Problem

We seek to solve differential equations of the form:

\[\frac{dy}{dt} = f(t, y), \quad y(t_0) = y_0\]

Where:

1. Euler's Method (Forward Euler)

The Euler method is the simplest numerical method, based on a first-order Taylor series approximation.

Algorithm:

\[y_{n+1} = y_n + h \cdot f(t_n, y_n)\]

Where:

Geometric Interpretation: The method follows the tangent line at \((t_n, y_n)\) for a distance \(h\).

Local Truncation Error: \(O(h^2)\)

Global Error: \(O(h)\) - first-order accurate

Euler's method is simple but has poor accuracy and stability. It requires very small step sizes for acceptable results and can be unstable for stiff equations.

2. Modified Euler Method (Heun's Method)

An improvement over Euler's method that uses a predictor-corrector approach:

Predictor:

\[\tilde{y}_{n+1} = y_n + h \cdot f(t_n, y_n)\]

Corrector:

\[y_{n+1} = y_n + \frac{h}{2}\left[f(t_n, y_n) + f(t_{n+1}, \tilde{y}_{n+1})\right]\]

Global Error: \(O(h^2)\) - second-order accurate

3. Runge-Kutta Methods

Runge-Kutta (RK) methods achieve higher accuracy by evaluating the derivative at multiple intermediate points.

Second-Order Runge-Kutta (RK2):

\[k_1 = h \cdot f(t_n, y_n)\]

\[k_2 = h \cdot f\left(t_n + \frac{h}{2}, y_n + \frac{k_1}{2}\right)\]

\[y_{n+1} = y_n + k_2\]

Global Error: \(O(h^2)\)

4. Fourth-Order Runge-Kutta (RK4)

The most widely used method, offering an excellent balance of accuracy and computational cost:

RK4 Algorithm:

\[k_1 = h \cdot f(t_n, y_n)\]

\[k_2 = h \cdot f\left(t_n + \frac{h}{2}, y_n + \frac{k_1}{2}\right)\]

\[k_3 = h \cdot f\left(t_n + \frac{h}{2}, y_n + \frac{k_2}{2}\right)\]

\[k_4 = h \cdot f(t_n + h, y_n + k_3)\]

\[y_{n+1} = y_n + \frac{1}{6}(k_1 + 2k_2 + 2k_3 + k_4)\]

Global Error: \(O(h^4)\) - fourth-order accurate

RK4 evaluates the derivative at four points: the start, two midpoints, and the end of the interval. It then takes a weighted average, giving more weight to the midpoint evaluations.

Error Analysis

Knowing where your numerical errors come from — and how large they are — is what makes a solution trustworthy:

Local Truncation Error (LTE): Error introduced in a single step

Global Error: Accumulated error over many steps

Order of Accuracy:

Absolute Error: \(|y_{true} - y_{numerical}|\)

Relative Error: \(\frac{|y_{true} - y_{numerical}|}{|y_{true}|}\)

Stability Analysis

Stability determines whether errors grow or decay as the solution progresses:

Test Equation: \(\frac{dy}{dt} = \lambda y\) where \(\lambda < 0\)

Stability Criterion: For Euler's method:

\[|1 + h\lambda| \leq 1 \implies h \leq \frac{2}{|\lambda|}\]

Stiff Equations: Equations with widely different time scales (large negative \(\lambda\)) require very small step sizes for explicit methods or implicit methods for stability.

Adaptive Step Size Control

Modern ODE solvers adjust step size automatically to maintain accuracy:

Embedded RK Methods: Use two approximations of different orders

Error estimate: \(\epsilon = |y_{higher} - y_{lower}|\)

If \(\epsilon > \text{tolerance}\): reduce \(h\)

If \(\epsilon < \text{tolerance}\): increase \(h\)

Systems of ODEs

All methods extend naturally to systems of equations:

\[\frac{d\mathbf{y}}{dt} = \mathbf{f}(t, \mathbf{y}), \quad \mathbf{y}(t_0) = \mathbf{y}_0\]

where \(\mathbf{y}\) and \(\mathbf{f}\) are vectors. Apply the method component-wise.

Example: Second-order ODE \(\frac{d^2y}{dt^2} = g(t, y, \frac{dy}{dt})\)

Convert to system: \(y_1 = y, \quad y_2 = \frac{dy}{dt}\)

\[\frac{dy_1}{dt} = y_2, \quad \frac{dy_2}{dt} = g(t, y_1, y_2)\]

Interactive Simulation: Comparing ODE Solvers

Compare different numerical methods solving the same differential equation. Observe how accuracy varies with step size and method choice.

0.10
1.0

Computational Cost

Euler: 1 function evaluation per step
RK2: 2 function evaluations per step
RK4: 4 function evaluations per step

The absolute error of each method at the final time is overlaid on the plot — read accuracy against cost.

Engineering Applications

Summary of Key Equations

Method Algorithm Order Evaluations/Step Stability
Euler \(y_{n+1} = y_n + hf(t_n, y_n)\) 1st 1 Conditionally stable
Modified Euler \(y_{n+1} = y_n + \frac{h}{2}[f(t_n, y_n) + f(t_{n+1}, \tilde{y}_{n+1})]\) 2nd 2 Better than Euler
RK2 Midpoint method 2nd 2 Good
RK4 \(y_{n+1} = y_n + \frac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4)\) 4th 4 Excellent

Important Concepts

Practice Problems

Problem 1: Euler's Method Application

Use Euler's method with step size \(h = 0.2\) to solve \(\frac{dy}{dt} = -2y\) with \(y(0) = 1\) from \(t = 0\) to \(t = 0.4\). Compare with the exact solution \(y(t) = e^{-2t}\).

Show Solution

Problem 2: RK4 Method

Apply one step of RK4 with \(h = 0.5\) to solve \(\frac{dy}{dt} = t + y\) with \(y(0) = 1\).

Show Solution

Problem 3: Error Analysis and Step Size Selection

For the equation \(\frac{dy}{dt} = -10y\) with \(y(0) = 1\):

(a) What is the maximum step size for Euler's method to be stable?
(b) Compute \(y(0.2)\) using step sizes \(h = 0.05\) and \(h = 0.025\), and verify that the error ratio is consistent with Euler being a first-order method.

Show Solution

Problem 4: System of ODEs

Use RK4 with \(h = 0.1\) to take one step for the system:

\[\frac{dx}{dt} = y, \quad \frac{dy}{dt} = -x, \quad x(0) = 1, \quad y(0) = 0\]

Show Solution

Knowledge Check Quiz

Q1: Euler's method is a:

Q2: How many function evaluations does RK4 require per time step?

Q3: If the step size is halved, the global error in RK4 is reduced by approximately:

Q4: Stiff equations are characterised by:

Q5: The local truncation error for Euler's method is:

Q6: Which method generally provides the best balance of accuracy and computational cost?

Q7: To convert a second-order ODE to a system of first-order ODEs, we:

Q8: Adaptive step size methods adjust h based on: