Linear Systems: The Algebra Behind Nodal Analysis
Every systematic circuit method in this course ends the same way: a set of linear equations, one per node or per mesh, that must be solved together. A three-node circuit is a pencil exercise; a forty-node filter board or a power grid with thousands of buses is not. This lesson shows what those equations look like as a matrix, how they are solved by elimination, when the answer can be trusted, and what the inverse matrix means physically. The general theory is in the Linear Algebra course: Gaussian Elimination, Cramer's Rule, Matrix Inverses and, closest to this lesson, Resistor Networks. Numerical behaviour is in LU Factorization and Matrix Conditioning.
From a circuit to
Kirchhoff's current law at each node, with Ohm's law for each resistor, is linear in the node voltages. Collected into matrix form it reads
where is the conductance matrix (siemens), the unknown node voltages and the source currents injected into the nodes. For the ladder below, with conductances in mS (since ):
Row 2 says: node 2 touches , and () and is coupled to nodes 1 and 3 through each. The structure is typical of circuits:
- Symmetric, , for resistors and independent sources (Node-Voltage Analysis).
- Sparse: a node connects to a handful of neighbours, so most entries are zero. Real simulators store only the nonzeros.
- Diagonally dominant: each diagonal entry is at least the sum of the magnitudes of the off-diagonal entries in its row (strictly larger wherever a resistor goes to ground). That guarantees a unique solution and makes elimination stable without row swaps.
Solving it: elimination, by hand once
Gaussian elimination turns the system into a triangular one and then back-substitutes. On the augmented matrix:
Back-substitution: , , . Each rung of this ladder halves the voltage.
Cramer's rule () is handy for systems and symbolic answers, but its cost grows factorially if done by cofactors; elimination costs about operations, and sparse elimination far less. SPICE uses sparse LU factorization: it factors once and reuses and for every right-hand side.
What means: transfer resistances
The solution is . Column of is the set of node voltages produced by injected at node alone, so its entries have units of ohms. They are transfer resistances: . The diagonal entry is the resistance seen between node and ground (the Thevenin resistance there). Because is symmetric, so is : injecting current at node 1 and measuring at node 3 gives the same ratio as the other way round. This is the reciprocity theorem, which returns for two-port networks.
Worked example: the ladder
Given: the ladder above, into node 1.
Find: the node voltages, , the resistance seen from node 1 to ground, and the transfer resistance from node 1 to node 3.
Solve: elimination gave , , . The determinant is the product of the pivots: . The resistance at node 1 is , and the transfer resistance is .
Check: by series and parallel reduction, node 1 sees . Same answer, by a different route.
In Python
import numpy as np
np.set_printoptions(precision=4, suppress=True)
G = np.array([[ 2.0, -1.0, 0.0],
[-1.0, 2.5, -1.0],
[ 0.0, -1.0, 2.0]]) # mS
i = np.array([6.0, 0.0, 0.0]) # mA
v = np.linalg.solve(G, i) # LU with partial pivoting
print("v (V):", v)
print("det G = %.4f mS^3" % np.linalg.det(G))
print("symmetric:", np.allclose(G, G.T))
Rt = np.linalg.inv(G) # kOhm: transfer resistances
print("G^-1 (kOhm):\n", Rt)
# several source patterns at once: one column per right-hand side
I = np.array([[6.0, 0.0], [0.0, 0.0], [0.0, 6.0]])
print("two cases:\n", np.linalg.solve(G, I))
# conditioning: a 1 ohm link between two nodes that each have 1 Mohm to ground
Gbad = np.array([[1 + 1e-6, -1.0], [-1.0, 1 + 1e-6]])
print("cond(G) = %.3g, cond(Gbad) = %.3g" % (np.linalg.cond(G), np.linalg.cond(Gbad)))v (V): [4. 2. 1.]
det G = 6.0000 mS^3
symmetric: True
G^-1 (kOhm):
[[0.6667 0.3333 0.1667]
[0.3333 0.6667 0.3333]
[0.1667 0.3333 0.6667]]
two cases:
[[4. 1.]
[2. 2.]
[1. 4.]]
cond(G) = 4.53, cond(Gbad) = 2e+06
The second column of "two cases" is injected at node 3 instead. The ladder is symmetric end to end (, ), so the answer is the first column upside down.
When to distrust the answer: conditioning
The condition number estimates how much relative error in the data (resistor values, rounding) can grow in the solution. Rule of thumb: you lose about of the 16 significant digits of double precision. The ladder has ; the second matrix, a link between two nodes that each have to ground, has . That is still fine in double precision (about 10 good digits remain), but it explains two things you will meet: simulators complain about "nearly singular" matrices when a node is connected only through huge resistances, and SPICE uses a tiny conductance gmin (default ), placed across semiconductor junctions and, during "gmin stepping", from nodes to ground, so that the matrix stays solvable. Physically, ill-conditioning means a quantity that hardly depends on the inputs you control: here the difference across the link.
Guided lab: the ladder on the bench
Hand calculation. With injected at node 1: , , . The source sees and delivers .
Simulation. The netlist lab solves the same matrix live. Change : every node voltage moves, because every row of is coupled to its neighbours.
At the defaults it shows , and .
What you would measure. Use a bench supply in constant-current mode set to , with its voltage limit at , and 1 % resistors (two in series make the if you have no single part). Expect , and , each within about 1.5 % (the resistors), scaled by the current you actually get: many supplies specify the current setting as a percentage plus a fixed offset of several milliamperes (for example ), which is large at , so measure the current with a DMM in series and multiply the expected voltages by ; check your supply's manual. If a supply has no constant-current mode, through a resistor into node 1 is not a substitute: Source Transformation shows it is a source with in parallel, which loads node 1 and gives , and instead (every voltage scaled by ).
Hazards and safe setup: voltages stay below and the largest dissipation is (in ); set the supply's voltage limit before switching on, since an open circuit on a current source drives the voltage to the limit.
Pitfalls
- Inverting to solve.
np.linalg.solve(G, i)is faster and more accurate thannp.linalg.inv(G) @ i; use the inverse only when you want the transfer resistances themselves. - A singular matrix. It almost always means a circuit error: a floating node, a loop of ideal voltage sources, a node connected only through current sources.
- Mixed units in one matrix. Keep conductances in one unit (S or mS) and currents in the matching one (A or mA).
- Trusting digits blindly. With a large condition number, check the residual and think about which quantity is sensitive.
Practice
- For the ladder, find when is injected at node 2 only, using the column of .
- Solve by elimination: (mS, mA).
- A matrix has . Roughly how many significant digits of a double-precision solution can you trust?
Answer
- Column 2 of is ; times gives .
- : , so and .
- About digits.
Test yourself
A circuit of resistors and independent sources gives a conductance matrix that is…
- always singular
- symmetric
- upper triangular
- diagonal
Entry of has units of…
- siemens
- volts
- ohms: it is the voltage at node per ampere injected at node
- amperes
Why does SPICE's gmin stepping add a tiny conductance from nodes to ground?
- to model leakage of real resistors
- to keep the matrix nonsingular when a node has no other path to ground
- to speed up AC analysis
- to make the matrix symmetric
Which is the preferred way to solve numerically?
np.linalg.inv(G) @ inp.linalg.solve(G, i)- Cramer's rule with cofactors
np.linalg.det(G)