Skip to article frontmatterSkip to article content

Linear systems

We now attend to the central problem of this chapter: Given a square, n×nn\times n matrix A\mathbf{A} and an nn-vector b\mathbf{b}, find an nn-vector x\mathbf{x} such that Ax=b\mathbf{A}\mathbf{x}=\mathbf{b}. Writing out these equations, we obtain

A11x1+A12x2++A1nxn=b1,A21x1+A22x2++A2nxn=b2,An1x1+An2x2++Annxn=bn.\begin{split} A_{11}x_1 + A_{12}x_2 + \cdots + A_{1n}x_n &= b_1, \\ A_{21}x_1 + A_{22}x_2 + \cdots + A_{2n}x_n &= b_2, \\ \vdots \\ A_{n1}x_1 + A_{n2}x_2 + \cdots + A_{nn}x_n &= b_n. \end{split}

If A\mathbf{A} is invertible, then the mathematical expression of the solution is x=A1b\mathbf{x}=\mathbf{A}^{-1}\mathbf{b} because

A1b=A1(Ax)=(A1A)x=Ix=x.\begin{split} \mathbf{A}^{-1}\mathbf{b} = \mathbf{A}^{-1} (\mathbf{A} \mathbf{x}) = (\mathbf{A}^{-1}\mathbf{A}) \mathbf{x} = \mathbf{I} \mathbf{x} = \mathbf{x}. \end{split}

When A\mathbf{A} is singular, then Ax=b\mathbf{A}\mathbf{x}=\mathbf{b} may have no solution or infinitely many solutions.

2.3.1Don’t use the inverse

Matrix inverses are indispensable for mathematical discussion and derivations. However, as you may remember from a linear algebra course, they are not trivial to compute from the entries of the original matrix. You might be surprised to learn that matrix inverses play almost no role in scientific computing.

In fact, when we encounter an expression such as x=A1b\mathbf{x} = \mathbf{A}^{-1} \mathbf{b} in computing, we interpret it as “solve the linear system Ax=b\mathbf{A} \mathbf{x} = \mathbf{b}” and apply whatever algorithm is most expedient based on what we know about A\mathbf{A}.

Julia
MATLAB
Python

As demonstrated in Example 2.1.1, the backslash (the \ symbol, not to be confused with the slash / used in web addresses) invokes a linear system solution.

2.3.2Triangular systems

The solution process is especially easy to demonstrate for a system with a triangular matrix. For example, consider the lower triangular system

[4000310010301112]x=[8501]. \begin{bmatrix} 4 & 0 & 0 & 0 \\ 3 & -1 & 0 & 0 \\ -1 & 0 & 3 & 0 \\ 1 & -1 & -1 & 2 \end{bmatrix} \mathbf{x} = \begin{bmatrix} 8 \\ 5 \\ 0 \\ 1 \end{bmatrix}.

The first row of this system states simply that 4x1=84x_1=8, which is easily solved as x1=8/4=2x_1=8/4=2. Now, the second row states that 3x1x2=53x_1-x_2=5. As x1x_1 is already known, it can be replaced to find that x2=(532)=1x_2 = -(5-3\cdot 2)=1. Similarly, the third row gives x3=(0+12)/3=2/3x_3=(0+1\cdot 2)/3 = 2/3, and the last row yields x4=(112+11+12/3)/2=1/3x_4=(1-1\cdot 2 + 1\cdot 1 + 1\cdot 2/3)/2 = 1/3. Hence, the solution is

x=[212/31/3]. \mathbf{x} = \begin{bmatrix} 2 \\ 1 \\ 2/3 \\ 1/3 \end{bmatrix}.

The process just described is called forward substitution. In the 4×44\times 4 lower triangular case of Lx=b\mathbf{L}\mathbf{x}=\mathbf{b} it leads to the formulas

x1=b1L11,x2=b2L21x1L22,x3=b3L31x1L32x2L33,x4=b4L41x1L42x2L43x3L44.\begin{split} x_1 &= \frac{b_1}{L_{11}}, \\ x_2 &= \frac{b_2 - L_{21}x_1}{L_{22}}, \\ x_3 &= \frac{b_3 - L_{31}x_1 - L_{32}x_2}{L_{33}}, \\ x_4 &= \frac{b_4 - L_{41}x_1 - L_{42}x_2 - L_{43}x_3}{L_{44}}. \end{split}

For upper triangular systems Ux=b\mathbf{U}\mathbf{x}=\mathbf{b} an analogous process of backward substitution begins by solving for the last component xn=bn/Unnx_n=b_n/U_{nn} and working backward. For the 4×44\times 4 case we have

[U11U12U13U140U22U23U2400U33U34000U44]x=[b1b2b3b4]. \begin{bmatrix} U_{11} & U_{12} & U_{13} & U_{14} \\ 0 & U_{22} & U_{23} & U_{24} \\ 0 & 0 & U_{33} & U_{34} \\ 0 & 0 & 0 & U_{44} \end{bmatrix} \mathbf{x} = \begin{bmatrix} b_1 \\ b_2 \\ b_3 \\ b_4 \end{bmatrix}.

Solving the system backward, starting with x4x_4 first and then proceeding in descending order, gives

x4=b4U44,x3=b3U34x4U33,x2=b2U23x3U24x4U22,x1=b1U12x2U13x3U14x4U11.\begin{split} x_4 &= \frac{b_4}{U_{44}}, \\ x_3 &= \frac{b_3 - U_{34}x_4}{U_{33}}, \\ x_2 &= \frac{b_2 - U_{23}x_3 - U_{24}x_4}{U_{22}}, \\ x_1 &= \frac{b_1 - U_{12}x_2 - U_{13}x_3 - U_{14}x_4}{U_{11}}. \end{split}

It should be clear that forward or backward substitution fails if and only if one of the diagonal entries of the system matrix is zero. We have essentially proved the following theorem.

2.3.3Implementation

Consider how to implement the sequential process implied by Equation (2.3.7). It seems clear that we want to loop through the elements of x\mathbf{x} in order. Within each iteration of that loop, we have an expression whose length depends on the iteration number. This leads to a nested loop structure.

The implementation of backward substitution is much like forward substitution and is given in Function 2.3.2.

The example in Example 2.3.3 is our first clue that linear system problems may have large condition numbers, making inaccurate solutions inevitable in floating-point arithmetic. We will learn how to spot such problems in Conditioning of linear systems. Before reaching that point, however, we need to discuss how to solve general linear systems, not just triangular ones.

2.3.4Exercises