Finite Difference Method

← Numerical Methods

Source inspiration: (Mathew 2000-2019).

Description

This page covers the finite difference method for a hyperbolic partial differential equation, the one-dimensional wave equation, as Mathews’ module “Finite Difference Method for Hyperbolic Equations” does. The problem is to find the displacement \(u(x, t)\) of a vibrating string with fixed ends, \(u_{tt} = c^2 u_{xx}\) for \(0 < x < a\) and \(0 < t < b\), given its initial position \(u(x, 0) = f(x)\) and initial velocity \(u_t(x, 0) = g(x)\).

The method covers the rectangle \(0 \le x \le a\), \(0 \le t \le b\) with a grid of spacing \(h\) in \(x\) and \(k\) in \(t\), replaces both second derivatives by central differences, and solves the resulting difference equation for the newest time level. With \(u_{i,j} \approx u(x_i, t_j)\) and the ratio \(r = ck/h\), each new row of the grid comes from the two rows before it:

\[ u_{i,j+1} = (2 - 2r^2)\,u_{i,j} + r^2\,(u_{i+1,j} + u_{i-1,j}) - u_{i,j-1}. \]

The first row is \(f\), and a second row is built from \(f\) and \(g\) with a Taylor expansion. This is an explicit method: the rows are computed one after another, with no system of equations to solve. As a general result, it is stable when \(r \le 1\); for \(r > 1\) the shortest wavelengths that a fine enough grid can carry grow geometrically. When \(r = 1\) and the second row is exact, every value it computes is exact. Mathews’ three examples show both sides: Examples 1 and 2 use \(r = 1\) and reproduce the exact solution to rounding error, and Example 3 takes \(r = 1.02\) and is unstable.

Animations

Each animation below advances the shape of the string \(u(x, t_j)\) one time row per frame and draws the exact solution at the same time (black) beside it. The computed values \(u_{i,j}\) at the grid points are joined by straight lines. Mathews draws all the rows at once as a three-dimensional surface over the indices \(i\) and \(j\); each frame here is one row of that surface. A second panel shows what one row alone does not: the largest error in each row (Case 1), the grid values seen from above with the paths of the string’s corners (Case 2), or the sawtooth mode that makes Example 3 unstable (Case 3). All three cases use Mathews’ grid of Examples 1 and 2, \(n = 11\) and \(m = 21\) (kept for Example 3, whose grid the archive has lost), formula (8) for the second row (except the comparison run in Case 1) and formula (6) for the later rows. The numbers in the titles and legends agree with the tables in the examples.

Mathews’ module links no animation, and his animation archive has none for partial differential equations, so all three animations are new, with no Mathews original. Cases 1 to 3 animate his Examples 1 to 3; Case 1 also runs Example 1 with the first-order second row (7), as in “What the exact second row is worth” below. Not animated are Mathews’ three-dimensional surfaces themselves (including his surface of the exact solution in Example 1, drawn here as the black curves), his plot of the initial position in Example 2 (the first frame of Case 2), his printed tables, and the refinement table for \(r = 0.5\) in Example 1.

Julia source scripts that generated these animations are linked under each case.

Case 1 — Example 1: \(f(x) = \sin \pi x + \sin 2\pi x\), \(r = 1\), second row by (8) and by (7)

Behavior: With Mathews’ second row (8) (red), which is exact here, the computed points lie on the exact solution in every row: the largest error in a row is never more than \(1.5 \times 10^{-15}\), which is rounding error, and at \(t = 1\) the string is back in its initial shape. With the first-order second row (7) (blue, dashed), the second row is already off by \(0.2212\), and formula (12) carries that error along without damping it: the largest error in a row reaches \(0.3976\) at \(t = 0.15\), \(0.35\), \(0.65\) and \(0.85\). At \(t = 0.5\) and \(t = 1\) it falls back to rounding level (\(8.9 \times 10^{-16}\) and \(7.8 \times 10^{-16}\)), because on this grid any solution of (12) that is zero in row 1, as the error is, is zero again in rows 11 and 21.

Julia source

Finite difference solution of u_tt = 4u_xx with u(x, 0) = sin πx + sin 2πx, u_t(x, 0) = 0, h = 0.1, k = 0.05 and r = 1, advanced one time row per frame from t = 0 to t = 1 against the exact solution; the run with Mathews’ second row (8) lies on the exact curve in every row, while the run with the first-order second row (7) is off by up to 0.3976; a log-scale panel shows the largest error in each row, at most 1.5 × 10⁻¹⁵ for (8) and between 0.1584 and 0.3976 for (7) except at t = 0.5 and t = 1, where it falls to rounding level

