Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

python - How a find tangent of a curve with available dataset for curvature?

I have a dataset for curvature and I need to find the tangent to the curve. My code is as follows but unfortunately, I am not getting the required resut:

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

The output is:

enter image description here

Edit 1:

I made the following changes to the code to get the tangent to curvature but unfortunately, this is a bit far from the curve. Kindly guide me the issue solution related to the problem. Thank you!

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

And the output is: enter image description here

question from:https://stackoverflow.com/questions/65831134/how-a-find-tangent-of-a-curve-with-available-dataset-for-curvature

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Most likely just a scaling problem that we can address by creating a 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 in case they are not evenly spaced.

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

Sample output: enter image description here

The figure if we plotted it in the same graph: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...