MatrixStep

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).

A =2114−60−272=100210−1−11·2110−8−2001

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.

  1. 01
    Start from A. L starts as the identity matrix and gets filled in with the multipliers.
    2114−60−272
  2. 02
    Clear 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
  3. 03
    Clear 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.

L =100210−1−11U =2110−8−2001

Check

Multiplying L and U must give back the original A.

100210−1−11·2110−8−2001=2114−60−272

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.

  1. Solve Ly = b from the top down (forward substitution). Because L is lower triangular, each row introduces only one new unknown.
  2. Solve Ux = y from the bottom up (back substitution).

Let's try it with b = (5, −2, 9).

Ly = b:y₁ = 52y₁ + y₂ = −2 → y₂ = −12−y₁ − y₂ + y₃ = 9 → y₃ = 2
Ux = y:x₃ = 2−8x₂ − 2x₃ = −12 → x₂ = 12x₁ + x₂ + x₃ = 5 → x₁ = 1

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.

P =0110PB =2103=1001·2103

The LU decomposition calculator builds and shows P automatically whenever a row swap is needed.

Common mistakes

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