python - scipy misc.derivative 结果不正确

标签 python scipy

我想知道我在这里做错了什么......

我正在试验一个简单而人为的函数,它对某些 x 值求导:

f(x) = x^3,然后计算导数 f'(x) = 3x^2 对于 x 在 1、2、3 处的值

>>> from scipy import misc
>>> def x2(x): return x*x*x
...
>>> misc.derivative(x2,1)
4.0
>>> misc.derivative(x2,2)
13.0
>>> misc.derivative(x2,3)
28.0    

问题:结果不正确,它们都比应有的值大 +1(应分别为 3、12 和 27)。

最佳答案

scipy.misc.derivative 不准确。它使用中心差分公式来计算导数。默认间距为 1.0,这对于很多应用程序来说都相当高。减少它会得到更准确的结果:

>>> from scipy import misc
>>> def x3(x): return x*x*x
... 
>>> misc.derivative(x3, 1)
4.0
>>> misc.derivative(x3, 1, dx=0.5)
3.25
>>> misc.derivative(x3, 1, dx=0.25)
3.0625
>>> misc.derivative(x3, 1, dx=1.0/2**16)
3.0000000002328306

关于python - scipy misc.derivative 结果不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18220216/

相关文章:

python - keras中的概率回归

python - multiprocessing.Pool,不会永久影响类变量

python - SciPy:什么是 "generalized"连续随机变量?

Python+Scipy+集成 : dealing with precision errors in functions with spikes

python - pip 从requirements.txt 安装文件时出错

python - Matlab 到 Python 的转换

python-2.7 - 用户、项目对的稀疏矩阵实现

python - 如何将网站图标添加到 Pelican 博客?

python - Python 类内部的继承

python - Python 脚本中的 Scapy