# help debugging 3-D plot code

1 view (last 30 days)

Show older comments

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 Comments

### Accepted Answer

Bjorn Gustavsson
on 17 Apr 2021

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 Comments

Bjorn Gustavsson
on 18 Apr 2021

### More Answers (0)

### See Also

### Community Treasure Hunt

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

Start Hunting!