21  P3: Chapter 3

Problem Class 3: Finite Differences

Published

April 6, 2026

Abstract
Exercises based on content from Lectures 12-15
using Plots, LinearAlgebra, LaTeXStrings

Exercise 21.1 So far, we have considered homogeneous boundary conditions u(0) = u(1) = 0. How would you approximate the problem

\begin{align} &-u'' + p u' + q u = f \qquad \text{on }(0,1) \nonumber\\ &u(0) = \alpha, \qquad u(1) = \beta \end{align}

with finite differences?

Exercise 21.2 So far, we have considered ADR

\begin{align} -u'' + p u' + q u = f \end{align}

with constant coefficients p,q. How would you write down a finite difference approximation to this equation if p and q are functions of x?

Exercise 21.3 (FNC example 10.4.2) Following on from the previous two questions, we try and solve the problem

\begin{align} &-u''(x) + a \cos(ax) u'(x) - a^2\sin(ax) u(x) = 0\nonumber\\ &u(0) = 1 \nonumber\\ &u(\tfrac{3\pi}2) = e^{-1}. \end{align}

Show that the exact solution is u(x) = e^{\sin ax}.

Spot and correct the error in the following code [we did this in class - instead tyr and understand the following code]:

"""
    adr_1d(n, p, q, f; a=0,b=1, α=0,β=0)

Approximate the solution to 
    -u′′ + p u′ + q u = 0 on (a,b)
    u(a) = α, u(b) = β
(linear Advection-Diffusion-Reaction equation with Dirichlet boundary conditions)
using central finite differences
"""
function adr_1d(n, p, q, f; a=0.,b=1., α=0.,β=0.)
    # mesh size
    h = (b-a)/n 
    # interior grid points 
    x = [ a + j *(b-a)/n for j  1:n-1 ] 

    # Finite Difference Coefficients
    # (using a second order central difference) 
    d = (2/h^2) * ones(n-1) + q.(x)
    d₋  = (-1/h^2)*ones(n-2) - p.(x[2:end])/(2h)
    d₊  = (-1/h^2)*ones(n-2) + p.(x[1:end-1])/(2h)
    Lh = Tridiagonal(d₋, d, d₊)
    F = f.(x)
    F[1] += (1/h^2 + p(x[1])/(2h))*α
    F[end] += (1/h^2 - p(x[end])/(2h))*β
    U = Lh \ F

    # add the boundary conditions
    U = [α, U..., β]
    x = [a, x..., b]

    return x, U
end

p(x) = cos(x)
q(x) = -sin(x)
f(x) = 0.    

x, u = adr_1d(50, p, q, f; a=0.,b=3π/2, α=1.,β=exp(-1))
P1 = scatter(x, u, title=L"$-u′′ + \cos(x) u′ - \sin(x) u = 0$", 
     label="finite difference approximation", markersize=2)
u_e(x) = exp(sin(x))
plot!( u_e, label="exact solution" )

Exercise 21.4 (i) Show that \hat{U}_0 = \frac{1}{\sqrt{n}} \sum_j U_j.
(ii) Write -L_{\rm per} U = \bm f in terms of \hat{U}_k and \hat{F}_k,
(iii) Hence show that, if F has zero mean \sum_{j=0}^{n-1} f(x_j) = 0, then there exists U solving -L_{\rm per} U = F.

Exercise 21.5 Read the introduction to the book

“Brigham, E. O. (1988). The Fast Fourier Transform and Its Applications.”