I am using the quad function from scipy.integrate v0.19.1 to integrate functions with a square-root like singularity at each end of the integration interval such as for example
In [1]: quad(lambda x: 1/sqrt(1-x**2), -1, 1)
Run Code Online (Sandbox Code Playgroud)
(I use the sqrt function from numpy v1.12.0) which immediately yields the correct result pi:
Out[1]: (3.141592653589591, 6.200897573194197e-10)
Run Code Online (Sandbox Code Playgroud)
According to the documentation of the quad function the keyword points should be used to indicate the locations of singularities or discontinuities of the integrand, but …
以下示例在Scipy参考页面上提供integration.
from scipy import integrate
N = 5
def f(t, x):
return np.exp(-x*t) / t**N
integrate.nquad(f, [[1, np.inf],[0, np.inf]])
Run Code Online (Sandbox Code Playgroud)
以下是我从IPython笔记本上得到的错误(在cloud.sagemath.com上):

我的猜测是cloud.sagemath.com没有升级到最新版本,Scipy因此缺少模块nquad.但是,我需要的只是两个变量的集成,因此想要使用dblquad已经在云上可用的变量.
因此,我修改了最后一行以适应dblquad语法,如下所示:

但它仍然会出现错误:TypeError: 'int' object is not callable.我的脚本中有什么错误?我已粘贴下面的整个错误消息:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-2d0c5cf05694> in <module>()
4 def f(t, x):
5 return np.exp(-x*t) / t**N
----> 6 integrate.dblquad(f,1, np.inf,0, np.inf)
/usr/local/sage/sage-6.2.rc0/local/lib/python2.7/site-packages/scipy/integrate/quadpack.pyc in dblquad(func, a, b, gfun, hfun, args, epsabs, epsrel) …Run Code Online (Sandbox Code Playgroud) 我正在尝试对两个“半”正态分布的总和进行积分。scipy.integrate.quad当我尝试在小范围内进行积分时工作正常,但在大范围内进行积分时返回 0。这是代码:
mu1 = 0
mu2 = 0
std1 = 1
std2 = 1
def integral_fun(x):
nor1 = 0.5 * ((1 / (np.sqrt(2 * np.pi) * std1)) * (np.e ** ((-(x-mu1) ** 2) / (2 * std1 **2))))
nor2 = 0.5 * ((1 / (np.sqrt(2 * np.pi) * std2)) * (np.e ** ((-(x-mu2) ** 2) / (2 * std2 **2))))
return nor1 + nor2
integrate.quad(integral_fun, -5, 5)
Out[54]: (0.9999994266968564, 8.668320228277793e-10)
integrate.quad(integral_fun, -10, 10)
Out[55]: (1.0000000000000002, 8.671029607900576e-10)
integrate.quad(integral_fun, -100000, …Run Code Online (Sandbox Code Playgroud) 我有一个矢量
并希望制作另一个长度相同的矢量,其第k个分量是
问题是:我们如何才能将其矢量化以提高速度?NumPy vectorize()实际上是for循环,所以它不计算.
Veedrac指出" 没有办法将纯Python函数应用于NumPy数组的每个元素而不会多次调用它 ".由于我使用的是NumPy函数而不是"纯Python"函数,我认为它可以进行矢量化,但我不知道如何.
import numpy as np
from scipy.integrate import quad
ws = 2 * np.random.random(10) - 1
n = len(ws)
integrals = np.empty(n)
def f(x, w):
if w < 0: return np.abs(x * w)
else: return np.exp(x) * w
def temp(x): return np.array([f(x, w) for w in ws]).sum()
def integrand(x, w): return f(x, w) * np.log(temp(x))
## Python for loop
for k in range(n):
integrals[k] = quad(integrand, -1, 1, args = ws[k])[0]
## NumPy vectorize …Run Code Online (Sandbox Code Playgroud) 我试图通过指定 的参数来更精确地计算积分epsabs,scipy.integrate.quad假设我们正在将函数sin(x) / x^2从 1e-16 积分到 1.0
from scipy.integrate import quad
import numpy
integrand = lambda x: numpy.sin(x) / x ** 2
integral = quad(integrand, 1e-16, 1.0)
Run Code Online (Sandbox Code Playgroud)
这给了我们
(36.760078801255595, 0.01091187908038005)
Run Code Online (Sandbox Code Playgroud)
为了使结果更加精确,我们通过以下方式指定绝对误差容限epsabs
from scipy.integrate import quad
import numpy
integrand = lambda x: numpy.sin(x) / x ** 2
integral = quad(integrand, 1e-16, 1.0, epsabs = 1e-4)
Run Code Online (Sandbox Code Playgroud)
结果一模一样,误差还大到0.0109!我对参数的理解epsabs有误吗?我应该采取什么不同的措施来提高积分的精度?
我正在尝试创建一个将在计算中进一步使用的整数值数组.问题是integrate.quad返回(回答,错误).我不能在其他计算中使用它,因为它不是浮点数; 它是一组两个数字.
我试图对由几个贝塞尔函数(第一类和第二类)组成的实值被积函数进行数值评估。被积函数正在振荡和衰减,需要在 0 和 +? 之间进行评估。到目前为止,我尝试使用 scipy.integrate 子包(quad 和 fixed_quad)都没有成功。评估值在实际上应该是平滑的时候跳来跳去。对于某些参数值集,我还会收到警告:“IntegrationWarning:积分可能发散,或缓慢收敛。” (已知收敛)或“IntegrationWarning:已达到最大细分数 (50)”。
等式来自:http : //dx.doi.org/10.1029/WR003i001p00241
它也可以在这里找到:http : //www.aqtesolv.com/papadopu.htm
感谢您在 Python 中对繁琐函数进行数值积分方面的任何帮助...
代码示例
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy import special as sps
import scipy.integrate as integrate
# define constants and variables (SI mks units):
r_w = 0.15
r_c = 0.16
b = 10
S_s = 1E-6
Q = 0.001
S = S_s*b
K=1E-8
T=K*b
alpha = (r_w**2)*S/r_c**2
def r_D(r): …Run Code Online (Sandbox Code Playgroud) quad ×7
python ×6
scipy ×6
integration ×2
numpy ×2
arrays ×1
integrate ×1
ipython ×1
precision ×1
python-3.x ×1