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?
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?