Python中的函数数学积分

3
我正在尝试集成这个函数:

enter image description here

但是我遇到了一个错误:
Traceback (most recent call last):

  File "<ipython console>", line 1, in <module>

  File "siestats.py", line 349, in NormalDistro

    P_inner = scipy.integrate(NDfx,-dev,dev)

TypeError: 'module' object is not callable

我的代码运行如下:

# Definition of the mathematical function:
def NDfx(x):

    return((1/math.sqrt((2*math.pi)))*(math.e**((-.5)*(x**2))))

# This Function normailizes x, u, and o2 (position of interest, mean and st dev) 
# and then calculates the probability up to position 'x'

def NormalDistro(u,o2,x):


    dev = abs((x-u)/o2)


    P_inner = scipy.integrate(NDfx,-dev,dev)

    P_outer = 1 - P_inner

    P = P_inner + P_outer/2

    return(P)

NormalDistro函数是用来导入和使用的,格式如下:

foo.NormalDistro(30,2.5,1.25)

作为一个例子。

3
请告诉我们它正试图调用哪个模块对象?这应该在错误信息中。通常发生在您执行“import foo”后紧接着执行“foo()”而不是“foo.bar()”。 - Spacedman
1
请发布完整的回溯信息。仅知道错误而不知道导致错误的原因是没有帮助的。 - John
既然您正在使用scipy,那么您在NDfx中使用pythonmath函数的理由是什么? - eat
@eat 你是什么意思?我不明白。 - James Mertz
3个回答

7
您试图调用的模块是scipy.integrate,您需要调用该模块内的一个函数。根据之前在聊天室中的评论,您可能想要使用scipy.integrate.quad()
此外,它返回一个元组(Result,MaximumError),因此在P的总和中不能直接使用P_inner,您需要使用P = P_inner[0] + P_outer/2

2

return 不需要使用 (),只需用 return P 就可以正常工作。

然而,这并不能解决您看到的问题,因为 return(foo) 也应该有效。

更多关于错误的信息将会有所帮助。


1

高斯函数的积分被定义为errorfunction。它可以从Python的math库或scipy.special中获得(如果您需要矢量化函数)。它被称为erf。还有erfc,互补误差函数也可用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接