A7 · Lesson 12 · Solving Ax = b
Writing a circuit as a matrix and letting the computer solve it
A resistor ladder with three junctions is about as far as most people go by hand. Real circuits have hundreds of nodes, and their equations are solved the same way every time: write the conductance matrix by inspection, write the vector of injected currents, and hand both to a solver. This lesson is that recipe, in code.
Predict
To solve in code, which is better: np.linalg.solve(G, i) or np.linalg.inv(G) @ i?
solve. It runs elimination once on this right-hand side, about operations, while forming the full inverse costs about three times as much and loses more accuracy to rounding. Numerical code almost never computes an inverse explicitly; "multiply by " on paper means "solve with " in code.
Name it
The recipe for a resistor circuit
- Pick a ground node and number the others 1 to n.
- Build by inspection: each diagonal entry is the sum of the conductances touching node ; each off-diagonal entry is minus the conductance joining and (zero if they aren't joined). is symmetric.
- Build : the current pushed into each node by current sources. A voltage source in series with a resistor can be swapped for a current source in parallel with (A source transformation, C1), which is how the ladder below gets its right-hand side.
- Solve for the node voltages.
- Check: compute the residual , which should be zero to rounding error, and branch currents from voltage differences.
For a ladder fed by through , with shunt resistors , , to ground at nodes 1, 2, 3 and series resistors (1 to 2) and (2 to 3):
The zeros in the corners are typical: each node touches only a few others, so real conductance matrices are mostly zeros (sparse), and simulators store and solve them in ways that skip the zeros.
Worked example
A two-node circuit has mS and mA. Find .
Show the solution
- Determinant.
- Inverse.
- Multiply.
- In NumPy.
np.linalg.solve(G, i)with in mS and in mA gives the same numbers in volts.
Answer V.
Worked example
For a 1000 × 1000 system, how does solve compare with inv(G) @ i?
Show the solution
- Cost of
solve. It factors once, about - Cost of the inverse. Forming the inverse takes about , three times as much, and then a multiplication on top.
- Accuracy. The inverse is also less accurate.
Answer solve wins on both counts: about a third of the work, and a more accurate answer.
Worked example
A three-node ladder has mS, with 1 mA injected at node 1. Find the node voltages.
Show the solution
- Eliminate below the first pivot. Solving , row 2 + ½ row 1 gives
- Eliminate below the second. Row 3 + ⅔ of that gives
- Back-substitute. = 0.25 V, = 0.5 V and = 0.75 V.
- The pattern. The voltage falls by a quarter volt per node along the ladder. In Python,
np.linalg.solve(G, [1, 0, 0])gives the same three numbers.
Answer = 0.75 V, = 0.5 V and = 0.25 V.
Check
Build G for the ladder above with all six resistors equal to R = 2.2 kΩ and = 10 V, build i, and solve for v, 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
Now with all six resistors different (they're defined as R1 to R6) and = 10 V: solve for v, then set I3 to the current through from node 1 to node 2, and resid to the largest absolute entry of .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Use the 2 × 2 inverse to solve for .
Check
A circuit has mS, with 1 mA injected at node 1.
(a)
What is ?
Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.
(b)
What is ?
Check
Node 1 has mS to ground and 1 mS to node 2; node 2 has 1 mS to ground; 1 mA flows into node 1.
Step 1
. What is ?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is , in volts?
Check
The three-node ladder above, mS, has kΩ. With 2 mA injected at node 2 and nothing else, what is the voltage at node 3?
Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.
Explain why
Why does a circuit with n nodes (besides ground) give an n × n conductance matrix?
Each non-ground node has one unknown voltage and one current-law equation: the currents leaving it add to zero. Each equation involves that node's voltage and the voltages of the nodes it connects to, so stacking the n equations gives n rows, and each row has one column per unknown voltage: an n × n matrix times the vector of n node voltages.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.