# Chapter 5 Diffusion. Parabolic Partial Differential Equations

## 5.1  Diffusion Equations

The diffusion equation,
 $\frac{\partial \mathit{\psi }}{\partial t}=\nabla .\left(D\nabla \mathit{\psi }\right)+s,$ $\left(5.1\right)$
arises in heat conduction, neutron transport, particle diffusion , and numerous other situations. There is a clear difference between the time variable, $t$, and the spatial variables $\mathbit{x}$. We'll talk mostly for brevity as if there is only one spatial dimension $x$, but this discussion can readily be generalized to multiple spatial dimensions. The highest time derivative is $\partial /\partial t$, first order. The highest spatial derivative is second order ${\partial }^{2}/\partial {x}^{2}$. The equation is classified as parabolic. Figure 5.1: Solving a diffusion problem generally requires a combination of spatial boundary conditions and temporal initial condition. Then the solution is propagated upward (forward in time), to fill in the multi-dimensional (time and space) solution domain.
Consequently, boundary conditions are not applied all around a closed contour (in the $x$-$t$ plane) but generally only at the two ends of the spatial range, and at one "initial" time, as illustrated in Fig. 5.1. The dependent variable solution is propagated from the initial condition forward in time (conventionally drawn as the vertical direction).

## 5.2  Time Advance Choices and Stability

### 5.2.1  Forward Time, Centered Space

For simplicity we'll take $D$ to be uniform in one cartesian dimension, and the meshes to be uniform with spacing $\mathit{\Delta }x$ and $\mathit{\Delta }t$. Then one way to write the equation in discrete finite differences is
 $\frac{{\mathit{\psi }}_{j}^{\left(n+1\right)}-{\mathit{\psi }}_{j}^{\left(n\right)}}{\mathit{\Delta }t}=D\frac{{\mathit{\psi }}_{j+1}^{\left(n\right)}-2{\mathit{\psi }}_{j}^{\left(n\right)}+{\mathit{\psi }}_{j-1}^{\left(n\right)}}{\mathit{\Delta }{x}^{2}}+{s}_{j}^{\left(n\right)}.$ $\left(5.2\right)$
We use $\left(n\right)$ to denote time index, and put it as a superscript to distinguish it from the space indices $j$ [$k$, $l$]. Of course this notation is not raising to a power. Notice in this equation the second order derivative in space is naturally centered and symmetric. However, the time derivative is not centered in time. It is really the value at $n+1/2$, not at the time index of everything else: $n$. This scheme is therefore "Forward" in Time, but Centered in Space (FTCS); see Fig. 5.2. Figure 5.2: Forward Time, Centered Space, (FTCS) difference scheme.
We immediately know from our previous experience that, because it is not centered in time, this scheme's accuracy is going to be only first order in $\mathit{\Delta }t$. Also, this scheme is explicit in time. The $\mathit{\psi }$ at $n+1$ is obtained using only prior ($n$) values of the other quantities:
 ${\mathit{\psi }}_{j}^{\left(n+1\right)}={\mathit{\psi }}_{j}^{\left(n\right)}+\frac{D\mathit{\Delta }t}{\mathit{\Delta }{x}^{2}}\left({\mathit{\psi }}_{j+1}^{\left(n\right)}-2{\mathit{\psi }}_{j}^{\left(n\right)}+{\mathit{\psi }}_{j-1}^{\left(n\right)}\right)+\mathit{\Delta }t\mathrm{ }{s}_{j}^{\left(n\right)}.$ $\left(5.3\right)$
A question then arises as to whether this scheme is stable. For an ordinary differential equation, we saw that with explicit integration, there was a maximum step size that could be allowed before the scheme became unstable. The same is true for hyperbolic and parabolic partial differential equations. For stability analysis, we ignore the source $S$ (because we are really analysing the deviation of the solution${}^{27}$). However, even so, it's a bit difficult to see immediately how to evaluate the amplification factor, because for partial differential equations there is variation in the spatial dimension(s) that has to be accounted for. It wasn't present for ordinary differential equations. The way this is generally handled is to turn the partial differential equation into an ordinary differential equation by examining separately all the Fourier components of the spatial variation. This sort of analysis is called Von Neumann stability analysis. It gives a precisely correct answer only for uniform grids and coefficients, but it is usually approximately correct, and hence in practice very useful even for non-uniform cases.
A Fourier component varies in space like $\mathrm{exp}\left(i{k}_{x}x\right)$ where ${k}_{x}$ is the wave number in the $x$-direction, (and $i$ is here the square root of minus 1). For such a Fourier component, ${\mathit{\psi }}_{j}\propto \mathrm{exp}\left({\mathit{ik}}_{x}\mathit{\Delta }xj\right)$, so that ${\mathit{\psi }}_{j+1}=\mathrm{exp}\left({\mathit{ik}}_{x}\mathit{\Delta }x\right){\mathit{\psi }}_{j}$ and ${\mathit{\psi }}_{j-1}=\mathrm{exp}\left(-{\mathit{ik}}_{x}\mathit{\Delta }x\right){\mathit{\psi }}_{j}$. Therefore
 ${\mathit{\psi }}_{j+1}-2{\mathit{\psi }}_{j}+{\mathit{\psi }}_{j-1}=\left({e}^{{\mathit{ik}}_{x}\mathit{\Delta }x}-2+{e}^{-{\mathit{ik}}_{x}\mathit{\Delta }x}\right){\mathit{\psi }}_{j}=-4{\mathrm{sin}}^{2}\left(\frac{{k}_{x}\mathit{\Delta }x}{2}\right){\mathit{\psi }}_{j}.$ $\left(5.4\right)$
