A12 · Lesson 23 · Lab: contour integrals in code
Integrating around circles, finding residues and counting zeros numerically
Lessons 11 to 22 turned contour integrals into residues and residues into real integrals, inverse transforms, zero counts and causality relations. Each step can be checked numerically, and the numerics are surprisingly good: the plain trapezoid rule, applied around a circle, converges faster than any power of the step for analytic integrands. In this lab you'll build a contour integrator, use it to find residues, check a residue-theorem integral against brute force, count zeros by winding, and evaluate a Kramers–Kronig principal value.
- The trapezoid rule with n points around a circle of radius r computes . Its error falls as : the farther the pole is inside the circle, relative to r, the faster.
- The curve shows that error against n for the r you choose, on a log scale. Every 10 extra points cut it by a factor .
- With r = 2, 32 points already give nine correct digits.
Predict
You integrate around with the trapezoid rule. Going from 16 to 32 points, roughly what happens to the error?
It falls by about : from to . On a closed curve the trapezoid rule has no end errors, and for an analytic integrand its error falls exponentially with the number of points, at a rate set by how close the nearest singularity is to the circle.
Name it
The trapezoid rule on a circle
On , , so with n equally spaced angles ,
For an integrand analytic in a ring around the circle, the error falls like , where ρ measures how far the nearest singularity is from the circle (here for a pole at 1 inside a circle of radius r). This exponential convergence makes contour integrals a practical numerical tool, for residues, inverse transforms and counting zeros.
Residues and zeros by computation
A residue is around a small circle enclosing only that pole. The number of zeros minus poles inside a curve is the winding number of , which you can read off by tracking along the curve with its jumps of removed (np.unwrap).
Principal values on a grid
A midpoint grid placed symmetrically about the singular point, , pairs each point at with one at . The large terms cancel in pairs, which is exactly what the principal value does.
Worked example
How fast does the trapezoid rule converge for ?
Show the solution
- Measured errors. 6.4 with 4 points, 0.11 with 8, with 16, and with 32. The last two match to three digits.
- Expand the integrand. On the circle, and the coefficient of in that product is .
- Where the error comes from. The n-point rule can't tell from on its sample points, so it adds that term's contribution, , to the answer.
- The rate. Each doubling of n squares the error.
Answer the error falls like : with 16 points and with 32.
Worked example
Count the zeros of inside and by computing winding numbers.
Show the solution
- Sample and unwrap. Sample at 4000 points around each circle, take
np.unwrap(np.angle(p)), and divide the total change by 2π. - The results. They are 1 and 5, matching the Rouché argument of lesson 21.
- How many samples. The number only has to be large enough that the angle never jumps by more than π between neighbors; otherwise unwrapping can lose a turn.
- Zeros near the circle. A zero very close to the circle makes the angle swing fast there, so it needs more points.
Answer one zero inside and five inside .
Check
What is the residue of at ? (A numerical contour integral around a small circle should give the same.)
Check
You integrate numerically over with L = 100 instead of the whole line. About how much do the missing tails contribute?
Check
Write contour_integral(f, c, r, n=64) using the trapezoid rule on the circle of radius r around c (f takes NumPy arrays). Set I to around .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Use a contour integral around a circle of radius 0.5 to set res0 and respi to the residues of at 0 and at π.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set by_residues to from the residues at the upper-half-plane roots of (find them with np.roots), and numeric to a trapezoid-rule value over with 2 000 001 points.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write winding(f, r, n=4000) returning the number of times winds around 0 as z goes once around . Set w1 and w2 for with r = 1 and r = 2.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write kk_x(R, w0) returning on the midpoint grid with over . Set X to its value for at .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does the trapezoid rule converge so much faster around a closed circle than on an ordinary interval?
On an interval, the trapezoid rule's error comes from the ends: the Euler–Maclaurin formula expresses it through the differences of the integrand's derivatives at the two endpoints, which gives an error proportional to . Around a closed curve the integrand is periodic, so every one of those end terms cancels, and the only error left is aliasing: the n sample points can't distinguish from a constant, so the rule adds in the Fourier coefficients at multiples of n. For an analytic integrand those coefficients fall exponentially, at a rate set by the distance to the nearest singularity, so the error falls exponentially with n.
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.