Skip to article frontmatterSkip to article content

Differentiation matrices

In Finite differences we used finite differences to turn a discrete collection of function values into an estimate of the derivative of the function at a point. Just as with differentiation in elementary calculus, we can generalize differences at a point into an operation that maps discretized functions to discretized derivatives.

10.3.1Matrices for finite differences

We first discretize the interval x[a,b]x\in[a,b] into equal pieces of length h=(ba)/nh=(b-a)/n, leading to the nodes

xi=a+ih,i=0,,n.x_i=a+i h, \qquad i=0,\ldots,n.

Note again that our node indexing scheme starts at zero. Our goal is to find a vector g\mathbf{g} such that gif(xi)g_i \approx f'(x_i) for i=0,,ni=0,\ldots,n. Our first try is the forward difference formula (5.4.2),

gi=fi+1fih,t=0,,n1.g_i = \frac{f_{i+1}-f_i}{h}, \qquad t=0,\ldots,n-1.

However, this leaves gng_n undefined, because the formula would refer to the unavailable value fn+1f_{n+1}. For gng_n we could resort to the backward difference

gn=fnfn1h.g_n = \frac{f_{n}-f_{n-1}}{h}.

We can summarize the entire set of formulas by defining

f=[f(x0)f(x1)f(xn1)f(xn)],\mathbf{f} = \begin{bmatrix} f(x_0) \\[1mm] f(x_1) \\[1mm] \vdots \\[1mm] f(x_{n-1}) \\[1mm] f(x_n) \end{bmatrix}, \quad

and then the vector equation

[f(x0)f(x1)f(xn1)f(xn)]Dxf,Dx=1h[11111111].\begin{bmatrix} f'(x_0) \\[1mm] f'(x_1) \\[1mm] \vdots \\[1mm] f'(x_{n-1}) \\[1mm] f'(x_n) \end{bmatrix} \approx \mathbf{D}_x \mathbf{f}, \qquad \mathbf{D}_x = \frac{1}{h} \begin{bmatrix} -1 & 1 & & & \\[1mm] & -1 & 1 & & \\[1mm] & & \ddots & \ddots & \\[1mm] & & & -1 & 1 \\[1mm] & & & -1 & 1 \end{bmatrix}.

Here as elsewhere, elements of Dx\mathbf{D}_x that are not shown are zero. We call Dx\mathbf{D}_x a differentiation matrix. Each row of Dx\mathbf{D}_x gives the weights of the finite-difference formula being used at one of the nodes.

The differentiation matrix Dx\mathbf{D}_x in (10.3.5) is not a unique choice. We are free to use whatever finite-difference formulas we like in each row. However, it makes sense to choose rows that are as similar as possible. Using second-order centered differences where possible and second-order one-sided formulas (see Table 5.4.2) at the boundary points leads to

Dx=1h[3221212012120121201212232].\mathbf{D}_x = \frac{1}{h} \begin{bmatrix} -\frac{3}{2} & 2 & -\frac{1}{2} & & & \\[1mm] -\frac{1}{2} & 0 & \frac{1}{2} & & & \\[1mm] & -\frac{1}{2} & 0 & \frac{1}{2} & & \\ & & \ddots & \ddots & \ddots & \\ & & & -\frac{1}{2} & 0 & \frac{1}{2} \\[1mm] & & & \frac{1}{2} & -2 & \frac{3}{2} \end{bmatrix}.

The differentiation matrices so far are banded matrices, i.e., all the nonzero values are along diagonals close to the main diagonal.[1]

10.3.2Second derivative

Similarly, we can define differentiation matrices for second derivatives. For example,

[f(x0)f(x1)f(x2)f(xn1)f(xn)]1h2[25411211211211452][f(x0)f(x1)f(x2)f(xn1)f(xn)]=Dxxf.\begin{bmatrix} f''(x_0) \\[1mm] f''(x_1) \\[1mm] f''(x_2) \\[1mm] \vdots \\[1mm] f''(x_{n-1}) \\[1mm] f''(x_n) \end{bmatrix} \approx \frac{1}{h^2} \begin{bmatrix} 2 & -5 & 4 & -1 & & \\[1mm] 1 & -2 & 1 & & & \\[1mm] & 1 & -2 & 1 & & \\[1mm] & & \ddots & \ddots & \ddots & \\[1mm] & & & 1 & -2 & 1 \\[1mm] & & -1 & 4 & -5 & 2 \end{bmatrix} \begin{bmatrix} f(x_0) \\[1mm] f(x_1) \\[1mm] f(x_2) \\[1mm] \vdots \\[1mm] f(x_{n-1}) \\[1mm] f(x_n) \end{bmatrix} = \mathbf{D}_{xx} \mathbf{f}.

We have multiple choices again for Dxx\mathbf{D}_{xx}, and it need not be the square of any particular Dx\mathbf{D}_x. As pointed out in Finite differences, squaring the first derivative is a valid approach but would place entries in Dxx\mathbf{D}_{xx} farther from the diagonal than is necessary.

