Interactive Educational Modules in
Scientific Computing

Finite Difference Approximations

This module illustrates the accuracy of finite difference approximations to the derivative of a function. A finite difference formula uses a weighted combination of values of the function at a sequence of (typically equally-spaced) sample points to approximate the derivative of the function at a desired point x. Appropriate weights for the sample function values can be derived using Taylor series expansions. Alternatively, and equivalently, the approximate derivative can also be obtained by differentiating at the desired point x the polynomial interpolating the sample function values. The order of accuracy of the resulting derivative approximation refers to the magnitude of the error as the spacing h between mesh points becomes arbitrarily small. For example, first-order accuracy means that the error is proportional to h, second-order accuracy means that the error is proportional to h2, and so on.

The user first selects a function from the left menu, and the function will be plotted in the left graph above. In addition to the example functions provided, the user can supply any desired function using the function parser provided in the function menu. The point x at which the derivative is to be approximated is then chosen using the left slider. The true value of the derivative is given by the slope of the tangent line at x, also drawn in the left graph. Next the user selects a finite difference formula from the right menu and a step size h using the right slider. The sample points for the chosen difference formula are plotted in the left graph, along with the polynomial interpolating the sample points, whose derivative at x (indicated by a light gray tangent line) gives the approximate derivative value.

For any chosen value of step size h, the magnitude of the resulting error in the approximate derivative value can be plotted in the right graph by clicking the Plot button. Plotting the error for a sequence of values of h yields a graph of the error curve, which in log-log scale is asymptotically a straight line whose slope gives the order of accuracy as h becomes arbitrarily small. Repeating this process for additional choices of finite difference formulas results in additional color-coded error curves whose slopes can be visually compared to illustrate the various orders of accuracy. The error plot can be erased at any time by clicking the Clear button.

For each finite difference formula in the menu, the sample points it uses are indicated by a sequence of integers. For the first-order forward difference formula, for example, the sequence 0, 1 means that the sample points are x = x+0h and x+h = x+1h. In addition to the standard finite difference formulas listed in the menu, the user can also add additional finite difference formulas by providing the appropriate sequence of integers for any desired set of sample points. Once added to the menu, these additional formulas can be used in the same way as the built-in ones.

References:

  1. Michael T. Heath, Scientific Computing, An Introductory Survey, 2nd edition, McGraw-Hill, New York, 2002. See Section 8.6.1.
  2. Randall J. LeVeque, Finite Difference Methods for Ordinary and Partial Differential Equations, SIAM, Philadelphia, 2007. See Figures 1.1 and 1.2.

Developers: Sukolsak Sakshuwong and Michael Heath