當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python SciPy misc.central_diff_weights用法及代碼示例

本文簡要介紹 python 語言中 scipy.misc.central_diff_weights 的用法。

用法:

scipy.misc.central_diff_weights(Np, ndiv=1)#

Np-point 中心導數的返回權重。

假設函數點等距。

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

參數

Np int

中心導數的點數。

ndiv 整數,可選

分區數。默認值為 1。

返回

w ndarray

Np-point 中心導數的權重。其大小為 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

相關用法


注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.misc.central_diff_weights。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。