Finding the gradient of a slope

35 次查看(过去 30 天)
Diane Scicluna
Diane Scicluna 2020-4-18
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 个评论
John D'Errico
John D'Errico 2020-4-18
编辑:John D'Errico 2020-4-18
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.

请先登录,再进行评论。

回答(2 个)

Ameer Hamza
Ameer Hamza 2020-4-18
You can use gradient(): https://www.mathworks.com/help/matlab/ref/gradient.html with mean().
For example
cooling_rate = mean(gradient(time,temperature))
  2 个评论
Diane Scicluna
Diane Scicluna 2020-4-20
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 2020-4-20
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

请先登录,再进行评论。


Star Strider
Star Strider 2020-4-20
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.
.

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by