How to plot sphere in sphere coordinates?

5 views (last 30 days)
Niklas Kurz
Niklas Kurz on 18 Oct 2020
Commented: Bruno Luong on 21 Oct 2020
I'm just trying to plot known sphere coordinates:
function sphere(r)
phi = linspace(0,2*pi);
theta = linspace(0,pi);
x = r*cos(phi).*sin(theta);
y = r*sin(phi).*sin(theta);
z = r*cos(theta);
plot3(x,y,z)
end
However this isn't doing the trick. What have I mathematically confounded?

Accepted Answer

J. Alex Lee
J. Alex Lee on 19 Oct 2020
Your problem is not with conversion or plotting, but defining the coordinates that you want...
[phi,theta] = meshgrid(linspace(0,2*pi),linspace(0,pi));
Also if you didn't know:
  2 Comments
J. Alex Lee
J. Alex Lee on 21 Oct 2020
sure thing. if you decide you don't want the rough way and you haven't seen it, check
[X,Y,Z] = r*sphere(n)
mesh(X,Y,Z)
% surf(X,Y,Z) % filled in faces
Also check out

Sign in to comment.

More Answers (1)

Bruno Luong
Bruno Luong on 21 Oct 2020
function sphere(r)
phi = linspace(0,2*pi);
theta = linspace(0,pi).'; % first change
x = r*cos(phi).*sin(theta);
y = r*sin(phi).*sin(theta);
z = r*cos(theta)+0*phi; % second change, make z same-size 2d array as x and y
plot3(x,y,z);
end
  3 Comments

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!