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?
1.5707963..., which is . NumPy and Python's cmath work in radians. np.angle(z, deg=True) gives degrees, and np.degrees(x) converts any radian value.
Name it
Complex numbers are built in
Python writes after a number: 3 + 4j, or 1j on its own. complex(a, b) builds 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 directly: r * np.exp(1j * theta), with θ in radians (np.radians(deg) converts).
Impedances. With :
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
- Build the impedance and divide. , and the current is .
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
- Read the results. = 118.1 Ω at 32.14°, so lagging the voltage by 32.1°.
- 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 and .
Show the solution
- 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
- Check. They're 90° apart, so the length is as the code confirms.
Answer the sum is .
Check
Write zc(C, f) that returns a capacitor's complex impedance, .
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 . Compute mag, the size of 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 and 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 . Set f_min to the frequency where is smallest and Z_min to that smallest . Then set f0 to 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?
Formulas like assume base units. If C is stored as 100 (meaning nF) instead of 100e-9, every result is wrong by a factor of a billion, and nothing in the code warns you. Storing everything in base units means formulas work as written, and the conversion to friendly units happens only once, when the result is printed or plotted.
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.