simpspns rule in excel vba

fionbaire

New Member
Joined
Dec 11, 2012
Messages
3
hi there, very new to excel vba so strugglinh a bit. I am getting an error 13 type mismatch when running the code below:

Function SimpsonInt(n As Integer, dLower As Double, dUpper As Double, sEq As String) As Double
'This function calculates the area under the curve y(x) defined by sEq from
'x=dLower to x=dUpper using Simpson's rule with n intervals
'Note: the independent variable in sEq must be "_x_"
'
'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"
Exit Function
End If
n = n * 2
h = (dUpper - dLower) / n
x = dLower
sum = 0#
For i = 1 To n Step 2
term = y(sEq, x) + 4 * y(sEq, x + h) + y(sEq, x + 2 * h)
sum = sum + term
x = x + 2 * h
Next i
SimpsonInt = sum * h / 3
End Function
Function y(sEq As String, x As Double) As Double
y = Evaluate(Replace(sEq, "_x_", x))
End Function

all inputs are coming froa a userform through textboxes and basically i am trying to replace the letter x in the entered string e.g x^3 changes to perhaps 3^3 then it gets evaluated etc and you end up with an integral value but im losing the faith, any ideas please?
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Shouldn't it be?

Code:
Call SimpsonInt(CInt(TextBox1.Value), CDbl(TextBox2.Value), CDbl(TextBox3.Value), TextBox4.Value)
 
Upvote 0
hi,

thanks a lot for your integral function, but I've got an issue with "x^2":
the result for this expression is zero : SimpsonInt(20, 1, 9, x ^ 2 + 3 * x) = 0 (i've also tried to write SimpsonInt(20, 1, 9, x * x + 3 * x) - but also = 0) , but should be 362.666
the problem is when "x" is written twice or more time in the same expression (x^3+2*x^2+3*x)...

please....can somebody help with this?

thanks in advance!!
 
Upvote 0
The function expects a string.

Try SimpsonInt(20, 1, 9, "x ^ 2 + 3 * x")
 
Upvote 0
The function signature requires the equation to be passed as a string:

Code:
Function SimpsonInt(n As Integer, dLower As Double, dUpper As Double, [COLOR="#FF0000"]sEq As String[/COLOR]) As Double

However, the substitution requires the variable to be "_x_", not "x"
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,393
Members
449,446
Latest member
CodeCybear

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