如何找到函数的第n个微分值的值?

时间:2017-05-26 13:00:32

标签: python algorithm python-3.x

let s = NSAttributedString(string: "", attributes: [NSForegroundColorAttributeName: UIColor.red])
let range = NSMakeRange(0, s.length)
let attrs = s.attributes(at: 0, longestEffectiveRange: nil, in: range)

我正在尝试找到i(lvl)(0),其中lvl是我被区分的次数。我不知道为什么我的代码为不同的d提供完全不同的输出。将会提供任何帮助:)

例如,

def differentiate(fn,lvl,value):
    d=0.0000001
    if lvl==0:
        return fn(value)
    else:
        return (differentiate(fn,lvl-1,value+d)-differentiate(fn,lvl-1,value))/d
def i(x):
    return 1 + x + x**2 + x**3 + x**4 +x**5+x**6

我希望得到:1,1,2,6,24,120,720。 但是,根据d的值,我会得到不同的值。

1 个答案:

答案 0 :(得分:2)

问题很容易理解。

您正在评估f(0 + d)等值,即

1 + 0.0000001 + 0.0000001² + 0.0000001³ + 0.0000001^4 + 5 0.0000001^5 = 
1.00000010000001000000100000010000005

当双精度浮点数能够代表大约15位有效数字时。

高阶的数值导数非常具有挑战性。