区分 scipy.interpolate.interp1d

differentiating scipy.interpolate.interp1d

给定两个向量 xy 我可以使用 scipy.interpolate.interp1d 来计算(二次)样条。然而,我对样条曲线本身并不直接感兴趣,而是对样条曲线的导数感兴趣。 我宁愿有一个明确的解决方案而不是数值导数。

但是我找不到多项式参数存储在 interp1d 中的位置。 我尝试了 interp1d.__dict__,其中包含 interp1d._spline,但是我没有找到此参数的定义。

勾选InterpolatedUnivariateSpline, it has the derivative method:注意,为了方便,导数returns是另一个样条对象,也可以指定导数的阶数(默认为1)。

from scipy.interpolate import InterpolatedUnivariateSpline as IUS
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(10)
y = x**2 + np.random.normal(size=10)

u = IUS(x,y)
u_der = u.derivative()

plt.plot(x, y, 'go')
plt.plot(x, u(x), 'b--')
plt.plot(x, u_der(x), 'k')