Page 194 - Excel for Scientists and Engineers: Numerical Methods
P. 194
CHAPTER 8 ROOTS OF EOUATIONS 171
End If
Reffext = reference.Address
'PARSE THE FORMULA INTO TERMS
'pointers: pl, beginning; p2, end of string.
FormulaText = FormulaText & " I' 'add extra character for parsing
pl = 1
ParenFlag = 0 'Keep track of left and right parentheses
For J = 1 To Len(Formu1aText)
char = Mid(FormulaText, J, 1)
If char = "(" Then ParenFlag = ParenFlag + 1
If char = ")" Then ParenFlag = ParenFlag - 1
If ((char = "+" Or char = "-") And ParenFlag = 0) Or J = Len(Formu1aText) -
Then
-term = Mid(FormulaText, pl , J - pl)
term = Application.Substitute(term, NameText, Reffext)
p2 = J: pl = p2
'GET THE EXPONENT AND COEFFICIENT FOR EACH TERM
'p3: location of reference in term.
If InStr(1, term, Reffext & 'IA") Then 'function returns zero if not found
'These are the x"2 and higher terms
p3 = InStr(1, term, Reffext & """)
expnumber = Mid(term, p3 + Len(Reffext) + 1, 1)
term = Left(term, p3 - 1) 'term is now the coefficient part
Elself InStr(1, term, Reffext) Then
'This is the x term
p3 = InStr(1, term, Reffext)
expnumber = 1
term = Left(term, p3 - 1) 'term is now the coefficient part
Else
'This is the constant term
expnumber = 0
End If
If term = I"' Then term = "=I" 'If missing, Evaluate will require a string.
If term = "+" Or term = "-" Then term = term & "1"
If Right(term, 1) = "*" Then term = Left(term, Len(term) - 1)
A(expnumber) = Evaluate(term)
End If
Next J
'RESIZE THE ARRAY
For J = 6 To 0 Step -1
If A(J) <> 0 Then N = J: Exit For
Next
ReDim Preserve A(N)
ReDim Root(1 To N, 1)
'REDUCE POLYNOMIAL SO THAT FIRST COEFF = 1
For J = 0 To N: A(J) = A(J) / A(N): Next
'SCALE THE POLYNOMIAL, IF NECESSARY
'<code to be added later>