3.4 Parametric Surfaces in Matlab: Sketch The Surface Defined by The Parametric Equations
3.4 Parametric Surfaces in Matlab: Sketch The Surface Defined by The Parametric Equations
Note that x, y, and z are defined in terms of two parameters, r and θ, which
are restriced to 0 ≤ r ≤ 1 and 0 ≤ θ ≤ 2π. The first task is to create a grid of
(r, θ) pairs, after which each pair (r, θ) will be mapped to a triplet (x, y, z) on the
surface via the parametric equations (3.1).
First, use the linspace commands to create vectors r and θ.
r=linspace(0,1,30);
theta=linspace(0,2*pi,30);
When using Matlab’s mesh command, too few grid points will create a surface
with the “Jaggies,” but too many grid points will create a mesh that appears
to be a solid mass of color, and you’ll be unable to distinguish grid lines on the
surface. The number 30 usually provides a nice number of grid points to start
your exploration, then you can adjust upward or downward from there.
The next step is to create the grid of (r, θ) pairs with Matlab’s meshgrid
command.
[r,theta]=meshgrid(r,theta);
If you remove the semicolons in each of the previous commands, you’ll note that
r and θ were first vectors, but after the meshgrid command, r and θ are matri-
ces. If your find this overwriting using the same variable distasteful, you can try
something like [R,THETA]=meshgrid(r,theta) instead. However, we have no
further use of the vectors r and θ, so we are perfectly happy overwriting r and θ
with the matrix output of the meshgrid command.
At this point, each row of the matrix r contains the contents of the former
vector r, and each column of the matrix θ contains the contents of the vector θ. If
you mentally superimpose the matrix θ atop the matrix r, you can imagine a two
dimensional grid of (r, θ) pairs. We now use the parametric equations (3.1) to
compute the triplets (x, y, z) at each pair (r, θ) in the grid. Note that this requires
the use of array operators, as one would expect.
x=r.*cos(theta);
y=r.*sin(theta);
z=r;
Each triplet (x, y, z) is a point on the surface. We can use the mesh command
to connect neighboring points with line segments.
mesh(x,y,z)
We orient the axis, “tighten” the plot, and turn the box on to provided some
depth to the visualization.
view(135,30)
axis tight
box on
Finally, labeling the axes helps us to visualize the orientation, as we can clearly
see in Figure 3.1.
xlabel(’x-axis’)
ylabel(’y-axis’)
zlabel(’z-axis’)
Section 3.4 Parametric Surfaces in Matlab 221
phi=linspace(0,pi,30);
theta=linspace(0,2*pi,30);
[phi,theta]=meshgrid(phi,theta);
Use the parametric equations (3.2) to calculate surface triplets (x, y, z) at each
grid pair (φ, θ). Again, array operators are required.
222 Chapter 3 Plotting in Matlab
x=sin(phi).*cos(theta);
y=sin(phi).*sin(theta);
z=cos(phi);
We can now create a mesh of the surface with Matlab’s mesh command.
mesh(x,y,z)
We adjust the orientation, turn the box on to add depth of visualization, then
issue axis equal command to show that the surface is actually a sphere, and not
an ellipsoid.
axis equal
view(135,30)
box on
Finally, we annotate the axes to produce the final image in Figure 3.2.
xlabel(’x-axis’)
ylabel(’y-axis’)
zlabel(’z-axis’)T
u=linspace(0,6*pi,60);
v=linspace(0,2*pi,60);
[u,v]=meshgrid(u,v);
Use the parametric equations (3.3) to compute (x, y, z) triplets at each (u, v)
pair in the grid. Again, array operators are expected.
x=2*(1-exp(u/(6*pi))).*cos(u).*cos(v/2).^2;
y=2*(-1+exp(u/(6*pi))).*sin(u).*cos(v/2).^2;
z=1-exp(u/(3*pi))-sin(v)+exp(u/(6*pi)).*sin(v);
mesh(x,y,z)
224 Chapter 3 Plotting in Matlab
Adjust the orientation, set axis equal, and turn the box on to add depth to the
visualization.
view(160,10)
axis equal
box on
Annotate the axes in the usual manner to produce the final “seashell” in Figure 3.3.
Matlab also offers a surf command which colors the patches between the gridlines.
We need only execute surf(x,y,z) to see the effect in Figure 3.4(a).
surf(x,y,z)
You might want to turn off hidden line removal and use the rotation tool on the
figure toolbar to turn and twist the figure.
hidden off
Finally, Matlab offers truly stunning graphics capabilities. We can use the surf
command in combination with edge and face coloring techniques, add lighting,
and turn the axes off to produce an image in Figure 3.4(b) that is quite beautiful.
Section 3.4 Parametric Surfaces in Matlab 225
surf(x,y,z,...
’FaceColor’,’interp’,...
’EdgeColor’,’none’,...
’FaceLighting’,’phong’)
camlight left
view(160,10)
axis equal
axis off
(a) (b)
Figure 3.4. A surface plot instead of a mesh colors the patches between meshlines on
the surface. Adding lighting makes for a beautiful image.
Parametric surfaces are just too much fun! Let’s look at another example.
u=linspace(0,2*pi,30);
v=linspace(-1,1,15);
[u,v]=meshgrid(u,v);
Use the parametric equations (3.4) to compute surface triplete (x, y, z) at each
(u, v) in the grid.
z=(r+v/2.*cos(u/2)).*cos(u);
y=(r+v/2.*cos(u/2)).*sin(u);
x=v/2.*sin(u/2);
surf(x,y,z)
Choose the pink colormap 2 set the axis equal, and add box on to help with
the depth of the visualization.
box on
axis equal
colormap pink
Finally, we set the orientation in order to compare our result in Figure 3.5(a)
with the famous Mobius Strip drawn by Escher in Figure 3.5(b).
You can create a Mobius strip on your own. Simply take a long strip of paper,
maybe 1-2 inches wide by 12-24 inches in length. Twist the strip once, then glue
the ends together to match what you see in Figures 3.5(a) and (b). As the ants
walk along the strip, they quickly learn that the strip has only one side, not two.
The Mobius Strip is an important example that is closely studied in a subject
called differential geometry.
2 For information on the available colormaps in Matlab, type help graph3d at the command
prompt.
Section 3.4 Parametric Surfaces in Matlab 227
(a) (b)
Figure 3.5. The classic Mobius Strip has only one side.
r=1;
u=linspace(0,2*pi,60);
v=linspace(0,2*pi,60);
[u,v]=meshgrid(u,v);
Calculate surface triplets (x, y, z) at each grid pair (u, v), using equations (3.5).
228 Chapter 3 Plotting in Matlab
x=(r+cos(u/2).*sin(v)-sin(u/2).*sin(2*v)).*cos(u);
y=(r+cos(u/2).*sin(v)-sin(u/2).*sin(2*v)).*sin(u);
z=sin(u/2).*sin(v)+cos(u/2).*sin(2*v);
Use the surf command to draw the surface, a Klein Bottle. We’ll use the default
view. The result is shown in Figure 3.6(a).
surf(x,y,z)
Let the creative juices flow! Do some interpretation of edge and face color, add
some lighting, and voila! You obtain the beautiful image shown in Figure 3.6(b).
(a) (b)
Figure 3.6. A Klein Bottle, one plain, one pretty! Both fascinating!
Here is the code used to do the fancy shading and lighting seen in Figure 3.6(b).
Section 3.4 Parametric Surfaces in Matlab 229
surf(x,y,z,...
’FaceColor’,’interp’,...
’EdgeColor’,’none’,...
’FaceLighting’,’phong’)
camlight left
colormap(hot)
axis off
Note the new choice of colormap, the “hot” colormap, which adds a “heated look”
to the surface of our Klein Bottle. Click the rotate icon on the toolbar, then use
the mouse to twist and turn and explore this fascinating surface!
Finally, check out the hand-made Klein Bottle in Figure 3.7 crafted by an
artist.
3.4 Exercises
1. Sketch the Enneper’s Surface de- 4. Sketch the Hyperboloid of One Sheet
fined by the parametric equations defined by the parametric equations
x = u − u3 /3 + uv 2 x = a cosh(u) cos v
3 2 y = b cosh u sin v
y = v − v /3 + u v
z = u2 − v 2 , z = c sinh u,
7. Sketch the Whitney Umbrella de- minus or positive infinity. Keep this
fined by the parametric equations in mind when selecting a vector v to
be used in an eventual grid of (u, v)
x = uv
pairs. Experiment with different val-
y=u ues of the constant a.
z = v2,
11. Sketch Helicoid defined by the
where −1 ≤ u, v ≤ 1. You will want parametric equations
to use axis equal on this exercise.
x = av cos u
8. Sketch the Steiner Surface defined y = av sin u
by the parametric equations
z = bu,
x = sin 2u cos2 v
where 0 ≤ u ≤ 2π and −d < v < d,
y = sin u sin 2v d > 0. Experiment with different val-
z = cos u sin 2v, ues of the constants a, b, and d, then
write a short description explaining
where 0 ≤ u ≤ π and −π/2 ≤ v ≤
how varying each of the constants a,
π/2. You will want to use axis equal
b, and d affects the surface.
on this exercise.
12. Sketch Catenoid defined by the
9. Sketch the Pseudosphere defined
parametric equations
by the parametric equations
x = cos u sin v x = a cos u cosh(v/a)
y = sin u sin v y = a sin u cosh(v/a)
z = cos v + ln(tan(v/2)), z = v,
10. Sketch Dini’s Surface defined by 13. Sketch Torus defined by the para-
the parametric equations metric equations
d = a (w cosh(au))2 + (a sin(wv))2 .
3.4 Answers
1. Create the grid and calculate x, 3. Set some constants and create the
y, and z at each (u, v) pair of the grid. grid.
mesh(x,y,z)
hold on
c=-1;
x=a*sinh(u).*cos(v);
y=b*sinh(u).*sin(v);
z=c*cosh(u);
mesh(x,y,z)
x=a*sinh(u).*cos(v);
y=b*sinh(u).*sin(v);
z=c*cosh(u);
7. Set up the grid.
x=cos(u).*sin(v);
Draw and orient the surface, then y=sin(u).*sin(v);
annotate the plot. z=cos(v)+log(tan(v/2));
axis equal
axis off
view(160,10)
title(’Pseudosphere.’)
surf(x,y,z,...
’FaceColor’,’interp’,...
’EdgeColor’,’none’,...
’FaceLighting’,’phong’)
camlight left
colormap(hot)
axis equal
view(160,10)
title(’Helicoid.’)
u=linspace(0,2*pi,40);
v=linspace(-d,d,40);
[u,v]=meshgrid(u,v);
u=linspace(0,2*pi,40);
v=u;
[u,v]=meshgrid(u,v);
x=(a+b*cos(v)).*cos(u);
y=(a+b*cos(v)).*sin(u);
z=b*sin(v);
surf(x,y,z,...
’FaceColor’,’interp’,...
’EdgeColor’,’none’,...
’FaceLighting’,’phong’)
camlight left
colormap(winter)
axis equal
axis off
view(150,20)
title(’Torus.’)