I want to 3D plot z=sqrt(1-x^2-y^2)
for the region x^2+y^2<=1
in MATLAB.
clear all;
clc;
x=-1:0.05:1;
y=-1:0.05:1;
[X,Y]=meshgrid(x,y);
Z=sqrt(1-X.^2-Y.^2);
mesh(X,Y,Z);
I don't know how to define the region x^2+y^2<=1
, because in mesh
we can't define the region of function. How to define it?
Here's one solution (tested in Octave, but should work in MATLAB). I scale the X
and Y
values to lie in the unit disk.
clear all;
clc;
x=-1:0.05:1;
y=-1:0.05:1;
[X,Y]=meshgrid(x,y);
R = sqrt(1+(min(abs(X),abs(Y))./max(abs(X),abs(Y))).^2);
R(R==0) = 1;
X = X ./ R;
Y = Y ./ R;
Z = real(sqrt(1-X.^2-Y.^2));
mesh(X,Y,Z);