Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Boundary Value Problems

Open in Colab

In a boundary value problem (BVP) the conditions are split between the two ends of an interval (for a PDE, they are given on the boundary of the domain), so the solution at every point depends on the data at both ends and has to be computed all at once.

The Heat Equation

The Heat Equation is a second-order PDE obeying

Δu(x,t)=∂tu(x,t)\Delta u(x, t) = \partial_t u(x, t)

where Δ\Delta is the Laplacian operator

Δ=∑i∂i2\Delta = \sum_i \partial_i^2

This equation is in a steady state if ∂tu=0\partial_t u = 0 (i.e. the solution is not changing with time).

We’ll consider the steady state of the heat equation on a 1-dimensional domain [0,1][0,1], where we have the ends of the domain set to fixed temperatures. In this case, we can write the solution as a boundary value problem for a second-order ODE:

d2udx2=0x∈(0,1),u(0)=a,u(1)=b.\frac{d^2 u}{dx^2} = 0 \qquad x\in (0,1), \quad u(0) = a, \quad u(1) = b.

You might think of this as describing the temperature of a metal bar which is placed between two objects of differing temperatures.

We’ll introduce the variable p=du/dxp = du / dx to obtain a system of first-order ODEs:

dpdx=0,dudx=p\frac{dp}{dx} = 0, \quad \frac{du}{dx} = p

with the same boundary conditions. The variable y = [u, p] will be a 2-vector containing the state.

We’ll use boundary conditions a=5a = 5 and b=1b = 1

Finally, we use solve_bvp from scipy.integrate to solve the boundary value problem. Its arguments are:

  • f(x, y), the right-hand side dy/dxdy/dx. It is called on all the mesh points at once, with x of shape (m,) and y of shape (2, m) (one column per point), and it must return an array of the same shape as y. np.vstack stacks rows into such an array, as in the example below.

  • bc(ya, yb), which receives the values of y at the two ends and returns the residuals of the boundary conditions, which are zero when the conditions hold. There is one condition per component of y, two here.

  • the initial mesh x, and an initial guess y0 for y on it, of shape (2, m).

we can plot the solution value

We see the solution is a linear function from (0,5) to (1,1). You can check that such a linear function solves the BVP mathematically.

The initial guess y0 was random noise. The problem is linear, so Newton’s method inside solve_bvp finds the solution in one step from any initial guess; for nonlinear problems the guess matters, as we will see.

A heat source

We also provide a non-zero right hand side. If heat is produced inside the bar at a rate s(x)s(x) per unit length, the heat equation becomes ∂tu=∂x2u+s(x)\partial_t u = \partial_x^2 u + s(x), and its steady state satisfies

d2udx2=−s(x).\frac{d^2 u}{dx^2} = -s(x).

We heat the middle of the bar, with s(x)=1s(x) = 1 for 0.4<x<0.60.4 < x < 0.6 and s(x)=0s(x) = 0 elsewhere, and hold both ends at temperature 0. We round off the edges of the source with tanh⁡\tanh over a width of about w=0.01w = 0.01 (see below for why), and this time we start from a coarse mesh of 11 points.

The temperature is linear where there is no source and curves downward where the bar is heated. By Fourier’s law the heat flux is −u′-u', and u′u' drops by 0.2 across the source, which is the total heat produced in (0.4,0.6)(0.4, 0.6); half of it flows out through each end.

Finite Differences

We can also replace derivatives by finite differences, as in Differentiation. Take NN interior points xi=ihx_i = ih, i=1,…,Ni = 1, \dots, N, with spacing h=1/(N+1)h = 1/(N + 1), so that x0=0x_0 = 0 and xN+1=1x_{N+1} = 1 are the ends of the interval. The second difference

u′′(xi)≈ui−1−2ui+ui+1h2u''(x_i) \approx \frac{u_{i-1} - 2u_i + u_{i+1}}{h^2}

