0

我想在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.

我该怎么办?

4

2 回答 2

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)
于 2012-10-13T17:03:52.723 回答
1

用这个改变 integrl 的返回:

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

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

于 2012-10-13T17:13:02.533 回答