Page 218 - Excel for Scientists and Engineers: Numerical Methods
P. 218
CHAPTER 9 SYSTEMS OF SIMULTANEOUS EQUATIONS 195
Option Base 1
Option Explicit
Function GaussElim(coeff-matrix, const-vector)
Dim AugMatrixO As Double, ResultVector() As Double
Dim NormFactor As Double
Dim temp As Double, term As Double, ElimFactor As Double
Dim I As Integer, J As Integer, K As Integer
Dim C As Integer, R As Integer
Dim N As Integer
N = coeff-matrix.Rows.Count
ReDim AugMatrix(N, N + I), ResultVector(N)
'Create augmented matrix with dimensions N x (N+I)
For I = 1 To N
ForJ=l TON
AugMatrix(1, J) = coeff-matrix(1, J)
Next J, I
ForJ = 1 To N
AugMatrix(J, N + 1) = const-vector(J)
Next
ForK= 1 To N
'Normalize each row, from column K to right.
'If normalization factor zero, swap rows
NormFactor = AugMatrix(K, K)
If NormFactor = 0 Then
ForJ=IToN+l
temp = AugMatrix(K, J)
AugMatrix(K, J) = AugMatrix(K + 1, J)
AugMatrix(K + 1, J) = temp
Next J
NormFactor = AugMatrix(K, K)
End If
For C = KTo N + 1
AugMatrix(K, C) = AugMatrix(K, C) / NormFactor
Next C
'Eliminate
For R = K + 1 To N
ElimFactor = AugMatrix(R, K)
For C = K To N + 1
AugMatrix(R, C) = AugMatrix(R, C) - AugMatrix(K, C) * ElimFactor
Next C
Next R
Next K
'Calculate and return the coefficients.
'Selected range can be either horizontal or vertical.
For K = N To 1 Step -1