has error O(h2)O(h^2), which you can check by adding the Taylor series of u(xi+h)u(x_i + h) and u(xi−h)u(x_i - h). Imposing u′′=f(x)u'' = f(x) at each interior point gives NN linear equations for the NN unknowns ui≈u(xi)u_i \approx u(x_i). The boundary values u0=au_0 = a and uN+1=bu_{N+1} = b are known, so they move to the right-hand side of the first and last equations:

1h2(−211−2⋱⋱⋱11−2)(u1u2⋮uN)=(f(x1)−a/h2f(x2)⋮f(xN)−b/h2).\frac{1}{h^2} \begin{pmatrix} -2 & 1 & & \\ 1 & -2 & \ddots & \\ & \ddots & \ddots & 1 \\ & & 1 & -2 \end{pmatrix} \begin{pmatrix} u_1 \\ u_2 \\ \vdots \\ u_N \end{pmatrix} = \begin{pmatrix} f(x_1) - a/h^2 \\ f(x_2) \\ \vdots \\ f(x_N) - b/h^2 \end{pmatrix}.

The matrix is tridiagonal, so we store it as a sparse matrix, and a sparse direct solve (see Sparse Linear Algebra) takes O(N)O(N) operations.

To check the method we need a problem with a known solution. With the boundary values a=5a = 5 and b=1b = 1 from the first example and f(x)=−π2sin⁡(πx)f(x) = -\pi^2 \sin(\pi x), the solution is u(x)=5−4x+sin⁡(πx)u(x) = 5 - 4x + \sin(\pi x). We measure the largest error on the grid for a sequence of grid sizes, as in Accuracy, Stability, and Stiffness.

The error falls like h2h^2 down to about 10-9 at h≈3×10−5h \approx 3 \times 10^{-5}, below which rounding errors take over, as for the difference quotients in Differentiation.

The same discretization turns the Bratu problem into the NN nonlinear equations

ui−1−2ui+ui+1h2+λeui=0,i=1,…,N,\frac{u_{i-1} - 2u_i + u_{i+1}}{h^2} + \lambda e^{u_i} = 0, \qquad i = 1, \dots, N,

which we can solve with Newton’s method. We do this for the two-dimensional version of the problem at the end of this section.

The Wave Equation

The Wave Equation is another second order PDE obeying

Δu(x,t)=∂t2u(x,t)\Delta u(x, t) = \partial_t^2 u(x, t)

One way to solve the wave equation is to use separation of variables. This gives solutions of the form

u(x,t)=v(x)eiωtu(x,t) = v(x) e^{i\omega t}

where v(x)v(x) satisfies the Helmholtz equation

Δv(x)=−ω2v(x)\Delta v(x) = -\omega^2 v(x)

In other words, v(x)v(x) is an eigenvector of the Laplacian operator with eigenvalue −ω2-\omega^2. The time dependent portion of the solution eiωte^{i\omega t} is simply a sinusoidal oscillation.

In a bounded domain, there is a discrete set of eigenvalues for the Laplacian. We’ll use the construction Δ=−∂T∂\Delta = -\partial^T \partial, where ∂\partial is the forward difference matrix. We take nn interior points xj=jhx_j = jh, j=1,…,nj = 1, \dots, n, with h=1/(n+1)h = 1/(n + 1), and hold the values at the two ends at zero, u0=un+1=0u_0 = u_{n+1} = 0 (a Dirichlet boundary condition, like a string with fixed ends). Then ∂\partial maps the nn interior values to the n+1n + 1 differences uj+1−uju_{j+1} - u_j, j=0,…,nj = 0, \dots, n.

Each row of −∂T∂-\partial^T \partial applied to uu gives the second difference uj−1−2uj+uj+1u_{j-1} - 2u_j + u_{j+1}. In the first and last rows one of the two neighbors is a boundary value, which is zero and drops out, so every diagonal entry is -2, and L/h2L/h^2 approximates d2/dx2d^2/dx^2 with u(0)=u(1)=0u(0) = u(1) = 0. It is the same matrix as second_difference(n, h) from the finite difference section, apart from the factor 1/h21/h^2.

