Finite Differences for BVPs
Instead of shooting from one end, we can discretize the whole interval at once: replace derivatives by finite differences at every grid point, and the boundary value problem becomes one system of equations for all the unknown values together. For linear problems the system is linear and banded, so it is solved in operations. This finite difference method is the gateway to numerical PDEs.
Discretizing a linear BVP
Consider on with , . On the grid , , use the centered second difference (Finite Differences):
with and moved to the right-hand side. The matrix is tridiagonal: .
import numpy as np
import scipy.sparse as sp
from scipy.sparse.linalg import spsolve
def solve_bvp_fd(q, f, a, b, alpha, beta, n):
x = np.linspace(a, b, n + 1)
h = (b - a) / n
xi = x[1:-1]
main = 2 / h**2 + q(xi)
off = -np.ones(n - 2) / h**2
A = sp.diags([off, main, off], [-1, 0, 1], format="csc")
rhs = f(xi).astype(float)
rhs[0] += alpha / h**2
rhs[-1] += beta / h**2
u = np.empty(n + 1); u[0], u[-1] = alpha, beta
u[1:-1] = spsolve(A, rhs)
return x, u
# -u'' + u = (1 + pi^2) sin(pi x), u(0) = u(1) = 0, exact u = sin(pi x)
q = lambda x: np.ones_like(x)
f = lambda x: (1 + np.pi**2) * np.sin(np.pi * x)
prev = None
for n in (10, 20, 40, 80, 160):
x, u = solve_bvp_fd(q, f, 0, 1, 0, 0, n)
err = np.max(np.abs(u - np.sin(np.pi * x)))
print(f"n = {n:3d}: max error {err:.3e}" + ("" if prev is None else f", ratio {prev / err:.2f}"))
prev = errn = 10: max error 7.499e-03
n = 20: max error 1.869e-03, ratio 4.01
n = 40: max error 4.669e-04, ratio 4.00
n = 80: max error 1.167e-04, ratio 4.00
n = 160: max error 2.917e-05, ratio 4.00
Second-order convergence, as the centered difference predicts.
First-derivative terms and boundary conditions
- A term uses the centered difference , keeping second order. If is large compared with (convection-dominated problems), centered differences oscillate and upwind differences or finer grids are needed.
- A Neumann condition adds as an unknown; a second-order treatment uses a ghost point with .
Nonlinear BVPs
For a nonlinear equation such as , the discrete equations are solved by Newton's method; the Jacobian is tridiagonal, so each Newton step is still (Newton's Method for Systems).
import numpy as np
from scipy.linalg import solve_banded
n = 100
x = np.linspace(0, 1, n + 1); h = 1 / n
u = 4 - 3 * x # initial guess: straight line through the boundary values
for it in range(8):
ui = u[1:-1]
F = (u[:-2] - 2 * ui + u[2:]) / h**2 - 1.5 * ui**2
ab = np.zeros((3, n - 1))
ab[0, 1:] = 1 / h**2; ab[2, :-1] = 1 / h**2
ab[1] = -2 / h**2 - 3 * ui
du = solve_banded((1, 1), ab, -F)
u[1:-1] += du
print(f"Newton {it + 1}: max |update| = {np.max(np.abs(du)):.1e}")
if np.max(np.abs(du)) < 1e-12:
break
print(f"max error vs 4/(1+x)^2: {np.max(np.abs(u - 4 / (1 + x)**2)):.2e}")Newton 1: max |update| = 7.2e-01
Newton 2: max |update| = 4.3e-02
Newton 3: max |update| = 1.5e-04
Newton 4: max |update| = 1.9e-09
Newton 5: max |update| = 2.1e-16
max error vs 4/(1+x)^2: 4.78e-05
Newton converges quadratically to the same solution that shooting found in The Shooting Method, with a discretization error of order .
In software
scipy.integrate.solve_bvp(fun, bc, x, y)(collocation with mesh refinement) for systems of first-order BVPs.scipy.sparse.diagsandspsolve(orsolve_banded) for the linear algebra; the same pattern extends to 2D and 3D (Poisson's Equation in 2D).
Exercises
Exercise 1: The equations
Write the finite difference equations for , , with .
Answer
and .
Solution
, , , . Solving gives , , exactly , because the centered difference is exact for cubics.
Exercise 2: Cost
How does the cost of solving the tridiagonal system grow with ?
Answer
Linearly, .
Solution
Tridiagonal elimination (Banded and Positive Definite Matrices).
Exercise 3: A Neumann condition (code)
Solve on with (ghost point) and . The exact solution is . Report the max error for and .
Answer
Errors about and : second order.
Solution
At the ghost value gives the equation .
import numpy as np
from scipy.linalg import solve_banded
for n in (50, 100):
x = np.linspace(0, 1, n + 1); h = 1 / n
f = np.pi**2 / 4 * np.cos(np.pi * x / 2)
ab = np.zeros((3, n)); ab[1] = 2 / h**2; ab[0, 1:] = -1 / h**2; ab[2, :-1] = -1 / h**2
ab[0, 1] = -2 / h**2 # ghost point doubles the coupling in row 0
u = solve_banded((1, 1), ab, f[:-1])
print(n, f"{np.max(np.abs(u - np.cos(np.pi * x[:-1] / 2))):.2e}")50 8.23e-05
100 2.06e-05
Test yourself
Discretizing with centered differences gives a matrix that is…
- dense
- tridiagonal
- diagonal
- upper triangular
The finite difference method for is accurate to…
A nonlinear BVP discretized by finite differences is solved by…
- one linear solve
- Newton's method on the discrete system
- bisection
- the QR algorithm