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

11 visualizaciones (últimos 30 días)
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*-');

Respuesta aceptada

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

Más respuestas (0)

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by