Three-Dimensional Graphics, Warped Curves and Surfaces, Contour Graphics
3.1 Three-Dimensional Graphics (3-D), Warped Curves
The basic commands that MATLAB uses to draw graphs that generate a line in three dimensions or warped curves are the following:
Here is an example:
>> x = 0:pi/50:10*pi;
>> y = sin(x);
>> z = cos(x);
>> plot3(x, y, z)
This generates the graph in Figure 3-1.
3.2 Polygons in Three Dimensions
MATLAB also allows for drawing polygons in three dimensions. To do this, use the following commands:
Here is an example:
>> x = cos(0:0.01:8*pi);
>> y = sin(0:0.01:8*pi);
>> z = 0:0.01:8*pi;
>> fill3(x,y,z,'r')
This generates the graph of Figure 3-2.
3.3 Graphics in Parametric 3-D Coordinates
Let’s see here how MATLAB draws curves in parametric coordinates in space, also known as warped curves in parametric coordinates. The fundamental problem is to get graphs of tridimensional functions in which the variable x, y and z depend, in turn, on a parameter t.
The command that can be used is plot3 and all its variants, by suitably defining intervals of variation of the parameter, not by the independent variable as it was until now.
This type of graphic is very useful in certain matters, such as, for example, differential geometry.
Here is an example:
>> t = -4*pi:0.01:4*pi;
>> x = cos(t) .^ 2;
>> y = sin(t) .* cos (t);
>> z = sin(t);
>> plot3(x, y, z)
This generates the graph in Figure 3-3.
3.4 Surfaces, Meshes and Contours
So far we have seen the 3D graphics based on a line that moves in three dimensions to form a warped curve. Now let’s see how 3D graphics corresponding to surfaces and their different variants are made available.
3.5 Surfaces in Explicit Coordinates
Surface graphics allow for dense representations of tridimensional figures, and in particular of functions of two variables. The first step in representing a function of two variables z = f(x,y) using a surface chart, is to use the command meshgrid, which basically defines the array of points (X, Y) on which the function of two variables is evaluated for its graphical representation. Its syntax is as follows:
[X, Y] = meshgrid(x,y)
Parameters x and y are vectors that are replicated into to matrices X and Y. Each matrix will have the same number of rows as the length of x, and the same number of columns as the length of y. The increments of values in each of the vectors determine the “spacing” of the resulting grid. For example, x of [-10:0.1:10] and y of [-2:0.1:2] will result in X of 40 columns and 100 rows. Each columns will have the same sequence of values between -10 and +10. While Y will also have 40 columns and 100 rows, each of the rows will have the same sequence of values between -2 and +2.
The second step is to use the available commands to effect the result, which are as follows:
EXERCISE 3-1
Represent the surface of the slope-intercept form:
-14/2 < x, y < 14/2
Also represent the surface with its contour.
>> [X, Y] = meshgrid(-7.5:.5:7.5);
>> Z = sin(sqrt(X.^2+Y.^2))./sqrt(X.^2+Y.^2);
>> surf(X, Y, Z)
This gives the graph of Figure 3-4.
The surface with the outline (contour) graph is shown in Figure 3-5. The following syntax is used:
>> [X, Y] = meshgrid(-7.5:.5:7.5);
>> Z = sin(sqrt(X.^2+Y.^2))./sqrt(X.^2+Y.^2);
>> surfc(X, Y, Z)
Figure 3-6 shows the chart shaded, using the syntax:
>> [X, Y] = meshgrid(-7.5:.5:7.5);
>> Z = sin(sqrt(X.^2+Y.^2))./sqrt(X.^2+Y.^2);
>> surfl(X, Y, Z)
3.6 Mesh Graphics
A three-dimensional mesh graph is defined by a function z = f(x,y), so that the points on the surface are represented on a grid, result of the z values are given by f(x,y) on corresponding points of the (x, y) plane. The appearance of a mesh chart is like a fishing net, with points on the surface on the nodes of the network. Actually, it is a graph of surface whose graph has the form of a network.
To represent a mesh graph, use the command mesh and its variants, whose syntax are as follows:
EXERCISE 3-2
Represent the mesh graph for the surface of equation:
z = xe (-x ^ 2 - y ^ 2) - 2 < x, y < 2
Also add their contours (a contour chart) and include a curtain.
The syntax presented here gives as a result the graph in Figure 3-7:
>> [X, Y] = meshgrid(-2:.1:2,-2:.1:2);
>> Z = X .* exp(-X.^2 - Y.^2);
>> mesh(X, Y, Z)
Figure 3-8 presents the mesh along with the contour graph (or contour chart) for the syntax as follows:
>> [X, Y] = meshgrid(-2:.1:2,-2:.1:2);
>> Z = X .* exp(-X.^2 - Y.^2);
>> meshc(X, Y, Z)
Finally, Figure 3-9 represent the mesh graphic and curtain option. The syntax is as follows:
>> [X, Y] = meshgrid(-2:.1:2,-2:.1:2);
>> Z = X .* exp(-X.^2 - Y.^2);
>> meshz(X, Y, Z)
3.7 Contour Graphics
Another option for visualizing a function of two variables is to use level curves calls (a system of dimensional planes. These curves are characterized as such because they are the points (x, y) on which the value of f(x,y) is constant. Thus, for example, in the weather charts, level curves representing the same temperature points are called isotherms, and contour lines of equal pressure, isobars. Contour lines, representing heights (values of f(x,y) that are equivalent, can describe surfaces in space. Thus, drawing different contours corresponding to constant heights, can be described as a map of lines on the surface level, MATLAB calls a contour graph. The contour plots can be represented in two and three dimensions.
A map showing the regions of the Earth’s surface, whose contour lines represent the height above the sea level, is called a topographic map. These maps, therefore show the variance of z = f(x,y) with respect to x and y. When the space between contour lines is large, it means that the variation of the variable z is slow, while a small space indicates a rapid change of z.
Commands used in MATLAB for the representation of isographs (contour lines) are as follows:
EXERCISE 3-3
Given the surface of equation:
z = Sine(x) Sine(y) - 2 < x, y < 2
Represent it with its contour. Then represent its two-dimensional outline with 20 graph lines and its three-dimensional outline with 50 chart lines. Also draw the corresponding density chart.
Figure 3-10 shows the graph of the surface with its contour. The syntax is as follows:
>> [X, Y] = meshgrid(-2:0.1:2);
>> Z = sin(X) .* sin(Y);
>> surfc(X, Y, Z)
Figure 3-11 shows the two-dimensional contour graph using 20 lines. The syntax is as follows:
>> [X, Y] = meshgrid(-2:0.1:2);
>> Z = sin(X) .* sin(Y);
>> contour(Z, 20)
Figure 3-12 shows the three-dimensional contour graph using 50 lines. The syntax is as follows:
>> [X, Y] = meshgrid(-2:0.1:2);
>> Z = sin(X) .* sin(Y);
>> contour3(Z, 50)
Finally, Figure 3-13 represents the density graph (with the contour shaded according to different intensities of color). The syntax is as follows:
>> [X, Y] = meshgrid(-2:0.1:2);
>> Z = sin (X) .* sin(Y);
>> pcolor(X, Y, Z)
3.8 Manipulating Three-Dimensional Graphics
There are commands in MATLAB which allow you to change the appearance of the same graph, either by your shader, the scale of its themes, colors, hidden lines, the point of view from which one can observe it, etc. Below, some of these commands are:
EXERCISE 3-4
Given the surface equation:
z = x2 - y2 - 2 < x, y < 2
represent it with strong lighting, dense shadows and gray colors. Using the same axis, show four different points of view and with the shading by default.
>> [X, Y] = meshgrid(-2:0.05:2);
>> surf(X,Y,Z),shading interp,brighten(0.75),colormap(gray(5))
This generates the graph in Figure 3-14.
We will now present in Figure 3-15 the surface focused from four different points of view. The syntax is as follows:
>> [X, Y] = meshgrid(-2:0.05:2);
>> Z = X .^ 2 – Y .^ 2;
>> subplot(2,2,1)
>> surf(X,Y,Z)
>> subplot(2,2,2)
>> surf(X,Y,Z),view(-90,0)
>> subplot(2,2,3)
>> surf(X,Y,Z),view(60,30)
>> subplot(2,2,4)
>> surf (X, Y, Z), view (- 10, 30)
3.9 Parametric Surfaces
MATLAB allows you to represent surfaces whose components depend on specified variations in parameters. To do this, you can use the commands surf and mesh, by properly defining the variables x, and z.
Cylindrical and spherical coordinate surfaces are representable in MATLAB, parameterizing them in advance.
In terms of surfaces of revolution, they always require ranges, which allow their graphical representation with MATLAB.
EXERCISE 3-5
Draw the surface of parametric coordinates:
x = 4cos(r) sec(t) y = 2sine(r) sec(t) z = tan(t) -2π< r <2π, -π< r <π
>> r = [-2*pi:0.1:2*pi]';
>> t = [-pi:0.1:pi];
>> X = 4 * cos(r) * sec(t);
>> Y = 2 * sin(r) * sec(t);
>> Z = ones(size(r)) * tan(t);
>> surf(X,Y,Z)
>> shading interp
Note that “ones” as used above is a function that produces a vector or matrix that is populated by ones. Similarly, “zeros” is populated by zeroes.
The graph is shown in Figure 3-16.
EXERCISE 3-6
Create the graph of the surface of revolution that is turning the function Sine (x) around the Z axis. also create the graph of the surface of revolution rotating the function e ^ x around the Y axis.
To obtain the equation of the surface, on the assumption that the rotation is around the Z axis, consider the graph of the generating curve y = r(z) in the plane YZ. Turning this graph around the Z axis forms a surface of revolution. The sections with flat z = z0 are circles whose RADIUS is r (z0) and equation x2 + y2 = [r(z0)]2. That means that the equation x2 + y2 = [r (z)] 2 describes the points on the surface of revolution. For our problem, we have r (z) = Sine (z) and the curve x2+ y2= Sine [z]2, which are parametric for the purpose of input for MATLAB graphics:
>> r =(0:0.1:2*pi)';
>> t =(-pi:0.1:2*pi);
>> X= cos (r)*sin (t);
>> Y= sin(r)*sin (t);
>> Z= ones (1, size (r))'* t;
>> surf(X,Y,Z), shading interp
This generates the graph in Figure 3-17.
If we propose this MATLAB entry:
r =(0:0.1:2*pi)';
t =(-2:0.1:2);
X= cos (r)*exp (t);
Y= ones(1, size(r))'*t;
Z= sin(r)*exp (t);
surf(X,Y,Z), shading interp
You get the graph in Figure 3-18, which has been found in the same way as the previous one, but rotating the exponential function around the Y axis (the generating curve is now the function e ^ x).
EXERCISE 3-7
Represent the cylinder {t, Sine [t], u}, with {t, 0, 2Pi} and {u, 0, 4} by revolving {Cos [t](3+Cos[u]), Sine [t](3+Cos[u]), Sine [u]}, with {t, 0, 2 Pi} and {u, 0, 2Pi}.
>> t = [0:0.1:2*pi]';
>> r = [0:0.1:4];
>> X = sin(t) * ones(size(r));
>> Y = cos(t) * ones(size(r));
>> Z = ones(size(t)) * r;
>> surf(X,Y,Z)
>> shading interp
You get the graph in Figure 3-19.
To represent the torus of revolution, we use the following syntax:
>> r = [0:0.1:2*pi]’;
>> t = [0:0.1:2*pi];
>> X = [3 + cos(r)] * cos(t);
>> Y = [3 + cos(r)] * sin(t);
>> Z = sin(t)’ * ones(size(t));
>> surf(X,Y,Z)
>> shading interp
We get the graph in Figure 3-20.
3.10 Special Geometric Forms
MATLAB enables commands to generate cylinders and spheres. We have:
As an example, let’s represent the cylinder generated by the curve 4Cos (t) when t varies between 0 and 2π. The syntax will be as follows:
>> t = 0:pi/10:2 * pi;
>> [X, Y, Z] = cylinder(4 * cos(t));
>> surf(X, Y, Z)
This generates the graph of Figure 3-21.
3.11 Other Graphics Handling Options
Graphic characteristics treated so far belong to the high level MATLAB GUI. However, there are low level (Handle Graphics) commands that allow creation and manipulation of figures, axes, lines, surfaces, images, text, menus and other graphics objects. Some of the commands to create graphical objects are:
Each object has a level of hierarchy. The parents of an object are superior in the hierarchy, and children are the objects of a lower hierarchy. The object is created with figure, then, the one created by axes and, finally, and at the same level, are the ones created by image, patch, surface, text and line. This means that if, for example, you want to create a surface, first has to create the figure that is going to be graphed, then the axes, and, finally, the surface itself.
So far we have seen commands that allow you to create objects, but, in addition, all these objects can have properties, as style of line, color, etc. The list of possible properties for each object is very long, and so full knowledge of the list requires consultation of MATLAB Reference manual, http://www.mathworks.com/help/matlab/. As a general rule, the name of a property of an object is a compound word whose components begin with capital letter. For example, the line style property has the name LineStyle. The names of the properties that are mapped by default to an object start with Default, as, for example, DefaultFigureColor, which assigns the color by default to a figure. Below, are some of the most typical properties for the different objects.
Object | Properties | Possible values |
---|---|---|
Figure | Color (background color) | ‘y’, ‘c’, ‘r’, ‘g’, ‘b’, ‘w’, ‘k’ |
ColorMap | hot(p), gray(p), pink(p),.... | |
Position | [left, bottom, width, height] | |
Name | string name | |
MinColorMap (min. color no.) | minimum number of colors for map | |
NextPlot (graph mode.) | new, add, replace | |
NumberTitle (No. in the figure) | on, off | |
Units (units of measurement) | pixels, inches, centimeters, points | |
Resize (size figure with mouse) | on (can be changed), off (no) | |
Axes | Box (box for the chart) | on, off |
Color (color of the axes) | ‘y’, ‘c’, ‘r’, ‘g’, ‘b’, ‘w’, ‘k’ | |
GridLineStyle (line for mesh) | ‘-’, ‘--’, ‘:’, ‘-.’ | |
Position | [left, bottom, width, height] | |
TickLength (length between marks) | a numeric value | |
TickDir (direction of) | in, out | |
Units | pixels, inches, centimeters, points | |
View (view) | [azimuth, elevation] | |
FontAngle (angle of source) | normal, italic, oblique | |
FontName (name of source) | the name of the source font | |
FontSize (font size) | numeric value | |
FontWeight (weight) | light, normal, demi, bold | |
DrawMode (drawing mode) | normal, fast | |
XDir, YDir, ZDir (direction of axes) | normal (growing from left to right), reverse | |
XGrid, YGrid, ZGrid (grids) | on, off | |
XLabel, YLabel, Zlabel (tags) | string containing the text of labels | |
XLim, YLim, ZLim (limit values) | [min, max] (range of variation) | |
XScale, YScale, ZScale (scales) | linear, log (log) | |
XTick,YTick,ZTick | [m1,m2,...] (situation marks on axis) | |
Line | Color (color of the line) | ‘y’, ‘c’, ‘r’, ‘g’, ‘b’, ‘w’, ‘k’ |
LineStyle (line style) | ‘-’ , ‘--’ , ‘:’ , ‘-.’ , ‘+’ , ‘*’, ‘.’ , ‘x’ | |
LineWidth (line width) | numeric value | |
Visible (visible line or not displayed.) | on, off | |
XData, YData, ZData (coordinates) | set of coordinates of the line | |
Text | Color (text color) | ‘y’, ‘, ‘c’, ‘r’, ‘g’, ‘b’, ‘w’, ‘k’ |
FontAngle (angle of source) | normal, italic, oblique | |
FontName (name of source) | the name of the source font | |
FontSize (font size) | numeric value | |
FontWeight (weight) | light, normal, demi, bold | |
HorizontalAlignment (hor setting.) | left, center, right | |
VerticalAlignment (adjust to vert.) | top, cap, middle, baseline, bottom | |
Position (position on screen) | [x, y, z] (point of situation) | |
Rotation (orientation of the text) | 0, ±90, ±180, ±270 | |
Units (units of measurement) | pixels, inches, centimeters, points | |
String | the text string | |
Surface | CData (color of each point) | color matrix |
EdgeColor (color grids) | ‘y’,‘m’,..., none, flat, interp | |
FaceColor (color of the faces) | ‘y’,‘m’,..., none, flat, interp | |
LineStyle (line style) | ‘-’ , ‘--’ , ‘:’ , ‘-.’ , ‘+’, ‘*’, ‘.’ , ‘x’ | |
LineWidth (line width) | numeric value | |
MeshStyle (lines in rows and col.) | row, column, both | |
Visible (visible line or not displayed.) | on, off | |
XData, YData, ZData (coordinates) | set of coordinates of the surface | |
Patch | CData (color of each point) | color matrix |
EdgeColor (color of the axes) | ‘y’, ‘m’,..., none, flat, interp | |
FaceColor (color of the faces) | ‘y’ ,‘m’,..., none, flat, interp | |
LineWidth (line width) | numeric value | |
Visible (visible line or not displayed) | on, off | |
XData, YData, ZData (coordinates) | set of coordinates of the surface | |
Image | CData (color of each point) | color matrix |
XData, YData (coordinates) | set of coordinates of the image |
Among the commands that allow you to perform operations with graphical objects already created are as follows:
Here are some examples:
The following syntax changes the limits of variation of the current X, Y and Z axes to the specified values:
>> set(gca, 'XLim', [0,10], 'YLim', [-25, 25], 'ZLim', [-8,10])
The following syntax places the color of the background of the current figure in white:
>> set(gcf, 'Color', 'w')
The following syntax returns the current properties for the surface previously created and assigned to the variable named surfh:
>> get(surfh)
The following syntax returns the line style to that of the surface surfh:
>> get(surfh, 'LineStyle')
The following syntax deletes the surface surfh:
>> delete(surfh)
EXERCISE 3-8
Represent coordinates of the following parametric surface:
x(t) = 4cos(r) cos(t), y(t) = 2sine(r) cos(t), z (t) = sine(t) -π< r <π , -π/2 < t <π/2
so that it is presented in a figure with title "Parametric Surface" and whose background color is white, with a black colored axis with the surface itself presented in a grid, colored according to the jet colormap, and enclosed in a cube.
>> r = [-pi:0.1:pi]';
t = [-pi/2:0.1:pi/2];
x = 4*cos(r)*cos(t);
y = 2*sin(r)*cos(t);
z = ones(1,size(r))'*sin(t);
surface = surf(x, y, z);
set(surface,'EdgeColor', interp')
set(gcf, 'Color', 'w', 'Name', 'Parametric Surface'),
set(gca,'XColor', 'k', 'YColor', 'k', 'ZColor', 'k', 'Box', 'on'),
Figure 3-22 represents the ordered surface.