Then substituting into eq. (5.3) for this Fourier component we find
 ${\mathit{\psi }}^{\left(n+1\right)}=\underset{\mathrm{Amplification} \mathrm{factor}}{\underset{⏟}{\left[1-\frac{D\mathit{\Delta }t}{\mathit{\Delta }{x}^{2}}4{\mathrm{sin}}^{2}\left(\frac{{k}_{x}\mathit{\Delta }x}{2}\right)\right]}}{\mathit{\psi }}^{\left(n\right)}.$ $\left(5.5\right)$
The amplification factor from each step to the next is the square bracket term. If it has a magnitude greater than 1, then instability will occur. If $D$ is negative it will in fact be greater than 1. This instability is not a numerical instability, though. It is a physical instability. The diffusion coefficient must be positive otherwise the diffusion equation is unstable regardless of numerical methods. So $D$ must be positive; and so are $\mathit{\Delta }t$, $\mathit{\Delta }x$. Therefore numerical instability will arise if the magnitude of the second (negative) term in the amplification factor exceeds 2.
If ${k}_{x}\mathit{\Delta }x$ is small, then that will make the second term small and unproblematic. We are most concerned about larger ${k}_{x}$ values that can make ${\mathrm{sin}}^{2}\left({k}_{x}\mathit{\Delta }x/2\right)$ approximately unity. In fact, the largest ${k}_{x}$ value that can be represented on a finite grid${}^{28}$ is such that the phase difference (${k}_{x}\mathit{\Delta }x$) between adjacent values is $\mathit{\pi }$ radians. That corresponds to a solution that oscillates in sign between adjacent nodes. For that Fourier component, therefore, ${\mathrm{sin}}^{2}\left({k}_{x}\mathit{\Delta }x/2\right)=1$.
Stability requires all Fourier modes to be stable, including the worst mode that has ${\mathrm{sin}}^{2}\left({k}_{x}\mathit{\Delta }x/2\right)=1$. Therefore the condition for stability is
 $\frac{4D\mathit{\Delta }t}{\mathit{\Delta }{x}^{2}}<2.$ $\left(5.6\right)$
There is, for the FTCS scheme, a maximum stable timestep equal to $\mathit{\Delta }{x}^{2}/2D$.
Incidentally, the fact that $\mathit{\Delta }t$ must therefore be no bigger than something proportional to $\mathit{\Delta }{x}^{2}$ makes the first order accuracy in time less of a problem. In fact, for a timestep at the stability limit, as we decrease $\mathit{\Delta }x$, improving the spatial accuracy proportional to $\mathit{\Delta }{x}^{2}$ because of the second order accuracy in space, we also improve the temporal accuracy by the same factor, proportional to $\mathit{\Delta }{x}^{2}$ because $\mathit{\Delta }t\propto \mathit{\Delta }{x}^{2}$.

### 5.2.2  Backward Time, Centered Space. Implicit Scheme.

In order to counteract the instability, we learned earlier that implicit schemes are helpful. The natural implicit time advance is simply to say that we use the values of the updated variables to evaluate the rest of the equation, instead of the prior values:
 ${\mathit{\psi }}_{j}^{\left(n+1\right)}={\mathit{\psi }}_{j}^{\left(n\right)}+\frac{D\mathit{\Delta }t}{\mathit{\Delta }{x}^{2}}\left({\mathit{\psi }}_{j+1}^{\left(n+1\right)}-2{\mathit{\psi }}_{j}^{\left(n+1\right)}+{\mathit{\psi }}_{j-1}^{\left(n+1\right)}\right)+\mathit{\Delta }t\mathrm{ }{s}_{j}^{\left(n+1\right)};$ $\left(5.7\right)$
This is a Backward in Time, Centered in Space scheme; illustrated in Fig. 5.3. Figure 5.3: Backward time, centered space, (BTCS) difference scheme.
We'll see a little later how to actually solve this equation for the values at $n+1$, but we can do the same stability analysis on it without knowing. The combination for the spatial Fourier mode is just as in eq. (5.4), so the update equation (ignoring $S$) for a Fourier mode is
 $\underset{\mathrm{Inverse} \mathrm{of} \mathrm{Amplification} \mathrm{factor}}{\underset{⏟}{\left[1+\frac{D\mathit{\Delta }t}{\mathit{\Delta }{x}^{2}}4{\mathrm{sin}}^{2}\left(\frac{{k}_{x}\mathit{\Delta }x}{2}\right)\right]}}{\mathit{\psi }}^{\left(n+1\right)}={\mathit{\psi }}^{\left(n\right)}.$ $\left(5.8\right)$
The amplification factor is the inverse of the square bracket factor on the left. That square bracket has magnitude always greater than one. Therefore the BTCS scheme is unconditionally stable. We can take timesteps as large as we like.
It turns out, however, that the only first order accuracy of this scheme, like the FTCS scheme, means that we don't generally want much to exceed the previously calculated stability limit. If we do so, we increasingly sacrifice temporal accuracy, even though not stability.

### 5.2.3  Partially implicit, Crank-Nicholson schemes.

