5.3. The finite-difference method#
The finite-difference method for solving a boundary value problem replaces the derivatives in the ODE with finite-difference approximations derived from the Taylor series. This results in linear system of algebraic equations that can be solved to give an approximation of the solution to the BVP.
The derivatives of \(y_i\) are approximated using values of neighbouring nodes \(y_{i+1}\), \(y_{i-1}\) etc. using expressions derived by truncating the Taylor series and rearranging to make the derivative term the subject. Some common finite-difference approximations are tabulated below.
Finite-difference approximation |
Error |
Name |
---|---|---|
\(y' = \dfrac{y_{i+1} - y_i}{h}\) |
\(O(h)\) |
Forward difference |
\(y' = \dfrac{y_i - y_{i-1}}{h}\) |
\(O(h)\) |
Backward difference |
\(y' = \dfrac{y_{i+1} - y_{i-1}}{2h}\) |
\(O(h^2)\) |
Central difference |
\(y'' = \dfrac{y_{i-1} - 2 y_i + y_{i+1}}{h^2}\) |
\(O(h^2)\) |
Symmetric difference |
The solution to a boundary value problem using then finite-difference method is determined by approximating the derivatives in the ODE using finite-differences. Consider the following boundary value problem
Using the symmetric difference to approximate \(y''\) in the ODE we have
Since we know that \(y_0 = a\) and \(y_n = b\) then
which can be written as the matrix equation
Example 5.4
Use the finite-difference method to solve the boundary below using a step length of \(h = 0.2\)
Solution
Using a forward difference to approximate \(y'\) and a symmetric difference to approximate \(y''\) we have
which can be simplified to
This gives the following system of linear equations
which we can write the linear system as the matrix equation
If we use a step length of \(h=0.2\) we have
and
Solving this system of linear equations gives
5.3.1. Code#
The code below calculates the solution to the boundary value problem in Example 5.4.
import numpy as np
import matplotlib.pyplot as plt
# Define BVP parameters
tspan = [0, 1] # boundaries of the t domain
bvals = [0, 2] # boundary values
h = 0.2 # step length
# Define linear system
n = int((tspan[1] - tspan[0]) / h) + 1
t = np.arange(n) * h # t array
A = np.eye(n)
b = np.zeros(n)
b[0], b[-1] = bvals[0], bvals[1]
for i in range(1, n-1):
A[i,i-1] = 1
A[i,i] = h - h ** 2 - 2
A[i,i+1] = 1 - h
# Solve linear system
y = np.linalg.solve(A, b)
% Define BVP parameters
tspan = [0, 1];
bvals = [0, 2];
h = 0.2;
% Define linear system
n = floor((tspan(2) - tspan(1)) / h) + 1;
t = (tspan(1) : h : tspan(2));
A = eye(n);
b = zeros(n, 1);
b(1) = bvals(1);
b(n) = bvals(2);
for i = 2 : n - 1
A(i, i-1) = 1;
A(i, i) = h - h ^ 2 - 2;
A(i, i+1) = 1 - h;
end
% Solve linear system
y = A \ b;
5.3.2. Order vs number of nodes#
The solutions seen in Example 5.4 seem to show that the finite-difference method produces reasonably accurate results for this boundary value problem. One way to improve on the accuracy of our solution is to increase the number of nodes used.
Consider the solution of the following BVP using the finite-difference method
Using forward and symmetric differences to approximate \(y'\) and \(y''\) respectively gives
So the linear system is
The solution of this BVP is shown in Fig. 5.4 for \(h=0.05\) and \(h = 0.005\). Since we used a first-order approximation for \(y'\) the error for this method is \(O(h)\) and we expect the solution using \(h=0.005\) to be more accurate.

Fig. 5.4 Solutions to the boundary value problem \(y'' + 3ty' + 7y = \cos (2t)\), \(t \in [0,3]\), \(y(0) = 1\), \(y(3) = 0\) using first-order finite-difference approximations with \(h=0.05\) and \(h=0.005\).#
To obtain a more accurate solution, instead of increasing the number of nodes we could use the central difference approximation (which is second-order accurate) to approximate \(y'\)
So the linear system for the second-order finite-difference method is
The solution using the second-order finite difference method with \(h=0.05\) has been plotted against the first-order solution using \(h=0.05\) and \(h=0.005\) in Fig. 5.5. The second order solution gives good agreement with the first order solution using 10 times fewer nodes.

Fig. 5.5 Solutions to the boundary value problem \(y'' + 3ty' + 7y = \cos (2t)\), \(t \in [0,3]\), \(y(0) = 1\), \(y(3) = 0\) using first-order and second-order finite-difference approximations with \(h=0.05\) and \(h=0.005\).#