Chapter 6
Direct Methods for Solving Linear Systems
Hung-Yuan Fan (范洪源)
Department of Mathematics, National Taiwan Normal University, Taiwan
Spring 2016
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Section 6.1
Linear Systems of Equations
(線性系統; 線性聯立⽅程組)
Objective
To solve a system of n linear equations with n unknowns:
E
1: a11x1+ a12x2+· · · + a1nxn= b1E
2: a21x1+ a22x2+· · · + a2nxn= b2...
E
n: an1x1+ an2x2+· · · + annxn= bn.(1)
Definition
The vectorx = [x1, x2,· · · , xn]T∈ Rn is called a solution to the linear system (1).
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Matrix-Vector Forms (矩陣-向量形式)
The linear system (1) can be rewritten as the followg matrix-vector form:
Ax = b,
where coefficient matrix A∈ Rn×n and right-hand side vector b∈ Rn are defined by
A =
a11 a12 · · · a1n
a21 a22 · · · a2n
... ... ... an1 an2 · · · ann
, b =
b1 b2 ... bn
.
Assumption: The matrix A is nonsingular throughout the
context.The Linear Solvers (線性系統的數值⽅法)
1 Direct Methods: (直接法)
Used for solving small- or medium-sized linear systems with full and dense coefficient matrices. (適⽤於求解中⼩型線性系 統)
Floating-point operation count (簡稱 flop)≈ O(n3).
Gaussian Elimination (⾼斯消去法,簡稱 GE) is an efficient
and stable algorithm for solving this type of linear systems.2 Iterative Methods: (迭代法)
Used for solving large and sparse linear systems with problem size n≥ 104. (適⽤於求解⼤型稀疏線性系統)
flop≈ O(n) per iteration if A is a sparse matrix.
Jacobi, Gauss-Seidel, SOR and CG-based methods,. . ..
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Elementary Row Operations (基本列運算)
Three Row Operations
In order to simplify linear system (1), we use
1 (λEi)→ (Ei): eq. Ei is replaced by λ· Ei for any λ̸= 0.
2 (Ei+ λEj)→ (Ei): eq. Ej is multiplied by any λ∈ R and added to eq. Ei.
3 (Ei)↔ (Ej): exchange eqs. Ei and Ej for i̸= j.
Gaussian Elimination (GE)
The Procedure of GE
Given a linear system Ax = b with A∈ Rn×n and b∈ Rn.
1 Form the augmented matrix ˜A(1)= [A| b] ∈ Rn×(n+1).
2 Applying row operations continuously, we obtain a finite sequence of augmented matrices, i.e.,
A˜(1) → ˜A(2)→ · · · → ˜A(n), where ˜A(n) is upper triangular. (上三⾓矩陣)
3 Use backward substitution (向後代入) to obtain xn, xn−1,· · · , x2, x1.
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
From ˜ A
(1)to ˜ A
(2)From the augmented matrix ˜A(1)= [A| b] = [a(1)ij ], we have
A˜(1) =
a(1)11 a(1)12 · · · a(1)1,n+1
a(1)21 a(1)22 · · · a(1)2,n+1 ... ... ... a(1)n1 a(1)n2 · · · a(1)n,n+1
.
From ˜ A
(1)to ˜ A
(2)(Conti’d)
If a(1)11 ̸= 0, do(
Ei− a(1)i1
a(1)11E1)
→ (Ei) for i = 2, 3, . . . , n⇒
A˜(2) =
a(1)11 a(1)12 · · · a(1)1,n+1 0 a(2)22 · · · a(2)2,n+1
... ... ... 0 a(2)n2 · · · a(2)n,n+1
.
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
From ˜ A
(2)to ˜ A
(3)Next, if a(2)22 ̸= 0, do(
Ei− a(2)i2
a(2)22 E2)
→ (Ei) for i = 3, 4, . . . , n
⇒
A˜(3) =
a(1)11 a(1)12 a(1)13 · · · a(1)1,n+1 0 a(2)22 a(2)23 · · · a(2)2,n+1 ... 0 a(3)33 · · · a(3)3,n+1 ... ... ... ... 0 0 a(3)n3 · · · a(3)n,n+1
.
From ˜ A
(k−1)to ˜ A
(k), k ≥ 2
For each k≥ 2, suppose augmented matrix ˜A(k−1) has the form
A˜(k−1)=
a(1)11 a(1)12 · · · a(1)1,k−2 a(1)1,k−1 a(1)1k · · · a(1)1,n+1 0 a(2)22 · · · a(2)2,k−2 a(2)2,k−1 a(2)2k · · · a(2)2,n+1
... 0 ... ... ... ...
... ... . .. a(k−2)k−2,k−2 a(k−2)k−2,k−1 a(k−2)k−2,k · · · a(k−2)k−2,n+1 ... .
.. 0 a(kk−1,k−1−1) a(kk−1,k−1) · · · a(kk−1,n+1−1) ... .
.. .
.. a(kk,k−1−1) a(kk,k−1) · · · a(kk,n+1−1)
... ... ... ... ... ...
0 0 · · · 0 a(kn,k−1)−1 a(kn,k−1) · · · a(kn,n+1−1)
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
From ˜ A
(k−1)to ˜ A
(k), k ≥ 2 (Conti’d)
If a(kk−1,k−1−1) ̸= 0, do (
Ei− a(ki,k−1−1)
a(kk−1,k−1−1) Ek−1)
→ (Ei) for k≤ i ≤ n ⇒
A˜(k) =
a(1)11 a(1)12 · · · a(1)1,k−2 a(1)1,k−1 a(1)1k · · · a(1)1,n+1 0 a(2)22 · · · a(2)2,k−2 a(2)2,k−1 a(2)2k · · · a(2)2,n+1
... 0 ... ... ... ...
... .
.. . .. a(kk−2,k−2−2) a(kk−2,k−1−2) a(kk−2,k−2) · · · a(kk−2,n+1−2) ... .
.. 0 a(k−1)k−1,k−1 a(k−1)k−1,k · · · a(k−1)k−1,n+1 ... .
.. .
.. 0 a(k)k,k · · · a(k)k,n+1
... ... ... ... ... ...
0 0 · · · 0 0 a(k)n,k · · · a(k)n,n+1
At Final Stage of GE
When k = n, the GE will produce an augmented matrix in upper
triangular form, i.e., (省略右上標記號)
A˜(n) ≡
a
11 a12 · · · a1,n−1 a1n | a1,n+1 0a
22 · · · a2,n−1 a2n | a2,n+1... 0 . .. ... ... | ... ... ... . .. an−1,n−1 an−1,n | an−1,n+1
0 0 · · · 0
a
nn | an,n+1
.
Note: Entries a
ii̸= 0 are called pivot elements (軸元) for 1≤ i ≤ n.. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Backward Substitution (向後代入)
From the special form of ˜A(n), we obtain the solution x to linear system (1) as follows:
Firstly, compute xn= an,n+1/ann.
Then compute xn−1, xn−2,· · · , x1 successively by
xi =
ai,n+1− ∑n
j=i+1
aijxj
aii , i = n− 1, n − 2, . . . , 1.
Note: This process is called the backward substitution of GE.
Backward Substitution with n = 3
Applying Backward Substitution for the 3× 3 linear system a11x1+ a12x2+ a13x3= b1
a22x2+ a23x3 = b2 a33x3 = b3,
the unique solution to above linear system is computed via x3= b3/a33,
x2= b2− a23x3 a22 , x1= b3− a12x2− a13x3
a11 .
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Will GE break down? (1/2)
Example 2, p. 363 (GE 無法執⾏的例⼦) Use GE to find the solution of the linear system
E1 : x1− x2+ 2x3− x4=−8, E2 : 2x1− 2x2+ 3x3− 3x4 =−20, E3 : x1+ x2+ x3 =−2,
E4 : x1− x2+ 4x3+ 3x4 = 4.
Sol: Form the augmented matrix ˜
A(1) asA˜(1) =
1 −1 2 −1 | −8
2 −2 3 −3 | −20
1 1 1 0 | −2
1 −1 4 3 | 4
.
Will GE break down? (2/2)
Since a(1)11 = 1, do (Ei− a(1)i1 E1)→ Ei for i = 2, 3, 4, we have
A˜(2) =
1 −1 2 −1 | −8
0
0
−1 −1 | −40 2 −1 1 | 6
0 0 2 4 | 12
.
Because a(2)22 = 0, GE will break down here and STOP!
How to fix it? Partial Pivoting Strategy!
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Partial Pivoting (1/2)
If we do (E2)↔ (E3), then ˜A(2) becomes
A˜(3) =
1 −1 2 −1 | −8
0
2
−1 1 | 60 0 −1 −1 | −4
0 0 2 4 | 12
.
Applying (E4+ 2E3)→ (E4) =⇒
A˜(4) =
1 −1 2 −1 | −8
0
2
−1 1 | 60 0 −1 −1 | −4
0 0 0 2 | 4
.
Partial Pivoting (2/2)
Use backward substitution ⇒ x4= 4
2 = 2
x3= [−4 − (−1)x4]
−1 = 2
x2= [6− (−1)x3− x4]
2 = 3
x1= [−8 − (−1)x2− 2x3− (−1)x4]
1 =−7.
It seems that GE with partial pivoting works well in this case!
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Pseudocode of Gaussian Elimination
To solve the n× n linear system (1).
Algorithm 6.1: GE with Backward Substitution INPUT dimension n; augmented matrix A = [aij]∈ Rn×(n+1). OUTPUT solution x1, x2,· · · , xn.
Step 1 For i = 1, . . . , n− 1 do Steps 2–4 Step 2 Find smallest i≤ p ≤ n s.t. api̸= 0.
If not, OUTPUT(‘No unique solution exists.’); STOP.
Step 3 If p̸= i, perform (Ep)↔ (Ei).
Step 4 For j = i + 1, . . . , n do Steps 5–6 Step 5 Set mji= aji/aii.
Step 6 Perform (Ej− mjiEi)→ (Ej).
Step 7 If ann = 0, OUTPUT(‘No unique solution exists.’); STOP.
Step 8 Set xn= an,n+1/ann. (Start backward substitution.) Step 9 For i = n− 1, . . . , 1 set xi= [ai,n+1−∑n
j=i+1aijxj]/aii. Step 10 OUTPUT(x1, x2,· · · , xn); STOP.
Operation Counts in Steps 5 and 6
Multiplications/Divisions:
n−1
∑
i=1
[(n− i) + (n − i)(n − i + 1)] =
n−1
∑
i=1
(n− i)(n − i + 2)
=
n−1
∑
i=1
(n2− 2ni + i2+ 2n− 2i) =
2n
3+ 3n2− 5n6
.Additions/Subtractions:
n−1
∑
i=1
(n− i)(n − i + 1) =
n−1
∑
i=1
(n2− 2ni + i2+ n− i) =
n
3− n3
.. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Operation Counts in Steps 8 and 9
Multiplications/Divisions:
1 +
n−1
∑
i=1
[(n− i) + 1] = 1 +
n−1
∑
i=1
(n− i) + (n − 1) =
n
2+ n2
.Additions/Subtractions:
n−1
∑
i=1
[(n− i − 1) + 1] =
n−1
∑
i=1
(n− i) =
n
2− n2
.Tota Number of Arithmetic Operations
Multiplications/Divisions:
2n3+ 3n2− 5n
6 +n2+ n 2 =
n
33
+ n2−n 3
.Additions/Subtractions:
n3− n
3 +n2− n 2 =
n
33
+n
22
−5n
6
. Total flop of GE ≈O(23n3) (as n→ ∞).. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Section 6.2
Pivoting Strategies
Occurrence of Small Pivot Elements (1/2)
Example 1, p. 372
Apply GE to solve the linear system
E1 :
0.003000x
1+ 59.14x2 = 59.17 E2 : 5.291x1− 6.130x2 = 46.78,using 4-digit rounding arithmetic and the exact solution is
x
1 = 10.00 and x2 = 1.000.Sol: Note that m
21= fl(0.0030005.291 ) = fl(1763.6¯6) = 1764. Then do (E2− m21E1)→ (E2)⇒fl(−6.130 + fl(−1764 · 59.14))x2= fl(46.78 + fl(−1764 · 59.17)) or−104300x2=−104400 and hence x2 ≈ 1.001.
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Occurrence of Small Pivot Elements (2/2)
Substituting x2 = 1.001 into E1, we obtain x1 ≈ fl(59.17− (59.14)(1.001)
0.003000
)
=−10.00, which gives a totally wrong answer for x1! Why?
Partial Pivoting (部分選軸元)
For each i = 1, 2, . . . , n− 1, find smallest integer p ≥ i s.t.
|a(i)pi| = max
i≤j≤n|a(i)ji |.
Then perform the row operation
(Ei)↔ (Ep) if p̸= i.
This strategy is also called maximal column pivoting.
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Improvement of Accuracy for Example 1
Example 2, p. 373 (改進例題 1 的計算精度)
Apply GE with partial pivoting to solve the linear system E1 :
0.003000x
1+ 59.14x2 = 59.17 E2 : 5.291x1− 6.130x2 = 46.78, using 4-digit rounding arithmetic.Sol: Since
|a11| < |a21|, we first perform (E1)↔ (E2):E1 : 5.291x1− 6.130x2 = 46.78 E2 : 0.003000x1+ 59.14x2= 59.17.
Then m21= fl(0.0030005.291 ) = 0.0005670. Do
(E2− m21E1)→ (E2)⇒ 59.14x2≈ 59.14 and hence x2= 1.000.
Moreover, x1 = 10.00 is correct now!
To solve the n× n linear system (1).
Algorithm 6.2: GE with Partial Pivoting
INPUT dimension n; augmented matrix A = [aij]∈ Rn×(n+1). OUTPUT solution x1, x2,· · · , xn.
Step 1 For i = 1, . . . , n− 1 do Steps 2–5
Step 2 Find smallest i≤ p ≤ n s.t.|api| = maxi≤j≤n|aji|.
Step 3 Ifapi= 0, OUTPUT(‘No unique solution exists.’); STOP.
Step 4 If p̸= i, perform (Ep)↔ (Ei).
Step 5 For j = i + 1, . . . , n do Steps 6–7 Step 6 Set mji= aji/aii.
Step 7 Perform (Ej− mjiEi)→ (Ej).
Step 8 If ann = 0, OUTPUT(‘No unique solution exists.’); STOP.
Step 9 Set xn= an,n+1/ann. (Start backward substitution.) Step 10 For i = n− 1, . . . , 1 set xi= [ai,n+1−∑n
j=i+1aijxj]/aii. Step 11 OUTPUT(x1, x2,· · · , xn); STOP.
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Occurrence of Coefficients of Large Magnitude
Example 1’
Apply GE with partial pivoting to solve the linear system E1 :
30.00x
1+ 591400x2 = 591700 E2 : 5.291x1− 6.130x2 = 46.78,using 4-digit rounding arithmetic. Exact solution is x1 = 10.00 and x2= 1.000.
Sol: Note that
m21= fl(5.291
30.00) = 0.1764.
Perform (E2− m21E1)→ (E2)⇒ −104300x2 ≈ −104400. Hence,
x
2 ≈ 1.001 and x1 ≈ −10.00! Why?Scaled Partial Pivoting
At the start of GE, compute n scale factors once as follows.
si = max
1≤j≤n|aij|, i = 1, 2, . . . , n.
For each i = 1, 2, . . . , n− 1, find smallest integer p ≥ i s.t.
|a(i)pi|
sp = max
i≤j≤n
|a(i)ji | sj . If i̸= p, perform (Ei)↔ (Ep).
This strategy is also called scaled-column pivoting.
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Example 2’
Apply GE with scaled partial pivoting to solve the linear system E1 :
30.00x
1+ 591400x2 = 591700E2 : 5.291x1− 6.130x2 = 46.78, using 4-digit rounding arithmetic.
Sol: First compute scale factors s
1= 591400 and ss2 = 6.130. For i = 1, we see that|a11|
s1 = fl( 30.00
591400) = 0.5073× 10−4< |a21|
s2 = fl(5.291
6.130) = 0.8631.
So, perform (E2)↔ (E1)⇒ we obtain the correct solution x1 = 10.00 and x2 = 1.000!
To solve the n× n linear system (1).
Algorithm 6.3: GE with Scaled Partial Pivoting INPUT dimension n; augmented matrix A = [aij]∈ Rn×(n+1). OUTPUT solution x1, x2,· · · , xn.
Step 1 For i = 1, . . . , n set si = max1≤j≤n|aij|;
Ifsi= 0, OUTPUT(‘No unique solution exists.’); STOP.
Step 2 For i = 1, . . . , n− 1 do Steps 3–6
Step 3 Find smallest i≤ p ≤ n s.t. |aspip|= maxi≤j≤n|asji|
j .
Step 4 Ifapi= 0, OUTPUT(‘No unique solution exists.’); STOP.
Step 5 If p̸= i, perform (Ep)↔ (Ei).
Step 6 For j = i + 1, . . . , n do Steps 7–8 Step 7 Set mji= aji/aii.
Step 8 Perform (Ej− mjiEi)→ (Ej).
Step 9 If ann = 0, OUTPUT(‘No unique solution exists.’); STOP.
Step 10 Set xn= an,n+1/ann. (Start backward substitution.) Step 11 For i = n− 1, . . . , 1 set xi= [ai,n+1−∑n
j=i+1aijxj]/aii.
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Complete Pivoting
For each k = 1, 2, . . . , n− 1, find integers k ≤ p, q ≤ n s.t.
|a(k)pq| = max
k≤i,j≤n|a(k)ij |.
If p̸= i or q ̸= i, row and/or column interchanges are performed to bring a(k)pq to the pivot position a(k)kk.
This strategy is also called the maximal pivoting at the kth step.
Section 6.5
Matrix Factorization
(矩陣分解)
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
Motivation
For solving a linear system Ax = b, it requires O(13n3) arithmetic operations to determine x∈ Rn.
If the right-hand vector b∈ Rn is changed to another vector ˜b (and coeff. matrix A is unchanged), how can we solve this linear system efficiently using some matrix factorization of A generated from GE?
In fact, if A has been factored into the triangular form A = LU,
where L is lower triangular and U is upper triangular, then the operation counts can be reduced toO(2n2)!
Comparison of Arithmetic Calculations
The relative rate of reduction of the operation counts O(2n2) compared with O(13n3) becomes larger and larger for n = 10, 102 and 103, respectively. The results are shown in the following table.
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
The 1st Step of GE
Let A(1)= A∈ Rn×n and b(1) = b∈ Rn for a linear system.
If a(1)1,1 ̸= 0, do(
Ei− a(1)i,1
a(1)1,1E1)
→ (Ei) for i = 2, 3, . . . , n⇒
A(2) =
a(1)1,1 a(1)1,2 · · · a(1)1,n 0 a(2)2,2 · · · a(2)2,n ... ... ... 0 a(2)n,2 · · · a(2)n,n
, b(2) =
b(2)1 b(2)2 ... b(2)n
.
The corresponding multipliers are given by
mi,1= a(1)i,1
a(1)11 , i = 2, 3, . . . , n.
The 1st Step of GE (Conti’d)
This is equivalent to
A(2)= M(1)A(1) and b(2)= M(1)b(1), where the first Gaussian transformation matrix M(1)∈ Rn×n is defined by
M(1)=
1 0 · · · 0
−m2,1 1 0 · · · 0
−m3,1 0 . .. ... ...
... ... . .. ... 0
−mn,1 0 · · · 0 1
.
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
At the kth Step of GE, 2 ≤ k ≤ n − 1
If a(k)k,k ̸= 0, do( Ei− a
(k) i,k
a(k)k,kEk)
→ (Ei) for k + 1≤ i ≤ n ⇒
A(k+1) =
a(1)1,1 · · · a(1)1,k a(1)1,k · · · a(1)1,n 0 . ..
.. .
.. .
.. . ..
. a(k)k,k a(k)k,k+1 · · · a(k)k,n ..
. 0 a(k+1)k+1,k+1 · · · a(k+1)k+1,n ..
.
.. .
.. .
.. . 0 · · · 0 a(k+1)n,k+1 · · · a(k+1)n,n
, b(k+1)=
b(k+1)1 b(k+1)2
.. . b(k+1)n
.
The corresponding multipliers are given by
mi,k= a(k)i,k
a(k)k,k, i = k + 1, k + 2, . . . , n.
At the kth Step of GE, 2 ≤ k ≤ n − 1 (Conti’d)
This is equivalent to
A(k+1) = M(k)A(k) and b(k+1)= M(k)b(k), where the kth Gaussian transformation matrix M(k)∈ Rn×n is defined by
M(k)=
1
0 · · · · · · 00 . .. . .. ...
...
1
0 ...... −mk+1,k
1
. .. ...... ... . .. 0
0 −mn,k
1
.
. . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . .
. .
. . . . .
The Inverse Matrix of M
(k)It is easily seen that the inverse matrix of M(k) is given by
L(k)= [M(k)]−1=
1
0 · · · · · · 00 . .. . .. ...
...
1
0 ...... mk+1,k
1
. .. ...... ... . .. 0
0 mn,k
1
(2)
for each k = 1, 2, . . . , n− 1.
Check . . .
M(k)L(k)= I or L(k)M(k)= I, where I denotes the n× n identity matrix.