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

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 ATCA 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 R1, 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?

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 −1 in the from column and +1 in the to column. For a connected circuit, A𝟏=𝟎 and the rank is n−1.

Null spaces from the SVD

The rows of VT 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 AT holds the branch-current patterns that satisfy KCL with no sources, the loop currents.

Assembling and solving

Drop the ground column to get Ar. With the branch conductances on the diagonal of C, the conductance matrix is G=ArTCAr, and G𝐯=𝐟 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 𝐞=A𝐮 and the branch currents 𝐰=C𝐞. Then 𝐞⋅𝐰=𝐮TATCA𝐮=𝐯T𝐟: 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
  1. Number the nodes and branches. With nodes n1, n2, n3 and ground numbered 0 to 3, and branches R1 (0→1), R2 (1→2), R3 (0→3), R4 (1→3) and R5 (2→3): A=[−11000−110−10010−10100−11]
  2. Row sums. Each row sums to zero, so A𝟏=𝟎.
  3. Rank. The first three columns are independent, so the rank is 3.

Answer A is the 5 × 4 matrix above, one row per branch with −1 at the node it leaves and +1 at the node it enters, and its rank is 3.

Worked example

Solve the lab circuit by hand from G=ArTCAr.

Show the solution
  1. Conductance matrix. In millisiemens, C=diag(1,1,1,1,0.5), and G=[2−10−13−10−11.5] each diagonal entry being the total conductance at a node.
  2. Determinant. det⁡G=2(4.5−1)−1(1.5)=5.5
  3. Cramer's rule. With 1 mA into n1: v1=3.55.5=0.636Vv2=1.55.5=0.273Vv3=15.5=0.182V
  4. Source power. 0.636×1=0.636mW

Answer v1 = 0.636 V, v2 = 0.273 V and v3 = 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 AT have?

Check

A node has branches of 1, 0.5 and 4 mS to other nodes. What is its diagonal entry in G=ATCA?

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 b×n 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 G=ArTCAr with C = 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?

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.