主题:求助,辛卜生积分
Function nisimpson(a As Double, b As Double, eps As Double) As Double
Dim n As Long, k As Long
Dim h As Double, t1 As Double, t2 As Double, s1 As Double, s2 As Double
Dim ep As Double, p As Double, x As Double
n = 1
h = b - a
t1 = h * (func(a) + func(b)) / 2
s1 = t1
ep = eps + 1
While (ep >= eps)
p = 0
For k = 0 To n - 1
x = a + (k + 0.5) * h
p = p + func(x)
Next k
t2 = (t1 + h * p) / 2
s2 = (4 * t2 - t1) / 3
ep = Abs(s2 - s1)
t1 = t2
s1 = s2
n = n + n
h = h / 2
Wend
nisimpson = s2
End Function
Sub form_load()
Dim a As Double, b As Double, eps As Double, v As Double
Dim PI As Double
PI = 3.1415926
a = 0
b = PI / 2
eps = 0.00001
v = nisimpson(a, b, eps)
MsgBox "积分值=" & v
End Sub
Function func(x As Double) As Double
Dim i As Integer, j As Integer, q As Integer
Dim u As Double, d As Double
Dim PI As Double
PI = 3.1415926
i = 1
j = 3
u = 0.2
q = 5
d = 1.4
[color=FF0000]func = 4 * (1 - u) * Log((i - j - 0.5) * q + (d * d * (Cos(x)) ^ 2 + (i - j - 0.5) ^ 2 * q * q) ^ 0.5)[/color](当定义这个函数时,若i比j大,运行正常,但是当i比j小时,就抱错)
End Function
请高手指点,多谢!
Dim n As Long, k As Long
Dim h As Double, t1 As Double, t2 As Double, s1 As Double, s2 As Double
Dim ep As Double, p As Double, x As Double
n = 1
h = b - a
t1 = h * (func(a) + func(b)) / 2
s1 = t1
ep = eps + 1
While (ep >= eps)
p = 0
For k = 0 To n - 1
x = a + (k + 0.5) * h
p = p + func(x)
Next k
t2 = (t1 + h * p) / 2
s2 = (4 * t2 - t1) / 3
ep = Abs(s2 - s1)
t1 = t2
s1 = s2
n = n + n
h = h / 2
Wend
nisimpson = s2
End Function
Sub form_load()
Dim a As Double, b As Double, eps As Double, v As Double
Dim PI As Double
PI = 3.1415926
a = 0
b = PI / 2
eps = 0.00001
v = nisimpson(a, b, eps)
MsgBox "积分值=" & v
End Sub
Function func(x As Double) As Double
Dim i As Integer, j As Integer, q As Integer
Dim u As Double, d As Double
Dim PI As Double
PI = 3.1415926
i = 1
j = 3
u = 0.2
q = 5
d = 1.4
[color=FF0000]func = 4 * (1 - u) * Log((i - j - 0.5) * q + (d * d * (Cos(x)) ^ 2 + (i - j - 0.5) ^ 2 * q * q) ^ 0.5)[/color](当定义这个函数时,若i比j大,运行正常,但是当i比j小时,就抱错)
End Function
请高手指点,多谢!