Guides8 min
LU Decomposition Step by Step: 3×3 Example and Solving Linear Systems
Do Gaussian elimination once, store the result as a product of two matrices, and you can solve a linear system with a changing right-hand side as many times as you like, quickly. That is LU decomposition. This guide works through the factorization on a 3×3 example and then uses it to solve Ax = b.
What is LU decomposition?
It writes a square matrix A as the product A = LU of a lower triangular matrix L and an upper triangular matrix U. L has 1s on its diagonal and 0s above it; U has 0s below its diagonal (the Doolittle form).
In fact U is just the row echelon form produced by Gaussian elimination, and L holds the multipliers used during elimination. So the decomposition is not a new computation; it is "a record of the elimination process".
Example: factoring a 3×3 matrix
If an elimination step says "subtract m times row j from row i", then that m goes into position (i, j) of L. If you added instead of subtracting, it is −m.
- 01Start from A. L starts as the identity matrix and gets filled in with the multipliers.2114−60−272
- 02Clear the entries below the pivot in column 1. The multipliers used here, l₂₁ = 2, l₃₁ = −1, go into the same positions of L.
R₂ ← R₂ − 2R₁R₃ ← R₃ + R₁2110−8−2083 - 03Clear the entries below the pivot in column 2. The multipliers used here, l₃₂ = −1, go into the same positions of L.
R₃ ← R₃ + R₂2110−8−2001
The final U is upper triangular, and collecting the recorded multipliers gives L.
Check
Multiplying L and U must give back the original A.
Solving a linear system with LU
In Ax = b, since A = LU, we have L(Ux) = b. Writing y for Ux splits this into two stages.
- Solve Ly = b from the top down (forward substitution). Because L is lower triangular, each row introduces only one new unknown.
- Solve Ux = y from the bottom up (back substitution).
Let's try it with b = (5, −2, 9).
The solution is (x, y, z) = (1, 1, 2). Substituting into the original equations confirms it. Even with ten different right-hand sides b, L and U stay the same and only the substitutions are repeated, which is far less work than computing an inverse.
Bonus: the determinant comes for free
Since det(L) = 1 and the determinant of a triangular matrix is the product of its diagonal entries, det(A) = det(U) = the product of the diagonal entries of U. In the example above, 2 · (−8) · 1 = −16, and cofactor expansion also gives −16. (If you swapped rows, the sign flips once for each swap.)
When a pivot is zero: PA = LU
If a pivot position is 0 during elimination, you have to swap with a row below. Recording the row swaps in a permutation matrix P gives PA = LU. For example, B = [[0, 3], [2, 1]] has a zero first pivot, so swap the two rows before factoring.
The LU decomposition calculator builds and shows P automatically whenever a row swap is needed.
Common mistakes
- Writing the multipliers in L with the wrong sign. "R₂ ← R₂ − 2R₁" gives l₂₁ = 2; "R₂ ← R₂ + 2R₁" gives l₂₁ = −2.
- Putting something other than 1 on the diagonal of L. In the Doolittle form, the diagonal of L is all 1s and the diagonal of U holds the pivots. (The Crout form does it the other way round.)
- Dividing by a zero pivot anyway. You must swap rows and record the swap in P.
- Doing the substitutions in the wrong direction. Ly = b goes top to bottom; Ux = y goes bottom to top.
FAQ
Is LU decomposition always possible?
If row swaps are allowed (PA = LU), every square matrix can be factored. To write A = LU without row swaps, no pivot may ever become 0 during elimination.
How is this different from computing the inverse?
With the inverse A⁻¹ you can solve x = A⁻¹b, but it costs about three times as much as LU. With several right-hand sides it is much faster to compute LU once and repeat only the substitutions, which is why almost all numerical software uses LU.
Can a singular matrix be factored?
The factorization itself still works. However, the last diagonal entry of U becomes 0, so det(A) = 0, and when solving Ax = b the back substitution runs into a division by zero.
Related guides