MATLAB: How can i plot it

surf

using spherical coordinates, plot only the part of the sphere z =sqrt(9-x^2-y^2) that lies below the cone z = sqrt(x^2+y^2).

Best Answer

You have two surfaces:
theta = linspace(0,2*pi,30);
phi = linspace(0,pi/2,30);
[theta,phi] = meshgrid(theta,phi);
r = 3;
[x,y] = sph2cart(theta,phi,r);
z_sphere = sqrt(9-x.^2-y.^2);
z_cone = sqrt(x.^2+y.^2);
%remove all complex elements
z_sphere(imag(z_sphere)>eps)=NaN;z_sphere=real(z_sphere);
They share the same coordinate grid, so now we can do element-wise operations. You need to remove points where z_sphere is higher than z_cone. The easiest way to remove points without messing up the coordinate grid is by marking them with NaN:
z_sphere( z_sphere>z_cone ) = NaN;
surf(x,y,z_cone,'FaceAlpha',0.3),hold on % optional line
surf(x,y,z_sphere)
view(45,45)