A3 · Lesson 17 · Lab: phasor circuits in Python
A few functions that solve any AC divider
With impedances, every AC circuit becomes Ohm's law with complex numbers, and Python's complex numbers do the arithmetic for you. In this lab you'll write a small toolkit: impedances of R, L and C, series and parallel combinations, a divider, a frequency sweep, and a solver that finds a filter's corner on its own. By the end, a filter you can write as 's is one you can analyze at any frequency in a line or two. If you haven't started A4 yet, its lessons on functions, loops and lists (lessons 1 to 9) cover the Python used here.
- The curve is for a series RLC circuit driven by 10 V amplitude, plotted against frequency on a log axis.
- Move , and . The peak sits at , where the inductor and capacitor cancel and only limits the current.
- Lower and watch the peak grow taller and narrower.
Predict
At the resonant frequency of a series RLC circuit, how big is the voltage across the capacitor compared with the source?
It can be many times bigger. At resonance the current is , and the capacitor's voltage is that current times its reactance, . With a small , can be 10 or 100. The inductor's voltage is just as big and points the opposite way, so the two cancel in the sum and only the resistor's voltage, equal to the source, remains. Components in a resonant circuit must be rated for that higher voltage.
Name it
Impedances as functions
Each part becomes a one-line function of frequency:
import math
def zR(R, f): return complex(R, 0)
def zL(L, f): return 1j * 2 * math.pi * f * L
def zC(C, f): return 1 / (1j * 2 * math.pi * f * C)
Series impedances add, and parallel ones add as reciprocals:
A function can take any number of arguments with *zs, which collects them into a list.
Dividers and transfer functions
The output across of a divider over is . Its size is the gain, the gain in decibels, and its angle the phase shift. Writing as a Python function of lets you evaluate it anywhere.
Finding a corner
A low-pass corner is where has fallen to of its low-frequency value, dB. For a simple RC it's , but for most filters there's no formula. Bisection finds it: changes sign across the corner, so halve a bracket of frequencies until it's tight. On a log axis, bisect in the logarithm of , taking the midpoint , so a bracket spanning decades closes evenly.
Worked example
Evaluate an RC low-pass with = 1 kΩ and = 100 nF at its corner, as the lab's code will.
Show the solution
- Corner frequency.
- The response there.
- What Python gives. Python gives
(0.5-0.5j): size , or dB, at an angle of .
Answer at the 1591.5 Hz corner, is (0.5-0.5j), a size of 0.7071 ( dB) at .
Worked example
A series circuit of 100 Ω, 10 mH and 2.2 µF is driven by 10 V amplitude at 1 kHz. What current flows?
Show the solution
- Impedance.
- Current.
- Compare with resonance. That's almost 0.1 A, since 1 kHz is close to the resonance at 1.07 kHz, where the current would be = 0.1 A exactly.
Answer a current of A flows, almost 0.1 A.
Worked example
How many bisection steps in narrow a corner bracket from [10 Hz, 1 MHz] to within 0.1%?
Show the solution
- Span of the bracket.
- Target width. Within 0.1% means a ratio of 1.001, which in decades is
- Count the halvings. Each step halves the span in decades, so it takes
Answer 14 steps.
Check
Where should a corner-finding routine land for an RC low-pass with 4.7 kΩ and 10 nF?
Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.
Check
A series RLC circuit (20 Ω, 50 mH, 10 µF) is driven at resonance with 5 V amplitude. What is the amplitude across the capacitor?
Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.
Check
Write zR(R, f), zL(L, f) and zC(C, f), returning complex impedances, and series(*zs) and parallel(*zs), which combine any number of them.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write divider(z1, z2), returning , and rc_lowpass(R, C, f), built from your impedance functions, returning the low-pass's at f. Then db(H) returns and deg(H) 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
Write corner(H, f_lo, f_hi). H is a function of frequency for a low-pass whose starts near 1. Bisect in (midpoint ) for 60 steps to find where , and return that frequency. The test uses it on an RC filter and on a two-stage filter that has no tidy formula.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write rlc_current(V, R, L, C, f), returning the complex current in a series RLC circuit driven by amplitude V at f, and resonance(L, C), returning . Use your impedance functions.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why bisect in instead of when hunting for a filter's corner?
Filter behavior spreads evenly across decades, so a sensible starting bracket spans several of them, perhaps 10 Hz to 1 MHz. Bisecting in itself puts the first midpoint at about 500 kHz, and every early step spends its effort near the top of the range, so a corner at 500 Hz takes many steps just to get the bracket down to the right decade. Bisecting in , with the midpoint , halves the number of decades each time, so the bracket closes at the same relative rate wherever the corner is. The result is the same precision in a predictable number of steps, matching the log axis a Bode plot uses for the same reason.
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.