Meshgrid multi-valued function data

9 views (last 30 days)
Ahmed Zankoor
Ahmed Zankoor on 12 Jun 2018
Edited: Ahmed Zankoor on 12 Jun 2018
If I have data z = f(x,y) which have at some (or all) (x,y) coordinates more than a value for z. How can I represent the corresponding z value for the X and Y mesh grid, where [X Y] = meshgrid(x,y). I also wonder how this is handled in sphere function. For example: >> [x y z] = sphere(50) , surf(x,y,z) Since x,y,z meshgrids are not in the normal format of mesh grids?
  2 Comments
Ahmed Zankoor
Ahmed Zankoor on 12 Jun 2018
Edited: Ahmed Zankoor on 12 Jun 2018
Thank you Rik, I got your point for sphere. What I can not understand is the meshgrid part, how can we express (x1,y1,z1) and (x1,x2,z2) using the same mishgrid X,Y matrices. Take this for example:
{radius = [3];
x = linspace(-radius,radius,100);
y = linspace(-radius,radius,100);
[X Y] = meshgrid(x,y);
Z = - sqrt(radius^2 - X.^2 - Y.^2);
Z(imag(Z)~= 0) = 0 ;
surf(X,Y,Z)
Z(imag(Z)~= 0) = 0 ;
surf(X,Y,Z)
end}
What if I want to surf both halves of the sphere ??

Sign in to comment.

Accepted Answer

Rik
Rik on 12 Jun 2018
You should use hold on to plot multiple parts. The code below is a fix to your code.
radius=3;
x = linspace(-radius,radius,100);
y = linspace(-radius,radius,100);
[X,Y] = meshgrid(x,y);
Z = sqrt(radius^2 - X.^2 - Y.^2);
Z(imag(Z)~= 0) = 0 ;
%set coordinates outside of the sphere to the edge
L=(X.^2+Y.^2)>(radius^2);
phi=atan2(Y,X);
X(L)=radius*cos(phi(L));
Y(L)=radius*sin(phi(L));
%put the two halves together
Z = [Z,-Z];
X = [X,X];
Y = [Y,Y];
figure(1),clf(1)
surf(X,Y,Z)
  1 Comment
Ahmed Zankoor
Ahmed Zankoor on 12 Jun 2018
Edited: Ahmed Zankoor on 12 Jun 2018
Thank you so much Rik. So this is my point, meshgrid 2D matrices must be single-valued (single value of z corresponding to each (x,y)), otherwise, we should use multiple surfaces (different Z matrices for the same X and Y).

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!