The best choice, for optimizing the efficiency of the numerical solution of diffusive problems is to use a scheme that is part forward and part backward. A combination of forward and backward, in which $\mathit{\theta }$ is the weight of the implicit or backward proportion, is to write
 $\begin{array}{ccc}\multicolumn{1}{c}{{\mathit{\psi }}_{j}^{\left(n+1\right)}={\mathit{\psi }}_{j}^{\left(n\right)}}& +\hfill & \mathit{\theta }\left[\frac{D\mathit{\Delta }t}{\mathit{\Delta }{x}^{2}}\left({\mathit{\psi }}_{j+1}^{\left(n+1\right)}-2{\mathit{\psi }}_{j}^{\left(n+1\right)}+{\mathit{\psi }}_{j-1}^{\left(n+1\right)}\right)+\mathit{\Delta }t {s}_{j}^{\left(n+1\right)}\right]\hfill \\ \multicolumn{1}{c}{}& +\hfill & \left(1-\mathit{\theta }\right)\left[\frac{D\mathit{\Delta }t}{\mathit{\Delta }{x}^{2}}\left({\mathit{\psi }}_{j+1}^{\left(n\right)}-2{\mathit{\psi }}_{j}^{\left(n\right)}+{\mathit{\psi }}_{j-1}^{\left(n\right)}\right)+\mathit{\Delta }t {s}_{j}^{\left(n\right)}\right].\hfill & \hfill \left(5.9\right)\end{array}$

This is sometimes called the "$\mathit{\theta }$-implicit" scheme. The amplification factor is straightforwardly
 $A=\frac{1-\left(1-\mathit{\theta }\right)\frac{4D\mathit{\Delta }t}{\mathit{\Delta }{x}^{2}}{\mathrm{sin}}^{2}\left(\frac{{k}_{x}\mathit{\Delta }x}{2}\right)}{1+\mathit{\theta }\frac{4D\mathit{\Delta }t}{\mathit{\Delta }{x}^{2}}{\mathrm{sin}}^{2}\left(\frac{{k}_{x}\mathit{\Delta }x}{2}\right)}.$ $\left(5.10\right)$
If $\mathit{\theta }\ge \frac{1}{2}$ then $|A|\le 1$, and the scheme is always stable. If $\mathit{\theta }<\frac{1}{2}$, then $|A|\le 1$ requires the stability criterion
 $\mathit{\Delta }t<\frac{\mathit{\Delta }{x}^{2}}{2D\left(1-2\mathit{\theta }\right)}.$ $\left(5.11\right)$
Thus the minimum degree of implicitness that guarantees stability for all sizes of timestep is $\mathit{\theta }=\frac{1}{2}$. This choice is called the "Crank-Nicholson" scheme.
It has a major advantage beyond stability. It is centered in time. That means it is second order accurate in time (as well as space). This accuracy makes it useful to take bigger steps than would be allowed by the (explicit advance) stability limit.

## 5.3  Implicit Advancing Matrix Method

An implicit or partially implicit scheme for advancing a parabolic equation generally results in equations containing more than one spatial point value at the updated time, for example ${\mathit{\psi }}_{j}^{\left(n+1\right)}$, ${\mathit{\psi }}_{j-1}^{\left(n+1\right)}$, ${\mathit{\psi }}_{j+1}^{\left(n+1\right)}$. Such an equation for all spatial positions can be written as a matrix equation. Gathering together the terms at $n$ and at $n+1$ from eq. (5.9), it can be written

 $\left(\begin{array}{ccccc}\hfill \ddots \hfill & \hfill \ddots \hfill & \hfill 0\hfill & \hfill 0\hfill & \hfill 0\hfill \\ \hfill \ddots \hfill & \hfill \ddots \hfill & \hfill \ddots \hfill & \hfill 0\hfill & \hfill 0\hfill \\ \hfill 0\hfill & \hfill {B}_{-}\hfill & \hfill {B}_{0}\hfill & \hfill {B}_{+}\hfill & \hfill 0\hfill \\ \hfill 0\hfill & \hfill 0\hfill & \hfill \ddots \hfill & \hfill \ddots \hfill & \hfill \ddots \hfill \\ \hfill 0\hfill & \hfill 0\hfill & \hfill 0\hfill & \hfill \ddots \hfill & \hfill \ddots \hfill \end{array}\right)\left(\begin{array}{c}\hfill {\mathit{\psi }}_{1}^{\left(n+1\right)}\hfill \\ \hfill :\hfill \\ \hfill {\mathit{\psi }}_{j}^{\left(n+1\right)}\hfill \\ \hfill :\hfill \\ \hfill {\mathit{\psi }}_{J}^{\left(n+1\right)}\hfill \end{array}\right)=\left(\begin{array}{ccccc}\hfill \ddots \hfill & \hfill \ddots \hfill & \hfill 0\hfill & \hfill 0\hfill & \hfill 0\hfill \\ \hfill \ddots \hfill & \hfill \ddots \hfill & \hfill \ddots \hfill & \hfill 0\hfill & \hfill 0\hfill \\ \hfill 0\hfill & \hfill {C}_{-}\hfill & \hfill {C}_{0}\hfill & \hfill {C}_{+}\hfill & \hfill 0\hfill \\ \hfill 0\hfill & \hfill 0\hfill & \hfill \ddots \hfill & \hfill \ddots \hfill & \hfill \ddots \hfill \\ \hfill 0\hfill & \hfill 0\hfill & \hfill 0\hfill & \hfill \ddots \hfill & \hfill \ddots \hfill \end{array}\right)\left(\begin{array}{c}\hfill {\mathit{\psi }}_{1}^{\left(n\right)}\hfill \\ \hfill :\hfill \\ \hfill {\mathit{\psi }}_{j}^{\left(n\right)}\hfill \\ \hfill :\hfill \\ \hfill {\mathit{\psi }}_{J}^{\left(n\right)}\hfill \end{array}\right)+\left(\begin{array}{c}\hfill {s}_{1}\hfill \\ \hfill :\hfill \\ \hfill {s}_{j}\hfill \\ \hfill :\hfill \\ \hfill {s}_{J}\hfill \end{array}\right)$ $\left(5.12\right)$
