1

I know this might be a silly question but seriously I can't find the answer. I have some data and I interpolated a curve based on them: now I want to know the slope of the curve at a given point on the plot... how can I do that easily and quickly? I mean, besides doing the math with two close points and finding the slope coefficient by hand.

Gian Mauro Musso
  • 112
  • 1
  • 12
  • 1
    You can do the math with two close points and find the slope by *programming*. – Ander Biguri Mar 25 '15 at 10:09
  • Yeah I know that, I was thinking about a graphical way where i could click on a point and view the slope instantly, so that I could quickly see the slope for various points. If something like this exists. – Gian Mauro Musso Mar 25 '15 at 10:13
  • have a look at [**`gradient`**](http://www.mathworks.com/help/matlab/ref/gradient.html). – Robert Seifert Mar 25 '15 at 10:14
  • You can create a callback function which can be called anytime you click on a plot function (and then have the callback function implement the gradient mentioned in the answer) . See this answer: http://stackoverflow.com/questions/8442659/matlab-plot-points-and-make-them-clickable-to-display-informations-about-it – alexandre iolov Mar 25 '15 at 10:31
  • Thank you for your comment, it sounds really interesting and I'll definitely look into it – Gian Mauro Musso Mar 25 '15 at 10:51

1 Answers1

2

You're looking for the gradient function.

%// example data
x = linspace(0,2*pi);
y = sin(x);

%// finer x-axis
x_in = linspace(0,2*pi,2000);

%// interpolated data
y_in = interp1(x,y,x_in);

%// point of interest
dx = pi;
idx = find(x_in > dx,1)

%// slot at x = pi
sl    = gradient(y_in,x_in);
s1_pi = sl(idx)

returns:

s1_pi =

     -0.99983

(expected s1_pi = 1)

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113