think90

三次样条曲线拟合及Matlab/Python实现

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/gophae/article/details/102808083

对于形如y = a + bx + c * x^2 + d * x^3 的三次spline曲线拟合的数学原理,我就不多说了。

我接了一个图给大家看看:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
数值计算的伪代码如下:
在这里插入图片描述
书名是:numerical_methods_for_engineers_for_engineers_chapra_canale_6th_edition
spline interpolation 在18.6章,想了解如何做三次曲线拟合的就去这个书里面找一下。

接下来就是Matlab 和 Python的实现:
Python代码来自https://github.com/gameinskysky/PythonRobotics/tree/master/PathPlanning/FrenetOptimalTrajectory
我稍作了修改:

class Spline:
    u"""
    Cubic Spline class
    """

    def __init__(self, x, y):
        self.b, self.c, self.d, self.w = [], [], [], []

        self.x = x
        self.y = y

        self.nx = len(x)  # dimension of x
        h = np.diff(x)

        # calc coefficient c
        self.a = [iy for iy in y]

        # calc coefficient c
        A = self.__calc_A(h)
        B = self.__calc_B(h)
        self.c = np.linalg.solve(A, B)
        #  print(self.c1)

        # calc spline coefficient b and d
        for i in range(self.nx - 1):
            self.d.append((self.c[i + 1] - self.c[i]) / (3.0 * h[i]))
            tb = (self.a[i + 1] - self.a[i]) / h[i] - h[i] * \
                (self.c[i + 1] + 2.0 * self.c[i]) / 3.0
            self.b.append(tb)

    def calc(self, t):
        u"""
        Calc position
        if t is outside of the input x, return None
        """

        if t < self.x[0]:
            return None
        elif t > self.x[-1]:
            return None

        i = self.__search_index(t)
        dx = t - self.x[i]
        result = self.a[i] + self.b[i] * dx + \
            self.c[i] * dx ** 2.0 + self.d[i] * dx ** 3.0

        return result

    def calcd(self, t):
        u"""
        Calc first derivative
        if t is outside of the input x, return None
        """

        if t < self.x[0]:
            return None
        elif t > self.x[-1]:
            return None

        i = self.__search_index(t)
        dx = t - self.x[i]
        result = self.b[i] + 2.0 * self.c[i] * dx 

分类:

技术点:

相关文章:

  • 2021-11-20
  • 2021-10-27
  • 2021-08-28
  • 2021-11-17
  • 2021-05-18
  • 2021-09-23
  • 2021-09-30
猜你喜欢
  • 2021-11-27
  • 2021-05-23
  • 2022-01-20
  • 2021-11-19
  • 2021-07-18
  • 2021-04-09
  • 2022-12-23
相关资源
相似解决方案