Simpson's Rule
Simpson's Rule
Simpson's Rule
Function y(x)
'This function computes the pdf of the standard normal
'distribution at x.
y = Application.NormDist(x, 0, 1, False)
End Function
With n = 10, Simpson gives the same answer as Excel's normdist function so, I think my
code works. Try it! Any comments are welcome.
Thanks,
- Tom Wellington
PS I created the Simpson function by "jazzing up" the Integral function on page 195 of
Bernard Liengme's book (see reference below). Thanks to professor Liengme for letting me
post my modification here.
Reference: Bernard V. Liengme, A Guide to Microsoft Excel for Scientists and Engineers. 2nd
Edition, 2000. Woborn, MA
As Long
Dim TheStep
As Double
Sub Simpson()
Sheets("Sheet3").Select
Dim integral As Double, delta As Double, start As Double, last As Double
Dim n As Integer
start = 0
n = 20
last = 1
delta = (last - start) / n
integral = 0
For i = 1 To n / 2
integral = integral + (delta / 3) * (fval(start + (2 * i - 2) * delta) + 4 * fval(start + (2 * i - 1) * delta) + fval(start
+ 2 * i * delta))
Next
Cells(1, 1) = integral
End Sub
In Excel, custom name a few cells. aval, bval, cval, inthigh, intlow, nval. This will stand for
a, b, c, integral high value, integral low value, and integer n value. Now, in VB, do the
following:
Dim a, b, c, ihigh, ilow, simp1, fa, fb, simp2, adb, simpson, As Double
Dim n as Integer
' Validate user input
' Inputs
a = Range("aval").Value
b = Range("bval").Value
c = Range("cval").Value
ihigh = Range("inthigh").Value
ilow = Range("intlow").Value
n = Range("nval").Value
If Len(a) = 0 Or Len(b) = 0 Or Len(c) = 0 Or Len(ihigh) = 0 Or Len(ilow) = 0 Or Len(n) = 0
Then
' Alert user that entries have not been filled out
MsgBox "You need to enter input values before continuing.", vbExclamation, "Warning
Message"
Exit Sub
End If
simp1 = (b-a)/6
fa = (a * aval^2) + (b * aval) + c
adb = (a + b)/2
simp2 = 4 * ((a * adb^2) + (b * adb) + c)
fb = (a * bval^2) + (b * bval) + c
' Calculate Simpson value
simpson = simp1*(fa + simp2 + fb)