We can use the Gershgorin disk theorem to bound the eigenvalues in the range [−4,0][-4,0] (LL is symmetric, so its eigenvalues are real, and each disk is centered at the diagonal entry -2, with radius at most 2, the sum of the off-diagonal entries in that row), or plot them experimentally:

For small kk the eigenvalues λk\lambda_k of LL, divided by h2h^2, are close to −(kπ)2-(k\pi)^2, the eigenvalues of d2/dx2d^2/dx^2 with u(0)=u(1)=0u(0) = u(1) = 0, but for large kk they are too small in magnitude. The second difference applied to sin⁡(kπx)\sin(k\pi x) gives −4h2sin⁡2(kπh2)sin⁡(kπx)-\frac{4}{h^2}\sin^2(\frac{k\pi h}{2})\sin(k\pi x), which is close to −(kπ)2sin⁡(kπx)-(k\pi)^2\sin(k\pi x) only when khkh is small, i.e., when there are many grid points per wavelength (compare the modified wavenumber in Spectral Methods).

Let’s plot the eigenvectors with the 4 smallest-magnitude eigenvalues:

The eigenvectors are of the form v(x)=sin⁡(kπx)v(x) = \sin(k \pi x) for k=1,2,3,…k = 1,2,3,\dots (dotted lines, scaled to unit length), and they vanish at both ends. All the eigenvalues are negative, −ωk2-\omega_k^2 with ωk≈kπ\omega_k \approx k\pi for small kk, so each eigenvector gives a solution u(x,t)=v(x)eiωktu(x, t) = v(x) e^{i\omega_k t} of the wave equation that oscillates in time without changing its shape, a standing wave of a string with fixed ends.

Now, let’s say we want to solve the boundary value problem

Δv=−ω2v,v(0)=a,v(1)=b\Delta v = -\omega^2 v, \quad v(0) = a, \quad v(1) = b

on [0,1][0, 1], which describes a string whose two ends are shaken with amplitudes aa and bb at frequency ω\omega. Again, we can use solve_bvp

The general solution of v′′=−ω2vv'' = -\omega^2 v is v=C1cos⁡(ωx)+C2sin⁡(ωx)v = C_1 \cos(\omega x) + C_2 \sin(\omega x). The condition at x=0x = 0 gives C1=aC_1 = a, and the condition at x=1x = 1 gives

C2=b−acos⁡ωsin⁡ω,C_2 = \frac{b - a\cos\omega}{\sin\omega},

which is the dashed line above. This breaks down when sin⁡ω=0\sin\omega = 0, i.e., ω=kπ\omega = k\pi. Then every solution with v(0)=av(0) = a has v(1)=acos⁡(kπ)=±av(1) = a\cos(k\pi) = \pm a, so the BVP has no solution unless b=±ab = \pm a, and infinitely many if it does. These are the natural frequencies ωk=kπ\omega_k = k\pi of the string, the frequencies of the standing waves sin⁡(kπx)\sin(k\pi x) that we computed above, for which −ω2-\omega^2 is an eigenvalue of d2/dx2d^2/dx^2 with zero boundary values. As ω\omega approaches one of them, the solution grows like 1/∣sin⁡ω∣1/|\sin\omega|, which is resonance.

solve_bvp does not detect the resonance. At ω=π\omega = \pi there is no solution, since b=1≠±5b = 1 \neq \pm 5, but the discretized problem is only nearly singular, so solve_bvp reports success and returns a solution of size about 109:

Boundary Value Problems in SymPy

SymPy offers functionality that can be used to solve BVPs in its sym.solvers.ode.dsolve function

in order to pass in boundary conditions, you can use the ics parameter

Let’s try the Helmholtz equation now. We declare ω\omega positive, so that SymPy writes the solution with sines and cosines rather than complex exponentials.

We see that the solution to the Helmholtz equation consists of sinusoidal waves. Let’s try putting in some boundary conditions

