Lecture Module 3 - System of Linear Equations
Lecture Module 3 - System of Linear Equations
(BWM 30603)
Lecture Module 3: System of Linear Equations
Topics
Elimination Methods
Gauss
Elimination
Gauss Elimination with (partial) pivoting
Iteration Method
Gauss Siedel
Lecture Module 3
BWM30603
Topics
Variant 1
Variant 2
Thomas Algorithm
Cholesky
Lecture Module 3
BWM30603
1 u 1+2 u 2 =1
2 u1 +9 u 2+1 u3=6
1 u 2 +9 u3+4 u 4 =2
4 u3 +3u 4 =8
Or in matrix notation, you can write [K ] {u }= { F }
[ ]{ } { }
1
2
0
0
2
9
1
0
0
1
9
4
0
0
4
3
u1
1
u2 = 6
2
u3
8
u4
BWM30603
[ ]{ } { }
1
2
0
0
2
9
1
0
0
1
9
4
0
0
4
3
u1
1
u2 = 6
2
u3
8
u4
The answer is
Lecture Module 3
YES !
BWM30603
Lecture Module 3
[K ] {u }= { F }
{u }=[ K ]1 { F }
BWM30603
Lecture Module 3
BWM30603
K=
1
2
0
0
Inv(K)=
1.8462
-0.4231
0.1154
-0.1538
U=
-1.6923
1.3462
-2.7308
6.3077
Lecture Module 3
2
9
1
0
0
1
9
4
-0.4231 0.1154
0.2115 -0.0577
-0.0577 0.2885
0.0769 -0.3846
0
0
4
3
F=
1
6
2
8
-0.1538
0.0769
-0.3846
0.8462
BWM30603
K=
1
2
0
0
Inv(K)=
1.8462
-0.4231
0.1154
-0.1538
U=
-1.6923
1.3462
-2.7308
6.3077
Lecture Module 3
2
9
1
0
0
1
9
4
-0.4231 0.1154
0.2115 -0.0577
-0.0577 0.2885
0.0769 -0.3846
0
0
4
3
F=
1
6
2
8
-0.1538
0.0769
-0.3846
0.8462
Example 3-1
Find the unknown variable of the system of linear equations below,
by using any mathematical tool that you have. (Freemat, SMath, LO Calc, MS Excel)
If you use proprietary software, make sure you have a proper legal license !
1 u 1+2 u 2 =1
3 u1 +4 u 2 +5 u3 =0
6 u2 +7 u3 +8 u 4 =2
10 u3 +11 u 4=3
Answer:
Lecture Module 3
{}{ }
u1
1.1173
u 2 = 0.0586
0.6235
u3
0.8395
u4
BWM30603
10
Lecture Module 3
11
Elimination Method
Gauss Elimination
In this method, the basic idea is modify the matrix into upper triangular.
Below is the basic idea in Gauss Elimination:
2 x 1+5 x 2=2
4 x 1 +10 x 2 =5
4 x1 +10 x 2 =4
4 x 1 +10 x 2 =5
20 x 2 =1
( multiply by - (-4/2) )
2 x 1 +5 x 2 =2
20 x 2=1
Then solve
x 2 =0.05
x 1 =1.125
Lecture Module 3
BWM30603
12
Elimination Method
Gauss Elimination
2 x 1+5 x 2=2
4 x 1 +10 x 2 =5
Forward
Eliminating
the second
equation then
replacing with
a new one
4 x1 +10 x 2 =4
4 x 1 +10 x 2 =5
20 x 2 =1
( multiply by - (-4/2) )
2 x 1 +5 x 2 =2
20 x 2=1
Backward
Then solve
x 2 =0.05
x 1 =1.125
Lecture Module 3
BWM30603
13
Elimination Method
Gauss Elimination
See how the matrix has been decomposed
from
5
10
]{ } { }
x1
= 2
5
x2
[ ]{ } { }
2 5 x1
= 2
0 20 x 2
1
to
[ A] { x }={b }
[U ] { x }= { f }
]{ } { } [
a 11 a12 a 1n x 1
b1
a 21 a 22 a 2n x 2
= b2
bn
a m1 a m2 a mn x n
a ii 0
Lecture Module 3
]{ } { }
fn
0
0 u mn x n
14
Elimination Method
Elimination step:
Step k
( 1k n1 )
(k )
assume a kk 0
(k)
define multiplier
mik =
a ik
a
i=k +1,, n
(k)
kk
(k)
(k )
( k +1)
(k)
(k )
a ij
bij
Lecture Module 3
=mik a kj +aij
=mik bk +bi
BWM30603
i , j=k +1,, n
15
Elimination Method
fn
x n=
u nn
Backward step k =n1, n2,, 1
1
xk=
g k u kj x j
ukk
j=k +1
Lecture Module 3
BWM30603
]
16
Example 3-2
a) Write the simultaneous equations below in a complete matrix
form and in augmented matrix form
x1 +2 x 2 =1
3 x 1+4 x 2 +5 x 3=0
6 x 2 +7 x 3 +8 x 4 =2
10 x 3 +11 x 4 =3
b) Find the unknown variables of simultaneous equations above
by using Gauss Elimination.
c) Double check your result by using Freemat.
Lecture Module 3
BWM30603
17
x1 +2 x 2 =1
3 x 1+4 x 2 +5 x 3=0
6 x 2 +7 x 3 +8 x 4 =2
10 x 3 +11 x 4 =3
Lecture Module 3
[
[
]{ } { }
]
1
3
0
0
2 0 0 x1
1
4 5 0 x2 = 0
6 7 8 x3
2
0 10 11 x 4
3
1
3
0
0
2 0 0
4 5 0
6 7 8
0 10 11
BWM30603
1
0
2
3
18
1
3
0
0
2 0 0
4 5 0
6 7 8
0 10 11
]
1
0
2
3
Elimination steps:
E1
E2
E3
E4
m21=
a21 3
= =3
a11 1
Lecture Module 3
]
1(3)
0
2
3
BWM30603
1 2
0 0
0 2 5 0
0 6
7 8
0 0 10 11
]
1
3
2
3
19
1 2
0 0
0 2 5 0
0 6
7 8
0 0 10 11
]
1
3
2
3
Elimination steps:
E1
E2
E3
E4
m32=
a 32 6
=
=3
a 22 2
1
2
0
0
0(3) 2(3) 5(3) 0(3)
0
6
7
8
0
0
10
11
Lecture Module 3
]
1
3(3)
2
3
BWM30603
1 2
0 0
0 2 5 0
0 0 22 8
0 0 10 11
]
1
3
7
3
20
1 2
0 0
0 2 5 0
0 0 22 8
0 0 10 11
]
1
3
7
3
Elimination steps:
E1
E2
E3
E4
a 43
=10/22
a 33
1
2
0
0
0
2
5
0
0(10/22) 0(10/22) 22(10/22) 8(10/ 22)
0
0
10
11
m43 =
Lecture Module 3
BWM30603
1
3
7(10/22)
3
1 2
0
0
0 2 5
0
0 0 22
8
0 0
0 7.364
]
1
3
7
6.182
21
1 2
0
0
0 2 5
0
0 0 22
8
0 0
0 7.364
Lecture Module 3
Substitution steps:
]{ } { }
x1
1
x2
= 3
7
x3
6.182
x4
x 4=
6.182
=0.839
7.364
x3=
1
78 x 4 ] =0.623
[
22
x2=
1
[3(5 x 3 +0 x 4 )]=0.059
2
x1=
1
[1(2 x 2+0 x 3 +0 x 4)]=1.117
1
BWM30603
22
Using Freemat
x1
x2
x3
x4
Lecture Module 3
BWM30603
23
Example 3-3
Write a simple code (function) of Gauss Elimination method
that can be used in Freemat, Matlab or Scilab. Use Example 3-2 to test the code.
function [x] = gaussElim(A,b)
% solving vector x from matrix equation A x = b
% filename gausselim.m
N = max(size(A));
% Perform Gaussian Elimination
for j=2:N;
for i=j:N;
m = A(i,j-1)/A(j-1,j-1);
A(i,:) = A(i,:) - A(j-1,:)*m;
b(i) = b(i) - m*b(j-1);
end;
End;
% Perform back substitution
x = zeros(N,1);
x(N) = b(N)/A(N,N);
for j=N-1:-1:1;
x(j) = (b(j)-A(j,j+1:N)*x(j+1:N))/A(j,j);
end ;
Lecture Module 3
BWM30603
24
Elimination Method
Gauss Elimination with Pivoting
(k )
Another operation
mik =
a ik
(k)
a kk
Lecture Module 3
BWM30603
25
Elimination Method
Gauss Elimination with Pivoting
m-code to implement Gauss Elimination with partial pivoting
Taken from: Applied Numerical Methods for Engineers and Scientists (3ed,2012, p.244) by Stephen C. Chapra
Lecture Module 3
BWM30603
26
Example 3-4
a) Write the simultaneous equations below in a complete matrix
form and in augmented matrix form
Lecture Module 3
BWM30603
27
[
[
0.02
500
4
300
1
7
0.02 300 10
500 1 3
4
7
1
Lecture Module 3
BWM30603
]{ } { }
]
10
3
1
x1
9
x2 = 6
2
x3
9
6
2
28
Elimination steps:
0.02 300 10
500 1 3
4
7
1
]
9
6
2
E1
E2
E3
rearrange
500 1 3
0.02 300 10
4
7
1
]
6
9
2
E1
E2
E3
m21=
a21 0.02
=
=4105
a11 500
Lecture Module 3
6(4 105 )
9
2
BWM30603
500 1 3
0 300 10
4
7
1
]
6
9
2
29
Elimination steps:
500 1 3
0 300 10
4
7
1
]
6
9
2
E1
E2
E3
m31=
a 31
4
=
=0.008
a 11 500
Lecture Module 3
] [
6(0.008)
9
2
BWM30603
500 1
3
0
300
10
0 7.008 0.976
]
6
9
1.952
30
Elimination steps:
500 1
3
0
300
10
0 7.008 0.976
]
6
9
1.952
E1
E2
E3
m32=
a 32 7.008
=
=0.0234
a 22 300
500
1
3
0(0.0234) 300 (0.0234) 10(0.0234)
0
7.008
0.976
6
9(0.0234)
1.952
[
Lecture Module 3
BWM30603
500 1
3
0 300
10
0
0 0.742
]
6
9
1.742
31
Substitution steps:
500 1
3
0 300
10
0
0 0.742
Lecture Module 3
]
6
9
1.742
]{ } { }
x1
500 1
3
6
0 300
10
x2 = 9
0
0 0.742 x 3
1.742
x3=
1.742
=2.348
0.742
x2=
1
910 x 3 ] =0.048
[
300
x1=
1
[ 6(1 x 2+3 x 3 )]=0.002
500
BWM30603
32
LO Calc result :
Lecture Module 3
Results from
gausselim and gausspivot
BWM30603
33
LU Factorization Methods
In Engineering, it is commonly said
In linear system
What is L U decomposition :
l 11 0 0
l
l
0
[ L]= 21 22
l m1 l m2 l mn
In linear system
Lecture Module 3
[ A]
[U ]
] [
0
0 u mn
is rectangular m=n
BWM30603
34
LU Decomposition Methods
Solution Steps
[ A]
1. Decomposition step
[U ]
2. Solving steps
{ x } = {b }
[ L]
[L]
{d } = {b }
{d }
[U ]
{ x } = {d }
{x}
Lecture Module 3
BWM30603
35
LU Decomposition
Doolitlle Method
[ A]
[L]
1 0 0
l
1 0
[ L]= 21
l n1 l n2 1
{ x } = {b }
[U ]
] [
0
0 u nn
Lecture Module 3
BWM30603
36
Example 3-5
a) Write the simultaneous equations below in a complete matrix form
[ A ] { x }={b }
2 x 11 x 22 x 3=1
4 x 1+6 x 2 +3 x 3 =13
4 x 1 2 x 2 +8 x 3 =6
Lecture Module 3
BWM30603
37
Decomposition step :
[ A ] { x }= {b }
2 1 2
4 6
3
4 2 8
]{ } { }
x1
1
x 2 = 13
6
x3
[ L ][ U ]=[ A ]
] [ ][ ] [
2 1 2
1 0 0 d
4 6
3 = a 1 0 0
4 2 8
b c 1 0
e
g
0
f
d
h = ad
i
bd
e
f
ae+g
af +h
be+cg bf +h+i
d =2
e=1
f =2
ad =4
ae+ g=6
af +h=3
a=2
g=4
h=1
bd =4 be+cg =2 bf +ch+i=8
b=2
c=1
i=3
Lecture Module 3
BWM30603
38
Decomposition step :
[
[
d =2 e=1
a =2 g=4
b=2 c=1
][
f =2
h=1
i=3
][
2 1 2
1
0 0 2 1 2
4 6
3 = 2 1 0 0 4 1
4 2 8
2 1 1 0 0
3
[ L]
Lecture Module 3
[U ]
BWM30603
39
[L]
{d } = {b }
]{ } { }
1
0 0 d1
1
2 1 0 d 2 = 13
2 1 1 d 3
6
Lecture Module 3
d 1=
1
=1
1
d 2=
1
13(2 d 1 ) ]=11
[
1
d 3=
1
[6(2 d 11 d 2 ) ]=3
1
BWM30603
40
[U ]
{ x } = {d }
]{ } { }
2 1 2 x 1
1
0 4 1 x 2 = 11
0 0
3 x3
3
3
x 3 = =1
3
Lecture Module 3
x2=
1
11(1 x 3 ) ] =3
[
4
x 1=
1
[1(1 x 22 x 3)]=2
2
BWM30603
41
Lecture Module 3
BWM30603
42
LU Decomposition
Doolitlle Method
The routine can be concluded here
Decomposition step:
Solution step:
for i=1:m
% Finding L
for k=1:i-1
L(i,k)=A(i,k);
for j=1:k-1
L(i,k)= L(i,k)-L(i,j)*U(j,k);
end
L(i,k) = L(i,k)/U(k,k);
end
% Finding U
for k=i:m
U(i,k) = A(i,k);
for j=1:i-1
U(i,k)= U(i,k)-L(i,j)*U(j,k);
end
end
end
% Assigning unity for all diagonal items of L
for i=1:m
L(i,i)=1;
end
Lecture Module 3
BWM30603
Example 3-6
Write matlab m function (doolittle.m) and test it to solve Example 3-5.
function[L,U,d,x]=doolittle(A,B)
% prepared by Waluyo Adi Siswanto
[m n]=size(A);
if (m ~= n )
disp ( 'LR2 error: Matrix must be square' );
return;
end;
L=zeros(m,m);
U=zeros(m,m);
% LU Decomposition (DOOLITTLE METHOD)
for i=1:m
for k=1:i-1
L(i,k)=A(i,k);
for j=1:k-1
L(i,k)= L(i,k)-L(i,j)*U(j,k);
end
L(i,k) = L(i,k)/U(k,k);
end
for k=i:m
U(i,k) = A(i,k);
for j=1:i-1
U(i,k)= U(i,k)-L(i,j)*U(j,k);
end
end
end
for i=1:m
L(i,i)=1;
End
% End of decomposition (DOOLITTLE METHOD)
Lecture Module 3
BWM30603
44
Lecture Module 3
BWM30603
45
Exercise 3-7
a) Decompose matrix [A] of the simultaneous equations below
according to Doolittle method
[ ]{ } { }
1
5
9
3
2
2
0
4
3
7
3
5
4
8
2
6
x1
3
x2
= 0
7
x3
8
x4
Lecture Module 3
BWM30603
46
Lecture Module 3
BWM30603
47
LU Decomposition
Crout Method
[ A]
[L]
l 11 0 0
l
l
0
[ L]= 21 22
l n1 l n2 l nn
{ x } = {b }
[U ]
] [
1 u12 u1n
0 1 u2n
[U ]=
0 0 1
Lecture Module 3
BWM30603
48
LU Decomposition
Crout Method
The algorithm for decomposition step:
i j , i=1, 2,, n
j1
Lij = Aij L ik U kj
k 1
1
U ij = Aij Lik U kj
Lii
k 1
j=1
Lij = Ai1
i=1
A ij
U ij =
A11
Lecture Module 3
BWM30603
49
Exercise 3-8
Modify your doolittle.m (example 3-6) to implement CROUT method and save as crout.m.
Try to solve example 3.7 by using crout.m. Try to solve by hand calculation according to Crout.
Simply replace the DOOLITTLE part
of Example 3-6 with the CROUT method
written in page 49.
Lecture Module 3
BWM30603
50
[ ]{ } { }
[ ] [ ][ ]
[
x1
3
x2
= 0
7
x3
8
x4
1
5
9
3
2
2
0
4
3
7
3
5
4
8
2
6
1
5
9
3
2
2
0
4
3
7
3
5
4
a
8
b
=
2
d
6
g
0
c
e
h
a=1
b=5
=
d =9
g=3
Lecture Module 3
Decomposition step :
0
0
f
i
0
0
0
j
1
0
0
0
k l m
1 n o
0 1 p
0 0 1
ak =2
al =3
bk+c=2
bl+cn=7
dk+e=0 dl +en+ f =3
gk+h=4 gl+hn+i=5
BWM30603
am=4
bm+co=8
dm+eo+ fp=2
gm+ho+ip+ j=6
]
51
Decomposition step :
a=1
ak =2
al=3
k =2
l=3
b=5 bk +c=2
bl+cn=7
c=8
n=1
d =9 dk +e=0 dl+en+ f =3
e=18
f =6
g=3 gk +h=4 gl +hn+i=5
h=2
i=2
[ ][
1
5
9
3
2
2
0
4
3
7
3
5
4
a=1
0
8
b=5 c=8
=
2
b=9 e=18
6
b=3 h=2
0
0
f =6
i=2
0
0
0
j=2/3
[ L]
Lecture Module 3
am=4
m=4
bm+co=8
o=1.5
dm+eo+ fp=2
p=7/6
gm+ho+ip+ j=6
j=2/3
][
1 k =2 l=3
0
1
n=1
0
0
1
0
0
0
m=4
o=1.5
p=7/6
1
[U ]
BWM30603
52
[L]
{d } = {b }
]{ } { }
d1
1
0
0
0
3
d2
5 8
0
0
= 0
9 18 6
0
7
d3
3 2 2 2/3 d 4
8
3
d 1 = =3
1
Lecture Module 3
d 2=
1
[ 0(5 d 1 )]=1.875
8
d 3=
1
7(9 d 118 d 2 ) ]=2.292
[
6
d 4=
1
[ 8(3 d 12 d 22 d 3)]=2.751
2 /3
BWM30603
53
[U ]
1
0
0
0
2
1
0
0
3 4
1 1.5
1 7 /6
0 1
Lecture Module 3
{ x } = {d }
]{ } { }
x1
3
x2
= 1.875
2.292
x3
2.751
x4
x 4=
2.751
=2.751
1
x3=
1
[2.292(7/6 x 4 )]=5.502
1
x2=
1
1.875(1 x 3 +1.5 x 4 ) ]=3.251
[
1
x 1=
1
[ 3(2 x 2+3 x 3+4 x 4) ]=2
1
BWM30603
54
LU Decomposition
Thomas Method Variant 1
Engineering problems often lead to coefficient matrices that are sparsely populated,
meaning that most elements of the matrix are zero. If all the nonzero terms are clustered
about the leading diagonal, then the matrix is said to be banded.
[ A] =
[ L]
][
[U ]
][
b1 c1 0
0
1 0 0
0
1 1 0
a 2 b2 c 2
0
a 2 2 0
0
0 1 2
0 a3
0 = 0 a3
0 + 0 0
bn1 c n1
n1 0
0 0
0 0 an
bn
0 0 a n n
0
0
0
1
0
n1
1
BWM30603
55
Exercise 3-9a
a) Decompose the banded matrix [A] of the simultaneous equations below
according to Thomas method (variant 1)
1
3
0
0
]{ } { }
2 0 0 x1
1
4 5 0 x2 = 0
6 7 8 x3
2
0 10 11 x 4
3
Lecture Module 3
BWM30603
56
[
[
]{ } { }
] [ ][
1
3
0
0
2 0 0 x1
1
4 5 0 x2 = 0
6 7 8 x3
2
0 10 11 x 4
3
1
3
0
0
1 0 0 0
2 0 0
4 5 0 = 3 2 0 0
6 7 8
0 6 3 0
0 10 11
0 0 10 4
Decomposition step :
1 1 0 0
0 1 2 0
0 0 1 3
0 0 0 1
1=1
1 1=2
3
31 + 2=4
2 2=5
=
6
62 +3 =7
3 3 =8
10
3+ 4=11
Lecture Module 3
BWM30603
]
57
Decomposition step :
1=1
1 1 =2
1=1
1 =2
3
31 +2 =4
2 2=5
3
2 =2 2 =2.5
6
62 + 3=7
3 3
6
3=22 3 =0.3636
10
3+ 4=11
10
4 =7.3636
1
3
0
0
][
2 0 0
1 0
0
0
4 5 0
3 2 0
0
=
6 7 8
0 6 22
0
0 10 11
0 0 10 7.3636
][
[ L]
Lecture Module 3
1
0
0
0
2
0
0
1 2.5
0
0
1
0.3636
0
0
1
[U ]
BWM30603
58
[L]
1 0
0
0
3 2 0
0
0 6 22
0
0 0 10 7.3636
{d } = {b }
]{ } {}
d1
1
d2
= 0
2
d3
3
d4
1
d 1 = =1
1
Lecture Module 3
d 2=
1
[ 0(3 d 1 )]=1.5
2
d 3=
1
2(6 d 2 ) ] =0.3182
[
22
d 4=
1
[ 3(10 d 3)]=0.8395
10
BWM30603
59
[U ]
1
0
0
0
2
0
0
1 2.5
0
0
1
0.3636
0
0
1
Lecture Module 3
{ x } = {d }
]{ } { }
x1
1
x2
1.5
=
0.3182
x3
0.8395
x4
x 4=
0.8395
=0.8395
1
x3=
1
[0.3182(0.3636 x 4)]=0.6234
1
x 2=
1
1.5(2.5 x 3 ) ]=0.0585
[
1
x 1=
1
[1(2 x 2)]=1.117
1
BWM30603
60
Crout method on banded matrix gives the same result of L and U as defined by Thomas .
Lecture Module 3
BWM30603
61
LU Decomposition
Thomas Method Variant 2
Another variation in Thomas method
[ A] =
[ L]
][
d 1 c1 0
0
1 0 0
a 2 d 2 c2
0
2 1 0
0 a3
0 = 0 3
d n1 c n1
1
0 0 n
0 0 an
dn
[U ]
][
1 c 1 0
0
0
0
0 2 c2
0
0 + 0 0
0
n1 c n1
0
1
0 0 0
n
BWM30603
62
1 =b1
i=2,3,n
a1
i =
i1
i=ai i c i1
Forward substitution:
d 1 =b1
d i =bi i d i1
Backward substitution:
x n =d n / n
x i =d ici x i+1
Lecture Module 3
BWM30603
63
Exercise 3-9b
Do example 3-9a by using Thomas method variant 2. Try the thomas.m also by hand!
Lecture Module 3
BWM30603
64
[
[
Decomposition step :
]{ } { }
] [ ][
1
3
0
0
2 0 0 x1
1
4 5 0 x2 = 0
6 7 8 x3
2
0 10 11 x 4
3
1
3
0
0
1 0 0
2 0 0
4 5 0 = 1 1 0
6 7 8
0 2 1
0 10 11
0 0 3
0 1 2 0 0
0 0 2 5 0
0 0 0 3 8
1 0 0 0 4
1 =1
2
=3 21 +2 =4
5
= 1 1
2 2 =6
52 + 3=7
8
3 3 =10 83 + 4 =11
Lecture Module 3
BWM30603
]
65
Decomposition step :
1 =1
2
1 =1
1 1 =3 21+ 2 =4
1 =3 2=2
2 2=6
2 =3
1
3
0
0
5
52 +3 =7
8
3 =22
3 3 =10
83+ 4 =11
3 =0.4545 4 =7.3636
][
2 0 0
1 0
0
4 5 0
3 1
0
=
6 7 8
0 3
1
0 10 11
0 0 0.4545
0
0
0
1
][
1 2
0
0
0 2 5
0
0 0 22
8
0 0
0 7.3636
[ L]
Lecture Module 3
[U ]
BWM30603
66
[L]
1 0
0
3 1
0
0 3
1
0 0 0.4545
0
0
0
1
{d } = {b }
]{ } {}
d1
1
d2
= 0
2
d3
3
d4
1
d 1 = =1
1
Lecture Module 3
d 2=
1
[ 0(3 d 1 )]=3
1
d 3=
1
2(3 d 2 ) ] =7
[
1
d 4=
1
[ 3(0.4545 d 3)]=6.1818
1
BWM30603
67
[U ]
1 2
0
0
0 2 5
0
0 0 22
8
0 0
0 7.3636
Lecture Module 3
{ x } = {d }
]{ } { }
x1
1
x2
= 3
7
x3
6.1818
x4
x 4=
6.1818
=0.8395
7.3636
x3=
1
[7(8 x 4 )]=0.6234
22
x 2=
1
3(5 x 3 ) ]=0.0585
[
2
x 1=
1
[1(2 x 2)]=1.117
1
BWM30603
68
Thomas Algorithm
The routine for Thomas (Variant 1) has been generalized for a faster calculation by using
a calculation table.
[ A] =
[ L]
][
[U ]
][
d 1 e1 0
0
1 0 0
0
1 1 0 0
c2 d 2 e2
0
c2 2 0
0
0 1 2 0
0 c3
0 = 0 c3
0 + 0 0
0
1 n1
d n1 e n1
n1 0
0 0 0
1
0 0 cn
dn
0 0 cn n
Lecture Module 3
BWM30603
]
69
...
di
ei
ci
bi
1=d 1
i =d ici i1
ei
i=
i
b1
y 1=
1
bici yi1
yi=
i
x n= y n
x i = y i i x i+1
Lecture Module 3
BWM30603
70
Exercise 3-9c
1
3
0
0
]{ } { }
2 0 0 x1
1
4 5 0 x2 = 0
6 7 8 x3
2
0 10 11 x 4
3
Lecture Module 3
BWM30603
71
1
3
0
0
]{ } { }
2 0 0 x1
1
4 5 0 x2 = 0
6 7 8 x3
2
0 10 11 x 4
3
di
ei
1=d 1
i =d ici i1
ei
i=
i
10
4-3(2)
=-2
7-6(-2.5)
=22
11-10(0.364)
=7.36
2/1
=2
5/-2
=-2.5
8/22
=0.364
ci
bi
11
b1
y1=
1
bici yi1
yi=
i
1/1
=1
0-3(1)/-2
=1.5
2-6(1.5)/22
=-0.318
3-10(-0.318)/7.36
=0.840
x n= y n
x i = y i i x i +1
1-2(0.06)
=1.12
1.5-(-2.5)(0.624)
=-0.06
-0.3180.364(0.840
=-0.624
0.840
Lecture Module 3
BWM30603
72
LU Decomposition
Cholesky Method
[ A]
[L]
l 11 0 0
l
l
0
[ L]= 21 22
l n1 l n2 l nn
Lecture Module 3
{ x } = {b }
[ U ]=[ L ]
] [
l 11 l 21 l nl
0 l 22 l n2
[U ]=
0 0 l nn
BWM30603
]
73
j1
L jj = A jj L 2jk
k =1
must be
positive
j=2, 3,, n
For non diagonal term
L jj =
j 1
A jj Lik L jk
k =1
L jj
j=2, 3,, n1
i= j+1, j+2,, n
Lecture Module 3
BWM30603
74
Exercise 3-10
Modify your doolittle.m (example 3-6) to implement CHOLESKY method
and save as cholesky.m.
Lecture Module 3
BWM30603
75
Exercise 3-11
a) Decompose matrix [A] of the simultaneous equations below
according to Cholesky method
8
2
3
4
2
5
1
0
]{ } { }
3 4 x1
1
1 0 x2 = 0
7 3 x3
0
3 11 x 4
9
Lecture Module 3
BWM30603
76
Decomposition step :
[ ]{ } { }
[ ] [ ][ ]
8
2
3
4
2
5
1
0
3 4 x1
1
1 0 x2 = 0
7 3 x3
0
3 11 x 4
9
8
2
3
4
2
5
1
0
3 4
a
1 0
b
=
7 3
d
3 11
g
0
c
e
h
0
0
f
i
0
0
0
j
a b
0 c
0 0
0 0
d
e
f
0
g
h
i
j
a 2 =8
ab=2 b2 +c 2 =5
=
2
2
2
ad =3 bd +ce=1 d +e + f =7
ag=4 bg +ch=0 dg+eh+if =3
Lecture Module 3
BWM30603
g +h +i + j =11
]
77
Decomposition step :
a 2 =8
a=2.8284
2
2
ab=2
b +c =5
b=0.7071 c=2.1213
2
2
2
ad =3
bd +ce=1
d +e + f =7
d =1.0607 e=0.1179
f =2.4210
ag=4
bg+ch=0
dg+eh+if =3 g 2 +h 2+i 2 + j 2=11
g=1.4142 h=0.4714 i=0.6425
j=2.8922
8
2
3
4
2
5
1
0
][
][
3 4
2.8284
0
0
0
2.8284 0.7071 1.0607 1.4142
1 0
0.7071 2.1213
0
0
0
2.1213 0.1179 0.4714
=
7 3
1.0607 0.1179 2.4210
0
0
0
2.4210 0.6425
3 11
1.4142 0.4714 0.6425 2.8922
0
0
0
2.8922
[U ]
[ L]
Lecture Module 3
BWM30603
78
[L]
{d } = {b }
]{ } { }
d1
2.8284
0
0
0
1
d2
0.7071 2.1213
0
0
= 0
1.0607 0.1179 2.4210
0
0
d3
1.4142 0.4714 0.6425 2.8922 d 4
9
Lecture Module 3
d 1=
1
=0.3536
2.8284
d 2=
1
[ 0(0.7071 d 1)]=0.1179
2.1213
d 3=
1
0(1.0607 d 1+0.1179 d 2 ) ]=0.1492
[
2.4210
d 4=
1
[ 9(1.4142 d 10.4714 d 2+0.6425 d 3 )]=2.9528
2.8922
BWM30603
79
[U ]
{ x } = {d }
Lecture Module 3
]{ } { }
x1
0.3536
x2
= 0.1179
0.1492
x3
2.9528
x4
x 4=
2.9528
=1.0210
2.8922
x3=
1
[0.1492(0.6425 x 4 )]=0.3326
2.4210
x2=
1
0.1179(0.1179 x 30.4714 x 4 ) ] =0.1898
[
2.1213
x1=
1
[ 0.3536(0.7071 x 2+1.0607 x 3+1.4142 x 4)]=0.3082
2.8284
BWM30603
80
Lecture Module 3
BWM30603
81
Iteration Method
Gauss-Seidel
In simultaneous equations
a n1 x 1 +a n2 x 2++a nn x n=bn
and using initial guess at k=1, the iteration can be started
(k +1)
1
( k +1)
(k +1)
b2 a 21 x 1
(k +1)
bn a 2n1 x 1
x2
(k )
(k +1)
xn
Lecture Module 3
(k)
+a 22 x 2 a 2n x n
a 22
( k+1)
+an2 x 2
a nn
BWM30603
(k )
a nn x n
82
Iteration Method
Gauss-Seidel: convergence criterion
Requirement for stable converge criterion: [A] must be diagonally dominant
n
aii= aij
j=1
j i
Rearrange
[
[
1
1
6
7
0
0
4
9
1
6
1
1
0
7
0
1
4
9
]{ } { }
]{ } { }
x1
2
x2 = 3
5
x3
x1
5
x2 = 2
3
x3
6> 0+ 1
7> 1+ 4
9> 1+ 0
diagonally dominant
Lecture Module 3
BWM30603
83
Iteration Method
Gauss-Seidel
In conclusion, for a general n equations
the Gauss-Seidel iterative method is defined as:
k =0,1,, n
i=1,2,, n
x
Lecture Module 3
i1
n
1
(k +1)
=
bi aij x j aij x(kj )
j=1
j=i +1
a ii
(k +1)
i
BWM30603
84
Iteration Method
Gauss-Seidel
function x = GaussSeidel(A,b,es,maxit)
% Gauss-Siedel method
if nargin<4, maxit=50; end
if nargin<3, es=0.00001; end
[m,n] = size(A);
if m~=n, error('Matrix A must be square'); end
C = A;
for i = 1:n
C(i,i) = 0;
x(i) = 0;
end
x = x';
for i = 1:n
for j = 1:n;
H = abs(A(i,j));
Check(i) = abs(A(i,i)) - sum(H);
if Check(i) < 0
fprintf('The matrix is not strictly diagonally dominant
at row %2i\n\n',i))
end
end
end
Lecture Module 3
BWM30603
for i = 1:n
C(i,1:n) = C(i,1:n)/A(i,i);
end
for i = 1:n
d(i) = b(i)/A(i,i);
end
iter = 0;
while (1)
xold = x;
for i = 1:n
x(i) = d(i)-C(i,:)*x;
if x(i) ~= 0
ea(i) = abs((x(i) - xold(i))/x(i)) * 100;
end
end
iter = iter+1;
if max(ea)<=es | iter >= maxit, break, end
end
85
Example 3-12
]{ } { }
0 2 7 3 x 1
3
4
0
1 9 x 2 = 3
3
1
1
0 x3
2
1 5 2
1 x4
5
Lecture Module 3
BWM30603
86
]{ } { }
0 2 7 3 x 1
3
4
0
1 9 x 2 = 3
3
1
1
0 x3
2
1 5 2
1 x4
5
0 >2+ 7+ 3
0 >4+ 1+ 9
1> 3+ 1+ 0
1> 1+ 5+ 2
X
X
X
X
3
1
1
0
1 5 2
1
0 2 7 3
4
0
1 9
]{ } { }
x1
2
x2
= 5
3
x3
3
x4
3> 1+ 1+ 0
5> 1+ 2+1
7> 0+ 2+ 3
9> 4+0+ 1
OK
OK
OK
OK
Lecture Module 3
BWM30603
87
]{ } { }
3
1
1
0 x1
2
1 5 2
1 x2 = 5
0 2 7 3 x 3
3
4
0
1 9 x 4
3
x(k)
1
Initial values
(k )
(k )
(k )
(k)
(k )
(k )
x2
x3
(k )
x4
(k )
21 x 2 1 x 3 0 1 x 4
2 x 2 x 3
(k +1)
x1 =
=
3
3
(k +1)
(k +1)
x2
5(1) x 1
( k +1)
( k +1)
3
( k +1)
4
3(0) x 1
Lecture Module 3
(k )
(k +1)
(k)
(k +1)
(2) x 2
7
( k +1)
3(4) x1
(k )
2 x 3 1 x 4
5+ x 1
=
5
(3) x 4
(k +1)
(0) x 2
9
BWM30603
( k +1)
(1) x 3
(k )
(k )
2 x 3 x 4
5
(k)
(k +1)
3+2 x 2 +3 x 4
=
7
( k +1)
( k +1)
34 x 1 x 3
=
9
88
Lecture Module 3
BWM30603
89
Lecture Module 3
BWM30603
90
Lecture Module 3
BWM30603
91