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?

 Akzeptierte Antwort

Star Strider
Star Strider am 2 Mai 2023

0 Stimmen

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.
.

4 Kommentare

Joshua Port
Joshua Port am 10 Sep. 2025
This would probably work if the grid were irregular in both directions. If one dimension has fixed spacing and the other has irregular spacing, though, the gradient in the direction with the fixed spacing will be Inf since gradient([fixed spacing dimension]) = 0 everywhere.
i may be missing something in your Comment, however it seems to work correctly here. (The gradient function has changed, and now permits the inclusion of the independent variable vectors as arguments, so doing element-wise division as a separate operation is no longer necessary.)
Lonv = sort(rand(1,20))*10 + 117;
Latv = linspace(35, 45, 25);
[Lat,Lon] = ndgrid(Latv, Lonv);
T = exp(-((Lon-122).^2/2.5 + (Lat - 40).^2/3))*10 + 10;
figure
surfc(Lat, Lon, T, EdgeColor='interp')
colormap(turbo)
colorbar
[gradient_T_Lon, gradient_T_Lat] = gradient(T, Lonv, Latv);
figure
surfc(Lat, Lon, gradient_T_Lon)
colormap(turbo)
colorbar
figure
surfc(Lat, Lon, gradient_T_Lat)
colormap(turbo)
colorbar
.
Joshua Port
Joshua Port am 10 Sep. 2025
Bearbeitet: Joshua Port am 10 Sep. 2025
I should have realized what this was getting at. If your data looks like this:
x_1d = linspace(0, 100)';
x = repmat(x_1d, 1, 100);
And you do:
gradient(x)
You'll get 0 everywhere. To compute df/dx you would need to do:
[~, dx] = gradient(x);
dfdx = gradient(f) ./ dx;
This method doesn't work when one of your dimensions is curvy, though. I wish gradient could take in an array of points, even scattered ones, and a function defined at those points, and compute the gradient. I have a method for dealing with this, but it requires a lot of headache.
Star Strider
Star Strider am 10 Sep. 2025
Of course, the derivative of a constant is zero. Beyond that, I am not following your use of the gradient function.
Stopping here.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Nathan Hardenberg
Nathan Hardenberg am 2 Mai 2023

0 Stimmen

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)
T = 1×8
-1.0000 -1.0000 -1.0000 -1.0000 -0.3333 -0.3333 -0.2000 -0.1111

Community Treasure Hunt

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

Start Hunting!

Translated by