Case 2 — Example 2: the plucked string, \(r = 1\), against d’Alembert’s solution

Behavior: The corner of the string at \(x = 0.6\) splits in two, and the two corners move in opposite directions, each one grid step \(h = 0.1\) per time step \(k = 0.05\), the wave speed \(c = 2\). In the \((x, t)\) plane on the right their paths run along the diagonals of the grid, because \(r = 1\) makes \(k = h/c\). The corner moving right reaches the fixed end \(x = 1\) at \(t = 0.2\) and the one moving left reaches \(x = 0\) at \(t = 0.3\); each is reflected there, and from \(t = 0.3\) to \(t = 0.7\) the whole string is at or below the axis. At \(t = 0.5\) the corners meet at \(x = 0.4\), where the string has its lowest point, \(-0.6\); the motion then runs in reverse, and at \(t = 1\) the string is back in its initial shape. At every grid time the exact string is straight between grid points, so the computed points joined by straight lines are the exact shape, and the largest error in a row is at most \(4.5 \times 10^{-16}\).

Julia source

Finite difference solution of u_tt = 4u_xx for the plucked string peaked at x = 0.6, with h = 0.1, k = 0.05 and r = 1, advanced one time row per frame from t = 0 to t = 1 against d’Alembert’s solution, with the corners of the exact string marked; a second panel shows the grid points of the (x, t) plane coloured by the computed values and the paths of the two corners along the grid diagonals, reflected at x = 1 at t = 0.2 and at x = 0 at t = 0.3 and meeting at x = 0.4 at t = 0.5, where the string reaches −0.6

Case 3 — Example 3: the same string with \(r = 1.02\), the sawtooth grows

Behavior: In the first rows the computed string stays close to the exact one; the largest error after one step is \(0.00255\). But the sawtooth \(\sin(9\pi x_i) = (-1)^i \sin(\pi x_i)\), the mode \(p = 9\), for which \(r^2 s_9 = 1.0149 > 1\), is already in \(f\) with coefficient \(c_9 = -0.01219\). From then on \(c_9\) changes sign at every step (open and filled markers) and its size grows at every step, by a ratio that approaches the size of the unstable root \(\lambda = -1.2762\) of (9) and is \(1.2761\) at the last step; the dashed line, drawn through the last point, has that slope. Over the \(20\) steps \(|c_9|\) grows by a factor of about \(66\), to \(0.7997\) at \(t = 1.02\). The largest error in a row also grows at every step, and from row 13 (\(t = 0.612\)) on it is within \(3\%\) of \(|c_9|\): the error is mostly the sawtooth. From row 18 (\(t = 0.867\)) on the computed string goes beyond \(0.6\), the largest displacement the true string ever has, and at \(t = 1.02\) its values alternate in sign from point to point, with \(\max|u| = 1.312\). The animation stops where Mathews’ rectangle ends, at \(t = 1.02\), and every row fits the fixed axis.

Julia source

Finite difference solution of u_tt = 4u_xx for the plucked string with h = 0.1, k = 0.051 and r = 1.02, advanced one time row per frame from t = 0 to t = 1.02 against d’Alembert’s solution, the computed string turning into a growing zigzag; a lower panel shows the sawtooth part of each row, c₉ sin 9πxᵢ, on an axis rescaled every frame, and a log-scale panel shows |c₉|, with c₉ alternating in sign (open and filled markers), growing from 0.01219 to 0.7997 by a factor per step that approaches the unstable root’s size, 1.2762, with the largest error in each row growing from 0.00255 to 0.7845

The Wave Equation

NoteDefinition — The wave equation problem

Find \(u(x, t)\) satisfying

\[ u_{tt}(x, t) = c^2\, u_{xx}(x, t) \qquad \text{for } 0 < x < a \text{ and } 0 < t < b, \tag{1} \]

with the boundary and initial conditions

\[ \begin{aligned} u(0, t) &= 0 \quad \text{and} \quad u(a, t) = 0 && \text{for } 0 \le t \le b, \\ u(x, 0) &= f(x) && \text{for } 0 \le x \le a, \\ u_t(x, 0) &= g(x) && \text{for } 0 < x < a. \end{aligned} \tag{2} \]

The wave equation models the displacement \(u\) of an elastic string held fixed at \(x = 0\) and \(x = a\); \(c\) is the speed at which waves travel along it. Analytic solutions can be found with Fourier series or with d’Alembert’s formula below, but the problem serves as the prototype of a hyperbolic equation, and the same difference method applies where no formula is available.

