





































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






































One of the problems encountered most frequently in scientific computation is the solution of systems of simultaneous linear equations. This chapter covers the solu- tion of linear systems by Gaussian elimination and the sensitivity of the solution to errors in the data and roundoff errors in the computation.
With matrix notation, a system of simultaneous linear equations is written
Ax = b.
In the most frequent case, when there are as many equations as unknowns, A is a given square matrix of order n, b is a given column vector of n components, and x is an unknown column vector of n components. Students of linear algebra learn that the solution to Ax = b can be written x = A−^1 b, where A−^1 is the inverse of A. However, in the vast majority of practical computational problems, it is unnecessary and inadvisable to actually compute A−^1. As an extreme but illustrative example, consider a system consisting of just one equation, such as 7 x = 21.
The best way to solve such a system is by division:
x =
Use of the matrix inverse would lead to
x = 7−^1 × 21 = 0. 142857 × 21 = 2. 99997.
The inverse requires more arithmetic—a division and a multiplication instead of just a division—and produces a less accurate answer. Similar considerations apply
September 16, 2013
2 Chapter 2. Linear Equations
to systems of more than one equation. This is even true in the common situation where there are several systems of equations with the same matrix A but different right-hand sides b. Consequently, we shall concentrate on the direct solution of systems of equations rather than the computation of the inverse.
To emphasize the distinction between solving linear equations and computing in- verses, Matlab has introduced nonstandard notation using backward slash and forward slash operators, “\” and “/”. If A is a matrix of any size and shape and B is a matrix with as many rows as A, then the solution to the system of simultaneous equations
AX = B
is denoted by X = A\B.
Think of this as dividing both sides of the equation by the coefficient matrix A. Because matrix multiplication is not commutative and A occurs on the left in the original equation, this is left division. Similarly, the solution to a system with A on the right and B with as many columns as A, XA = B,
is obtained by right division, X = B/A.
This notation applies even if A is not square, so that the number of equations is not the same as the number of unknowns. However, in this chapter, we limit ourselves to systems with square coefficient matrices.
To illustrate the general linear equation solution algorithm, consider an example of order three: (^)
x 1 x 2 x 3
This, of course, represents the three simultaneous equations
10 x 1 − 7 x 2 = 7, − 3 x 1 + 2x 2 + 6x 3 = 4, 5 x 1 − x 2 + 5x 3 = 6.
The first step of the solution algorithm uses the first equation to eliminate x 1 from the other equations. This is accomplished by adding 0.3 times the first equation
4 Chapter 2. Linear Equations
The entire algorithm can be compactly expressed in matrix notation. For this example, let
The matrix L contains the multipliers used during the elimination, the matrix U is the final coefficient matrix, and the matrix P describes the pivoting. With these three matrices, we have LU = P A.
In other words, the original coefficient matrix can be expressed in terms of products involving matrices with simpler structure.
A permutation matrix is an identity matrix with the rows and columns interchanged. It has exactly one 1 in each row and column; all the other elements are 0. For example,
Multiplying a matrix A on the left by a permutation matrix to give P A permutes the rows of A. Multiplying on the right, AP , permutes the columns of A. Matlab can also use a permutation vector as a row or column index to rear- range the rows or columns of a matrix. Continuing with the P above, let p be the vector
p = [4 1 3 2]
Then PA and A(p,:) are equal. The resulting matrix has the fourth row of A as its first row, the first row of A as its second row, and so on. Similarly, AP and A(:,p) both produce the same permutation of the columns of A. The P*A notation is closer to traditional mathematics, P A, while the A(p,:) notation is faster and uses less memory. Linear equations involving permutation matrices are trivial to solve. The solution to P x = b
is simply a rearrangement of the components of b:
x = P T^ b.
An upper triangular matrix has all its nonzero elements above or on the main diagonal. A unit lower triangular matrix has ones on the main diagonal and all the
2.5. LU Factorization 5
rest of its nonzero elements below the main diagonal. For example,
is upper triangular, and
is unit lower triangular. Linear equations involving triangular matrices are also easily solved. There are two variants of the algorithm for solving an n-by-n upper triangular system U x = b. Both begin by solving the last equation for the last variable, then the next-to-last equation for the next-to-last variable, and so on. One subtracts multiples of the columns of U from b.
x = zeros(n,1); for k = n:-1: x(k) = b(k)/U(k,k); i = (1:k-1)’; b(i) = b(i) - x(k)*U(i,k); end
The other uses inner products between the rows of U and portions of the emerging solution x.
x = zeros(n,1); for k = n:-1: j = k+1:n; x(k) = (b(k) - U(k,j)*x(j))/U(k,k); end
The algorithm that is almost universally used to solve square systems of simultane- ous linear equations is one of the oldest numerical methods, the systematic elimi- nation method, generally named after C. F. Gauss. Research in the period 1955 to 1965 revealed the importance of two aspects of Gaussian elimination that were not emphasized in earlier work: the search for pivots and the proper interpretation of the effect of rounding errors. In general, Gaussian elimination has two stages, the forward elimination and the back substitution. The forward elimination consists of n − 1 steps. At the kth step, multiples of the kth equation are subtracted from the remaining equations to eliminate the kth variable. If the coefficient of xk is “small,” it is advisable to
2.6. Why Is Pivoting Necessary? 7
The relation LU = P A is called the LU factorization or the triangular de- composition of A. It should be emphasized that nothing new has been introduced. Computationally, elimination is done by row operations on the coefficient matrix, not by actual matrix multiplication. LU factorization is simply Gaussian elimina- tion expressed in matrix notation. With this factorization, a general system of equations
Ax = b
becomes a pair of triangular systems
Ly = P b, U x = y.
The diagonal elements of U are called pivots. The kth pivot is the coefficient of the kth variable in the kth equation at the kth step of the elimination. In our 3-by- example, the pivots are 10, 2.5, and 6.2. Both the computation of the multipliers and the back substitution require divisions by the pivots. Consequently, the algorithm cannot be carried out if any of the pivots are zero. Intuition should tell us that it is a bad idea to complete the computation if any of the pivots are nearly zero. To see this, let us change our example slightly to
x 1 x 2 x 3
The (2, 2) element of the matrix has been changed from 2.000 to 2.099, and the right-hand side has also been changed so that the exact answer is still (0, − 1 , 1)T^. Let us assume that the solution is to be computed on a hypothetical machine that does decimal floating-point arithmetic with five significant digits. The first step of the elimination produces
x 1 x 2 x 3
The (2, 2) element is now quite small compared with the other elements in the ma- trix. Nevertheless, let us complete the elimination without using any interchanges. The next step requires adding 2. 5 · 103 times the second equation to the third:
(5 + (2. 5 · 103 )(6))x 3 = (2.5 + (2. 5 · 103 )(6.001)).
On the right-hand side, this involves multiplying 6.001 by 2. 5 · 103. The result is
(5 + 1. 5000 · 104 )x 3 = (2. 5 + 1. 50025 · 104 )
8 Chapter 2. Linear Equations
are lost in roundoff errors. On this hypothetical machine, the last equation becomes
The back substitution begins with
x 3 =
Because the exact answer is x 3 = 1, it does not appear that the error is too serious. Unfortunately, x 2 must be determined from the equation
− 0. 001 x 2 + (6)(0.99993) = 6. 001 ,
which gives
x 2 =
Finally, x 1 is determined from the first equation,
10 x 1 + (−7)(− 1 .5) = 7,
which gives x 1 = − 0. 35.
Instead of (0, − 1 , 1)T^ , we have obtained (− 0. 35 , − 1. 5 , 0 .99993)T^. Where did things go wrong? There was no “accumulation of rounding error” caused by doing thousands of arithmetic operations. The matrix is not close to singular. The difficulty comes from choosing a small pivot at the second step of the elimination. As a result, the multiplier is 2. 5 · 103 , and the final equation involves coefficients that are 10^3 times as large as those in the original problem. Roundoff errors that are small if compared to these large coefficients are unacceptable in terms of the original matrix and the actual solution. We leave it to the reader to verify that if the second and third equations are interchanged, then no large multipliers are necessary and the final result is accurate. This turns out to be true in general: If the multipliers are all less than or equal to one in magnitude, then the computed solution can be proved to be satisfactory. Keeping the multipliers less than one in absolute value can be ensured by a process known as partial pivoting. At the kth step of the forward elimination, the pivot is taken to be the largest (in absolute value) element in the unreduced part of the kth column. The row containing this pivot is interchanged with the kth row to bring the pivot element into the (k, k) position. The same interchanges must be done with the elements of the right-hand side b. The unknowns in x are not reordered because the columns of A are not interchanged.
We have three functions implementing the algorithms discussed in this chapter. The first function, lutx, is a readable version of the built-in Matlab function lu.
10 Chapter 2. Linear Equations
At the kth step of the elimination, i and j are index vectors of length n-k. The operation A(i,k)*A(k,j) multiplies a column vector by a row vector to produce a square, rank one matrix of order n-k. This matrix is then subtracted from the submatrix of the same size in the bottom right corner of A. In a programming language without vector and matrix operations, this update of a portion of A would be done with doubly nested loops on i and j. The second function, bslashtx, is a simplified version of the built-in Matlab backslash operator. It begins by checking for three important special cases: lower triangular, upper triangular, and symmetric positive definite. Linear systems with these properties can be solved in less time than a general system.
function x = bslashtx(A,b) % BSLASHTX Solve linear system (backslash) % x = bslashtx(A,b) solves A*x = b
[n,n] = size(A); if isequal(triu(A,1),zeros(n,n)) % Lower triangular x = forward(A,b); return elseif isequal(tril(A,-1),zeros(n,n)) % Upper triangular x = backsubs(A,b); return elseif isequal(A,A’) [R,fail] = chol(A); if ~fail % Positive definite y = forward(R’,b); x = backsubs(R,y); return end end
If none of the special cases is detected, bslashtx calls lutx to permute and fac- tor the coefficient matrix, then uses the permutation and factors to complete the solution of a linear system.
% Triangular factorization [L,U,p] = lutx(A);
% Permutation and forward elimination y = forward(L,b(p));
% Back substitution x = backsubs(U,y);
2.8. Effect of Roundoff Errors 11
The bslashtx function employs subfunctions to carry out the solution of lower and upper triangular systems.
function x = forward(L,x) % FORWARD. Forward elimination. % For lower triangular L, x = forward(L,b) solves Lx = b. [n,n] = size(L); for k = 1:n j = 1:k-1; x(k) = (x(k) - L(k,j)x(j))/L(k,k); end
function x = backsubs(U,x) % BACKSUBS. Back substitution. % For upper triangular U, x = backsubs(U,b) solves Ux = b. [n,n] = size(U); for k = n:-1: j = k+1:n; x(k) = (x(k) - U(k,j)x(j))/U(k,k); end
A third function, lugui, shows the steps in LU decomposition by Gaussian elimination. It is a version of lutx that allows you to experiment with various pivot selection strategies. At the kth step of the elimination, the largest element in the unreduced portion of the kth column is shown in magenta. This is the element that partial pivoting would ordinarily select as the pivot. You can then choose among four different pivoting strategies:
The chosen pivot is shown in red and the resulting elimination step is taken. As the process proceeds, the emerging columns of L are shown in green and the emerging rows of U in blue.
The rounding errors introduced during the solution of a linear system of equations almost always cause the computed solution—which we now denote by x∗—to differ somewhat from the theoretical solution x = A−^1 b. In fact, if the elements of x
2.8. Effect of Roundoff Errors 13
The residuals are less than 10−^3. We could hardly expect better on a three-digit machine. However, it is easy to see that the exact solution to this system is
x =
So the components of our computed solution actually have the wrong signs; the error is larger than the solution itself. Were the small residuals just a lucky fluke? You should realize that this example is highly contrived. The matrix is very close to being singular and is not typical of most problems encountered in practice. Nevertheless, let us track down the reason for the small residuals. If Gaussian elimination with partial pivoting is carried out for this example on a computer with six or more digits, the forward elimination will produce a system something like (
x 1 x 2
Notice that the sign of U 2 , 2 differs from that obtained with three-digit computation. Now the back substitution produces
x 2 =
x 1 =
the exact answer. On our three-digit machine, x 2 was computed by dividing two quantities, both of which were on the order of rounding errors and one of which did not even have the correct sign. Hence x 2 can turn out to be almost anything. Then this arbitrary value of x 2 was substituted into the first equation to obtain x 1. We can reasonably expect the residual from the first equation to be small— x 1 was computed in such a way as to make this certain. Now comes a subtle but crucial point. We can also expect the residual from the second equation to be small, precisely because the matrix is so close to being singular. The two equations are very nearly multiples of one another, so any pair (x 1 , x 2 ) that nearly satisfies the first equation will also nearly satisfy the second. If the matrix were known to be exactly singular, we would not need the second equation at all—any solution of the first would automatically satisfy the second. In Figure 2.1, the exact solution is marked with a circle and the computed solution with an asterisk. Even though the computed solution is far from the exact intersection, it is close to both lines because they are nearly parallel. Although this example is contrived and atypical, the conclusion we reached is not. It is probably the single most important fact that we have learned about matrix computation since the invention of the digital computer:
Gaussian elimination with partial pivoting is guaranteed to produce small residuals.
14 Chapter 2. Linear Equations
−1.5 −1 −0.5 0 0.5 1 1.5 2
−1.
−
−0.
0
0.
1
1.
Figure 2.1. The computed solution, marked by an asterisk, shows a large error, but a small residual.
Now that we have stated it so strongly, we must make a couple of qualifying remarks. By “guaranteed” we mean it is possible to prove a precise theorem that assumes certain technical details about how the floating-point arithmetic system works and that establishes certain inequalities that the components of the residual must satisfy. If the arithmetic units work some other way or if there is a bug in the particular program, then the “guarantee” is void. Furthermore, by “small” we mean on the order of roundoff error relative to three quantities: the size of the elements of the original coefficient matrix, the size of the elements of the coefficient matrix at intermediate steps of the elimination process, and the size of the elements of the computed solution. If any of these are “large,” then the residual will not necessarily be small in an absolute sense. Finally, even if the residual is small, we have made no claims that the error will be small. The relationship between the size of the residual and the size of the error is determined in part by a quantity known as the condition number of the matrix, which is the subject of the next section.
The coefficients in the matrix and right-hand side of a system of simultaneous linear equations are rarely known exactly. Some systems arise from experiments, and so the coefficients are subject to observational errors. Other systems have coefficients given by formulas that involve roundoff error in their evaluation. Even if the system can be stored exactly in the computer, it is almost inevitable that roundoff errors will be introduced during its solution. It can be shown that roundoff errors in Gaussian elimination have the same effect on the answer as errors in the original
16 Chapter 2. Linear Equations
∥cx∥ = |c|∥x∥ for all scalars c, ∥x + y∥ ≤ ∥x∥ + ∥y∥ (the triangle inequality).
In Matlab, ∥x∥p is computed by norm(x,p), and norm(x) is the same as norm(x,2). For example,
x = (1:4)/ norm1 = norm(x,1) norm2 = norm(x) norminf = norm(x,inf)
produces
x = 0.2000 0.4000 0.6000 0.
norm1 =
norm2 =
norminf =
Multiplication of a vector x by a matrix A results in a new vector Ax that can have a very different norm from x. This change in norm is directly related to the sensitivity we want to measure. The range of the possible change can be expressed by two numbers:
M = max
∥Ax∥ ∥x∥
m = min ∥Ax∥ ∥x∥
The max and min are taken over all nonzero vectors x. Note that if A is singular, then m = 0. The ratio M/m is called the condition number of A:
κ(A) =
max ∥∥Axx∥∥ min ∥∥Axx∥∥
The actual numerical value of κ(A) depends on the vector norm being used, but we are usually only interested in order of magnitude estimates of the condition number, so the particular norm is usually not very important. Consider a system of equations
Ax = b
2.9. Norms and Condition Numbers 17
and a second system obtained by altering the right-hand side:
A(x + δx) = b + δb.
We think of δb as being the error in b and δx as being the resulting error in x, although we need not make any assumptions that the errors are small. Because A(δx) = δb, the definitions of M and m immediately lead to
∥b∥ ≤ M ∥x∥
and ∥δb∥ ≥ m∥δx∥.
Consequently, if m ̸= 0, ∥δx∥ ∥x∥
≤ κ(A) ∥δb∥ ∥b∥
The quantity ∥δb∥/∥b∥ is the relative change in the right-hand side, and the quantity ∥δx∥/∥x∥ is the relative error caused by this change. The advantage of using relative changes is that they are dimensionless, that is, they are not affected by overall scale factors. This shows that the condition number is a relative error magnification factor. Changes in the right-hand side can cause changes κ(A) times as large in the solution. It turns out that the same is true of changes in the coefficient matrix itself. The condition number is also a measure of nearness to singularity. Although we have not yet developed the mathematical tools necessary to make the idea pre- cise, the condition number can be thought of as the reciprocal of the relative distance from the matrix to the set of singular matrices. So, if κ(A) is large, A is close to singular. Some of the basic properties of the condition number are easily derived. Clearly, M ≥ m, and so κ(A) ≥ 1.
If P is a permutation matrix, then the components of P x are simply a rearrangement of the components of x. It follows that ∥P x∥ = ∥x∥ for all x, and so
κ(P ) = 1.
In particular, κ(I) = 1. If A is multiplied by a scalar c, then M and m are both multiplied by the same scalar, and so
κ(cA) = κ(A).
If D is a diagonal matrix, then
κ(D) =
max |dii| min |dii|
These last two properties are two of the reasons that κ(A) is a better measure of nearness to singularity than the determinant of A. As an extreme example, consider
2.9. Norms and Condition Numbers 19
obtain the solutions is irrelevant. The example is constructed to have a fairly large condition number so that the effect of changes in b is quite pronounced, but similar behavior can be expected in any problem with a large condition number. The condition number also plays a fundamental role in the analysis of the roundoff errors introduced during the solution by Gaussian elimination. Let us assume that A and b have elements that are exact floating-point numbers, and let x∗ be the vector of floating-point numbers obtained from a linear equation solver such as the function we shall present in the next section. We also assume that exact singularity is not detected and that there are no underflows or overflows. Then it is possible to establish the following inequalities:
∥b − Ax∗∥ ∥A∥∥x∗∥
≤ ρϵ,
∥x − x∗∥ ∥x∗∥
≤ ρκ(A)ϵ.
Here ϵ is the relative machine precision eps and ρ is defined more carefully later, but it usually has a value no larger than about 10. The first inequality says that the relative residual can usually be expected to be about the size of roundoff error, no matter how badly conditioned the matrix is. This was illustrated by the example in the previous section. The second inequality requires that A be nonsingular and involves the exact solution x. It follows directly from the first inequality and the definition of κ(A) and says that the relative error will also be small if κ(A) is small but might be quite large if the matrix is nearly singular. In the extreme case where A is singular but the singularity is not detected, the first inequality still holds but the second has no meaning. To be more precise about the quantity ρ, it is necessary to introduce the idea of a matrix norm and establish some further inequalities. Readers who are not interested in such details can skip the remainder of this section. The quantity M defined earlier is known as the norm of the matrix. The notation for the matrix norm is the same as for the vector norm:
∥A∥ = max
∥Ax∥ ∥x∥
It is not hard to see that ∥A−^1 ∥ = 1/m, so an equivalent definition of the condition number is κ(A) = ∥A∥∥A−^1 ∥. Again, the actual numerical values of the matrix norm and condition number depend on the underlying vector norm. It is easy to compute the matrix norms corresponding to the l 1 and l∞ vector norms. In fact, it is not hard to show that
∥A∥ 1 = max j
i
|ai,j |,
∥A∥∞ = max i
j
|ai,j |.
20 Chapter 2. Linear Equations
Computing the matrix norm corresponding to the l 2 vector norm involves the sin- gular value decomposition (SVD), which is discussed in a later chapter. Matlab computes matrix norms with norm(A,p) for p = 1, 2, or inf. The basic result in the study of roundoff error in Gaussian elimination is due to J. H. Wilkinson. He proved that the computed solution x∗ exactly satisfies
(A + E)x∗ = b,
where E is a matrix whose elements are about the size of roundoff errors in the elements of A. There are some rare situations where the intermediate matrices obtained during Gaussian elimination have elements that are larger than those of A, and there is some effect from accumulation of rounding errors in large matrices, but it can be expected that if ρ is defined by
∥E∥ ∥A∥
= ρϵ,
then ρ will rarely be bigger than about 10. From this basic result, we can immediately derive inequalities involving the residual and the error in the computed solution. The residual is given by
b − Ax∗ = Ex∗,
and hence ∥b − Ax∗∥ = ∥Ex∗∥ ≤ ∥E∥∥x∗∥.
The residual involves the product Ax∗, so it is appropriate to consider the relative residual, which compares the norm of b − Ax to the norms of A and x∗. It follows directly from the above inequalities that
∥b − Ax∗∥ ∥A∥∥x∗∥
≤ ρϵ.
If A is nonsingular, the error can be expressed using the inverse of A by
x − x∗ = A−^1 (b − Ax∗),
and so ∥x − x∗∥ ≤ ∥A−^1 ∥∥E∥∥x∗∥.
It is simplest to compare the norm of the error with the norm of the computed solution. Thus the relative error satisfies
∥x − x∗∥ ∥x∗∥
≤ ρ∥A∥∥A−^1 ∥ϵ.
Hence ∥x − x∗∥ ∥x∗∥ ≤ ρκ(A)ϵ.