python中的双积分

时间:2012-10-13 16:56:01

标签: python scipy

我想在python中做一个双积分。在http://docs.scipy.org/doc/scipy/reference/tutorial/integrate.html的帮助下,我做了以下

import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate


def func(x,a):
    return a*x*x

def integrl(a):
    return integrate.quad(func,0,10,args=(a))

print integrate.quad(lambda x: integrl(x),0,1)

但是我收到以下错误

Traceback (most recent call last):
File "test.py", line 12, in <module>
print integrate.quad(lambda x: integrl(x),0,1)
File "/usr/lib/python2.7/dist-packages/scipy/integrate/quadpack.py", line 245, in quad
retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points)
File "/usr/lib/python2.7/dist-packages/scipy/integrate/quadpack.py", line 309, in _quad
return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
quadpack.error: Supplied function does not return a valid float.

我该怎么办?

2 个答案:

答案 0 :(得分:1)

错误在于integrl功能。在那里我应该指定要集成的参数。应该读正确的代码

import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate


def func(x,a):
   return a*x*x

def integrl(a):
   return integrate.quad(func,0,10,args=(a))[0]

print integrate.quad(lambda x: integrl(x),0,1)

答案 1 :(得分:1)

通过这个改变积分的回报:

return integrate.quad(func,0,10,args=(a))[0]

integ.quad函数默认返回一个包含积分值和绝对误差估计的元组:scipy doc