scipy.integrate.fixed_quad 可以用函数边界计算积分吗? [英] Can scipy.integrate.fixed_quad compute integral with functional boundaries?

查看:60
本文介绍了scipy.integrate.fixed_quad 可以用函数边界计算积分吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对三角形上的函数进行数值积分,类似于

import scipy.integrate 作为集成内部 = lambda x:integrate.quad(lambda x,y: 1, 0, x, args=(x))[0]外部 = 集成四边形(内部,0, 1)[0]打印(外)0.5

但使用 scipy.integrate.fixed_quad 函数(它具有积分顺序 n 作为参数).然而,当我写

inside = lambda x:integred.fixed_quad(lambda x,y: 1, 0, x, args=(x), n=5)打印(里面(5))

<块引用>

回溯(最近一次调用最后一次):文件",第 1 行,在文件",第 1 行,在文件中/用户/用户名/anaconda/lib/python3.5/site-packages/scipy/integrate/quadrature.py",第 82 行,在 fixed_quad 中return (b-a)/2.0 * np.sum(w*func(y, *args), axis=0), None

TypeError: * 后的 () 参数必须是可迭代的,而不是 int

我不知道自己做错了什么,因为我正在关注 scipy.integrate.fixed_quad.

解决方案

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

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

然后

里面(5)

收益

(5.0, 无)

线

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

quad 一样工作,检查 args 是否是元组;如果不是,则进行转换(直接取自源代码):

如果不是 isinstance(args, tuple):args = (args,)

fixed_quad 中,情况并非如此,这就是您在一种而不是两种情况下都收到错误的原因.

I would like to numerically integrate function over a triangle similarly as

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

but using scipy.integrate.fixed_quad function (which has integration order n as parameter). However, when I write

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

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

I don't know what I'm doing wrong as I'm following the documentation on scipy.integrate.fixed_quad.

解决方案

The problem is your definition of args, args=(x). It should be passed as a tuple, so you need to add an additional comma to make it a tuple:

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

Then

inside(5)

yields

(5.0, None)

The line

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

works as in quad it is checked whether args is a tuple; if not it is converted (directly taken from the source code):

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

In fixed_quad that is not the case and that's why you received the error in one but not both cases.

这篇关于scipy.integrate.fixed_quad 可以用函数边界计算积分吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