Derivation of the Difference Equation

Partition the rectangle \(R = \{(x, t) : 0 \le x \le a,\ 0 \le t \le b\}\) into \(n - 1\) by \(m - 1\) rectangles with sides \(h = a/(n - 1)\) and \(k = b/(m - 1)\). Following Mathews, the grid points are numbered from \(1\):

\[ x_i = (i - 1)h, \quad i = 1, \ldots, n, \qquad t_j = (j - 1)k, \quad j = 1, \ldots, m, \]

and \(u_{i,j}\) denotes the approximation to \(u(x_i, t_j)\). The bottom row \(j = 1\) is the known initial position, \(u_{i,1} = f(x_i)\), and the end columns are the fixed ends, \(u_{1,j} = u_{n,j} = 0\).

The central-difference formulas for the two second derivatives are

\[ u_{tt}(x, t) = \frac{u(x, t + k) - 2u(x, t) + u(x, t - k)}{k^2} + O(k^2), \tag{3} \]

\[ u_{xx}(x, t) = \frac{u(x + h, t) - 2u(x, t) + u(x - h, t)}{h^2} + O(h^2). \tag{4} \]

Dropping the \(O(k^2)\) and \(O(h^2)\) terms and substituting into (1) gives the difference equation

\[ \frac{u_{i,j+1} - 2u_{i,j} + u_{i,j-1}}{k^2} = c^2\, \frac{u_{i+1,j} - 2u_{i,j} + u_{i-1,j}}{h^2}. \tag{5} \]

With the substitution \(r = ck/h\) it becomes \(u_{i,j+1} - 2u_{i,j} + u_{i,j-1} = r^2 (u_{i+1,j} - 2u_{i,j} + u_{i-1,j})\), which is solved for the value on the newest row:

ImportantExplicit formula for the wave equation

\[ u_{i,j+1} = (2 - 2r^2)\,u_{i,j} + r^2\,(u_{i+1,j} + u_{i-1,j}) - u_{i,j-1}, \qquad i = 2, 3, \ldots, n - 1, \tag{6} \]

where \(r = ck/h\).

Formula (6) computes row \(j + 1\) from four known values: three neighbouring values \(u_{i-1,j}\), \(u_{i,j}\), \(u_{i+1,j}\) in row \(j\) and the value \(u_{i,j-1}\) directly below in row \(j - 1\). This four-point pattern is the stencil of the method. Because it reaches back two rows, two starting rows are needed.

Starting Values

The first row is \(u_{i,1} = f(x_i) = f_i\). The second row, at \(t = k\), is not given, so it is built from Taylor’s formula in \(t\) about \((x_i, 0)\). To first order, \(u(x_i, k) = u(x_i, 0) + u_t(x_i, 0)\,k + O(k^2)\), and with \(g_i = g(x_i)\) this gives

\[ u_{i,2} = f_i + k\,g_i, \qquad i = 2, 3, \ldots, n - 1. \tag{7} \]

Errors in the second row are carried through the rest of the grid by formula (6), so a better second row is worth having. When \(f\) has a second derivative, the wave equation turns the unknown \(u_{tt}\) at \(t = 0\) into a known \(x\)-derivative of \(f\):

\[ u_{tt}(x_i, 0) = c^2 u_{xx}(x_i, 0) = c^2 f''(x_i) = c^2\, \frac{f_{i+1} - 2f_i + f_{i-1}}{h^2} + O(h^2). \]

Taylor’s formula of order 2, \(u(x, k) = u(x, 0) + u_t(x, 0)\,k + \tfrac12 u_{tt}(x, 0)\,k^2 + O(k^3)\), then gives

\[ u(x_i, k) = f_i + k g_i + \frac{c^2 k^2}{2h^2}\,(f_{i+1} - 2f_i + f_{i-1}) + O(h^2)\,O(k^2) + O(k^3), \]

and with \(r = ck/h\) this simplifies to the second-row formula that Mathews’ program uses:

ImportantSecond row

\[ u_{i,2} = (1 - r^2) f_i + k g_i + \frac{r^2}{2}\,(f_{i+1} + f_{i-1}), \qquad i = 2, 3, \ldots, n - 1. \tag{8} \]

Stability

Mathews calls the method stable if an error made at one stage of the calculation is eventually damped out, and states that stability of formula (6) requires \(r = ck/h \le 1\). The condition is a general result, and a Fourier analysis shows where it comes from; it also shows that for \(r \le 1\) errors are not damped but stay bounded. Since the boundary values are zero, look for solutions of (6) of the form \(u_{i,j} = \lambda^{j} \sin(p\pi x_i/a)\), one for each mode \(p = 1, 2, \ldots, n - 2\) that the grid can represent. Substituting into (6) and dividing by \(\lambda^{j-1}\sin(p\pi x_i/a)\) gives

