# Tangent on a curve

조회 수: 43(최근 30일)
Babu Sankhi 2020년 10월 5일
답변: Priysha LNU 2020년 10월 8일
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
##### 댓글 수: 1표시숨기기 없음
John D'Errico 2020년 10월 5일
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.

댓글을 달려면 로그인하십시오.

### 답변(1개)

Priysha LNU 2020년 10월 8일
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.

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!