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.
Note again that our node indexing scheme starts at zero. Our goal is to find a vector g such that gi≈f′(xi) for i=0,…,n. Our first try is the forward difference formula (5.4.2),
Here as elsewhere, elements of Dx that are not shown are zero. We call Dx a differentiation matrix. Each row of Dx gives the weights of the finite-difference formula being used at one of the nodes.
The differentiation matrix Dx 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
We have multiple choices again for Dxx, and it need not be the square of any particular Dx. As pointed out in Finite differences, squaring the first derivative is a valid approach but would place entries in Dxx 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,…,xn, are returned by Function 10.3.1.
Recall that finite-difference formulas are derived in three steps:
Choose a node index set S near node i.
Interpolate with a polynomial using the nodes in S.
Differentiate the interpolant and evaluate at node i.
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:
where c0=cn=2 and ci=1 for i=1,…,n−1. Note that this matrix is dense. The simplest way to compute a second derivative is by squaring Dx, 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] for Chebyshev nodes to any [a,b]. It also takes a different approach to computing the diagonal elements of Dx than the formulas in (10.3.9) (see Exercise 5).
According to Theorem 9.3.1, the convergence of polynomial interpolation to f using Chebyshev nodes is spectral if f is analytic (at least having infinitely many derivatives) on the interval. The derivatives of f are also approximated with spectral accuracy.
(a) ✍ Calculate Dx2 using (10.3.5) to define Dx.
(b) ⌨ Repeat the experiment of Demo 10.3.1, but using this version of Dx2 to estimate f′′. What is the apparent order of accuracy in f′′?
(a) ✍ Find the derivative of f(x)=sign(x)x2 on the interval [−1,1]. (If this gives you trouble, use an equivalent piecewise definition of f.) What is special about this function at x=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 n, there is only one node at which the error for computing f′ in part (b) is nonzero.
⌨ To get a fourth-order accurate version of Dx, five points per row are needed, including two special rows at each boundary. For a fourth-order Dxx, 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.
✍ Explain in detail how lines 23-24 in Function 10.3.2 correctly change the interval from [−1,1] to [a,b].
(a) ✍ What is the derivative of a constant function?
(b) ✍ Explain why for any reasonable differentiation matrix D, we should find j=0∑nDij=0 for all i.
(c) ✍ What does this have to do with Function 10.3.2? Refer to specific line(s) in the function for your answer.
Define the (n+1)×(n+1) matrix T=⎣⎡11⋮111⋱⋯1⎦⎤.
(a) ✍ Write out Tu for a generic vector u (start with a zero index). How is this like integration?
(b) ✍ Find the inverse of T for any n. (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?