As we found above, the coefficient of sin⁡(ωx)\sin(\omega x) has sin⁡ω\sin\omega in the denominator, so the formula breaks down at the resonant frequencies ω=kπ\omega = k\pi. For ω=10\omega = 10 it gives the solution we computed with solve_bvp:

Solving BVPs using Optimization

You might also consider finding numerical solutions to BVPs using scipy.optimize. Let’s say we have a non-linear equation with boundary conditions

Δu=−f(u)in Ω,u(x)=0for x∈∂Ω,\Delta u = -f(u) \quad \text{in } \Omega, \qquad u(x) = 0 \quad \text{for } x\in \partial \Omega,

where ∂Ω\partial \Omega is the boundary of the domain. We can seek to solve the optimization problem

minimizeu∥Δu+f(u)∥2subject to u(x)=0for x∈∂Ω,\mathop{\mathsf{minimize}}_u \|\Delta u + f(u)\|^2 \quad \text{subject to } u(x) = 0 \quad \text{for } x \in \partial \Omega,

whose minimum value is zero exactly when uu solves the BVP.

We’ll use the Bratu nonlinearity f(u)=λeuf(u) = \lambda e^u on the unit square Ω=[0,1]2\Omega = [0, 1]^2, which describes the steady temperature of a square plate in which a chemical reaction releases heat at a rate that grows exponentially with temperature, and its edges held at temperature 0. This modifies the example from the SciPy Cookbook, which uses f(u)=u3f(u) = u^3.

With nn interior points in each direction, at xi=ihx_i = ih and yj=jhy_j = jh with h=1/(n+1)h = 1/(n + 1), the five-point Laplacian

Δu(xi,yj)≈ui−1,j+ui+1,j+ui,j−1+ui,j+1−4uijh2\Delta u(x_i, y_j) \approx \frac{u_{i-1,j} + u_{i+1,j} + u_{i,j-1} + u_{i,j+1} - 4u_{ij}}{h^2}

gives one equation for each of the n2n^2 interior values uiju_{ij}. The boundary values are zero, so they drop out of these equations, and the constraint holds automatically. If we order the unknowns as np.reshape does for an n×nn \times n array, the matrix of the discrete Laplacian is L=D⊗I+I⊗DL = D \otimes I + I \otimes D, where DD is the second_difference matrix from above, II is the n×nn \times n identity, and ⊗\otimes is the Kronecker product (sparse.kron, see Differentiation).

Each diagonal block couples a point to its neighbors in the same row of the grid, and the identity blocks next to it couple the point to its neighbors in the rows above and below.

The residual of the discretized equations is r(u)=Lu+λeu\mathbf r(\mathbf u) = L\mathbf u + \lambda e^{\mathbf u}, with the exponential applied entrywise, and its Jacobian is

J(u)=L+λ diag(eu),J(\mathbf u) = L + \lambda\, \mathrm{diag}(e^{\mathbf u}),

a sparse matrix with the same sparsity pattern as LL. We pass both to least_squares, starting from u=0\mathbf u = 0, with λ=6\lambda = 6.

The final cost 12∥r∥2\frac 12 \|\mathbf r\|^2 is about 5×10−135 \times 10^{-13}, so least_squares has found a solution of the equations. If the cost stays well above zero, least_squares has stopped at a local minimum of ∥r∥\|\mathbf r\| that does not solve the equations, so this is worth checking every time.

The plate is hottest in the center, where u≈0.8u \approx 0.8. Note that λ=6\lambda = 6 is larger than the λ∗≈3.51\lambda^* \approx 3.51 beyond which the slab has no steady state; the plate loses heat through all four edges, so it can balance a stronger reaction.

Since there are as many equations as unknowns, we can also apply Newton’s method from Root Finding directly. Each step solves the sparse linear system J(u) δ=−r(u)J(\mathbf u)\, \boldsymbol\delta = -\mathbf r(\mathbf u) with spsolve and updates u←u+δ\mathbf u \leftarrow \mathbf u + \boldsymbol\delta.