与 scipy 整合
Integrate with scipy
我想从0~1积分x**(-2/3)*(x-1)**(1/3)
,之前换变量用辛普森法则求解,现在想尝试一些更快捷的方法
我的代码:
x1 = lambda x: x**(-2/3)*(x-1)**(1/3)
integrate.quad(x1,0,1)
错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/scipy/integrate/quadpack.py", line 281, in quad
retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points)
File "/usr/lib/python3/dist-packages/scipy/integrate/quadpack.py", line 345, in _quad
return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
quadpack.error: Supplied function does not return a valid float.
我该如何解决?
即使标题说与 numpy 集成,我想你的意思是 scipy..你的问题是你的函数中有复数。例如,(x-1)**(1/3)
部分对于 [0, 1) 中的 x 变得复数,但 scipy.integrate.quad
不处理复数。参见例如Use scipy.integrate.quad to integrate complex numbers 获取更多信息。
我想从0~1积分x**(-2/3)*(x-1)**(1/3)
,之前换变量用辛普森法则求解,现在想尝试一些更快捷的方法
我的代码:
x1 = lambda x: x**(-2/3)*(x-1)**(1/3)
integrate.quad(x1,0,1)
错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/scipy/integrate/quadpack.py", line 281, in quad
retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points)
File "/usr/lib/python3/dist-packages/scipy/integrate/quadpack.py", line 345, in _quad
return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
quadpack.error: Supplied function does not return a valid float.
我该如何解决?
即使标题说与 numpy 集成,我想你的意思是 scipy..你的问题是你的函数中有复数。例如,(x-1)**(1/3)
部分对于 [0, 1) 中的 x 变得复数,但 scipy.integrate.quad
不处理复数。参见例如Use scipy.integrate.quad to integrate complex numbers 获取更多信息。