Linear systems¶
We now attend to the central problem of this chapter: Given a square, n×n matrix A and an n-vector b, find an n-vector x such that Ax=b. Writing out these equations, we obtain
If A is nonsingular, then the mathematical expression of the solution is x=A−1b, because
When A is singular, then Ax=b may have no solution or infinitely many solutions.
If we define
then it is easy to check that for any real value of α we have
Hence the linear system Sx=b with $\mathbf{b}= [1\0] $ has infinitely many solutions. For many other choices of b the system can be proven to have no solutions.
Don’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. While it can be done numerically by computer, it almost never is, because when the goal is to solve a linear system of equations, the inverse is not needed—and the process of finding it is slower than solving the original problem.
Julia does have a command inv that finds the inverse of a matrix. But, as demonstrated in Interpolating the population of China, in order to solve a linear system of equations, you should use backslash (the \
symbol, not to be confused with the slash /
used in web addresses).
Triangular systems¶
The solution process is especially easy to demonstrate for a system with a triangular matrix. For example, consider the lower triangular system
The first row of this system states simply that 4x1=8, which is easily solved as x1=8/4=2. Now, the second row states that 3x1−x2=5. As x1 is already known, it can be replaced to find that x2=−(5−3⋅2)=1. Similarly, the third row gives x3=(0+1⋅2)/3=2/3, and the last row yields x4=(1−1⋅2+1⋅1+1⋅2/3)/2=1/3. Hence the solution is
The process just described is called forward substitution. In the 4×4 lower triangular case of Lx=b it leads to the formulas
For upper triangular systems Ux=b an analogous process of backward substitution begins by solving for the last component xn=bn/Unn and working backward. For the 4×4 case we have
Solving the system backward, starting with x4 first and then proceeding in descending order, gives
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.
A triangular matrix is singular if and only if at least one of its diagonal elements is zero.
Implementation¶
Consider how to implement the sequential process implied by equation (27). It seems clear that we want to loop through the elements of x in order. Within each iteration of that loop, we have an expression whose length depends on the iteration number. One way we could do this would be with a nested loop:
A briefer version of the inner loop over j
is the comprehension
However, when i
equals 1, the range 1:i-1
is empty and the sum fails. To avoid this we can handle this case before the i
loop begins, and start that loop at 2. This is the approach taken in Function 12.
Forward substitution to solve a lower-triangular linear system
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
The implementation of backward substitution is much like forward substitution and is given in Function 13.
Backward substitution to solve an upper-triangular linear system
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
The example in Solving triangular systems 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 later in the chapter. Before reaching that point, however, we need to discuss how to solve general linear systems, not just triangular ones.
Exercises¶
✍ Find a right-hand side vector b such that the system [0100]x=b has no solution.
✍ Solve the following triangular systems by hand.
(a) −2x1=−4x1−x2=23x1+2x2+x3=1 (b) [40001−200−14402−551]x=[−41−35] (c) 3x1+2x2+x3=1x2−x3=22x3=−4
⌨ Use forwardsub to solve the systems from the previous problem. Verify that the solution is correct by computing Lx and subtracting b.
⌨ Use backsub to solve the following systems. Verify that the solution is correct by computing Ux and subtracting b.
(a) [3100−1−2003]x=[116] (b) [31060−1−2700340005]x=[4115]
⌨ If B∈Rn×p, has columns b1,…,bp, then we can pose p linear systems at once by writing AX=B, where X is n×p. Specifically, this equation implies Axj=bj for j=1,…,p.
(a) Modify forwardsub and backsub so that they solve the case where the second input is n×p for p≥1.
(b) If AX=I, then X=A−1. Use this fact to write a function
ltinverse
that uses your modified forwardsub to compute the inverse of a lower triangular matrix. Test your function on at least two nontrivial matrices. (We remind you here that this is just an exercise; matrix inverses are rarely a good idea in numerical practice!)⌨ Solving triangular systems showed solutions of Ax=b, where
A=[1−10α−ββ01−100001−100001−100001],b=[α0001].Solve with α=0.1 and β=10,100,…,1012, making a table of the values of β and |x1−1|.