Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
M
I
Updated @2016
@2016 and by mikelab.net
Unauthorized reproduction is prohibited.
Numerical Linear
Algebra
204111 Computer & Programming
Arnon Rungsawang
http://mike.cpe.ku.ac.th/204111
Department of Computer Engineering
Kasetsart University
Bangkok, Thailand.
Linear Equations
2x1 + x2 -3x3 = -1
-x1 + 3x2 +2x3 = 12
3x1 + x2 -3x3 = 0
x1 + 2x2 +3x3 + x4 = 0
-4x1 + 5x2 +4x3
= 1
-5x1 + 3x2 +3x3
= 0
x4 = -1
0.04x1 + 0.04x2 + 0.12x3 = 3
0.56x1 1.56x2 + 0.32x3 = 1
0.24x1 + 1.24x2 0.28x3 = 0
M I K E
2x1 + x2 -3x3 = -1
-x1 + 3x2 +2x3 = 12
3x1 + x2 -3x3 = 0
M I K E
Gauss Elimination
Gauss elimination applies only to inhomogeneous sets of equations.
equations
It consists of (a) forward elimination,
elimination and (b) backward substitution.
substitution
The forward elimination proceeds as follows:
Every equation i, where i >= 2 is subtracted from the first equation
times ai1/a11
a21
a
a
a
) x1 (a22 a12 21 ) x2 ... (a2 N a1N 21 ) xN y2 21 y1
a11
a11
a11
a11
...
(a N1 a11
M I K E
aN1
a
a
a
) x1 (a N 2 a12 N1 ) x2 ... (a NN a1N N 1 ) xN y N N 1 y1
a11
a11
a11
a11
Gauss Elimination
forward elimination
aN1
a
a
a
) x1 (a N 2 a12 N1 ) x2 ... (a NN a1N N 1 ) xN y N N 1 y1
a11
a11
a11
a11
aij' aij a1 j
ai1
a11
...
'
a 'N 2 x2 a N' 3 x3 ... a NN
xN y N'
M I K E
Gauss Elimination
forward elimination
...
'
a 'N 2 x2 a N' 3 x3 ... a NN
xN y N'
ai' 2
a a a
'
a22
''
ij
'
ij
'
2j
...
''
aN'' 3 x3 ... aNN
xN y N''
M I K E
Gauss Elimination
backward substitution
'
'
a22
x2 a23
x3 ... a2' N xN y2'
...
a NNN 1 xN y NN 1
The backward substitution procedure starts with the last equation.
The solution of xn is obtained from the last equation:
xN y NN 1 / a NNN 1
x1 ( y1 a1 j x j ) / a11
j 2
M I K E
Gauss Elimination
Gauss elimination can be carried out by writing only the coefficients
and the right-side terms in an array form.
a11 a12 a13 a1N-1 a1N y1
a21 a22 a23 a2N-1 a2N y2
Forward elimination
N-1
0 0
0 0
a NN yN
M I K E
1
3
1
-3 -1
2 12
-3 0
2
1
-3
0
7/2 1/2
0 -1/2 3/2
-1
23/2
3/2
2
1
-3
0
7/2 1/2
0 -1/2 3/2
2
0
0
-1
23/2
3/2
1
-3
7/2 1/2
0 11/7
-1
23/2
22/7
M I K E
Guass-Jordan Elimination
GuassGuass-Jordan elimination is a variant of Gauss elimination.
It shares with Gauss elimination, the same forward elimination
process, but is different in the backward process.
The backward process of Gauss-Jordan elimination is called
backward elimination.
elimination
Starting from the last forward elimination processes:
a11 a12
a13 ...
a1N 1
a1N
y1
'
a22
'
a23
...
a2' N 1
a2' N
y2'
''
a33
...
a3'' N 1
a3'' N
y3''
...
M I K E
...
... a NN12N 1
a NN12N
y NN12
...
a NNN 1
y NN 1
10
Guass-Jordan Elimination
backward elimination
a11 a12
a13 ...
a1N 1
a1N
y1
a11 a12
a13 ...
a1N 1
a1N
y1
'
a22
'
a23
...
a2' N 1
a2' N
y2'
'
a22
'
a23
...
a2' N 1
a2' N
y2'
''
a33
...
a3'' N 1
a3'' N
y3''
N 1
0
yN yNN 1 / aNN
''
a33
...
a3'' N 1
a3'' N
y3''
...
...
...
...
... a NN12N 1
a NN12N
y NN12
... a NN12N 1
...
a NNN 1
y NN 1
...
a11 a12
0
'
a22
a NN12N
y NN12
yN
y1
a11 a12
a13 ...
a1N 1
y1
'
a23
... a2' N 1 0
y2
'
a22
'
a23
...
a2' N 1
y2
''
a33
... a3'' N 1
y3 y ' y / a
...
''
a33
...
a3'' N 1
y3
...
N 1
N 1
N 2
N 1 N 1
...
...
...
y N' 1
... a NN12N 1
y N 1
...
yN
...
yN
M I K E
11
Guass-Jordan Elimination
a11 a12
0
'
a22
backward elimination
y1
a11 a12
a13 ... 0 0
y1'
'
a23
... a2' N 1 0
y2
'
a22
'
a23
... 0 0
y2'
''
a33
... a3'' N 1
y3
''
a33
... 0 0
y3'
...
...
...
...
...
y N' 1
... 1 0
y N' 1
...
yN
... 0 1
yN
Final solution:
M I K E
xi yiN i
1 0 0 ... 0 0
y1N 1
0 1 0 ... 0 0
y2N 2
0 0 1 ... 0 0
y3N 3
...
...
0 0 0 ... 1 0
y N' 1
0 0 0 ... 0 1
yN
12
0.04 x1
0.56 x1
0.04 x2
1.56 x2
0.12 x3
0.32 x3
3
1
0.24 x1
1.24 x2
0.28 x3
M I K E
1
2
3
1 times 0.56/(-0.04):
1 time 0.24/(-0.04):
0.04
0.56
0.24
0.04
0.12
1.56
0.32
1.24 0.28
0.56 0.56
0.24
0.24
3
1
0
1.68 42
0.72
18
(A)
(B)
13
M I K E
x3
25 / 1 25
x2
[43 2(25)] /( 1) 7
x1
14
row 1
row 2
row 3
row 1
row 2
row 3
0.333 0.333 1 25
row 1
row 2
row 3
1 1 0 0
0 1 0 7
0 0 1 25
M I K E
2 43
1 25
1 0
0 1 25
row 1
row 2
row 3
25
row 1
row 2
row 3
row 1
row 2
row 3
1 0 0 7
0 1 0
7
0 0 1 25
0 7
1 25
15
Pivoting
Solve the following array of eqations:
0 10
1 2
1 3 1 6
2 4
1 5
0 10
1 2
1 3 1 6
2 4
1 5
M I K E
2 4
1 5
1 3 1 6
0 10
1 2
2 4
1
5
0 1 3/ 2 7 / 2
0 10
1
2
2 4
1
5
0 10
1
2
0 1 3/ 2 7 / 2
x3
x2
x1
(5 4 x2 x3 ) / 2 2.7187
0 10
0 16 / 10 33 / 10
16
Your exercises
(as homework)
1.334 E 4 4.123E 1
1.777 2.367 E 5
9.188
0 1.015E 1
1.002 E 2 1.442 E 4 7.014 E 2
1.988E 4
5.321
0.9618012
13824.121
(a) Using only single precision (float), solve the equations without
pivoting and then with pivoting.
(b) Repeat by using double precision (double).
M I K E
17
Unsolvable Problems
A set of linear equations is not always numerically solvable. The following
three sets of equations are simple but important examples:
(a)
(b)
(c )
2x 2 y
x 2y 2
2x
y
0
Infinite number
of solutions
No solutions
No solutions
M I K E
18
2x 2 y 2
y
Infinite number
of solutions
M I K E
19
y 1
y 0
No solutions
M I K E
20
x 2y 2
2x
No solutions
M I K E
21
Inversion of a Matrix
Consider a linear equation in matrix notation:
Ax y
where A is a square matrix.
A pre-multiplication by a square matrix G yields:
GAx Gy
If G is chosen to be the inverse of A, namely A-1, the above equation
reduces to:
x A1 y
GI A1
M I K E
22
a11 a12
a21 a22
a13 1 0 0
a23 0 1 0
a31 a32
a33 0 0 1
M I K E
23
2 1 3
A 1 3
2
3 1 3
We first write A and I in one array:
2 1 3 1 0 0
1 3
2 0
1 0
3 1 3 0 0 1
M I K E
24
2 1 3 1 0 0
1 3
2 0
1 0
3 1 3 0 0 1
1 3
1 0 0
3.5 0.5
0.5 1 0
Now the second row times 0.5/3.5 is subtracted from the third row:
1 3
1 0 0
3.5 0.5
0.5 1 0
M I K E
0 0
0 3.5
0.5
0.5
1 0
2
0
25
0 0
1 3
0 3.5
0.5
0.5
1 0
0 3.5 0.5
0.5
2
0
The second row is subtracted by 0.5 times the last row, the first row is
added by 3 times the last row, and the second row is divided by 3.5:
1 3
2 1 0
0 3.5 0.5
0.5
0 1 0
2
0
M I K E
1.72727 0.27272
0 0 1 0.90909 0.09090
1.90908
0.63636
26
1.72727 0.27272
1.90908
1 0 0
0 1 0
0 0 1 0.90909 0.09090
0.63636
0 0 1 0.90909 0.09090
0.63636
2 1 3
A 1 3
2
3 1 3
M I K E
1
0
1
27
LU Decomposition
The LU decomposition scheme is a transformation of a matrix A to a
product of two matrices:
A=LU
where L is a lower triangular matrix and U is the upper triangular
matrix.
The LU decomposition of a 3x3 matrix is illustrated as:
a11 a12
a
21 a22
a31 a32
M I K E
28
LU Decomposition (2)
a11 a12
a
21 a22
a31 a32
To evaluate uij and lij, we first multiply the first row of L by each
column of U, and compare the result to the first row of A, and it was
found that the first row of U is identical to that of A:
u1j=a1j, j=1 to 3
M I K E
29
LU Decomposition (3)
a11 a12
a
21 a22
a31 a32
Multiplying the second and the third row of L by the first column of U,
respectively, and comparing to the left side yields:
a21= l21u11, a31=l31u11
or equivalently:
l21 = a21/u11, l31 = a31 / u11
M I K E
30
LU Decomposition (4)
a11 a12
a
21 a22
a31 a32
Multiplying the second row of L by the second and the third column of
U and comparing to the left side yields:
M I K E
31
LU Decomposition (5)
a11 a12
a
21 a22
a31 a32
M I K E
32
LU Decomposition (6)
a11 a12
a
21 a22
a31 a32
M I K E
33
Example of LU computation
Decompose the following matrix into L and U ones:
2 1 3
A 1 3
2
3 1 3
1
0 0
1
3
2
L 0.5
1 0, U 0 3.5
0
1.5 0.1428 1
0
0 1.5714
M I K E
34
LU decomposition of AN
The first row of U, u1j for j=1 to N, is obtained by
u1 j a1 j , j 1 to N
The first column of L, li1 for i=2 to N, is obtained by
u2 j a2 j l21u1 j , j 2 to N
The second column of L is obtained by
M I K E
35
LU decomposition of AN (2)
The nth row of U is obtained by
n 1
All diagonal elements of L are not calculated, as they are all unity.
M I K E
36
LU decomposition of AN (3)
As the elements in the upper triangular part of L and the elements in
the lower triangular part of U are all zero, and the diagonal part of L
are all unity, we can store both L and U of a big matirx in one array
to save memory space.
1
0
u
21
22
23
l31 l32 1 0
0 u33
u
u
21
22
23
M I K E
37
2
l31 l32 1 0
0 u33 x3 y3
This equation can be solved, by setting:
Ux=z
and the underlying linear equations become:
Lz=y
M I K E
38
1 0 0 z1 y1
l
z y
1
0
21
2 2
l31 l32 1 z3 y3
The solution is calculated recursively as:
z1 y1
z2 [ y2 z1l21]
z3 [ y3 z1l31 z 2l32 ]
M I K E
39
2
0
0 u33 x3 z3
The solution becomes:
z3
x3
u33
z 2 u23 x3
x2
u22
z1 u12 x2 u13 x3
x1
u11
M I K E
40
z1 y1
i 1
zi yi lij z j , i 2,3,...N
j 1
xN
zN
u NN
zi uij x j
j i 1
, i N 1, N 2,...,3,2,1
xi
uii
M I K E
41
Determinant
A practical way of calculating the determinant is to use the forward
elimination process of Gauss elimination or, alternatively, the LU decomposition. But, we first look at two important rules of determinants:
Rule1
Rule
1: det
det(BC)
(BC) = det
det(B)
(B)det
det(C)
(C)
which means tht the determinant of a product of matrices is the
product of the determinants of all the matrices.
Rule2
Rule
2: det
det(M)
(M) = the product of all diagonal elements of M
if M is upper or lower triangular matrix.
If no pivoting (exchange between pair of rows) is used, calculation of
the determinant using the LU decomposition is straightforward.
According to Rule1 and Rule2, the determinant can be written as:
N
ii
i 1
M I K E
42
Determinant (2)
When the pivoting is used in the LU decomposition, its effect should
be taken into consideration.
First, we recognize that the LU decomposition with pivoting is
equivalent to performing two separate processes:
(1) transform A to A by performing all shuffling of rows (pivoting
pivoting),
(2) then decompose A to LU with no pivoting.
M I K E
43
Determinant (3)
The determinant of A may now be written as:
det(A) = det(P-1)det(L)det(U)
or equivalently:
det(A) = det(U)
where det(L)=1 is used and = det(P-1) equal +1 or 1 depending on
whether the number of pivotings is odd or even, respectively.
The determinant of a matrix may also be calculated during the process
of Gauss elimination. This is because when the forward elimination is
completed, the original matrix has been transformed to the U matrix of
the LU decompostion.
Therefore, the determinant can be calculated by taking the product of
all the terms along the diagonal line and then multiplying by 1 or 1
depending on whether the number of pivoting operations is performed.
performed
M I K E
44
2 1 3
A 1 3
2
3 1 3
After the forward elimination (with no pivoting), the upper triangular
matrix is:
1
3
2
0 3.5
0
0 1.5714
Therefore, we get
det(A) = (2)(3.5)(1.5714) = 11
M I K E
45
Ill-conditioned Problems
Ill-conditioned problems are solvable, but their solutions become very
Illinaccurate because of severe round-off errors.
Small round-off errors or changes in coefficients can cause significant
errors in the solution for an ill-conditioned problem. Although the effect
of round-off errors increases as the size of the equations become larger,
it can still be illustrated by considering only two equations below:
0.12065
12065x
x + 0.98775
98775y
y = 2.01045 (line A)
0.12032
12032x
x + 0.98755
98755y
y = 2.00555 (line B)
where the two equations are very close to each other. The solution will
be denoted by (x1,y1) as:
x1 = 14
14..7403
y1 = 0.23942
M I K E
46
M I K E
x1 = 17
17..97563
y1 = -0.15928
47
Your exercises
(homework)
1 10 E 20 10 E 10
1 1
10 E 20 10 E 20
1 10 E 40 1
10 E 10
1 10 E 40 10 E 50 1
1 10 E 40 10 E 50
1 1
Using Gauss-Jordan Elimination and pivoting technique.
(ref: http://www.icdd.com/resources/axa/VOL42/V42_68.pdf)
M I K E
48
Your exercises 2
(homework)
M I K E
49
Online Cal
Gaussian Eliminator
http://planetcalc.com/3571/
M I K E
50