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

CHAPTER 10  ORDINARY DIFFERENTIAL EQUATIONS. PART I                 227


                  I
                   Dim T As String, temp As String
                   Dim NRepl  As Integer, J As Integer
                   Dim dummy As Double
                   T = FormulaText
                   'First, do substitution  of all instances of x address with value
                   NRepl = (Len(T) - Len(Application.Substitute(T, XRef,  I"'))) I  Len(XRef)
                   For J = NRepl To 1 Step -1
                     temp = Application.Substitute(T, XRef, XValue & " ", J)
                     On Error GoTo ErrorHandlerl
                     dummy = Evaluate(temp)
                     T = temp
                   ptl: Next J
                   'Then do substitution of all instances of y address with value
                   NRepl = (Len(T) - Len(Application.Substitute(T, YRef, "")))  I  Len(YRef)
                   For J = NRepl To 1 Step -1
                     temp = Application.Substitute(T, YRef, YValue 81 " ", J)
                     On Error GoTo ErrorHandlerZ
                     dummy = Evaluate(temp)
                     T = temp
                    pt2: Next J
                    result = Evaluate(T)
                    Exit Sub
                    'ERROR HANDLER ROUTINES.
                    ErrorHandlerl  :
                    'Trappable error number 13 (Type mismatch) is expected.
                    If Err.Number = 13 Then
                      On Error GoTo 0   'Disable the error handler.
                      Resume ptl   'and continue execution.
                    Else
                      End  'Some other error, so quit completely
                    End If
                    ErrorHandlerZ:
                    If  Err.Num ber = 13 Then
                      On Error GoTo 0
                      Resume pt2
                    Else
                      End
                    End If
                    End Sub
                            Figure 10-7.  Custom function for Runge-Kutta calculation.
                      (folder 'Chapter 10 Examples', workbook 'ODE Examples', module 'RungeKuttal')
                   In Figure 10-8, the custom function is applied to the same first-order reaction
                kinetics problem that was  calculated on a worksheet  in the preceding sections.
                The formulas in cells C6 and D7 are, respectively,
                   =-k*D6

                and = R u nge 1 (A6, D6, C6, A7-A6)
   245   246   247   248   249   250   251   252   253   254   255