Machine Problem No. 5

49
Numerical Methods Group No.:__________________________________ Rating:__________________________ Date Performed: _________________________ 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: Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 1

description

Machine Problem 5 in Numerical methods

Transcript of Machine Problem No. 5

Page 1: Machine Problem No. 5

Numerical Methods

Group No.:__________________________________ Rating:__________________________Date Performed: _________________________ Date Submitted: __________________

Numerical Methods

LINEAR ALGEBRAIC EQUATIONS: GAUSS ELIMINATION AND LU DECOMPOSITIONMachine 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 17 2 −2 −2 −5 −1.25 0

−9 2.3 2.5 0 3 2 40 −3 −7 −7 −1 5.5 −1

−2.5 −5 2 2 −4.5 −1 3.5−8 3.75 −3.35 −3.35 0 −2.1 2−1 −2 4 4 −5 1.15 10

][x1x2x3x4x5x6x7

]=[−1.100027.750014.525045.8000

−24.1750−20.212532.2500

]System B:

[0 10 2.5 −3 −1.1 2.7 −3 2.3

−0.5 1 −2 0 2.5 −1.9 −5 −1.53 −0.75 1.2 2 −10 1 −1 −8

−1 −1 4 −7 −1 3 2 102 0.5 −1 2 −3.1 −4 −2.3 −13.5 2 1.5 −3.35 −1 1.5 −1 2.5−1 4 3.75 4 5 −2 4.5 −3.5−4 0 −2 0 2 3 −7 0

][x1x2x3x4x5x6x7x8

]=[17.27502.67502.3500

−7.75001.0750

−6.5000−0.625030.0000

]Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 1

Page 2: Machine Problem No. 5

Numerical Methods

System C:

[3 −9 −5 −3 0 −1 10 5 107 4 1 0 −1 5 1 −1 −79 8 −2 −5 5 3 −7 1 −1010 −3 4 7 −10 −6 −5 8 −50 4 −7 −6 4 4 −6 −8 −12 2 10 6 −4 −7 −4 7 −88 9 −2 3 0 2 3 −3 39 −7 −2 2 1 −1 4 4 02 −3 0 −1 −2 −1 −5 −2 −4

] [x1x2x3x4x5x6x7x8x9

]=[10920

−16122310

−16

]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.

4. (a) Create a 3×3 Hilbert matrix. This will be the matrix [A ]. Multiply the matrix by the column vector [ x ]=[1 1 1 ]T , generating the matrix [b ]. With this, implement Gauss elimination, Gauss-Jordan elimination, and LU decomposition to solve the system [A ] [x ]=[b ], with the matrix [ x ] unknown.(b) Repeat Problem 4(a) using a 7×7 Hilbert matrix.(c) Repeat Problem 4(a) using a 10×10 Hilbert matrix.(d) In each of the cases, explain the discrepancies using condition numbers for the matrix. Use the Frobenius, as well as the row-sum norms.

5. Polynomial interpolation consists of determining the unique (n−1 )th-order polynomial that fits n data points. Such polynomials have the general form

f ( x )=p1 xn−1+ p2 x

n−2+⋯+pn−1 x+ pn

where the p’s are constant coefficients. A straightforward way for computing the coefficients is to generate n linear algebraic equations that can be solved simultaneously for the coefficients.

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 2

Page 3: Machine Problem No. 5

Numerical Methods

Determine the coefficients of a fourth-order polynomial f ( x )=p1 x4+ p2 x

3+ p3 x2+ p4 x+ p5 that

passes through the following five points: (200,0.746 ), (250,0.675 ), (300,0.616 ), (400,0.525 ) and (500,0.457 ). Substitute each of these pairs into f ( x ) to yield a system of equations with five unknowns (the p’s). Use Gauss elimination and LU 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 EliminationDOFOR 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) ENDDOENDDO

