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

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 n-node circuit with k different right-hand sides.
  • The upper curve factors from scratch each time: k(n33+n2). The lower one factors once and reuses L and U: n33+kn2.
  • Raise n. The gap widens, because factoring grows like n3 and each extra solve only like n2.

Predict

A 500-node circuit must be solved for 1000 different source values. How much faster is factoring once than factoring every time?

Name it

Elimination with partial pivoting

For each column k:

  1. find the row at or below k with the largest |aik|, and swap it into row k;
  2. for each row i below, subtract ℓik=aik/akk times row k.

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 L gives A=LU (or PA=LU with row exchanges). Then A𝐱=𝐛 is two triangular solves:

  • forward: L𝐲=𝐛, top to bottom, yi=bi−∑j<iℓijyj;
  • back: U𝐱=𝐲, bottom to top, xi=(yi−∑j>iuijxj)/uii.

Cost

Factoring an n×n matrix takes about n33 multiply-adds; each pair of triangular solves takes about n2. A banded matrix, like a ladder's tridiagonal one, needs only about n 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
  1. Count the work. Factoring takes 100033=3.3×108 multiply-adds, and each solve 10002=106.
  2. 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.
  3. 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 [10−20111]𝐱=[12] in double precision, without and with pivoting.

Show the solution
  1. The true answer. It is very close to (1,1).
  2. Eliminate without pivoting. The multiplier is 1020, u22=1−1020 rounds to −1020, and the right side becomes 2−1020≈−1020.
  3. Back-substitute. x2=1 x1=1−110−20=0 completely wrong.
  4. With pivoting. The rows swap so the pivot is 1, the multiplier is 10−20, and the answer comes out (1,1) to full precision.
  5. Where the fault lies. The matrix is perfectly well conditioned; only the unpivoted algorithm was unstable.

Answer without pivoting the answer comes out (0,1), completely wrong; with pivoting it's (1,1) 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 10−20 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 G=[2−10−12−10−12] 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 n-node ladder, n = 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 vn/2+1/vn/2 (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?

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.