Skip to article frontmatterSkip to article content

Boundaries

So far we have considered the method of lines for problems with periodic end conditions, which is much like having no boundary at all. How can boundary conditions be incorporated into this technique?

Suppose we are given a nonlinear PDE of the form

ut=ϕ(t,x,u,ux,uxx),axb. u_t = \phi(t,x,u,u_x,u_{xx}), \quad a \le x \le b.

Not all such PDEs are parabolic (essentially, including diffusion), but we will assume this to be the case. Suppose also that the solution is subject to the boundary conditions

g1(u(a,t),ux(a,t))=0,g2(u(b,t),ux(b,t))=0.\begin{split} g_1\left( u(a,t), \frac{\partial u}{\partial x}(a,t) \right) &= 0, \\ g_2\left( u(b,t), \frac{\partial u}{\partial x}(b,t) \right) &= 0. \\ \end{split}

These include Dirichlet, Neumann, and Robin conditions, which are the linear cases of (11.5.2).

11.5.1Boundary removal

As usual, we replace u(x,t)u(x,t) by the semidiscretized u(t)\mathbf{u}(t), where ui(t)u^(xi,t)u_i(t)\approx \hat{u}(x_i,t) and i=0,,mi=0,\ldots,m. We require the endpoints of the interval to be included in the discretization, that is, x0=ax_0=a and xm=bx_m=b. Then we have a division of the semidiscrete unknown u(t)\mathbf{u}(t) into interior and boundary nodes:

u=[u0vum], \mathbf{u} = \begin{bmatrix} u_0 \\ \mathbf{v} \\ u_m \end{bmatrix},

where v\mathbf{v} are the solution values over the interior of the interval. The guiding principle is to let the interior unknowns v\mathbf{v} be governed by a discrete form of the PDE, while the endpoint values are chosen to satisfy the boundary conditions. As a result, we will develop an initial-value problem for the interior unknowns only:

dvdt=f(t,v). \frac{d \mathbf{v}}{d t} = \mathbf{f}(t,\mathbf{v}).

The boundary conditions are used only in the definition of f\mathbf{f}. As in Nonlinearity and boundary conditions, define

u=Dxu.\mathbf{u}' = \mathbf{D}_x \mathbf{u}.

Then Equation (11.5.2) takes the form

g1(u0,u0)=0,g2(um,um)=0.\begin{split} g_1( u_0, u'_0 ) &= 0, \\ g_2( u_m, u'_m ) &= 0. \end{split}

Given a value of v\mathbf{v} for the interior nodes, Equation (11.5.6) may be considered a system of two equations for the unknown boundary values u0u_0 and umu_m. This system will be a linear one for Dirichlet, Neumann, and Robin conditions.

11.5.2Implementation

The steps to evaluate f\mathbf{f} in (11.5.4) now go as follows.

Our full implementation of the method of lines for (11.5.1)--(11.5.2) is given in Function 11.5.2. It uses Function 10.3.2 (diffcheb) to set up a Chebyshev discretization. The nested function extend performs steps 1--2 of Algorithm 11.5.1 by calling Function 4.6.3 (levenberg) to solve the potentially nonlinear system (11.5.6). Then it sets up and solves an IVP, adding steps 3--4 of Algorithm 11.5.1 within the ode! function. Finally, it returns the node vector x and a function of t that applies extend to v(t)\mathbf{v}(t) to compute u(t)\mathbf{u}(t).

In many specific problems, extend does more work than is truly necessary. Dirichlet boundary conditions, for instance, define u0u_0 and umu_m directly, and there is no need to solve a nonlinear system. Exercise 11.5.6 asks you to modify the code to take advantage of this case. The price of solving a more general set of problems in Function 11.5.2 is some speed in such special cases.[1]

Finally, we return to the example of the Black–Scholes equation from Black–Scholes equation.

11.5.3Exercises

Footnotes
  1. An important advanced feature of Julia is multiple dispatch, which allows you to make multiple definitions of a function for different sequences and types of input arguments. Thus, addition to the original Function 11.5.2, we could also define a modified version in which g₁ and g₂ are of numeric type for the Dirichlet case. The correct version would be chosen (dispatched) depending on how the boundary conditions were supplied by the caller, allowing us speed when possible and generality as a fallback.