Skip to article frontmatterSkip to article content

Finite differences

Now we turn to one of the most common and important applications of interpolants: finding derivatives of functions. Because differentiation is a linear operation, we will constrain ourselves to formulas that are linear in the nodal values.

Note that while (5.4.1) is about finding the derivative at a single point xx, the same formula can be applied for different xx. The usual situation is a regularly spaced grid of nodes, a,a+h,a+2h,,ba,a+h,a+2h,\ldots,b, and then the value of ff at each node takes part in multiple applications of the formula. This will be demonstrated in Example 5.4.1 below.

5.4.1Common examples

There are three appealing special cases of (5.4.1) that get special attention.

The simplest example of a forward difference formula is inspired by the familiar limit definition of a derivative:

f(x)f(x+h)f(x)h, f'(x) \approx \frac{f(x+h)-f(x)}{h},

which is (5.4.1) with p=0p=0, q=1q=1, a0=1a_0=-1, and a1=1a_1=1. Analogously, we have the backward difference

f(x)f(x)f(xh)h, f'(x) \approx \frac{f(x)-f(x-h)}{h},

in which p=1p=1, q=0q=0.

As pointed out in Example 5.4.1, the only real distinction between (5.4.2) and (5.4.3) is whether we think that ff' is being evaluated at the left node or the right one. Symmetry would suggest that we should evaluate it halfway between. That is the motivation behind centered difference formulas.

Let’s derive the shortest centered formula using p=q=1p=q=1. For simplicity, we will set x=0x=0 without affecting the result. This means that f(h)f(-h), f(0)f(0), and f(h)f(h) are all available in (5.4.1).

Note that (5.4.2) is simply the slope of the line through the points (0,f(0))\bigl(0,f(0)\bigr) and (h,f(h))\bigl(h,f(h)\bigr). One route to using all three function values is to differentiate the quadratic polynomial that interpolates (h,f(h))\bigl(-h,f(-h)\bigr) as well (see Exercise 1):

Q(x)=x(xh)2h2f(h)x2h2h2f(0)+x(x+h)2h2f(h).Q(x) = \frac{x(x-h)}{2h^2} f(-h) - \frac{x^2-h^2}{h^2} f(0) + \frac{x(x+h)}{2h^2} f(h).

This leads to

f(0)Q(0)=f(h)f(h)2h.f'(0) \approx Q'(0) = \frac{f(h)-f(-h)}{2h}.

This result is equivalent to (5.4.1) with p=q=1p=q=1 and weights a1=12a_{-1}=-\frac{1}{2}, a0=0a_0=0, and a1=12a_1=\frac{1}{2}. Observe that while the value of f(0)f(0) was available during the derivation, its weight ends up being zero.

Besides the aesthetic appeal of symmetry, in Convergence of finite differences we will see another important advantage of (5.4.8) compared to the one-sided formulas.

We can in principle derive any finite-difference formula from the same process: Interpolate the given function values, then differentiate the interpolant exactly. Some results of the process are given in Table 5.4.1 for centered differences, and in Table 5.4.2 for forward differences. Both show the weights for estimating the derivative at x=0x=0. To get backward differences, you change the signs and reverse the order of the coefficients in any row of Table 5.4.2; see Exercise 2.

Table 5.4.1:Weights for centered finite-difference formulas.

order4h-4h3h-3h2h-2hh-h0hh2h2h3h3h4h4h
212-\frac{1}{2}012\frac{1}{2}
4112\frac{1}{12}23-\frac{2}{3}023\frac{2}{3}112-\frac{1}{12}
6160-\frac{1}{60}320\frac{3}{20}34-\frac{3}{4}034\frac{3}{4}320-\frac{3}{20}160\frac{1}{60}
81280\frac{1}{280}4105-\frac{4}{105}15\frac{1}{5}45-\frac{4}{5}045\frac{4}{5}15-\frac{1}{5}4105\frac{4}{105}1280-\frac{1}{280}

Table 5.4.2:Weights for forward finite-difference formulas. To get backward differences, change the signs and reverse the order of the coefficients.

order0hh2h2h3h3h4h4h
1-11
232-\frac{3}{2}212-\frac{1}{2}
3116-\frac{11}{6}332-\frac{3}{2}13\frac{1}{3}
42512-\frac{25}{12}4-343\frac{4}{3}14-\frac{1}{4}

The main motivation for using more function values in a formula is to improve the accuracy. This is measured by order of accuracy, which is shown in the tables and explored in Section 5.5.

5.4.2Higher derivatives

Many applications require the second derivative of a function. It’s tempting to use the finite difference of a finite difference. For example, applying (5.4.8) to ff' gives

f(0)f(h)f(h)2h.f''(0) \approx \frac{ f'(h) - f'(h) }{2h}.

