我尝试将cos(x)从0积分到2pi,但没有找到0。你知道吗
我的代码使用这个数值积分:
def rectangles(f,a,b,n) :
h=(b-a)/n/1.0
print h
z=0.0
for i in range(n) :
z=z+f(a+i*h)
return h*z
功能是:
def f1(x):
return cos(x)
我使用mpmath biblio:
from mpmath import *
mp.dps=300; mp.pretty=True
电话是:
rectangles(f1,0.0,2.0*pi,100000)
从0到π/2是可以的,从π/2到π,等等,但是如果我想从0到2pi,我会找到:-4.81。你知道吗
这很奇怪,因为如果我写1.9999*π或2.00001*π就行了!你知道吗
不,你得到了
而-4.813e-301=-4.813*10^(-301)非常非常接近于零。你知道吗
[旁白:如果
b
和a
和n
都是整数,那么h=(b-a)/n/1.0
中的/1.0
没有多大帮助,因为除法已经失去了精度。]相关问题 更多 >
编程相关推荐