我想用sin计算python中sin(x)/x的正定积分。N= 256。它似乎不太好用:
from scipy import integrate
exact = integrate.quad(lambda x : (np.sin(x))/x, 0, 2*np.pi)[0]
print("Exact value of integral:", exact)
# Approx of sin(x)/x by Trapezoidal rule
x = np.linspace(0, 2*np.pi, 257)
f = lambda x : (np.sin(x))/x
approximation = np.trapz(f(x), x)
print ("Estimated value of trapezoidal O(h^2):", round(approximation, 5),
'+', round((2/256)**2, 5))
print ("real error:", exact - approximation)
# Approx of sin(x)/x by Simpsons 1/3 rule
approximation = integrate.simps(f(x), x)
print("Estimated value of simpsons O(h^4):", round(approximation, 9),
'+', round((2/256)**4, 9))
print ("real error:", exact - approximation)
plt.figure()
plt.plot(x, f(x))
plt.show()精确的积分计算得很好,但是我得到了一个四边形误差.怎么啦?
Exact value of integral: 1.4181515761326284
Estimated value of trapezoidal O(h^2): nan + 6e-05
real error: nan
Estimated value of simpsons O(h^4): nan + 4e-09
real error: nan提前感谢!
发布于 2018-06-10 15:36:18
在您的代码中至少存在一些问题:
0开始,因此,当您计算要集成的函数时,在梯形积分的开头,有:sin(0)/0 = nan。您应该使用数字零而不是确切的零(在下面的示例中,我使用了1e-12)。nan时,nan + 1.0 = nan:这意味着在您的代码中,当您对间隔上的积分进行求和时,第一个nan会扰乱您的所有结果。2/256是两个整数之间的除法,结果是0。尝试使用2.0/256.0 (谢谢@MaxU指出这一点)。这是您修改的代码(我在python2中运行它,这是我现在使用的pc中安装的):
from scipy import integrate
import numpy as np
exact = integrate.quad(lambda x : (np.sin(x))/x, 0, 2*np.pi)[0]
print("Exact value of integral:", exact)
# Approx of sin(x)/x by Trapezoidal rule
x = np.linspace(1e-12, 2*np.pi, 257) # <- 0 has become a numeric 0
f = lambda x : (np.sin(x))/x
approximation = np.trapz(f(x), x)
print ("Estimated value of trapezoidal O(h^2):", round(approximation, 5),
'+', round((2.0/256.0)**2, 5))
print ("real error:", exact - approximation)
# Approx of sin(x)/x by Simpsons 1/3 rule
approximation = integrate.simps(f(x), x)
print("Estimated value of simpsons O(h^4):", round(approximation, 9),
'+', round((2/256)**4, 9))
print ("real error:", exact - approximation)它的产出:
('Exact value of integral:', 1.4181515761326284)
('Estimated value of trapezoidal O(h^2):', 1.41816, '+', 6e-05)
('real error:', -7.9895502944626884e-06)
('Estimated value of simpsons O(h^4):', 1.418151576, '+', 0.0)
('real error:', 2.7310242955991271e-10)Discalimer sin(x)/x -> 1 for x -> 0的极限,但由于sin(1e-12)/1e-13 = 1的浮动四舍五入!
发布于 2018-06-10 15:57:12
您可以使函数返回1( sin(x)/x在0中的限制),而不是x == 0的NaN。这样,您就不必为了排除0而欺骗和更改集成的时间间隔。
import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate
exact = integrate.quad(lambda x : (np.sin(x))/x, 0, 2*np.pi)[0]
print("Exact value of integral:", exact)
def f(x):
out = np.sin(x) / x
# For x == 0, we get nan. We replace it by the
# limit of sin(x)/x in 0
out[np.isnan(out)] = 1
return out
# Approx of sin(x)/x by Trapezoidal rule
x = np.linspace(0, 2*np.pi, 257)
approximation = np.trapz(f(x), x)
print ("Estimated value of trapezoidal O(h^2):", round(approximation, 5),
'+', round((2/256)**2, 5))
print ("real error:", exact - approximation)
# Approx of sin(x)/x by Simpsons 1/3 rule
approximation = integrate.simps(f(x), x)
print("Estimated value of simpsons O(h^4):", round(approximation, 9),
'+', round((2/256)**4, 9))
print ("real error:", exact - approximation)
plt.figure()
plt.plot(x, f(x))
plt.show()输出:
Exact value of integral: 1.4181515761326284
Estimated value of trapezoidal O(h^2): 1.41816 + 6e-05
real error: -7.98955129322e-06
Estimated value of simpsons O(h^4): 1.418151576 + 4e-09
real error: 2.72103006793e-10发布于 2018-06-10 15:40:13
NaN的意思是“不是一个数字”。在你的例子中,基本上是无限的。当您创建:
x = np.linspace(0, 2*np.pi, 257)创建一个值为0的数组,然后尝试将其除以x,而不能除以0.
一种解决方案是使用以下方法:
x = np.linspace(0.1, 2*np.pi, 257)这给了你这个
Exact value of integral: 1.4181515761326284
Estimated value of trapezoidal O(h^2): 1.31822 + 6e-05
real error: 0.099935104987
Estimated value of simpsons O(h^4): 1.318207115 + 4e-09
real error: 0.0999444614012你越接近于零,近似值就越好!
https://stackoverflow.com/questions/50785188
复制相似问题