Using diff() or gradient() or other methods to solve the local slope of a set of discrete point?

26 visualizzazioni (ultimi 30 giorni)
Hi, I have a set of x and y, each has 44 values, and I use the following method to solve the local slope of this curve.
The two slopes look different, and I don't one which one is more reasonable?
load x.mat
load y.mat
% 1. gradient, the result has 44 values (same with dataset)
slope1 = gradient(y);
% 2. diff(), the result has 43 values
slope2 = diff(y)./diff(x);
% figure
plot(x,y,'ko-'); hold on;
plot(x,slope1,'r*-');
plot(x(2:end),slope2,'b*-');

Risposta accettata

Jan
Jan il 4 Ott 2022
load x.mat
load y.mat
slope1 = gradient(y);
slope2 = diff(y) ./ diff(x);
slope3 = gradient(y, x); % Consider x in the slope
plot(x, y, 'k-'); hold on;
plot(x, slope1, 'r-');
plot(x(2:end), slope2, 'b-');
plot(x, slope3, 'bo');
This shows, that diff(y)./diff(x) is a bestter choice except for the missing element, because diff(x) has one element less than x.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Tag

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by