MATLAB: How to calculate the gradient of 3D velocity

gradientvelocity fieldswind

Let's say the "wind.mat" in matlab. I was supposed to get 9 tensors of the velocity gradients, du/dx, du/dy, du/dz, dv/dx, dv/dy, dv/dz, dw/dx, dw/dy, dw/dz.
I calculate following dudx=gradient(u)./gradient(x), dudy=gradient(u)./gradient(y) …
but the gradient(y)=0 and gradient(z)=0 for this equally-spaced mat, results in a infinity value in the du/dy tensor.
Anyone can help me to correct the script for the gradient calculation?

Best Answer

Unfortunately you will have to do something like this:
[dudx,dudy,dudz] = gradient(u,squeeze(x(1,:,1)),squeeze(y(:,1,1)),squeeze(z(1,1,:)));
If you want the gradients of the wind-vector components in wind.mat.
Two additional points to mention about gradient:
1, it does not give a properly centred difference approximation to the gradient for cases where the coordinates are not varying linearly. It just uses a naive centred difference-scheme.
2, the above obviously requires the grid to be plaid.
HTH