Python sin(x)/x的数值积分
我想用scipy在python中计算sin(x)/x的平方的定积分。n=256。它似乎不太管用:Python sin(x)/x的数值积分,python,python-3.x,numpy,scipy,integral,Python,Python 3.x,Numpy,Scipy,Integral,我想用scipy在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
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
提前谢谢 您的代码中至少存在一些问题:
0
开始,因此当您计算要积分的函数时,在梯形积分的开头,您有:sin(0)/0=nan
。您应该使用数字零而不是精确零(在下面的示例中,我使用了1e-12
)nan
,nan+1.0=nan
:这意味着在你的代码中,当你对区间内的积分求和时,第一个nan
会弄乱你的所有结果2/256
是两个整数之间的除法,结果是0
。请改用2.0/256.0
(感谢@MaxU指出这一点)Discalimerx->0的
sin(x)/x->1的限制,但是由于sin(1e-12)/1e-13=1的浮动舍入 您的代码中至少存在一些问题:
您的linspace从0
开始,因此当您计算要积分的函数时,在梯形积分的开头,您有:sin(0)/0=nan
。您应该使用数字零而不是精确零(在下面的示例中,我使用了1e-12
)
当你得到第一个nan
,nan+1.0=nan
:这意味着在你的代码中,当你对区间内的积分求和时,第一个nan
会弄乱你的所有结果
仅适用于python 2:除法2/256
是两个整数之间的除法,结果是0
。请改用2.0/256.0
(感谢@MaxU指出这一点)
这是您的代码修复(我在python2中运行它,这是我现在使用的pc中安装的):
Discalimerx->0的sin(x)/x->1的限制,但是由于sin(1e-12)/1e-13=1的浮动舍入 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
离零越近,近似值就越好 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
离零越近,近似值就越好 对于x==0,可以使函数返回1(sin(x)/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
对于x==0,可以使函数返回1(sin(x)/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
我想最好是0.0000000000001!哈哈,没错,但更好的是Matteo刚刚在我面前,1e-12,这是0之前可能的最小数字!我想最好是0.0000000000001!哈哈,没错,但更好的是Matteo刚刚在我面前,1e-12,这是0之前可能的最小数字!