A7 · Lesson 21 · Lab: circuit graphs in code
Kirchhoff's laws as matrices you can compute with
Lessons 19 and 20 turned a circuit's wiring into an incidence matrix and read Kirchhoff's laws off its four subspaces. In this lab you'll compute with that structure: build the incidence matrix from a list of branches, find a basis of loop currents from the SVD, assemble the conductance matrix as and solve the circuit, then check Tellegen's theorem, that the power the source delivers equals the power every resistor takes. The circuit is the one below: four nodes counting ground, five resistors and a current source.
- A 1 mA source feeds node n1 of a two-section ladder. The meters read the three node voltages.
- Change , the resistor between n1 and n2. The voltages beyond it fall as it grows, and n1's rises.
- With every part at its starting value, the meters should match what your code computes: 0.636, 0.273 and 0.182 V.
Predict
This circuit has 4 nodes counting ground and 5 resistors. How many independent loop currents does it have?
Two: . They're the two meshes, one through , and , the other through , and . The current source is left out of the graph and enters as a current injected at n1; it doesn't add a loop.
Name it
The incidence matrix in code
Number the nodes from 0, with ground last, and list each branch as a pair (from, to). The incidence matrix has one row per branch, with in the from column and in the to column. For a connected circuit, and the rank is .
Null spaces from the SVD
The rows of in an SVD whose singular values are (numerically) zero span the null space. In NumPy:
u, s, vt = np.linalg.svd(M)
r = np.sum(s > 1e-10 * s[0])
basis = vt[r:].T # columns span the null space of M
The null space of holds the branch-current patterns that satisfy KCL with no sources, the loop currents.
Assembling and solving
Drop the ground column to get . With the branch conductances on the diagonal of , the conductance matrix is , and gives the node voltages for injected currents . This is exactly what a simulator does, one branch at a time.
Tellegen's theorem
With ground at 0 V appended to , the branch voltages are and the branch currents . Then : the power taken by the branches equals the power the sources put in.
Worked example
Write the incidence matrix of the lab circuit.
Show the solution
- Number the nodes and branches. With nodes n1, n2, n3 and ground numbered 0 to 3, and branches (0→1), (1→2), (0→3), (1→3) and (2→3):
- Row sums. Each row sums to zero, so .
- Rank. The first three columns are independent, so the rank is 3.
Answer is the 5 × 4 matrix above, one row per branch with at the node it leaves and at the node it enters, and its rank is 3.
Worked example
Solve the lab circuit by hand from .
Show the solution
- Conductance matrix. In millisiemens, , and each diagonal entry being the total conductance at a node.
- Determinant.
- Cramer's rule. With 1 mA into n1:
- Source power.
Answer = 0.636 V, = 0.273 V and = 0.182 V, and the source delivers 0.636 mW.
Check
A connected circuit graph has 6 nodes and 11 branches. How many columns does a basis of the null space of have?
Check
A node has branches of 1, 0.5 and 4 mS to other nodes. What is its diagonal entry in ?
Write units the way you normally would: 1.5 mS, 1500 µS and 0.0015 S are the same answer.
Check
Write incidence(edges, n) returning the incidence matrix. Build it for the lab circuit and set rank with np.linalg.matrix_rank.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write null_space(M) from the SVD, and set loops = null_space(A.T) for the lab circuit (its incidence matrix is in the starter).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Assemble with = diag(1, 1, 1, 1, 0.5) mS, solve for 1 mA into node 0, and set v to the three node voltages.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Check Tellegen's theorem: append 0 V for ground to v, form the branch voltages e = A @ u and currents w = C @ e, and set p_branches = e @ w and p_source to the power the 1 mA source delivers.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why is the incidence matrix's rank always one less than the number of nodes for a connected circuit?
Each row of measures a voltage difference across a branch, so raising every node's voltage by the same amount changes no row: the constant vector is always in the null space. In a connected circuit, nothing else is. If , every branch has zero voltage across it, so neighboring nodes have equal voltages, and following branches from any node reaches all the others, forcing every voltage to be the same. The null space is exactly the line of constant vectors, dimension 1, so the rank is . Grounding one node removes that freedom, which is why the reduced matrix is invertible.
Practice
Keep practicing
4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.