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

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?

Name it

Complex admittances

Each part's admittance at angular frequency ω:

PartImpedanceAdmittance
resistorR1/R
inductorjωL1jωL
capacitor1jωCjωC

They stamp into the matrix exactly as conductances did in C1.25: +y on the two diagonal entries, −y 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 20log10⁡|H| and the phase of H=Vout/Vin. With a 1 V source, Vout is H itself.

Worked example

What admittances does the solver stamp for 1 mH and 1 µF at 1 kHz?

Show the solution
  1. Inductor impedance. ZL=j2π×1000×10−3=j6.28Ω
  2. Inductor admittance. yL=1j6.28=−j0.159S
  3. Capacitor impedance. ZC=1j2π×1000×10−6=−j159Ω
  4. Capacitor admittance. yC=1−j159=j6.28mS

Answer it stamps yL=−j0.159 S for the inductor and yC=j6.28 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
  1. Current at resonance. At f0=5.03 kHz the current is I=1V10Ω=0.1A
  2. The capacitor's reactance. XC=L/C=316Ω
  3. Capacitor voltage. |VC|=0.1×316=31.6V
  4. Gain. That's a gain of +30 dB, which is Q=31.6.

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 20log10⁡|vout| 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?

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.