>>>For Backward Substitutionx(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.1Sub Gauss()

Dim a(), b(), x() As DoubleDim i, j, n, k As IntegerDim factor, sum As Double

'i = row number'j = column number'n = matrix size'k = counter

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 3

Page 4: Machine Problem No. 5

Numerical Methods

'a() = Matrix A'b() = Matrix B'x() = Matrix X

n = Range("B1").ValueReDim a(1 To n, 1 To n)ReDim b(1 To n)ReDim x(1 To n)

Range("B11:I17").ClearRange("B19:I25").ClearRange("B27:B33").ClearRange("B3").SelectFor 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).SelectNext i

'For Forward EliminationFor 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 iNext 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).SelectNext i

'For Backward Substitutionx(n) = b(n) / a(n, n)For i = n - 1 To 1 Step -1

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 4

Page 5: Machine Problem No. 5

Numerical Methods

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).SelectNext i

Range("B1").Select

End Sub________________________________________________________________

Sub GaussPivoting()

Dim a(), b(), x() As DoubleDim i, j, n, k, m As IntegerDim factor, sum As Double

'i = row number'j = column number'n = matrix size'k = counter

'a() = Matrix A'b() = Matrix B'x() = Matrix X

n = Range("B1").ValueReDim a(1 To n, 1 To n)ReDim b(1 To n)ReDim x(1 To n)

Range("B11:I17").ClearRange("B19:I25").ClearRange("B27:B33").ClearRange("B3").SelectFor i = 1 To n For j = 1 To n a(i, j) = ActiveCell.Value

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 5

Page 6: Machine Problem No. 5

Numerical Methods

ActiveCell.Offset(0, 1).Select Next j b(i) = ActiveCell.Value ActiveCell.Offset(1, -1 * n).SelectNext i

'For Partial Pivoting

k = 1p = kbig = a(k, k)For ii = k + 1 To n dummy = Abs(a(ii, k)) If dummy > big Then big = dummy p = ii End IfNext 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).SelectNext i

'For Forward Elimination

For k = 1 To n - 1

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 6

Page 7: Machine Problem No. 5

Numerical Methods

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 iNext 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).SelectNext 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).SelectNext 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.

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 7

Page 8: Machine Problem No. 5

Numerical Methods

Code 6.2.1 – Pseudocode for Problem 5.2Call 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 ENDDOElse MsgBox "ill-conditioned system"End IfEnd

Code 6.2.2a – VBA Code for Problem 5.2aOption ExplicitSub LUD()Dim a(), b(), x(), ai() As DoubleDim i, j, n, k As IntegerDim factor, sum As DoubleDim tol, er As Single

tol = 0.000001n = Range("c3").ValueReDim 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).SelectNext 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

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 8

Page 9: Machine Problem No. 5

Numerical Methods

ActiveCell.Offset(1, -n).Select Next i

Else MsgBox "ill-conditioned system"End IfEnd SubSub LUDminverse(a, b, n, x, tol, er, ai)Dim i As Integer, j As IntegerDim 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 ai(j, i) = x(j) Next j Next iEnd IfEnd SubSub Decompose(a, n, tol, o, s, er)Dim i As Integer, j As Integer, k As IntegerDim 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

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 9

Page 10: Machine Problem No. 5

Numerical Methods

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 kIf (Abs(a(o(k), k) / s(o(k))) < tol) Then er = -1End SubSub Pivot(a, o, s, n, k)Dim ii As Integer, p As IntegerDim big As Single, dummy As Single

p = kbig = 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 iidummy = o(p)o(p) = o(k)o(k) = dummyEnd SubSub Substitute(a, o, n, b, x)Dim k As Integer, i As Integer, j As IntegerDim 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 kx(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)

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 10

Page 11: Machine Problem No. 5

Numerical Methods

Next iEnd Sub

Code 5.2.2b – VBA Code for Problem 5.2bOption ExplicitSub LUD()Dim a(), b(), x(), ai() As DoubleDim i, j, n, k As IntegerDim factor, sum As DoubleDim tol, er As Single

