我正在尝试使用 for 循环在 python 中编写辛普森规则,但我不断收到断言错误并且无法找出原因。
def integrate_numeric(xmin, xmax, N):
xsum = 0
msum = 0
h = (xmax-xmin)//N
for i in range(0, N):
xsum += f(xmin + i*h)
print (xsum)
for i in range(0,N-1):
msum += f(xmin + (h/2) + i*h)
print (msum)
I = (h/6) * (f(xmin) + 4*(msum) + 2*(xsum) + f(xmax))
return I
F:
def f(x):
return (x**2) * numpy.sin(x)
样本:
assert numpy.isclose(integrate_numeric(xmin=0, xmax=4, N=50), 1.096591)