Meshgrid multi-valued function data
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ahmed Zankoor
am 12 Jun. 2018
Bearbeitet: Ahmed Zankoor
am 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 Kommentare
Rik
am 12 Jun. 2018
You can best think of meshgrid as returning coordinate grids. That means that you can use it as an input to a function that returns an non-scalar, but you'll have to use cellfun or a similar function.
sphere does not use meshgrid internally. The reason the vector outputs of sphere are not in a meshgrid format seems simple: you'dd have way too many points that are useless. You can have a peek by typing edit sphere, although I'dd urge you never to edit internal functions.
Akzeptierte Antwort
Rik
am 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 Kommentar
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Surface and Mesh Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!