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).

2 comentarios

Rik
Rik el 19 de Dic. de 2020
Sounds like you need meshgrid (or ndgrid). What did you try?
Minh Nguyen
Minh Nguyen el 19 de Dic. de 2020
theta = linspace(0,2*pi,30);
phi = linspace(0,pi/2,30);
[theta,phi] = meshgrid(theta,phi);
r=3;
[x,y,z] = sph2cart(theta,phi,r);
surf(x,y,z,'FaceAlpha',0.3)
hold on
x = -3:0.2:3 ;
y = -3:0.2:3 ;
[x,y] = meshgrid(x,y) ;
z = sqrt(x.^2+y.^2);
surf(x,y,z)
I use this code and i can plot two graph 2 z functions but my teacher asked me to plot only the graph below the cone and i dont know how to do it

Iniciar sesión para comentar.

 Respuesta aceptada

Rik
Rik el 19 de Dic. de 2020

0 votos

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)

Más respuestas (0)

Etiquetas

Preguntada:

el 19 de Dic. de 2020

Comentada:

el 19 de Dic. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by