Slope of a plotted function in a specified point.

4 visualizzazioni (ultimi 30 giorni)
Hi,
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.
Edward.

Risposte (1)

Star Strider
Star Strider il 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
figure
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.

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by