\[ \lambda^2 - (2 - 4r^2 s_p)\,\lambda + 1 = 0, \qquad s_p = \sin^2\!\left(\frac{p\pi h}{2a}\right). \tag{9} \]

The two roots have product \(1\). If \(|2 - 4r^2 s_p| < 2\), that is \(r^2 s_p < 1\), they are complex conjugates of modulus \(1\), and the mode neither grows nor decays. If \(r^2 s_p = 1\) they coincide at \(\lambda = -1\) and the mode can grow, but only linearly. If \(r^2 s_p > 1\), they are real and negative, and one of them has modulus greater than \(1\): the mode grows geometrically and changes sign at every step. Since \(s_p < 1\) for every mode, \(r \le 1\) gives \(r^2 s_p < 1\), and all modes stay bounded. As \(h \to 0\) the largest \(s_p\) approaches \(1\), so \(r \le 1\) is the condition that works for every grid.

TipStability condition (general result)

The explicit formula (6) for the wave equation is stable when

\[ r = \frac{ck}{h} \le 1. \]

For \(r > 1\), on any grid fine enough that \(r^2 s_{n-2} > 1\), the highest modes the grid can carry grow without bound as the rows are computed.

The condition has a physical reading. The exact value \(u(x, t)\) depends on the initial data in \([x - ct,\ x + ct]\), while the value \(u_{i,j}\) computed by (6) can depend only on initial values in \([x_i - (j-1)h,\ x_i + (j-1)h]\), because each row reaches one grid point further to each side. The numerical interval contains the true one only when \(h/k \ge c\), which is \(r \le 1\) (the Courant–Friedrichs–Lewy condition). Suitable implicit methods, which are more work to implement, have no stability restriction on \(r\).

WarningOn a coarse grid the threshold is slightly above 1

Only the modes \(p \le n - 2\) exist on a given grid, so the exact threshold there is \(r = 1/\sqrt{s_{n-2}}\), slightly above \(1\). For the grid of Examples 1 to 3 (\(n = 11\), \(h = 0.1\)) it is \(1/\sin(9\pi/20) \approx 1.0125\); only mode \(p = 9\) is unstable at \(r = 1.02\).

D’Alembert’s Solution and Exact Rows

The French mathematician Jean Le Rond d’Alembert (1717–1783) found that

\[ u(x, t) = F(x + ct) + G(x - ct) \tag{10} \]

solves the wave equation (1), as direct substitution shows: \(u_{tt} = c^2 F''(x + ct) + c^2 G''(x - ct) = c^2 u_{xx}\). The boundary conditions \(u(0, t) = u(a, t) = 0\) hold when \(G(z) = -F(-z)\) and \(F\) has period \(2a\), since then \(u(0, t) = F(ct) - F(ct) = 0\) and \(u(a, t) = F(ct + a) - F(ct - a) = 0\); in particular they hold when \(G = F\) with \(F\) odd and of period \(2a\), as in (11) below. The solution with \(u(x, 0) = f(x)\) and \(u_t(x, 0) = 0\) is

\[ u(x, t) = \tfrac12 \bigl(\tilde f(x + ct) + \tilde f(x - ct)\bigr), \tag{11} \]

where \(\tilde f\) is the odd extension of \(f\) with period \(2a\). The initial shape splits into two halves that travel left and right at speed \(c\) and are reflected, upside down, at the fixed ends.

When \(r = 1\), that is \(k = h/c\), the diagonals of the grid line up with the characteristics \(x \pm ct = \text{const}\), and the method becomes exact.

TipTheorem — Two exact rows give an exact grid

Assume that the two rows \(u_{i,1} = u(x_i, 0)\) and \(u_{i,2} = u(x_i, k)\), \(i = 1, 2, \ldots, n\), are exact values of the solution of (1). If the step \(k = h/c\) is chosen, then \(r = 1\) and formula (6) becomes

\[ u_{i,j+1} = u_{i+1,j} + u_{i-1,j} - u_{i,j-1}, \tag{12} \]

and the values it produces throughout the grid are exact values of the solution (apart from computer rounding).

Proof. With \(ck = h\), \(x_i - ct_j = (i - 1)h - (j - 1)h = (i - j)h\) and \(x_i + ct_j = (i + j - 2)h\), so by (10) every exact grid value has the form \(u_{i,j} = F((i + j - 2)h) + G((i - j)h)\). Substituting this into the right side of (12),

