MATLAB Answers

How do I plot a toroid in MATLAB?

358 views (last 30 days)
I would like to plot a toroid in MATLAB but MATLAB does not have a built in function to do this.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
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);
hold on;
[X,Y,Z] = pol2cart(T,R,Z_bottom);
axis equal
shading interp

Sign in to comment.

More Answers (1)

Alex Pedcenko
Alex Pedcenko on 5 Nov 2017
Edited: Alex Pedcenko on 27 Sep 2019
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:
% now we generate n x n matrices for x,y,z according to eqn of torus
surf(x,y,z); % plot surface
daspect([1 1 1]) % preserves the shape of torus
colormap('jet') % change color appearance

Sign in to comment.


No tags entered yet.


Community Treasure Hunt

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

Start Hunting!

Translated by