scipy.interpolate.LSQUnivariateSpline.
roots#
- LSQUnivariateSpline.roots()[source]#
傳回 spline 的零點。
另請參閱
說明
限制:FITPACK 僅支援三次 spline。對於非三次 spline,請使用 PPoly.root(請參閱下方範例)。
範例
對於某些資料,此方法可能會遺漏根。當其中一個 spline 節點(FITPACK 自動放置)恰好與真根重合時,就會發生這種情況。一種解決方法是轉換為
PPoly
,它使用不同的尋根演算法。例如,
>>> x = [1.96, 1.97, 1.98, 1.99, 2.00, 2.01, 2.02, 2.03, 2.04, 2.05] >>> y = [-6.365470e-03, -4.790580e-03, -3.204320e-03, -1.607270e-03, ... 4.440892e-16, 1.616930e-03, 3.243000e-03, 4.877670e-03, ... 6.520430e-03, 8.170770e-03] >>> from scipy.interpolate import UnivariateSpline >>> spl = UnivariateSpline(x, y, s=0) >>> spl.roots() array([], dtype=float64)
轉換為 PPoly 物件確實會在 x=2 找到根
>>> from scipy.interpolate import splrep, PPoly >>> tck = splrep(x, y, s=0) >>> ppoly = PPoly.from_spline(tck) >>> ppoly.roots(extrapolate=False) array([2.])