拟合两条切线之间的多项式 python
Fit Polynomial between two tangents python
我想在两条线之间拟合一个多项式,每条线都有各自的斜率,见下图。
假设我有 {L_1, m_1} 和 {L_0, m_0} 其中 {L_i} 是行和 {m_i} 相应直线的斜率。拟合多项式可能需要其他参数,但我的初始约束是相互平行的线与斜率之间的距离。有谁知道对这项任务有帮助的图书馆吗?谢谢。
NumPy 库提供了解决此问题的好工具,请参阅 numpy.polyfit。下面是如何实现 polyfit 函数的源代码。 注意至少需要2对点,2个在结尾,2个在开头。功能。这些点对应该有直线的斜率。然后从最后的点,可以延长切线。这种方法很实用,但它适用于需要的东西
import warnings
import numpy as np
import matplotlib.pyplot as plt
def polynomial_fit(x1,y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6):
'''
coordinates required for polynomial generation, -pairs at the begining
3 pairs at the end
xi, east
yi, west
return
Polynomial coefficient of degree 3
'''
# x coordinates of the required six points for x and y
x_n = np.array([x1, x2, x3, x4, x5, x6])
y_n = np.array([y1, y2, y3, y4, y5, y6])
# polyfit function
poly = np.polyfit(x_n, y_n, 3)
# writes the coeffiecient with respective x^deg
eq_poly = np.poly1d(poly)
print(eq_poly)
# set range where x will be plotted
t = np.linspace(x1, x6, 100)
# creates a plot with the six points polynomial
plt.scatter(x_n, y_n)
plt.plot(x_n, y_n, "--", t, eq_poly(t), "-")
test = polynomial_fit(7.19447831, 369605.543,
9, 369613.4986,
10, 369624.701,
42, 369851.94471859,
43, 369865.3556,
44, 369878.7666)
合身度如下图所示。我认为 docstring 解释得很好,所以它会让遇到同样问题的任何人受益。
我想在两条线之间拟合一个多项式,每条线都有各自的斜率,见下图。
假设我有 {L_1, m_1} 和 {L_0, m_0} 其中 {L_i} 是行和 {m_i} 相应直线的斜率。拟合多项式可能需要其他参数,但我的初始约束是相互平行的线与斜率之间的距离。有谁知道对这项任务有帮助的图书馆吗?谢谢。
NumPy 库提供了解决此问题的好工具,请参阅 numpy.polyfit。下面是如何实现 polyfit 函数的源代码。 注意至少需要2对点,2个在结尾,2个在开头。功能。这些点对应该有直线的斜率。然后从最后的点,可以延长切线。这种方法很实用,但它适用于需要的东西
import warnings
import numpy as np
import matplotlib.pyplot as plt
def polynomial_fit(x1,y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6):
'''
coordinates required for polynomial generation, -pairs at the begining
3 pairs at the end
xi, east
yi, west
return
Polynomial coefficient of degree 3
'''
# x coordinates of the required six points for x and y
x_n = np.array([x1, x2, x3, x4, x5, x6])
y_n = np.array([y1, y2, y3, y4, y5, y6])
# polyfit function
poly = np.polyfit(x_n, y_n, 3)
# writes the coeffiecient with respective x^deg
eq_poly = np.poly1d(poly)
print(eq_poly)
# set range where x will be plotted
t = np.linspace(x1, x6, 100)
# creates a plot with the six points polynomial
plt.scatter(x_n, y_n)
plt.plot(x_n, y_n, "--", t, eq_poly(t), "-")
test = polynomial_fit(7.19447831, 369605.543,
9, 369613.4986,
10, 369624.701,
42, 369851.94471859,
43, 369865.3556,
44, 369878.7666)
合身度如下图所示。我认为 docstring 解释得很好,所以它会让遇到同样问题的任何人受益。