在 Matlab 中绘制球坐标系
Plotting spherical coordinate system in Matlab
我想在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;
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
和 figure
属性完成其余工作。
由于提供给我的有用答案,我现在制作了一些我很满意的东西。我确实使用了稍微不同的方法来绘制弧线。
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')
剧情如下:
我想在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;
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
和 figure
属性完成其余工作。
由于提供给我的有用答案,我现在制作了一些我很满意的东西。我确实使用了稍微不同的方法来绘制弧线。
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')
剧情如下: