from numpy import *
from scipy import linalg
from scipy.linalg import norm
from matplotlib.pyplot import *
from prettytable import PrettyTable
from timeit import default_timer as timer
import sys
sys.path.append('fncbook/')
import fncbook as FNC
# This (optional) block is for improving the display of plots.
# from IPython.display import set_matplotlib_formats
# set_matplotlib_formats("svg","pdf")
# %config InlineBackend.figure_format = 'svg'
rcParams["figure.figsize"] = [7, 4]
rcParams["lines.linewidth"] = 2
rcParams["lines.markersize"] = 4
rcParams['animation.html'] = "jshtml" # or try "html5"
All the finite-difference formulas in the previous section based on equally spaced nodes converge as the node spacing h decreases to zero. However, note that to discretize a function over an interval [a,b], we use h=(b−a)/n, which implies n=(b−a)/h=O(h−1). As h→0, the total number of nodes needed grows without bound. So we would like to make h as large as possible while still achieving some acceptable accuracy.
Although we are measuring the truncation error only at x=0, it could be defined for other x as well. The definition adjusts naturally to use f′′(0) for difference formulas targeting the second derivative.
All the finite-difference formulas given in Finite differences are convergent.
Of major interest is the rate at which τf→0 in a convergent formula.
Hence, the forward-difference formula in Example 5.5.1 has order of accuracy equal to 1; i.e., it is first-order accurate. All else being equal, a higher order of accuracy is preferred, since O(hm) vanishes more quickly for larger values of m. As a rule, including more function values in a finite-difference formula (i.e., increasing the number of weights in (5.4.1)) increases the order of accuracy, as can be seen in Table 5.4.1 and Table 5.4.2.
Order of accuracy is calculated by expanding τf in a Taylor series about h=0 and ignoring all but the leading term.[1]
The truncation error τf(h) of a finite-difference formula is dominated by a leading term O(hm) for an integer m. This error decreases as h→0. However, we have not yet accounted for the effects of roundoff error. To keep matters as simple as possible, let’s consider the forward difference
As h→0, the numerator approaches zero even though the values f(x+h) and f(x) are not necessarily near zero. This is the recipe for subtractive cancellation error! In fact, finite-difference formulas are inherently ill-conditioned as h→0. To be precise, recall that the condition number for the problem of computing f(x+h)−f(x) is
Equation (5.5.8) indicates that while the truncation error τ vanishes as h decreases, the roundoff error actually increases thanks to the subtractive cancellation. At some value of h the two error contributions will be of roughly equal size. This occurs when
for a constant K that depends on x and f, but not h. In summary, for a first-order finite-difference method, the optimum spacing between nodes is proportional to ϵmach1/2. (This observation explains the choice of δ in Function 4.6.1.)
For a method of truncation order m, the details of the subtractive cancellation are a bit different, but the conclusion generalizes.
A different statement of the conclusion is that for a first-order formula, at most we can expect accuracy in only about half of the available machine digits. As m increases, we get ever closer to using the full accuracy available. Higher-order finite-difference methods are both more efficient and less vulnerable to roundoff than low-order methods.
The term truncation error is derived from the idea that the finite-difference formula, being finite, has to truncate the series representation and thus cannot be exactly correct for all functions.