Together the matrices (10.3.6) and (10.3.7) give second-order approximations of the first and second derivatives at all nodes. These matrices, as well as the nodes x0,,xnx_0,\ldots,x_n, are returned by Function 10.3.1.

10.3.3Spectral differentiation

Recall that finite-difference formulas are derived in three steps:

  1. Choose a node index set SS near node ii.
  2. Interpolate with a polynomial using the nodes in SS.
  3. Differentiate the interpolant and evaluate at node ii.

We can modify this process by using a global interpolant, either polynomial or trigonometric, as in Chapter 9. Rather than choosing a different index set for each node, we use all of the nodes each time.

In a nonperiodic setting we use Chebyshev second-kind points for stability:

xk=cos(kπn),k=0,,n;x_k = -\cos\left(\frac{k \pi}{n}\right), \qquad k=0,\ldots,n;

then the resulting Chebyshev differentiation matrix has entries

D00=2n2+16,Dnn=2n2+16,Dij={xi2(1xi2),i=j,cicj(1)i+jxixj,ij, \begin{gathered} D_{00} = \dfrac{2n^2+1}{6}, \qquad D_{n n} = -\dfrac{2n^2+1}{6}, \\ D_{ij} = \begin{cases} -\dfrac{x_i}{2(1-x_i^2)}, & i=j, \\[4mm] \dfrac{c_i}{c_j}\, \dfrac{(-1)^{i+j}}{x_i-x_j}, & i\neq j, \end{cases} \end{gathered}

where c0=cn=2c_0=c_n=2 and ci=1c_i=1 for i=1,,n1i=1,\ldots,n-1. Note that this matrix is dense. The simplest way to compute a second derivative is by squaring Dx\mathbf{D}_x, as there is no longer any concern about the bandwidth of the result.

Function 10.3.2 returns these two matrices. The function uses a change of variable to transplant the standard [1,1][-1,1] for Chebyshev nodes to any [a,b][a,b]. It also takes a different approach to computing the diagonal elements of Dx\mathbf{D}_x than the formulas in (10.3.9) (see Exercise 5).

According to Theorem 9.3.1, the convergence of polynomial interpolation to ff using Chebyshev nodes is spectral if ff is analytic (at least having infinitely many derivatives) on the interval. The derivatives of ff are also approximated with spectral accuracy.

10.3.4Exercises

  1. (a) ✍ Calculate Dx2\mathbf{D}_x^2 using (10.3.5) to define Dx\mathbf{D}_x.

    (b) ⌨ Repeat the experiment of Demo 10.3.1, but using this version of Dx2\mathbf{D}_x^2 to estimate ff''. What is the apparent order of accuracy in ff''?

  2. (a) ✍ Find the derivative of f(x)=sign(x)x2f(x) =\operatorname{sign}(x)x^2 on the interval [1,1][-1,1]. (If this gives you trouble, use an equivalent piecewise definition of ff.) What is special about this function at x=0x=0?

    (b) ⌨ Adapt Demo 10.3.1 to operate on the function from part (a), computing only the first derivative. What is the observed order of accuracy?

    (c) ✍ Show that for even values of nn, there is only one node at which the error for computing ff' in part (b) is nonzero.

  3. ⌨ To get a fourth-order accurate version of Dx\mathbf{D}_x, five points per row are needed, including two special rows at each boundary. For a fourth-order Dxx\mathbf{D}_{xx}, five symmetric points per row are needed for interior rows and six points are needed for the rows near a boundary.

    (a) Modify Function 10.3.1 to a function diffmat4, which outputs fourth-order accurate differentiation matrices. You may want to use Function 5.4.1.

    (b) Repeat the experiment of Demo 10.3.1 using diffmat4 in place of Function 10.3.1, and compare observed errors to fourth-order accuracy.

  4. ✍ Explain in detail how lines 23-24 in Function 10.3.2 correctly change the interval from [1,1][-1,1] to [a,b][a,b].

  5. (a) ✍ What is the derivative of a constant function?

    (b) ✍ Explain why for any reasonable differentiation matrix D\mathbf{D}, we should find j=0nDij=0\displaystyle \sum_{j=0}^nD_{ij}=0 for all ii.

    (c) ✍ What does this have to do with Function 10.3.2? Refer to specific line(s) in the function for your answer.

  6. Define the (n+1)×(n+1)(n+1)\times (n+1) matrix T=[111111]\mathbf{T} = \displaystyle \begin{bmatrix} 1 & & & \\ 1 & 1 & & \\ \vdots & & \ddots & \\ 1 & 1 & \cdots & 1 \end{bmatrix}.

    (a) ✍ Write out Tu\mathbf{T}\mathbf{u} for a generic vector u\mathbf{u} (start with a zero index). How is this like integration?

    (b) ✍ Find the inverse of T\mathbf{T} for any nn. (You can use Julia to find the pattern, but show that the result is correct in general.) What does this have to do with the inverse of integration?

Footnotes
  1. In order to exploit this structure efficiently in Julia, these matrices first need to be constructed as or converted to sparse or tridiagonal form.