How to compute gradients of a data on a 3D grid?
조회 수: 56 (최근 30일)
이전 댓글 표시
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
채택된 답변
Aykut Satici
2014년 8월 19일
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
Aykut Satici
2014년 8월 20일
편집: Aykut Satici
2014년 8월 20일
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)).
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!