“int”在python cod中不可调用

2024-10-01 15:43:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我被要求用中心差分法写一个程序

"Central difference formula"

def difc(f,x,h):
    h = float(h)
    return (f(x + h) - f(x - h))/2*h

"Entraphy function"
f = 259.8*(3.782*x - 2.997*(10**-3)*(x**2)/2 + 9.847*(10**-6)*(x**3)/3 - 9.681*(10**-9)*(x**4)/4 + 3.243*(10**-12)*(x**5)/5 - 1.064*10**3)
"temperature"
x = 500

print (difc(f,500,5))

当我运行代码时,我得到了错误

line 9, in derivative 
    return (f(x+h) - f(x))/h
TypeError: 'int' object is not callable"

我哪里出错了?你知道吗


Tags: 代码程序returndeffunctionfloat中心central
1条回答
网友
1楼 · 发布于 2024-10-01 15:43:47

似乎f不是一个函数,而是一个值(即使此时x没有定义…)。也许您想定义一个实际的函数,并且使用您的格式,lambda是最佳的候选:

f = lambda x: 259.8*(3.782*x - 2.997*(10**-3)*(x**2)/2 + 9.847*(10**-6)*(x**3)/3 - 9.681*(10**-9)*(x**4)/4 + 3.243*(10**-12)*(x**5)/5 - 1.064*10**3)

相关问题 更多 >

    热门问题