How can I graph the following parametric functions on a 3d graph?

조회 수: 2 (최근 30일)
Hayley Dylla
Hayley Dylla 2014년 12월 3일
답변: Carlos Guerrero García 2022년 12월 1일
Hi everyone! The three functions are as follows:
  • x=r*cos(theta)sin(phi)
  • y=r*sin(theta)sin(phi)
  • z=4r
the three equations should make a hemisphere. This is what I have:
theta=[0:0.1:6.28];
phi=[0:0.1:6.28];
r=[0:0.1:2];
[THETA,PHI]=meshgrid(theta,phi);
X2=R.*cos(THETA).*sin(PHI);
Y2=R.*sin(THETA).*sin(PHI);
Z2=R.*cos(PHI);
mesh(X2,Y2,Z2)
I know that's obviously not right, but I don't know what to do to make it right. Any help would be appreciated! Thanks!

답변 (2개)

Marco Castelli
Marco Castelli 2014년 12월 3일
If R is a constant the code is:
theta=[0:0.1:6.28];
phi=[0:0.1:6.28];
R=2;
[THETA,PHI]=meshgrid(theta,phi);
X2=R.*cos(THETA).*sin(PHI);
Y2=R.*sin(THETA).*sin(PHI);
Z2=R.*cos(PHI);
mesh(X2,Y2,Z2)
In this case , if you want to see how the surface changes with r, you can use subplot to plot more picture in one window:
theta=[0:0.1:6.28];
phi=[0:0.1:6.28];
R=1;
[THETA,PHI]=meshgrid(theta,phi);
X1=R.*cos(THETA).*sin(PHI);
Y1=R.*sin(THETA).*sin(PHI);
Z1=R.*cos(PHI);
R=2;
[THETA,PHI]=meshgrid(theta,phi);
X2=R.*cos(THETA).*sin(PHI);
Y2=R.*sin(THETA).*sin(PHI);
Z2=R.*cos(PHI);
mesh(X2,Y2,Z2)
figure
hold on
subplot(1,2,1)
mesh(X1,Y1,Z1)
axis([-2 2 -2 2 -2 2])
subplot(1,2,2)
mesh(X2,Y2,Z2)
axis([-2 2 -2 2 -2 2])

Carlos Guerrero García
Carlos Guerrero García 2022년 12월 1일
And...what about an animation?
[THETA,PHI]=meshgrid(0:2*pi/60:2*pi,0:pi/60:pi);
for R=0.1:0.01:2;
mesh(R.*cos(THETA).*sin(PHI),R.*sin(THETA).*sin(PHI),R.*cos(PHI));
title(['R=',num2str(R)]);
axis equal;
axis([-2 2 -2 2 -2 2]);
drawnow;
end

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by