Finding the gradient of a slope

35 visualizaciones (últimos 30 días)
Diane Scicluna
Diane Scicluna el 18 de Abr. de 2020
Respondida: Star Strider el 20 de Abr. de 2020
I have this graph and I need to find the cooling rate.
I need to split the curve into smaller lines and then find the gradient of each line. Then I need to find the average of each gradient up to approximately 25 degrees to find the cooling rate. I have a lot of data to go through.
Can anyone help me please?
Thanks in advance!
  1 comentario
John D'Errico
John D'Errico el 18 de Abr. de 2020
Editada: John D'Errico el 18 de Abr. de 2020
That is what computers are good at. You use loops, and let the computer do the work. Of course, it looks like you managed to create zillions of variables all with numbered names. Next time, don't do that, as then you end up in a situation where you have a lot of work to do. Instead, use arrays. Then you can just write a loop.
If you want a suggestion, put all of your data into one array, even if that requires using a cell array, and some typing up front. Then just use a loop.

Iniciar sesión para comentar.

Respuestas (2)

Ameer Hamza
Ameer Hamza el 18 de Abr. de 2020
You can use gradient(): https://www.mathworks.com/help/matlab/ref/gradient.html with mean().
For example
cooling_rate = mean(gradient(time,temperature))
  2 comentarios
Diane Scicluna
Diane Scicluna el 20 de Abr. de 2020
Thank you for your help! The code worked but unfortunately I have so much data that the only output was INF.
Ameer Hamza
Ameer Hamza el 20 de Abr. de 2020
Diane, It is not because if a large dataset. It will happen if there is a vertical line in your dataset. For example, consider these small vectors
x = [0 1 1 1 2];
y = [0 1 2 3 4];
g = gradient(y,x);
but the gradient still become infinity at some points
g =
1 2 Inf 2 1

Iniciar sesión para comentar.


Star Strider
Star Strider el 20 de Abr. de 2020
The gradient function second argument is the uniform spacing constant (in the documentation, ‘h’, assumed to be 1 by default if not provided).
Regardless of the spacing of the independent variable vector, and whether it is uniform or not, dividing the gradient of the dependent variable by the gradient of the independent variable will give the correct result for the numerical derivative of the dependent variable with respect to the independent variable.
For example, using the ‘fig.fig’ file:
F = openfig('fig.fig');
Lines = findobj(F, 'Type','Line'); % Find ‘Line’ Objects
for k = 1:numel(Lines)
x{k} = Lines(k).XData;
y{k} = Lines(k).YData;
dydx{k} = gradient(y{k}) ./ gradient(x{k}); % Iterate & Calculate Gradient
end
figure
hold on
for k = 1:numel(dydx)
plot(x{k}, y{k}, '-b', x{k}, dydx{k}*10+10, '--c') % Plot Data & Numerical Derivatives
end
hold off
grid
hl = legend('Data', '$\frac{dy}{dx}$');
hl.Interpreter = 'latex';
There is only one 'Line' object in this file, however the code is written to accommodate several. (This code will require minor modifications to use with data that are not extracted from one or more figures.)
I do not know if this will result in infinite results, since I do not have the data that generated those results in the current code.
.

Categorías

Más información sobre Spline Postprocessing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by