首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sin(x)/x的数值积分

sin(x)/x的数值积分
EN

Stack Overflow用户
提问于 2018-06-10 15:24:58
回答 4查看 3.2K关注 0票数 1

我想用sin计算python中sin(x)/x的正定积分。N= 256。它似乎不太好用:

代码语言:javascript
复制
 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()

精确的积分计算得很好,但是我得到了一个四边形误差.怎么啦?

代码语言:javascript
复制
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

提前感谢!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-06-10 15:36:18

在您的代码中至少存在一些问题:

  1. 您的linspace0开始,因此,当您计算要集成的函数时,在梯形积分的开头,有:sin(0)/0 = nan。您应该使用数字零而不是确切的零(在下面的示例中,我使用了1e-12)。
  2. 当您得到第一个nan时,nan + 1.0 = nan:这意味着在您的代码中,当您对间隔上的积分进行求和时,第一个nan会扰乱您的所有结果。
  3. 用于python2,只有:除法2/256是两个整数之间的除法,结果是0。尝试使用2.0/256.0 (谢谢@MaxU指出这一点)。

这是您修改的代码(我在python2中运行它,这是我现在使用的pc中安装的):

代码语言:javascript
复制
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)

它的产出:

代码语言:javascript
复制
('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的浮动四舍五入!

票数 3
EN

Stack Overflow用户

发布于 2018-06-10 15:57:12

您可以使函数返回1( sin(x)/x在0中的限制),而不是x == 0的NaN。这样,您就不必为了排除0而欺骗和更改集成的时间间隔。

代码语言:javascript
复制
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()

输出:

代码语言:javascript
复制
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
票数 3
EN

Stack Overflow用户

发布于 2018-06-10 15:40:13

NaN的意思是“不是一个数字”。在你的例子中,基本上是无限的。当您创建:

代码语言:javascript
复制
x = np.linspace(0, 2*np.pi, 257)

创建一个值为0的数组,然后尝试将其除以x,而不能除以0.

一种解决方案是使用以下方法:

代码语言:javascript
复制
x = np.linspace(0.1, 2*np.pi, 257)

这给了你这个

代码语言:javascript
复制
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

你越接近于零,近似值就越好!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50785188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档