Unit 2
Unit 2
1
A more complex example of a special structure is an upper triangular matrix
a11 a12 . . . a1n
. .
a22 . . ..
A=
.. .
. ..
ann
where all elements above the main diagonal are zero: aij = 0 for all i > j . In this case each
equation is possibly coupled only to those following it but not to those preceding it. Thus, we can
solve an upper triangular system of equations backwards.
f or k = n : −1 : 1
bk − nj=k+1 akj xj
P
xk =
akk
end
where all elements above the main diagonal are zero: aij = 0 for all i < j . The situation is similar
to the upper triangular case, except that we apply forward substitution to compute a solution.
The algorithm is given on this page.
f or k = 1 : n
Pk−1
bk − j=1 akj xj
xk =
akk
end
2
0.1.3 Gaussian elimination
We use elementary row transformations to reduce the given linear system to an equivalent upper
triangular form. Then we solve the resulting system using backward substitution. The reason this
works is that the solution to our problem Ax = b is invariant to
• row interchanges.
These claims can be easily verified directly. Simply recall that Ax = b is a concise form for the
system of equations
This produces
a11 a12 · · · a1n | b1
0 a(1) (1)
· · · a2n
(1)
| b2
22
(A(1) |b(1) ) = ..
.. . . . .. ..
. . . | .
(1) (1) (1)
0 an2 · · · ann | bn
• Next, consider the (n − 1) × n submatrix of (A(1) |b(1) ) obtained by ignoring the first row and
column. These are the elements that have been modified by the first stage of the process,
described above, but not set to 0. Apply exactly the same step as before to this submatrix;
3
a32
i.e., subtract (1) × the second row from the third row, etc.
a22
After this second stage we have an augmented matrix of the form
a11 a12 a13 · · · a1n | b1
0 a(1)
22
(1) (1)
a23 · · · a2n
(1)
| b2
(2) (2) (2)
(A(2) |b(2) ) = 0
0 a33 · · · a3n | b3
. .. .. . . . ..
.. . . . .. | .
(2) (2) (2)
0 0 an3 · · · ann | bn
The superscripts in the above expression show for each element at which stage it has last
been modified.
• Repeat the process. After n − 1 such stages we obtain an upper triangular system
a11 a12 a13 · · · a1n | b1
0 a(1)22
(1) (1)
a23 · · · a2n | b2
(1)
(2) (2) (2)
(A (n−1) (n−1)
|b
0
)= 0 a 33 · · · a3n | b 3
. . . . . .
. . . . . .
. . . . . | .
(n−1) (n−1)
0 0 0 · · · ann | bn
This procedure leads to the Gaussian elimination algorithm in its simplest form given on the
next page. In stating the Gaussian elimination algorithm we have not bothered to zero out
the elements below the main diagonal in the modified matrix A: this part of the matrix is
not considered in the ensuing backward substitution. Of course, for this algorithm to work
(k−1)
we need to assume that all those pivotal elements akk by which we divide are nonzero and
in fact not very close to 0 either. We deal with the question of ensuring this later
– Algorithm: Gaussian Elimination.
Given a real, nonsingular n × n matrix A and a vector b of size n, first transform into
upper triangular form,
f or k =1 : n − 1
f or i = k + 1 : n
aik
lik =
akk
f or i = k + 1 : n
aij = aij − lik akj
end
bi = bi − lik bk
end
end
• The cost of the Gaussian elimination algorithm in terms of operation count is approximately
n−1 n−1
X X 2
((n − k) + 2(n − k)(n − k + 1)) ≈ 2 (n − k)2 = n3 + O(n2 )
k=1 k=1
3
flops.
4
0.2 LU Decomposition
In this section we show that the process of Gaussian elimination in fact decomposes the matrix A
into a product L × U of a lower triangular matrix L and an upper triangular matrix U . Let us
(k−1)
continue to assume that the elements akk encountered in the Gaussian elimination process are
all bounded safely away from 0. We will remove this assumption in the next section.
• Elementary lower triangular matrices:
Consider the first stage of Gaussian elimination described above. These are elementary row
opera- tions applied to the matrix A to zero out its first column below the (1, 1) entry.
These operations are also applied to the right-hand-side vector b. Note, however, that the
operations on b can actually be done at a later time because they do not affect those done
on A. We can capture the effect of this first stage by defining the elementary n × n lower
triangular matrix
1
−l21 1
(1) −l31 1
M =
.. ...
.
−ln1 1
Likewise, the effect of the second stage of Gaussian elimination, which is to zero out the
second column of A(1) below the main diagonal, can be written as
A(2) = M (2) A(1) = M (2) M (1) A and b(2) = M (2) b(1) = M (2) M (1) b
where
1
1
−l32 1
M (2) =
−l42 1
.. ..
. .
−ln2 1
Likewise,
b(n−1) = M (n−1) · · · M (2) M (1) b.
Multiplying U by [M (n−1) ]−1 , then by [M (n−2) ]−1 , etc., we obtain
A = LU
5
where
L = [M (1) ]−1 · · · [M (n−2) ]−1 [M (n−1) ]−1
.
– The Gaussian elimination process for the first column yields l21 = 1/1 = 1 and l31 =
3/1 = 3, so
1 0 0 1 −1 3
M (1) = −1 1 0 , A(1) = M (1) A = 0 2 −3
−3 0 1 0 1 −8
– The Gaussian elimination process for the second column yields l32 = 1/2, so
1 0 0 1 −1 3
M (2) = 0 1 0 , U = A(2) = M (2) A(1) = 0 2 −3
0 −1/2 1 0 0 −6.5
– Note that
1 0 0 1 0 0
[M (1) ]−1 = 1 1 0 , [M (2) ]−1 = 0 1 0 ,
3 0 1 0 1/2 1
1 0 0
[M (1) ]−1 [M (2) ]−1 = 1 1 0 = L,
3 1/2 1
and
1 0 0 1 −1 3 1 −1 3
LU = 1 1 0 0 2 −3 = 1 1 0 = A.
3 1/2 1 0 0 −6.5 3 −2 1
– This explicitly specifies the LU decomposition of the given matrix A.
• Rather than constructing L and U by using the elimination steps, it is possible to solve
directly for these matrices. Let us illustrate the direct computation of L and U in the case
of n = 3. Write A = LU as
a11 a12 a13 1 0 0 u11 u12 u13
a21 a22 a23 = l21 1 0 0 u22 u23
a31 a32 a33 l31 l32 1 0 0 u33
6
• We can also write A = LU as
a11 a12 a13 l11 0 0 1 u12 u13
a21 a22 a23 = l21 l22 0 0 1 u23
a31 a32 a33 l31 l32 l33 0 0 1
A = LU
Given also a right-hand-side vector b:
A = LDL>
where L is unit lower triangular and D is diagonal with positive elements ukk .
It is also possible to write D = D1/2 D1/2 with
√ √ √
D1/2 = diag( u11 , u22 · · · , unn )
A = GG>
with G = LD1/2 a lower triangular matrix. This is called the Cholesky decomposition.
7
• Next we modify the algorithm of Gaussian elimination by pivoting, thus making it generally
stable. The process of Gaussian elimination (or LU decomposition) described above cannot
be applied unmodified to all nonsingular matrices.
x1 + x2 + x3 = 1
x1 + x2 + 2x3 = 2
x1 + 2x2 + 2x3 = 1.
The matrix A defined by these equations is nonsingular (indeed, the unique solution
is x = (1, −1, 1)> ), also all elements of A are nonzero, and yet Gaussian elimination
yields after the first stage
1 1 1 | 1 1 1 1 | 1
1 1 2 | 2 =⇒ 0 0 1 | 1
1 2 2 | 1 0 1 1 | 0
(1)
Next, a22 = 0 and we cannot proceed further.
In this case there is an obvious remedy: simply interchange the second and the third
rows! This yields
1 1 1 | 1 1 1 1 | 1
1 2 2 | 1 =⇒ 0 1 1 | 0
1 1 2 | 2 0 0 1 | 1
x1 + x2 + x3 = 1
x1 + 1.0001x2 + 2x3 = 2
x1 + 2x2 + 2x3 = 1.
The exact solution, correct to 5 digits, is x ≈ (1, −1.0001, 1.0001)> . Now, Gaussian
elimination in exact arithmetic can be completed and yields
1 1 1 | 1 1 1 1 | 1 1 1 1 | 1
1 1.0001 2 | 2 =⇒ 0 0.0001 1 | 1 =⇒ 0 0.0001 1 | 1
1 2 2 | 1 0 1 1 | 0 0 0 −9999 | −10000
8
Assume next that we are using floating point arithmetic with base β = 10 and precision
t = 3. Then backward substitution gives
x3 = 1, x2 = 0, x1 = 0.
On the other hand, if we interchange the second and third rows we obtain
1 1 1 | 1 1 1 1 | 1 1 1 1 | 1
1 2 2 | 1 =⇒ 0
1 1 | 0 =⇒ 0 1 1 | 0
1 1.0001 2 | 2 0 0.0001 1 | 1 0 0 .9999 | 1
which is correct, in that the difference between this solution and the exact solution is
less than the rounding unit.
• Partial pivoting
The problem highlighted in these simple examples is real and general. Recall from last unit
that roundoff errors may be unduly magnified if divided by a small value. Here, if a pivotal
(k−1)
element akk is small in magnitude, then roundoff errors are amplified, both in subsequent
stages of the Gaussian elimination process and (even more apparently) in the backward
substitution phase.
These simple examples also suggest a strategy to resolve the difficulty, called partial pivoting:
as the elimination proceeds for k = 1, · · · , n − 1, at each stage k choose q = q(k) as the
smallest integer for which
(k−1) (k−1)
|aqk | = max |aik |,
k≤i≤n
and interchange rows k and q. Then proceed with the elimination process.
• We could modify the partial pivoting strategy into one called scaled partial pivoting. Thus,
define initially for each row i of A a size
si = max |aij |.
1≤j≤n
Then, at each stage k of the Gaussian elimination procedure, choose q = q(k) as the smallest
integer
(k−1) (k−1)
|aqk | |aik |
= max
sq k≤i≤n si
and interchange rows k and q.
• A strategy that does guarantee stability is complete pivoting: at each stage choose q and r
as the smallest integers for which
(k−1) (k−1)
|aqr | = max |aij |,
k≤i,j≤n
and interchange both row q and column r with row i and column j, respectively. However,
this strategy is significantly more expensive than partial pivoting.
9
Solution Using Gaussian Elimination with Partial Pivoting
Given the system:
−0.39999
x3 = ≈ −0.17391 ≈ −0.174
2.3
−2.14314 − (−3.6)(−0.174)
x2 = ≈ 0.69238 ≈ 0.692
−4
−0.329193 − 6(−0.174)
x1 = ≈ 0.14296 ≈ 0.143
5
10
Solution of the Linear System Using Gaussian Elimination with Scaling
We solve the system:
3x + 2y + 100z = 105
−x + 3y + 100z = 102
x + 2y − z = 2
|3| | − 1| |1|
= 0.03, = 0.01, = 0.5
100 100 2
Row 3 has largest ratio ⇒ Swap Row 1 and Row 3
New matrix:
0.03 0.02 1 1.05 0.5 1 −0.5 1
−0.01 0.03 1 1.02 → −0.01 0.03 1 1.02
0.5 1 −0.5 1 0.03 0.02 1 1.05
11
Step 5: Partial Pivoting for Column 2
Scaled ratios for Column 2:
|0.05| | − 0.04|
= 0.05, = 0.03
0.99 1.03
No row swap needed.
1.822z = 1.822 ⇒ z = 1
0.05y + 0.99(1) = 1.04 ⇒ y = 1
0.5x + 1(1) − 1(1) = 2 ⇒ x = 1
Step 2: LU Decomposition
Find L and U such that A = LU where:
1 0 0 u11 u12 u13
L = l21 1 0 , U = 0 u22 u23
l31 l32 1 0 0 u33
12
l32 = −2
u33 = −10
Final Solution
x=1, y = 0.5 , z = −0.5
Problem
Assume the LU decomposition of a square matrix A ∈ R500×500 takes 5 seconds. How many
systems of the form
Axi = bi , i = 1, 2, . . . , k
can be solved in the next 6 seconds after the decomposition is complete?
Solution
Once the LU decomposition is computed, each system Ax = b can be solved by performing:
1. Forward substitution to solve Ly = b
2. Backward substitution to solve U x = y
Each substitution step is an O(n2 ) operation, so the total cost to solve one system is propor-
tional to n2 . Let c denote the constant of proportionality. Then the LU decomposition time is
approximately:
2
TLU = n3 · c.
3
Given that TLU = 5 seconds for n = 500, we solve for c:
2 5·3 15
5 = · (500)3 · c ⇒ c = = = 6 × 10−8 .
3 2 · 5003 250 · 106
Now, the time required to solve one linear system is:
Tsolve = 2n2 · c = 2 · (500)2 · 6 × 10−8 = 2 · 250,000 · 6 × 10−8 = 0.03 seconds.
Thus, the number of systems that can be solved in 6 seconds is:
6
k= = 200.
0.03
13
Conclusion
200 systems can be solved in 6 seconds after the LU decomposition is complete.
Conclusion
200 systems can be solved in 6 seconds after the LU decomposition is complete.
Given the system:
3x + 2y + 4z = 7
2x + y + z = 4
3x + 5y + 3z = 3
Step 2: LU Decomposition
Find L and U such that A = LU where:
l11 0 0 1 u12 u13
L = l21 l22 0 , U = 0 1 u23
l31 l32 l33 0 0 1
14
Final Solution
65
x= 3
, y = − 50
3
, z= 4
3
Final Solution
x=1, y=1, z=1
|3| | − 1| |1|
= 0.03, = 0.01, = 0.5
100 100 2
Row 3 has largest ratio ⇒ Swap Row 1 and Row 3
New matrix:
1 2 −1 2
−1 3 100 102
3 2 100 105
15
Step 5: Partial Pivoting for Column 2
Scaled ratios for Column 2:
|5| | − 4|
= 1, =1
5 4
No row swap needed.
182.2z = 182.2 ⇒ z = 1
5y + 99(1) = 104 ⇒ y = 1
x + 2(1) − 1(1) = 2 ⇒ x = 1
Verification
Substitute (x, y, z) = (1, 1, 1) into original equations:
3(1) + 2(1) + 100(1) = 105
−1(1) + 3(1) + 100(1) = 102
1(1) + 2(1) − 1(1) = 2
Vector Norms
A vector norm is a function k · k from Rn to R satisfying:
16
Matrix Norms
A matrix norm kAk satisfies:
kAxkv
kAk = max .
x6=0 kxkv
17
Condition Number of a Matrix
Let A be a nonsingular matrix. The condition number of A with respect to a matrix norm k · k is
defined as
κ(A) = kAk · kA−1 k.
In the 2-norm (spectral norm) this becomes
σmax (A)
κ2 (A) = ,
σmin (A)
where σmax (A) and σmin (A) are the largest and smallest singular values of A, respectively.
A matrix with κ(A) ≈ 1 is called well-conditioned, while a matrix with a large condition
number is called ill-conditioned. The condition number provides an upper bound on the relative
error:
kδxk kδbk
. κ(A) · .
kxk kbk
so
κ2 (B) ≈ 4 × 104 .
18
Definition
A square matrix A = [aij ] ∈ Rn×n is called strictly diagonally dominant if:
n
X
|aii | > |aij |, for all i = 1, 2, . . . , n.
j=1
j6=i
That is, in each row of A, the magnitude of the diagonal entry is strictly greater than the sum of
the magnitudes of the other (non-diagonal) entries.
Interpretation
Geometrically, strict diagonal dominance means that each equation in the linear system Ax = b
has its main variable coefficient significantly larger (in magnitude) than the coefficients of other
variables in that equation. This property often guarantees stability and convergence of iterative
methods such as the Gauss–Seidel and Jacobi methods.
Example
Consider the matrix:
4 −1 0
A = −1 4 −1 .
0 −1 3
Check strict diagonal dominance:
Non-example
The matrix
2 3
B=
1 1
fails the first row test:
|2| >
6 |3| ⇒ Not strictly diagonally dominant.
Applications
Strictly diagonally dominant matrices are important because:
• They guarantee nonsingularity (invertibility).
• They ensure convergence of iterative solvers.
• They often arise from discretizations of PDEs (e.g., Laplace equation).
19
Definition
A real symmetric matrix A ∈ Rn×n is said to be positive definite if:
xT Ax > 0 ∀ x ∈ Rn , x 6= 0.
xT Ax ≥ 0 ∀ x ∈ Rn .
Equivalent Conditions
For a real symmetric matrix A, the following are equivalent:
3. All leading principal minors are positive (Sylvester’s criterion): det(Ak ) > 0 for k =
1, 2, . . . , n, where Ak is the top-left k × k submatrix of A.
Example
Consider the matrix:
2 −1
A= .
−1 2
Eigenvalue check
We solve det(A − λI) = 0:
2 − λ −1
det = (2 − λ)2 − 1 = 0,
−1 2 − λ
20
Principal minors check
det([2]) = 2 > 0,
det(A) = (2)(2) − (−1)(−1) = 4 − 1 = 3 > 0.
Since all leading principal minors are positive, A is positive definite.
A=M −K
(M − K)x = b
M x = Kx + b
x = M −1 Kx + M −1 b
Now, if we have a starting guess x(0) for x, this suggests the iteration
x(k+1) = M −1 Kx(k) + M −1 b.
• Although we do not know for which linear systems the iteration scheme (6.24) converges, if
any, let us tacitly take it as our basic iterative method for solving linear systems.
A=D−L−U
where D is the diagonal of A, and −U and −L the strictly upper and lower triangular part
of A, respectively. This leads to two classical iterative methods, known as the Jacobi and
the Gauss-Seidel methods.
21
0.3.2 The Jacobi Method
Jacobi iteration is defined by choosing M = D and K = L + U , which gives the iteration scheme
We observe that D is easy to invert, since it is a diagonal matrix. For example, n = 3, we have
k+1 −1 k −1
x1 a11 0 −a12 −a13 x1 a11 b1
xk+1 −1 k −1
2
= a22 −a21 0 −a23 x2 + a22 b2 .
k+1 −1 k −1
x3 a33 −a31 −a32 0 x3 a33 b3
Hence, it gives
xk+1
1 = a−1 k k
11 (−a12 x2 − a13 x3 + b1 ),
xk+1
2 = a−1 k k
22 (−a21 x1 − a23 x3 + b2 ),
xk+1
3 = a−1 k k
33 (−a31 x1 − a32 x2 + b3 ).
Let’s solve the following system of linear equations using the Jacobi method:
Ax = b,
where
4 −1 0 x1 15
A = −1 4 −1 , x = x2 ,
b = 10 .
0 −1 4 x3 10
The Jacobi method updates each component of x iteratively as follows:
(k+1) 1 (k)
x1 = 15 + x2 ,
4
(k+1) 1 (k) (k)
x2 =10 + x1 + x3 ,
4
(k+1) 1 (k)
x3 = 10 + x2 .
4
T
Given an initial guess x(0) = 0 0 0 , the iterations proceed until the solution converges.
22
0.3.3 The Gauss-Seidel Method
In the Gauss-Seidel method for M = D − L and K = U , which gives the iteration scheme
We observe that since D − L has a lower triangular structure, the effect of (D − L)−1 can be
computed by forward elimination. For example, n = 3, we have
xk+1
1 = a−1 k k
11 (−a12 x2 − a13 x3 + b1 ),
xk+1
2 = a−1 k+1
22 (−a21 x1 − a23 xk3 + b2 ),
xk+1
3 = a−1 k+1
33 (−a31 x1 − a32 xk+1
2 + b3 ).
Using the Gauss-Seidel method, the iterative updates for each variable are:
(k+1) 1 (k)
x1 = 15 + x2 ,
4
(k+1) 1 (k+1) (k)
x2 = 10 + x1 + x3 ,
4
(k+1) 1 (k+1)
x3 = 10 + x2 .
4
Starting with an initial guess x(0) = (0, 0, 0)> , the iterations proceed as follows:
• Iteration 1:
(1) 1
x1 = (15 + 0) = 3.75,
4
(1) 1
x2 = (10 + 3.75 + 0) = 3.4375,
4
(1) 1
x3 = (10 + 3.4375) = 3.359375.
4
• Iteration 2:
(2) 1
x1 = (15 + 3.4375) = 4.609375,
4
(2) 1
x2 = (10 + 4.609375 + 3.359375) = 4.4921875,
4
(2) 1
x3 = (10 + 4.4921875) = 3.623046875.
4
• Continue iterating until convergence.
23
0.3.4 Convergence Analysis
We now return to the question of convergence of our basic iterative method. By inspection, we
see that it can be rewritten as
x(k+1) = Hx(k) + c (1)
where H = M −1 K is the relation matrix, and c = M −1 b. Let e(k) = x − xk be the error after
k iterations. A relation between the errors in successive approximations can be be derived by
subtracting x = Hx + c from (1)
e(k+1) = xk+1 − x = H(x(k) − x) = · · · = H k+1 (x(0) − x) = H k+1 e0
Here, for convergence, we require H k+1 e → 0 as k → ∞ for any e. It turns out that this
requirement is equivalent to ρ(H) < 1, where ρ(H) = max1≤k≤n |λk (H)| is the so-called spectral
radius of H, that is, the magnitude of the extremal eigenvalue of H.
• Both Jacobi and Gauss-Seidel methods converge if A is strictly diagonally dominant.
• The Gauss-Seidel method also converges if A is SPD.
Convergence Condition
The Jacobi method converges if and only if
ρ(HJ ) < 1,
where ρ(·) denotes the spectral radius.
Sufficient Condition
If A is strictly diagonally dominant, i.e.,
X
|aii | > |aij |, ∀i,
j6=i
then P
|aij |
j6=i
kHJ k∞ = max <1
i |aii |
and hence ρ(HJ ) < 1, guaranteeing convergence.
24
Convergence of the Gauss-Seidel Method
For the Gauss-Seidel method, the iteration matrix is given by:
HGS = (D − L)−1 U,
where A = D − L − U is the decomposition of matrix A into its diagonal (D), strictly lower
triangular (L), and strictly upper triangular (U ) parts.
The eigenvalues λ of HGS satisfy the characteristic equation:
det(λD − λL − U ) = 0.
Let us denote:
Aλ = λD − λL − U.
However, the eigenvalue equation requires det(Aλ ) = 0. This leads to a contradiction unless
|λ| < 1.
Conclusion: All eigenvalues λ of HGS must satisfy |λ| < 1, which guarantees the convergence
of the Gauss-Seidel method for strictly diagonally dominant matrices.
25
Extension to the Jacobi Method
The proof for the Jacobi method follows a similar reasoning. The iteration matrix for Jacobi is:
HJ = D−1 (L + U ).
26
Iteration 1
(1) 1.25
x1 = (15 − (−1) · 0 − 0) = 4.6875
4
(1) 1.25
x2 = (10 − (−1) · 4.6875 − (−1) · 0) = 4.78515625
4
(1) 1.25
x3 = (10 − (−1) · 4.78515625) = 4.9932861328125
4
Thus,
4.6875
x(1) = 4.78515625 .
4.9932861328125
Iteration 2
(2) 1.25
x1 = (1 − 1.25) · 4.6875 + (15 − (−1) · 4.78515625 − 0) = 4.95758056640625
4
(2) 1.25
x2 = (1−1.25)·4.78515625+ (10−(−1)·4.95758056640625−(−1)·4.9932861328125) = 5.00056266784
4
(2) 1.25
x3 = (1−1.25)·4.9932861328125+ (10−(−1)·5.00056266784668) = 5.006518870592117
4
Thus,
4.95758056640625
x(2) = 5.00056266784668 .
5.006518870592117
27