Chapter3
Chapter3
Linear systems of equations are ubiquitous in scientific computing – they arise when solving problems
in many applications, including biology, chemistry, physics, engineering and economics, and they
appear in nearly every chapter of this book. A fundamental numerical problem involving linear
systems is that of finding a solution (if one exists) to a set of n linear equations in n unknowns.
The first digital computers (developed in the 1940’s primarily for scientific computing problems)
required about an hour to solve linear systems involving only 10 equations in 10 unknowns. Modern
computers are substantially more powerful, and we can now solve linear systems involving thousands
of equations in thousands of unknowns in a fraction of a second. Indeed, many problems in science
and industry involve millions of equations in millions of unknowns. In this chapter we study the
most commonly used algorithm, Gaussian elimination with partial pivoting, to solve these important
problems.
After a brief introduction to linear systems, we discuss computational techniques for problems
(diagonal, lower and upper triangular) that are simple to solve. We then describe Gaussian elimina-
tion with partial pivoting, which is an algorithm that reduces a general linear system to one that is
simple to solve. Important matrix factorizations associated with Gaussian elimination are described,
and issues regarding accuracy of computed solutions are discussed. The chapter ends with a section
describing Matlab implementations, as well as the main tools provided by Matlab for solving
linear systems.
53
54 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS
and recall from Section 1.2 that the vector on the left side of the equal sign can be written as a
linear combination of column vectors, or equivalently as a matrix–vector product:
2 3 2 3 2 3 2 32 3 2 3
a1,1 a1,2 a1,n a1,1 a1,2 · · · a1,n x1 b1
6 a2,1 7 6 a2,2 7 6 a2,n 7 6 a2,1 a2,2 · · · a2,n 7 6 x2 7 6 b2 7
6 7 6 7 6 7 6 76 7 6 7
6 .. 7 x1 + 6 .. 7 x2 + · · · + 6 .. 7 xn = 6 .. .. .. .. 7 6 .. 7 = 6 .. 7
4 . 5 4 . 5 4 . 5 4 . . . . 5 4 . 5 4 . 5
an,1 an,2 an,n an,1 an,2 · · · an,n xn bn
Thus, in matrix–vector notation, the linear system is represented as
Ax = b
where
2 3
a1,1 a1,2 ··· a1,n
6 a2,1 a2,2 ··· a2,n 7
6 7
A=6 . .. .. .. 7 is the coefficient matrix of order n,
4 .. . . . 5
an,1 an,2 ··· an,n
2 3
x1
6 x2 7
6 7
x=6 . 7 is the unknown, or solution vector of length n, and
4 .. 5
xn
2 3
b1
6 b2 7
6 7
b=6.7 is the right hand side vector of length n.
4 .. 5
bn
We consider problems where the coefficients ai,j and the right hand side values bi are real numbers.
A solution of the linear system Ax = b of order n is a vector x that satisfies the equation Ax = b.
The solution set of a linear system is the set of all its solutions.
Example 3.1.1. The linear system of equations
1x1 + 1x2 + 1x3 = 3
1x1 + ( 1)x2 + 4x3 = 4
2x1 + 3x2 + ( 5)x3 = 0
is of order n = 3 with unknowns x1 , x2 and x3 . The matrix–vector form is Ax = b where
2 3 2 3 2 3
1 1 1 x1 3
A=4 1 1 4 5 , x = 4 x2 5 , b = 4 4 5
2 3 5 x3 0
This linear system has precisely one solution, given by x1 = 1, x2 = 1 and x3 = 1, so
2 3
1
x=4 1 5
1
Linear systems arising in most realistic applications are usually much larger than the order n = 3
system of the previous example. However, a lot can be learned about linear systems by looking at
small problems. Consider, for example, the 2 ⇥ 2 linear system:
a1,1 a1,2 x1 b1 a1,1 x1 + a1,2 x2 = b1
= ,
a2,1 a2,2 x2 b2 a2,1 x1 + a2,2 x2 = b2 .
3.1. LINEAR SYSTEMS 55
3 3 3
2 2 2
1 1 1
0 0 0
0 0.5 1 1.5 2 2.5 3 3.5 4 0 0.5 1 1.5 2 2.5 3 3.5 4 0 0.5 1 1.5 2 2.5 3 3.5 4
Figure 3.1: Possible solution sets for a general 2 ⇥ 2 linear system. The left plot shows two lines that
intersect at only one point (unique solution), the middle plot shows two parallel lines that do not
intersect at any points (no solution), and the right plot shows two identical lines (infinitely many
solutions).
This conclusion holds for linear systems of any order; that is, a linear system of order n has
either no solution, 1 solution, or an infinite number of distinct solutions. A linear system
Ax = b of order n is nonsingular if it has one and only one solution. A linear system is singular if
it has either no solution or an infinite number of distinct solutions; which of these two possibilities
applies depends on the relationship between the matrix A and the right hand side vector b, a matter
that is considered in a first linear algebra course.
Whether a linear system Ax = b is singular or nonsingular depends solely on properties of its
coefficient matrix A. In particular, the linear system Ax = b is nonsingular if and only if the matrix
A is invertible; that is, if and only if there is a matrix, A 1 , such that AA 1 = A 1 A = I, where I
is the identity matrix, 2 3
1 0 ··· 0
6 . 7
6 0 1 . . . .. 7
I=6 6 . .
7.
7
4 .. .. ... 0 5
0 ··· 0 1
So, we say A is nonsingular (invertible) if the linear system Ax = b is nonsingular, and A is singular
(non-invertible) if the linear system Ax = b is singular. It is not always easy to determine, a-priori,
whether or not a matrix is singular especially in the presence of roundo↵ errors. This point will be
addressed in Section 3.6.
Example 3.1.2. Consider the linear systems of order 2:
1 2 x1 5 x2 = 12 x1 + 52 x2 = 12 x1 + 52
(a) = ) )
3 4 x2 6 x2 = 34 x1 + 64 x2 = 34 x1 + 32
This linear system consists of two lines with unequal slopes. Thus the lines intersect at only
one point, and the linear system has a unique solution.
56 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS
1 5 1 5
1 2 x1 5 x2 = 2 x1 + 2 x2 = 2 x1 + 2
(b) = ) 3 6
) 1
3 6 x2 6 x2 = 6 x1
+ 6 x2 = 2 x1 + 1
This linear system consists of two lines with equal slopes. Thus the lines are parallel. Since
the intercepts are not identical, the lines do not intersect at any points, and the linear system
has no solution.
1 2 x1 5 x2 = 12 x1 + 52 x2 = 12 x1 + 52
(c) = ) )
3 6 x2 15 x2 = 36 x1 + 156 x2 = 12 x1 + 52
This linear system consists of two lines with equal slopes. Thus the lines are parallel. Since
the intercepts are also equal, the lines are identical, and the linear system has infinitely many
solutions.
Problem 3.1.1. Consider the linear system of Example 3.1.1. If the coefficient matrix A
remains unchanged,
2 3 then what choice of right hand side vector b would lead to the solution
1
vector x = 4 2 5?
3
Problem 3.1.2. Consider any linear system of equations of order 3. The solution of each
equation can be portrayed as a plane in a 3-dimensional space. Describe geometrically how
3 planes can intersect in a 3-dimensional space. Why must two planes that intersect at two
distinct points intersect at an infinite number of points? Explain how you would conclude
that if a linear system of order 3 has 2 distinct solutions it must have an infinite number of
distinct solutions.
Problem 3.1.3. Give an example of one equation in one unknown that has no solution.
Give another example of one equation in one unknown that has precisely one solution, and
another example of one equation in one unknown that has an infinite number of solutions.
Problem 3.1.4. The determinant of an n⇥n matrix, det(A), is a number that theoretically
can be used computationally to indicate if a matrix is singular. Specifically, if det(A) 6=
0 then A is nonsingular. The formula to compute det(A) for a general n ⇥ n matrix is
complicated, but there are some special cases where it can be computed fairly easily. In the
case of a 2 ⇥ 2 matrix, ✓ ◆
a b
det = ad bc.
c d
Compute the determinants of the 2 ⇥ 2 matrices in Example 3.1.2. Why do these results
make sense?
Important note: The determinant is a good theoretical test for singularity, but it is not a
practical test in computational problems. This is discussed in more detail in Section 3.6.
(c) the strictly upper triangular entries, i.e., the entries ai,j for which i < j.
3.2. SIMPLY SOLVED LINEAR SYSTEMS 57
2 32 3 2 3
( 1)x1 + 0x2 + 0x3 = 1 1 0 0 x1 1
(a) 0x1 + 3x2 + 0x3 = 6 , 4 0 3 0 5 4 x2 5 = 4 6 5
0x1 + 0x2 + ( 5)x3 = 15 0 0 5 x3 15
2 32 3 2 3
( 1)x1 + 0x2 + 0x3 = 1 1 0 0 x1 1
(b) 2x1 + 3x2 + 0x3 = 8 , 4 2 3 0 5 4 x2 5 = 4 8 5
( 1)x1 + 4x2 + ( 5)x3 = 8 1 4 5 x3 8
2 32 3 2 3
( 1)x1 + 2x2 + ( 1)x3 = 0 1 2 1 x1 0
(c) 0x1 + 3x2 + 6x3 = 24 , 4 0 3 6 5 4 x2 5 = 4 24 5
0x1 + 0x2 + ( 5)x3 = 15 0 0 5 x3 15
The locations of the diagonal, strictly lower triangular, and strictly upper triangular entries of A
are illustrated in Fig. 3.3. The lower triangular entries are composed of the strictly lower triangular
and diagonal entries, as illustrated in Fig. 3.4. Similarly, the upper triangular entries are composed
of the strictly upper triangular and diagonal entries. Using this terminology, we say that the linear
system in Fig. 3.2(a) is diagonal, the linear system in Fig. 3.2(b) is lower triangular, and the linear
system in Fig. 3.2(c) is upper triangular.
(s
a21 tri
ctl
yu
pp
er
tria
ng
(d ular
(s iago
tri pa
ctl na rt)
yl lp
ow ar
er t)
tria
ng
ula
rp
ar
t) a
n−1,n
Problem 3.2.1. Let A be a matrix of order n. Show that A has n2 entries, that n2 n =
n(n 1) entries lie o↵ the diagonal, and that each strictly triangular portion of A has
n(n 1)
entries.
2
58 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS
a a a a a12 a
11 12 1n 11 1n
Figure 3.4: Illustration of the lower triangular and upper triangular parts of a matrix.
1 6 15
Example 3.2.1. In Fig. 3.2(a) the solution is x1 = ( 1) = 1, x2 = 3 = 2 and x3 = ( 5) = 3.
Example 3.2.2. Consider the following singular diagonal matrix, A, and the vectors b and d:
2 3 2 3 2 3
3 0 0 1 1
A=4 0 0 0 5, b = 4 1 5, d = 4 0 5.
0 0 2 0 1
(a) The linear system Ax = b has no solution. Although we can solve the first and last equations
to get x1 = 13 and x3 = 0, it is not possible to solve the second equation:
1
0 · x1 + 0 · x2 + 0 · x3 = 1 ) 0· + 0 · x2 + 0 · 0 = 1 ) 0 · x2 = 1.
3
Clearly there is no value of x2 that satisfies the equation 0 · x2 = 1.
(b) The linear system Ax = d has infinitely many solutions. From the first and last equations we
obtain x1 = 13 and x3 = 12 . The second equation is
1 1
0 · x1 + 0 · x2 + 0 · x3 = 0 ) 0· + 0 · x2 + 0 · = 0 ) 0 · x2 = 0,
3 2
and thus x2 can be any real number.
3.2. SIMPLY SOLVED LINEAR SYSTEMS 59
Why is this matrix singular? Find a vector b so that the linear system Ax = b has no
solution. Find a vector b so that the linear system Ax = b has infinitely many solutions.
1 8 2x1 8 2·1
Example 3.2.3. In Fig. 3.2(b) the solution is x1 = ( 1) = 1, x2 = 3 = 3 = 2 and
8 ( 1)x1 4x2 8 ( 1)·1 4·2
x3 = ( 5) = ( 5) = 3.
To write a computer code to implement forward substitution, we must formulate the process
in a systematic way. To motivate the two most “popular” approaches to implementing forward
substitution, consider the following lower triangular linear system.
a1,1 x1 = b1
a2,1 x1 + a2,2 x2 = b2
a3,1 x1 + a3,2 x2 + a3,3 x3 = b3
By transferring the terms involving the o↵-diagonal entries of A to the right hand side we obtain
a1,1 x1 = b1
a2,2 x2 = b2 a2,1 x1
a3,3 x3 = b3 a3,1 x1 a3,2 x2
60 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS
The right hand sides can be divided naturally into rows and columns.
• Row–oriented forward substitution updates (modifies) the right hand side one row at a time.
That is, after computing x1 , x2 , . . . , xi 1 , we update bi as:
i 1
X
bi := bi (ai,1 x1 + ai,2 x2 + · · · + ai,i 1 xi 1 ) = bi ai,j xj
j=1
The symbol “:=” means assign the value computed on the right hand side to the variable on
the left hand side. Here, and later, when the lower limit of the sum exceeds the upper limit
the sum is P
considered to be “empty”. In this process the variable bi is “overwritten” with the
i 1
value bi j=1 ai,j xj . With this procedure to update the right hand side, an algorithm for
row-oriented forward substitution could have the form:
for each i = 1, 2, . . . , n
Pi 1
update bi := bi j=1 ai,j xj
compute xi := bi /ai,i
Notice that each update step uses elements in the ith row of A, ai,1 , ai,2 , . . . , ai,i 1.
• Column–oriented forward substitution updates (modifies) the right hand side one column at a
time. That is, after computing xj , we update bj+1 , bj+2 , . . . , bn as:
With this procedure, an algorithm for column-oriented forward substitution could have the
form:
for each j = 1, 2, . . . , n
compute xj := bj /aj,j
update bi := bi ai,j xj , i = j + 1, j + 2, . . . , n
Notice in this case the update steps use elements in the jth column of A, aj+1,j , aj+2,j , . . . , an,j .
Pseudocodes implementing row– and column–oriented forward substitution are presented in Fig. 3.5.
Note that:
• We again use the symbol “:=” to assign values to variables.
• The “for” loops step in ones. So, “for i = 1 to n means execute the loop for each value i = 1,
i = 2, until i = n, in turn. (Later, in Fig. 3.6 we use “downto” when we want a loop to count
backwards in ones.)
• When a loop counting forward has the form, for example, “for j = 1 to i 1” and for a given
value of i we have i 1 < 1 then the loop is considered empty and does not execute. A similar
convention applies for empty loops counting backwards.
• The algorithms destroy the original entries of b. If these entries are needed for later calculations,
they must be saved elsewhere. In some implementations, the entries of x are written over the
corresponding entries of b.
3.2. SIMPLY SOLVED LINEAR SYSTEMS 61
Row–Oriented Column–Oriented
for i = 1 to n for j = 1 to n
for j = 1 to i 1 xj := bj /aj,j
bi := bi ai,j xj for i = j + 1 to n
next j bi := bi ai,j xj
xi := bi /ai,i next i
next i next j
Figure 3.5: Pseudocode for Row– and Column–Oriented Forward Substitution. Note that the algo-
rithm assumes that the matrix A is lower triangular.
The forward substitution process can break down if a diagonal entry of the lower triangular
matrix is zero. In this case, the lower triangular matrix is singular, and a linear system involving
such a matrix may have no solution or infinitely many solutions.
Problem 3.2.4. Illustrate the operation of column–oriented forward substitution when used
to solve the lower triangular linear system in Fig. 3.2(b). [Hint: Show the value of b each
time it has been modified by the for-loop and the value of each entry of x as it is computed.]
Problem 3.2.5. Use row–oriented forward substitution to solve the linear system:
Problem 3.2.6. Repeat Problem 3.2.5 but using the column–oriented version.
Problem 3.2.7. Modify the row– and the column–oriented pseudocodes in Fig. 3.5 so that
the solution x is written over the right hand side b.
Problem 3.2.8. Consider a general lower triangular linear system of order n. Show that
n(n 1) n(n 1)
row–oriented forward substitution costs multiplications, subtractions, and
2 2
Pn n(n + 1)
n divisions. [Hint: i=1 i = .]
2
Problem 3.2.9. Repeat Problem 3.2.8 for the column–oriented version of forward substi-
tution.
Problem 3.2.10. Develop pseudocodes, analogous to those in Fig. 3.5, for row–oriented
and column–oriented methods of solving the following linear system of order 3
Why is A singular? For what values c does the linear system Ax = b have no solution? For
what values c does the linear system Ax = b have infinitely many solutions?
15 24 6x3 24 6·3
Example 3.2.4. In the case in Fig. 3.2(c) the solution is x3 = ( 5) = 3, x2 = 3 = 3 =2
0 ( 1)x3 2x2 0 ( 1)·3 2·2
and x1 = ( 1) = ( 1) = 1.
As with forward substitution, there are two popular implementations of backward substitution.
To motivate these implementations, consider the following upper triangular linear system.
By transferring the terms involving the o↵-diagonal entries of A to the right hand side we obtain
The right hand sides of these equations can be divided naturally into rows and columns.
• Row–oriented backward substitution updates (modifies) the right hand side one row at a time.
That is, after computing xn , xn 1 , . . . , xi+1 , we update bi as:
n
X
bi := bi (ai,i+1 xi+1 + ai,i+2 xi+2 + · · · + ai,n xn ) = bi ai,j xj
j=i+1
Pn
In this case the variable bi is “overwritten” with the value bi j=i+1 ai,j xj . With this
procedure to update the right hand side, an algorithm for row-oriented backward substitution
could have the form:
for each i = n, n 1, . . . , 1
Pn
update bi := bi j=i+1 ai,j xj
compute xi := bi /ai,i
Notice that each update step uses elements in the ith row of A, ai,i+1 , ai,i+2 , . . . , ai,n .
3.2. SIMPLY SOLVED LINEAR SYSTEMS 63
• Column–oriented backward substitution updates (modifies) the right hand side one column at
a time. That is, after computing xj , we update b1 , b2 , . . . , bj 1 as:
b1 := b1 a1,j xj
b2 := b2 a2,j xj
..
.
bj 1 := bj 1 aj 1,j xj
With this procedure, an algorithm for column-oriented backward substitution could have the
form:
for each j = n, n 1, . . . , 1
compute xj := bj /aj,j
update bi := bi ai,j xj , i = 1, 2, . . . , j 1
Notice in this case the update steps use elements in the jth column of A, a1,j , a2,j , . . . , aj 1,j .
Pseudocodes implementing row– and column–oriented backward substitution are presented in Fig. 3.6.
Row–Oriented Column–Oriented
Figure 3.6: Pseudocode Row– and Column–Oriented Backward Substitution. Note that the algorithm
assumes that the matrix A is upper triangular.
The backward substitution process can break down if a diagonal entry of the upper triangular
matrix is zero. In this case, the upper triangular matrix is singular, and a linear system involving
such a matrix may have no solution or infinitely many solutions.
Problem 3.2.14. Use row–oriented backward substitution to solve the linear system:
Problem 3.2.15. Repeat Problem 3.2.14 using the column–oriented backward substitution.
Problem 3.2.16. Modify the row– and column–oriented pseudocodes for backward substi-
tution so that the solution x is written over b.
Problem 3.2.17. Consider a general upper triangular linear system of order n. Show that
n(n 1) n(n 1)
row–oriented backward substitution costs multiplications, subtractions,
2 2
and n divisions.
Problem 3.2.18. Repeat Problem 3.2.17 using column–oriented backward substitution.
Problem 3.2.19. Develop pseudocodes, analogous to those in Fig. 3.6, for row– and
column–oriented methods of solving the following linear system:
a1,3 x3 = b1
a2,2 x2 + a2,3 x3 = b2
a3,1 x1 + a3,2 x2 + a3,3 x3 = b3
Why is A singular? For what values c does the linear system Ax = b have no solution? For
what values c does the linear system Ax = b have infinitely many solutions?
Problem 3.2.21. The determinant of a triangular matrix, be it diagonal, lower triangular,
or upper triangular, is the product of its diagonal entries.
(a) Show that a triangular matrix is nonsingular if and only if each of its diagonal entries
is nonzero.
(b) Compute the determinant of each of the triangular matrices in Problems 3.2.5, 3.2.11,
3.2.14 and 3.2.20.
Why do these results make sense computationally?
This process continues until the last equation involves only one unknown variable, and the
transformed linear system is in upper triangular form. The last stage of the algorithm simply involves
identifying the coefficient in the last equation as the final pivot element. In our simple example, we
are done at stage 3, where we identify the last pivot element by underlining the coefficient for x3 in
66 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS
the last equation, and obtain the upper triangular linear system
( 3) x1 + 1x2 + 2x3 = 3
7 1
3 x2 + ( 3 )x3 = 1
16 64
7 x3 = 7
GEPP is now finished. When the entries on the diagonal of the final upper triangular linear
system are nonzero, as it is in our illustrative example, the linear system is nonsingular and its
solution may be determined by backward substitution. (Recommendation: If you are determining
a solution by hand in exact arithmetic, you may check that your computed solution is correct by
showing that it satisfies all the equations of the original linear system. If you are determining the
solution approximately this check may be unreliable as we will see later.)
The diagonal entries of the upper triangular linear system produced by GEPP play an important
role. Specifically, the k th diagonal entry, i.e., the coefficient of xk in the k th equation, is the pivot
for the k th stage of GEPP. So, the upper triangular linear system produced by GEPP is nonsingular
if and only if each stage of GEPP has a pivot.
To summarize:
• GEPP can always be used to transform a linear system of order n into an upper triangular
linear system with the same solution set.
• The k th stage of GEPP begins with a linear system that involves n k + 1 equations in the
n k + 1 unknowns xk , xk+1 , · · · , xn . The k th stage of GEPP eliminates xk and ends with a
linear system that involves n k equations in the n k unknowns xk+1 , xk+2 , · · · , xn .
• If GEPP finds a non-zero pivot at every stage, then the final upper triangular linear system
is nonsingular. The solution of the original linear system can be determined by applying
backward substitution to this upper triangular linear system.
• If GEPP fails to find a non-zero pivot at some stage, then the linear system is singular and
the original linear system either has no solution or an infinite number of solutions.
We emphasize that if GEPP does not find a non-zero pivot at some stage, we can
conclude immediately that the original linear system is singular. Consequently, many
GEPP codes simply terminate elimination and return a message that indicates the original linear
system is singular.
Example 3.3.1. The previous discussion showed that GEPP transforms the linear system to upper
triangular form:
Using backward substitution, we find that the solution of this linear system is given by:
64 1
7 1+ 3 ·4 3 1·1 2·4
x3 = 16 = 4, x2 = 7 = 1, x1 = = 2.
7 3
3
4 x1 + 6x2 + ( 10)x3 = 0
In the final stage we identify the final pivot en- 0x1 + ( 2.5) x2 + 6.5x3 = 4
try, and observe that all pivots are non-zero, and 0x1 + 0x2 + 4.4 x3 = 4.4
thus the linear system is nonsingular and there is
a unique solution.
Problem 3.3.1. Use GEPP followed by backward substitution to solve the linear system of
Example 3.1.1. Explicitly display the value of each pivot and each multiplier.
68 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS
Problem 3.3.2. Use GEPP followed by backward substitution to solve the following linear
system. Explicitly display the value of each pivot and multiplier.
Problem 3.3.3. Use GEPP and backward substitution to solve the following linear system.
Explicitly display the value of each pivot and multiplier.
2x1 + x3 = 1
x2 + 4x3 = 3
x1 + 2x2 = 2
Problem 3.3.4. GEPP provides an efficient way to compute the determinant of any matrix.
Recall that the operations used by GEPP are (1) exchange two equations, and (2) subtract a
multiple of one equation from another (di↵erent) equation. Of these two operations, only the
exchange operation changes the value of the determinant of the matrix of coefficients, and
then it only changes its sign. In particular, suppose GEPP transforms the coefficient matrix
A into the upper triangular coefficient matrix U using m actual exchanges, i.e., exchange
steps where an exchange of equations actually occurs. Then
Recall from Problem 3.2.21 that the determinant of a triangular matrix is the product
of its diagonal entries.
(b) Use GEPP to calculate the determinant of each of the matrices:
2 3
2 3 1 1 1 1
1 1 1 6
4 2 6 2 3 4 5 7
7
1 2 5 and 4 1 2 2 1 5
4 3 0
2 6 3 7
Performing the interchange, calculating the multipliers m21 = 0.6667 and m31 = 0.3333, and
eliminating x1 from the second and third equations we have
Performing the interchange, calculating the multiplier m32 = 0.1429, and eliminating x2 from the
third equation we have
Backsolving we have x3 = 4.001, x2 = 1.000 and x1 = 2.001, a rounding error level perturbation of
the answer with exact arithmetic.
If we consider now Example 3.3.2, we see that all the working was exact in four significant digit
decimal arithmetic. So, we will get precisely the same answers as before. However, we cannot
represent exactly the arithmetic in this example in binary DP arithmetic. So, computationally, in
say Matlab, we would obtain an answer that di↵ered from the exact answer at the level of roundo↵
error.
Consider next the problem in Example 3.3.3:
Performing the interchange, calculating the multipliers m21 = 0.3333 and m31 = 0.3333, and
eliminating x1 from the second and third equations we have
That is, roundo↵ does not a↵ect the result and we still observe that the pivot for the next stage is
zero hence we must stop.
If, instead, we scale the second equation to give
then the first step of GEPP is to interchange the first and second equations to give
1 3
Next, we calculate the multipliers m21 = = 0.1429 and m31 = = 0.4286. Eliminating
( 7) ( 7)
we compute
( 7)x1 + 14x2 + ( 7)x3 = 7
0x1 + (0.001)x2 + ( 2)x3 = 3
0x1 + 0x2 + 6x3 = 3
Observe that the elimination is now complete. The result di↵ers from the exact arithmetic results
in just the (2, 2) position but this small change is crucial because the resulting system is now
nonsingular. When we compute a solution, we obtain, x3 = 0.5, x2 = 4000 and x1 = 8000; the
70 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS
large values possibly giving away that something is wrong. In the next subsection we describe a
GEPP algorithm. This algorithm includes a test for singularity that would flag the above problem
as singular even though all the pivots are nonzero.
Example 3.3.4. Here is a further example designed to reinforce what we have just seen on the
e↵ects of inexact arithmetic. Using exact arithmetic, 2 stages of GEPP transforms the singular
linear system:
1x1 + 1x2 + 1x3 = 1
1x1 + ( 1)x2 + 2x3 = 2
3x1 + 1x2 + 4x3 = 4
into the triangular form:
3x1 + 1x2 + 4x3 = 4
0x1 + ( 4/3)x2 + 2/3x3 = 2/3
0x1 + 0x2 + 0x3 = 0
which has an infinite number of solutions.
Using GEPP with four significant digit decimal arithmetic, we compute multipliers m21 = m31 =
0.3333 and eliminating we get
and backsolving we compute x3 = 1 and x2 = x1 = 0, one solution of the original linear system.
Here, we observe some mild e↵ects of loss of significance due to cancellation. Later, in Section 3.3.4,
we will see much greater numerical errors due to loss of significance.
• for stages k = 1, 2, . . . , n 1
- find the kth pivot
- if the kth pivot is zero, quit – the linear system is singular
- perform row interchanges, if needed
- compute the multipliers
- perform the elimination
• for stage n, if the final pivot is zero, quit – the linear system is singular
• perform backward substitution
Before presenting pseudocode for the entire algorithm, we consider implementation details for some
of the GEPP steps. Assume that we are given the coefficient matrix A and right hand side vector b
of the linear system.
• To find the kth pivot, we need to find the largest of the values |ak,k |, |ak+1,k |, . . . , |an,k |. This
can be done using a simple search:
p := k
for i = k + 1 to n
if |ai,k | > |ap,k | then p := i
next i
• If the above search finds that p > k, then we need to exchange rows. This is fairly simple,
though we have to be careful to use “temporary” storage when overwriting the coefficients. (In
some languages, such as Matlab, you can perform the interchanges directly, as we will show
in Section 3.7; the temporary storage is created and used in the background and is invisible
to the user.)
if p > k then
for j = k to n
temp := ak,j
ak,j := ap,j
ap,j :=temp
next j
temp := bk
bk := bp
bp :=temp
endif
Notice that when exchanging equations, we must exchange the coefficients in the matrix A and
the corresponding values in the right hand side vector b. (Another possibility is to perform the
interchanges virtually; that is, to rewrite the algorithm so that we don’t physically interchange
elements but instead leave them in place but act as if they had been interchanged. This
involves using indirect addressing which may be more efficient than physically performing
interchanges.)
• Computing the multipliers, mi,k , and performing the elimination step can be implemented as:
for i = k + 1 to n
mi,k := ai,k /ak,k
ai,k := 0
for j = k + 1 to n
ai,j := ai,j mi,k ⇤ ak,j
next j
bi := bi mi,k ⇤ bk
next i
Since we know that ai,k should be zero after the elimination step, we do not perform the
elimination step on these values, and instead just set them to zero using the instruction ai,k :=
72 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS
0. However, we note that since these elements are not involved in determining the solution via
backward substitution, this instruction is unnecessary, and has only a “cosmetic” purpose.
Putting these steps together, and including pseudocode for backward substitution, we obtain the
pseudocode shown in Fig. 3.7. Note that we implemented the elimination step to update ai,j in a
specific order. In particular, the entries in row k + 1 are updated first, followed by the entries in
row k + 2, and so on, until the entries in row n are updated. As with the forward and backward
substitution methods, we refer to this as row-oriented GEPP, and to be consistent we combine it with
row-oriented backward substitution. It is not difficult to modify the GEPP procedure so that it is
column-oriented, and in that case we would combine it with column-oriented backward substitution.
During stage k, the pseudocode in Fig. 3.7 only modifies equations (k + 1) through n and the
coefficients multiplying xk , xk+1 , · · · , xn . Of the three fundamental operations: add (or subtract),
multiply, and divide, generally add (or subtract) is fastest, multiply is intermediate, and divide
is slowest. So, rather than compute the multiplier as mi,k := ai,k /ak,k , one might compute the
reciprocal ak,k := 1/ak,k outside the i loop and then compute the multiplier as mi,k := ai,k ak,k . In
stage k, this would replace n k divisions with 1 division followed by n k multiplications, which is
usually faster. Keeping the reciprocal in ak,k also helps in backward substitution where each divide
is replaced by a multiply.
Problem 3.3.6. In Fig. 3.7, in the second for-j loop (the elimination loop) the code could
be logically simplified by recognizing that the e↵ect of ai,k = 0 could be achieved by removing
this statement and extending the for-j loop so it starts at j = k instead of j = k + 1. Why
is this NOT a good idea?
Problem 3.3.7. In Fig. 3.7, the elimination step is row–oriented. Change the elimination
step so it is column–oriented. Hint: You must exchange the order of the for-i and for-j
loops.
Problem 3.3.8. In Fig. 3.7, show that the elimination step of the k th stage of GEPP
requires n k divisions to compute the multipliers, and (n k)2 + n k multiplications
and subtractions to perform the elimination. Conclude that the elimination steps of GEPP
requires about
Pn 1 Pn 1 n(n 1) n2
k=1 (n k) = k=1 k = ⇠ divisions
2 23
Pn 1 2
Pn 1 2 n(n 1)(2n 1) n
k=1 (n k) = k=1 k = ⇠ multiplications and subtractions
6 3
f (n)
The notation f (n) ⇠ g(n) is used when the ratio approaches 1 as n becomes large.
g(n)
Problem 3.3.9. How does the result in Problem 3.3.8 change if we first compute the recip-
rocals of the pivots and then use these values in computing the multipliers?
Problem 3.3.10. How many comparisons does GEPP use to find pivot elements?
Problem 3.3.11. How many assignment statements are needed for all the interchanges in
the GEPP algorithm, assuming that at each stage an interchange is required?
for k = 1 to n 1
p := k
for i = k + 1 to n
if |ai,k | > |ap,k | then p := i
next i
if p > k then
for j = k to n
temp := ak,j ; ak,j := ap,j ; ap,j := temp
next j
temp := bk ; bk := bp ; bp := temp
endif
if ak,k = 0 then return (linear system is singular)
for i = k + 1 to n
mi,k := ai,k /ak,k
ai,k := 0
for j = k + 1 to n
ai,j := ai,j mi,k ⇤ ak,j
next j
bi := bi mi,k ⇤ bk
next i
next k
if an,n = 0 then return (linear system is singular)
for i = n downto 1 do
for j = i + 1 to n
bi := bi ai,j ⇤ xj
next j
xi := bi /ai,i
next i
Figure 3.7: Pseudocode for row oriented GEPP and Backward Substitution
74 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS
0.000025x1 + 1x2 = 1
0x1 + ( 40000)x2 = 40000
Backward substitution commits no further rounding errors and produces the approximate solution
40000 1 x2
x2 = = 1, x1 = =0
40000 0.000025
This computed solution di↵ers significantly from the exact solution. Why? Observe that the com-
puted solution has x2 = 1. This is an accurate approximation of its exact value 39998 39999 , in error
1 x2
by 39998
39999 1 = 1
39999 . When the approximate value of x 1 is computed as 0.000025 , catastrophic
cancellation occurs when the approximate value x2 = 1 is subtracted from 1.
If we include the exchange step, the result of the exchange step is
1x1 + 1x2 = 2
0.000025x1 + 1x2 = 1
0.000025x1 + 1x2 = 1
0x1 + ( 39999)x2 = 39998
3.3. GAUSSIAN ELIMINATION WITH PARTIAL PIVOTING 75
0.000025x1 + 1x2 = 1
0x1 + ( 40000)x2 = 40000
Problem 3.3.12. Use Gaussian elimination to verify that the exact solution of the linear
system
0.000025x1 + 1x2 = 1
1x1 + 1x2 = 2
is
40000 39998
x1 = ⇡ 1, x2 = ⇡ 1
39999 39999
76 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS
Problem 3.3.13. (Watkins) Carry out Gaussian elimination without exchange steps and
with row–oriented backward substitution on the linear system:
Use round-to-nearest 4 significant digit decimal arithmetic. Display each pivot, each multi-
plier, and the result of each elimination step. Does catastrophic cancellation occur, and if
so where? Hint: The exact solution is x1 = 1, x2 = 1 and x3 = 1. The computed solution
with approximate arithmetic and no exchanges is x1 = 4.000, x2 = 1.012 and x3 = 2.000.
Problem 3.3.14. Carry out GEPP (with exchange steps) and row–oriented backward sub-
stitution on the linear system in Problem 3.3.13. Use round-to-nearest 4 significant digit
decimal arithmetic. Display each pivot, each multiplier, and the result of each elimination
step. Does catastrophic cancellation occur, and if so where?
Problem 3.3.15. Round the entries in the linear system in Problem 3.3.13 to three sig-
nificant digits. Now, repeat the calculations in Problems 3.3.13 and 3.3.14 using round-to-
nearest 3 significant digit decimal arithmetic. What do you observe?
are equivalent, but the factored form is clearly much easier to solve. In general, we cannot solve linear
systems so easily (i.e., by inspection), but decomposing A makes solving the linear system Ax = b
computationally simpler. Some knowledge of matrix algebra, especially matrix multiplication, is
needed to understand the concepts introduced here; a review is given in Section 1.2.
3.4.1 LU Factorization
Our aim here is to show that if Gaussian elimination can be used to reduce an n ⇥ n matrix A to
upper triangular form, then the information computed in the elimination process can be used to
write A as the product
A = LU
where L is a unit lower triangular matrix (that is, a lower triangular matrix with 1’s on the diagonal)
and U is an upper triangular matrix. This is called a matrix factorization, and we will see that
the idea of matrix factorization is very powerful when solving, analyzing and understanding linear
algebra problems.
To see how we can get to the LU factorization, suppose we apply Gaussian elimination to a n ⇥ n
matrix A without interchanging any rows. For example, for n = 3,
2 3 2 3 2 3
a1,1 a1,2 a1,3 a1,1 a1,2 a1,3 a1,1 a1,2 a1,3
6 7 6 (1) (1) 7 6 (1) (1) 7
4 a2,1 a2,2 a2,3 5 ! 4 0 a2,2 a2,3 5 ! 4 0 a2,2 a2,3 5
a3,1 a3,2 a3,3 (1) (1) (2)
0 a3,2 a3,3 0 0 a3,3
(1)
a2,1 a3,1 a3,2
m2,1 = a1,1 , m3,1 = a1,1 m3,2 = (1)
a2,2
3.4. GAUSSIAN ELIMINATION AND MATRIX FACTORIZATIONS 77
(k)
Here the superscript on the entry ai,j indicates an element of the matrix modified during the kth
elimination step. Recall that, in general, the multipliers are computed as
element to be eliminated
mi,j = .
current pivot element
Instead of using arrows to show the elimination steps, we can use matrix-matrix multiplication with
elimination matrices. Specifically, consider the multipliers m21 and m31 used to eliminate the entries
a21 and a31 in the first column of A. If we put these into a unit lower triangular matrix as follows,
2 3
1 0 0
M1 = 4 m21 1 0 5 ,
m31 0 1
then
2 32 a a12 a13
3
1 0 0 11
4 6 7
M1 A = m21 1 0 5 4 a21 a22 a23 5
m31 0 1 a31 a32 a33
2 3
a11 a12 a13
6 7
= 4 a21 m21 a11 a22 m21 a12 a23 m21 a13 5
a31 m31 a11 a32 m31 a12 a33 m31 a13
2 3
a1,1 a1,2 a1,3
6 (1) (1) 7
= 4 0 a22 a23 5
(1) (1)
0 a32 a33
Now use the multipliers for the next column to similarly define the elimination matrix M2 ,
2 3
1 0 0
M2 = 4 0 1 0 5
0 m32 1
M2 M1 A = U (upper triangular)
Notice that M1 and M2 are nonsingular (as an exercise you should explain why this is the case),
and so we can write
A = M 1 1 M2 1 U
78 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS
The next observation we make is that the inverse of an elimination matrix is very easy to compute,
we just need to change the signs of mij to mij ; that is,
2 3 2 3
1 0 0 1 0 0
M1 1 = 4 m21 1 0 5 and M2 1 = 4 0 1 0 5
m31 0 1 0 m32 1
This can be easily verified by showing that M1 1 M1 = I and M2 1 M2 = I.
The final observation we need is that the product of elimination matrices (and the product of
their inverses) is a unit lower triangular matrix. In particular, observe that
2 32 3 2 3
1 0 0 1 0 0 1 0 0
M1 1 M2 1 = 4 m21 1 0 5 4 0 1 0 5 = 4 m21 1 0 5
m31 0 1 0 m32 1 m31 m32 1
Thus, for this simple 3 ⇥ 3 example, we have computed
A = (M1 1 M2 1 )U = LU
where L is a unit lower triangular matrix, with multipliers below the main diagonal, and U is the
upper triangular matrix obtained after the elimination is complete.
It is not difficult to generalize (e.g., by induction) to a general n ⇥ n matrix. Specifically, if A is
an n ⇥ n matrix, then:
• An elimination matrix Mj is the identity matrix, except that the jth column has negative
multipliers, mij , below the main diagonal, i = j + 1, j + 2, . . . , n,
2 3
1
6 .. 7
6 . 7
6 7
6 1 7
6
Mi = 6 7
m 7
6 j+1,j 7
6 .. . 7
4 . . . 5
mn,j 1
and thus
2 3 2 3 2 3
1 0 0 1 0 0 1 2 3
L = 4 m2,1 1 0 5=4 2 1 0 5 and U =4 0 7 4 5.
5 50
m3,1 m3,2 1 3 7 1 0 0 7
Ax = b ) LU x = b ) Ly = b, where U x = y.
So, to solve Ax = b:
• Compute the factorization A = LU .
• Solve Ly = b using forward substitution.
• Solve U x = y using backward substitution.
x3 = 1.
2x2 x3 = 3 ) x2 = (3 + 1)/2 = 2.
x1 + 2x2 x3 = 2 ) x1 = 2 2(2) + 1 = 1.
Therefore, the solution of Ax = b is given by
2 3
1
x=4 2 5.
1
(a) Use Gaussian elimination without row interchanges to find the factorization A = LU .
(b) Use the factorization of A to solve Ax = b.
Problem 3.4.4. Suppose
2 3 2 3
4 8 12 8 3
6 3 1 1 4 7 6 60 7
A=6
4
7 and b=6 7
4 1 5.
1 2 3 4 5
2 3 2 1 5
(a) Use Gaussian elimination without row interchanges to find the factorization A = LU .
(b) Use the factorization of A to solve Ax = b.
3.4.2 P A = LU Factorization
The practical implementation of Gaussian elimination uses partial pivoting to determine if row
interchanges are needed. We show that this results in a modification of the LU factorization. Row
interchanges can by represented mathematically as multiplication by a permutation matrix, obtained
by interchanging rows of the identity matrix.
3.4. GAUSSIAN ELIMINATION AND MATRIX FACTORIZATIONS 81
In Gaussian elimination with partial pivoting, because |a13 | = 5 is the largest (in magnitude) entry
in the first column, we begin by switching rows 1 and 3. This can be represented in terms of matrix-
matrix multiplication with a permutation matrix P , which is constructed by switching the first and
third rows of a 4 ⇥ 4 identity matrix. Specifically,
2 3 2 3
1 0 0 0 0 0 1 0
6 0 1 0 0 7 switch 6 0 1 0 0 7
I=6 4 0 0 1 0 5
7 ! P =6 4 1 0 0 0 5
7
rows 1 and 3
0 0 0 1 0 0 0 1
Multiplying the matrix A on the left by P switches its first and third rows. That is,
2 32 3 2 3
0 0 1 0 0 3 8 6 5 2 4 7
6 0 1 0 0 7 6 2 3 0 1 7 6 1 7
PA = 6 76 7=6 2 3 0 7
4 1 0 0 0 54 5 2 4 7 5 4 0 3 8 6 5
0 0 0 1 1 1 1 1 1 1 1 1
We can now combine elimination matrices with permutation matrices to describe Gaussian elim-
ination with partial pivoting using matrix-matrix multiplications. Specifically,
Mn 1 Pn 1 · · · M 2 P2 M 1 P1 A = U
where
• P1 is the permutation matrix that swaps the largest, in magnitude, entry in the first column
of A to the (1,1) location.
• M1 is the elimination matrix that zeros out all entries of the first column of P1 A below the
(1,1) pivot entry.
• P2 is the permutation matrix that swaps the largest, in magnitude, entry on and below the
(2,2) diagonal entry in the second column of M1 P1 A with the (2,2) location.
• M2 is the elimination matrix that zeros out all entries of the second column of P2 M1 P1 A below
the (2,2) pivot entry.
• etc.
Thus, after completing Gaussian elimination with partial pivoting, we have
1
A = (Mn 1 Pn 1 · · · M 2 P 2 M 1 P1 ) U. (3.1)
P = Pn 1 · · · P2 P1 ,
is a unit lower triangular matrix. Thus, if we multiply P on both sides of equation (3.1), we obtain
1
PA = P (Mn 1 Pn 1 · · · M 2 P2 M 1 P1 ) U
= LU .
Therefore, when we apply Gaussian elimination with partial pivoting by rows to reduce A to
upper triangular form, we obtain an LU factorization of a permuted version of A. That is,
P A = LU
where P is a permutation matrix representing all row interchanges in the order that they are applied.
When writing code, or performing hand calculations, it is not necessary to explicitly form any
of the matrices Pi or Mi , but instead we can proceed as for the LU factorization, and keep track of
the row interchanges and multipliers as follows:
• Each time we switch rows of A, we switch corresponding multipliers in L. For example, if at
stage 3 we switch rows 3 and 5, then we must also switch the previously computed multipliers
m3,k and m5,k , k = 1, 2.
• Begin with P = I. Each time we switch rows of A, we switch corresponding rows of P .
Example 3.4.4. This example illustrates the process of computing P A = LU for the matrix
2 3
1 2 4
A=4 4 5 6 5.
7 8 9
A P multipliers
2 3 2 3
1 2 4 1 0 0
4 4 5 6 5 4 0 1 0 5 nothing yet
7 8 9 0 0 1
# # #
2 3 2 3
7 8 9 0 0 1
4 4 5 6 5 4 0 1 0 5 nothing yet
1 2 4 1 0 0
# # #
2 3 2 3
7 8 9 0 0 1
4 0 3 6 5 4 0 1 0 5 m21 = 4
m31 = 1
7 7 7 7
6 19
0 7 7 1 0 0
# # #
2 3 2 3
7 8 9 0 0 1
4 0 6 19 5 4 1 0 0 5 m21 = 1
m31 = 4
7 7 7 7
3 6
0 7 7 0 1 0
# # #
2 3 2 3
7 8 9 0 0 1
4 0 6 19 5 4 1 0 0 5 m21 = 1
m31 = 4
m32 = 1
7 7 7 7 2
1
0 0 2 0 1 0
3.4. GAUSSIAN ELIMINATION AND MATRIX FACTORIZATIONS 83
From the information in the final step of the process, we obtain the P A = LU factorization, where
2 3 2 3 2 3
0 0 1 1 0 0 7 8 9
P = 4 1 0 0 5 , L = 4 17 1 0 5 , U = 4 0 67 19 5
7
4 1 1
0 1 0 7 2 1 0 0 2
An efficient algorithm for computing the P A = LU factorization usually does not explicitly
construct P , but instead keeps an index of “pointers” to rows, but we will leave that discussion for
more advanced courses on numerical linear algebra.
If we can compute the factorization P A = LU , then
Ax = b ) P Ax = P b ) LU x = P b ) Ly = P b, where U x = y.
Therefore, to solve Ax = b:
• Compute the factorization P A = LU .
It is important to emphasize the importance, and power of matrix factorizations. The cost of
computing A = LU or P A = LU requires O(n3 ) FLOPS, but forward and backward solves require
only O(n2 ) FLOPS. Thus, if we need to solve multiple linear systems where the matrix A does not
change, but with di↵erent right hand side vectors, such as
Ax1 = b1 , Ax2 = b2 · · ·
then we need only compute the (relatively expensive) matrix factorization once, and reuse it for all
linear system solves.
Matrix factorizations might be useful for other calculations, such as computing the determinant
of A. Recall the following properties of determinants:
• The determinant of the permutation matrix P is ±1; it is +1 if an even number of row swaps
were performed, and 1 if odd number of row swaps were performed.
• The determinant of a triangular matrix is the product of its diagonal entries. In particular,
det(L) = 1 because it is a unit lower triangular matrix.
det(P A) = det(LU )
det(P )det(A) = det(L)det(U )
±det(A) = det(U )
det(A) = ±det(U ) = ±u11 u22 · · · unn
Problem 3.4.5. Use Gaussian elimination with partial pivoting to find the P A = LU
factorization of the matrices:
2 3
2 3 2 3 2 1 0 3
1 3 4 3 6 9 6 0 3 1
4 7
A=4 0 1 5 5, B = 4 2 5 2 5, C = 6 4 1
4 2 7
1 5
1 1 2
2 0 4 3 4 11 5
2 2 1 13
⇥ ⇤
Problem 3.4.6. Suppose that bT = 3 60 1 5 , and suppose that Gaussian elimina-
tion with partial pivoting has been used on a matrix A to obtain its P A = LU factorization,
where
2 3 2 3 2 3
0 1 0 0 1 0 0 0 4 8 12 8
6 0 0 0 1 7 6 3/4 1 0 0 7 6 10 7
P =6 7 6 7 , U = 6 0 5 10 7.
4 1 0 0 0 5 , L = 4 1/4 0 1 0 5 4 0 0 6 6 5
0 0 1 0 1/2 1/5 1/3 1 0 0 0 1
then 2 3 2 3
3 0 1 5 3 1 0 2
6 0 2 4 8 7 6 0 2 3 1 7
AT = 6
4
7=A but BT = 6 7=6 B.
1 4 1 2 5 4 1 4 1 1 5
5 8 2 6 5 8 2 6
Thus A is symmetric, but B is not symmetric.
A matrix A 2 Rn⇥n is positive definite if xT Ax > 0 for all x 2 Rn , x 6= 0. The special structure
of a symmetric and positive definite matrix (which we will abbreviate as SPD) allows us to compute
a special LU factorization, which is called the Cholesky factorization. Specifically, it can be shown
that an n ⇥ n matrix A is SPD if and only if it can be factored as1
where R is an upper triangular matrix with positive entries on the diagonal. Pivoting is generally
not needed to compute this factorization. The “if and only if” part of the above statement is
important. This means that if we are given a matrix A, and the Cholesky factorization fails, then
we know A is not SPD, but if it succeeds, then we know A is SPD.
To give a brief outline on how to compute a Cholesky factorization, it is perhaps easiest to begin
with a small 3 ⇥ 3 example. That is, consider
2 3
a11 a12 a13
A = 4 a12 a22 a23 5 .
a13 a23 a33
the notation used in this book (which better matches what is the default from computed in Matlab), with L = RT .
86 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS
• Now set A = RT R and match corresponding components to solve for rij . That is,
2 p
r11 = a11 ) r11 = a11
r11 r12 = a12 ) r12 = a12 /r11
r11 r13 = a13 ) r13 = a13 /r11
p
2 2 2
r12 + r22 = a22 ) r22 = a22 r12
r13 r12 + r23 r22 = a23 ) r23 = (a23 r13 r12 ) /r22
p
2 2 2 2 2
r13 + r23 + r33 = a33 ) r33 = a33 r13 r23
The above process can easily be generalized for any n ⇥ n SPD matrix. We skip efficient imple-
mentation details, but make two observations. First, generally we do not need to consider pivoting
when computing the Cholesky factorization of an SPD matrix, and the values inside the square root
symbols are always positive. The second observation is that because the matrix is symmetric, it
should not be surprising that an efficient implementation costs approximately half the number of
FLOPS needed for standard P A = LU factorizations.
Solving linear systems with the Cholesky factorization is essentially the same as with P A = LU .
That is, if A = RT R, then
Ax = b ) RT Rx = b ) RT (Rx) = b ,
and so to compute x,
• use forward substitution to solve RT y = b, and
• use backward substitution to solve Rx = y.
3.5.2 QR factorization
A matrix Q 2 Rn⇥n is called orthogonal if the columns of Q form an orthonormal set. That is, if
we write ⇥ ⇤
Q = q1 q2 · · · qn ,
where qj is the jth column of Q, then
⇢
1 if i=j
qiT qj = .
0 if i 6= j
That is, QT Q = I, and thus the inverse of Q is simply QT . This is a very nice property!
A first course on linear algebra often covers a topic called Gram-Schmidt orthonormalization,
which transforms a linearly independent set of vectors into an orthonormal set. We will not review
the Gram-Schmidt method in this book, but state that if it is applied to the columns of a nonsingular
matrix A 2 Rn⇥n , then it results in a matrix factorization of the form
A = QR ,
where Q 2 Rn⇥n is an orthogonal matrix, and R 2 Rn⇥n is upper triangular. This is called the QR
factorization of A.
We remark that if the columns of the (possibly over-determined rectangular) matrix A 2 Rm⇥n ,
m n, then it is still possible to compute a QR factorization of A. This will be discussed when we
consider the topic of least squares in the chapter on curve fitting. We also remark that there are
other (often better) approaches than Gram-Schmidt for computing A = QR (e.g., Householder and
Givens methods), but these are best left for a more advanced course on numerical linear algebra.
Computing solutions of Ax = b with the QR factorization is also straight forward:
Ax = b ) QRx = b ) Rx = QT b ,
and so to solve Ax = b,
• compute d = QT b, and
• use backward substitution to solve Rx = d.
Although we do not discuss algorithms for computing A = QR in this book, we should note that if
A 2 Rn⇥n is nonsingular, then an efficient implementation requires O(n3 ) FLOPS. But the hidden
constant in the O(·) notation is approximately two times that for the P A = LU factorization. Thus,
P A = LU is usually the preferred factorization for solving n ⇥ n nonsingular systems of equations.
However, the QR factorization is superior for solving least squares problems.
A = U ⌃V T ) AV = U ⌃
and so, 2 3
1
6 .. 7
⇥ ⇤ ⇥ ⇤6
6 . 7
7
A v1 ··· vn = u1 ··· un un+1 ··· um 6 7.
6 n 7
4 5
That is,
Avi = i ui , i = 1, 2, . . . , n
The SVD has the following properties:
• If rank(A) = r, then
1 ··· r > r+1 = ··· = n =0
. In particular, if A is n ⇥ n and nonsingular, then all singular values are nonzero.
• If rank(A) = r, then the nullspace of A is:
null(A) = span{vr+1 , . . . , vn }
range(A) = span{u1 , . . . , ur }
Ax = b ) U ⌃V T x = b ) ⌃V T x = U T b ,
and so to solve Ax = b,
3.6. THE ACCURACY OF COMPUTED SOLUTIONS 89
• compute d = U T b,
• solve the diagonal system ⌃y = d,
• compute x = V y.
Although we do not discuss algorithms for computing the SVD in this book, we should note that if
A 2 Rn⇥n is nonsingular, then an efficient implementation requires O(n3 ) FLOPS. But the hidden
constant in the O(·) notation is approximately nine times that for the QR factorization, and eighteen
times that for the P A = LU factorization. Because it is so expensive, it is rarely used to solve linear
systems, but it is a superior tool to analyze sensitivity of linear systems, and it finds use in important
applications such as rank deficient least squares problems, principle component analysis (PCA), and
even data compression.
Problem 3.5.4. Our definition of the SVD assumes that m n (that is, A has at least as
many rows as columns). Show that a similar definition, and hence decomposition, can be
written for the case m < n (that is, A has more columns than rows).
Hint: Consider using our original definition of the SVD for AT .
Problem 3.5.7. In this problem we consider relationships between singular values and
eigenvalues. Recall from your basic linear algebra class that if B 2 Rn⇥n , then is an
eigenvalue of B if there is a nonzero vector x 2 Rn such that
Bx = x .
Avi = i ui and AT u i = i vi .
seem natural to consider the error vector e = x̂ x and determine if e is small. However, e is a
vector with possibly many entries, so what does it mean to say “e is small”? To answer this question,
we need the concept of vector norm, which uses the notation k · k, and must satisfy the following
properties:
1. kvk 0 for all vectors v 2 Rn , and kvk = 0 if and only if v = 0 (that is, the vector with all
zero entries),
• The 2-norm is the standard Euclidean length of a vector taught in multivariable calculus and
linear algebra courses. Specifically, if
2 3
e1
6 e2 7
6 7
e=6 . 7
4 .. 5
en
n
!1/p
X
p
kekp = |ei | .
i=1
Although other norms are used in certain applications, we usually use the 2-norm. However, any
norm gives us a single number, and if the norm of the error,
kek = kx̂ xk
kx̂ xk
kxk
provided x 6= 0.
3.6. THE ACCURACY OF COMPUTED SOLUTIONS 91
Problem 3.6.1. Consider the previous example, and compute the relative error using the
1-norm and the 1-norm.
⇥ ⇤T
Problem 3.6.2. If x = 1 2 3 0 1 , compute kxk1 , kxk2 , and kxk1 .
Other matrix norms that are often used in scientific computing are the class of p-norms, which are
said to be induced by the corresponding vector norms, and are defined as
kAxkp
kAkp = max .
x6=0 kxkp
This is not a very useful definition for actual computations, but fortunately there are short cut
formulas that can be used for three of the most popular matrix p-norms. We will not prove these,
but proofs can be found in more advanced books on numerical analysis.
• The matrix 2-norm is defined as
kAxk2
kAk2 = max ,
x6=0 kxk2
but can be computed as
kAk2 = 1 ,
where 1 is the largest singular value of A.
92 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS
kAxk kAkkxk,
and
kABk kAkkBk ,
provided the matrix multiplication of AB is defined.
Problem 3.6.3. If 2 3
1 2 3
6 2 0 5 7
A=6
4
7,
1 1 1 5
2 4 0
compute kAk1 , kAk1 , and kAkF .
where aj is the j-th column vector of the matrix A, and trace is the sum of diagonal entries
of the given matrix.
Problem 3.6.5. In general, for an induced matrix norm, the following inequality holds:
kAxk kAkkxk
1 1
kA k2 = (reciprocal of the smallest singular value of A).
n
krk = kb Ax̂k ,
Why does this happen? We can gain a little insight by examining a simple 2 ⇥ 2 linear system,
a11 x1 + a12 x2 = b1
Ax = b )
a21 x1 + a22 x2 = b2
94 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS
and assume that a12 6= 0 and a22 6= 0. Each equation is a line, which then can be written as
a11 b1 a21 b2
x2 = x1 + and x2 = x1 + .
a12 a12 a22 a22
x1
The solution of the linear system, x = is the point where the two lines intersect. Consider
x2
the following two very di↵erent cases (it might also help to look at the plots in Figure 3.1):
Case 1: The slopes, aa1112
and aa2122
are very di↵erent, e.g., the two lines are nearly perpendic-
ular. Then small changes in b or A (e.g., due to round o↵ error) will not dramatically change
the intersection point. In this case, the rows of A are linearly independent – very far from
being linearly dependent, and A is very far from being singular. In this case, we say the matrix
A, and hence the linear system Ax = b, is well-conditioned.
Case 2: The slopes, aa11 12
and aa2122
are nearly equal, e.g., the two lines are nearly parallel.
In this case, small changes in b or A (e.g., due to round o↵ error) can cause a dramatic change
the intersection point. Here, the rows of A are nearly linearly dependent, and thus A is very
close to being singular. In this case we say that the matrix A, and hence the linear system
Ax = b, is ill-conditioned.
This idea of conditioning can be extended to larger systems. In general, if the matrix is nearly
singular (i.e., the columns or rows are nearly linearly dependent), then we say the matrix A is
ill-conditioned.
So far our discussion of conditioning is a bit vague, and it would be nice to have a formal definition
and/or way to determine if a matrix is ill-conditioned. We can do this by recalling the SVD; that
is, if A = U ⌃V T , where
⌃ = diag( 1 , 2 , . . . , n ) ,
where 1 2 ··· n 0. Recall that the rank(A) is the number of nonzero singular values.
That is, A is singular if the smallest singular value, n = 0.
Now suppose A is nonsingular, so that n 6= 0. How do we determine if A “is nearly singular”?
One way is to consider the ratio of the largest and smallest singular values, n1 . This ratio will be
⇡ 1 if the matrix is well-conditioned (e.g., as in the case of the identity matrix, I), and very large if
the matrix is ill-conditioned. We know that kAk2 = 1 , and from Problem 3.6.6 we also know that
kA 1 k2 = 1n . Thus, we can define the condition number associated with the matrix 2-norm
to be
1
2 (A) = kAk2 kA 1 k2 = .
n
More generally, for any matrix norm, we define the condition number as
1
(A) = kAkkA k.
Using MATLAB’s svd function, we find that 1 ⇡ 1.1505e + 00 and 2 ⇡ 8.6915e 07, and hence
1 1
2 (A) = kAk2 kA k2 = ⇡ 1.3238e + 06 .
n
1 kAk
. (3.4)
kxk kbk
are small.
To prove an algorithm is backward stable requires establishing bounds for the error norms in
(3.6), and showing that the bounds are small. This is a relatively advanced topic, so we do not
provide any results in this book, but interested readers can find the results in many excellent books
on numerical linear algebra and matrix computations. However, it is important for readers of this
book to understand that one of the nice properties of GEPP is that, generally, Â is “close to” A and
b̂ is “close to” b, and thus GEPP is generally2 backward stable. When GEPP is backward stable,
the computed solution x̂ of Ax = b is the exact solution of Âx̂ = b̂ where  is close to A and b̂ is
close to b.
We also mention that the best algorithms for Cholesky, QR and SVD (e.g., the ones used by
Matlab) are backward stable.
How do the concepts of well-conditioned and ill-conditioned linear systems relate to the concept
of algorithm stability? Backward error analysis shows that the approximate solution of Ax = b
computed by Cholesky, QR and SVD, and usually for GEPP, are exact solutions of a related linear
system Âx̂ = b̂, where  is close to A and b̂ is close to b. Thus, if Ax = b is well-conditioned, it
follows that the computed solution is accurate. On the other hand, if Ax = b is ill-conditioned, even
if  is close to A and b̂ is close to b, these small di↵erences may lead to a large di↵erence between
the exact solution of Ax = b and the the computed solution x̂. In summary, when using a backward
stable algorithm to solve a well-conditioned linear system, the computed solution is accurate. On
the other hand, if the linear system is ill-conditioned, then even with the most stable algorithms,
the computed solution may not be accurate.
Problem 3.6.7. This example illustrates how not to determine conditioning of a linear
system. We know that det(A) = 0 when the linear system Ax = b is singular. So, we might
assume that the magnitude of det(A) might be a good indicator of how close the matrix A
is to a singular matrix. Unfortunately, this is not always the case. Consider, for example,
the two linear systems:
2 32 3 2 3 2 32 3 2 3
1 0 0 x1 1 0.5 0 0 x1 0.5
4 0 1 0 5 4 x2 5 = 4 2 5 and 4 0 0.5 0 5 4 x2 5 = 4 1.0 5
0 0 1 x3 3 0 0 0.5 x3 1.5
where the second is obtained from the first by multiplying each of its equations by 0.5.
(a) Find and compare the determinant of the two coefficient matrices.
(b) Show that GEPP produces identical solutions for both linear systems (this should be
trivial because each linear system is diagonal, so no exchanges or eliminations need be
performed).
Thus, this problem shows that the magnitude of the determinant is not necessarily a good
indicator of how close a coefficient matrix is to the nearest singular matrix.
2 We use the term generally because the error bounds depend on something called a growth factor, which is generally
small (in which case the algorithm is backward stable), but there are unusual cases where the growth factor can be
large. This topic is studied in more advanced courses on numerical linear algebra.
3.7. MATLAB NOTES 97
Problem 3.6.10. Consider a linear system Ax = b. When this linear system is placed into
the computer’s memory, say by reading the coefficient matrix and right hand side from a
data file, the entries of A and b must be rounded to floating–point numbers. If the linear
system is well–conditioned, and the solution of this “rounded” linear system is computed
exactly, will this solution be accurate? Answer the same question but assuming that the
linear system Ax = b is ill–conditioned.
First however we develop Matlab implementations of some of the algorithms discussed in this
chapter. These examples build on the introduction in Chapter 1, and are designed to introduce
useful Matlab commands and to teach proper Matlab programming techniques.
function x = DiagSolve1(A,b)
%
% x = DiagSolve1(A, b);
%
% Solve Ax=b, where A is an n-by-n diagonal matrix.
%
n = length(b); x = zeros(n,1);
for i = 1:n
if A(i,i) == 0
error(’Input matrix is singular’)
end
x(i) = b(i) / A(i,i);
end
3.7. MATLAB NOTES 99
We use the Matlab function length to determine the dimension of the linear system, assumed
the same as the length of the right hand side vector, and we use the error function to print an error
message in the command window, and terminate the computation, if the matrix is singular. We can
shorten this code, and make it more efficient by using array operations:
function x = DiagSolve2(A, b)
%
% x = DiagSolve2(A, b);
%
% Solve Ax=b, where A is an n-by-n diagonal matrix.
%
d = diag(A);
if any(d == 0)
error(’Input matrix is singular’)
end
x = b ./ d;
In DiagSolve2, we use Matlab’s diag function to extract the diagonal entries of A, and store
them in a column vector d. If there is at least one 0 entry in the vector d, then any(d == 0) returns
true, otherwise it returns false. If all diagonal entries are nonzero, the solution is computed using
the element-wise division operation, ./ In most cases, if we know the matrix is diagonal, then we
can substantially reduce memory requirements by using only a single vector (not a matrix) to store
the diagonal elements, as follows:
function x = DiagSolve3(d, b)
%
% x = DiagSolve3(d, b);
%
% Solve Ax=b, where A is an n-by-n diagonal matrix.
%
% Input: d = vector containing diagonal entries of A
% b = right hand side vector
%
if any(d == 0)
error(’Diagonal matrix defined by input is singular’)
end
x = b ./ d;
In the algorithms DiagSolve2 and DiagSolve3 we do not check if length(b) is equal to length(d).
If they are not equal, Matlab will report an array dimension error.
Problem 3.7.1. Implement the functions DiagSolve1, DiagSolve2, and DiagSolve3, and
use them to solve the linear system in Fig. 3.2(a).
100 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS
function x = LowerSolve0(A, b)
%
% x = LowerSolve0(A, b);
%
% Solve Ax=b, where A is an n-by-n lower triangular matrix,
% using row-oriented forward substitution.
%
n = length(b);, x = zeros(n,1);
for i = 1:n
for j = 1:i-1
b(i) = b(i) - A(i,j)*x(j);
end
x(i) = b(i) / A(i,i);
end
This implementation can be improved in several ways. As with the diagonal solve functions, we
should include a statement that checks to see if A(i,i) is zero. Also, the innermost loop can be
replaced with a single Matlab array operation. Observe that we can write the algorithm as:
3.7. MATLAB NOTES 101
for i = 1 : n
xi = (bi (ai1 x1 + ai2 x2 + · · · + ai,i 1 xi 1 )) /aii
end
Using linear algebra notation, we can write this as:
for i = 1 0
:n 2 31
x1 ,
B ⇥ ⇤6 x2 7C
B 6 7C
x i = Bb i ai1 ai2 ··· ai,i 1 6 .. 7C aii
@ 4 . 5A
xi 1
end
Recall that, in Matlab, we can specify entries in a matrix or vector using colon notation. That is,
2 3
x1
6 x2 7 ⇥ ⇤
6 7
x(1 : i 1) = 6 . 7 and A(i, 1 : i 1) = ai,1 ai,2 · · · ai,i 1 .
.
4 . 5
xi 1
So, using Matlab notation, the algorithm for row-oriented forward substitution can be written:
for i = 1 : n
x(i) = (b(i) A(i, 1 : i 1) ⇤ x(1 : i 1))/ A(i, i)
end
When i = 1, Matlab considers A(i, 1 : i 1) and x(1 : i 1) to be ”empty” matrices, and the
computation A(i, 1 : i 1) ⇤ x(1 : i 1) gives 0. Thus, the algorithm simply computes x(1) =
b(1)/A(1, 1), as it should. To summarize, a Matlab function to solve a lower triangular system
using row oriented forward substitution could be written:
function x = LowerSolve1(A, b)
%
% x = LowerSolve1(A, b);
%
% Solve Ax=b, where A is an n-by-n lower triangular matrix,
% using row-oriented forward substitution.
%
if any(diag(A) == 0)
error(’Input matrix is singular’)
end
n = length(b);, x = zeros(n,1);
for i = 1:n
x(i) = (b(i) - A(i,1:i-1)*x(1:i-1)) / A(i,i);
end
function x = LowerSolve2(A, b)
%
% x = LowerSolve2(A, b);
%
% Solve Ax=b, where A is an n-by-n lower triangular matrix,
% using column-oriented forward substitution.
%
if any(diag(A) == 0)
error(’Input matrix is singular’)
end
n = length(b);, x = zeros(n,1);
for j = 1:n
x(j) = b(j) / A(j,j);
b(j+1:n) = b(j+1:n) - A(j+1:n,j)*x(j);
end
Problem 3.7.3. Implement the functions LowerSolve1 and LowerSolve2, and use them
to solve the linear system in Fig. 3.2(b).
Problem 3.7.4. Consider the following Matlab commands:
[piv, i] = max(abs(A(k:n,k)));
then, after execution of this command, piv contains the largest entry (in magnitude) in the
vector A(k:n,k), and i is its location in the vector. Note that if i = 1, then the index p in
Fig. 3.7 is p = k and, in general,
p = i + k - 1;
• Once the pivot row, p, is known, then the pth row of A is switched with the k th row, and the
corresponding entries of b must be switched. This may be implemented using Matlab’s array
indexing capabilities:
That is, b([k,p]) = b([p,k]) instructs Matlab to replace b(k) with the original b(p), and
to replace b(p) with original b(k). Matlab’s internal memory manager makes appropriate
copies of the data so that the original entries are not overwritten before the assignments are
completed. Similarly, A([k,p],k:n) = A([p,k],k:n) instructs Matlab to replace the rows
specified on the left with the original rows specified on the right.
• The elimination step is straightforward; compute the multipliers, which we store in the strictly
lower triangular part of A:
for i = k+1:n
A(i,k+1:n) = A(i,k+1:n) - A(i,k)*A(k,k+1:n);, b(i) = b(i) - A(i,k)*b(k);
end
for i = n:-1:1
x(i) = (b(i) - A(i,i+1:n)*x(i+1:n)) / A(i,i);
end
The statement for i = n:-1:1 indicates that the loop runs over values i = n, n 1, . . . , 1;
that is, it runs from i=n in steps of -1 until it reaches i=1.
• How do we implement a check for singularity? Due to roundo↵ errors, it is unlikely that
any pivots akk will be exactly zero, and so a statement if A(k,k) == 0 will usually miss
detecting singularity. An alternative is to check if akk is small in magnitude compared to, say,
the largest entry in magnitude in the matrix A:
104 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS
Here, the command A(:) reshapes the matrix A into one long vector, and max(abs(A(:)))
finds the largest entry. Of course, this test also traps matrices which are close to but not
exactly singular, which is usually reasonable as by definition they tend to be ill–conditioned.
• Finally, certain other initializations are needed: the dimension, n, space for the solution vector,
and for the multipliers. We use the length, zeros, and eye functions:
function x = gepp(A, b)
%
% Solves Ax=b using Gaussian elimination with partial pivoting
% by rows for size. Initializations:
%
n = length(b); x = zeros(n,1);
tol = eps*max(A(:));
%
% Loop for stages k = 1, 2, ..., n-1
%
for k = 1:n-1
%
% Search for pivot entry:
%
[piv, psub] = max(abs(A(k:n,k)));, p = psub + k - 1;
%
% Exchange current row, k, with pivot row, p:
%
A([k,p],k:n) = A([p,k],k:n);, b([k,p]) = b([p,k]);
%
% Check to see if A is singular:
%
if abs(A(k,k)) < tol
error(’Linear system appears to be singular’)
end
%
% Perform the elimination step - row-oriented:
%
A(k+1:n,k) = A(k+1:n,k) / A(k,k);
for i = k+1:n
A(i,k+1:n) = A(i,k+1:n) - A(i,k)*A(k,k+1:n);, b(i) = b(i) - A(i,k)*b(k);
end
end
%
% Check to see if A is singular:
%
if abs(A(n,n)) < tol
error(’Linear system appears to be singular’)
end
%
% Solve the upper triangular system by row-oriented backward substitution:
%
for i = n:-1:1
x(i) = (b(i) - A(i,i+1:n)*x(i+1:n)) / A(i,i);
end
The function gepp above implements a function that uses GEPP to solve Ax = b.
Problem 3.7.8. Implement the function gepp, and use it to solve the linear systems given
in problems 3.3.2 and 3.3.3.
106 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS
-----------------------------------------------------
n error residual condition number
-----------------------------------------------------
where
x = A \ b;
Matlab first checks if the matrix A has a special structure, including diagonal, upper triangu-
lar, and lower triangular. If a special structure is recognized (for example, upper triangular),
then a special method (for example, column-oriented backward substitution) is used to solve
Ax = b. If a special structure is not recognized then Gaussian elimination with partial pivoting
is used to solve Ax = b. During the process of solving the linear system, Matlab estimates
the reciprocal of the condition number of A. If A is ill–conditioned, then a warning message is
printed along with the estimate, RCOND, of the reciprocal of the condition number.
3.7. MATLAB NOTES 107
Because the backslash operator is so powerful, we use it almost exclusively to solve general linear
systems.
We can compute explicitly the P A = LU factorization using the lu function:
[L, U, P] = lu(A)
Given this factorization, and a vector b, we could solve the Ax = b using the statements:
d = P * b;, y = L \ d;, x = U \ y;
These statements could be combined into one instruction:
x = U \ ( L \ (P * b) );
Thus, given a matrix A and vector b we could solve the linear system Ax = b as follows:
[L, U, P] = lu(A);
x = U \ ( L \ (P * b) );
Note, Matlab follows the rules of operator precedence thus backslash and * are of equal precedence.
With operators of equal precedence Matlab works from the left. So, without parentheses the
instruction
x = U \ L \ P * b ;
would be interpreted as
x = ((U \ L) \ P) * b;
The cost and accuracy of this approach is essentially the same as using the backslash operator.
So when would we prefer to explicitly compute the P A = LU factorization? One situation is when
we need to solve several linear systems with the same coefficient matrix, but di↵erent right hand
side vectors. For large systems it is far more expensive to compute the P A = LU factorization than
it is to use forward and backward substitution to solve corresponding lower and upper triangular
systems. Thus, if we can compute the factorization just once, and use it for the various di↵erent
right hand side vectors, we can make a substantial savings. This is illustrated in problem 3.7.18,
which involves a relatively small linear system.
Problem 3.7.13. Use the backslash operator to solve the systems given in problems 3.3.2
and 3.3.3.
Problem 3.7.14. Use the backslash operator to solve the linear system
2 32 3 2 3
1 2 3 x1 1
4 4 5 6 5 4 x2 5 = 4 0 5 .
7 8 9 x3 1
Problem 3.7.15. Repeat problem 3.7.11 using the backslash operator in addition to gepp.
108 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS
Problem 3.7.16. Consider the linear system defined by the following Matlab commands:
Does the matrix C have a special structure? Execute the following Matlab commands:
tic, x1 = A \ b;, toc
tic, x2 = C \ b;, toc
tic, x3 = triu(C) \ b;, toc
Are the solutions x1, x2 and x3 good approximations to the exact solution, x =
ones(500,1)? What do you observe about the time required to solve each of the linear
systems?
Problem 3.7.18. Create a script M–file containing the following Matlab statements:
n = 50;, A = rand(n);
tic
for k = 1:n
b = rand(n,1);, x = A \ b;
end
toc
tic
[L, U, P] = lu(A);
for k = 1:n
b = rand(n,1);, x = U \ ( L \ (P * b) );
end
toc
The dimension of the problem is set to n = 50. Experiment with other values of n, such as
n = 100, 150, 200. What do you observe?