\[ \begin{aligned} u_{i+1,j} + u_{i-1,j} - u_{i,j-1} &= F((i + j - 1)h) + F((i + j - 3)h) - F((i + j - 3)h) \\ &\quad + G((i + 1 - j)h) + G((i - 1 - j)h) - G((i - j + 1)h) \\ &= F((i + (j + 1) - 2)h) + G((i - (j + 1))h) = u_{i,j+1}. \qquad \blacksquare \end{aligned} \]

The theorem needs an exact second row, and formulas (7) and (8) generally do not give one. There is one common case where they do. When \(g = 0\) and \(r = 1\), formula (8) reduces to

\[ u_{i,2} = \frac{f_{i-1} + f_{i+1}}{2}, \qquad i = 2, 3, \ldots, n - 1, \tag{13} \]

and by (11) the exact value is \(u(x_i, k) = \tfrac12\bigl(\tilde f(x_i + h) + \tilde f(x_i - h)\bigr)\), which is the same thing, because \(ck = h\) and the points \(x_i \pm h\) are grid points in \([0, a]\), where \(\tilde f = f\). So with \(g = 0\) and \(r = 1\) the method reproduces the exact solution at every grid point. Examples 1 and 2 are of this kind.

Algorithm

Mathews’ program has two parts.

  1. Starting rows. Set \(h = a/(n - 1)\), \(k = b/(m - 1)\) and \(r = ck/h\). Fill row 1 with \(u_{i,1} = f(x_i)\) for \(i = 1, \ldots, n\), and fill row 2 by formula (8) for \(i = 2, \ldots, n - 1\). All other entries start at \(0\), so the end columns \(u_{1,j}\) and \(u_{n,j}\) stay \(0\) for \(j \ge 2\).
  2. Later rows. For \(j = 3, 4, \ldots, m\) and \(i = 2, \ldots, n - 1\), compute \[ u_{i,j} = (2 - 2r^2)\,u_{i,j-1} + r^2\,(u_{i+1,j-1} + u_{i-1,j-1}) - u_{i,j-2}. \]

The result is an \(n \times m\) array whose column \(j\) is the shape of the string at time \(t_j\). Mathews plots this array as a surface over the index grid (the axes are labelled \(x(i)\) and \(t(j)\) and carry the indices \(i = 1, \ldots, 11\) and \(j = 1, \ldots, 21\), not the values of \(x\) and \(t\)), seen from the viewpoint \((4, 2, 3)\) and coloured by height.

In all three examples below \(c = 2\) (so \(c^2 = 4\)), the string has length \(a = 1\), \(g(x) = 0\), and the grid has \(n = 11\) columns and \(m = 21\) rows (Mathews’ grid in Examples 1 and 2; the archive has lost the lines that state Example 3’s grid, and the same grid is kept there). The examples differ in the initial position \(f\) and the time interval \([0, b]\).

Example 1 — \(f(x) = \sin(\pi x) + \sin(2\pi x)\), \(r = 1\)

Solve

\[ u_{tt}(x, t) = 4\,u_{xx}(x, t), \qquad 0 < x < 1, \quad 0 < t < 1, \]

with \(u(0, t) = u(1, t) = 0\), \(u(x, 0) = f(x) = \sin(\pi x) + \sin(2\pi x)\) and \(u_t(x, 0) = g(x) = 0\), over the rectangle \(R = \{(x, t) : 0 \le x \le 1,\ 0 \le t \le 1\}\), and compare with the exact solution

\[ u(x, t) = \sin(\pi x)\cos(2\pi t) + \sin(2\pi x)\cos(4\pi t). \tag{14} \]

Step 1 — grid. With \(n = 11\) and \(m = 21\),

\[ h = \frac{1}{10} = 0.1, \qquad k = \frac{1}{20} = 0.05, \qquad r = \frac{ck}{h} = \frac{2(0.05)}{0.1} = 1. \]

Step 2 — first row. \(u_{i,1} = f(x_i)\); for example \(u_{2,1} = \sin(0.1\pi) + \sin(0.2\pi) = 0.309017 + 0.587785 = 0.896802\).

Step 3 — second row. Since \(g = 0\) and \(r = 1\), formula (13) applies: \(u_{i,2} = (f_{i-1} + f_{i+1})/2\). For example

\[ u_{2,2} = \frac{f(0) + f(0.2)}{2} = \frac{0 + 1.538842}{2} = 0.769421. \]

