Professional Documents
Culture Documents
Group No.:__________________________________
Date Performed: _________________________
Rating:__________________________
Date Submitted: __________________
Numerical Methods
LINEAR ALGEBRAIC EQUATIONS: GAUSS ELIMINATION AND LU DECOMPOSITION
Machine Problem No. 6
I. OBJECTIVES
1. Develop algorithms to implement Gauss elimination, Gauss-Jordan elimination and LU
decomposition using MS Excel VBA and MathScript.
2. Use the algorithms developed to solve systems of linear algebraic equations and finding the
inverse of the matrix.
3. Evaluate the condition number of a matrix and use this to explain discrepancies in the results.
II. MACHINE PROBLEMS
1. Implement Gauss and Gauss-Jordan elimination with partial pivoting in VBA and MathScript. Use
the algorithm to solve the following system of equations.
System A:
][ ] [ ]
1
7.5
2.2
2.2
3.2
2.3
1 x1
1.1000
x
2
7
2
2
2
5 1.25 0
27.7500
9
2.3
2.5
0
3
2
4 x3
14.5250
0
3
7
7
1
5.5 1 x 4 = 45.8000
2.5 5
2
2
4.5
1
3.5 x 5 24.1750
8 3.75 3.35 3.35
0
2.1
2 x 6 20.2125
1
2
4
4
5
1.15 10 x
32.2500
7
System B:
Numerical Methods
][ ] [ ]
x1
0
10
2.5
3
1.1 2.7
3
2.3
17.2750
x
2
0.5
1
2
0
2.5 1.9 5 1.5
2.6750
3
0.75 1.2
2
10
1
1
8 x 3
2.3500
x
1
1
4
7
1
3
2
10
4
= 7.7500
2
0.5
1
2
3.1 4 2.3 1 x 5
1.0750
3.5
2
1.5 3.35 1
1.5
1
2.5 x 6 6.5000
1
4
3.75
4
5
2
4.5 3.5 x
0.6250
7
4
0
2
0
2
3
7
0
30.0000
x8
System C:
3
7
9
10
0
2
8
9
2
9
4
8
3
4
2
9
7
3
5
1
2
4
7
10
2
2
0
3
0
1 10
0 1
5
1
5
5
3 7
7 10 6 5
6
4
4 6
6 4 7 4
3
0
2
3
2
1
1 4
1 2 1 5
][ ] [ ]
x1
5
10
10
1 7 x 2
9
x
1 10 3
2
8 5 x 4
0
8 1 x 5 = 16
7 8 x 6
12
3
3 x
23
7
4
0 x
10
8
2 4
16
x9
2. There are two types of LU decomposition. The one demonstrated in the discussion is the Doolittle
decomposition. An alternative one is called Crout decomposition (Chapra, Art. 10.1.4 pp.281-283).
Implement both types of decomposition with partial pivoting in VBA and Mathscript and use them to
solve the systems given in Problem 1. Include in each case an algorithm which counts the number
of flops performed. Compare each.
3. Employ LU decomposition (Doolittle and Crout) to determine the inverses of the coefficient
matrices given in Problem 1. Compare the results of the program with the MINVERSE function in
MS Excel and inv()in MathScript.
Numerical Methods
3 3
4. (a) Create a
column vector
[ x ]= [ 1 1 1 ]
[ A ][ x ] =[ b ] ,
f ( x )= p1 x
where the
n 2
+ p2 x
++ pn1 x+ pn
to generate n linear algebraic equations that can be solved simultaneously for the coefficients.
Determine the coefficients of a fourth-order polynomial
that passes through the following five points:
( 400,0.525 )
and
f ( x )= p1 x 4 + p 2 x 3 + p3 x 2 + p4 x + p 5
( 200,0.746 ) ,
( 250,0.675 ) , ( 300,0.616 ) ,
f (x)
to yield a
decomposition to solve for the coefficients. Also, determine and interpret the condition number and
relate this as to how the curve fits into the given data points. Plot the approximate curve.
III. METHODOLOGY
Code 5.1.1 Pseudocode for Problem 5.1
>>>For Forward Elimination
DOFOR k = 1, n - 1
DOFOR i = k + 1, n
factor = a(i, k) / a(k, k)
DOFOR j = k + 1, n
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 3
Numerical Methods
a(i, j) = a(i, j) - factor * a(k, j)
ENDDO
b(i) = b(i) - factor * b(k)
ENDDO
ENDDO
>>>For Backward Substitution
x(n) = b(n) / a(n, n)
DOFOR i = n 1, 1, -1
sum = b(i)
DOFOR j = i + 1, n
sum = sum - a(i, j) * x(j)
ENDDO
x(i) = sum / a(i, i)
ENDDO
After the group formulated a well-structured pseudocode, the equivalent VBA code is already
generated. In code 6.1.2, the VBA code for machine problem 6.1 is stated.
Code 6.1.2 VBA Code for Problem 6.1
Sub Gauss()
Dim a(), b(), x() As Double
Dim i, j, n, k As Integer
Dim factor, sum As Double
'i
'j
'n
'k
=
=
=
=
row number
column number
matrix size
counter
'a() = Matrix A
'b() = Matrix B
'x() = Matrix X
n = Range("B1").Value
ReDim a(1 To n, 1 To n)
ReDim b(1 To n)
ReDim x(1 To n)
Range("B11:I17").Clear
Range("B19:I25").Clear
Range("B27:B33").Clear
Range("B3").Select
For i = 1 To n
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 4
Numerical Methods
For j = 1 To n
a(i, j) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Next j
b(i) = ActiveCell.Value
ActiveCell.Offset(1, -1 * n).Select
Next i
'For Forward Elimination
For k = 1 To n - 1
For i = k + 1 To n
factor = a(i, k) / a(k, k)
For j = k + 1 To n
a(i, j) = a(i, j) - factor * a(k, j)
Next j
b(i) = b(i) - factor * b(k)
Next i
Next k
ActiveCell.Offset(1, 0).Select
For i = 1 To n
For j = 1 To n
ActiveCell.Value = a(i, j)
ActiveCell.Offset(0, 1).Select
Next j
ActiveCell.Value = b(i)
ActiveCell.Offset(1, -1 * n).Select
Next i
'For Backward Substitution
x(n) = b(n) / a(n, n)
For i = n - 1 To 1 Step -1
sum = b(i)
For j = i + 1 To n
sum = sum - a(i, j) * x(j)
Next j
x(i) = sum / a(i, i)
Next i
Range("B27").Select
For i = 1 To n
ActiveCell.Value = x(i)
ActiveCell.Offset(1, 0).Select
Next i
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 5
Numerical Methods
Range("B1").Select
End Sub
________________________________________________________________
Sub GaussPivoting()
Dim a(), b(), x() As Double
Dim i, j, n, k, m As Integer
Dim factor, sum As Double
'i
'j
'n
'k
=
=
=
=
row number
column number
matrix size
counter
'a() = Matrix A
'b() = Matrix B
'x() = Matrix X
n = Range("B1").Value
ReDim a(1 To n, 1 To n)
ReDim b(1 To n)
ReDim x(1 To n)
Range("B11:I17").Clear
Range("B19:I25").Clear
Range("B27:B33").Clear
Range("B3").Select
For i = 1 To n
For j = 1 To n
a(i, j) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Next j
b(i) = ActiveCell.Value
ActiveCell.Offset(1, -1 * n).Select
Next i
'For Partial Pivoting
k =
p =
big
For
1
k
= a(k, k)
ii = k + 1 To n
dummy = Abs(a(ii, k))
If dummy > big Then
big = dummy
Numerical Methods
p = ii
End If
Next ii
If p <> k Then
For jj = k To n
dummy = a(p, jj)
a(p, jj) = a(k, jj)
a(k, jj) = dummy
Next jj
dummy = b(p)
b(p) = b(k)
b(k) = dummy
End If
ActiveCell.Offset(1, 0).Select
For i = 1 To n
For j = 1 To n
ActiveCell.Value = a(i, j)
ActiveCell.Offset(0, 1).Select
Next j
ActiveCell.Value = b(i)
ActiveCell.Offset(1, -1 * n).Select
Next i
Numerical Methods
Next j
ActiveCell.Value = b(i)
ActiveCell.Offset(1, -1 * n).Select
Next i
'For Backward Substitution
x(n) = b(n) / a(n, n)
For i = n - 1 To 1 Step -1
sum = b(i)
For j = i + 1 To n
sum = sum - a(i, j) * x(j)
Next j
x(i) = sum / a(i, i)
Next i
ActiveCell.Offset(1, 0).Select
For i = 1 To n
ActiveCell.Value = x(i)
ActiveCell.Offset(1, 0).Select
Next i
Range("B1").Select
End Sub
Machine Problem 5.2
In the second machine problem, the group formulates a pseudocode in order to program it
correctly. In the code 6.2.1, the pseudocode of the machine problem 6.2 is stated.
Code 6.2.1 Pseudocode for Problem 5.2
Call LUDminverse(a(), b(), n, x(), tol, er, ai())
If er = 0 Then
DOFOR i = 1, n
DOFOR j = 1, n
DISPLAY = ai(i, j)
ENDDO
ENDDO
Else
MsgBox "ill-conditioned system"
End If
End
Numerical Methods
Code 6.2.2a VBA Code for Problem 5.2a
Option Explicit
Sub LUD()
Dim a(), b(), x(), ai() As Double
Dim i, j, n, k As Integer
Dim factor, sum As Double
Dim tol, er As Single
tol = 0.000001
n = Range("c3").Value
ReDim a(1 To n, 1 To n)
ReDim b(1 To n)
ReDim x(1 To n)
ReDim ai(1 To n, 1 To n)
Range("c6").Select
For i = 1 To n
For j = 1 To n
a(i, j) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Next j
b(i) = ActiveCell.Value
ActiveCell.Offset(1, -1 * n).Select
Next i
Call LUDminverse(a(), b(), n, x(), tol, er, ai())
If er = 0 Then
ActiveCell.Offset(2, 0).Select
For i = 1 To n
For j = 1 To n
ActiveCell.Value = ai(i, j)
ActiveCell.Offset(0, 1).Select
Next j
ActiveCell.Offset(1, -n).Select
Next i
Else
End
End
Sub
Dim
Dim
ReDim o(1 To n)
ReDim s(1 To n)
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 9
Numerical Methods
Call Decompose(a, n, tol, o(), s(), er)
If er = 0 Then
For i = 1 To n
For j = 1 To n
If i = j Then
b(j) = 1
Else
b(j) = 0
End If
Next j
Call Substitute(a, o, n, b, x)
For j = 1 To n
ai(j, i) = x(j)
Next j
Next i
End If
End Sub
Sub Decompose(a, n, tol, o, s, er)
Dim i As Integer, j As Integer, k As Integer
Dim factor As Single
For i = 1 To n
o(i) = i
s(i) = Abs(a(i, 1))
For j = 2 To n
If Abs(a(i, j)) > s(i) Then s(i) = Abs(a(i, j))
Next j
Next i
For k = 1 To n - 1
Call Pivot(a, o, s, n, k)
If Abs(a(o(k), k) / s(o(k))) < tol Then
er = -1
Exit For
End If
For i = k + 1 To n
factor = a(o(i), k) / a(o(k), k)
a(o(i), k) = factor
For j = k + 1 To n
a(o(i), j) = a(o(i), j) - factor * a(o(k), j)
Next j
Next i
Next k
If (Abs(a(o(k), k) / s(o(k))) < tol) Then er = -1
End Sub
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 10
Numerical Methods
Sub Pivot(a, o, s, n, k)
Dim ii As Integer, p As Integer
Dim big As Single, dummy As Single
p = k
big = Abs(a(o(k), k) / s(o(k)))
For ii = k + 1 To n
dummy = Abs(a(o(ii), k) / s(o(ii)))
If dummy > big Then
big = dummy
p = ii
End If
Next ii
dummy = o(p)
o(p) = o(k)
o(k) = dummy
End Sub
Sub Substitute(a, o, n, b, x)
Dim k As Integer, i As Integer, j As Integer
Dim sum As Single, factor As Single
For k = 1 To n - 1
For i = k + 1 To n
factor = a(o(i), k)
b(o(i)) = b(o(i)) - factor * b(o(k))
Next i
Next k
x(n) = b(o(n)) / a(o(n), n)
For i = n - 1 To 1 Step -1
sum = 0
For j = i + 1 To n
sum = sum + a(o(i), j) * x(j)
Next j
x(i) = (b(o(i)) - sum) / a(o(i), i)
Next i
End Sub
Code 5.2.2b VBA Code for Problem 5.2b
Option Explicit
Sub LUD()
Dim a(), b(), x(), ai() As Double
Dim i, j, n, k As Integer
Dim factor, sum As Double
Dim tol, er As Single
Numerical Methods
tol = 0.000001
n = Range("c3").Value
ReDim a(1 To n, 1 To n)
ReDim b(1 To n)
ReDim x(1 To n)
ReDim ai(1 To n, 1 To n)
Range("c6").Select
For i = 1 To n
For j = 1 To n
a(i, j) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Next j
b(i) = ActiveCell.Value
ActiveCell.Offset(1, -1 * n).Select
Next i
Call LUDminverse(a(), b(), n, x(), tol, er, ai())
If er = 0 Then
ActiveCell.Offset(2, 0).Select
For i = 1 To n
For j = 1 To n
ActiveCell.Value = ai(i, j)
ActiveCell.Offset(0, 1).Select
Next j
ActiveCell.Offset(1, -n).Select
Next i
Else
End
End
Sub
Dim
Dim
ReDim o(1 To n)
ReDim s(1 To n)
Call Decompose(a, n, tol, o(), s(), er)
If er = 0 Then
For i = 1 To n
For j = 1 To n
If i = j Then
b(j) = 1
Else
b(j) = 0
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 12
Numerical Methods
End If
Next j
Call Substitute(a, o, n, b, x)
For j = 1 To n
ai(j, i) = x(j)
Next j
Next i
End If
End Sub
Sub Decompose(a, n, tol, o, s, er)
Dim i As Integer, j As Integer, k As Integer
Dim factor As Single
For i = 1 To n
o(i) = i
s(i) = Abs(a(i, 1))
For j = 2 To n
If Abs(a(i, j)) > s(i) Then s(i) = Abs(a(i, j))
Next j
Next i
For k = 1 To n - 1
Call Pivot(a, o, s, n, k)
If Abs(a(o(k), k) / s(o(k))) < tol Then
er = -1
Exit For
End If
For i = k + 1 To n
factor = a(o(i), k) / a(o(k), k)
a(o(i), k) = factor
For j = k + 1 To n
a(o(i), j) = a(o(i), j) - factor * a(o(k), j)
Next j
Next i
Next k
If (Abs(a(o(k), k) / s(o(k))) < tol) Then er = -1
End Sub
Sub Pivot(a, o, s, n, k)
Dim ii As Integer, p As Integer
Dim big As Single, dummy As Single
p = k
big = Abs(a(o(k), k) / s(o(k)))
For ii = k + 1 To n
dummy = Abs(a(o(ii), k) / s(o(ii)))
If dummy > big Then
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 13
Numerical Methods
big = dummy
p = ii
End If
Next ii
dummy = o(p)
o(p) = o(k)
o(k) = dummy
End Sub
Sub Substitute(a, o, n, b, x)
Dim k As Integer, i As Integer, j As Integer
Dim sum As Single, factor As Single
For k = 1 To n - 1
For i = k + 1 To n
factor = a(o(i), k)
b(o(i)) = b(o(i)) - factor * b(o(k))
Next i
Next k
x(n) = b(o(n)) / a(o(n), n)
For i = n - 1 To 1 Step -1
sum = 0
For j = i + 1 To n
sum = sum + a(o(i), j) * x(j)
Next j
x(i) = (b(o(i)) - sum) / a(o(i), i)
Next i
End Sub
Code 5.2.2c VBA Code for Problem 5.2c
Option Explicit
Sub LUD()
Dim a(), b(), x(), ai() As Double
Dim i, j, n, k As Integer
Dim factor, sum As Double
Dim tol, er As Single
tol = 0.000001
n = Range("c3").Value
ReDim a(1 To n, 1 To n)
ReDim b(1 To n)
ReDim x(1 To n)
ReDim ai(1 To n, 1 To n)
Range("c6").Select
For i = 1 To n
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 14
Numerical Methods
For j = 1 To n
a(i, j) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Next j
b(i) = ActiveCell.Value
ActiveCell.Offset(1, -1 * n).Select
Next i
Call LUDminverse(a(), b(), n, x(), tol, er, ai())
If er = 0 Then
ActiveCell.Offset(2, 0).Select
For i = 1 To n
For j = 1 To n
ActiveCell.Value = ai(i, j)
ActiveCell.Offset(0, 1).Select
Next j
ActiveCell.Offset(1, -n).Select
Next i
Else
End
End
Sub
Dim
Dim
ReDim o(1 To n)
ReDim s(1 To n)
Call Decompose(a, n, tol, o(), s(), er)
If er = 0 Then
For i = 1 To n
For j = 1 To n
If i = j Then
b(j) = 1
Else
b(j) = 0
End If
Next j
Call Substitute(a, o, n, b, x)
For j = 1 To n
ai(j, i) = x(j)
Next j
Next i
End If
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 15
Numerical Methods
End
Sub
Dim
Dim
Sub
Decompose(a, n, tol, o, s, er)
i As Integer, j As Integer, k As Integer
factor As Single
For i = 1 To n
o(i) = i
s(i) = Abs(a(i, 1))
For j = 2 To n
If Abs(a(i, j)) > s(i) Then s(i) = Abs(a(i, j))
Next j
Next i
For k = 1 To n - 1
Call Pivot(a, o, s, n, k)
If Abs(a(o(k), k) / s(o(k))) < tol Then
er = -1
Exit For
End If
For i = k + 1 To n
factor = a(o(i), k) / a(o(k), k)
a(o(i), k) = factor
For j = k + 1 To n
a(o(i), j) = a(o(i), j) - factor * a(o(k), j)
Next j
Next i
Next k
If (Abs(a(o(k), k) / s(o(k))) < tol) Then er = -1
End Sub
Sub Pivot(a, o, s, n, k)
Dim ii As Integer, p As Integer
Dim big As Single, dummy As Single
p = k
big = Abs(a(o(k), k) / s(o(k)))
For ii = k + 1 To n
dummy = Abs(a(o(ii), k) / s(o(ii)))
If dummy > big Then
big = dummy
p = ii
End If
Next ii
dummy = o(p)
o(p) = o(k)
o(k) = dummy
End Sub
Sub Substitute(a, o, n, b, x)
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 16
Numerical Methods
Dim k As Integer, i As Integer, j As Integer
Dim sum As Single, factor As Single
For k = 1 To n - 1
For i = k + 1 To n
factor = a(o(i), k)
b(o(i)) = b(o(i)) - factor * b(o(k))
Next i
Next k
x(n) = b(o(n)) / a(o(n), n)
For i = n - 1 To 1 Step -1
sum = 0
For j = i + 1 To n
sum = sum + a(o(i), j) * x(j)
Next j
x(i) = (b(o(i)) - sum) / a(o(i), i)
Next i
End Sub
Machine Problem 5.3
In the third machine problem, the group formulates a pseudocode in order to program it correctly.
In the code 6.3.1, the pseudocode of the machine problem 6.3 is stated.
Code 5.3.1 Pseudocode for Problem 5.3
Call LUDminverse(a(), b(), n, x(), tol, er, ai())
If er = 0 Then
DOFOR i = 1, n
DOFOR j = 1, n
DISPLAY ai(i, j)
ENDDO
ENDDO
Else
MsgBox "ill-conditioned system"
End If
END
After the group formulated a well-structured pseudocode, the equivalent VBA code is already
generated. In code 6.3.2, the VBA code for machine problem 6.3 is stated.
Code 5.3.2a VBA Code for Problem 5.3a
Option Explicit
Sub LUD()
Dim a(), b(), x(), ai() As Double
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 17
Numerical Methods
Dim i, j, n, k As Integer
Dim factor, sum As Double
Dim tol, er As Single
tol = 0.000001
n = Range("c3").Value
ReDim a(1 To n, 1 To n)
ReDim b(1 To n)
ReDim x(1 To n)
ReDim ai(1 To n, 1 To n)
Range("c6").Select
For i = 1 To n
For j = 1 To n
a(i, j) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Next j
b(i) = ActiveCell.Value
ActiveCell.Offset(1, -1 * n).Select
Next i
Call LUDminverse(a(), b(), n, x(), tol, er, ai())
If er = 0 Then
ActiveCell.Offset(2, 0).Select
For i = 1 To n
For j = 1 To n
ActiveCell.Value = ai(i, j)
ActiveCell.Offset(0, 1).Select
Next j
ActiveCell.Offset(1, -n).Select
Next i
Else
MsgBox "ill-conditioned system"
End If
End Sub
________________________________________________________________
Sub LUDminverse(a, b, n, x, tol, er, ai)
Dim i As Integer, j As Integer
Dim o() As Single, s() As Single
ReDim o(1 To n)
ReDim s(1 To n)
Call Decompose(a, n, tol, o(), s(), er)
If er = 0 Then
For i = 1 To n
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 18
Numerical Methods
For j = 1 To n
If i = j Then
b(j) = 1
Else
b(j) = 0
End If
Next j
Call Substitute(a, o, n, b, x)
For j = 1 To n
ai(j, i) = x(j)
Next j
Next i
End If
End Sub
________________________________________________________________
Sub Decompose(a, n, tol, o, s, er)
Dim i As Integer, j As Integer, k As Integer
Dim factor As Single
For i = 1 To n
o(i) = i
s(i) = Abs(a(i, 1))
For j = 2 To n
If Abs(a(i, j)) > s(i) Then s(i) = Abs(a(i, j))
Next j
Next i
For k = 1 To n - 1
Call Pivot(a, o, s, n, k)
If Abs(a(o(k), k) / s(o(k))) < tol Then
er = -1
Exit For
End If
For i = k + 1 To n
factor = a(o(i), k) / a(o(k), k)
a(o(i), k) = factor
For j = k + 1 To n
a(o(i), j) = a(o(i), j) - factor * a(o(k), j)
Next j
Next i
Next k
If (Abs(a(o(k), k) / s(o(k))) < tol) Then er = -1
End Sub
________________________________________________________________
Sub Pivot(a, o, s, n, k)
Dim ii As Integer, p As Integer
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 19
Numerical Methods
Dim big As Single, dummy As Single
p = k
big = Abs(a(o(k), k) / s(o(k)))
For ii = k + 1 To n
dummy = Abs(a(o(ii), k) / s(o(ii)))
If dummy > big Then
big = dummy
p = ii
End If
Next ii
dummy = o(p)
o(p) = o(k)
o(k) = dummy
End Sub
Sub Substitute(a, o, n, b, x)
Dim k As Integer, i As Integer, j As Integer
Dim sum As Single, factor As Single
For k = 1 To n - 1
For i = k + 1 To n
factor = a(o(i), k)
b(o(i)) = b(o(i)) - factor * b(o(k))
Next i
Next k
x(n) = b(o(n)) / a(o(n), n)
For i = n - 1 To 1 Step -1
sum = 0
For j = i + 1 To n
sum = sum + a(o(i), j) * x(j)
Next j
x(i) = (b(o(i)) - sum) / a(o(i), i)
Next i
End Sub
Code 5.3.2b VBA Code for Problem 5.3b
Option Explicit
Sub LUD()
Dim a(), b(), x(), ai() As Double
Dim i, j, n, k As Integer
Dim factor, sum As Double
Dim tol, er As Single
tol = 0.000001
n = Range("c3").Value
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 20
Numerical Methods
ReDim a(1 To n, 1 To n)
ReDim b(1 To n)
ReDim x(1 To n)
ReDim ai(1 To n, 1 To n)
Range("c6").Select
For i = 1 To n
For j = 1 To n
a(i, j) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Next j
b(i) = ActiveCell.Value
ActiveCell.Offset(1, -1 * n).Select
Next i
Call LUDminverse(a(), b(), n, x(), tol, er, ai())
If er = 0 Then
ActiveCell.Offset(2, 0).Select
For i = 1 To n
For j = 1 To n
ActiveCell.Value = ai(i, j)
ActiveCell.Offset(0, 1).Select
Next j
ActiveCell.Offset(1, -n).Select
Next i
Else
MsgBox "ill-conditioned system"
End If
End Sub
________________________________________________________________
Sub LUDminverse(a, b, n, x, tol, er, ai)
Dim i As Integer, j As Integer
Dim o() As Single, s() As Single
ReDim o(1 To n)
ReDim s(1 To n)
Call Decompose(a, n, tol, o(), s(), er)
If er = 0 Then
For i = 1 To n
For j = 1 To n
If i = j Then
b(j) = 1
Else
b(j) = 0
End If
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 21
Numerical Methods
Next j
Call Substitute(a, o, n, b, x)
For j = 1 To n
ai(j, i) = x(j)
Next j
Next i
End If
End Sub
________________________________________________________________
Sub Decompose(a, n, tol, o, s, er)
Dim i As Integer, j As Integer, k As Integer
Dim factor As Single
For i = 1 To n
o(i) = i
s(i) = Abs(a(i, 1))
For j = 2 To n
If Abs(a(i, j)) > s(i) Then s(i) = Abs(a(i, j))
Next j
Next i
For k = 1 To n - 1
Call Pivot(a, o, s, n, k)
If Abs(a(o(k), k) / s(o(k))) < tol Then
er = -1
Exit For
End If
For i = k + 1 To n
factor = a(o(i), k) / a(o(k), k)
a(o(i), k) = factor
For j = k + 1 To n
a(o(i), j) = a(o(i), j) - factor * a(o(k), j)
Next j
Next i
Next k
If (Abs(a(o(k), k) / s(o(k))) < tol) Then er = -1
End Sub
________________________________________________________________
Sub Pivot(a, o, s, n, k)
Dim ii As Integer, p As Integer
Dim big As Single, dummy As Single
p = k
big = Abs(a(o(k), k) / s(o(k)))
For ii = k + 1 To n
dummy = Abs(a(o(ii), k) / s(o(ii)))
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 22
Numerical Methods
If dummy > big Then
big = dummy
p = ii
End If
Next ii
dummy = o(p)
o(p) = o(k)
o(k) = dummy
End Sub
________________________________________________________________
Sub Substitute(a, o, n, b, x)
Dim k As Integer, i As Integer, j As Integer
Dim sum As Single, factor As Single
For k = 1 To n - 1
For i = k + 1 To n
factor = a(o(i), k)
b(o(i)) = b(o(i)) - factor * b(o(k))
Next i
Next k
x(n) = b(o(n)) / a(o(n), n)
For i = n - 1 To 1 Step -1
sum = 0
For j = i + 1 To n
sum = sum + a(o(i), j) * x(j)
Next j
x(i) = (b(o(i)) - sum) / a(o(i), i)
Next i
End Sub
Numerical Methods
Range("c6").Select
For i = 1 To n
For j = 1 To n
a(i, j) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Next j
b(i) = ActiveCell.Value
ActiveCell.Offset(1, -1 * n).Select
Next i
Call LUDminverse(a(), b(), n, x(), tol, er, ai())
If er = 0 Then
ActiveCell.Offset(2, 0).Select
For i = 1 To n
For j = 1 To n
ActiveCell.Value = ai(i, j)
ActiveCell.Offset(0, 1).Select
Next j
ActiveCell.Offset(1, -n).Select
Next i
Else
MsgBox "ill-conditioned system"
End If
End Sub
________________________________________________________________
Sub LUDminverse(a, b, n, x, tol, er, ai)
Dim i As Integer, j As Integer
Dim o() As Single, s() As Single
ReDim o(1 To n)
ReDim s(1 To n)
Call Decompose(a, n, tol, o(), s(), er)
If er = 0 Then
For i = 1 To n
For j = 1 To n
If i = j Then
b(j) = 1
Else
b(j) = 0
End If
Next j
Call Substitute(a, o, n, b, x)
For j = 1 To n
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 24
Numerical Methods
ai(j, i) = x(j)
Next j
Next i
End If
End Sub
________________________________________________________________
Sub Decompose(a, n, tol, o, s, er)
Dim i As Integer, j As Integer, k As Integer
Dim factor As Single
For i = 1 To n
o(i) = i
s(i) = Abs(a(i, 1))
For j = 2 To n
If Abs(a(i, j)) > s(i) Then s(i) = Abs(a(i, j))
Next j
Next i
For k = 1 To n - 1
Call Pivot(a, o, s, n, k)
If Abs(a(o(k), k) / s(o(k))) < tol Then
er = -1
Exit For
End If
For i = k + 1 To n
factor = a(o(i), k) / a(o(k), k)
a(o(i), k) = factor
For j = k + 1 To n
a(o(i), j) = a(o(i), j) - factor * a(o(k), j)
Next j
Next i
Next k
If (Abs(a(o(k), k) / s(o(k))) < tol) Then er = -1
End Sub
________________________________________________________________
Sub Pivot(a, o, s, n, k)
Dim ii As Integer, p As Integer
Dim big As Single, dummy As Single
p = k
big = Abs(a(o(k), k) / s(o(k)))
For ii = k + 1 To n
dummy = Abs(a(o(ii), k) / s(o(ii)))
If dummy > big Then
big = dummy
p = ii
End If
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 25
Numerical Methods
Next ii
dummy = o(p)
o(p) = o(k)
o(k) = dummy
End Sub
________________________________________________________________
Sub Substitute(a, o, n, b, x)
Dim k As Integer, i As Integer, j As Integer
Dim sum As Single, factor As Single
For k = 1 To n - 1
For i = k + 1 To n
factor = a(o(i), k)
b(o(i)) = b(o(i)) - factor * b(o(k))
Next i
Next k
x(n) = b(o(n)) / a(o(n), n)
For i = n - 1 To 1 Step -1
sum = 0
For j = i + 1 To n
sum = sum + a(o(i), j) * x(j)
Next j
x(i) = (b(o(i)) - sum) / a(o(i), i)
Next i
End Sub
Machine Problem 5.4
In the fourth machine problem, the group formulates a pseudocode in order to program it correctly.
In the code 6.4.1, the pseudocode of the machine problem 5.4 is stated.
Code 5.4.1a Pseudocode for Problem 5.4a
>>>For Forward Elimination
DOFOR k = 1, n - 1
DOFOR i = k + 1, n
factor = a(i, k) / a(k, k)
DOFOR j = k + 1, n
a(i, j) = a(i, j) - factor * a(k, j)
ENDDO
b(i) = b(i) - factor * b(k)
ENDDO
ENDDO
>>>For Backward Substitution
x(n) = b(n) / a(n, n)
DOFOR i = n 1, 1, -1
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 26
Numerical Methods
sum = b(i)
DOFOR j = i + 1, n
sum = sum - a(i, j) * x(j)
ENDDO
x(i) = sum / a(i, i)
ENDDO
Code 5.4.1b Pseudocode for Problem 5.4b
>>>Forward Elimination
DOFOR k = 1, n
b(k) = b(k) / a(k, k)
DOFOR m = n, k, -1
a(k, m) = a(k, m) / a(k, k)
ENDDO
DOFOR i = k + 1, n
factor = a(i, k) / a(k, k)
DOFOR j = k, n
a(i, j) = a(i, j) - factor * a(k, j)
ENDDO
b(i) = b(i) - factor * b(k)
ENDDO
ENDDO
>>>Backward elimination
DOFOR k = n, 1, -1
DOFOR i = k 1, 1, -1
factor = a(i, k) / a(k, k)
DOFOR j = n, 1, -1
a(i, j) = a(i, j) - factor * a(k, j)
ENDDO
b(i) = b(i) - factor * b(k)
ENDDO
ENDDO
Code 5.4.1c Pseudocode for Problem 5.4c
>>>LU Algorithm
er = 0
DOFOR i = 1, n
o(i) = i
s(i) = Abs(a(i, 1))
DOFOR j = 2, n
If Abs(a(i, j)) > s(i) Then
s(i) = Abs(a(i, j))
End If
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 27
Numerical Methods
ENDDO
ENDDO
DOFOR k = 1, n - 1
p = k
big = Abs(a(k, k) / s(k))
DOFOR ii = k + 1, n
dummy = Abs(a(ii, k) / s(ii))
If dummy > big Then
big = dummy
p = ii
End If
ENDDO
dummy = o(p)
o(p) = o(k)
o(k) = dummy
If Abs(a(k, k) / s(k)) < tol Then
er = -1
ENDDO
End If
DOFOR i = k + 1, n
factor = a(i, k) / a(k, k)
a(i, k) = factor
DOFOR j = k + 1, n
a(i, j) = a(i, j) - factor * a(k, j)
ENDDO
ENDDO
ENDDO
If Abs(a(k, k) / s(k)) < tol Then
er = -1
End If
If er <> -1 Then
DOFOR i = 2, n
sum = b(i)
DOFOR j = 1, i - 1
sum = sum - a(i, j) * b(j)
ENDDO
b(i) = sum
ENDDO
x(n) = b(n) / a(n, n)
DOFOR i = n 1, 1, -1
sum = 0
DOFOR j = i + 1, n
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 28
Numerical Methods
sum = sum + a(i, j) * x(j)
ENDDO
x(i) = (b(i) - sum) / a(i, i)
ENDDO
End If
After the group formulated a well-structured pseudocode, the equivalent VBA code is already
generated. In code 6.4.2, the VBA code for machine problem 6.4 is stated.
Code 5.4.2a VBA Code for Problem 5.4a
>>>For any Square Matrix
Sub GaussElim()
Dim a(), b(), x() As Double
Dim i, j, n, k As Integer
Dim factor, sum As Double
'i
'j
'n
'k
=
=
=
=
row number
column number
matrix size
counter
'a() = Matrix A
'b() = Matrix B
'x() = Matrix X
n = Range("B1").Value
ReDim a(1 To n, 1 To n)
ReDim b(1 To n)
ReDim x(1 To n)
Range("B7:E9").Clear
Range("B11:B13").Clear
Range("B3").Select
For i = 1 To n
For j = 1 To n
a(i, j) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Next j
b(i) = ActiveCell.Value
ActiveCell.Offset(1, -1 * n).Select
Next i
Numerical Methods
'For Forward Elimination
For k = 1 To n - 1
For i = k + 1 To n
factor = a(i, k) / a(k, k)
For j = k + 1 To n
a(i, j) = a(i, j) - factor * a(k, j)
Next j
b(i) = b(i) - factor * b(k)
Next i
Next k
ActiveCell.Offset(1, 0).Select
For i = 1 To n
For j = 1 To n
ActiveCell.Value = a(i, j)
ActiveCell.Offset(0, 1).Select
Next j
ActiveCell.Value = b(i)
ActiveCell.Offset(1, -1 * n).Select
Next i
'For Backward Substitution
x(n) = b(n) / a(n, n)
For i = n - 1 To 1 Step -1
sum = b(i)
For j = i + 1 To n
sum = sum - a(i, j) * x(j)
Next j
x(i) = sum / a(i, i)
Next i
ActiveCell.Offset(1, 0).Select
For i = 1 To n
ActiveCell.Value = x(i)
ActiveCell.Offset(1, 0).Select
Next i
End Sub
Code 5.4.2b VBA Code for Problem 5.4b
For any Square Matrix
Sub GaussJordan()
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 30
Numerical Methods
Dim a(), b(), x() As Double
Dim i, j, n, k, m As Integer
Dim factor, sum As Double
'i
'j
'n
'k
=
=
=
=
row number
column number
matrix size
counter
'a() = Matrix A
'b() = Matrix B
'x() = Matrix X
n = Range("B1").Value
ReDim a(1 To n, 1 To n)
ReDim b(1 To n)
ReDim x(1 To n)
Range("B7:E9").Clear
Range("B11:B13").Clear
Range("B3").Select
For i = 1 To n
For j = 1 To n
a(i, j) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Next j
b(i) = ActiveCell.Value
ActiveCell.Offset(1, -1 * n).Select
Next i
'For Forward Elimination
For k = 1 To n
b(k) = b(k) / a(k, k)
For m = n To k Step -1
a(k, m) = a(k, m) / a(k, k)
Next m
For i = k + 1 To n
factor = a(i, k) / a(k, k)
For j = k To n
a(i, j) = a(i, j) - factor * a(k, j)
Next j
b(i) = b(i) - factor * b(k)
Next i
Next k
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 31
Numerical Methods
'For Backward elimination
For k = n To 1 Step -1
For i = k - 1 To 1 Step -1
factor = a(i, k) / a(k, k)
For j = n To 1 Step -1
a(i, j) = a(i, j) - factor * a(k, j)
Next j
b(i) = b(i) - factor * b(k)
Next i
Next k
ActiveCell.Offset(1, 0).Select
For i = 1 To n
For j = 1 To n
ActiveCell.Value = a(i, j)
ActiveCell.Offset(0, 1).Select
Next j
ActiveCell.Value = b(i)
ActiveCell.Offset(1, -1 * n).Select
Next i
ActiveCell.Offset(1, 0).Select
For i = 1 To n
ActiveCell.Value = b(i)
ActiveCell.Offset(1, 0).Select
Next i
End Sub
Code 5.4.2c VBA Code for Problem 5.4c
Sub LUDecom()
Dim
Dim
Dim
tol
n = Range("B1").Value
ReDim a(1 To n, 1 To n)
ReDim b(1 To n)
ReDim o(1 To n)
ReDim s(1 To n)
ReDim x(1 To n)
Machine Problem No.6 Linear Algebraic Equations: Gauss Elimination and LU
Decomposition
Page 32
Numerical Methods
Range("B7:E9").Clear
Range("B11:B13").Clear
Range("B3").Select
For i = 1 To n
For j = 1 To n
a(i, j) = ActiveCell.Value
ActiveCell.Offset(0, 1).Select
Next j
b(i) = ActiveCell.Value
ActiveCell.Offset(1, -1 * n).Select
Next i
'LU Algorithm
er = 0
For i = 1 To n
o(i) = i
s(i) = Abs(a(i, 1))
For j = 2 To n
If Abs(a(i, j)) > s(i) Then
s(i) = Abs(a(i, j))
End If
Next j
Next i
For k =
p =
big
For
1 To n - 1
k
= Abs(a(k, k) / s(k))
ii = k + 1 To n
dummy = Abs(a(ii, k) / s(ii))
If dummy > big Then
big = dummy
p = ii
End If
Next ii
dummy = o(p)
o(p) = o(k)
o(k) = dummy
If Abs(a(k, k) / s(k)) < tol Then
er = -1
Exit For
End If
For i = k + 1 To n
factor = a(i, k) / a(k, k)
a(i, k) = factor
Numerical Methods
For j = k + 1 To n
a(i, j) = a(i, j) - factor * a(k, j)
Next j
Next i
Next k
If Abs(a(k, k) / s(k)) < tol Then
er = -1
End If
If er <> -1
For i =
sum
For
Then
2 To n
= b(i)
j = 1 To i - 1
sum = sum - a(i, j) * b(j)
Next j
b(i) = sum
Next i
Numerical Methods
ActiveCell.Offset(1, 0).Select
Next i
End Sub
Numerical Methods
Numerical Methods
Machine Problem 5.2
Numerical Methods
Machine Problem 5.3
Numerical Methods
Numerical Methods
Numerical Methods
Gauss-Jordan with Pivoting is better than Gauss Elimination because it doesnt include Back
Substitution.
There is not much of a difference finding the inverse of the matrix when the LU decomposition or
the MInverse is used. Only that the inverses (both in two methods) does not arrive at the same
value of the inverse at a particular term.
VI. REFERENCES
Chapra, & Canale (2006).Numerical Methods for Engineers.New York : McGraw Hill