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

26 次查看(过去 30 天)
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*-');

采纳的回答

Jan
Jan 2022-10-4
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by