Page 229 - Excel for Scientists and Engineers: Numerical Methods
P. 229

206                                        EXCEL: NUMERICAL METHODS



                   Option Base 1
                   Option Explicit
                   ,+++++++++++++++++++i+++++++++++++++++++++i+i++++++ii+++
                   Function GaussSeidel(coeff-matrix,  const-vector,  Optional init-values)
                   '  Solving systems of linear equations by the GaussSeidel method.
                   '  Coefficients matrix cannot have zero diagonal element.
                   Dim Resultvector()  As Double
                   Dim I  As Integer, J As Integer, K As Integer
                   Dim N As Integer, Nlterations As Integer
                   Dim R As Integer, C As Integer
                   Dim ConvergeFlag  As Boolean
                   Dim result As Double, sum As Double
                   N = coeff_matrix.Rows.Count
                   If coeff-matrix.Columns.Count  <> N Or const-vector.Rows.Count  -
                   c> N Then GaussSeidel = CVErr(x1ErrRef): Exit Function
                   ReDirn ResultVector(N)
                   ' Following shows code for either fixed or adjustable iteration parameters.
                   ' MaxChange and Maxlterations are set in the Tools/Options/Calculation menu.
                   tolerance = 0.00000001
                   Nlterations = 100
                   ' User can specify optional initial values for the calculation
                   ' This may be helpful for large arrays.
                   If Not (IsMissing(init-values)) Then
                   ' Test if init-values  is a Range.
                    If Not (IsError(init-values.Address)) Then
                     If init-values.Rows.Count  = 1 Then
                      K = init-values.Columns.Count
                     Else
                      K = init-values.Rows.Count
                     End If
                    Else
                   ' init-values  must be an expression.
                     K = UBound(init-values)
                    End If
                    For I = 1 To K
                     ResultVector(1) = init-values(1)
                    Next I
                   End If
                   ' Begin the iteration process.
                   For J = 1 To Nlterations
                   ' Flag will be set to false if any of the result values has not yet converged.
                   ConvergeFlag  = True
                   ' Do each row in the matrix.
                   For R = 1 To N
                    sum = 0
                   '  Sum each term in the row, but skip term on the diagonal.
                    ForC= 1 TON
                     sum = sum + coeff-matrix(R, C) * ResultVector(C)                 I
   224   225   226   227   228   229   230   231   232   233   234