A438 of 56
0 XP
  1. Hook
  2. Predict
  3. Name it
  4. Check
  5. Explain

A4 · Lesson 38 · Building circuit equations in code

Let the computer write the matrix too

Lesson 37 solved a two-node circuit by writing its matrix out by hand. That works for two nodes and fails quietly for twenty: one wrong sign in a hand-built matrix gives a plausible wrong answer. Every circuit simulator since SPICE builds the matrix itself, one part at a time. Each part adds a small pattern of numbers, its stamp, to the rows and columns of the nodes it touches. Write the stamps once and any netlist becomes a matrix equation.

Predict

A 1 kΩ resistor connects node 1 to node 2. How many entries of the nodal conductance matrix G does it change?

Name it

Nodal equations

Number the nodes 1 to n and call ground node 0, the reference everything is measured from. Kirchhoff's current law at each node says the currents leaving through the parts equal the current injected by sources:

G𝐯=𝐢

𝐯 holds the node voltages, 𝐢 the injected currents, and G the conductances.

Stamps

  • A resistor R from node a to node b: add g=1/R to Gaa and Gbb, and −g to Gab and Gba. Skip any entry that involves node 0.
  • A current source pushing I into node a (and out of node b): add I to ia and −I to ib.
import numpy as np

def stamp_R(G, a, b, R):
    g = 1 / R
    if a: G[a - 1, a - 1] += g
    if b: G[b - 1, b - 1] += g
    if a and b:
        G[a - 1, b - 1] -= g
        G[b - 1, a - 1] -= g

Python's indexes start at 0, so node k lives in row k−1; ground has no row at all.

Voltage sources: one extra unknown

A voltage source fixes a voltage difference but its current is whatever the circuit needs, so its current becomes an extra unknown. For a source V from node a (+) to ground, enlarge the matrix by one row and column:

  • the new column puts the source's current, flowing into its + terminal, into node a's KCL equation: a 1 at row a;
  • the new row says va=V: a 1 at column a, and V on the right.

This is modified nodal analysis, the form SPICE solves. The solved source current comes out negative when the source is delivering power, since current flows out of its + terminal.

Why ground has no row

Voltages only mean something relative to a reference. Keep a row for every node including ground, and adding the same constant to every voltage changes nothing, so the matrix has no unique solution: it's singular. Removing ground's row and column pins the reference at 0 V.

Worked example

A 1 mA source feeds node 1. R1 = 1 kΩ goes from node 1 to ground, R2 = 2 kΩ from node 1 to node 2, and R3 = 3 kΩ from node 2 to ground. Find the node voltages.

Show the solution
  1. Stamp the resistors and solve.
G = np.zeros((2, 2))
stamp_R(G, 1, 0, 1e3)
stamp_R(G, 1, 2, 2e3)
stamp_R(G, 2, 0, 3e3)
i = np.array([1e-3, 0])
print(np.linalg.solve(G, i))    # [0.8333 0.5]
  1. The matrix. G=[1.5−0.5−0.50.8333]mS giving v1 = 0.833 V and v2 = 0.5 V.
  2. Check node 1 by hand. R2 and R3 in series are 5 kΩ, in parallel with 1 kΩ that's 833 Ω, and 1 mA through it is 0.833 V.
  3. Check node 2. R3 then gets 3/5 of that, 0.5 V.

Answer v1 = 0.833 V and v2 = 0.5 V.

Worked example

A 5 V source drives node 1. R1 = 1 kΩ joins nodes 1 and 2, R2 = 2 kΩ goes from node 2 to ground, R3 = 2 kΩ joins nodes 2 and 3, and R4 = 1 kΩ goes from node 3 to ground. Find the voltages and the source current.

Show the solution
  1. Stamp the resistors into the big matrix. Slicing A[:n, :n] gives a view into the same array, so the resistor stamps land in the big matrix directly.
  2. Add the source and solve. Its current is the extra unknown.
n = 3
A = np.zeros((n + 1, n + 1))
G = A[:n, :n]                    # a view: stamps go straight into A
for a, b, R in [(1, 2, 1e3), (2, 0, 2e3), (2, 3, 2e3), (3, 0, 1e3)]:
    stamp_R(G, a, b, R)