or symbolically
 $\mathbf{B}{\mathbit{\psi }}_{n+1}=\mathbf{C}{\mathbit{\psi }}_{n}+\mathbf{s},$ $\left(5.13\right)$
where $J$ is the total length of the spatial mesh (the maximum of $j$), and the coefficients are
 ${B}_{0}=1+2\frac{D\mathit{\Delta }t}{\mathit{\Delta }{x}^{2}}\mathit{\theta },\mathrm{ }{B}_{+}={B}_{-}=-\frac{D\mathit{\Delta }t}{\mathit{\Delta }{x}^{2}}\mathit{\theta },$ $\left(5.14\right)$

 ${C}_{0}=1-2\frac{D\mathit{\Delta }t}{\mathit{\Delta }{x}^{2}}\left(1-\mathit{\theta }\right),\mathrm{ }{C}_{+}={C}_{-}=+\frac{D\mathit{\Delta }t}{\mathit{\Delta }{x}^{2}}\left(1-\mathit{\theta }\right),$ $\left(5.15\right)$

 $\mathrm{and}\mathrm{ }{s}_{j}=\mathit{\Delta }t\left[\mathit{\theta }{s}_{j}^{\left(n+1\right)}+\left(1-\mathit{\theta }\right){s}_{j}^{\left(n\right)}\right].$ $\left(5.16\right)$
[Notice the $\mathit{\Delta }t$ scaling factor in ${s}_{j}$.]
We are here assuming that the source terms do not depend upon $\mathit{\psi }$. Eq. (5.13) can be solved formally by inverting the matrix $\mathbf{B}$:
 ${\mathbit{\psi }}_{n+1}={\mathbf{B}}^{-1}\mathbf{C}{\mathbit{\psi }}_{n}+{\mathbf{B}}^{-1}\mathbf{s}.$ $\left(5.17\right)$
Therefore the additional work involved in using an implicit scheme is that we have (effectively) to invert the matrix $\mathbf{B}$, and multiply by the inverse at each timestep.
Provided the mesh is not too large, this can be a managable approach. In a situation where $\mathbf{B}$ and $\mathbf{C}$ do not change with time, the inversion${}^{29}$ need only be done once; and each step in time involves only a matrix multiplication by ${\mathbf{B}}^{-1}\mathbf{C}$ of the values from the previous timestep, ${\mathbit{\psi }}_{n}$.
If the matrix has the tridiagonal form of eq. (5.12), where the entries are non-zero only on the diagonal and the immediately adjacent subdiagonals, then it will certainly be more computationally efficient to solve for the updated variable by elimination${}^{30}$ rather than inverting the matrix and multiplying. This is a reflection of the fact that the matrix $\mathbf{B}$ is sparse. All but a very small fraction of its coefficients are zero. The fundamental problem is that the inverse of a sparse matrix is generally not sparse. Consequently, even though multiplication by the original sparse matrix actually requires only a few individual arithmetic operations, and easy short-cuts can be implemented, there are no obvious short-cuts for doing the matrix multiplication by the inverse. As long as we are implementing solutions in a mathematical system like Octave using the built-in facilities, we won't notice any difference because we are not using short-cuts.

