MATH-125 · Lesson 5 of 12 · 33 min
LU factorization and computational linear algebra
Engineering software solves linear systems constantly. A structural analysis program assembles a stiffness matrix with hundreds of thousands of unknowns and solves it for each load case; a circuit simulator solves a linear system at every time step; a weather model solves enormous systems many times an hour. Lesson 3 showed how Gaussian elimination solves a system by hand, and Lesson 4 introduced the matrix algebra that describes it. This lesson looks at how computers actually solve linear systems efficiently and reliably. It shows that elimination is equivalent to factoring the matrix into lower and upper triangular parts, explains why that factorization is so useful, counts the cost of the operations, introduces special methods for the symmetric, banded and sparse matrices that dominate engineering, and explains how the condition number warns when a computed solution cannot be trusted.
Elimination as a factorization
Gaussian elimination reduces a matrix \(A\) to an upper triangular matrix \(U\) by subtracting multiples of earlier rows from later rows. Each such step is equivalent to multiplying by a simple elementary matrix, and the multipliers used can be stored in a lower triangular matrix \(L\) with ones on its diagonal. The result is the LU factorization:
where \(L\) is lower triangular, holding the multipliers \(\ell_{ij}\) (the amount of row \(j\) subtracted from row \(i\)), and \(U\) is the upper triangular matrix that elimination produces. Nothing new is computed: the factorization is simply a record of the elimination.
Worked problem
An LU factorization
Find the LU factorization of \(A = \begin{pmatrix} 2 & 1 & 1 \\ 4 & 3 & 3 \\ 8 & 7 & 9 \end{pmatrix}\).
Show solution
- Eliminate below the first pivot: row 2 − 2 × row 1 gives \((0, 1, 1)\); row 3 − 4 × row 1 gives \((0, 3, 5)\). Multipliers \(\ell_{21} = 2\), \(\ell_{31} = 4\).
- Eliminate below the second pivot: row 3 − 3 × row 2 gives \((0, 0, 2)\). Multiplier \(\ell_{32} = 3\).
- \[L = \begin{pmatrix} 1 & 0 & 0 \\ 2 & 1 & 0 \\ 4 & 3 & 1 \end{pmatrix}, \qquad U = \begin{pmatrix} 2 & 1 & 1 \\ 0 & 1 & 1 \\ 0 & 0 & 2 \end{pmatrix}\]
Answer: A = LU with the multipliers in L. Multiplying L by U gives back A, a useful check.
Solving with the factorization
Once \(A = LU\) is known, the system \(A\mathbf{x} = \mathbf{b}\) becomes \(L(U\mathbf{x}) = \mathbf{b}\). Write \(\mathbf{y} = U\mathbf{x}\) and solve in two easy stages:
- Forward substitution: solve \(L\mathbf{y} = \mathbf{b}\) from the top row down.
- Back substitution: solve \(U\mathbf{x} = \mathbf{y}\) from the bottom row up.
Triangular systems are cheap to solve, because each step involves only one new unknown.
Worked problem
Two load cases
Using the factorization above, solve \(A\mathbf{x} = \mathbf{b}\) for \(\mathbf{b} = (4, 10, 24)\) and then for \(\mathbf{b} = (1, 2, 6)\).
Show solution
- First case, forward: \(y_1 = 4\); \(2(4) + y_2 = 10\), so \(y_2 = 2\); \(4(4) + 3(2) + y_3 = 24\), so \(y_3 = 2\).
- Back: \(2x_3 = 2\), \(x_3 = 1\); \(x_2 + 1 = 2\), \(x_2 = 1\); \(2x_1 + 1 + 1 = 4\), \(x_1 = 1\).
- Second case, forward: \(y = (1, 0, 2)\). Back: \(x_3 = 1\), \(x_2 = -1\), \(x_1 = \tfrac{1}{2}(1 + 1 - 1) = 0.5\).
Answer: x = (1, 1, 1) and x = (0.5, −1, 1). The expensive elimination was done once; each new right-hand side, such as a new load case on a structure, costs only two triangular solves.
In practice, rows are interchanged during elimination to put the largest available entry in the pivot position (partial pivoting, Lesson 3), which keeps rounding errors under control. The interchanges are recorded in a permutation matrix \(P\), and the factorization becomes \(PA = LU\). Every numerical library computes this factorization when asked to solve a general linear system.
The cost of solving linear systems
Counting arithmetic operations shows why factorization matters. For an \(n \times n\) matrix:
- LU factorization takes about \(\tfrac{2}{3}n^3\) floating-point operations.
- Each forward and back substitution pair takes about \(2n^2\) operations.
- Computing the inverse \(A^{-1}\) takes about \(2n^3\) operations, three times as many as the factorization, and multiplying \(A^{-1}\mathbf{b}\) costs another \(2n^2\).
The \(n^3\) growth is severe: doubling the size of a problem multiplies the factorization cost by eight. For \(n = 10{,}000\), the factorization requires about \(6.7 \times 10^{11}\) operations, a fraction of a second to a few seconds on a modern processor, while each additional right-hand side needs only \(2 \times 10^8\). This is why engineers almost never compute matrix inverses: solving with an LU factorization is cheaper and more accurate. The notation \(A^{-1}\mathbf{b}\) means "the solution of \(A\mathbf{x} = \mathbf{b}\)", not an instruction to form the inverse.
Worked problem
Planning a computation
A structural model has 20,000 unknowns and 50 load cases. Compare the operation counts of (a) solving each case by elimination from scratch, (b) one LU factorization plus 50 pairs of triangular solves.
Show solution
- (a) \(50 \times \tfrac{2}{3}(20{,}000)^3 = 50 \times 5.3 \times 10^{12} = 2.7 \times 10^{14}\).
- (b) \(5.3 \times 10^{12} + 50 \times 2(20{,}000)^2 = 5.3 \times 10^{12} + 4 \times 10^{10} = 5.4 \times 10^{12}\).
Answer: Factoring once is about fifty times cheaper. In reality such a model would also be sparse, which, as the next section shows, reduces the cost far more.
Exploiting structure
The matrices that arise in engineering are rarely arbitrary. Recognizing their structure can reduce the work by orders of magnitude.
Symmetric positive definite matrices and Cholesky factorization
Stiffness matrices, conductance matrices and many others are symmetric (\(A^T = A\)) and positive definite: \(\mathbf{x}^T A\mathbf{x} > 0\) for every non-zero \(\mathbf{x}\), which physically means that any non-zero displacement stores positive energy. Such matrices have a Cholesky factorization \(A = R^T R\), with \(R\) upper triangular, which needs no pivoting and costs about \(\tfrac{1}{3}n^3\) operations, half the cost of LU. Failure of the Cholesky factorization is itself informative: in a structural model it usually means the structure is not properly supported, so that some displacement stores no energy (a mechanism).
Banded and tridiagonal matrices
In many problems each unknown interacts only with its neighbours: the temperatures at points along a rod, the displacements of masses connected in a chain, the nodes of a long transmission line. The resulting matrices are banded, with non-zero entries only near the diagonal. For a tridiagonal matrix, elimination involves only a few operations per row, and the Thomas algorithm solves the system in about \(8n\) operations instead of \(\tfrac{2}{3}n^3\).
Worked problem
Temperatures along a rod
The steady temperatures at three interior points of a rod with ends at 0 °C and 100 °C, with equal heat generation, satisfy \(2T_1 - T_2 = 10\), \(-T_1 + 2T_2 - T_3 = 10\), \(-T_2 + 2T_3 = 110\). Solve by eliminating down the band.
Show solution
- Row 2 + ½ row 1: \(1.5T_2 - T_3 = 15\). Row 3 + (2/3) of the new row 2: \((4/3)T_3 = 120\), so \(T_3 = 90\).
- Back: \(1.5T_2 = 15 + 90\), \(T_2 = 70\); \(2T_1 = 10 + 70\), \(T_1 = 40\).
Answer: T = (40, 70, 90) °C. Each elimination step touched only one neighbouring row, which is why tridiagonal systems with millions of unknowns are solved in a fraction of a second.
Sparse matrices
Large engineering models produce sparse matrices, in which almost all entries are zero: a finite element model with a million unknowns might have only about 30 non-zero entries per row. Storing only the non-zeros saves enormous memory, and sparse direct solvers reorder the unknowns to limit fill-in, the creation of new non-zeros during elimination. Alternatively, iterative methods avoid factorization entirely.
Iterative methods
Iterative methods start from a guess and improve it repeatedly, using only multiplications by the matrix, which are cheap for sparse matrices. The simplest, the Jacobi method, solves each equation for its own diagonal unknown using the previous values of the others; the Gauss-Seidel method uses each new value as soon as it is computed. Both converge if the matrix is strictly diagonally dominant, meaning each diagonal entry is larger in magnitude than the sum of the other entries in its row, a property many physical models have.
Worked problem
Gauss-Seidel iteration
Apply Gauss-Seidel to \(4x + y = 9\), \(x + 3y = 7\), starting from \(x = y = 0\).
Show solution
- Iteration 1: \(x = (9 - 0)/4 = 2.25\); \(y = (7 - 2.25)/3 = 1.583\).
- Iteration 2: \(x = (9 - 1.583)/4 = 1.854\); \(y = (7 - 1.854)/3 = 1.715\).
- Iteration 3: \(x = 1.821\); \(y = 1.726\). The exact solution is \(x = 20/11 = 1.818\), \(y = 19/11 = 1.727\).
Answer: Three iterations give three correct digits. Modern iterative solvers, such as the conjugate gradient method for symmetric positive definite systems, converge far faster with good preconditioning and are the methods of choice for the largest problems (MATH-300).
Conditioning: when can a solution be trusted?
Lesson 3 showed that a nearly singular system can magnify small changes in its data into large changes in its solution. The condition number \(\kappa(A)\) measures this sensitivity. Roughly, if the data (the entries of \(A\) or \(\mathbf{b}\)) have relative errors of size \(\epsilon\), the solution can have relative errors up to about \(\kappa(A)\,\epsilon\):
where \(\|\cdot\|\) denotes a vector or matrix norm (a measure of size). The condition number is at least 1; a value of \(10^k\) means that up to \(k\) significant digits can be lost. Since double-precision arithmetic carries about 16 digits, a matrix with \(\kappa = 10^{12}\) leaves only about four trustworthy digits, and if the input data are known to only three digits, the answer may be meaningless.
Worked problem
An ill-conditioned system
Solve \(x + y = 2\), \(x + 1.0001y = 2.0001\), then change the second right-hand side to 2.0002.
Show solution
- First system: subtracting, \(0.0001y = 0.0001\), so \(y = 1\), \(x = 1\).
- Second system: \(0.0001y = 0.0002\), so \(y = 2\), \(x = 0\).
Answer: A change of 0.005 percent in one number changed the solution completely. The two equations describe nearly parallel lines, and the condition number is about 40,000. In practice, ill-conditioning arises from nearly redundant measurements, poorly scaled units, or models whose parts barely constrain each other; the remedy is usually to reformulate the model, not to compute more carefully.
A small residual \(\mathbf{r} = \mathbf{b} - A\hat{\mathbf{x}}\) does not guarantee an accurate solution \(\hat{\mathbf{x}}\) when the matrix is ill-conditioned: many very different vectors nearly satisfy the equations. Numerical libraries can estimate the condition number cheaply alongside the solution, and responsible engineering software warns when it is large.
Common mistakes
MistakeComputing A⁻¹ to solve Ax = b.
InsteadFactor A = LU (or Cholesky) and use triangular solves.
MistakeRefactoring the matrix for every new right-hand side.
InsteadFactor once and reuse L and U.
MistakeIgnoring symmetry, bandedness and sparsity.
InsteadUse Cholesky, banded or sparse solvers to save time and memory.
MistakeTrusting a solution because its residual is small.
InsteadCheck the condition number; ill-conditioned systems can have small residuals and large errors.
MistakeMixing very different units in one system without scaling.
InsteadScale equations and unknowns to similar magnitudes to improve conditioning.
Summary and key terms
- Gaussian elimination is equivalent to the factorization A = LU, with multipliers in L.
- With A = LU, each system is solved by forward and back substitution; with pivoting, PA = LU.
- Factorization costs about ⅔n³ operations, each solve about 2n²; inverses are rarely needed.
- Symmetric positive definite matrices use Cholesky factorization at half the cost.
- Banded and sparse matrices allow dramatically cheaper solution; tridiagonal systems cost O(n).
- Iterative methods such as Gauss-Seidel suit large sparse, diagonally dominant systems.
- The condition number κ(A) bounds error amplification; κ = 10ᵏ can cost k digits.
- LU factorization
- Writing a matrix as a product of lower and upper triangular matrices.
- Forward substitution
- Solving a lower triangular system from the top down.
- Back substitution
- Solving an upper triangular system from the bottom up.
- Permutation matrix
- A matrix that records row interchanges.
- Positive definite
- A symmetric matrix with xᵀAx > 0 for all non-zero x.
- Cholesky factorization
- A = RᵀR for symmetric positive definite matrices.
- Banded matrix
- A matrix whose non-zeros lie near the diagonal.
- Sparse matrix
- A matrix with mostly zero entries.
- Diagonally dominant
- Each diagonal entry exceeds the sum of the other entries in its row in magnitude.
- Condition number
- A measure of how much a matrix can amplify relative errors.
Practice problems
Work each problem on paper first, then reveal the answer to check.
Find the LU factorization of \(\begin{pmatrix} 1 & 2 \\ 3 & 8 \end{pmatrix}\).
Show answer
L = [[1, 0], [3, 1]], U = [[1, 2], [0, 2]].
With the factorization above, solve for b = (5, 19).
Show answer
y = (5, 4); x₂ = 2, x₁ = 1.
How does the factorization cost change if n triples?
Show answer
It rises 27 times.
Why is computing A⁻¹ usually avoided?
Show answer
It costs about three times as much as LU and is less accurate.
What physical property makes a stiffness matrix positive definite?
Show answer
Any non-zero displacement stores positive strain energy.
What might a failed Cholesky factorization of a stiffness matrix indicate?
Show answer
The structure is not adequately supported (a mechanism).
Approximately how many operations does the Thomas algorithm need for n = 10⁶?
Show answer
About 8 × 10⁶.
Is \(\begin{pmatrix} 5 & 2 & 1 \\ 1 & 4 & 1 \\ 2 & 1 & 6 \end{pmatrix}\) strictly diagonally dominant?
Show answer
Yes: 5 > 3, 4 > 2, 6 > 3.
One Jacobi step for 4x + y = 9, x + 3y = 7 from (0, 0)?
Show answer
(2.25, 2.333).
Data accurate to 4 digits and κ = 10³. How many digits can be trusted?
Show answer
About one.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.