在Matlab中绘制球面坐标系

时间:2016-08-23 08:37:49

标签: matlab plot system coordinate

我想在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;

2 个答案:

答案 0 :(得分:0)

<button onclick="myFunction()"> Click me </button>


 function myFunction()
    {
        document.getElementById('clickCount').setAttribute("class", "style1");
    }

您可以使用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 axes属性执行其余操作。

答案 1 :(得分: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