Finding the gradient of a slope

조회 수: 34 (최근 30일)
Diane Scicluna
Diane Scicluna 2020년 4월 18일
답변: Star Strider 2020년 4월 20일
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