tol = 0.000001n = Range("c3").ValueReDim 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).SelectNext 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 IfEnd SubSub LUDminverse(a, b, n, x, tol, er, ai)Dim i As Integer, j As IntegerDim o() As Single, s() As Single

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 11

Page 12: Machine Problem No. 5

Numerical Methods

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 iEnd IfEnd SubSub Decompose(a, n, tol, o, s, er)Dim i As Integer, j As Integer, k As IntegerDim 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

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 12

Page 13: Machine Problem No. 5

Numerical Methods

Next kIf (Abs(a(o(k), k) / s(o(k))) < tol) Then er = -1End SubSub Pivot(a, o, s, n, k)Dim ii As Integer, p As IntegerDim big As Single, dummy As Single

p = kbig = 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 iidummy = o(p)o(p) = o(k)o(k) = dummyEnd SubSub Substitute(a, o, n, b, x)Dim k As Integer, i As Integer, j As IntegerDim 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 kx(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 iEnd Sub

Code 5.2.2c – VBA Code for Problem 5.2cOption ExplicitSub LUD()Dim a(), b(), x(), ai() As DoubleDim i, j, n, k As IntegerDim factor, sum As Double

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 13

Page 14: Machine Problem No. 5

Numerical Methods

Dim tol, er As Single

tol = 0.000001n = Range("c3").ValueReDim 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).SelectNext 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 IfEnd SubSub LUDminverse(a, b, n, x, tol, er, ai)Dim i As Integer, j As IntegerDim 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

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 14

Page 15: Machine Problem No. 5

Numerical Methods

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 iEnd IfEnd SubSub Decompose(a, n, tol, o, s, er)Dim i As Integer, j As Integer, k As IntegerDim 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 kIf (Abs(a(o(k), k) / s(o(k))) < tol) Then er = -1End SubSub Pivot(a, o, s, n, k)Dim ii As Integer, p As IntegerDim big As Single, dummy As Single

p = kbig = Abs(a(o(k), k) / s(o(k))) For ii = k + 1 To n

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 15

Page 16: Machine Problem No. 5

Numerical Methods

dummy = Abs(a(o(ii), k) / s(o(ii))) If dummy > big Then big = dummy p = ii End If Next iidummy = o(p)o(p) = o(k)o(k) = dummyEnd SubSub Substitute(a, o, n, b, x)Dim k As Integer, i As Integer, j As IntegerDim 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 kx(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 iEnd 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.3Call 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 ENDDOElse MsgBox "ill-conditioned system"End If

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 16

Page 17: Machine Problem No. 5

Numerical Methods

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.3aOption ExplicitSub LUD()Dim a(), b(), x(), ai() As DoubleDim i, j, n, k As IntegerDim factor, sum As DoubleDim tol, er As Single

tol = 0.000001n = Range("c3").ValueReDim 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).SelectNext 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 IfEnd Sub_______________________________________________________________

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 17

Page 18: Machine Problem No. 5

Numerical Methods

_Sub LUDminverse(a, b, n, x, tol, er, ai)Dim i As Integer, j As IntegerDim 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 ai(j, i) = x(j) Next j Next iEnd IfEnd Sub________________________________________________________________Sub Decompose(a, n, tol, o, s, er)Dim i As Integer, j As Integer, k As IntegerDim 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

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 18

Page 19: Machine Problem No. 5

Numerical Methods

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 kIf (Abs(a(o(k), k) / s(o(k))) < tol) Then er = -1End Sub________________________________________________________________Sub Pivot(a, o, s, n, k)Dim ii As Integer, p As IntegerDim big As Single, dummy As Single

