如何使用可用的曲率数据集找到曲线的切线?
How a find tangent of a curve with available dataset for curvature?
我有一个曲率数据集,我需要找到曲线的切线。我的代码如下,但不幸的是,我没有得到所需的结果:
chData = efficient.get('Car.Road.y')
fittedParameters = (np.gradient(chData_m_5[:],1)) # 999 values
plt.plot(chData[1:]) # orginally 1000 values
plt.plot(fittedParameters)
plt.show()
输出为:
编辑 1:
我对代码进行了以下更改以获得曲率的切线,但不幸的是,这离曲线有点远。请指导我与问题相关的问题解决方案。谢谢!
fig, ax1 = plt.subplots()
chData_m = efficient.get('Car.Road.y')
x_fit = chData_m.timestamps
y_fit = chData_m.samples
fittedParameters = np.polyfit(x_fit[:],y_fit[:],1)
f = plt.figure(figsize=(800/100.0, 600/100.0), dpi=100)
axes = f.add_subplot(111)
# first the raw data as a scatter plot
axes.plot(x_fit, y_fit, 'D')
# create data for the fitted equation plot
xModel = np.linspace(min(x_fit), max(x_fit))
yModel = np.polyval(fittedParameters, xModel)
# now the model as a line plot
axes.plot(xModel, yModel)
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label
# polynomial derivative from numpy
deriv = np.polyder(fittedParameters)
# for plotting
minX = min(x_fit)
maxX = max(x_fit)
# value of derivative (slope) at a specific X value, so
# that a straight line tangent can be plotted at the point
# you might place this code in a loop to animate
pointVal = 10.0 # example X value
y_value_at_point = np.polyval(fittedParameters, pointVal)
slope_at_point = np.polyval(deriv, pointVal)
ylow = (minX - pointVal) * slope_at_point + y_value_at_point
yhigh = (maxX - pointVal) * slope_at_point + y_value_at_point
# now the tangent as a line plot
axes.plot([minX, maxX], [ylow, yhigh])
plt.show()
plt.close('all') # clean up after using pyplot
输出为:
很可能只是一个缩放问题,我们可以通过创建一个 twin axis for the gradient that is scaled independently of the original data. To be on the safe side, we also provide the x-values to np.gradient 来解决,以防它们的间距不均匀。
import matplotlib.pyplot as plt
import numpy as np
fig, ax1 = plt.subplots()
def func(x, a=0, b=100, c=1, n=3.5):
return a + (b/(1+(c/x)**n))
x_fit = np.linspace(0.1, 70, 100)
y_fit = func(x_fit, 1, 2, 15, 2.4)
tang = np.gradient(y_fit, x_fit)
ax1.plot(x_fit, y_fit, c="blue", label="data")
ax1.legend()
ax1.set_ylabel("data")
ax2 = ax1.twinx()
ax2.plot(x_fit, tang, c="red", label="gradient")
ax2.legend()
ax2.set_ylabel("gradient")
plt.show()
示例输出:
如果我们把它画在同一张图中的图:
我有一个曲率数据集,我需要找到曲线的切线。我的代码如下,但不幸的是,我没有得到所需的结果:
chData = efficient.get('Car.Road.y')
fittedParameters = (np.gradient(chData_m_5[:],1)) # 999 values
plt.plot(chData[1:]) # orginally 1000 values
plt.plot(fittedParameters)
plt.show()
输出为:
编辑 1:
我对代码进行了以下更改以获得曲率的切线,但不幸的是,这离曲线有点远。请指导我与问题相关的问题解决方案。谢谢!
fig, ax1 = plt.subplots()
chData_m = efficient.get('Car.Road.y')
x_fit = chData_m.timestamps
y_fit = chData_m.samples
fittedParameters = np.polyfit(x_fit[:],y_fit[:],1)
f = plt.figure(figsize=(800/100.0, 600/100.0), dpi=100)
axes = f.add_subplot(111)
# first the raw data as a scatter plot
axes.plot(x_fit, y_fit, 'D')
# create data for the fitted equation plot
xModel = np.linspace(min(x_fit), max(x_fit))
yModel = np.polyval(fittedParameters, xModel)
# now the model as a line plot
axes.plot(xModel, yModel)
axes.set_xlabel('X Data') # X axis data label
axes.set_ylabel('Y Data') # Y axis data label
# polynomial derivative from numpy
deriv = np.polyder(fittedParameters)
# for plotting
minX = min(x_fit)
maxX = max(x_fit)
# value of derivative (slope) at a specific X value, so
# that a straight line tangent can be plotted at the point
# you might place this code in a loop to animate
pointVal = 10.0 # example X value
y_value_at_point = np.polyval(fittedParameters, pointVal)
slope_at_point = np.polyval(deriv, pointVal)
ylow = (minX - pointVal) * slope_at_point + y_value_at_point
yhigh = (maxX - pointVal) * slope_at_point + y_value_at_point
# now the tangent as a line plot
axes.plot([minX, maxX], [ylow, yhigh])
plt.show()
plt.close('all') # clean up after using pyplot
输出为:
很可能只是一个缩放问题,我们可以通过创建一个 twin axis for the gradient that is scaled independently of the original data. To be on the safe side, we also provide the x-values to np.gradient 来解决,以防它们的间距不均匀。
import matplotlib.pyplot as plt
import numpy as np
fig, ax1 = plt.subplots()
def func(x, a=0, b=100, c=1, n=3.5):
return a + (b/(1+(c/x)**n))
x_fit = np.linspace(0.1, 70, 100)
y_fit = func(x_fit, 1, 2, 15, 2.4)
tang = np.gradient(y_fit, x_fit)
ax1.plot(x_fit, y_fit, c="blue", label="data")
ax1.legend()
ax1.set_ylabel("data")
ax2 = ax1.twinx()
ax2.plot(x_fit, tang, c="red", label="gradient")
ax2.legend()
ax2.set_ylabel("gradient")
plt.show()
示例输出:
如果我们把它画在同一张图中的图: