Sub topic 12
Building the Conductance Matrix in Code
This chapter assumes you can write KCL equations at each node, put them in the form G v = i, and read basic NumPy code. Here you will automate the assembly of G and i, so a computer builds the matrix for any resistor network.
Why Build the Matrix by Rule
Writing KCL by hand works for two or three nodes. It becomes error-prone at ten. The matrix G follows a pattern, so a program can fill it in one element at a time. This is called stamping: each component leaves a fixed "stamp" on the matrix.
Numbering the nodes makes this possible. Node 0 is ground. Nodes 1 to n are the unknowns. Node k maps to row and column k - 1 of the matrix, because ground has no equation.
The Stamping Rules
A resistor R between nodes a and b has conductance g = 1/R. It changes G as follows:
| Entry | Change | Condition |
|---|---|---|
| G[a, a] | add g | a is not ground |
| G[b, b] | add g | b is not ground |
| G[a, b] and G[b, a] | subtract g | neither node is ground |
A resistor to ground touches only one diagonal entry. A current source that pushes amps into node to adds amps to that node's entry in i. It subtracts amps from the node it draws from, if that node is not ground.
The Code
This function applies the rules. Each resistor is a tuple (node_a, node_b, ohms). Each source is (from_node, to_node, amps).
import numpy as np
def build_nodal(n, resistors, current_sources):
"""Node 0 is ground; nodes 1..n are unknowns."""
G = np.zeros((n, n))
i = np.zeros(n)
for a, b, r in resistors:
g = 1.0 / r
for node in (a, b):
if node > 0:
G[node - 1, node - 1] += g
if a > 0 and b > 0:
G[a - 1, b - 1] -= g
G[b - 1, a - 1] -= g
for frm, to, amps in current_sources:
if to > 0:
i[to - 1] += amps
if frm > 0:
i[frm - 1] -= amps
return G, i
# 2 A into node 1; 4 ohm to ground, 2 ohm between nodes, 6 ohm to ground.
G, i = build_nodal(2, [(1, 0, 4), (1, 2, 2), (2, 0, 6)], [(0, 1, 2)])
print(G)
print(np.linalg.solve(G, i)) # [5.33333333 4. ]The printed G is [[0.75, -0.5], [-0.5, 0.6667]]. You can check it by hand. G[0, 0] is 1/4 + 1/2 = 0.75, the sum of the conductances at node 1. The off-diagonal -0.5 is the negative conductance of the 2 ohm resistor shared by nodes 1 and 2.
Checks Worth Adding
Building G separately from solving it lets you test the matrix before you trust the answer.
- G should be symmetric for resistor-only circuits.
np.allclose(G, G.T)catches a stamping mistake. - Each diagonal entry should be positive and at least as large as the sum of the absolute values of the off-diagonals in its row, when every node has a path to ground.
- After solving, compute
G @ v - i. It should be close to zero. This is the KCL check from the previous chapter, done in one line.
Limits of This Version
The function handles resistors and independent current sources only. Grounded voltage sources, supernodes, and dependent sources need extra rows or modified entries. Those stamps follow the same idea, but they change the structure of the matrix, so this chapter leaves them out.
Exercise
In build_nodal, a 5 ohm resistor connects node 2 to ground (node 0). Which entries of G change, and by how much?
Show answer
Only G[1, 1] changes, by +0.2 (that is 1/5). Node 0 is ground, so it has no row or column, and the off-diagonal rule does not apply.
Exercise
A 3 A source is listed as (2, 0, 3) in currentsources. What does buildnodal do with it?
Show answer
The source pushes current into ground, so to is 0 and nothing is added. It draws 3 A from node 2, so i[1] is reduced by 3.