How to find the slope of a tangent on a point on a nonlinear curve?

I have a set of data points x and y. I am plotting these on a (x,y) graph. The result is a nonlinear curve. On each and every point on the curve, tangents can be drawn and the slopes for every tangent will be different. I want to know, how I can I draw these tangents and find their slope. Please guide. I am new to MATLAB.

 Akzeptierte Antwort

John D'Errico
John D'Errico am 15 Apr. 2015
Bearbeitet: John D'Errico am 15 Apr. 2015

2 Stimmen

You cannot find a slope until you define a curve. Simple points are not a curve. You have connected the points in your mind, so you "see" a curve. But all you have are points.
So you need to find a function that interpolates the points. A spline will do. Then differentiate the spline to get the slope.
You will probably want to use tools like spline (to fit a spline), fnder (differentiate it), and fnval (evaluate the derivative).
If these points form a completely general multi-valued relationship, such that there are multiple values of y for a given point x, you can still form a spline model, but it will take an extra step or two to do the work. An example of such a curve is a circle, where a direct spline fit using spline will fail.

7 Kommentare

Thanks for replying. I have attached my curve in the question section now. This is how the plot of x, y looks. Now I want to find the value of the slope of the tangent at any point on the curve, for example when x = 415 or x = 203 or x = 365.
Since you have a bunch of points, I guess John's submission to the file exchange is your best bet ...
Titus
John D'Errico
John D'Errico am 15 Apr. 2015
Bearbeitet: John D'Errico am 15 Apr. 2015
Sadly, derivest does not do differentiation of a point set. I did write and post a Savitsky-Golay scheme. But I'd suggest a differentiation of a spline interpolant as the best choice.
Do this:
spl = pchip(x,y);
splder = fnder(spl);
d = fnval(splder,[415,203,365]);
I chose pchip as the interpolant here, but an alternative would have been to use spline for that part instead. Since your function is not terribly smooth, those sharp transitions in slope will probably cause some ringing behavior from spline. You could also have built a smoother approximant using my slm toolbox, found on the File Exchange.
Thanks John. It seems to be working fine :-)
can u please share the whole code by taking some random data points
I'm a bit perplexed. I already did share the code. I.e., use pchip, then fnder, then fnval. Other tools might apply in the scenario where you don't want to use pchip, or lack the curve fitting toolbox.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

farzad
farzad am 15 Apr. 2015

0 Stimmen

I think you just can define a straight line between each of the two point and simply find the slope of that line

1 Kommentar

@farzad: I can not define a straight line between two points on the curve to find slope as the slope changes at every point. Its a nonlinear curve. Further, I need to find the tangent at the particular point and then find its slope. Tangent at a particular point on the curve is unique and hence its slope.

Melden Sie sich an, um zu kommentieren.

Kategorien

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by