## 5.4  Multiple Space Dimensions Figure 5.4: Example solution, at a particular time, of a diffusion equation in two space dimensions. The value of $\mathit{\psi }$ is visualized in this perspective plot as the height of the surface.
When there is more than one spatial coordinate dimension, as illustrated in Fig. 5.4, nothing changes formally about our method of solution of parabolic equations. What changes, however, is that we need a systematic way to turn the entire spatial mesh in multiple dimensions into a column vector like those in eq. (5.12). In other words, we must index all the spatial positions with a single index $j$. But generally if we have multiple dimensions, the natural (physical) indexing of the elements of the mesh is by a number of multiple indices equal to the number of dimensions: e.g. ${\mathit{\psi }}_{k,l,m}$, where $k$, $l$, $m$ correspond to the coordinates $x$, $y$, $z$.
Reordering the mesh elements is not a particularly difficult task algebraically, but it requires an intuitively tricky geometrical mental shift. In general, if we have a quantity ${\mathit{\psi }}_{k,l,...}$ indexed on a multidimensional mesh whose lengths in the different mesh dimensions are $K$, $L$, ... then we re-index them into a single index $j$ as follows. Start with all indices equal to 1. Count through the first index, $k=1,...,K$. Then increment the second index $l$ and repeat $k=1,...,K$. Continue this process for $l=1,...,L$. Then increment the next index (if any), and continue till all indices are exhausted. Incidentally, this is precisely the order in which the elements are stored in computer memory when using a language like Fortran when using the entire allocated array. Figure 5.5: Conversion of a two-dimensional array into a single column vector using column order is like stacking all the columns below one another.
The result is that for the giant column vector ${\mathit{\psi }}_{j}$, the first $K$ elements are the original multidimensionally indexed elements ${\mathit{\psi }}_{k,1,...}, k=1,...,K$; the next $K$ elements, $j=K+1,...,2K$, are the original ${\mathit{\psi }}_{k,2,...}$ and so on.${}^{31}$
In multiple dimensions, the second derivative operator (something like ${\nabla }^{2}$) is represented by a stencil, as illustrated in eq. (4.23). The importance of the reordering of the elements into a column vector is that although the components of the stencil that are adjacent in the first index ($k$) remain adjacent in $j$, those in the other index(es) ($l$) do not. For example the elements ${\mathit{\psi }}_{k,l}$ and ${\mathit{\psi }}_{k,l+1}$ have $j$-indices $j=K.\left(l-1\right)+k$ and $j=K.l+k$ respectively; they are a distance $K$ apart. This fact means that for multiple spatial dimensions the matrices $\mathbf{B}$ and $\mathbf{C}$ are no longer simply tridiagonal. Instead, they have an additional non-zero diagonal, a distance $K$ from the main diagonal. If the boundary conditions are $\mathit{\psi }=0$, each matrix is "Block-Tridiagonal", having a form like this:
 $L \mathrm{blocks}\left\{\left[\begin{array}{ccccc}\hfill \begin{array}{ccccc}\hfill c\hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill a\hfill & \hfill c\hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill .\hfill & \hfill .\hfill & \hfill .\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill & \hfill a\hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill \end{array}\hfill & \hfill \begin{array}{ccccc}\hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill \end{array}\hfill & \hfill \hfill & \hfill \hfill & \hfill \right\}\begin{array}{c}\hfill \mathrm{Block}\hfill \\ \hfill \mathrm{size}\hfill \\ \hfill K\hfill \\ \hfill \hfill \end{array}\hfill \\ \hfill \begin{array}{ccccc}\hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill \end{array}\hfill & \hfill \begin{array}{ccccc}\hfill c\hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill a\hfill & \hfill c\hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill .\hfill & \hfill .\hfill & \hfill .\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill & \hfill a\hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill \end{array}\hfill & \hfill \begin{array}{ccccc}\hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill \end{array}\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \begin{array}{ccccc}\hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill \end{array}\hfill & \hfill \begin{array}{ccccc}\hfill c\hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill a\hfill & \hfill c\hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill .\hfill & \hfill .\hfill & \hfill .\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill & \hfill a\hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill \end{array}\hfill & \hfill \begin{array}{ccccc}\hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill \end{array}\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \begin{array}{ccccc}\hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill \end{array}\hfill & \hfill \begin{array}{ccccc}\hfill c\hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill a\hfill & \hfill c\hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill .\hfill & \hfill .\hfill & \hfill .\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill & \hfill a\hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill \end{array}\hfill & \hfill \begin{array}{ccccc}\hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill \end{array}\hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \begin{array}{ccccc}\hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill \end{array}\hfill & \hfill \begin{array}{ccccc}\hfill c\hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill a\hfill & \hfill c\hfill & \hfill a\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill .\hfill & \hfill .\hfill & \hfill .\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill & \hfill a\hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill \end{array}\hfill \end{array}\right]\right\}J=K×L \text{rows}.$ $\left(5.18\right)$
Here, $c$ denotes the coefficient of the center of the stencil, and $a$ denotes the coefficients of the adjacent points in the stencil. One can think of the total matrix as being composed of $L×L$ blocks, each of size $K×K$. All the blocks are zero except the tridiagonal ones. And each non-zero block is itself tridiagonal (or diagonal). If there are further dimensions $M$, then the giant matrix is a tridiagonal composite of $M×M$ blocks, each of a two dimensional ($L×L$ block) type of eq. (5.18). And so on.
It is very important to think carefully about the boundary conditions. These occur at the boundaries of each block. Notice how the corner (boundary) entries of the extra subdiagonal blocks make zero certain coefficients of the subdiagonals of the total matrix. One important consequence of the block-tridiagonal form (5.18) is that it is not so easy to do tridiagonal elimination rather than matrix inversion.

## 5.5  Estimating Computational Cost