Step 4 — later rows. With \(r = 1\), formula (12) gives each new value from three old ones. For example, at \(x_2 = 0.1\), \(t_3 = 0.1\),

\[ u_{2,3} = u_{3,2} + u_{1,2} - u_{2,1} = 1.328438 + 0 - 0.896802 = 0.431636. \]

Continuing row by row gives the table below for \(0 \le t_j \le 0.5\) (columns are the interior points \(x_i = 0.1, \ldots, 0.9\); the end values are \(0\)).

\(t_j\) \(0.1\) \(0.2\) \(0.3\) \(0.4\) \(0.5\) \(0.6\) \(0.7\) \(0.8\) \(0.9\)
0.00 0.896802 1.538842 1.760074 1.538842 1.000000 0.363271 −0.142040 −0.363271 −0.278768
0.05 0.769421 1.328438 1.538842 1.380037 0.951057 0.428980 0.000000 −0.210404 −0.181636
0.10 0.431636 0.769421 0.948401 0.951057 0.809017 0.587785 0.360616 0.181636 0.068364
0.15 0.000000 0.051599 0.181636 0.377381 0.587785 0.740653 0.769421 0.639384 0.363271
0.20 −0.380037 −0.587785 −0.519421 −0.181636 0.309017 0.769421 1.019421 0.951057 0.571020
0.25 −0.587785 −0.951057 −0.951057 −0.587785 0.000000 0.587785 0.951057 0.951057 0.587785
0.30 −0.571020 −0.951057 −1.019421 −0.769421 −0.309017 0.181636 0.519421 0.587785 0.380037
0.35 −0.363271 −0.639384 −0.769421 −0.740653 −0.587785 −0.377381 −0.181636 −0.051599 0.000000
0.40 −0.068364 −0.181636 −0.360616 −0.587785 −0.809017 −0.951057 −0.948401 −0.769421 −0.431636
0.45 0.181636 0.210404 0.000000 −0.428980 −0.951057 −1.380037 −1.538842 −1.328438 −0.769421
0.50 0.278768 0.363271 0.142040 −0.363271 −1.000000 −1.538842 −1.760074 −1.538842 −0.896802

The rows for \(0.55 \le t_j \le 1\) repeat these in reverse order: row \(t_j\) equals row \(1 - t_j\), as the exact solution requires, since \(\cos(2\pi(1 - t)) = \cos(2\pi t)\) and \(\cos(4\pi(1 - t)) = \cos(4\pi t)\). At \(t = 1\) the string is back in its initial position.

Verification. The function (14) satisfies \(u_{tt} - 4u_{xx} = 0\), \(u(x, 0) = f(x)\) and \(u_t(x, 0) = 0\). Over all \(11 \times 21\) grid points the computed values differ from (14) by at most \(1.5 \times 10^{-15}\), which is rounding error: by the theorem above the method is exact here. For example, at \(x = 0.3\), \(t = 0.2\) the table gives \(-0.519421\), and (14) gives \(\sin(0.3\pi)\cos(0.4\pi) + \sin(0.6\pi)\cos(0.8\pi) = 0.809017(0.309017) + 0.951057(-0.809017) = -0.519421\).

Mathews prints the whole \(21\)-row table rounded to three significant digits, and every entry agrees with the computed values. His surface plot of the grid values is followed by a surface plot of the exact solution (14) over \(0 \le t \le 1\), \(0 \le x \le 1\) from the same viewpoint, and the two look alike. Three slips in the module are corrected here. The example statement gives the exact solution as \(\sin(\pi x)\cos(\pi t) + \sin(2\pi x)\cos(4\pi t)\), which does not satisfy \(u_{tt} = 4u_{xx}\) (its residual is \(3\pi^2 \sin(\pi x)\cos(\pi t)\)); the notebook itself compares with (14). The remark before the solution says “\(c = 2\), \(h = 0.2\), \(k = 0.04\)” but then computes \(r = 2(0.05)/0.1 = 1\), and the notebook uses \(h = 0.1\) and \(k = 0.05\). And the textbook version of this table (Mathews’ linked proof, Table 10.1) prints \(\pm 0.951056\) where the value \(\sin(0.4\pi) = 0.9510565\) rounds to \(\pm 0.951057\).

What the exact second row is worth

The theorem depends on the second row being exact. If the first-order formula (7), \(u_{i,2} = f_i\), is used instead of (13) on the same grid, the second row is already off by up to \(0.2212\), and the error over the whole grid reaches \(0.3976\), since formula (6) carries it forward without damping it.