p = kbig = 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 iidummy = o(p)o(p) = o(k)o(k) = dummyEnd SubSub Substitute(a, o, n, b, x)Dim k As Integer, i As Integer, j As IntegerDim 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 kx(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 iEnd Sub

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 19

Page 20: Machine Problem No. 5

Numerical Methods

Code 5.3.2b – VBA Code for Problem 5.3bOption ExplicitSub LUD()Dim a(), b(), x(), ai() As DoubleDim i, j, n, k As IntegerDim factor, sum As DoubleDim tol, er As Single

tol = 0.000001n = Range("c3").ValueReDim 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).SelectNext 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 IfEnd Sub________________________________________________________________Sub LUDminverse(a, b, n, x, tol, er, ai)Dim i As Integer, j As IntegerDim o() As Single, s() As Single

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 20

Page 21: Machine Problem No. 5

Numerical Methods

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 iEnd IfEnd Sub________________________________________________________________Sub Decompose(a, n, tol, o, s, er)Dim i As Integer, j As Integer, k As IntegerDim 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)

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 21

Page 22: Machine Problem No. 5

Numerical Methods

Next j Next i Next kIf (Abs(a(o(k), k) / s(o(k))) < tol) Then er = -1End Sub________________________________________________________________Sub Pivot(a, o, s, n, k)Dim ii As Integer, p As IntegerDim big As Single, dummy As Single

p = kbig = 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 iidummy = o(p)o(p) = o(k)o(k) = dummyEnd Sub________________________________________________________________Sub Substitute(a, o, n, b, x)Dim k As Integer, i As Integer, j As IntegerDim 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 kx(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 iEnd Sub

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 22

Page 23: Machine Problem No. 5

Numerical Methods

Code 5.3.2c – VBA Code for Problem 5.3cOption ExplicitSub LUD()Dim a(), b(), x(), ai() As DoubleDim i, j, n, k As IntegerDim factor, sum As DoubleDim tol, er As Single

tol = 0.000001n = Range("c3").ValueReDim 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).SelectNext 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 IfEnd Sub________________________________________________________________Sub LUDminverse(a, b, n, x, tol, er, ai)Dim i As Integer, j As IntegerDim o() As Single, s() As Single

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 23

Page 24: Machine Problem No. 5

Numerical Methods

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 iEnd IfEnd Sub________________________________________________________________Sub Decompose(a, n, tol, o, s, er)Dim i As Integer, j As Integer, k As IntegerDim 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)

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 24

Page 25: Machine Problem No. 5

Numerical Methods

Next j Next i Next kIf (Abs(a(o(k), k) / s(o(k))) < tol) Then er = -1End Sub________________________________________________________________Sub Pivot(a, o, s, n, k)Dim ii As Integer, p As IntegerDim big As Single, dummy As Single

p = kbig = 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 iidummy = o(p)o(p) = o(k)o(k) = dummyEnd Sub________________________________________________________________Sub Substitute(a, o, n, b, x)Dim k As Integer, i As Integer, j As IntegerDim 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 kx(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 iEnd Sub

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 25

Page 26: Machine Problem No. 5

Numerical Methods

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 EliminationDOFOR 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) ENDDOENDDO

>>>For Backward Substitutionx(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

Code 5.4.1b – Pseudocode for Problem 5.4b>>>Forward EliminationDOFOR 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) ENDDOENDDO

>>>Backward eliminationDOFOR k = n, 1, -1 DOFOR i = k – 1, 1, -1 factor = a(i, k) / a(k, k)

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 26

Page 27: Machine Problem No. 5

Numerical Methods

DOFOR j = n, 1, -1 a(i, j) = a(i, j) - factor * a(k, j) ENDDO b(i) = b(i) - factor * b(k) ENDDOENDDO

Code 5.4.1c – Pseudocode for Problem 5.4c>>>LU Algorithmer = 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 ENDDOENDDO

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 ENDDOENDDO

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 27

Page 28: Machine Problem No. 5

Numerical Methods

If Abs(a(k, k) / s(k)) < tol Then er = -1End 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 sum = sum + a(i, j) * x(j) ENDDO x(i) = (b(i) - sum) / a(i, i) ENDDOEnd 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 DoubleDim i, j, n, k As IntegerDim factor, sum As Double

