A436 of 56
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

A4 · Lesson 36 · Complex numbers in code

Phasors and impedances in a few lines of Python

In A3 you added phasors by converting to rectangular form, adding, and converting back, by hand. Python's complex numbers do all of that for you, and NumPy does it for a thousand frequencies at once. This is how filter responses get computed in practice.

Predict

With import numpy as np, what does np.angle(1j) return?

Name it

Complex numbers are built in

Python writes j after a number: 3 + 4j, or 1j on its own. complex(a, b) builds a+jb from two variables. Arithmetic, including division, just works:

import numpy as np
z = 3 + 4j
abs(z)                    # 5.0
np.angle(z, deg=True)     # 53.13...
z.real, z.imag            # 3.0, 4.0
z.conjugate()             # (3-4j)

To build a number from polar form, use rejθ directly: r * np.exp(1j * theta), with θ in radians (np.radians(deg) converts).

Impedances. With ω=2πf:

w = 2 * np.pi * f
ZR = R
ZC = 1 / (1j * w * C)
ZL = 1j * w * L

Series impedances add; parallel ones combine as Z1 * Z2 / (Z1 + Z2), the same formula as for resistors.

Arrays of frequencies. If f is an array, every line above computes all the frequencies at once, and np.abs and np.angle work element by element.

Units. Code has no units, so the course keeps every quantity in base SI units: ohms, farads, henries, hertz, seconds. A capacitor of 100 nF is C = 100e-9. If you keep to that, the formulas need no conversion factors.

Worked example

Find the current in a 100 Ω resistor and 10 mH inductor in series, driven with 5 V amplitude at 1 kHz.

Show the solution
  1. Build the impedance and divide. Z=R+jωL, and the current is 5/Z.
import cmath, math
Z = 100 + 1j * 2 * math.pi * 1000 * 10e-3
I = 5 / Z
print(abs(Z), math.degrees(cmath.phase(Z)))     # 118.1 32.14
print(abs(I) * 1e3, math.degrees(cmath.phase(I)))   # 42.34 -32.14
  1. Read the results. |Z| = 118.1 Ω at 32.14°, so |I|=5V118.1Ω=42.3mA lagging the voltage by 32.1°.
  2. Compare. It's the same answer A3's hand calculation gives, in three lines.

Answer 42.3 mA, lagging the voltage by 32.1°.

Worked example

Add 3∠20° and 4∠110°.

Show the solution
  1. Make each phasor and add. cmath.exp(1j * θ) turns each angle into a unit phasor.
S = 3 * cmath.exp(1j * math.radians(20)) + 4 * cmath.exp(1j * math.radians(110))
print(abs(S), math.degrees(cmath.phase(S)))     # 5.0 73.13
  1. Check. They're 90° apart, so the length is |S|=32+42=5 as the code confirms.

Answer the sum is 5∠73.13°.

Check

Write zc(C, f) that returns a capacitor's complex impedance, 1/(j2πfC).

C = 100 nF (in farads) and f = 50 Hz are already defined.

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

Check

An RC low-pass filter has H=ZC/(R+ZC). Compute mag, the size of H at 10000 Hz, and phase_deg, its angle in degrees.

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

Check

Add the phasors 2∠−60° and 4∠150° in code. Set amp to the sum's amplitude and ang to its angle in degrees.

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

Check

Why does 1 / (1j * w * C) give a capacitor's impedance in ohms, with no conversion factor?

Check

A series RLC has R = 10 Ω, L = 1 mH and C = 100 nF. Using a NumPy array of 200,001 frequencies from 1 kHz to 100 kHz, compute the complex impedance Z=R+jωL+1/(jωC). Set f_min to the frequency where |Z| is smallest and Z_min to that smallest |Z|. Then set f0 to 1/(2πLC) for comparison.

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

Check

What does (1j) ** 2 give in Python?

Explain why

Why is it safer to keep every value in base SI units in code, rather than in the units printed on the parts?

Practice

Keep practicing

7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.