【发布时间】:2020-12-25 13:51:30
【问题描述】:
我在 (0,infinite) 限制内积分时遇到困难。我正在尝试做这个 Jupyter 笔记本(不知道这是否是相关信息)。代码如下:
import numpy as np
import matplotlib.pyplot as plt
import scipy as sc
import math
from scipy.integrate import quad
def integrand3(x,z,m,n):
return ((np.cosh(x))**m)*((np.sinh(x))**n)*(np.exp(-z*np.cosh(x)))
def IgK0(z,m,n):
IntK1 = quad(integrand3, 0, 1, args=(z,m,n))
return IntK1
IgK0(1,1,1)
这给出了结果:
(0.19224739693489237, 2.1343748650136926e-15)
没关系。但是当我用无穷大替换上限时,我得到'nan'作为输出。见以下代码:
def IgKI(z,m,n):
IntKI = quad(integrand3, 0, np.inf, args=(z,m,n))
return IntKI
当我对(m,n)使用(0,0)值时,虽然有一些我不明白的错误,但至少我得到了一些答案。
IgKI(1,0,0)
<ipython-input-53-9b9d0956fa85>:2: RuntimeWarning: overflow encountered in cosh
return ((np.cosh(x))**m)*((np.sinh(x))**n)*(np.exp(-z*np.cosh(x)))
<ipython-input-53-9b9d0956fa85>:2: RuntimeWarning: overflow encountered in sinh
return ((np.cosh(x))**m)*((np.sinh(x))**n)*(np.exp(-z*np.cosh(x)))
(0.42102443824067737, 1.3628527263018718e-08)
但是当我对 (m,n) 使用任何其他值时,我会得到以下结果:
IgKI(1,0,1)
<ipython-input-53-9b9d0956fa85>:2: RuntimeWarning: overflow encountered in cosh
return ((np.cosh(x))**m)*((np.sinh(x))**n)*(np.exp(-z*np.cosh(x)))
<ipython-input-53-9b9d0956fa85>:2: RuntimeWarning: overflow encountered in sinh
return ((np.cosh(x))**m)*((np.sinh(x))**n)*(np.exp(-z*np.cosh(x)))
<ipython-input-53-9b9d0956fa85>:2: RuntimeWarning: invalid value encountered in double_scalars
return ((np.cosh(x))**m)*((np.sinh(x))**n)*(np.exp(-z*np.cosh(x)))
<ipython-input-76-9bee7a5d6456>:2: IntegrationWarning: The occurrence of roundoff error is detected, which prevents
the requested tolerance from being achieved. The error may be
underestimated.
IntKI = quad(integrand3, 0, np.inf, args=(z,m,n))
(nan, nan)
那么,我做错了什么?
【问题讨论】:
-
您为什么希望数值积分方法能够在无限极限下工作?这对我来说没有意义。即使存在解析解,在数值上你能做的最好的事情就是尝试几个大数,然后投影一个收敛。
标签: python numpy scipy integration infinity