'i = row number'j = column number'n = matrix size'k = counter

'a() = Matrix A'b() = Matrix B'x() = Matrix X

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 28

Page 29: Machine Problem No. 5

Numerical Methods

n = Range("B1").ValueReDim a(1 To n, 1 To n)ReDim b(1 To n)ReDim x(1 To n)

Range("B7:E9").ClearRange("B11:B13").ClearRange("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).SelectNext i

'For Forward EliminationFor 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 iNext 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).SelectNext i

'For Backward Substitutionx(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

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 29

Page 30: Machine Problem No. 5

Numerical Methods

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).SelectNext i

End Sub

Code 5.4.2b – VBA Code for Problem 5.4bFor any Square Matrix

Sub GaussJordan()

Dim a(), b(), x() As DoubleDim i, j, n, k, m As IntegerDim factor, sum As Double

'i = row number'j = column number'n = matrix size'k = counter

'a() = Matrix A'b() = Matrix B'x() = Matrix X

n = Range("B1").ValueReDim a(1 To n, 1 To n)ReDim b(1 To n)ReDim x(1 To n)

Range("B7:E9").ClearRange("B11:B13").ClearRange("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

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 30

Page 31: Machine Problem No. 5

Numerical Methods

Next i

'For Forward EliminationFor 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 iNext k

'For Backward eliminationFor 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 iNext 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).SelectNext i

ActiveCell.Offset(1, 0).Select

For i = 1 To n ActiveCell.Value = b(i) ActiveCell.Offset(1, 0).SelectNext i

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 31

Page 32: Machine Problem No. 5

Numerical Methods

End Sub

Code 5.4.2c – VBA Code for Problem 5.4cSub LUDecom()

Dim a(), b(), o(), s(), x() As DoubleDim i, j, n, k As IntegerDim factor, sum, er, tol As Doubletol = 0.5 * 10 ^ (2 - 6)

n = Range("B1").ValueReDim 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)

Range("B7:E9").ClearRange("B11:B13").ClearRange("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).SelectNext i

'LU Algorithmer = 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 jNext i

For k = 1 To n - 1 p = k big = Abs(a(k, k) / s(k))

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 32

Page 33: Machine Problem No. 5

Numerical Methods

For 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 For j = k + 1 To n a(i, j) = a(i, j) - factor * a(k, j) Next j Next iNext k

If Abs(a(k, k) / s(k)) < tol Then er = -1End If

If er <> -1 Then For i = 2 To n sum = b(i) For j = 1 To i - 1 sum = sum - a(i, j) * b(j) Next j b(i) = sum Next i

x(n) = b(n) / a(n, n)

For i = n - 1 To 1 Step -1 sum = 0 For j = i + 1 To n sum = sum + a(i, j) * x(j) Next j x(i) = (b(i) - sum) / a(i, i) Next iEnd If

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 33

Page 34: Machine Problem No. 5

Numerical Methods

'For The Output

Range("B7").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).SelectNext i

Range("B11").Select

For i = 1 To n ActiveCell.Value = x(i) ActiveCell.Offset(1, 0).Select Next i

End Sub

IV. RESULTS AND INTERPRETATION

Machine Problem 5.1

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 34

Page 35: Machine Problem No. 5

Numerical Methods

Machine Problem 5.2

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 35

Page 36: Machine Problem No. 5

Numerical Methods

Machine Problem 5.3

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 36

Page 37: Machine Problem No. 5

Numerical Methods

Machine Problem 5.4

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 37

Page 38: Machine Problem No. 5

Numerical Methods

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 38

Page 39: Machine Problem No. 5

Numerical Methods

V. CONCLUSIONS AND RECOMMENDATIONS

The students arrived to the following conclusions:

1. Gauss-Jordan with Pivoting is better than Gauss Elimination because it doesn’t include Back Substitution.

2. 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

Machine Problem No.6 – Linear Algebraic Equations: Gauss Elimination and LU Decomposition Page 39