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

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 ∮ezz−1dz. Its error falls as 2πer−n: 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 r10.
  • With r = 2, 32 points already give nine correct digits.

Predict

You integrate ezz−1 around |z|=2 with the trapezoid rule. Going from 16 to 32 points, roughly what happens to the error?

Name it

The trapezoid rule on a circle

On z=c+rejθ, dz=jrejθdθ, so with n equally spaced angles θk=2πk/n,

∮fdz≈2πn∑k=0n−1f(zk)jrejθk.

For an integrand analytic in a ring around the circle, the error falls like ρ−n, where ρ measures how far the nearest singularity is from the circle (here ρ=r/1 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 12πj∮fdz around a small circle enclosing only that pole. The number of zeros minus poles inside a curve is the winding number of f, which you can read off by tracking arg⁡f along the curve with its jumps of 2π removed (np.unwrap).

Principal values on a grid

A midpoint grid placed symmetrically about the singular point, ω′=ω+(k+12)d, pairs each point at +u with one at −u. The large terms ±R(ω)u cancel in pairs, which is exactly what the principal value does.

Worked example

How fast does the trapezoid rule converge for ∮|z|=2ezz−1dz=2πje?

Show the solution
  1. Measured errors. 6.4 with 4 points, 0.11 with 8, 2.6×10−4 with 16, and 4.0×10−9 with 32. The last two match 2πe⋅2−n to three digits.
  2. Expand the integrand. On the circle, zezz−1=ez∑m≥0z−m and the coefficient of z−n in that product is ∑k1k!=e.
  3. Where the error comes from. The n-point rule can't tell z−n from z0 on its sample points, so it adds that term's contribution, 2πjer−n, to the answer.
  4. The rate. Each doubling of n squares the error.

Answer the error falls like 2πe⋅2−n: 2.6×10−4 with 16 points and 4.0×10−9 with 32.

Worked example

Count the zeros of z5+3z+1 inside |z|=1 and |z|=2 by computing winding numbers.

Show the solution
  1. Sample and unwrap. Sample p(z) at 4000 points around each circle, take np.unwrap(np.angle(p)), and divide the total change by 2π.
  2. The results. They are 1 and 5, matching the Rouché argument of lesson 21.
  3. 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.
  4. 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 |z|=1 and five inside |z|=2.

Check

What is the residue of e1zsin⁡z at z=π? (A numerical contour integral around a small circle should give the same.)

Check

You integrate 1x4+1 numerically over [−L,L] 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 ∮ezz−1dz around |z|=2.

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 ezsin⁡z 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 ∫−∞∞dxx4+1 from the residues 14z3 at the upper-half-plane roots of z4+1 (find them with np.roots), and numeric to a trapezoid-rule value over [−1000,1000] 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 f(z) winds around 0 as z goes once around |z|=r. Set w1 and w2 for z5+3z+1 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 1πPV∫R(ω′)ω′−ω0dω′ on the midpoint grid ω′=ω0+(k+12)d with d=10−3 over |ω′−ω0|<1000. Set X to its value for R=11+ω2 at ω0=2.

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?

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.