How do I plot a toroid in MATLAB?

조회 수: 113 (최근 30일)
MathWorks Support Team
MathWorks Support Team 2009년 6월 27일
답변: DGM 2022년 1월 8일
I would like to plot a toroid in MATLAB but MATLAB does not have a built in function to do this.

채택된 답변

MathWorks Support Team
MathWorks Support Team 2009년 6월 27일
You will need to formulate the x, y, and z-coordinate matrices manually and then plot them using the SURF function.
The SURF and MESH functions accept only one set of x, y, and z-coordinates, but in a toroid, (x,y) ordered pairs can have two corresponding z-coordinates. Therefore, to plot a toroid in MATLAB, you will need to plot the top and bottom halves as two separate surfaces on the same plot. For example:
%%Create R and THETA data
theta = 0:pi/10:2*pi;
r = 2*pi:pi/20:3*pi;
[R,T] = meshgrid(r,theta);
%%Create top and bottom halves
Z_top = 2*sin(R);
Z_bottom = -2*sin(R);
%%Convert to Cartesian coordinates and plot
[X,Y,Z] = pol2cart(T,R,Z_top);
surf(X,Y,Z);
hold on;
[X,Y,Z] = pol2cart(T,R,Z_bottom);
surf(X,Y,Z);
axis equal
shading interp
  댓글 수: 3
Stephen23
Stephen23 2019년 9월 27일
편집: MathWorks Support Team 2020년 1월 2일
"This is not a torus..."
That is correct: it is not a torus.
However it is a toroid, which is what the title and the answer state it is.
It might help to revise the difference between a toroid (what this question and answer are about) and a torus (which is what your comment is about), e.g. from Wikipedia:
or from Wolfram Mathematics http://mathworld.wolfram.com/Toroid.html :
The answer creates a toroid from sine curves, just as it states. It does not create a torus, nor does the answer state that it creates a torus.
Alex Pedcenko
Alex Pedcenko 2019년 9월 27일
you're right

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Alex Pedcenko
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');
torus1.png
  댓글 수: 6
Alex Pedcenko
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
Stephen23 2020년 7월 5일

댓글을 달려면 로그인하십시오.


DGM
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.

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by