问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/42270420/can-scipy-integrate-fixed-quad-compute-integral-with-functional-boundaries