help debugging 3-D plot code

조회 수: 1 (최근 30일)
Brendan Clark
Brendan Clark 2021년 4월 16일
댓글: Bjorn Gustavsson 2021년 4월 18일
I'm trying to figure out why this ellipsoid plot isn't working the way it's supposed to.
It seems that the grid direction may be wrong, but every way that I've tried to change it changes the plot in other undesirable ways as well.
I'm not allowed to use the ellipsoid function, I have to use meshgrid, its very close to correct, but isn't.
The code thus far is.
clear variables
clc
[phi,theta] = meshgrid(linspace(0,2*pi,20),(linspace(0,pi,20)));
figure(1)
a=1;
b=.5;
x=a*cos(phi).*cos(theta);
y=b*cos(phi).*sin(theta);
z=b*sin(phi);
surf(x,y,z);
hold on
AZ=-13;
EL=28;
view(AZ,EL)
xlabel('x')
ylabel('y')
zlabel('z')
title('Ellipsoid')
set(gca, 'XTick',[-0.8,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,0.8])
set(gca, 'YTick',[-0.4,-0.2,0,0.2,0.4])
when run the code produces
what I need is
  댓글 수: 3
Brendan Clark
Brendan Clark 2021년 4월 16일
ope, good catch
its supposed to be
x=a*cos(φ)sin(θ);
y=b*cos(φ)sin(θ);
z=b*sin(φ);
when i corrected the cos to sin that was incorrect I ended up with a 2d graph on the 3d plan, what were the corrections that you made?
Clayton Gotberg
Clayton Gotberg 2021년 4월 17일
Check your new equations again! Now, x and y are defined the same.

댓글을 달려면 로그인하십시오.

채택된 답변

Bjorn Gustavsson
Bjorn Gustavsson 2021년 4월 17일
You have to get your equations for x, y and z right:
x = a*cos(phi).*sin(theta);
y = b*sin(phi).*sin(theta);
z = b*cos(theta);
surf(x,y,z);
HTH
  댓글 수: 2
Brendan Clark
Brendan Clark 2021년 4월 18일
Thank you. This would be the third typo I've found in my homework assignment for this week. What I posted for the equations with x and y being the same was copy and pasted from my hw. The right equation did the trick.
Bjorn Gustavsson
Bjorn Gustavsson 2021년 4월 18일
When I have these sorts of typo-problems I find that printing the code out on paper and annotate it by hand with circling in, crossing over and connecting variables and statements to be helpful. If you see what you think that you've written and not what you've actually written then that helps - sometimes.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by