A1321 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 ∫−11exdx, on a log scale: 7.7×10−3, 6.5×10−5, 3.0×10−7, 8.2×10−10 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 n−4.

Predict

You fit a degree-10 polynomial through 11 points of 11+25x2 and then through 11 Chebyshev points. Roughly how do the worst errors compare?

Name it

Chebyshev coefficients from samples

Sample f at xj=cos⁡θj with θj=(j+12)πN, j = 0, …, N − 1, and compute ck=2N∑jf(xj)cos⁡kθj. 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 c02 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 x=a+b2+b−a2t and multiply the weights by b−a2.

Lobes

For ∫0∞sin⁡xxdx, integrate each lobe [kπ,(k+1)π] with a Gauss rule, form partial sums, and average neighbors repeatedly (Euler's transform, lesson 20). In NumPy, np.sinc(x / np.pi) is sin⁡xx, with the value 1 at 0 built in.

Worked example

Compute the first Chebyshev coefficients of ex from 12 samples.

Show the solution
  1. The sums. With N = 12 and θj=(j+0.5)π12, compute 212∑jecos⁡θjcos⁡kθj For k = 0 to 5 they give 2.532132, 1.130318, 0.271495, 0.044337, 0.005474, 0.000543.
  2. Compare with the exact values. These match 2Ik(1) to every digit shown. The constant term of the series is half the first, 1.266066, as in lesson 13.
  3. Why twelve samples are enough. The coefficients fall so fast that the aliasing error, from coefficients beyond k = 12, is below 10−13.

Answer 2.532132, 1.130318, 0.271495, 0.044337, 0.005474 and 0.000543 for k = 0 to 5, matching 2Ik(1).

Worked example

How fast does Gauss–Legendre quadrature converge on ∫−11exdx=e−1e?

Show the solution
  1. The errors. With 2, 3, 4 and 5 points they are −7.7×10−3, −6.5×10−5, −3.0×10−7 and −8.2×10−10.
  2. The rate. Each extra point gains about 2 to 2.6 digits, more as n grows.
  3. Why. An n-point rule is exact for polynomials of degree 2n − 1, and ex's Taylor terms shrink factorially, so the error falls faster than any power of n.
  4. 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 c0 to c5 of ex, from N = 12 samples with ck=2N∑jf(cos⁡θj)cos⁡kθj and θj=(j+1/2)πN.

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 f=11+25x2 (use np.polyfit and np.polyval). Set equal for 11 equally spaced nodes and cheb for the 11 Chebyshev nodes cos⁡(2i+1)π22.

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 ∫−11exdx (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 ∫01cos⁡xxdx by substituting x=t2 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 sin⁡xx 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?

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.