繁体   English   中英

如何使用可用的曲率数据集找到曲线的切线?

[英]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()

output 是:

在此处输入图像描述

编辑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

output 是: 在此处输入图像描述

很可能只是一个缩放问题,我们可以通过为独立于原始数据缩放的梯度创建双轴来解决。 为了安全起见,我们还向np.gradient提供 x 值,以防它们的间距不均匀。

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()

样品 output: 在此处输入图像描述

如果我们将其绘制在同一张图中,该图: 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM