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"
for a collection of nodes t0,…,tn in [−1,1] and weights w0,…,wn. (Throughout this section we use [−1,1] as the domain of the integral; for a general interval [a,b], see Exercise 9.6.4.) The nodes and weights are independent of the integrand f(x) and determine the implementation and properties of the formula.
The process for deriving a specific method was to interpolate the integrand, then integrate the interpolant. Piecewise linear interpolation at equally spaced nodes, for instance, produces the trapezoid formula. When the integrand is approximated by a spectrally accurate global function, the integration formulas are also spectrally accurate.
and integrate the resulting polynomial interpolant, the method should have spectral accuracy for a smooth integrand. The resulting algorithm is known as Clenshaw–Curtis integration.
Having specified the nodes in (9.6.1), all that remains is to find the weights. The Lagrange form of the interpolating polynomial is
There are different formulas for odd values of n. Note that the weights also depend on n; e.g. w2 for n=4 is not the same as w2 for n=10. Also note that the interpolant itself never needs to be computed.
Function 9.6.1 performs Clenshaw–Curtis integration for even values of n.[1]
where Qn[f] stands for the application of the formula to function f. (We start the sum from k=1 instead of k=0 for notational convenience in what follows.)
The interpolation approach spurred us to use Chebyshev nodes. But it’s not clear that these are ideal nodes for the specific application of finding an integral. Instead, we can define formula as the integral of a polynomial interpolant, but with the weights and nodes chosen to satisfy an optimality criterion. As usual, we denote the set of all polynomials of degree at most m by Pm.
Since there are n nodes and n weights available to choose, it seems plausible to expect m=2n−1, and this intuition turns out to be correct. Hence, the goal is now to find nodes tk and weights wk such that
If these conditions are satisfied, the resulting method is called Gauss–Legendre integration or simply Gaussian integration. Because the integration formula is linear, i.e., Qn[αp+q]=αQn[p]+Qn[q], it is sufficient to show that Qn gets the exact value for the monomials 1,x,x2,…,x2n−1.
Generalizing the process above to general n would be daunting, as the conditions on the nodes and weights are nonlinear. Fortunately, a more elegant approach is possible.
From Theorem 9.4.3 we know that the roots of Pn are distinct and all within (−1,1). (Indeed, it would be strange to have the integral of a function depend on some of its values outside the integration interval!) While there is no explicit formula for the roots, there are fast algorithms to compute them and the integration weights on demand. Function 9.6.2 uses one of the oldest methods, practical up to n=100 or so.
Both Clenshaw–Curtis and Gauss–Legendre integration are spectrally accurate. The Clenshaw–Curtis method on n+1 points has degree n, whereas the Gauss–Legendre method with n points has degree 2n−1. For this reason, it is possible for Gauss–Legendre to converge at a rate that is “twice as fast,” i.e., with roughly the square of the error of Clenshaw–Curtis. But the full story is not simple.
The difference in convergence between Clenshaw–Curtis and Gauss–Legendre is dwarfed by the difference between spectral and algebraic convergence. It is possible, though, to encounter integrands for which adaptivity is critical. Choosing a method is highly problem-dependent, but a rule of thumb is that for large error tolerances, an adaptive low-order method is likely to be a good choice, while for high accuracy, the spectral methods often dominate.