With \(r < 1\) the method is no longer exact, but it converges at second order. Running Example 1 with \(r = 0.5\) (so \(k = h/4\)) over \(0 \le t \le 1\), with the second row from (8):

\(h\) \(k\) max error over the grid ratio to the previous error
\(0.1\) \(0.025\) \(1.392 \times 10^{-1}\) —
\(0.05\) \(0.0125\) \(3.609 \times 10^{-2}\) \(3.857\)
\(0.025\) \(0.00625\) \(9.031 \times 10^{-3}\) \(3.996\)
\(0.0125\) \(0.003125\) \(2.257 \times 10^{-3}\) \(4.002\)

Halving \(h\) and \(k\) together divides the error by about \(4\), the behaviour expected from the \(O(h^2)\) and \(O(k^2)\) terms dropped in (3) and (4).

Example 2 — a plucked string, \(r = 1\)

Solve \(u_{tt}(x, t) = 4\,u_{xx}(x, t)\) for \(0 < x < 1\), \(0 < t < 1\), with \(u(0, t) = u(1, t) = 0\), \(u_t(x, 0) = g(x) = 0\) and the initial position

\[ u(x, 0) = f(x) = \begin{cases} x & \text{for } 0 \le x \le \tfrac35, \\[1mm] \tfrac32 - \tfrac32 x & \text{for } \tfrac35 \le x \le 1, \end{cases} \]

over the rectangle \(R = \{(x, t) : 0 \le x \le 1,\ 0 \le t \le 1\}\). The string is pulled up to height \(0.6\) at \(x = 0.6\) and released from rest; Mathews first plots \(f\) on \([0, 1]\).

Step 1 — grid. As in Example 1, \(n = 11\), \(m = 21\), \(h = 0.1\), \(k = 0.05\) and \(r = 2(0.05)/0.1 = 1\), so \(r \le 1\) holds.

Step 2 — first and second rows. \(u_{i,1} = f(x_i)\), and by (13), \(u_{i,2} = (f_{i-1} + f_{i+1})/2\). Only the point at the peak changes: \(u_{7,2} = (f(0.5) + f(0.7))/2 = (0.5 + 0.45)/2 = 0.475\), while for instance \(u_{6,2} = (0.4 + 0.6)/2 = 0.5 = f(0.5)\), because \(f\) is linear on each side of the peak.

Step 3 — later rows. Formula (12) gives, for example, \(u_{6,3} = u_{7,2} + u_{5,2} - u_{6,1} = 0.475 + 0.4 - 0.5 = 0.375\). The rows for \(0 \le t_j \le 0.5\) are

\(t_j\) \(0.1\) \(0.2\) \(0.3\) \(0.4\) \(0.5\) \(0.6\) \(0.7\) \(0.8\) \(0.9\)
0.00 0.100 0.200 0.300 0.400 0.500 0.600 0.450 0.300 0.150
0.05 0.100 0.200 0.300 0.400 0.500 0.475 0.450 0.300 0.150
0.10 0.100 0.200 0.300 0.400 0.375 0.350 0.325 0.300 0.150
0.15 0.100 0.200 0.300 0.275 0.250 0.225 0.200 0.175 0.150
0.20 0.100 0.200 0.175 0.150 0.125 0.100 0.075 0.050 0.025
0.25 0.100 0.075 0.050 0.025 0.000 −0.025 −0.050 −0.075 −0.100
0.30 −0.025 −0.050 −0.075 −0.100 −0.125 −0.150 −0.175 −0.200 −0.100
0.35 −0.150 −0.175 −0.200 −0.225 −0.250 −0.275 −0.300 −0.200 −0.100
0.40 −0.150 −0.300 −0.325 −0.350 −0.375 −0.400 −0.300 −0.200 −0.100
0.45 −0.150 −0.300 −0.450 −0.475 −0.500 −0.400 −0.300 −0.200 −0.100
0.50 −0.150 −0.300 −0.450 −0.600 −0.500 −0.400 −0.300 −0.200 −0.100

and, as in Example 1, the rows for \(0.55 \le t_j \le 1\) repeat these in reverse order, ending at \(t = 1\) with the initial shape. Every value on the grid is a multiple of \(0.025\).

