Is gradient() similar to diff()?
167 次查看(过去 30 天)
显示 更早的评论
Hi,
I have altitude(z) profiles of temperature(T). I need to calculate dT/dz. I calculated gradient(T,h),h is step size. Again, I calculated diff(T)./diff(z) . Both results are different. Which one is correct.
0 个评论
回答(1 个)
Walter Roberson
2021-2-14
Notice your diff(T)./diff(z) is one entry shorter than gradient(T,h) (which could have been written gradient(T,z)
z = 0:10:90
T = sort(randn(1,10),'descend')
diff(T)./diff(z)
gradient(T,z)
You will notice they are all different except for the first and the last.
So they are different. So which one is "right" ?
Answer:
dT/dz is a measure of the rate of change of the continuous function T with respect to the independent variable z. But if you only have discrete T measurements, how do you estimate the rate of change?
You cannot get the "true" rate of change because in theory the "true" function might be have a high frequency sine() wave on it that just happens to contribute the same value (such as 0) to each measured point, so to get the "true" rate of change you would need a symbolic representation of the generating function in order to take its derivative using calculus techniques.
So you need to estimate the rate of change by examining the functions and "nearby" values and fitting an approximating function to them and taking the derivative of the approximating function and evaluating the derivative at a location.
diff() versus gradient() is a difference in approximating function.
diff(T)./diff(z) approximates using which takes into account only one adjacent point.
gradient(T,z) approximates in the middle using "Central Differences", which takes into account data from the previous point, the current point, and the next point. If the approximating function resembles the actual function, it would be expected that using Central Differences would be a better representation of the local slope than using only the slope to the next point.
Consider, for example, that if you have three points, then for the middle point you can fit a quadratic function to the three, then take the derivative and evaluate at the location of the central point. You would mostly expect the result to be a better approximation of the local slope than if you had measured only the slope from the second to third point without taking the first point into account.
There are other gradient techniques such as using cubic spline (which involves 5 points). Going much beyond 5 tends to amplify noise too much.
4 个评论
Walter Roberson
2021-2-14
Even in R2015b the documentation says,
- N spacing values (h1,h2,...) specifies the spacing for each dimension of F. Scalar spacing parameters specify a constant spacing for each dimension. Vector parameters specify the coordinates of the values along corresponding dimensions of F. In this case, the length of the vector must match the size of the corresponding dimension.
So vectors of exact coordinates are permitted.
So what is happening for you is that your T is not a vector: it is at least 2 dimensional. And you when you take gradient() you have to supply one h parameter for each dimension, and expect as many outputs as you have dimensions. There is no parameter to restrict it to specific dimensions.
z = 0:10:90;
L = 1:5;
T = sort(randn(length(L),length(z)),2,'descend')
diff(T,[],2)./diff(z)
size(T), size(L), size(z)
[Gz, GL] = gradient(T,z,L)
Note that the first h parameter to gradient() is for hx, which corresponds to the second dimension; that is why the z values (corresponding to second dimension) appear first.
You are not required to use the second gradient for anything.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!