我在(0,无限)限制内积分时遇到困难。我正在尝试做这个 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
当我使用 (0,0) 值作为 (m,n) 时,尽管存在一些我不明白的错误,但至少我得到了一些答案。
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)
那么,我做错了什么?
繁星淼淼
相关分类