如何在Python中仅在特定点上进行平滑样条插值设置的导数?

问题描述 投票:1回答:1

TL; DR

显然scipy没有提供类似于MATLAB spapi的方法,该方法只允许在插值样条线上的特定点设置导数,确保平滑过渡。我想找到一种在Python中执行此操作的方法。

整体

我的目标是使用Python 3进行样条插值,但仅在特定点设置所需的导数。例如,假设数组X定义了对象的位置,点对点,我希望用样条线表示可行的轨迹,但还要确保起点和终点(其他点)的速度(一阶导数)均为零对衍生产品没有限制。在此示例中,我也可能希望在相同点上使加速度(二阶导数)等于零。

换言之,我想要的是样条插值的实现,类似于MATLAB的spapi函数,引用为here

spline = spapi(knots,x,y)返回样条f(如果有)的顺序

k = length(knots) - length(x)

具有结序列的结]

(*) f(x(j)) = y(:,j), all j.

[如果x的某些条目相同,则采用可触摸的意义

”,即,在Dm(j)f(x(j)) = y(:, j)m(j) : = #{ i < j : x(i) = x(j) }以及Dmf和[C0 ] [的导数。因此,mth中位点z的r倍重复对应于x处的值和f的第一r – 1导数的规定。我尝试过的

[我知道zfrom_derivatives类的方法BPoly,但这带来了一个关键问题,即当在任何时候都未指定导数时,该算法不能保证平滑过渡,如[ C0]。我也尝试了建议的scipy.interpolate,但正如预期的那样,出现了同样的问题。

因此,接下来,我将简单介绍我想要达到的目标,无论成功与否。

[here不带导数

这里,使用here方法的示例,在MATLAB中,

没有

设置任何导数。不像我想要的那样,因为导数的起点和终点都很高:spapi
以及相应的参考点图和内插样条线:spapi

这里是该样条的一阶导数:xi = linspace(0, 10, 6); xnew = linspace(0, 10, 100); yi = [0 2 1 4 2 0]; knots = optknt(xi, order); ref_spline = spapi(knots, xi, yi); spline = fnval(xnew, ref_spline); der_spline = gradient(spline);

[Spline interpolation without setting derivatives, using <code>spapi</code>及其衍生物

这里,使用1st derivative of the interpolated spline方法的示例,在MATLAB中,

将导数设置为spapi] >>在起点和终点。完全符合预期的结果,整个样条线都平滑过渡,并且在起点和终点处的导数等于spapi

0
以及参考点与样条线的图:0

这里是该样条的一阶导数:xi = linspace(0, 10, 6); xnew = linspace(0, 10, 100); xder = [0 xi 10]; yi = [0 2 1 4 2 0]; ynew = [0 yi 0]; knots = optknt(xder, order); ref_spline = spapi(knots, xder, ynew); spline = fnval(xnew, ref_spline); der_spline = gradient(spline);

[Spline interpolation setting derivatives as zero at start and end, using <code>spapi</code>及其衍生物

在这里,在起点和终点都使用1st derivative of the interpolated spline

将导数设置为BPoly.from_derivatives] >>。即使导数在开始和结束时都是BPoly.from_derivatives,也不会成功,但不能保证沿样条线的平滑过渡:

0
为清楚起见,定义0的行仅用包含索引ref_points = [0, 2, 1, 4, 2, 0]
time_vector = np.linspace(0, 10, 100)
time_points = np.linspace(0, 10, 6)

ref_complete = [[ref_points[j] if (i == 0) else 0 for i in range(2)] if (
    (j == 0) or (j == len(ref_points) - 1)) else [ref_points[j]] for j in range(len(ref_points))]

ref_spline = BPoly.from_derivatives(time_points, ref_complete)
spline = ref_spline(time_vector)

der_spline = ref_spline.derivative(1)
der_y = der_spline(time_vector)
处的原始值和索引ref_complete处的ref_points的数组替换0的第一个和最后一个元素:

0

以及参考点与样条线的图:1

这里是该样条的一阶导数:>>> ref_points [0, 2, 1, 4, 2, 0] >>> ref_complete [[0, 0], [2], [1], [4], [2], [0, 0]]

TL; DR显然scipy没有提供类似于MATLAB的spapi的方法,该方法只允许在特定点设置导数,以确保沿插值方向平滑过渡...

如果只想在两端进行零导数的三次样条插值,就足够了[Spline interpolation setting derivatives as zero at start and end, using <code>BPoly.from_derivatives</code>]

1st derivative of the interpolated spline

python matlab scipy interpolation spline
1个回答
0
投票
如果只想在两端进行零导数的三次样条插值,就足够了[Spline interpolation setting derivatives as zero at start and end, using <code>BPoly.from_derivatives</code>]

1st derivative of the interpolated spline

© www.soinside.com 2019 - 2024. All rights reserved.