TExcelTAD
Home › Numerical Methods › Finite Differences for Boundary Value Problems

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 O(n)O(n) operations. This finite difference method is the gateway to numerical PDEs.

Discretizing a linear BVP

Consider −u′′+q(x)u=f(x)-u'' + q(x)u = f(x) on [a,b][a, b] with u(a)=αu(a) = \alpha, u(b)=βu(b) = \beta. On the grid xi=a+ihx_i = a + ih, h=b−anh = \frac{b - a}{n}, use the centered second difference (Finite Differences):

−ui−1−2ui+ui+1h2+q(xi)ui=f(xi),i=1,…,n−1,-\frac{u_{i-1} - 2u_i + u_{i+1}}{h^2} + q(x_i)u_i = f(x_i), \qquad i = 1, \ldots, n - 1,

with u0=αu_0 = \alpha and un=βu_n = \beta moved to the right-hand side. The matrix is tridiagonal: 1h2tridiag⁡(−1,2,−1)+diag⁡(q)\frac{1}{h^2}\operatorname{tridiag}(-1, 2, -1) + \operatorname{diag}(q).

python · try it yourself
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 = err
n =  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 p(x)u′p(x)u' uses the centered difference ui+1−ui−12h\frac{u_{i+1} - u_{i-1}}{2h}, keeping second order. If pp is large compared with 1h\frac{1}{h} (convection-dominated problems), centered differences oscillate and upwind differences or finer grids are needed.
  • A Neumann condition u′(b)=γu'(b) = \gamma adds unu_n as an unknown; a second-order treatment uses a ghost point un+1u_{n+1} with un+1−un−12h=γ\frac{u_{n+1} - u_{n-1}}{2h} = \gamma.

Nonlinear BVPs

For a nonlinear equation such as u′′=1.5u2u'' = 1.5u^2, the discrete equations F(u)=0\mathbf{F}(\mathbf{u}) = \mathbf{0} are solved by Newton's method; the Jacobian is tridiagonal, so each Newton step is still O(n)O(n) (Newton's Method for Systems).

python · try it yourself
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 h2h^2.

In software

  • scipy.integrate.solve_bvp(fun, bc, x, y) (collocation with mesh refinement) for systems of first-order BVPs.
  • scipy.sparse.diags and spsolve (or solve_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 u′′=6xu'' = 6x, u(0)=0u(0) = 0, u(1)=1u(1) = 1 with n=3n = 3.

Answer

0−2u1+u21/9=2\frac{0 - 2u_1 + u_2}{1/9} = 2 and u1−2u2+11/9=4\frac{u_1 - 2u_2 + 1}{1/9} = 4.

Solution

h=13h = \frac{1}{3}, x1=13x_1 = \frac{1}{3}, x2=23x_2 = \frac{2}{3}, 6xi=2,46x_i = 2, 4. Solving gives u1=127u_1 = \frac{1}{27}, u2=827u_2 = \frac{8}{27}, exactly x3x^3, because the centered difference is exact for cubics.

Exercise 2: Cost

How does the cost of solving the tridiagonal system grow with nn?

Answer

Linearly, O(n)O(n).

Solution

Tridiagonal elimination (Banded and Positive Definite Matrices).

Exercise 3: A Neumann condition (code)

Solve −u′′=π2cos⁡(πx/2)/4-u'' = \pi^2\cos(\pi x/2)/4 on [0,1][0, 1] with u′(0)=0u'(0) = 0 (ghost point) and u(1)=0u(1) = 0. The exact solution is cos⁡(πx/2)\cos(\pi x/2). Report the max error for n=50n = 50 and n=100n = 100.

Answer

Errors about 10−410^{-4} and 2.5×10−52.5 \times 10^{-5}: second order.

Solution

At x0x_0 the ghost value u−1=u1u_{-1} = u_1 gives the equation 2u0−2u1h2=f0\frac{2u_0 - 2u_1}{h^2} = f_0.

python · try it yourself
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 −u′′=f-u'' = f with centered differences gives a matrix that is…

  1. dense
  2. tridiagonal
  3. diagonal
  4. upper triangular

The finite difference method for −u′′+qu=f-u'' + qu = f is accurate to…

  1. O(h)O(h)
  2. O(h2)O(h^2)
  3. O(h4)O(h^4)
  4. O(1)O(1)

A nonlinear BVP discretized by finite differences is solved by…

  1. one linear solve
  2. Newton's method on the discrete system
  3. bisection
  4. the QR algorithm