The cylinder function in MATLAB generates coordinate data representing the surface of a cylinder. These coordinates are essential for creating three-dimensional visualizations using functions like surf or mesh.
Function Syntax and Behavior
The cylinder function produces x, y, and z coordinates for a unit cylinder. By default, it creates a cylinder with a height of 1 unit and generates 20 equally spaced points around its circumference. The function offers several calling conventions depending on your visualization requirements.
Basic Syntax Variations
The primary syntax forms include:
[X, Y, Z] = cylinder returns coordinates for a cylinder with radius 1, using 20 points around the circumference.
[X, Y, Z] = cylinder(r) uses the vector r to define a profile curve. Each element in r represents the radius at equally spaced heights along the unit height of the cylinder. The function still generates 20 points around the circumference.
[X, Y, Z] = cylinder(r, n) accepts both a profile vector r and specifies n points around the circumference, providing finer control over mesh density.
cylinder(axes_handle, ...) directs the output to a specific axes handle rather than the current axes retrieved by gca.
When called without output arguments, cylinder automatically renders the surface using the surf function.
Creating Cylindrical Surfaces
The following example demonstrates generating a cylinder with specific dimensions and applying visualization enhancements:
clc;
close all;
clear all;
warning off;
% Define cylinder parameters
circumferencePoints = 60;
baseRadius = 3;
totalHeight = 12;
% Generate mesh coordinates
[meshX, meshY, meshZ] = cylinder(baseRadius, circumferencePoints);
meshZ = meshZ * totalHeight;
% Render the lateral surface
surf(meshX, meshY, meshZ, 'EdgeColor', 'none');
% Configure visual properties
colormap('parula');
shading interp;
lighting gouraud;
light('Position', [1, 1, 1]);
axis equal;
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
grid on;
view(45, 30);
Generating Conical Shapes
The same function creates cones by modifying the profile vector. Setting the first radius element to zero produces a pointed apex, transforming the cylinder into a cone:
clc;
close all;
clear all;
warning off;
% Define cone parameters
circumferencePoints = 60;
profileRadii = [0, 2.5];
totalHeight = 12;
% Generate mesh coordinates
[meshX, meshY, meshZ] = cylinder(profileRadii, circumferencePoints);
meshZ = meshZ * totalHeight;
% Render the lateral surface
surf(meshX, meshY, meshZ, 'EdgeColor', 'none');
% Configure visual properties
colormap('copper');
shading interp;
lighting phong;
light('Position', [-1, -1, 0]);
axis equal;
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
grid on;
view(-45, 25);
Practical Considerations
When working with the cylinder function, several factors influence the quality of your visualization. The n parameter controls angular resolution, with higher values producing smoother surfaces but requiring more computational resources. The profile vector determines the cross-sectional shape at each height level, enabling creation of tapered cylinders, cones, and other axisymmetric solids.
Surface rendering benefits significantly from proper lighting configuration. The lighting function supports multiple algorithms, each producing distinct visual characteristics. Gouraud shading works well for curved surfaces, while Phong shading handles specular highlights more accurately. Position your light source carefully to avoid artifacts in the rendered surface.