-1
我必须在python中为函数e^((-x)^2)
写出梯形和辛普森规则。Python中的梯形和辛普森规则?
这是我到目前为止。它给出的答案是8218.7167913
,但根据我的老师的答案是像1.77251356....
。
我哪里错了?
from numpy import *
def f(x):
return e**((-x)**2)
def trapezoid(f, a, b, n):
deltax = float(b - a)/(n)
h = float(b - a)/n
s = 0.0
s += f(a)/2.0
for i in range(1, n):
s += f(a + i*h)
s += f(b)/2.0
return s * h
print trapezoid(f, -3, 3, 6)
你会得到什么错误?如果你的代码没有问题,但你得到的是错误的答案,请尝试打印出一些中间值来查看错误在你的逻辑中的位置。 – 2014-10-22 04:26:05