可以scipy.integrate.fixed_quad用函数边界计算积分?

2024-04-20 01:56:55 发布

您现在位置:Python中文网/ 问答频道 /正文

类似地,我想把一个函数数值积分

import scipy.integrate as integrate
inside = lambda x: integrate.quad(lambda x,y: 1, 0, x, args=(x))[0]
outside = integrate.quad(inside, 0, 1)[0]
print(outside)
0.5

但是使用scipy.integrate.fixed_quad函数(它以积分顺序n作为参数)。但是,当我写的时候

^{pr2}$

Traceback (most recent call last): File "", line 1, in File "", line 1, in File "/Users/username/anaconda/lib/python3.5/site-packages/scipy/integrate/ quadrature.py", line 82, in fixed_quad return (b-a)/2.0 * np.sum(w*func(y, *args), axis=0), None

TypeError: () argument after * must be an iterable, not int

我不知道我做错了什么,因为我在跟踪scipy.integrate.fixed_quad上的文档。在


Tags: lambda函数inimportaslineargsscipy
1条回答
网友
1楼 · 发布于 2024-04-20 01:56:55

问题是你对argsargs=(x)的定义。它应该作为元组传递,因此需要添加一个额外的逗号使其成为元组:

inside = lambda x: integrate.fixed_quad(lambda x,y: 1, 0, x, args=(x,), n=5)

那么

^{pr2}$

收益率

(5.0, None)

线

integrate.quad(lambda x,y: 1, 0, x, args=(x))[0]

工作方式与quad一样,检查args是否为元组;如果不是,则转换它(直接从源代码获取):

if not isinstance(args, tuple):
        args = (args,)

fixed_quad中,情况并非如此,这就是为什么在一种情况下(而不是在两种情况下)都收到了错误。在

相关问题 更多 >