A13 · Lesson 21 · Lab: approximation and quadrature in code
Chebyshev coefficients, Runge's phenomenon, Gauss rules, singularities and lobe sums
Lessons 11 to 20 built approximations and integration rules on paper. Here you'll run them. You'll compute Chebyshev coefficients from a dozen samples and see them match the exact values, reproduce Runge's phenomenon and its cure, watch Gauss–Legendre quadrature gain two or three digits per extra point, remove a singularity by substitution, and accelerate the slowly converging integral of sin x / x by averaging lobe sums.
- The dots are the errors of Gauss–Legendre rules with n points on , on a log scale: , , , for n = 2 to 5.
- The line through them falls by roughly two to three digits per point, the signature of spectral convergence for an entire integrand.
- Compare Simpson's rule, whose error with the same number of points falls only as .
Predict
You fit a degree-10 polynomial through 11 points of and then through 11 Chebyshev points. Roughly how do the worst errors compare?
About 1.9 for equally spaced points and 0.11 for Chebyshev points. You'll reproduce both numbers below with np.polyfit. The equally spaced fit oscillates wildly near the ends; the Chebyshev fit spreads its error evenly.
Name it
Chebyshev coefficients from samples
Sample f at with , j = 0, …, N − 1, and compute . That's the midpoint rule on the integral formula of lesson 13, and since the integrand is periodic in θ it's spectrally accurate: a dozen samples give the first several coefficients to full precision. (The series uses for the constant term.)
Gauss–Legendre in NumPy
t, w = np.polynomial.legendre.leggauss(n) returns nodes and weights on [−1, 1]. For [a, b], use and multiply the weights by .
Lobes
For , integrate each lobe with a Gauss rule, form partial sums, and average neighbors repeatedly (Euler's transform, lesson 20). In NumPy, np.sinc(x / np.pi) is , with the value 1 at 0 built in.
Worked example
Compute the first Chebyshev coefficients of from 12 samples.
Show the solution
- The sums. With N = 12 and , compute For k = 0 to 5 they give 2.532132, 1.130318, 0.271495, 0.044337, 0.005474, 0.000543.
- Compare with the exact values. These match to every digit shown. The constant term of the series is half the first, 1.266066, as in lesson 13.
- Why twelve samples are enough. The coefficients fall so fast that the aliasing error, from coefficients beyond k = 12, is below .
Answer 2.532132, 1.130318, 0.271495, 0.044337, 0.005474 and 0.000543 for k = 0 to 5, matching .
Worked example
How fast does Gauss–Legendre quadrature converge on ?
Show the solution
- The errors. With 2, 3, 4 and 5 points they are , , and .
- The rate. Each extra point gains about 2 to 2.6 digits, more as n grows.
- Why. An n-point rule is exact for polynomials of degree 2n − 1, and 's Taylor terms shrink factorially, so the error falls faster than any power of n.
- Compare with the trapezoid rule. Five function evaluations give nine digits, which the trapezoid rule would need about 30 000 evaluations to match.
Answer faster than any power of n: each point adds about 2 to 2.6 digits, and five points give nine digits.
Check
What is the smallest Gauss–Legendre rule that integrates every polynomial of degree 8 exactly?
Check
A rule's error falls from 0.0077 to 3 × 10⁻⁶ with one more point. How many decimal digits did that point gain?
Check
Set c to the list of Chebyshev coefficients to of , from N = 12 samples with and .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write interp_err(nodes) returning the worst error, on 20 001 points of [−1, 1], of the polynomial through nodes for (use np.polyfit and np.polyval). Set equal for 11 equally spaced nodes and cheb for the 11 Chebyshev nodes .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Use np.polynomial.legendre.leggauss to set errs to the errors of the n-point rules, n = 2 to 5, on (computed minus exact).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Compute by substituting and applying the 20-point Gauss–Legendre rule on [0, 1]. Set value.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Integrate each of the first 8 lobes of with the 40-point Gauss rule, form the partial sums S, then average neighbors three times. Set last to the final averaged value.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why do a dozen samples give Chebyshev coefficients to full precision, when quadrature usually needs many more points?
Substituting turns the Chebyshev coefficient integral into the Fourier cosine coefficient of the periodic, smooth function . The midpoint rule on a periodic integrand doesn't suffer the endpoint errors that normally limit it; its only error is aliasing, where coefficient k picks up contributions from coefficients near 2N − k and beyond. For an analytic function those far coefficients are smaller than rounding error once N exceeds a modest number, so the computed coefficients are exact to the last digit. The same reasoning explains why the trapezoid rule is spectrally accurate for periodic functions.
Practice
Keep practicing
4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.