# Tangent on a curve

113 views (last 30 days)
Babu Sankhi on 5 Oct 2020
Answered: Priysha LNU on 8 Oct 2020
Hello,
I have a plot (along with data which I plotted) pasted above. I have to draw two tangents at different points A and B and should find the C ( the coordinates at which they meet).
It would be great if anyone can help me. How can I do it?
Thank you
John D'Errico on 5 Oct 2020
Since this is surely homework (if you have to do this) then what have you tried? If nothing, why not?
What is a tangent line? That is, it is just a line that passes through a point, with a given slope. Can you compute the slope between two points? If not, then should you be spending some time reading about some basic algebra, and what is the meaning of a slope?
Once you have found the equations of two tangent lines, how do you find the point where two lines cross? Again, this is basic algebra. Do you know how to solve it on paper? If so, then you should know how to write the necessary MATLAB code, or at least be able to make an effort.
Seriously, you need to make an effort on homework. Else, you learn nothing except how to get someone to do your work for you in the future.
So show what you tried. Then ask for help.

Priysha LNU on 8 Oct 2020
You'll need to fit a function to the data and then take its derivative. Let x and y be the two lists of data points. First, the fit:
plot(x,y,'.');
hold on
n = 10;
[p,~,mu] = polyfit(x,y,n); % degree 10 polynomial fit, centered and scaled
% Plot the fit with the data
xfit = linspace(min(x),max(x),100);
yfit = polyval(p,xfit,[],mu);
plot(xfit,yfit,'r')
Now you can use a numerical derivative to calculate the tangent. You may download the package Adaptive Robust Numerical Differentiation package from the File Exchange. Then you may do the following:
idx = input(['Enter index in the range ',num2str([1 length(x)]),':']);
x0 = x(idx);
f = @(x) polyval(p,x,[],mu);
df = derivest(f,x0); % Here is the derivative
% Calculate the end points of a tangent line
xt = [x0-0.05 x0+0.05];
yt = f(x0) + (xt-x0)*df;
plot(xt,yt)
DISCLAIMER: These are my own views and in no way depict those of MathWorks.

### Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by