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

slope of a tangent on a 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.

Best Answer

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.