14.3. Boundary Value Problems#
Another class of ODEs are boundary value problems (BVPs), where conditions on the solution are given at two different values for the independent variable (instead of at only an initial time, as for the IVPs). In these problems the independent variable is often a spatial coordinate, so we denote it by \(x\). A model BVP problem is the Poisson equation with Dirichlet conditions at the endpoints, for example:
This problem can be solved using finite differences. Introduce \(n+2\) points between 0 and 1, a grid spacing \(h = 1/(n+1)\) and a corresponding grid of points \(x_j = jh\), \(j=0,1,\ldots,n+1\). At each of these grid points, we will approximate the solution numerically, that is, \(u_j \approx u(x_j)\). To impose the differential equations, we need to estimate the second derivative \(u''(x)\) at each grid point. We can do this using finite difference approximations, for example the second-order accurate formula
Using this we can approximate the differential equation at all interior points:
The boundary values are simply imposed by setting \(u_0 = \alpha\) and \(u_{n+1} = \beta\). This leads to a tridiagonal linear system of equations \(Au=b\) where
14.3.1. Example BVP#
For example we consider the boundary value problem
We use a grid with \(n=49\) interior points (50 intervals).
using PyPlot, PyCall, LinearAlgebra
n = 49
h = 1 / (n+1)
x = h*(1:n)
f(x) = 10exp(2x)*sin(2π*x)
A = SymTridiagonal(-2ones(n), ones(n))
b = h^2*f.(x)
b[1] -= -1
b[end] -= 1
u = A \ b
plot(x, u)
grid(true)