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

A12 · Lesson 9 · Lab: complex functions in code

Checking analyticity, branches, roots and maps numerically

Lessons 1 to 8 built up the complex derivative, the Cauchy–Riemann equations, branches of the logarithm, roots and bilinear maps. Python works with complex numbers directly (1 + 2j, with cmath for functions), so each of those ideas can be tested by computation. In this lab you'll take derivatives in two directions to see analyticity hold and fail, pick branches of the logarithm, place a filter's poles as roots, check the Smith chart's circles to rounding error, and measure how a conformal map stretches area.

  • A central difference f(z+h)−f(z−h)2h has an error that shrinks as h2. For f=z3 the error is exactly h2.
  • The curve shows that error against h on log axes, together with rounding error, which grows as ε/h with ε≈10−16.
  • The two meet near h=10−5, where the total error is smallest. That's the step the lab uses.

Predict

You estimate the derivative of z¯ at a point once by stepping along the real axis and once by stepping along the imaginary axis. What do you get?

Name it

Complex numbers in Python

Write j as 1j. abs(z) is the size, cmath.phase(z) the principal angle in (−π,π], z.conjugate() the conjugate, and cmath.exp, cmath.log and cmath.sqrt the principal-branch functions. NumPy arrays of complex numbers work the same way, element by element.

Testing analyticity numerically

Take central differences along x and along y: f(z+h)−f(z−h)2h and f(z+jh)−f(z−jh)2jh. For an analytic f both approach f′(z), and their difference is about h2. For a non-analytic f they differ by a fixed amount however small h gets.

Checking a map

To test a claim like "this line maps onto that circle", map many points along the line and compute the largest distance from the circle. A correct claim leaves only rounding error, around 10−15; a wrong one leaves a visible gap.

Worked example

Find the poles of a fourth-order Butterworth filter with ωc=1 numerically.

Show the solution
  1. The equation. The poles are the left-half-plane roots of s8=−1
  2. Find them in code. Compute all eight roots with r = [cmath.exp(1j*(math.pi + 2*math.pi*k)/8) for k in range(8)], then keep those with negative real part.
  3. The four poles. −0.3827±0.9239j−0.9239±0.3827j at angles of 112.5° and 157.5° and their mirror images.
  4. Multiply out the pairs. (s2+0.7654s+1)×(s2+1.8478s+1) gives the familiar Butterworth polynomial; the coefficients 2cos⁡67.5° and 2cos⁡22.5° come straight from the pole angles.

Answer the poles are −0.3827±0.9239j and −0.9239±0.3827j, at angles of 112.5° and 157.5° and their mirror images.

Worked example

Check numerically that w=1/z maps the line Rez=12 onto the circle |w−1|=1.

Show the solution
  1. Map a long stretch of the line. y = np.linspace(-100, 100, 4001), then w = 1/(0.5 + 1j*y).
  2. Measure the worst miss. np.max(np.abs(np.abs(w - 1) - 1)) comes out about 2×10−16: every mapped point lies on the circle to rounding error.
  3. A control. Test against the wrong circle |w−1|=0.9: the largest gap is then 0.1.
  4. Why bother. Numerical checks like this catch sign slips and wrong centers quickly, before they reach a design.

Answer every mapped point is within about 2×10−16 of the circle |w−1|=1, which is rounding error.

Check

For f=z3, the central difference f(z+h)−f(z−h)2h equals 3z2+h2 exactly. What is its error with h = 0.001?

Check

f(z)=z2 maps a tiny square near z0=1+1j. By what factor does the square's area grow?

Check

Write deriv_x(f, z, h) and deriv_y(f, z, h), central differences stepping along the real and imaginary directions. With h = 1e-5, set a_x, a_y for f=z3 at z0=1+2j, and b_x, b_y for f=z¯ there.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write log_branch(z, k) returning ln⁡|z|+j(Argz+2πk). Use it to set jj0 to jj on the principal branch and jj1 on the branch k = 1, both as ejlog⁡j.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write nth_roots(w, n) returning the n roots of w. Set poles to the left-half-plane roots of s8=−1, the poles of a fourth-order Butterworth filter, sorted by imaginary part.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Map the constant-resistance line z=2+jx, −50≤x≤50 (2001 points), through Γ=z−1z+1. Set dev to the largest distance of the image from the circle with center rr+1 and radius 1r+1, and back to the largest error after mapping back with z=1+Γ1−Γ.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write image_area(f, z0, h): map the corners of the square with corners z0, z0+h, z0+h+jh, z0+jh through f and return the area of the resulting quadrilateral (the shoelace formula). Set ratio to that area over h2 for f=z2, z0=1+j and h=10−4.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why is the best step for a numerical derivative neither very large nor very small?

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.