Page 254 - Excel for Scientists and Engineers: Numerical Methods
P. 254
CHAPTER 10 ORDINARY DIFFERENTIAL EQUATIONS. PART I 23 1
K = 3: X = x-variable.Value + H I2
For J = 1 To N: Y(J) = y-variables(J).Value + term(2, J) 12: Next J
Call eval3(N, FormulaText, XAddr, YAddr, X, Y, H, K, term)
K = 4: X = x-variable.Value + H
For J = 1 To N: Y(J) = y-variables(J).Value + term(3, J): Next J
Call eval3(N, FormulaText, XAddr, YAddr, X, Y, H, K, term)
For J = 1 To N
Y(J) = y-variables(J).Value+(term(l , J)+2*term(2, J)+2*term(3, J)+term(4, J)) / 6
Next J
RK3 = Y
End Function
..............................................................
Sub eval3(N, FormulaText, XAddr, YAddr, X, Y, H, K, term)
Dim I As Integer, J As Integer
Dim T As String
ForJ = 1 To N
T = FormulaText(J)
Call SubstitutelnStringCT, XAddr, X)
For I = 1 To N
Call SubstitutelnStringCT, YAddr(l), Y(I))
Next I
terrn(K, J) = H * Evaluate(T)
Next J
End Sub
...............................................................
Sub SubstitutelnString(T, Ref, Value)
'Replaces each instance of e.g., $A$2 in formula with number value, e.g., 0.20,
then evaluates.
'Must do this replacement from end of formula to beginning.
'Modified 03/08/06 to handle possible un-intended replacement of e.g., $A$2 in
$A$22.
'Method: replace $A$2 with value & " "
'so that $A$22 becomes "0.20 2" and this formula evaluates to an error.
Dim temp As String
Dim NReplacements As Integer, J As integer
Dim dummy As Double
'Substitute all instances of address with value
NReplacements = (Len(T) - Len(Application.Substitute(T, Ref, "'I))) / Len(Ref)
For J = NReplacements To 1 Step -1
temp = Application.Substitute(T, Ref, Value 8, " ", J)
On Error GoTo ErrorHandler
dummy = Evaluate(temp)
T = temp
ptl: Next J
Exit Sub
ErrorHandler:
'Trappable error number 13 (Type mismatch) is expected.