无法与 dblquad 集成
Can't integrate with dblquad
所以我想集成一个带有常量的二重积分,比如 a、b 等,用户可以在其中分配这些常量的值:
积分的极限是x[0,1]和y[-1,2]
import numpy as np
import scipy.integrate as integrate
def g(y,x,a):
return a*x*y
a = int(input('Insert a value --> '))
result = integrate.dblquad(g, 0, 1, lambda x: -1, lambda x: 2, args=(a))[0]
print(result)
但是我收到这个错误,我不明白为什么:
TypeError: integrate() argument after * must be an iterable, not int
我不明白。因为当我做同样的事情但是使用 quad() Python 时,它是正确的:
import numpy as np
import scipy.integrate as integrate
def g(x,a):
return a*x
a = int(input('Insert a value --> '))
result = integrate.quad(g, 0, 1, args=(a))[0]
print(result)
结果:
0.5
这里的问题是您在可选参数 args 中提供的值是一个元组。在quad, it is what the function expects, but for dblquad, a sequence is required. Even though tuples are sequences (immutable ones), it seems that scipy makes a difference here and therefore it is why an error is raised. However it is misleading as a tuple is definitely an iterable的情况下。无论如何,这应该有效:
result = integrate.dblquad(g, 0, 1, lambda x: -1, lambda x: 2, args=[a])[0]
所以我想集成一个带有常量的二重积分,比如 a、b 等,用户可以在其中分配这些常量的值:
积分的极限是x[0,1]和y[-1,2]
import numpy as np
import scipy.integrate as integrate
def g(y,x,a):
return a*x*y
a = int(input('Insert a value --> '))
result = integrate.dblquad(g, 0, 1, lambda x: -1, lambda x: 2, args=(a))[0]
print(result)
但是我收到这个错误,我不明白为什么:
TypeError: integrate() argument after * must be an iterable, not int
我不明白。因为当我做同样的事情但是使用 quad() Python 时,它是正确的:
import numpy as np
import scipy.integrate as integrate
def g(x,a):
return a*x
a = int(input('Insert a value --> '))
result = integrate.quad(g, 0, 1, args=(a))[0]
print(result)
结果:
0.5
这里的问题是您在可选参数 args 中提供的值是一个元组。在quad, it is what the function expects, but for dblquad, a sequence is required. Even though tuples are sequences (immutable ones), it seems that scipy makes a difference here and therefore it is why an error is raised. However it is misleading as a tuple is definitely an iterable的情况下。无论如何,这应该有效:
result = integrate.dblquad(g, 0, 1, lambda x: -1, lambda x: 2, args=[a])[0]