Passing a function defined in a cell to a VBA function

ssheikh

New Member
Joined
Apr 30, 2012
Messages
4
I have a numeric integration function using Simpson rule as follows:

Code:
Function SimpsonInt(a As Double, b As Double, n As Integer) As Double
'This function calculates the area under the curve y(x) from
'x=a to x=b using Simpson's rule with n intervals
'
'Local variables
Dim h As Double, sum As Double, term As Double
Dim x As Double
Dim i As Integer

'Do error checking
If n = 0  Then
  Simpson = 0#
  MsgBox "Sorry # of intervals has to be > 0 and even"
  Exit Function
End If
n = 2 * n
h = (b - a) / n
x = a
sum = 0#
 
For i = 1 To n Step 2
  term = y(x) + 4 * y(x + h) + y(x + 2 * h)
  sum = sum + term
  x = x + 2 * h
  Next i
 
SimpsonInt = sum * h / 3
 
End Function

Function y(x As Double) As Double
 
' a_, b_, and Vmax are named cells in spreadsheet
y = x ^ (a_ - 1) * (Vmax - x) ^ (b_ - 1)
 
End Function

How can I pass the function y(x) to the SimpsonInt function. I have several different equations that I want to be able to take the integral of. I am trying to find a way of passing that equation that defines y(x) to SimpsonInt so I dont have to define each equation as a function in VBA.

Thanks.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Forum statistics

Threads
1,215,097
Messages
6,123,079
Members
449,094
Latest member
mystic19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top