A13 · Lesson 53 · Project: a nonlinear circuit simulator
Nodal analysis, Newton's method and implicit time steps, assembled into a small SPICE
Every method in the second half of this course meets in a circuit simulator. Nodal analysis turns a circuit into a sparse linear system. Diodes make it nonlinear, so each solve becomes a Newton iteration with companion models and step limiting. Capacitors make it dynamic, and because real circuits are stiff, time steps are implicit: backward Euler turns each capacitor into another companion model. In this project you'll build all three layers in a hundred lines of Python and use them to simulate a half-wave rectifier with a smoothing capacitor, the first circuit in most power supplies.
Predict
A 5 V, 50 Hz sine drives a diode through 10 Ω into 100 µF in parallel with 1 kΩ. About what's the capacitor's peak voltage once it has charged?
About 4.1 V. The capacitor charges near each peak through the diode and 10 Ω, so it reaches the 5 V peak less about 0.7 V across the diode and a little across the resistor, then sags through the 1 kΩ load until the next peak. Your simulator gives 4.07 V with 0.64 V of ripple. With 100 Ω in place of 10 Ω the diode conducts so briefly that the capacitor settles near 2.9 V: the source's resistance matters as much as the diode's drop.
Name it
Layer 1: linear nodal analysis
Number the nodes 1 to n, with 0 as ground. A resistor R between a and b adds to and and subtracts it from and (skipping ground's row and column). A current source pushing I into node a adds I to . Solve . A voltage source with a series resistor is entered as its Norton equivalent: a current into its node and R to ground.
Layer 2: diodes by Newton's method
A diode from a to b with voltage carries . Linearized at the current guess, it's a conductance in parallel with a current source (lesson 34). Each iteration stamps the resistors, sources and the diodes' companion models, solves, and repeats until the change is below a tolerance. To keep the exponential from overflowing, limit each iteration's change of every node voltage to 0.1 V.
Layer 3: transients by backward Euler
A capacitor C between a and b, with step h and previous voltage , becomes a conductance in parallel with a current source pushing current toward a (lesson 43). At each time step, update the sources to , run the Newton loop of layer 2 with the capacitors' companion models included, and store the result.
Data used in the tests
A, = 0.02585 V. The two-diode circuit of lesson 34 settles at V, V.
Worked example
Write the nodal matrix for 1 kΩ from node 1 to ground, 2 kΩ from node 1 to node 2, and 3 kΩ from node 2 to ground.
Show the solution
- Stamp each resistor. Each resistor contributes to the diagonal of each node it touches and to the off-diagonals between them; ground's row and column are left out.
- The matrix.
Answer the diagonal entries are and S, and both off-diagonal entries are S.
Worked example
A capacitor of 100 µF at 4 V is simulated with h = 0.1 ms. What's its companion model?
Show the solution
- The conductance.
- The current source. In parallel sits a source of into the node at its positive end.
- Check. With nothing else attached, the next solve gives back 4 V, as it should for an isolated capacitor.
Answer a 1 S conductance in parallel with a 4 A current source into the capacitor's positive node.
Check
Step 1. Write solve_linear(n, resistors, sources): resistors is a list of (a, b, R), sources a list of (node, current_in), node 0 is ground. Return the node voltages as a NumPy array v with v[0] for node 1.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 2. Write solve_dc(n, resistors, sources, diodes) with Newton's method: diodes is a list of (a, b) (anode, cathode). Start from all zeros, limit each node's change to 0.1 V per iteration, stop when the largest change is below V. Return (v, iterations). stamp_r is included.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Step 3. Write rectifier(h, t_end): a 5 V, 50 Hz sine through 10 Ω (as its Norton equivalent into node 1), a diode from node 1 to node 2, and 100 µF and 1 kΩ from node 2 to ground. Use backward Euler with step h, starting from rest, and the Newton loop of step 2 at every step (starting from the previous solution). Return the lists ts and v2s (node 2's voltage). solve_dc is included and accepts v= (a start) and extra= (a function that stamps more into G and i).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
What conductance stands for a 100 µF capacitor with a 20 µs backward-Euler step?
Write units the way you normally would: 1.5 S, 1500 mS and 0.0015 kS are the same answer.
Check
A current of 10 mA flows into a node with 1000 Ω and 2000 Ω to ground. What's the node voltage?
Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.
Explain why
Why do circuit simulators solve each time step with Newton's method on a linear "companion" circuit?
Stiff circuits need implicit time steps, and an implicit step requires solving the circuit's equations at the new time, which are nonlinear because of diodes and transistors. Newton's method linearizes those equations at a guess. In circuit terms, linearizing a device replaces it by a conductance and a current source, and the backward-Euler formula replaces each capacitor by a conductance C/h and a current source carrying its previous charge. Every element then looks like a resistor or a source, so each Newton iteration is just nodal analysis of a linear circuit: one sparse linear solve. Repeating that until the voltages stop changing gives the solution at the new time, and the next time step starts from it.
Practice
Keep practicing
5 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.