高阶函数——接收lambda函数进行导数计算
High order functions - receive lambda func for derivative calculation
我是 python 的新手,我一直在尝试实现数值导数高阶函数,但我无法修改我的代码以获得所需的结果:
def deriv(f):
return lambda x: ((f(x + h) - f(x)) / h)
但是在调用函数时出现错误,我无法解释。
欢迎使用 Whosebug!下次我建议添加错误描述,以帮助我们更好地帮助您。
在这种情况下,我想您可能遗漏了变量 h 的定义。
尝试将此行添加到您的代码中:
def derivative(f):
h=0.000001
return lambda x: ((f(x + h) - f(x)) / h)
或:
def deriv(f, h=0.000001):
return lambda x: ((f(x + h) - f(x)) / h)
我是 python 的新手,我一直在尝试实现数值导数高阶函数,但我无法修改我的代码以获得所需的结果:
def deriv(f):
return lambda x: ((f(x + h) - f(x)) / h)
但是在调用函数时出现错误,我无法解释。
欢迎使用 Whosebug!下次我建议添加错误描述,以帮助我们更好地帮助您。
在这种情况下,我想您可能遗漏了变量 h 的定义。 尝试将此行添加到您的代码中:
def derivative(f):
h=0.000001
return lambda x: ((f(x + h) - f(x)) / h)
或:
def deriv(f, h=0.000001):
return lambda x: ((f(x + h) - f(x)) / h)