What the table shows. The corner of the plucked string at \(x = 0.6\) splits into two corners, one moving left and one moving right, each one grid step \(h = 0.1\) per time step \(k = 0.05\), which is the wave speed \(c = 2\). In the row \(t = 0.05\) they are at \(x = 0.5\) and \(x = 0.7\), and in the row \(t = 0.10\) at \(x = 0.4\) and \(x = 0.8\). Between them the string is a straight segment of slope \(-0.25\), the average of the two slopes \(1\) and \(-1.5\) of \(f\). Each corner is reflected at a fixed end and comes back with the displacement turned upside down. At \(t = 0.5\) the string has the shape \(u(x, 0.5) = -f(1 - x)\), the initial shape mirrored and inverted, with its lowest point \(-0.6\) at \(x = 0.4\); it then retraces its motion and returns to \(f\) at \(t = 1\).

Verification. Here \(f\) has a corner, so the solution is d’Alembert’s formula (11) with \(\tilde f\) the odd, period-2 extension of \(f\). The computed values agree with (11) at all \(11 \times 21\) grid points to within \(4.5 \times 10^{-16}\). For example, at \(x = 0.4\), \(t = 0.5\): \(\tfrac12(\tilde f(1.4) + \tilde f(-0.6)) = \tfrac12(-f(0.6) - f(0.6)) = -0.6\), the entry in the table. Mathews prints the full \(21\)-row table, and every entry agrees with the values computed here; the textbook’s Table 10.2 gives the same rows for \(0 \le t \le 0.5\).

Example 3 — the same string with \(r = 1.02\): instability

Mathews repeats Example 2 with the same \(f\), \(g = 0\) and \(c = 2\), but over the slightly longer rectangle \(R = \{(x, t) : 0 \le x \le 1,\ 0 \le t \le 1.02\}\), and asks the reader to notice the instability this produces, as predicted by the analysis above. The archive has lost the lines of the notebook that state his grid, and the plot of his result. Keeping Example 2’s grid, \(n = 11\) and \(m = 21\), the new interval gives

\[ h = 0.1, \qquad k = \frac{1.02}{20} = 0.051, \qquad r = \frac{2(0.051)}{0.1} = 1.02 > 1. \]

Steps. Row 1 is \(f\), row 2 comes from formula (8) with \(r^2 = 1.0404\), and rows 3 to 21 from formula (6) with \(2 - 2r^2 = -0.0808\). The second row is no longer exact (formula (13) no longer applies), but the larger problem is stability: by (9) the mode \(p = 9\), which on this grid is the sawtooth \(\sin(9\pi x_i) = (-1)^{i}\sin(\pi x_i)\), has \(r^2 s_9 > 1\) and the roots \(\lambda = -1.2762\) and \(\lambda = -0.7836\). That mode changes sign at every step and grows. In the table, its size is the absolute value of the \(p = 9\) coefficient \(\tfrac{2}{10}\sum_{i} u_{i,j}\sin(9\pi x_i)\) in the discrete sine expansion of row \(j\).

\(j\) \(t_j\) \(\max_i \lvert u_{i,j}\rvert\) \(\max_i \lvert u_{i,j} - u(x_i, t_j)\rvert\) size of the sawtooth mode \(p = 9\)
1 0.000 0.6000 0.0000 0.0122
5 0.204 0.2000 0.0135 0.0185
9 0.408 0.4530 0.0580 0.0437
13 0.612 0.4595 0.1145 0.114
17 0.816 0.4744 0.3079 0.302
21 1.020 1.3118 0.7845 0.800

Here \(u(x, t)\) is the exact d’Alembert solution (11), whose values never exceed \(0.6\) in absolute value. The error grows at every step, from \(0.0026\) at \(j = 2\) to \(0.7845\) at \(j = 21\). The sawtooth mode, which is already present in \(f\) with amplitude \(0.0122\), grows by a factor of about \(66\) over the \(20\) steps, and its step-to-step growth ratio settles at \(1.276\), the size of the unstable root. On the last row the computed string and the exact one are

\(0.1\) \(0.2\) \(0.3\) \(0.4\) \(0.5\) \(0.6\) \(0.7\) \(0.8\) \(0.9\)
\(u_{i,21}\) −0.1535 0.6807 −0.3575 1.1637 −0.2845 1.3118 −0.1773 0.7648 −0.0969
\(u(x_i, 1.02)\) 0.1000 0.2000 0.3000 0.4000 0.5000 0.5500 0.4500 0.3000 0.1500

The computed values alternate in sign from point to point and reach more than twice the largest displacement the true string ever has. A two-percent increase in \(k\) over Example 2 is enough to break the method, as the stability condition \(r \le 1\) predicts.

References

Mathew, John H. 2000-2019. Numerical Analysis - Numerical Methods Modules. https://web.archive.org/web/20190808102217/http://mathfaculty.fullerton.edu/mathews/n2003/NumericalUndergradMod.html.