C3 · Lesson 25 · AC solver lab
A complex-number circuit solver and a frequency sweep in NumPy
A circuit simulator's AC analysis is the nodal solver of C1 with one change: the numbers are complex. Each capacitor and inductor contributes an admittance that depends on frequency, so the simulator builds and solves the matrix once per frequency, hundreds of times across a sweep, to draw a Bode plot. In this lab you'll extend C1's modified nodal analysis to complex admittances, check it against lesson 8's hand calculation, and sweep a filter and a resonant circuit.
- An RC low-pass's gain against frequency, computed the way your solver will: one complex solve per frequency, here done by the course's own engine.
- At the corner, the gain is −3.01 dB; a decade above, about −20 dB.
- Change R or C and watch the whole curve slide: a sweep is many independent solves.
Predict
A solver sweeps a circuit at 200 frequencies. How many times does it build and solve the complex matrix?
200 times. The admittances and change with frequency, so each frequency has its own matrix. Each solve gives magnitude and phase together, since the answer is complex. Real simulators reuse the matrix's pattern of nonzero entries between frequencies to save time.
Name it
Complex admittances
Each part's admittance at angular frequency ω:
| Part | Impedance | Admittance |
|---|---|---|
| resistor | ||
| inductor | ||
| capacitor |
They stamp into the matrix exactly as conductances did in C1.25: on the two diagonal entries, on the two off-diagonal ones. Voltage sources add a row and column each (modified nodal analysis), with their phasor value on the right side.
In NumPy
A = np.zeros((n, n), dtype=complex) # complex entries
y = 1 / (1j * w * L) # 1j is Python's j
v = np.linalg.solve(A, z) # works with complex arrays
abs(v[0]), np.angle(v[0], deg=True) # magnitude and phase
Sweeping
A Bode plot solves at many frequencies, usually spaced evenly on a log scale (np.logspace), and plots and the phase of . With a 1 V source, is itself.
Worked example
What admittances does the solver stamp for 1 mH and 1 µF at 1 kHz?
Show the solution
- Inductor impedance.
- Inductor admittance.
- Capacitor impedance.
- Capacitor admittance.
Answer it stamps S for the inductor and mS for the capacitor.
Worked example
A series RLC (10 Ω, 10 mH, 100 nF) driven by 1 V: what gain does the solver find across the capacitor at resonance?
Show the solution
- Current at resonance. At kHz the current is
- The capacitor's reactance.
- Capacitor voltage.
- Gain. That's a gain of +30 dB, which is .
Answer a gain of 31.6, or +30 dB, which equals the circuit's Q.
Check
What is the magnitude of the impedance the solver uses for 10 mH at 0.1 kHz?
Write units the way you normally would: 1.5 Ω, 1500 mΩ and 0.0015 kΩ are the same answer.
Check
What is the magnitude of the admittance for 10 nF at 0.1 kHz?
Write units the way you normally would: 1.5 mS, 1500 µS and 0.0015 S are the same answer.
Check
An AC MNA matrix has 10 node unknowns and 1 voltage sources. How many complex entries does it have?
Check
A sweep takes 20 points per decade over 2 decades, including both ends. How many solves does it need?
Check
Write impedance(kind, value, f) returning a complex impedance for 'R', 'L' or 'C' at frequency f (Hz), and ac_mna(netlist, n, f) returning the complex node voltages for entries ('R'|'L'|'C', a, b, value), ('I', a, b, phasor) and ('V', a, b, phasor).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write gain_db(netlist, n, out, freqs): for a netlist driven by a 1 V source, return an array of at each frequency in freqs. Your ac_mna is included.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why can a single complex solve give both the magnitude and the phase of a circuit's response?
A phasor packs a sinusoid's amplitude and phase into one complex number, and the circuit equations at one frequency are linear equations among phasors. Solving them with complex arithmetic produces each node voltage as a complex number, whose magnitude is the amplitude and whose angle is the phase relative to the source. Nothing is lost by solving once: the real and imaginary parts carry the in-phase and quadrature parts of the response, which together are exactly the magnitude and phase.
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.