Page 158 - Excel for Scientists and Engineers: Numerical Methods
P. 158
CHAPTER 7 INTEGRATION 135
For J = NRepl To 1 Step -1
temp = Application.Substitute(T, XRef, X & " ", J)
If IsError(Evaluate(temp)) Then GoTo ptl
T = temp
ptl: Next J
F1 = Evaluate0
T = Application.ConvertFormula(FormulaString, xlAl, xlA , xlAbsolute)
For J = NRepl To 1 Step -1
temp = Application.Substitute(T, XRef, X + H & " '0 J)
If IsError(Evaluate(temp)) Then GoTo pt2
T = temp
pt2: Next J
F2 = Evaluate0
area = area + H * (FI + F2) 2
X=X+H
Next K
IntearateT = area
I EndcFunction
Figure 7-8. VBA Function procedure to integrate a worksheet formula
by the trapezoidal approximation method.
(folder 'Chapter 07 Examples,' workbook 'Integration,' module 'Simplehtegration')
Function IntegrateS(expression, variable, from-lower, to-upper)
'Simpson's 113 rule area integration
Dim Formulastring As String, T As String, Xref As String
Dim H As Double, area As Double, X As Double
Dim N As Integer, K As Integer, J As Integer
Dim NRepl As Integer
Dim temp As String
Dim YO As Double, Y1 As Double, Y2 As Double
Formulastring = expression.Formula
XRef = variable.Address
N = 1000
H = (to-upper - from-lower) / N / 2
For K = 0 To N - 1
X=2*K*H
T = Application.ConvertFormula(FormulaString, xlAl , xlAl , xlAbsolute)
NRepl = (Len0 - Len(Application.Substitute(T, XRef, "'I))) / Len(XRef)
For J = NRepl To 1 Step -1
temp = Application.Substitute(T, XRef, from-lower + X & " ", J)
If IsError(Evaluate(temp)) Then GoTo ptl
T = temp
ptl: Next J
YO = Evaluate(T)