A[0, n] = A[n, 0] = 1            # source at node 1
rhs = np.zeros(n + 1)
rhs[n] = 5
x = np.linalg.solve(A, rhs)
print(x)                         # [5. 2.7273 0.9091 -0.0022727]
  1. Read the solution. The voltages are 5, 2.727 and 0.909 V, and the source current is −2.27 mA: 2.27 mA flowing out of its + terminal.
  2. Check by hand. R2 in parallel with R3 + R4 is 2 kΩ ∥ 3 kΩ = 1.2 kΩ, so the source sees 2.2 kΩ and supplies 5V2.2kΩ=2.27mA

Answer the nodes sit at 5, 2.727 and 0.909 V, and the source supplies 2.27 mA.

Worked example

A Wheatstone bridge has 1 kΩ in three arms and 1.1 kΩ in the fourth (the lower right), fed from 10 V, with a 100 Ω meter between the two midpoints. What current flows through the meter?

Show the solution
  1. Number the nodes and stamp. Number the top node 1, the left midpoint 2 and the right midpoint 3, and stamp R1 (1–2, 1 kΩ), R2 (2–0, 1 kΩ), R3 (1–3, 1 kΩ), R4 (3–0, 1.1 kΩ) and the meter (2–3, 100 Ω), with the 10 V source at node 1.
  2. Solve. The solution gives v2 = 5.1059 V and v3 = 5.1271 V.
  3. Find the meter current. i=v2−v3100Ω=−0.212mA that is, 0.212 mA from the right midpoint to the left.
  4. Check with Thevenin's theorem. With the meter removed the midpoints sit at 5 V and 10×1.1/2.1 = 5.238 V, 0.238 V apart, behind 500+523.8 = 1023.8 Ω.
  5. Add the meter back. With its 100 Ω, the current is i=0.238V1123.8Ω=0.212mA
  6. What it took. The program needed no cleverness: five stamps and one solve, for a circuit with no series or parallel shortcut.

Answer 0.212 mA flows through the meter, from the right midpoint to the left.

Check

Write stamp_R(G, a, b, R) that adds a resistor between nodes a and b to the conductance matrix G in place, with node 0 as ground (no row) and node k in row k−1. Then set G for the starter's random netlist of resistors on 4 nodes.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

A current source pushes I = 2 mA into node 1. R1 = 1200 Ω goes from node 1 to ground, R2 = 4700 Ω from node 1 to node 2, and R3 = 8200 Ω from node 2 to ground. Build G and 𝐢 with stamps and set v to the solved node voltages.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Build the bridge from the third example with modified nodal analysis: V = 9 V at node 1, 1 kΩ from 1 to 2, from 2 to ground and from 1 to 3, d = 0.9 kΩ from 3 to ground, and a meter of Rg = 10 Ω from 2 to 3. Set x to the solution vector (three voltages, then the source current) and Im to the meter current from node 2 to node 3.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

A source Vs = 5 V feeds a divider, R1 = 1000 Ω on top and R2 = 4700 Ω below, and a load RL hangs across R2. Write load_power(RL) that builds and solves the circuit with modified nodal analysis and returns the power in RL. Then search RL over np.logspace(1, 6, 2001) and set RL_best to the value that maximizes the power, and Rth to the divider's Thevenin resistance, R1 in parallel with R2.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

For the starter's random resistor network on 3 nodes with a 1 mA source into node 1, solve for v, then check Kirchhoff's current law yourself: set kcl to an array holding, for each node, the total current leaving it through the resistors minus the current injected there. It should be zero to rounding error.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Node 1 connects to ground through 2.2 kΩ, to node 2 through 1.2 kΩ and to node 3 through 4.7 kΩ. What is the diagonal entry G11 of the conductance matrix?

Write units the way you normally would: 1.5 S, 1500 mS and 0.0015 kS are the same answer.

Check

Why does nodal analysis leave the ground node out of the matrix?

Explain why

A resistor network's conductance matrix is always symmetric. Explain why, from the resistor stamp.

Practice

Keep practicing

8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.