![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1372449/image.png)
gradient with irregular grid
20 views (last 30 days)
Show older comments
Alessio Colella
on 2 May 2023
Answered: Star Strider
on 2 May 2023
I have as a variable the temperature at a given depth and longitude, so it is a vector (317,1) where the 317 are all latitudes.
How do I get the gradient of this variable (which I call T) knowing that the latitude grid is irregular?
I can try
gradient_T=gradient(T,e1v) where e1v is the irregular vector?
0 Comments
Accepted Answer
Star Strider
on 2 May 2023
The gradient function assumes a fixed step size for the second argument.
The way I calculate the numerical derivative using an irregular grid for the reference (assuming vectors here) is:
gradient_T = gradient(T) ./ gradient(e1v);
That will essentially calculate
and generally produces the result I want.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1372449/image.png)
.
0 Comments
More Answers (1)
Nathan Hardenberg
on 2 May 2023
You can give the positions of the corresponding values as the second function argument. In your case the lattitude for each temperature measurement.
temperature = [30 29 28 27 26 25 24 23];
latitude = [1 2 3 4 5 10 11 20];
T = gradient(temperature, latitude)
0 Comments
See Also
Categories
Find more on Earth, Ocean, and Atmospheric Sciences in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!