Is gradient() similar to diff()?

198 次查看(过去 30 天)
Madan Kumar
Madan Kumar 2021-2-14
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.

回答(1 个)

Walter Roberson
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
z = 1×10
0 10 20 30 40 50 60 70 80 90
T = sort(randn(1,10),'descend')
T = 1×10
1.0808 0.6418 0.3712 -0.1580 -0.3233 -0.5326 -0.7267 -0.9016 -1.4800 -2.4100
diff(T)./diff(z)
ans = 1×9
-0.0439 -0.0271 -0.0529 -0.0165 -0.0209 -0.0194 -0.0175 -0.0578 -0.0930
gradient(T,z)
ans = 1×10
-0.0439 -0.0355 -0.0400 -0.0347 -0.0187 -0.0202 -0.0184 -0.0377 -0.0754 -0.0930
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
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')
T = 5×10
1.8891 1.8149 1.3954 1.1279 0.7642 0.3374 -0.1439 -0.2334 -0.3774 -0.5378 2.0249 1.9570 1.6639 1.5411 1.4652 0.3243 0.3205 -0.2707 -1.2119 -1.4535 1.3176 1.3130 0.7936 0.2390 0.0887 -0.1051 -0.2768 -0.4223 -0.5113 -1.2401 0.9824 0.9276 0.8228 0.3431 0.3212 0.2969 0.2691 -0.1237 -0.6026 -0.9944 2.1536 1.8203 0.7512 0.5212 0.0765 0.0048 -0.0199 -0.5206 -0.8672 -1.4342
diff(T,[],2)./diff(z)
ans = 5×9
-0.0074 -0.0419 -0.0268 -0.0364 -0.0427 -0.0481 -0.0090 -0.0144 -0.0160 -0.0068 -0.0293 -0.0123 -0.0076 -0.1141 -0.0004 -0.0591 -0.0941 -0.0242 -0.0005 -0.0519 -0.0555 -0.0150 -0.0194 -0.0172 -0.0145 -0.0089 -0.0729 -0.0055 -0.0105 -0.0480 -0.0022 -0.0024 -0.0028 -0.0393 -0.0479 -0.0392 -0.0333 -0.1069 -0.0230 -0.0445 -0.0072 -0.0025 -0.0501 -0.0347 -0.0567
size(T), size(L), size(z)
ans = 1×2
5 10
ans = 1×2
1 5
ans = 1×2
1 10
[Gz, GL] = gradient(T,z,L)
Gz = 5×10
-0.0074 -0.0247 -0.0343 -0.0316 -0.0395 -0.0454 -0.0285 -0.0117 -0.0152 -0.0160 -0.0068 -0.0181 -0.0208 -0.0099 -0.0608 -0.0572 -0.0298 -0.0766 -0.0591 -0.0242 -0.0005 -0.0262 -0.0537 -0.0352 -0.0172 -0.0183 -0.0159 -0.0117 -0.0409 -0.0729 -0.0055 -0.0080 -0.0292 -0.0251 -0.0023 -0.0026 -0.0210 -0.0436 -0.0435 -0.0392 -0.0333 -0.0701 -0.0650 -0.0337 -0.0258 -0.0048 -0.0263 -0.0424 -0.0457 -0.0567
GL = 5×10
0.1358 0.1421 0.2684 0.4132 0.7010 -0.0131 0.4643 -0.0373 -0.8345 -0.9157 -0.2857 -0.2510 -0.3009 -0.4445 -0.3378 -0.2212 -0.0665 -0.0944 -0.0670 -0.3512 -0.5213 -0.5147 -0.4205 -0.5990 -0.5720 -0.0137 -0.0257 0.0735 0.3047 0.2295 0.4180 0.2537 -0.0212 0.1411 -0.0061 0.0550 0.1284 -0.0491 -0.1779 -0.0970 1.1712 0.8927 -0.0717 0.1782 -0.2446 -0.2921 -0.2890 -0.3969 -0.2646 -0.4398
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.

请先登录,再进行评论。

类别

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

产品


版本

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by