The computational cost of executing different numerical algorithms often has to be considered for large scale computing. This cost is generally expressed in the number of floating point operations (FLOPs). Roughly, this is the number of multiplications (or divisions) required. Additions are cheap.
For an $N×N$ matrix, multiplying into a length-$N$ column vector costs (simplemindedly) $N$ rows times $N$ operations per row: ${N}^{2}$ (FLOPs). By extending this to $N$ columns, we see that multiplying two matrices costs ${N}^{3}$. Although the process of inversion${}^{32}$ of a non-singular matrix, seems far more difficult to do, because the algorithm is much more complicated, it also costs roughly ${N}^{3}$ operations. These estimates are accurate only to within a factor of two or so. But that is enough for most purposes.
Inverting or multiplying random matrices using Octave on my laptop, for $N=1000$ takes about 1 second. That seems amazingly fast to me, because it corresponds to about 1 FLOP per nanosecond. But that's about what can be achieved these days provided the cache contains enough of the data.
Trouble is that if we are dealing with two-dimensions in space, each of length $K=100$, then the side of the matrix we have to invert is $J={K}^{2}=10000$. A multiplication or inversion would take at least ${J}^{3}/{10}^{9}=1000$ seconds; that's a quarter of an hour. Waiting that long is frustrating, and if many inversions are needed, time rapidly runs out.
Inverting the matrix representing a two-dimensional problem takes ${K}^{6}$ operations. And for a three-dimensional problem it's ${K}^{9}$. This rapid escalation of the cost means that one doesn't generally approach multidimensional problems in terms of simplistic matrix inversion. In particular, it is difficult to implement an implicit scheme for advancing the diffusion equation. And it is probably more realistic just to use an explicit scheme, recognizing and observing the stability limit, eq. (5.6), on maximum time-step size.

## Worked Example: Crank Nicholson Matrices

Express the following parabolic partial differential equation in two space variables ($r$ and $\mathit{\phi }$) and one time variable ($t$) as a matrix equation using the Crank-Nicholson scheme on uniform grids.
 ${\nabla }^{2}\mathit{\psi }=\frac{1}{{r}^{2}}\frac{\partial }{\partial r}\left({r}^{2}\frac{\partial \mathit{\psi }}{\partial r}\right)+\frac{1}{{r}^{2}}\frac{{\partial }^{2}\mathit{\psi }}{\partial {\mathit{\phi }}^{2}}=\frac{\partial \mathit{\psi }}{\partial t}.$ $\left(5.19\right)$
Find the coefficients and layout of the required matrices if the boundary conditions are $\frac{\partial \mathit{\psi }}{\partial r}=0$ at $r=0$, $\mathit{\psi }=0$ at $r=R$, and periodicity in $\mathit{\phi }$.

Let $k$ and $l$ be indices in the $r$ and $\mathit{\phi }$ coordinates, and let $\mathit{\Delta }r$ and $\mathit{\Delta }\mathit{\phi }$ denote their grid spacings. The finite difference form of the spatial derivative terms is
 $\begin{array}{ccc}\multicolumn{1}{c}{\frac{1}{{r}^{2}}\frac{\partial }{\partial r}{\left({r}^{2}\frac{\partial \mathit{\psi }}{\partial r}\right)}_{k,l}}& =\hfill & \frac{1}{{r}_{k}^{2}}\left({r}_{k+1/2}^{2}\frac{{\mathit{\psi }}_{k+1,l}-{\mathit{\psi }}_{k,l}}{{r}_{k+1}-{r}_{k}}-{r}_{k-1/2}^{2}\frac{{\mathit{\psi }}_{k,l}-{\mathit{\psi }}_{k-1,l}}{{r}_{k}-{r}_{k-1}}\right)\frac{1}{{r}_{k+1/2}-{r}_{k-1/2}}\hfill \\ \multicolumn{1}{c}{}& =\hfill & \left[{\left(\frac{{r}_{k}+\frac{\mathit{\Delta }r}{2}}{{r}_{k}}\right)}^{2}\left({\mathit{\psi }}_{k+1,l}-{\mathit{\psi }}_{k,l}\right)-{\left(\frac{{r}_{k}-\frac{\mathit{\Delta }r}{2}}{{r}_{k}}\right)}^{2}\left({\mathit{\psi }}_{k,l}-{\mathit{\psi }}_{k-1,l}\right)\right]\frac{1}{\mathit{\Delta }{r}^{2}}\hfill \\ \multicolumn{1}{c}{}& =\hfill & \left[{\left(1+\frac{\mathit{\Delta }r}{2{r}_{k}}\right)}^{2}{\mathit{\psi }}_{k+1,l}-\left(2+\frac{\mathit{\Delta }{r}^{2}}{2{r}_{k}^{2}}\right){\mathit{\psi }}_{k,l}+{\left(1-\frac{\mathit{\Delta }r}{2{r}_{k}}\right)}^{2}{\mathit{\psi }}_{k-1,l}\right]\frac{1}{\mathit{\Delta }{r}^{2}}\hfill \\ \multicolumn{1}{c}{{\frac{1}{{r}^{2}}\frac{{\partial }^{2}\mathit{\psi }}{\partial {\mathit{\theta }}^{2}}|}_{k,l}}& =\hfill & \frac{1}{{r}_{k}^{2}}\left(\frac{{\mathit{\psi }}_{k,l+1}-{\mathit{\psi }}_{k,l}}{{\mathit{\theta }}_{l+1}-{\mathit{\theta }}_{l}}-\frac{{\mathit{\psi }}_{k,l}-{\mathit{\psi }}_{k,l-1}}{{\mathit{\theta }}_{l}-{\mathit{\theta }}_{l-1}}\right)\frac{1}{{\mathit{\theta }}_{l+1/2}-{\mathit{\theta }}_{l-1/2}}\hfill \\ \multicolumn{1}{c}{}& =\hfill & \left({\mathit{\psi }}_{k,l+1}-2{\mathit{\psi }}_{k,l}+{\mathit{\psi }}_{k,l-1}\right)\frac{1}{{r}_{k}^{2}\mathit{\Delta }{\mathit{\theta }}^{2}}.\hfill & \hfill \left(5.20\right)\end{array}$

