What is the difference between these two meshgrid?
10 次查看(过去 30 天)
显示 更早的评论
I'm trying to draw isosurface of function, and my code is
[y,x,z] = ndgrid(linspace(-3,3,30),linspace(-3,3,30),linspace(-3,3,30));
r = sqrt(x.^2 + y.^2 + z.^2);
f = abs(x.* y.* exp(-r)./r);
isosurface(x,y,z,f,0.1);
Then I get the figure like
But If I change the order of the variables in the first line [y,x,z] →[x,y,z],
[x,y,z] = ndgrid(linspace(-3,3,30),linspace(-3,3,30),linspace(-3,3,30));
r = sqrt(x.^2 + y.^2 + z.^2);
f = abs(x.* y.* exp(-r)./r);
isosurface(x,y,z,f,0.1);
The I get the error message and the figure looks like
I don't see any differences on the definition of meshgrid between two codes.
What's the problem?
2 个评论
Mike Garrity
2015-12-17
编辑:Mike Garrity
2015-12-17
I think it's probably this message from isonormals.
Error using interp3 (line 146)
Input grid is not a valid MESHGRID.
Error in isonormals (line 75)
n(:,1)=interp3(x, y, z, nx, verts(:,1), verts(:,2), verts(:,3));
Error in isosurface (line 127)
isonormals(x,y,z,data, p);
The calculation of the normals inside isosurface is pretty picky about the format of the input arrays. It wants the first to be changing on the 1st axis, the 2nd to be changing on the 2nd axis, and the 3rd to be changing on the 3rd axis.
This is why the examples which combine ndgrid and isosurface usually have that odd pattern of getting the output of ndgrid as [y,x,z].
回答(1 个)
sam0037
2015-12-21
Hi,
In ISOSURFACE(X,Y,Z,V,isovalue) function, the arrays X, Y, and Z represent a Cartesian, axis-aligned grid. The coordinate arrays (X, Y, and Z) must be monotonic and conform to the format produced by MESHGRID function. Hence, the second code snippet throws an error since the input to ISOSURFACE are not valid (i.e. X,Y, and Z are in NDGRID format). However, in the first code snippet the arrays X,Y and Z are permuted to convert the arrays from NDGRID format to MESHGRID format and as a result works for the first case.
Refer to the following MATLAB Documentation link to know more about Interpolating Gridded Data in MATLAB:
The section 'Converting Between Grid Formats' in the above documentation link explains how to convert a meshgrid to an equivalent ndgrid.
Thanks,
Shamim
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!