A7 · Lesson 13 · Lab: elimination and LU in code
Writing the solver that circuit simulators are built on
np.linalg.solve hides a few dozen lines of code that every circuit simulator, finite-element program and control design tool depends on. In this lab you'll write those lines yourself: elimination with row exchanges, the LU factorization, and forward and back substitution. Then you'll use them the way simulators do, factoring once and solving for many source values, and finally solve a twenty-node ladder whose voltages fall by the same ratio at every node.
- The curves count multiply-adds for solving an -node circuit with different right-hand sides.
- The upper curve factors from scratch each time: . The lower one factors once and reuses and : .
- Raise . The gap widens, because factoring grows like and each extra solve only like .
Predict
A 500-node circuit must be solved for 1000 different source values. How much faster is factoring once than factoring every time?
About 140 times. Factoring costs multiply-adds, and each solve with the factors costs . Every time: . Once: . A frequency sweep or a transient run reuses the factors the same way whenever the matrix doesn't change.
Name it
Elimination with partial pivoting
For each column :
- find the row at or below with the largest , and swap it into row ;
- for each row below, subtract times row .
Choosing the largest pivot keeps every multiplier at most 1 in size, which stops rounding errors from being magnified. Without it, a tiny pivot can wreck an answer even when the matrix is well conditioned.
LU and substitution
Storing the multipliers in a unit lower-triangular gives (or with row exchanges). Then is two triangular solves:
- forward: , top to bottom, ;
- back: , bottom to top, .
Cost
Factoring an matrix takes about multiply-adds; each pair of triangular solves takes about . A banded matrix, like a ladder's tridiagonal one, needs only about of each, which is why simulators exploit sparsity.
Worked example
How long does factoring a 1000 × 1000 matrix take, and each solve after it?
Show the solution
- Count the work. Factoring takes multiply-adds, and each solve .
- Convert to time. At a billion multiply-adds a second (a modest rate for a laptop core running plain loops in a compiled language), that's about 0.3 s to factor and 1 ms per solve.
- In pure Python. Pure Python loops run about 100 times slower, which is why NumPy hands the work to compiled LAPACK routines.
Answer about 0.3 s to factor and 1 ms for each solve, at a billion multiply-adds a second.
Worked example
Solve in double precision, without and with pivoting.
Show the solution
- The true answer. It is very close to .
- Eliminate without pivoting. The multiplier is , rounds to , and the right side becomes .
- Back-substitute. completely wrong.
- With pivoting. The rows swap so the pivot is 1, the multiplier is , and the answer comes out to full precision.
- Where the fault lies. The matrix is perfectly well conditioned; only the unpivoted algorithm was unstable.
Answer without pivoting the answer comes out , completely wrong; with pivoting it's to full precision.
Check
About how many multiply-adds does factoring a 300 × 300 matrix take?
Check
A 100-node circuit is solved for 10 source values. How many times faster is factoring once than factoring each time?
Check
Write gauss_solve(A, b): Gaussian elimination with partial pivoting, then back substitution. Don't call np.linalg. Set x = gauss_solve(A, b) for the system with the pivot.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write lu(A) returning L and U with no row exchanges (assume the pivots are nonzero). Factor the matrix from lesson 9.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write lu_solve(L, U, b) by forward and back substitution. Factor the ladder mS once with your lu (in the starter), then set volts to node 3's voltage for currents of 1, 2, 3, 4 and 5 mA into node 1.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Build the conductance matrix of an -node ladder, = 30: 1 mS between neighboring nodes and 1 mS from every node to ground, so interior diagonal entries are 3 and the two end ones are 2. Inject 1 mA at node 1, solve, and set ratio to (with nodes counted from 1).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
A circuit simulator runs a transient analysis of a linear circuit with a fixed time step. Why does it factor the matrix once instead of solving from scratch at every step?
With a fixed step and fixed parts, the matrix in each step's equations is the same; only the right-hand side changes, as sources and the stored charges and currents of capacitors and inductors evolve. Factoring costs about operations and each solve with the factors about , so for a circuit of a few hundred nodes run for thousands of steps, factoring once is a hundred or more times faster. When the step size changes or a nonlinear part's operating point moves, the matrix changes and must be refactored, which is why simulators try to keep the step constant and reuse factors whenever they can.
Practice
Keep practicing
5 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.