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?
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
As usual, we replace u(x,t) by the semidiscretized u(t), where ui(t)≈u^(xi,t) and i=0,…,m. We require the endpoints of the interval to be included in the discretization, that is, x0=a and xm=b. Then we have a division of the semidiscrete unknown u(t) into interior and boundary nodes:
where v are the solution values over the interior of the interval. The guiding principle is to let the interior unknowns 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:
Given a value of v for the interior nodes, Equation (11.5.6) may be considered a system of two equations for the unknown boundary values u0 and um. This system will be a linear one for Dirichlet, Neumann, and Robin conditions.
The steps to evaluate 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) to compute u(t).
In many specific problems, extend does more work than is truly necessary. Dirichlet boundary conditions, for instance, define u0 and um directly, and there is no need to solve a nonlinear system. Exercise 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.
✍ Suppose second-order finite differences with m=3 are used to discretize the heat equation on x∈[0,2], with boundary conditions ux(0,t)=0 and ux(2,t)=1. If at some time t, u1=1 and u2=2, set up and solve the equations (11.5.6) for u0 and um.
⌨ Use Function 11.5.2 to solve the heat equation for 0≤x≤5 with initial condition u(x,0)=x(5−x) and subject to the boundary conditions u(0,t)=0, u(5,t)−ux(5,t)=5. Plot the solution at t=1 and find the value of u(2.5,1).
Consider Demo 11.5.4, combining diffusion with a nonlinear source term.
(a) ✍ Suppose we ignore the diffusion. Use separation of variables (or computer algebra) to solve the IVP ut=u2, u(0)=A>0. What happens as t→1/A from below?
(b) ⌨ Try to continue the solution in the demo to t=1. What happens?
(c) ⌨ Let the initial condition be u(x,0)=Cx4(1−x)2; the demo uses C=400. To the nearest 10, find a critical value C0 such that the solution approaches zero asymptotically if C<C0, but not otherwise.
⌨ The Allen–Cahn equation is used as a model for systems that prefer to be in one of two stable states. The governing PDE is
(a) Solve the problem with β=1.1 up to time t=8, plotting the solution at 6 equally spaced times. (You should see the solution decay down to the constant value -1.)
(b) Solve again with β=1.6. (This time the part of the bump will grow to just about reach u=1, and stay there.)
⌨ The Fisher equation is ut=uxx+u−u2. Assume that 0≤x≤6 and that the boundary conditions are ux(0,t)=u(6,t)=0.
(a) For the initial condition u(x,0)=21[1+cos(πx/2)], use Function 11.5.2 with m=80 to solve the Fisher equation and plot the solution at times t=0,0.5,…,3. What is u(0,3)?
(b) Repeat part (a), but increase the final time until it appears that the solution has reached a steady state (i.e., stopped changing in time). Find an accurate value of u(0,t) as t→∞.
(c) If we set ut=0 in the Fisher equation at steady state, we get a TPBVP in u(x). Use Function 10.5.1 with m=300 to solve this BVP, and make sure that the value at x=0 matches the result of part (b) to at least four digits.
⌨ Modify Function 11.5.2 for the special case of Dirichlet boundary conditions, in which (11.5.2) becomes simply
Your function should accept numbers α and β as input arguments in place of g1 and g2. Test your function on the problem in Demo 11.5.3.
⌨ Modify Function 11.5.2 for the special case of homogeneous Neumann boundary conditions. There is no longer any need for the input arguments for g1 and g2. Your implementation should solve a 2×2 linear system of equations to find the boundary values within the nested function extend. Test your function on the heat equation on x∈[0,4], t∈[0,1] with initial condition u(x,0)=x2(4−x)4.
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.