How do I plot a toroid in MATLAB?
显示 更早的评论
I would like to plot a toroid in MATLAB but MATLAB does not have a built in function to do this.
采纳的回答
更多回答(2 个)
Alex Pedcenko
2017-11-5
编辑:Alex Pedcenko
2019-9-27
R=5; % outer radius of torus
r=2; % inner tube radius
th=linspace(0,2*pi,36); % e.g. 36 partitions along perimeter of the tube
phi=linspace(0,2*pi,18); % e.g. 18 partitions along azimuth of torus
% we convert our vectors phi and th to [n x n] matrices with meshgrid command:
[Phi,Th]=meshgrid(phi,th);
% now we generate n x n matrices for x,y,z according to eqn of torus
x=(R+r.*cos(Th)).*cos(Phi);
y=(R+r.*cos(Th)).*sin(Phi);
z=r.*sin(Th);
surf(x,y,z); % plot surface
daspect([1 1 1]) % preserves the shape of torus
colormap('jet') % change color appearance
title('Torus')
xlabel('X');ylabel('Y');zlabel('Z');

6 个评论
Stephen23
2019-9-27
Alex Pedcenko's "Answer" moved here:
you can just add:
hold on
plot3(x,y,z,'o')
by the end of my code, if this is what you meant.
Stephen23
2019-9-27
Sonal Gupta 's comment moved here:
Thanks. This works as needed. How do I make these markers filled? 'MarkerFaceColor' and 'filled' options don't seem to work. Also, is there a way to plot these markers as 3D color balls like this?
Stephen23
2019-9-27
Alex Pedcenko's "Answer" moved here:
Well... you can make them 3D if you generate a set of spheres (see: help sphere) with a small radius and position them at these coordinates, but it will be much slower (your PC will need to render huge number of 3D objects) than this method above.
e.g
R=5; % outer radius of torus
r=2; % inner tube radius
th=linspace(0,2*pi,18); % e.g. 18 partitions along perimeter of the tube
phi=linspace(0,2*pi,36); % e.g. 36 partitions along azimuth of torus
% we convert our vectors phi and th to [n x n] matrices with meshgrid command:
[Phi,Th]=meshgrid(phi,th);
% now we generate n x n matrices for x,y,z according to eqn of torus
x=(R+r.*cos(Th)).*cos(Phi);
y=(R+r.*cos(Th)).*sin(Phi);
z=r.*sin(Th);
mesh(x,y,z,'EdgeColor','k'); % plot surface
daspect([1 1 1]) % preserves the shape of torus
%colormap('jet') % change color appearance
title('Torus')
xlabel('X');ylabel('Y');zlabel('Z');
%% spheres:
hold on
[X Y Z]=sphere(10);
R=0.15; % size of the spheres
lighting gouraud
for i = 1:numel(x)
surf(R*X+x(i),R*Y+y(i),R*Z+z(i),'FaceColor',[0.2 0.2 0.2],'FaceAlpha',0.8,'FaceLighting','gouraud','EdgeColor','none');
end
camlight
Alex Pedcenko
2019-10-4
Well,
you then need to plot torus itself with no lines with the same grid as before, e.g.
surf(x,y,z); % plot surface
shading interp % dont show lines of the "mesh"
But afterwards generate coodinates of your three (circular) lines in x-y plane, plot them on top of the torus surface and then plot spheres (if you still need them) at the nodes of these three lines.
Alex Pedcenko
2020-7-5
How about 3D spiral?
R=5; % outer radius of torus
a=1; % inner tube smaller radius
b=1; % inner tube larger radius
p=0.5; % pitch in z-direction
N=10; %turns along z
th=linspace(0,2*pi,36); % e.g. 36 partitions along perimeter of the tube
phi=linspace(0,N*2*pi,36*N); % e.g. 18 partitions along azimuth of torus
% we convert our vectors phi and th to [n x n] matrices with meshgrid command:
[Phi,Th]=meshgrid(phi,th);
% now we generate n x n matrices for x,y,z according to eqn of torus
x=(R+a.*cos(Th)).*cos(Phi);
y=(R+b.*cos(Th)).*sin(Phi);
z=a.*sin(Th)+p*Phi;
surf(x,y,z); % plot surface
daspect([1 1 1]) % preserves the shape of torus
colormap('jet') % change color appearance
%shading interp
title('Not a Torus')
xlabel('X');ylabel('Y');zlabel('Z');

Stephen23
2020-7-5
Looks good... please animate and upload a GIF!
DGM
2022-1-8
MATLAB may not have a built-in function, but that doesn't mean there aren't any functions out there that can conveniently do the work.
I'm sure this isn't the only thing on the File Exchange, but it's the one I use. Syntax is similar to sphere() or ellipsoid(), returning three matrices which can be fed to surf() or mesh(). The input arguments are the center location, radii, order, and number of points.
center = [0 0 0];
radius = [1 1 1 3];
order = 2;
npoints = 100;
[x y z] = supertoroid(center,radius,order,npoints);
surf(x,y,z)
shading flat
axis equal
colormap(parula)
view(-16,27)
camlight

As axis orders are independent and user-defined, the profile and sections do not have to be circular, but can be any superellipse:
radius = [1 1 2 3];
order = [5 3];

radius = [1 1 1 3];
order = [0.8 4];

Also included is a generalized superellipsoid tool.
类别
在 帮助中心 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!