Clear Filters
Clear Filters

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

18 views (last 30 days)
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*-');

Accepted Answer

Jan
Jan on 4 Oct 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.

More Answers (0)

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!