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*-');
0 个评论
采纳的回答
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!