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

172                                        EXCEL: NUMERICAL METHODS




                   Call EvaluateByBairstowMethod(N, A, Root)
                   Bairstow  = Root()
                   End Function
                   ..........................................................
                   Sub EvaluateByBairstowMethod(N, A, Root)
                   Code adapted from Shoup, "Numerical Methods for the Personal Computer".
                   Dim B() As Double, C() As Double
                   Dim M As Integer, I As Integer, J As Integer, IT As Integer
                   Dim P As Double, Q  As Double, delP  As Double, delQ  As Double
                   Dim denom As Double, S1 As Double
                   Dim tolerance As Double
                   ReDirn B(N + 2), C(N + 2)
                   tolerance = 0.000000000000001
                   M=N
                   While M > 0
                   If M = 1 Then Root(M, 0) = -A(O):  Call Sort(Root, N): Exit Sub
                   P = 0: Q = 0: delP = 1 : delQ = 1
                   For I = 0 To N: B(I) = 0: C(I) = 0: Next
                   For IT = 1 To 20
                   If Abs(delP) < tolerance And Abs(delQ) < tolerance Then Exit For
                    For J = 0 To M
                     B(M - J) =A(M - J) + P * B(M - J + 1) + Q * B(M- J + 2)
                     C(M - J) = B(M - J) + P * C(M - J + 1) + Q * C(M - J + 2)
                    Next J
                     denom = C(2) A  2 - C(l) * C(3)
                     delP = (-B(l) * C(2) + B(0) * C(3)) / denom
                     delQ = (-C(2) * B(0) + C(l) * B(1)) I denom
                     P = P + delP
                     Q = Q + delQ
                   Next IT
                   S1 =PA2+4*Q
                   If S1 i Then
                         0
                    'Handle imaginary roots
                    Root(M, 0) = P / 2: Root(M, 1) = Sqr(-S1) / 2
                    Root(M - 1, 0) = P I2: Root(M - 1, 1) = -Sqr(-S1) / 2
                   Else
                    'Handle real roots
                    Root(M,  0) = (P + Sqr(S1)) / 2
                    Root(M - 1, 0) = (P - Sqr(S1)) / 2
                   End If
                   For I = M To 0 Step -1: A(I) = B(I + 2): Next
                   M=M-2
                   Wend
                   End Sub
                   '+++++++++i+++++++++++i+++++i+++i~++++++++i+++i++++i+++i+++
                   Sub Sort(Root, N)
                   'SORT ROOTS IN ASCENDING ORDER
                   Dim I As Integer, J As Integer
   190   191   192   193   194   195   196   197   198   199   200