三次样条曲线的导数是二次样条曲线。SciPy只有一个内置的方法来寻找三次样条曲线的根。所以有两种方法:采用四次样条插值,可以方便地求出其导数的根。在
使用三次样条曲线(通常更可取),并为其导数的根编写一个自定义函数。在
我在下面描述这两种解决方案。在
四次样条曲线
使用InterpolatedUnivariateSpline。它的.derivative方法返回一个三次样条曲线,可以对其应用.roots方法。在from scipy.interpolate import InterpolatedUnivariateSpline
f = InterpolatedUnivariateSpline(x_axis, y_axis, k=4)
cr_pts = f.derivative().roots()
cr_pts = np.append(cr_pts, (x_axis[0], x_axis[-1])) # also check the endpoints of the interval
cr_vals = f(cr_pts)
min_index = np.argmin(cr_vals)
max_index = np.argmax(cr_vals)
print("Maximum value {} at {}\nMinimum value {} at {}".format(cr_vals[max_index], cr_pts[max_index], cr_vals[min_index], cr_pts[min_index]))
输出:Maximum value 6.779687224066201 at 2.1824928509277037
Minimum value 0.34588448400295346 at 2.207586817729