Therefore, expressing the positions across the spatial grid in terms of a single index $j=k+K.\left(l-1\right)$ (where $K$ is the size of the $k$-grid, and $L$ of the $l$-grid), the differential operator ${\nabla }^{2}$ becomes a matrix $\mathbf{M}$ multiplying the vector $\mathbit{\psi }={\mathit{\psi }}_{j}$, of the form
 $\mathbf{M}=\left[\begin{array}{ccccc}\hfill \begin{array}{ccccc}\hfill f\hfill & \hfill e\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill a\hfill & \hfill c\hfill & \hfill b\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill .\hfill & \hfill .\hfill & \hfill .\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill & \hfill b\hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill \end{array}\hfill & \hfill \begin{array}{ccccc}\hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill \end{array}\hfill & \hfill \hfill & \hfill \hfill & \hfill \begin{array}{ccccc}\hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill \end{array}\hfill \\ \hfill \begin{array}{ccccc}\hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill \end{array}\hfill & \hfill \begin{array}{ccccc}\hfill f\hfill & \hfill e\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill a\hfill & \hfill c\hfill & \hfill b\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill .\hfill & \hfill .\hfill & \hfill .\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill & \hfill b\hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill \end{array}\hfill & \hfill \begin{array}{ccccc}\hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill \end{array}\hfill & \hfill \hfill & \hfill \right\}\begin{array}{c}\hfill \mathrm{Block}\hfill \\ \hfill \mathrm{size}\hfill \\ \hfill K\hfill \\ \hfill \hfill \end{array}\hfill \\ \hfill \hfill & \hfill \begin{array}{ccccc}\hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill \end{array}\hfill & \hfill \begin{array}{ccccc}\hfill f\hfill & \hfill e\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill a\hfill & \hfill c\hfill & \hfill b\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill .\hfill & \hfill .\hfill & \hfill .\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill & \hfill b\hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill \end{array}\hfill & \hfill \begin{array}{ccccc}\hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill \end{array}\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \begin{array}{ccccc}\hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill \end{array}\hfill & \hfill \begin{array}{ccccc}\hfill f\hfill & \hfill e\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill a\hfill & \hfill c\hfill & \hfill b\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill .\hfill & \hfill .\hfill & \hfill .\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill & \hfill b\hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill \end{array}\hfill & \hfill \begin{array}{ccccc}\hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill \end{array}\hfill \\ \hfill \begin{array}{ccccc}\hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill \end{array}\hfill & \hfill \hfill & \hfill \hfill & \hfill \begin{array}{ccccc}\hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill d\hfill \end{array}\hfill & \hfill \begin{array}{ccccc}\hfill f\hfill & \hfill e\hfill & \hfill \hfill & \hfill \hfill & \hfill \hfill \\ \hfill a\hfill & \hfill c\hfill & \hfill b\hfill & \hfill \hfill & \hfill \hfill \\ \hfill \hfill & \hfill .\hfill & \hfill .\hfill & \hfill .\hfill & \hfill \hfill \\ \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill & \hfill b\hfill \\ \hfill \hfill & \hfill \hfill & \hfill \hfill & \hfill a\hfill & \hfill c\hfill \end{array}\hfill \end{array}\right]\right\}\text{L blocks}.$ $\left(5.21\right)$
The explanation of this form is as follows. The coefficients of a generic row corresponding to $r$-position index $k$ are
 $a={\left(1-\frac{\mathit{\Delta }r}{2{r}_{k}}\right)}^{2}\frac{1}{\mathit{\Delta }{r}^{2}}, b={\left(1+\frac{\mathit{\Delta }r}{2{r}_{k}}\right)}^{2}\frac{1}{\mathit{\Delta }{r}^{2}}, d=\frac{1}{{r}_{k}^{2}\mathit{\Delta }{\mathit{\phi }}^{2}}, c=-\left(a+b+2d\right).$ $\left(5.22\right)$
The periodic boundary conditions in $\mathit{\phi }$ are implemented by the appearance of off-diagonal, $d$-type, blocks at the upper right and lower left of the matrix. The $r$-boundary conditions are the same for all the blocks on the diagonal, the tridiagonal $\mathit{acb}$-type blocks. At the $r=R$ boundary, (which would be the bottom row of each block), the condition $\mathit{\psi }=0$ means no contribution arises to the differential operator from the $\mathit{\psi }$-value there. The condition therefore allows us simply to omit the $r=R$ row from the matrix, choosing the $k=K$ index to refer to the position $r=R-\mathit{\Delta }r$. At the $r=0$ end (the top row of each block), the condition $\frac{\partial \mathit{\psi }}{\partial r}=0$ can be implemented in a properly centered way by choosing the $r$-grid to be aligned to the half-integral positions.${}^{33}$ In other words, ${r}_{0}=-\mathit{\Delta }r/2$, ${r}_{1}=+\mathit{\Delta }r/2$, ${r}_{n}=\left(n-1/2\right)\mathit{\Delta }r$. (The ${r}_{0}$ position values are not represented in the matrices.) In that case, there is zero contribution to the difference scheme from the first derivative at position ${r}_{1-1/2}=0$ (because ${r}^{2}$ there is zero), and the $r$-second-derivative operator at $k=1$ becomes $\left(1+\frac{\mathit{\Delta }r}{2{r}_{1}}{\right)}^{2}\left({\mathit{\psi }}_{2,l}-{\mathit{\psi }}_{1,l}\right)/\mathit{\Delta }{r}^{2}=4\left({\mathit{\psi }}_{2,l}-{\mathit{\psi }}_{1,l}\right)/\mathit{\Delta }{r}^{2}$, giving rise to equal and opposite coefficients $e=4/\mathit{\Delta }{r}^{2}$. The diagonal entry on those $k=1$ rows is minus the sum of all the other coefficients on the row: $f=-\left(e+2d\right)$.
The Crank-Nicholson scheme for the differential equation time advance is then
 ${\mathbit{\psi }}^{\left(n+1\right)}-{\mathbit{\psi }}^{\left(n\right)}=\frac{\mathit{\Delta }t}{2}\mathbf{M}{\mathbit{\psi }}^{\left(n+1\right)}+\frac{\mathit{\Delta }t}{2}\mathbf{M}{\mathbit{\psi }}^{\left(n\right)},$ $\left(5.23\right)$
