我需要用三点端点公式求函数的n导数
有人能帮我写代码吗?因为我的似乎真的很缺乏
这是我的代码:
formula = input('Input the formula here : ')
n = input('Input the exponent here: ')
def f(x):
return eval(formula)
fungsi = pow(formula,n)
x0 = eval(input('Input the approximation of x here : '))
h = eval(input('Input the stepsize h here : '))
def TPEP(x,h,n,formula): #Three Point End Point
return (1/(2*h))*(-3*f(x,n-1)+4*f(x+h,n-1)-f(x+2*h,n-1))
print('Derivative of f in x0 = {0} is : '.format(x0))
print("f'({0}) = {1:.7f} (Three Point Endpoint)".format(x0,TPEP(x0,h,n,formula)))
如果有人能帮忙,我将非常感激。多谢各位
我认为主要的一点是,您必须检查
TPEP
中的终止。第二件事是您实际上必须执行递归:您调用的是f
,而不是导数TPEP
的递归近似。使用其他一些修复程序(小心、未经过彻底测试且无错误处理):产生
同样,在
f
的eval
中,使用一个变量(这里是局部范围)以不同的x
-值方便地对其求值相关问题 更多 >
编程相关推荐