微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我会收到 scipy.integrate._quadrature 的“TypeError: 'module' object is not callable”?

如何解决为什么我会收到 scipy.integrate._quadrature 的“TypeError: 'module' object is not callable”?

所以我在编码方面是个新手。我正在关注我教授的一位读者,但有时读者没有更新。下面的代码首先是为 scipy.integrate.quadrature 实现的,我第一次收到一个错误,说模块不存在,所以我发现它现在被命名为“scipy.integrate._quadrature”。

但是,现在代码对我定义的函数“func(x)”有困难。这是否意味着我必须实现我希望直接集成到 _quadrature 模块中的方程?如下:quad(x*np.sin(x),a,b)。如果是这样,我将如何定义 x?

提前致谢!非常感谢您的帮助。

import math 
import scipy.integrate._quadrature as quad         

def func(x):
    return x*np.sin(x)

def exactIntegral(a,b):
    Iab = -b*np.cos(b) + np.sin(b) + a*np.cos(b) - np.sin(a)
    return Iab

a = 0.0
b = 2.0 

exact = exactIntegral(a,b)
estimate = quad(func,b)                               #TypeError: 'module' object is not callable

print('Exact %1.6f Numerical %1.6f' %(exact,estimate[0]))
print('Error %1.3e' % np.abs(exact-estimate[0]))

解决方法

你可以试试这个:

import scipy.integrate as integrate 
[..] 
estimate = integrate.quadrature(func,a,b) 

_quadrature 不是函数,而是包含您要查找的函数的文件/模块。 scipy.integrate 模块包含您要查找的内容。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。