Calculate derivatives in a non-uniform grid

43 views (last 30 days)
moreza7
moreza7 on 1 Aug 2019
Answered: Justino Martinez on 15 Jan 2024
I have a non-uniform grid (non-equal intervals between nodes). [x,y]
I also have the data (U) calculated on this grid.
I want to calculate the derivatives of U.
How can I do this?

Answers (3)

Star Strider
Star Strider on 1 Aug 2019
The gradient function is an option, specifically:
dydx = gradient(y) ./ gradient(x);
for vectors, or more generally for matrices:
[dxr,dxc] = gradient(x);
[dyr,dyc] = gradient(y);
dc = dyc./dxc; % Column Derivatives
dr = dyr./dxr; % Row Derivatives
Try that to see if it gives you an acceptable result.
  3 Comments
Star Strider
Star Strider on 1 Aug 2019
I am guessing that your ‘non-uniform grid’ (that I call ‘G’ here) is a matrix, as is ‘U’.
I would do something like this:
[dxr,dxc] = gradient(G);
[dyr,dyc] = gradient(U);
dc = dyc./dxc; % Column Derivatives
dr = dyr./dxr; % Row Derivatives
So ‘dc’ takes the derivatives along the columns, and ‘dr’ along the rows. See the documentation for the gradient function (that I linked to in my Answer) for details.
Walter Roberson
Walter Roberson on 22 Jun 2020
[dux, duy] = gradient(U, x, y);
duy ./ dux
perhaps?

Sign in to comment.


Alessandro Mura
Alessandro Mura on 22 Jun 2020
Edited: Alessandro Mura on 22 Jun 2020
Hi,
the solution is using the Jacobian matrix.
This is used to transform gradients between the coordinate system of (row,column) to (x,y).
First calculate the gradients of U,x and y
[dxi,dxj]=gradient(x);
[dyi,dyj]=gradient(y);
[dui,duj]=gradient(u);
then the Jacobian of the transformation is
[dxi dxj
dyi dyj]
the determinant is
DET=dxi.*dyj- dxj.*dyi;
the inverse of the Jacobian has these 4 coefficients:
JAC11=DET.*dxi;
JAC21=DET.*dyi;
JAC12=DET.*dxj;
JAC22=DET.*dyj;
Then to transform the gradient [dui,duj] into dux, duy you just do:
dux=dui.*JAC11+duj.*JAC12;
duy=dui.*JAC21+duj.*JAC22;

Justino Martinez
Justino Martinez on 15 Jan 2024
I don't know if I have missing something in the notation, but the inverse of the Jacobian for these 4 coefficients shold be
JAC11 = dyj./DET
JAC21 = -dxj./DET
JAC12 = -dyi./DET
JAC22 = dxi./DET
isn't it?

Categories

Find more on Polynomials 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!