from numpy import *
from scipy import linalg
from scipy.linalg import norm
from matplotlib.pyplot import *
from prettytable import PrettyTable
from timeit import default_timer as timer
import sys
sys.path.append('fncbook/')
import fncbook as FNC
# This (optional) block is for improving the display of plots.
# from IPython.display import set_matplotlib_formats
# set_matplotlib_formats("svg","pdf")
# %config InlineBackend.figure_format = 'svg'
rcParams["figure.figsize"] = [7, 4]
rcParams["lines.linewidth"] = 2
rcParams["lines.markersize"] = 4
rcParams['animation.html'] = "jshtml" # or try "html5"
Our strategy in Black–Scholes equation was to discretize both the time and space derivatives using finite differences, then rearrange so that we could march the solution forward through time. It was partially effective, but as Example 11.1.3 shows, not a sure thing, for reasons we look into starting in the next section.
First, though, we want to look at a broader version of the discretization approach. To introduce ideas, let’s use the simpler heat equation, ut=uxx, as a model. Because boundaries always complicate things, we will start by doing the next best thing to having no boundaries at all: periodic end conditions. Specifically, we will solve the PDE over 0≤x<1 and require at all times that
This is a little different from simply u(1,t)=u(0,t), as Figure 11.2.1 illustrates.
Figure 11.2.1:Left: A function whose values are the same at the endpoints of an interval does not necessarily extend to a smooth periodic function. Right: For a truly periodic function, the function values and all derivatives match at the endpoints of one period.
As a reminder, we use u^ when we specifically refer to the exact solution of the PDE. In order to avoid carrying along redundant information about the function, we use xi=ih only for i=0,…,m−1, where h=1/m, and it’s understood that a reference to xm is silently translated to one at x0. More generally, we have the identity
This step is called semidiscretization, since space is discretized but time is not. As in Chapter 10, we will replace uxx with multiplication of u by a differentiation matrix Dxx. The canonical choice is the three-point finite-difference formula (5.4.9), which in light of the periodicity (11.2.2) leads to
Note well how the first and last rows have elements that “wrap around” from one end of the domain to the other by periodicity. Because we will be using this matrix quite a lot, we create Function 11.2.1 to compute it, as well as the corresponding second-order first derivative matrix Dx for periodic end conditions.
The PDE ut=uxx is now approximated by the semidiscrete problem
which is simply a linear, constant-coefficient system of ordinary differential equations. Given the initial values u(0) obtained from u(xi,0), we have an initial-value problem that we already know how to solve!
Semidiscretization is often called the method of lines. Despite the name, it is not exactly a single method because both space and time discretizations have to be specified in order to get a concrete algorithm. The key concept is the separation of those two discretizations, and in that way, it’s related to separation of variables in analytic methods for the heat equation.
The method in Example 11.2.1 and Example 11.2.2 is essentially the same one we used for the Black–Scholes equation in Black–Scholes equation. By changing the time integrator, we can get much better results.
Example 11.2.4 suggests that implicit time stepping methods have an important role in diffusion. We will analyze the reason in the next few sections.
Instead of coding one of the Runge–Kutta or multistep formulas directly for a method of lines solution, we could use any of the IVP solvers from Chapter 6, or a solver from the DifferentialEquations package, to solve the ODE initial-value problem (11.2.5).
The adaptive time integrators can all produce solutions. But, as seen in Example 11.2.5, they are not equivalent in every important sense. Whether we choose to implement a method directly with a fixed step size, or automatically with adaptation, there is something crucial to understand about the semidiscrete problem (11.2.5) that will occupy our attention in the next two sections.