scipy.misc.

central_diff_weights#

scipy.misc.central_diff_weights(Np, ndiv=1)[原始碼]#

傳回 Np 點中心差分的權重。

假設函數點間距相等。

如果權重在向量 w 中,則導數為 w[0] * f(x-ho*dx) + … + w[-1] * f(x+h0*dx)

自版本 1.10.0 起已棄用: central_diff_weights 已從 SciPy 1.10.0 中的 scipy.misc.central_diff_weights 棄用,並將在 SciPy 1.12.0 中完全移除。您可以考慮使用 findiff:maroba/findiff 或 numdifftools:pbrod/numdifftools

參數:
Npint

中心差分的點數。

ndivint, 可選

除數。預設值為 1。

傳回值:
wndarray

Np 點中心差分的權重。其大小為 Np

注意事項

對於大量的點數可能不準確。

參考資料

範例

我們可以計算函數的導數值。

>>> from scipy.misc import central_diff_weights
>>> def f(x):
...     return 2 * x**2 + 3
>>> x = 3.0 # derivative point
>>> h = 0.1 # differential step
>>> Np = 3 # point number for central derivative
>>> weights = central_diff_weights(Np) # weights for first derivative
>>> vals = [f(x + (i - Np/2) * h) for i in range(Np)]
>>> sum(w * v for (w, v) in zip(weights, vals))/h
11.79999999999998

此值接近解析解:f’(x) = 4x,因此 f’(3) = 12