# Solid of Revolution – Cylinders in Matlab

### Revolving a 2D function about the x-axis

 A solid of revolution is generated when a function, for example y = f(x), rotates about a line of the same plane, for example y = 0. We’re going to show some simple experiments in Matlab to create 3D graphs by using the built-in function ‘cylinder’. The built-in function cylinder generates x, y, and z-coordinates of a unit cylinder. You can plot the cylindrical surface or object using instructions surf or mesh.

Naturally, you can always type 'help cylinder' on your command window to see the explanation and examples of this function.

The basic format [X, Y, Z] = cylinder(r) returns the x, y, and z-coordinates of a cylinder using r to define a profile curve. cylinder treats its first argument as a profile curve. It’s important to note that the resulting surface object is generated by rotating the curve about the x-axis, and then aligning it with the z-axis.

Let’s say that we want to plot a section of the trigonometric sine function, from 0 to П/2, and then rotate it about the x-axis. We can do this:

% Clear memory, clean screen, close any figure
clear, clc, close all

% Define our initial profile
x = linspace(0, pi/2, 20);
y = sin(x);

% We draw the profile
subplot(221), plot(x,y), axis equal
title('Original function (profile)')
xlabel(
'x'); ylabel('y');

% We use the cylinder function to rotate and align
% with the z-axis, to produce a 3D solid

[X,Y,Z] = cylinder(y);
subplot(222), surf(X,Y,Z), axis
square
xlabel('z'); ylabel('y'); zlabel('x')

% We can have another view, along the x-axis
subplot(223), surf(X,Y,Z), axis square
xlabel('z'); ylabel('y'); zlabel('x')
view(0,90)

% We produce another image, now a lateral view
subplot(224), surf(X,Y,Z), axis square
xlabel('z'); ylabel('y'); zlabel('x')
view(90,0)

Let’s note that the shape or envelop is what we want. When we rotate the line, the x-limits are lost, and and become 0 to 1. We keep the idea for this 3D image, though.

If we interchange the parameters for the surf function, we can achieve another views. We can play with coordinates and labels. Keep an eye on the axes, values and labels, so you can know what you’re seeing.

% Let's create another figure and plot it from the back
figure
subplot(221), surf(Z,X,Y), axis
square
xlabel('x'); ylabel('z'); zlabel('y')

% A view from the top, showing high y-values
subplot(222), surf(Z,X,Y), axis square
xlabel('x'); ylabel('z'); zlabel('y')
view(0,90)

% A view from the bottom, showing low y-values
subplot(223), surf(Z,X,Y), axis square
xlabel('x'); ylabel('z'); zlabel('y')
view(0,-90)

% A final arbitrary view for this solid of revolution
subplot(224), surf(Z,X,Y), axis square
xlabel('x'); ylabel('z'); zlabel('y')
view(35, 15)

Let’s change the limits and profile function (first lines above), keep the rest of the code as it is, and see the interesting solids of revolution and pretty images that we get:

x = linspace(pi/2, 2*pi, 20);
y = cos(x);

From 'Solid of Revolution' to Matlab Examples home

From 'Solid of Revolution' to '3D Graphs Menu'

 Top Other 3D Graphs

## Related pages

simple interest investment calculatortaylor series tutorialhow to calculate standard deviation in matlabx and y intercept calculator onlinetrapezoid rule matlabsimple compounding calculatormatlab fzero examplewhile loops matlabfor and while loops matlabhow to write function in matlablu decomposition matlab codematlab loglogsolve second degree equation onlinehow to plot lines in matlabpascals triangleprogram to convert binary to octaldecimal to binary examplescompound interest problem solvergray code to binary convertermatlab quadnonlinear equation solver matlabcollatz conjectureirregular polygon area calculatorfinding polynomial rootslinear regression with matlabtower of hanoi codeonline chi square calculatorpascal triangle c codematlab guide examplessolve quadratic equation matlabmatlab visualize 3d matrixdefine optimmatlab density plothow to convert binary numbers to octalmatlab loglogdepreciation of cars calculatormatlab nonlinear curve fittingtaylor series online calculator3d surface plot matlabconvertir de decimal a octaldecimal ascii tablemaking a piecewise function continuousquad matlabcalculate factorialsgray code to binary code converterrevolution of solidsevaluate the definite integral calculatorpiecewise definitionpolynomial root finderconvert string to number matlabdouble factorial calculatorrc circuit problems with solutionhow to calculate salvage value of a carmatlab curvefittingcalculator for factorialscharacter map asciimatlab spherehypotenuse calcroots of quadratic equation calculatormatlab spherematlab graphical user interfacesolving equations in matlabhow to decode ascii codeintegrals in matlabdefine piecewise function matlabnumerical differentiation in matlabfzero function matlaboctal to binary examplesamortization table for mortgagede morgan theorem examplesplot 3d in matlabascii values of alphabets and numbersuniformly distributed random numbers matlabexamples of simple interest and compound interestbinary to octal conversion table