How to plot sphere in sphere coordinates?

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?

 採用された回答

J. Alex Lee
J. Alex Lee 2020 年 10 月 19 日

0 投票

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 件のコメント

Niklas Kurz
Niklas Kurz 2020 年 10 月 21 日
yea, but I wanted to go through the rough way ;)
You could have said additionally that I need to swap plot3(x,y,z) with mesh(x,y,z), but all in all you've been nudging me in the right direction. Thx
J. Alex Lee
J. Alex Lee 2020 年 10 月 21 日
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

サインインしてコメントする。

その他の回答 (1 件)

Bruno Luong
Bruno Luong 2020 年 10 月 21 日

1 投票

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 件のコメント

J. Alex Lee
J. Alex Lee 2020 年 10 月 21 日
nice. might be confusing depending on the audience though :)
As a side note, I wish plot also supported "implicit expansion" so you don't have to make the second change. As long as we have committed to the silent syntax, why not go the full nine yards!
Niklas Kurz
Niklas Kurz 2020 年 10 月 21 日
What a hoot! Just one thing: what does
.'
stand for?
Bruno Luong
Bruno Luong 2020 年 10 月 21 日
Click on .'

サインインしてコメントする。

カテゴリ

製品

質問済み:

2020 年 10 月 18 日

コメント済み:

2020 年 10 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by