5-MATH 307 Numerical Methods-System of Linear A E-File 5
5-MATH 307 Numerical Methods-System of Linear A E-File 5
1
Naïve Gauss Elimination
2
Naïve Gaussian Elimination
Two steps
1. Forward Elimination
2. Back Substitution
3
Forward Elimination
The goal of forward elimination is to transform the
coefficient matrix into an upper triangular matrix
25 5 1 x1 106.8
64 8 1 x 177.2
2
144 12 1 x3 279.2
25 5 1 x1 106.8
0 4.8 1.56 x 96.21
2
0 0 0.7 x3 0.735
4
Forward Elimination
A set of n equations and n unknowns
a11x1 a12x2 a13x3 ... a1n xn b1
a21x1 a22x2 a23x3 ... a2n xn b2
. .
. .
. .
5
Forward Elimination
Step 1 :
For Equation 2, divide Equation 1 by a11 and
multiply by a21.
a21
a (a11x1 a12 x2 a13x3 ... a1n xn b1 )
11
or a x ... a x b
'
22 2
'
2n n
'
2
7
Forward Elimination
Repeat this procedure for the remaining equations
to reduce the set of equations as
a x a x ... a x b
'.
.
32
. 2
.
.
.
' .
33 3..
'
3n n
'
3
a x a x ... a x b
'
n2 2
'
n3 3
'
nn n
'
n
End of Step 1 8
Forward Elimination
Step 2
Repeat the same procedure for the 3rd term of
Equation 3.
a x a x ... a x b
'
22 2
'
23 3
'
2n n
'
2
a x ... a x b
"
33 3 . .
"
3n n
"
3
. .
. .
a x ... a x b
"
n3 3
"
nn n
"
n
End of Step 2
9
Forward Elimination
At the end of (n-1) Forward Elimination steps, the
system of equations will look like
a11x1 a12 x2 a13 x3 ... a1n xn b1
'
a22 x2 a23
'
x3 ... a2' n xn b2'
a x ... a x b
"
33 3
"
3n n
"
3
. .
. .
. .
n 1 n 1
ann xn bn
11
Back Substitution
Solve each equation starting from the last equation
25 5 1 x1 106.8
0 4.8 1.56 x 96.21
2
0 0 0.7 x3 0.735
12
Back Substitution Starting Eqns
a11x1 a12 x2 a13 x3 ... a1n xn b1
'
a22 x2 a23
'
x3 ... a2' n xn b2'
"
a33 x3 ... an" xn b3"
. .
. .
. .
n 1 n 1
ann xn bn
13
Back Substitution
Start with the last equation because it has only one unknown
( n 1)
b
xn n
( n 1)
a nn
14
Back Substitution
( n 1)
b
xn n
( n 1)
a nn
i 1
aiji 1 x j
n
bi
j i 1
xi i 1 for i n 1,...,1
a ii
15
Naïve Gauss Elimination
Example
16
Example 1
The upward velocity of a rocket is given at three different times
t12 t1 1 a 1 v1
2 a v
t 2 t2 1 2 2
t 32 t3 1
a 3
v3
Using data from Table 1, the matrix becomes:
25 5 1 a1 106.8
64 8 1 a 177.2
2
144 12 1 a3 279.2
18
Example 1 Cont.
25 5 1 a1 106.8 25 5 1 106.8
64 8 1 a 2 177.2 64 8 1 177.2
144 12 1 a3 279.2 144 12 1 279.2
1. Forward Elimination
2. Back Substitution
19
Forward Elimination
20
Number of Steps of Forward
Elimination
21
Forward Elimination: Step 1
25 5 1 106.8 Divide Equation 1 by 25 and
64 8 1 177.2
64
multiply it by 64, 2.56 .
144 12 1 279.2 25
25 5 1 106.8 2.56 64 12.8 2.56 273.408
. 64 8 1 177.2
Subtract the result from 64 12.8 2.56 273.408
Equation 2
0 4.8 1.56 96.208
25 5 1 106.8
Substitute new equation for 0 4.8 1.56 96.208
Equation 2
144 12 1 279.2
22
Forward Elimination: Step 1 (cont.)
25 5 1 106.8
Divide Equation 1 by 25 and
0 4.8 1.56 96.208
144
144 12 1 279.2 multiply it by 144, 5.76 .
25
25 5 1 106.8
Substitute new equation for 0 4.8 1.56 96.208
Equation 3
0 0 0.7 0.76
24
Back Substitution
25
Back Substitution
25 5 1 106.8 25 5 1 a1 106.8
0 4.8 1.56 96.2 0 4.8 1.56 a 96.208
2
0 0 0.7 0.7 0 0 0.7 a3 0.76
Solving for a3
0.7 a3 0.76
0.76
a3
0.7
a3 1.08571
26
Back Substitution (cont.)
25 5 1 a1 106.8
0 4.8 1.56 a 96.208
2
0 0 0.7 a3 0.76
Solving for a2
4.8a2 1.56a3 96.208
96.208 1.56a3
a2
4.8
96.208 1.56 1.08571
a2
4.8
a2 19.6905
27
Back Substitution (cont.)
25 5 1 a1 106.8
0 4.8 1.56 a 96.2
2
0 0 0.7 a3 0.76
Solving for a1
25a1 5a2 a3 106.8
106.8 5a2 a3
a1
25
106.8 5 19.6905 1.08571
25
0.290472
28
Naïve Gaussian Elimination Solution
25 5 1 a1 106.8
64 8 1 a2 177.2
144 12 1 a3 279.2
a1 0.290472
a 19.6905
2
a3 1.08571
29
Example 1 Cont.
Solution a1 0.290472
a 19.6905
The solution vector is
2
a3 1.08571
The polynomial that passes through the three data points is then:
129.686 m/s.
30
Naïve Gauss Elimination
Pitfalls
31
Pitfall#1. Division by zero
10 x2 7 x3 3
6 x1 2 x2 3x3 11
5 x1 x2 5 x3 9
0 10 7 x1 3
6 2 3 x2 11
5 1 5 x3 9
32
Is division by zero an issue here?
12 x1 10 x2 7 x3 15
6 x1 5 x2 3x3 14
5 x1 x2 5 x3 9
12 10 7 x1 15
6 5 3 x2 14
5 1 5 x3 9
33
Is division by zero an issue here? YES
12 x1 10 x2 7 x3 15
6 x1 5 x2 3x3 14
24 x1 x2 5 x3 28
20 15 10 x1 45
3 2.249 7 x 1.751
2
5 1 3 x3 9
Exact Solution
x1 1
x 1
2
x3 1 35
Pitfall#2. Large Round-off Errors
20 15 10 x1 45
3 2.249 7 x 1.751
2
5 1 3 x3 9
38
Avoiding Pitfalls
39
Jacobi Method
40
Jacobi Method
An iterative method.
Basic Procedure:
-Algebraically solve each linear equation for xi
-Assume an initial guess solution array
-Solve for each xi and repeat
-Use absolute relative approximate error after each iteration to
check if error is within a pre-specified tolerance.
41
Jacobi Method
Why?
The Jacobi Method allows the user to control round-off error.
42
Jacobi Method
Algorithm
A set of n equations and n unknowns:
If: the diagonal elements are
a11x1 a12x2 a13x3 ... a1n xn b1 non-zero
a21x1 a22 x2 a23x3 ... a2n xn b2 Rewrite each equation solving
. .
. . for the corresponding unknown
. .
ex:
an1x1 an 2 x2 an3 x3 ... ann xn bn
First equation, solve for x1
Second equation, solve for x2
43
Jacobi Method
Algorithm
Rewriting each equation
c1 a12 x2 a13 x3 a1n xn
x1 From Equation 1
a11
c2 a21x1 a23 x3 a2 n xn
x2 From equation 2
a22
cn 1 an 1,1 x1 an 1, 2 x2 an 1,n 2 xn 2 an 1,n xn From equation n-1
xn 1
an 1,n 1
cn an1 x1 an 2 x2 an ,n 1 xn 1
xn From equation n
ann
44
Jacobi Method
Algorithm
General Form of each equation
n
a
n
c1 a1 j x j cn 1 n 1, j xj
j 1 j 1
j n 1
x1
j 1 xn 1
a11 an 1,n 1
n
c n a nj x j
n
c2 a2 j x j
j 1 j 1
j n
x2
j2
xn
a 22 a nn
45
Jacobi Method
Algorithm
General Form for any row ‘i’
n
ci aij x j
j 1
j i
xi , i 1,2,, n.
aii
46
Jacobi Method
x1
x Use rewritten equations to solve for
2 each value of xi.
xn -1
xn
47
48
49
50
51
Jacobi Method examples
• Consider the two-by-two system x
1
y3
2x y 6 2
x 2y 6 1
• Start with x (1)
y (1)
1/ 2 y x3
2
• Simultaneous updating
– New values of the variables are
not used until a new iteration step
is begun
y ( 2)
1 (1) 1 11
x ( 2) y 3 3
2 4 4
y (1)
1 (1) 1 11
y ( 2) x 3 3
2 4 4 x (1) x ( 2)
Jacobi Method
• Con’t
1 ( 2) 11 13
x (3)
y 3 3
2 8 8
1 ( 2) 11 13
y (3) x 3 3
2 8 8
Jacobi Method
• Consider the three-by-three system
2 x1 x2 x3 1
x1 2 x2 x3 6
x1 x2 2 x3 3
x1 0.5 x2 0.5 x3 0.5
x2 0.5 x1 0.5 x3 3.0
x3 0.5 x1 0.5 x2 1.5
58
Gauss-Seidel Method
An iterative method.
Basic Procedure:
-Algebraically solve each linear equation for xi
-Assume an initial guess solution array
-Solve for each xi and repeat
-Use absolute relative approximate error after each iteration to
check if error is within a pre-specified tolerance.
59
Gauss-Seidel Method
Why?
The Gauss-Seidel Method allows the user to control round-off error.
60
Gauss-Seidel Method
Algorithm
A set of n equations and n unknowns:
If: the diagonal elements are
a11x1 a12x2 a13x3 ... a1n xn b1 non-zero
a21x1 a22 x2 a23x3 ... a2n xn b2 Rewrite each equation solving
. .
. . for the corresponding unknown
. .
ex:
an1x1 an 2 x2 an3 x3 ... ann xn bn
First equation, solve for x1
Second equation, solve for x2
61
Gauss-Seidel Method
Algorithm
Rewriting each equation
c1 a12 x2 a13 x3 a1n xn
x1 From Equation 1
a11
c2 a21x1 a23 x3 a2 n xn
x2 From equation 2
a22
cn 1 an 1,1 x1 an 1, 2 x2 an 1,n 2 xn 2 an 1,n xn From equation n-1
xn 1
an 1,n 1
cn an1 x1 an 2 x2 an ,n 1 xn 1
xn From equation n
ann
62
Gauss-Seidel Method
Algorithm
General Form of each equation
n
a
n
c1 a1 j x j cn 1 n 1, j xj
j 1 j 1
j n 1
x1
j 1 xn 1
a11 an 1,n 1
n
c n a nj x j
n
c2 a2 j x j
j 1 j 1
j n
x2
j2
xn
a 22 a nn
63
Gauss-Seidel Method
Algorithm
General Form for any row ‘i’
n
ci aij x j
j 1
j i
xi , i 1,2,, n.
aii
64
Gauss-Seidel Method
65
Gauss-Seidel Method
66
Gauss-Seidel Method: Example 1
The upward velocity of a rocket is given at three different times
5 106.8
8 177.2
12 279.2
a1 1
a 2
Initial Guess: Assume an initial guess of
2
a3 5
68
Gauss-Seidel Method: Example 1
69
Gauss-Seidel Method: Example 1
When solving for a2, how many of the initial guess values were used?
70
Gauss-Seidel Method: Example 1
155.36 5.0000
a 3 x100 103.22%
155.36
71
Gauss-Seidel Method: Example 1
Iteration #2
Using
a1 3.6720 the values of ai are found:
a 7.8510 106.8 5 7.8510 155.36
2 a1 12.056
a3 155.36 25
from iteration #1
177.2 6412.056 155.36
a2 54.882
8
72
Gauss-Seidel Method: Example 1
73
Gauss-Seidel Method: Example 1
Repeating more iterations, the following values are obtained
Iteration a1 a 1 % a2 a 2 % a3 a 3 %
1 3.6720 72.767 −7.8510 125.47 −155.36 103.22
2 12.056 69.543 −54.882 85.695 −798.34 80.540
3 47.182 74.447 −255.51 78.521 −3448.9 76.852
4 193.33 75.595 −1093.4 76.632 −14440 76.116
5 800.53 75.850 −4577.2 76.112 −60072 75.963
6 3322.6 75.906 −19049 75.972 −249580 75.931
Notice – The relative errors are not decreasing at any significant rate
Is there a fix?
One class of system of equations always converges: One with a diagonally
dominant coefficient matrix.
76
Gauss-Seidel Method: Example 2
77
Gauss-Seidel Method: Example 2
The inequalities are all true and at least one row is strictly greater than:
Therefore: The solution should converge using the Gauss-Siedel Method
78
Gauss-Seidel Method: Example 2
1 3 x 2 5 x3 1 30 51
x1 x1 0.50000
12 12
4.9000 0
a 2
100 100.00%
4.9000
3.0923 1.0000
a 3
100 67.662%
3.0923
The maximum absolute relative error after the first iteration is 100%
80
Gauss-Seidel Method: Example 2
After Iteration #1
x1 0.5000
x 4.9000
2
x3 3.0923
Substituting x the
1 x values
0.14679 into the After Iteration #2
x 3.7153
equations x 3.8118
2
3
1 34.9000 53.0923
x1 0.14679
12
28 0.14679 33.0923
x2 3.7153
5
76 30.14679 74.900
x3 3.8118
13
81
Gauss-Seidel Method: Example 2
This is much larger than the maximum absolute relative error obtained in
iteration #1. Is this a problem?
82
Gauss-Seidel Method: Example 2
Repeating more iterations, the following values are obtained
Iteration a1 a 1 % a2 a 2 % a3 a 3 %
x1 0.99919 x1 1
The solution obtained x 3.0001 is close to the exact solution of x 3 .
2 2
x3 4.0001 x3 4
83
Example : Jacobi Method
3x1 – x2 = 2 ; X1 + 4x2 = 5
Step : 1
Step : 2
Step : 3
Step : 4
Step : 6
Step : 7
Step : 8