提问人:فراز 提问时间:10/24/2023 最后编辑:jaredفراز 更新时间:10/25/2023 访问量:88
MATLAB 和 SciPy 的差异 贝塞尔函数的数值积分结果
Differences Between MATLAB and SciPy numerical Integration Results with Bessel Functions
问:
我希望在 SciPy 中复制以下 MATLAB 代码的结果。
MATLAB版本:
f = @(x, y) besselh(0, 2, x.^2 + y.^2);
integral2(f, -0.1, 0.1, -0.1, 0.1)
MATLAB结果为:
ans =
0.0400 + 0.1390i
SciPy 版本:
f = lambda x, y: scipy.special.hankel2(0, x**2 + y**2)
scipy.integrate.dblquad(f, -0.1, 0.1, lambda x: -0.1, lambda x: 0.1)
但是,当我运行 SciPy 代码时,我收到以下警告:
IntegrationWarning: The occurrence of roundoff error is detected ...
结果与MATLAB输出不同:
(nan, 2.22e-15)
有人可以解释为什么我会收到这个警告,以及如何获得类似于 MATLAB 的结果吗?
答:
3赞
jared
10/25/2023
#1
我认为问题在于积分通过原点,从而产生 H0 的 NaN。这是因为 H0 = J0 - i*Y0,而 Y0 的渐近线为 y 轴。
对于这种特定情况,您可以使用上面的 H0 定义将积分拆分为实部和复部。执行此操作时,您可以确保积分的复数部分不会越过 0。
import scipy
j0 = lambda x, y: scipy.special.jv(0, x**2 + y**2)
y0 = lambda x, y: scipy.special.yv(0, x**2 + y**2)
r1 = scipy.integrate.dblquad(j0, -0.1, 0.1, -0.1, 0.1)
r21 = scipy.integrate.dblquad(y0, 1e-10, 0.1, -0.1, 0.1)
r22 = scipy.integrate.dblquad(y0, -0.1, -1e-10, -0.1, 0.1)
res = r1[0] - 1j*(r21[0]+r22[0]) # 0.039999377783047595+0.13896313686573833j
评论
0赞
فراز
10/25/2023
感谢您的回复!这种奇点提取方法是否普遍适用,或者是否有需要考虑的特定条件?
0赞
jared
10/25/2023
你有一个不连续的函数。你必须从数学上确定它是否可积。在这种情况下,您有一个不连续性,并且可以拆分该不连续性的积分。从数学上讲,你必须确定积分是否收敛,因为你取了不连续性的极限。这是一个数学问题,我会让其他知道更多的人来回答。
评论