I have plotted a graph in matlab with:
plot(x,y)
and my graph has different slopes, how do i draw tangents on each slope and calculate the coefficient for the slope?
解决方案
If you don't have an explicit function for the plotted points, you can use finite differences for estimating the derivative. The following is appropriate for points not on the border of the data span:
plot(x,y);
hold all;
% first sort the points, so x is monotonically rising
[x, sortidx] = sort(x);
y = y(sortidx);
% this is the x point for which you want to compute the slope
xslope = (x(1)+x(end))/2;
idx_a = find(x
idx_b = find(x>xslope,1,'first');
% or even simpler:
idx_b = idx_a+1;
% this assumes min(x)
xa = x(idx_a);
xb =