C1 · Lesson 25 · Nodal solver lab
Build a circuit solver in NumPy from a netlist
Every circuit simulator, from SPICE to the sims in this course, does what you did by hand in lesson 9: write KCL at every node and solve the linear equations. The difference is that it builds the equations automatically from a list of parts. Each resistor adds the same small pattern of four numbers to a matrix, a "stamp", wherever its two nodes are. In this lab you'll write that stamping loop, handle current sources and then voltage sources, and use the solver to find a resistance that no series-parallel trick can.
- A divider driving a load from a 12 V source, solved by the course's own circuit engine, which builds and solves exactly the matrix you'll build.
- Every slider change rebuilds the conductance matrix and solves it again, in microseconds.
- Predict the output for a 20 kΩ load before checking the meter.
Predict
Nodal analysis of a circuit with 50 nodes (plus ground) and only resistors and current sources. How big is its conductance matrix?
50 × 50: one row (one KCL equation) and one column (one unknown voltage) per node other than ground, whose voltage is fixed at zero. Most entries are zero, since each node connects to only a few others, so large simulators store only the nonzero ones.
Name it
The stamp
Number the nodes 1 to , with 0 for ground. KCL at every node is (lesson 9). A resistor of conductance between nodes and adds to :
| column | column | |
|---|---|---|
| row | ||
| row |
leaving out any row or column for ground. A current source of flowing from through the source into subtracts from and adds it to . Add every part's stamp and solve.
Voltage sources: modified nodal analysis
A voltage source fixes a voltage difference but its current is unknown, so it needs a new unknown, its current, and a new equation, . Modified nodal analysis (MNA) grows the matrix by one row and column per source:
where column of has +1 in row and −1 in row for source . Each comes out as the current flowing into the source's + terminal from the circuit; a negative value means the source is delivering power. SPICE builds exactly this.
Equivalent resistance
To find the resistance between two nodes, inject 1 A in at one and out at the other and solve: the voltage difference, in volts, is the resistance in ohms. This works for any network, bridges included (lesson 11).
In NumPy
import numpy as np
G = np.zeros((n, n)) # n × n of zeros
G[a - 1, b - 1] += -g # node a is row a - 1: Python counts from 0
v = np.linalg.solve(G, i) # solve G v = iWorked example
Write the conductance matrix for a 2 mA current source into node 1, with 1 kΩ from node 1 to ground, 1 kΩ from node 1 to node 2, and 2 kΩ from node 2 to ground.
Show the solution
- Node 1's row. In mS, node 1 has on the diagonal and to node 2.
- Node 2's row. Node 2 has on the diagonal.
- The system. In mA:
- Solve.
Answer the conductance matrix is mS, giving = 1.5 V and = 1.0 V.
Worked example
Lesson 11's bridge, driven by 10 V at its input, solved by MNA. What are the node voltages and the source's current?
Show the solution
- Node voltages. With nodes 1 (input), 2 () and 3 (), the solver gives
- Source current. The solver gives −0.34426 A: 0.344 A flows out of its + terminal.
- Input resistance. matching the delta-wye calculation.
Answer the nodes sit at 10 V, 7.869 V and 7.377 V, and the source current is −0.344 A, which makes the input resistance 29.05 Ω.
Check
A circuit has 50 nodes besides ground. How many entries does its full conductance matrix have?
Check
A 2.2 kΩ resistor joins nodes 2 and 3. What does it add to the entry in row 2, column 3?
Write units the way you normally would: 1.5 mS, 1500 µS and 0.0015 S are the same answer.
Check
A circuit has 10 non-ground nodes and 1 voltage sources. How many unknowns does modified nodal analysis solve for?
Check
A node connects to ground through 1 mS and 0.5 mS, and a 3 mA source feeds it. What is its voltage?
Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.
Check
Write nodal(netlist, n): each entry is ('R', a, b, ohms) or ('I', a, b, amps) (current flowing from node a through the source into node b), with node 0 as ground. Build and and return the voltages of nodes 1 to n as an array.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write mna(netlist, n), accepting ('V', a, b, volts) (with a the + terminal) as well as resistors and current sources. Return (v, j): the node voltages and the voltage sources' currents, in the order the sources appear.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write req(netlist, n, a, b=0): the resistance between nodes a and b of a resistor network, found by injecting 1 A into a and out of b with your nodal.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does modified nodal analysis add an unknown for each voltage source, instead of just writing KCL at every node?
KCL at a node adds up the currents leaving it, and for a resistor that current is easy to write from the node voltages, . A voltage source has no such formula: its current can be anything, set by the rest of the circuit, while its voltage is fixed. So its current has to be treated as an extra unknown, appearing in the KCL equations at both of its nodes, and the source's own equation, , is added to make up the count. That way every kind of part fits into one matrix, with no special handling for floating sources.
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.