Slope of a plotted function in a specified point.

14 views (last 30 days)
Edward Simpson Songel
Edward Simpson Songel on 12 Nov 2020
Answered: Star Strider on 15 Nov 2020
I've currently plotted an array signal that displays x and F(x) (not in a continuous manner but in a discreet one as it's an array). The signal plot is shown below.
My question is if there is a way to find out the slope of a point in the plot or if you can somehow get a slope from two close points on the plot, so I can then calculate m (slope constant). I need this in order to find out the transfere function of the signal.
Thank you very much in advance.
Yours faithfully.

Answers (2)

VBBV on 13 Nov 2020
%if true
% code
t = 1:100
y = sin(2*pi*t)
YY = gradient(y)
  1 Comment
Edward Simpson Songel
Edward Simpson Songel on 15 Nov 2020
Hello Vasishta,
What I meant is if there is any graphic instrument on the plot command or any command that I can use to measure the slope at each point. From your reply what I see you do is establish a time period t and then use a sine function to calculate the slope. But in my case, I've got a signal out from an output of a machine, and i need to study the slope of that empirical signal. Is there any way I can do that?
Thank you very much for your reply and attention.

Sign in to comment.

Star Strider
Star Strider on 15 Nov 2020
It would be easier to have your data.
Try this:
xv = linspace(0, 5); % Create Data
yv = sin(2*pi*xv*2.5); % Create Data
slopefcn = @(xsel,xv,yv) interp1(xv, gradient(yv)./gradient(xv), xsel); % Interpolate Slope At ‘xsel’
yval = @(xsel,xv,yv) interp1(xv, yv, xsel); % Interpolate ‘yval’ At ‘xsel’
xsel = 2.35; % Selected ‘X’ Value
ysel = yval(xsel,xv,yv); % Corresponding ‘Y’ Value
m = slopefcn(xsel, xv, yv); % Calculate Slope
b = ysel - m*xsel; % Calculate Y-Intercept (To Draw Line)
calcLine = m*(xsel+[-1 1]*0.05) + b; % Calculate Line For Plot
plot(xv, yv)
hold on
plot(xsel, ysel, '+r') % Plot Point (Demonstration Only)
plot(xsel+[-1 1]*0.05, calcLine, '-r') % Plot Line (Demonstration Only)
hold off
My code calculates the slope at ‘xsel’ and then plots a line using it in order to demonstrate that it works. That part of the code is not necessary if you do not want to plot a line at the ‘xsel’ point. The data do not need to be regularly-sampled for it to work.

Community Treasure Hunt

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

Start Hunting!

Translated by