2016-08-23 1153 views
0

我想在Matlab中繪製球座標系。在Matlab中繪製球座標系

這是我想要的圖像的一種創造:

This is what I'd like to create in matlab.

可能有人給我一些提示? (到目前爲止,我已經繪製的直角座標系)

這是我曾嘗試自己:

hold on 
x0=0; 
y0=0; 
z0=0; 

plot3(x0+[0, 1, nan, 0, 0, nan, 0, 0], y0+[0, 0, nan, 0, 1, nan, 0, 0], z0+[0, 0, nan, 0, 0, nan, 0, 1])  
text([x0+1, x0, x0], [y0, y0+1, y0], [z0, z0, z0+1], ['X';'Y';'Z']); 

r=0.5; 
[x,y,z] = sphere(100); 
hsurf = surf(x*r, y*r, z*r); 
axis equal; 
+1

提示:創建一個從笛卡爾到球面座標的轉換矩陣,並繪製笛卡爾的。關於在SO上提問的提示:請列出更多你自己嘗試過的內容,最好是用[mcve]中的代碼,以便人們知道你實際上已經嘗試過。在這種情況下,他們更有可能提供幫助。 – Adriaan

+0

好的,謝謝。我已經添加了我迄今管理的內容=) 我會考慮你的提示併發布我得到的內容。 – lily23

回答

0
function[]=SphereToCartesian(r,theta,phi) 
%% plot cartesian coordinates: 
plot3([0 0 0;r 0 0],[0 0 0;0 r 0],[0 0 0;0 0 r],'k'); 

%% plot the ball 
line('xdata',sphcart(r,theta,phi,'x'),'ydata',sphcart(r,theta,phi,'y'),'zdata',sphcart(r,theta,phi,'z'),'marker','.','markersize',5); 

%% Plot the arm 
line('xdata',[0 sphcart(r,theta,phi,'x')],'ydata',[0 sphcart(r,theta,phi,'y')],'zdata',[0 sphcart(r,theta,phi,'z')],'linestyle','--'); 

%% Plot the projections 
line('xdata',[0 sphcart(r,theta,phi,'x')],'ydata',[0 sphcart(r,theta,phi,'y')],'zdata',[0 0],'linestyle','--'); 

%% Plot the arcs 
thetas=[0:0.1:theta theta]; 
line('xdata',sphcart(.1*r,thetas,phi,'x'),'ydata', sphcart(.1*r,thetas,phi,'y'),'zdata',sphcart(.1*r,thetas,phi,'z')); 

%% Labels 
text(sphcart(r,theta,phi,'x'),sphcart(r,theta,phi,'y'),sphcart(r,theta,phi,'z'),'r (x,y,z)') 

%% transform 
function[OUT]=sphcart(R,THETA,PHI,COORD) 
    if strcmpi(COORD,'x') 
    OUT=R.*cos(THETA).*cos(PHI); 
    elseif strcmpi(COORD,'y') 
    OUT=R.*cos(THETA).*sin(PHI); 
    elseif strcmpi(COORD,'z') 
    OUT=R.*sin(THETA) 
    else 
    disp('Wrong coordinate!'); 
    OUT=nan; 
    end 
end 
end 

其餘的你可以做使用axesfigure性能。

+0

很好的答案,謝謝! 當我使用此功能時,「繪製弧線」部分不起作用,並且警告顯示「一個或多個以下屬性的值出錯:XData ZData 陣列形狀或大小錯誤」 您是否遇到此問題還是爲你繪製弧線? – lily23

+0

我在Matlab 2013b上試了一下,它工作。你的'theta'和'phi'角度是弧度還是度數? – Crowley

+0

我正在使用弧度。 我切換到Matlab 2013b,發現錯誤現在說 「警告:行XData長度(9),YData長度(9)和ZData長度(1)必須等於」 這是發生在電弧繪圖正在發生。我用r = 1,phi = pi/4,theta = pi/4來測試。 – lily23

0

我現在生產的東西我很高興得益於提供給我的有用答案。我確實使用了稍微不同的方法繪製弧線。

hold on 
r =1; 
phi = pi/4; 
theta = pi/4; 

%% plot cartesian coordinates: 
x0=0; 
y0=0; 
z0=0; 

plot3(x0+[0, .8, nan, 0, 0, nan, 0, 0], y0+[0, 0, nan, 0, .8, nan, 0, 0], z0+[0, 0, nan, 0, 0, nan, 0, .8],'k')  
text([x0+.85, x0, x0], [y0, y0+.8, y0], [z0, z0, z0+.85], ['$x$';'$y$';'$z$'],'FontSize',14, 'Interpreter','latex'); 
%% plot the ball 
line('xdata',sphcart(r,theta,phi,'x'),'ydata',sphcart(r,theta,phi,'y'),'zdata',sphcart(r,theta,phi,'z'),'marker','.','markersize',5); 

%% Plot the arm 
line('xdata',[0 sphcart(r,theta,phi,'x')],'ydata',[0 sphcart(r,theta,phi,'y')],'zdata',[0 sphcart(r,theta,phi,'z')]); 

%% Plot the projections 
line('xdata',[0 sphcart(r,theta,phi,'x')],'ydata',[0 sphcart(r,theta,phi,'y')],'zdata',[0 0],'linestyle','--'); 

%% Line from xy plane to point 
line('xdata',[sphcart(r,theta,phi,'x') sphcart(r,theta,phi,'x')],'ydata',[sphcart(r,theta,phi,'y') sphcart(r,theta,phi,'y')],'zdata',[0 sphcart(r,theta,phi,'z')],'linestyle','--') 

%% label r 
text(.5,.5,.8,'$r$','FontSize',14, 'Interpreter','latex') 

%% change view point 
az = 100; 
el = 45; 
view(az,el) 

%% get rid of axis labels 
set(gca, 'XTick', [], 'YTick', [], 'ZTick', []) 
set(gca, 'xcolor', 'w', 'ycolor', 'w','zcolor', 'w') ; 
%% arc (xy) 
theta = [0: pi/4*0.0001 :pi/4]; 
phi = linspace(0,0,10001); 
r = linspace(0.25,0.25,10001); 

[X,Y,Z]=sph2cart(theta,phi,r); 

plot3(X,Y,Z,'Color','k'); 

% label arc 
text(.3,0.08,0,'$\theta$','FontSize',14,'Interpreter','latex') 

%% arc down from z 
phi = [pi/4: pi/4*0.0001 :pi/2]; 
theta = linspace(pi/4,pi/4,10001); 
r = linspace(0.25,0.25,10001); 
[X,Y,Z]=sph2cart(theta,phi,r); 

plot3(X,Y,Z,'Color','k'); 

% label arc 
text(.1,.08,0.4,'$\phi$','FontSize',14,'Interpreter','latex') 

這裏的情節: enter image description here