Hi,
I have x and y values which represent some sort of sin wave but I don't know the function. something like x = [1.190984579 1.15776987 1.12455516 1.096085409 1.067615658 1.034400949 1.003558719 0.975088968 0.944246738 0.911032028 0.877817319 0.846975089 0.825622776]
y = [0.526690391 0.517200474 0.519572954 0.533807829 0.550415184 0.56227758 0.552787663 0.536180308 0.521945433 0.521945433 0.524317912 0.517200474 0.495848161]
I want to draw tangent line on the point I specify. Please help me do it.
Thanks.

 Accepted Answer

Andrew Newell
Andrew Newell on 26 Jun 2011
You'll need to fit a function to the data and then take its derivative. 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. I recommend downloading the package Adaptive Robust Numerical Differentiation package from the File Exchange. Then you can 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)
(Edited to accept an index number instead of an x value).

More Answers (3)

Judah S
Judah S on 26 Jun 2011

0 votes

Thanks Andrew.
Kindly check my this approach.
function tangent1
close all;
clear all;
clc;
format long;
x = [1.190984579 1.15776987 1.12455516 1.096085409 1.067615658 1.034400949 1.003558719 0.975088968 0.944246738 0.911032028 0.877817319 0.846975089 0.825622776];
y = [0.526690391 0.517200474 0.519572954 0.533807829 0.550415184 0.56227758 0.552787663 0.536180308 0.521945433 0.521945433 0.524317912 0.517200474 0.495848161];
idx = input('Enter index of X:');
plot(x,y)
%%Slope at a given point
m=(y(idx+1)-y(idx))/(x(idx+1)-x(idx)) %Equation of forward difference
m1=(y(idx)-y(idx-1))/(x(idx)-x(idx-1)) %Equation of backward difference
m2=(y(idx+1)-y(idx-1))/(x(idx+1)-x(idx-1)) %Average of both, also equation of secant
fprintf('Equation of tangent \n')
fprintf('q = %4.2f*(p-%4.2f)+%4.2f \n',m1,x(idx),y(idx))
p = 0.75:0.01:1.6;
q = m1*(p-x(idx))+ y(idx);
hold on;
plot(p,q,'r')
end
Do you think it's correct?
Thanks,

5 Comments

Andrew Newell
Andrew Newell on 26 Jun 2011
It depends on what you are trying to do. To me, these points look like a coarse sampling of a smooth function. If so, you're computing a secant, not a tangent. That's why I suggested fitting the function.
A minor point: the function fails if you choose the starting point or end point of the curve.
Judah S
Judah S on 26 Jun 2011
Thanks for the comment.
Yes it fails because of the m1. It probably won't if I use m.
Appreciate your help.
:-)
Andrew Newell
Andrew Newell on 26 Jun 2011
Actually, m2 works pretty well.
Judah S
Judah S on 26 Jun 2011
Your code is awesome. This is exactly what I needed.
I was considering interpolation or extrapolation to choose points since I was using indexes.
derivest is really cool.
Andrew Newell
Andrew Newell on 26 Jun 2011
Glad you like it! I agree, derivest is very cool.

Sign in to comment.

Judah S
Judah S on 30 Jun 2011

0 votes

Hi Andrew,
I am curious to know if I want to measure angle between tangent line and X or Y axis, how can I do that?
Thanks.

Categories

Find more on Mathematics and Optimization 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!