Documente Academic
Documente Profesional
Documente Cultură
( ( (
( ( (
( ( (
=
( ( (
( ( (
6 2 2 4 12
12 8 6 10 34
3 13 9 3 27
6 4 1 18 38
(
(
(
(
(
(
[AC]. [A] [X]=[C]
Gauss Elimination: Steps
22 August 2014 ME262 Numerical Analysis Sessional
5
1. Forward elimination
This leads to an upper triangular matrix
2. Back-Substitution
Determine x
i
Forward Elimination: Algorithm
22 August 2014 ME262 Numerical Analysis Sessional
6
(
(
(
(
(
(
(
(
n nn n n n
n
n
n
c a a a a
c a a a a
c a a a a
c a a a a
. .
. . . . . . .
. . . . . . .
. .
. .
. .
3 2 1
3 3 33 32 31
2 2 23 22 21
1 1 13 12 11
(
(
(
(
(
(
(
(
n nn n n n
n
n
n
c a a a a
c a a a a
c a a a a
c a a a a
. .
. . . . . . .
. . . . . . .
. .
. .
. .
3 2 1
3 3 33 32 31
2 2 23 22 21
1 1 13 12 11
(
(
(
(
(
(
(
(
+
+
+
+
1 3 2 1
1 3 3 33 32 31
1 2 2 23 22 21
1 1 1 13 12 11
. .
. . . . . . .
. . . . . . .
. .
. .
. .
nn nn n n n
n n
n n
n n
a a a a a
a a a a a
a a a a a
a a a a a
Forward Elimination: Algorithm
22 August 2014 ME262 Numerical Analysis Sessional
8
11 12 13 1 1 1
22 23 2 2 1
31 32 33 3 3 1
1 2 3 1
. .
0 . .
. .
. . . . . . .
. . . . . . .
. .
n n
n n
n n
n n n nn nn
a a a a a
a a a a
a a a a a
a a a a a
+
+
+
+
(
(
' ' ' '
(
(
(
(
(
(
(
(
(
(
(
(
(
(
(
+
+
+
+
1 3 2 1
1 3 3 33 32 31
1 2 2 23 22 21
1 1 1 13 12 11
. .
. . . . . . .
. . . . . . .
. .
. .
. .
nn nn n n n
n n
n n
n n
a a a a a
a a a a a
a a a a a
a a a a a
Multipliers
21
11
2 1
a
Row Row
a
| |
|
\ .
21
21 21 11
21
22 22 12
21
23 23 13
21
2 1 2 1 1
11
11
1
11
1
1
.
.
n n n
a
a a a
a
a a a
a
a a a
a
a
a
a
a
a
a
a
+ + +
' =
' =
' =
' =
Forward Elimination: Algorithm
22 August 2014 ME262 Numerical Analysis Sessional
9
(
(
(
(
(
(
(
(
+
+
+
+
1 3 2 1
1 3 3 33 32 31
1 2 2 23 22 21
1 1 1 13 12 11
. .
. . . . . . .
. . . . . . .
. .
. .
. .
nn nn n n n
n n
n n
n n
a a a a a
a a a a a
a a a a a
a a a a a
21
31
11
1
11
1
1
1
1
.
.
.
1
2
3
n
a
Row Row
a
Row Row
a
R
a
a
n
a
ow Row
| |
|
\ .
| |
|
\ .
| |
|
\ .
Forward Elimination: Algorithm
22 August 2014 ME262 Numerical Analysis Sessional
10
(
(
(
(
(
(
(
(
=
1 12 2 13 3
1
11
y U x U x
x
U
=
11 12 13 1 1
22 23 2 2
33 3 3
0
0 0
U U U x y
U U x y
U x y
( ( (
( ( (
=
( ( (
( ( (
Backward Substitution
22 August 2014 ME262 Numerical Analysis Sessional
15
Perform back substitution to get {X} by simple division
Substitute this into (n-1)
th
equation
Solve for x
n-1
Repeat the process to solve for x
n-2
,
x
n-3
,
.
x
2
, x
1
) ( ) (
,
) (
,
2 n
1 n n
2 n
n 1 n 1 n
2 n
1 n 1 n
b x a x a
= +
) (
) (
1 n
nn
1 n
n
n
a
b
x
=
Backward Substitution
22 August 2014 ME262 Numerical Analysis Sessional
16
(
=
+ =
+ j
n
i j
ij in
ii
i
x a a
a
x
1
1
1
(
(
(
(
(
(
(
(
+
+
+
+
1
1
1
1 4 4 44
1 3 3 33 33
1 2 2 24 23 22
1 1 1 14 13 12 11
. 0 0 0 0
. . . . . . .
. 0 0 0
. 0 0
. 0
.
n
nn
n
nn
n n
n n
n n
n n
a a
a a a
a a a a
a a a a a
a a a a a a
Problem
22 August 2014 ME262 Numerical Analysis Sessional
17
By using gauss elimination method find the augmented upper triangular matrix of
the following equations:
0 2
2 2 3 2 2
7 3 4
6 5 6 6
4 2
4 3 2 1
4 2 1
4 3 2 1
= +
= + + +
= +
= +
x x
x x x x
x x x
x x x x
(
(
(
(
0 1 0 2 0
2 2 3 2 2
7 1 0 3 4
6 5 6 1 6
input
for k=1:n-1
for i=k+1:n
factor=a
ik
/ a
kk
;
for j=k:n+1
a
ij
=a
ij
-factor a
kj
;
end
end
end
Problem
22 August 2014 ME262 Numerical Analysis Sessional
18
(
(
(
(
0 1 0 2 0
2 2 3 2 2
7 1 0 3 4
6 5 6 1 6
(
(
(
(
1200 . 3 5600 . 1 0000 . 0 0000 . 0 0000 . 0
0000 . 9 6364 . 5 8182 . 6 0000 . 0 0000 . 0
0000 . 11 3333 . 4 0000 . 4 6667 . 3 0000 . 0
0000 . 6 0000 . 5 0000 . 6 0000 . 1 0000 . 6
input
output
Gauss Elimination: MATLAB Code
22 August 2014 ME262 Numerical Analysis Sessional
19
%Gauss Elimination
A = [6 1 -6 -5; 4 -3 0 1; 2 2 3 2; 0 2 0 1]; %co-efficient matrix
C = [6; -7; -2; 0]; %constant matrix
[m,n] = size(A);
if m~=n
disp('Matrix Must be Square');
end
Aug = [A C];
disp('Augmented Matrix is:');
disp(Aug);
Gauss Elimination: MATLAB Code
22 August 2014 ME262 Numerical Analysis Sessional
20
%forward elimination
for k = 1:n-1
for i = k+1:n
factor = Aug(i,k)/Aug(k,k);
for j = k:n+1
Aug(i,j) = Aug(i,j)-factor*Aug(k,j);
end
end
end
disp('Upper Triangular Matrix is:');
disp(Aug);
%backward substitution
x = zeros(n,1);
x(n) = Aug(n,n+1)/Aug(n,n);
sum = 0;
for i = n-1:-1:1
for j = i+1:n
sum = sum+Aug(i,j)*x(j);
end
x(i)= (1/Aug(i,i))*(Aug(i,n+1)-sum);
sum = 0;
end
disp('Solution is {X}:');
disp(x);
22 August 2014 ME262 Numerical Analysis Sessional
21
Iterative Methods
If the system is too big, direct solution will have
problems with
the round-off errors
The storage capacity of the computer.
Iterative methods are more appropriate when
the number of equations involved is large (typically of
the order of 100 or more)
and/or
the matrix is sparse (less memory requirements)
22 August 2014 ME262 Numerical Analysis Sessional
22
Jacobi method
Gauss-Seidel Method
Successive Over Relaxation (SOR)
Iterative Methods
22 August 2014 ME262 Numerical Analysis Sessional
23
11 1 12 2 13 3 14 4 1
21 1 22 2 23 3 24 4 2
31 1 32 2 33 3 34 4 3
41 1 42 2 43 3 44 4 4
a x a x a x a x c
a x a x a x a x c
a x a x a x a x c
a x a x a x a x c
+ + + =
+ + + =
+ + + =
+ + + =
1 1 12 2 13 3 14 4 11
2 2 21 1 23 3 24 4 22
3 3 31 1 32 2 34 4 33
4 4 41 1 42 2 43 3 44
( ) /
( ) /
( ) /
( ) /
x c a x a x a x a
x c a x a x a x a
x c a x a x a x a
x c a x a x a x a
=
can be converted to
Iterative Methods
22 August 2014 ME262 Numerical Analysis Sessional
24
1 1 12 2 13 3 14 4 11
2 2 21 1 23 3 24 4 22
3 3 31 1 32 2 34 4 33
4 4 41 1 42 2 43 3 44
( ) /
( ) /
( ) /
( ) /
new
new
ne
old old old
old old old
old o w
new
ld old
old old old
x c a x a x a x a
x c a x a x a x a
x c a x a x a x a
x c a x a x a x a
Jacobi Method
22 August 2014 ME262 Numerical Analysis Sessional
25
1 1 12 2 13 3 14 4 11
2 2 21 1 23 3 24 4 22
3 3 31 1 32 2 34 4 33
4 4 41 1 42 2 43 3 44
( ) /
( ) /
( ) /
( ) /
new
new new
new new new
new new new new
old old old
old old
old
x c a x a x a x a
x c a x a x a x a
x c a x a x a x a
x c a x a x a x a
Differ from Jacobi method by sequential updating:
use new x
i
immediately as they become available
Gauss-Seidel Method
22 August 2014 ME262 Numerical Analysis Sessional
26
Algorithm
A set of n equations and n unknowns:
11 1 12 2 13 3 1 1
21 1 22 2 23 3 2 2
31 1 32 2 33 3 3 3
1 1 2 2 3 3
...
...
...
. . .
. . .
...
n n
n n
n n
n n n nn n n
a x a x a x a x c
a x a x a x a x c
a x a x a x a x c
a x a x a x a x c
+ + + + =
+ + + + =
+ + + + =
+ + + + =
If the diagonal elements are non-zero
Rewrite each equation solving for the corresponding unknown
First equation, solve for x
1
Second equation, solve for x
2
22 August 2014 ME262 Numerical Analysis Sessional
27
Rewriting each equation
11
1 3 13 2 12 1
1
a
x a x a x a c
x
n n
=
n-1 n-1,1 1 n-1,2 2 n-1,n-2 n-2 n-1,n n
n-1
n-1,n-1
n n1 1 n2 2 n,n-1 n-1
n
nn
c - a x - a x - a x - a x
x =
a
c - a x - a x -- a x
x =
a
2 21 1 23 3 2n n
2
22
c - a x - a x - a x
x =
a
Algorithm
22 August 2014 ME262 Numerical Analysis Sessional
28
=
n
1 1j j
j=1
j 1
1
11
c - a x
x =
a
=
n
2 2j j
j=1
j 2
2
22
c - a x
x =
a
=
n
n nj j
j=1
j n
n
nn
c - a x
x =
a
1 12 2 13 3 1n n
1
11
c - a x - a x - a x
x =
a
2 21 1 23 3 2n n
2
22
c - a x - a x - a x
x =
a
n n1 1 n2 2 n,n-1 n-1
n
nn
c - a x - a x -- a x
x =
a
General Form of each equation
Algorithm
22 August 2014 ME262 Numerical Analysis Sessional
29
Algorithm
General form for any row i in a single iteration
11
1
1
1 1
1
a
x a c
x
n
j
j
j j
=
=
=
22
2
1
2 2
2
a
x a c
x
j
n
j
j
j
=
=
=
nn
n
n j
j
j nj n
n
a
x a c
x
=
=
=
1
=
n
i ij j
j=1
j i
i
ii
i = 1, 2,, n.
c - a x
x = ,
a
22 August 2014 ME262 Numerical Analysis Sessional
30
Gauss-Seidel Vs. Jacobi
G
a
u
s
s
-
S
e
i
d
e
l
M
e
t
h
o
d
J
a
c
o
b
i
M
e
t
h
o
d
22 August 2014 ME262 Numerical Analysis Sessional
31
=
(
(
(
(
n
(k-1)
i ij j
j=1
j i
(k)
i
ii
c - a x
x =
a
i = 1 , 2 , 3 n
k = no of iteration
n = no of equation
=
n
i ij j
j=1
j i
i
ii
i = 1, 2,, n.
c - a x
x = ,
a
Jacobi Method
Gauss-Seidel Method
22 August 2014 ME262 Numerical Analysis Sessional
32
(
(
i -1 n
(k) (k-1)
i ij j ij j
j=1 j=i+1
(k)
i
ii
c - a x - a x
x =
a
i = 1 , 2 , 3 n
k = no of iteration
n = no of equation
=
n
i ij j
j=1
j i
i
ii
= 1, 2,, n.
c - a x
x = , i
a
Class Problem
22 August 2014 ME262 Numerical Analysis Sessional
33
By using Gauss Seidel method solve the following equations:
a
=0.0000001
12 5 2
10 3
16 2 4
3 2 1
3 2 1
3 2 1
= + +
= + +
= + +
x x x
x x x
x x x
x
1
=3.0
x
2
=2.0
x
3
=1.0
1
( ) ( 1)
1 1
( )
i n
k k
i ij j ij j
j j i
k
i
ii
c a x a x
x
a
= = +
(
(
=
i = 1 , 2 , 3 n
k = no of iteration
n = no of equation
34
% Gauss Seidel
a=[4,1,2;1,3,1;1,2,5];
c=[16;10;12];
n=3;
for i=1:n
x(i)=0; xold(i)=0;
end
sum=0;
err=0;
for k=1:1000 % k is the number of iterations
for i=1:n
for j=1:n
if j~=i
sum=sum+a(i,j)*x(j);
end
end
x(i)=(c(i)-sum)/a(i,i);
sum=0.0;
err=err+abs((x(i)-xold(i))/x(i));
xold(i)=x(i);
end
if err<=0.0000001 break; end
err=0.0;
end
for i=1:n disp (x(i)); end
disp (k);
Jacobi Iteration
22 August 2014 ME262 Numerical Analysis Sessional
35
Convergence Graph
0
0.2
0.4
0.6
0.8
1
0 2 4 6 8 10
Number of Iterations
F
u
n
c
t
i
o
n
a
l
V
a
l
u
e
X1
X2
X3
Iteration 1 2 3 4 5 6 7
X
1
0.5 0.2 0.45 0.324 0.429 0.376 0.42
X
2
0.6 0.1 0.352 0.142 0.248 0.16 0.204
X
3
1 0.52 0.92 0.718 0.886 0.802 0.872
[ Results are not of the previous example]
Gauss-Seidel Iteration
22 August 2014 ME262 Numerical Analysis Sessional
36
[ Results are not of the previous example]
Iteration 1 2 3 4 5 6 7
X
1
0.5 0.25 0.345 0.384 0.401 0.408 0.411
X
2
0.5 0.31 0.231 0.197 0.183 0.177 0.175
X
3
0.6 0.75 0.815 0.842 0.854 0.858 0.858
Convergence Graph
0
0.2
0.4
0.6
0.8
1
0 2 4 6 8 10
Number of Iteration
F
u
n
c
t
i
o
n
a
l
V
a
l
u
e
s
X1
X2
X3
Matrix Inversion Method
22 August 2014 ME262 Numerical Analysis Sessional
37
Forward slash ( / )
Back-slash ( \ )
Multiplication by the inverse of the quantity under the slash
b * ) ( inv x
b \ A x
b A x
b Ax
1
A =
=
=
=