【问题标题】:error in my simpsons rule integration我的辛普森一家规则集成中的错误
【发布时间】:2018-05-19 15:11:13
【问题描述】:

我编写了以下代码来使用辛普森规则来集成任意函数,在我的例子中是 sin(x):

import math


delta_x=(x2-x1)/N

def simpson(x1,x2,f,N):
    sum=0
    i=1
    for i in range(1,N+1):

        sum+=f(x1+i*delta_x)

    sum1=(3*delta_x)*sum

    return(sum1)

print(simpson(0,math.pi,math.sin(x),100))

但是,我在 sum+=f(x1+i*delta_x) 行上收到错误“float object not callable”。有谁知道有什么问题吗?

谢谢:)

【问题讨论】:

  • sum 是一个内置函数 - 我建议不要定义任何变量来覆盖内置函数!

标签: python integration simpsons-rule


【解决方案1】:

函数的f 参数应该是一个函数。 math.sin(x) 不返回函数,它计算 x 的 sin 并返回该数字。 simpson() 然后尝试将其作为函数调用。

你应该只传递math.sin,它会在循环中被simpson()调用。

print(simpson(0,math.pi,math.sin,100))

【讨论】:

    猜你喜欢
    • 2016-05-05
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2016-01-12
    • 2013-11-05
    • 2015-12-15
    相关资源
    最近更新 更多