How to compute gradients of a data on a 3D grid?
56 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Pradeep Bhat
el 22 de Jul. de 2014
Comentada: Pradeep Bhat
el 25 de Ag. de 2014
Hi,
I am interested in calculating gradient of a vector, say, f at all points on a 3D grid where I have the values for each components of the vector f. The grid points are not evenly spaced in any of the dimensions. They are, in fact, points corresponding to a meshed geometry. Are there MATLAB programs already written for such an application? If not, what inbuilt functions in MATLAB are useful for this purpose? (I think that the gradent function is not useful here as it requires uniform spacing of the grid points. Is it?)
0 comentarios
Respuesta aceptada
Aykut Satici
el 19 de Ag. de 2014
It is my understanding that you would like to find the gradient of a function f from R^3 to R. If you mean a vector-valued function, i.e., f: R^3 --> R^3, then you would have to find the gradient of each component of this function. In any case, the "gradient" function is indeed useful. If you look at the documentation,
one way to call this function is to specify the spacing for each dimension of f. Therefore, you can write something like this:
h1 = 0.2;
h2 = 0.1;
h3 = 0.05;
x = -3:h1:3;
y = -2:h2:2;
z = 0:h3:1;
[x,y,z] = ndgrid(x,y,z);
f = exp(-z.^2).*cos(x) + sin(y);
gradf = gradient(f,h1,h2,h3)
3 comentarios
Aykut Satici
el 20 de Ag. de 2014
Editada: Aykut Satici
el 20 de Ag. de 2014
Hello Pradeep,
Let us concentrate on one of the component functions of this vector function. Computing the gradient for the whole vector function will then amount to repeating the same procedure.
Do you need the gradient information at the exact points that you have the values of the function on? If not, you can interpolate your function on a uniformly spaced grid and use the "gradient" function on this interpolated function. This solution would look like the following:
x = (-3 + 6*rand(50,1));
y = (-2 + 4*rand(50,1));
z = rand(50,1);
f = exp(-z.^2).*cos(x) + sin(y);
fInterpolated = scatteredInterpolant(x,y,z,f);
h1 = 0.2;
h2 = 0.1;
h3 = 0.05;
xq = -3:h1:3;
yq = -2:h2:2;
zq = 0:h3:1;
[xq,yq,zq] = ndgrid(xq,yq,zq);
fgrid = fInterpolated(xq,yq,zq);
[gradfx,gradfy, gradfz] = gradient(fgrid,h1,h2,h3)
If you would like the gradient at the exact point that you have the value of the function on, then you would have to write a simple difference code. The "fInterpolated" variable is still useful here because with that object you can compute the value of the function at any point in R^3. In particular, it will agree with the value of the function at the sample points. Thus, for example, you can loop over your points and compute the gradient at each of these points. Here I have used the central difference method:
dx = 0.1;
dy = 0.1;
dz = 0.1;
gradfx = zeros(length(f),1);
gradfy = zeros(length(f),1);
gradfz = zeros(length(f),1);
for i = 1:length(f)-1
gradfx(i) = 1/2 * ( fInterpolated(x(i)+dx,y(i),z(i)) - ...
fInterpolated(x(i)-dx,y(i),z(i)) );
gradfy(i) = 1/2 * ( fInterpolated(x(i),y(i)+dy,z(i)) - ...
fInterpolated(x(i),y(i)-dy,z(i)) );
gradfz(i) = 1/2 * ( fInterpolated(x(i),y(i),z(i)+dz) - ...
fInterpolated(x(i),y(i),z(i)-dz) );
end
In this latter approach, for example, gradfx(i) would be the partial derivative of f in the x-direction and [gradfx(i), gradfy(i), gradfz(i)] would be the gradient of f at the point (x(i),y(i),z(i)).
Más respuestas (0)
Ver también
Categorías
Más información sobre Surface and Mesh Plots en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!