Then applying (5.4.8) to approximate the appearances of ff' leads to

f(0)f(2h)2f(0)+f(2h)4h2.f''(0) \approx \frac{ f(-2h) - 2 f(0) + f(2h) }{4h^2}.

This is a valid formula, but it uses values at ±2h\pm 2h rather than the closer values at ±h\pm h. A better and more generalizable tactic is to return to the quadratic Q(x)Q(x) in (5.4.7) and use Q(0)Q''(0) to approximate f(0)f''(0). Doing so yields

f(0)f(h)2f(0)+f(h)h2, f''(0) \approx \frac{ f(-h) - 2 f(0) + f(h) }{h^2},

which is the simplest centered second-difference formula. As with the first derivative, we can choose larger values of pp and qq in (5.4.1) to get new formulas, such as

f(0)f(0)2f(h)+f(2h)h2,f''(0) \approx \frac{ f(0) - 2 f(h) + f(2h) }{h^2},

and

f(0)2f(0)5f(h)+4f(2h)f(3h)h2.f''(0) \approx \frac{ 2f(0) - 5 f(h) + 4 f(2h) -f(3h) }{h^2}.

For the second derivative, converting a forward difference to a backward difference requires reversing the order of the weights, while not changing their signs.

5.4.3Arbitrary nodes

Although function values at equally spaced nodes are a common and convenient situation, the node locations may be arbitrary. The general form of a finite-difference formula is

f(m)(0)k=0rck,mf(tk). f^{(m)}(0) \approx \sum_{k=0}^{r} c_{k,m} \,f(t_k).

We no longer assume equally spaced nodes, so there is no “hh” to be used in the formula. As before, the weights may be applied after any translation of the independent variable. The weights again follow from the interpolate/differentiate recipe, but the algebra becomes complicated. Fortunately there is an elegant recursion known as Fornberg’s algorithm that can calculate these weights for any desired formula. We present it without derivation as Function 5.4.1.

5.4.4Exercises

  1. ✍ This problem refers to Q(x)Q(x) defined by (5.4.7).

    (a) Show that Q(x)Q(x) interpolates the three values of ff at x=hx=-h, x=0x=0, and x=hx=h.

    (b) Show that Q(0)Q'(0) gives the finite-difference formula defined by (5.4.8).

    only - Unknown Directive
  2. (a)Table 5.4.2 lists forward difference formulas in which p=0p=0 in (5.4.1). Show that the change of variable g(x)=f(x)g(x) = f(-x) transforms these formulas into backward difference formulas with q=0q=0, and write out the table analogous to Table 5.4.2 for backward differences.

    (b) ⌨ Suppose you are given the nodes t0=0.9t_0=0.9, t1=1t_1=1, and t2=1.1t_2=1.1, and f(x)=sin(2x)f(x) = \sin(2x). Using formulas from Table 5.4.1 and Table 5.4.2, compute second-order accurate approximations to ff' at each of the three nodes.

    only - Unknown Directive
  3. ⌨ Let f(x)=exf(x)=e^{-x}, x=0.5x=0.5, and h=0.2h=0.2. Using Function 5.4.1 to get the necessary weights on five nodes centered at xx, find finite-difference approximations to the first, second, third, and fourth derivatives of ff. Make a table showing the derivative values and the errors in each case.

  4. ⌨ In the manner of Demo 5.4.5, use Function 5.4.1 on centered node vectors of length 3, 5, 7, and 9 to produce a table analogous to Table 5.4.1 for the second derivative f(0)f''(0). (You do not need to show the orders of accuracy, just the weights.)

  5. ⌨ For this problem, let f(x)=tan(2x)f(x)=\tan(2x).

    (a) ⌨ Apply Function 5.4.1 to find a finite-difference approximation to f(0.3)f''(0.3) using the five nodes tj=0.3+jht_j=0.3+jh for j=2,,2j=-2,\ldots,2 and h=0.05h=0.05. Compare to the exact value of f(0.3)f''(0.3).

    (b) ⌨ Repeat part (a) for f(0.75)f''(0.75) on the nodes tj=0.75+jht_j=0.75+jh. Why is the finite-difference result so inaccurate? (Hint: A plot of ff might be informative.)

  6. ✍ Find the finite-difference formula for f(0)f''(0) that results from applying (5.4.2) on ff' and then (5.4.3) on ff' within that result.

  7. (a) ✍ Show using L’Hôpital’s Rule that the centered formula approximation (5.4.8) converges to an equality as h0h\to 0.

    (b) ✍ Derive two conditions on the finite-difference weights in (5.4.1) that arise from requiring convergence as h0h\to 0. (Hint: Consider what is required in order to apply L’Hôpital’s Rule, as well as the result of applying it.)