which on rearrangement becomes
 ${\mathbit{\psi }}^{\left(n+1\right)}=\left(\mathbf{I}-\frac{\mathit{\Delta }t}{2}\mathbf{M}{\right)}^{-1}\left(\mathbf{I}+\frac{\mathit{\Delta }t}{2}\mathbf{M}\right){\mathbit{\psi }}^{\left(n\right)}.$ $\left(5.24\right)$

## Exercise 5. Diffusion and Parabolic Equations.

1. Write a computer code to solve the diffusive equation
 $\frac{\partial \mathit{\psi }}{\partial t}=D\frac{{\partial }^{2}\mathit{\psi }}{\partial {x}^{2}}+s\left(x\right)$

For constant, uniform diffusivity $D$ and constant specified source $s\left(x\right)$. Use a uniform $x$-mesh with ${N}_{x}$ nodes. Consider boundary conditions to be $\mathit{\psi }$ fixed, equal to ${\mathit{\psi }}_{1}$, ${\mathit{\psi }}_{2}$ at the domain boundaries, $x=-1,1$, and the initial condition to be $\mathit{\psi }=0$ at $t=0$.
Construct a matrix $\mathbf{G}={G}_{\mathit{ij}}$ such that $\mathbf{G}\mathbit{\psi }={\nabla }^{2}\mathbit{\psi }$. Use it to implement the FTCS scheme
 ${\mathbit{\psi }}^{\left(n+1\right)}=\left(\mathbf{I}+\mathit{\Delta }t\mathbf{G}\right){\mathbit{\psi }}^{\left(n\right)}+\mathit{\Delta }t \mathbf{s},$

paying special attention to the boundary conditions.
Solve the time-dependent problem for $D=1$, $s=1$, ${N}_{x}=30$, ${\mathit{\psi }}_{1}={\mathit{\psi }}_{2}=0$, $t=0\to 1$, storing your results in a matrix $\mathit{\psi }\left(x,t\right)={\mathit{\psi }}_{{j}_{x},{j}_{t}}$, and plotting that matrix at the end of the solution, for examination.
Experiment with various $\mathit{\Delta }t$ to establish the dependence of the accuracy and stability of your solution on $\mathit{\Delta }t$. In particular, without finding an "exact" solution of the equation by any other method,
(i) Find experimentally the value of $\mathit{\Delta }t$ above which the scheme becomes unstable.
(ii) Estimate experimentally the fractional error arising from finite time step duration in $\mathit{\psi }\left(t=1,x=0\right)$ when using a $\mathit{\Delta }t$ approximately equal to the maximum stable value.
(iii) By varying ${N}_{x}$, estimate experimentally the fractional error at ${N}_{x}=30$ arising from finite spatial differences. Which is more significant, time or space difference error?

2. Develop a modified version of your code to implement the $\mathit{\theta }$-implicit scheme:
 $\left(\mathbf{I}-\mathit{\Delta }t\mathit{\theta }\mathbf{G}\right){\mathit{\psi }}^{\left(n+1\right)}=\left(\mathbf{I}+\mathit{\Delta }t\left(1-\mathit{\theta }\right)\mathbf{G}\right){\mathit{\psi }}^{\left(n\right)}+\mathit{\Delta }t s,$

in the form
 ${\mathit{\psi }}^{\left(n+1\right)}={\mathbf{B}}^{-1}\mathbf{C}{\mathit{\psi }}^{\left(n\right)}+{\mathbf{B}}^{-1}\mathit{\Delta }t\mathrm{ }s$

(i) Experiment with different $\mathit{\Delta }t$ and $\mathit{\theta }$ values, for the same time-dependent problem and find experimentally the value of $\mathit{\theta }$ for which instability disappears for all $\mathit{\Delta }t$.
(ii) Suppose we are limited to only 50 timesteps to solve over the time $0 so $\mathit{\Delta }t=0.02$. Find experimentally the optimum value of $\mathit{\theta }$ which produces the most accurate results.

Submit the following as your solution for each part:
1. Your code in a computer format that is capable of being executed, citing the language it is written in.
2. The requested experimental $\mathit{\Delta }t$ and/or $\mathit{\theta }$ values.
3. A plot of your solution for at least one of the cases.
4. A brief description of how you determined the accuracy of the result.