MATLAB Answers

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

조회 수: 3(최근 30일)
Hayley Dylla
Hayley Dylla 2014년 12월 3일
답변: Marco Castelli 2014년 12월 3일
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!

답변(1개)

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

Community Treasure Hunt

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

Start Hunting!

Translated by