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);
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);
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).
Best Answer