Three-Dimensional Plots
Functions of two variables are sometimes difficult to visualize with a two dimensional plot. Fortunately, MATLAB provides many functions for creating three-dimensional plots. Here we will summarize the basic functions to create three types of plots: line plots, surface plots, and contour plots. Information about the related functions is available in MATLAB help (category graph 3d).
Three-Dimensional Line Plots
Lines in three-dimensional space can be plotted with the plot 3 function. Its syntax is plot 3 (x, y , z). For example, the following equations generate a three-dimensional curve as the parameter t is varied over some range:

. x = e-0.051 sin t
y = e-0.051 cos t

The curve x = e-O.051 sin t, Y = e-O.051 cas t, Z = t plotted with the plot3 furiction.

The curve x = e-O.051 sin t, Y = e-O.051 cas t, Z = t plotted with the
plot3 function.

If we let t vary from t = 0 to t = 10 zr , the sin and cos functions will vary through five cycles, while the absolute values of x and y become smaller as t increases. This .process results in the spiral curve shown in Figure 5.8-1, which was produced with the following session. »t = [O:piI SO:l0*pi)»plot 3(exp(-O.OS*t) .*sin(t) ,exp(-O.OS*t) .*cos(t),t)x label (‘x’) ,y label (‘y’), z label (‘ z r }, grid
Note that the grid and label functions work with the plot 3 function, and that we can label the z-axis by using the z label function, which we have seen for the first time. ‘Similarly, we can use the other plot-enhancement functions discussed in Sections 5.1 and 5.2 to add it title and text and to specify line type and,color,

Surface Mesh Plots

 The function z = f (x, y) represents a surface when plotted on x yz axes, and the mesh function provides the means to.generate a surface plot. Before you can use
this function, you must generate a grid of points in the xy plane, and then evaluate the function f(x, y) at these points. The mesh grid function generates the grid. Its syntax is Ix .Y] = mesh grid(x,y).If x = [x min:xs pacing:x max] and y = [y min: ys pacing :y max 1, then this function will generate the coordinates rof. a rectangular grid with one comer at tx min; y min) and the opposite

corner at (x max . y max). Each rectangular panel in the grid will have a width equal to x spacing and a depth equal to y spacing. The resulting matrices X and Y contain the coordinate pairs of every point in the grid. These pairs are then used to evaluate the function.
The function [X, Y) = mesh grid (x) is equivalent to [X, Y) =
mesh grid (x , x) and can be used if x and y have the same minimum values, the same maximum values, and the same spacing. Using this form, yo y can type [X, Y) = mesh grid (min:spacing:max), where min and max specify the minimum and maximum values of both x and y and spacing is the desired spacing of the x and y values.
After the grid is computed. you create the surface plot with the mesh function. Its syntax is mesh (x , y r z ) . The grid, label, and text functions can be used with the mesh function. The following session shows how to generate the surface plot of the function z =  xe-1(x-\)+Y1, for -2 x 2 and-2 )’  2, with a spacing of 0.1. This plot appears in Figure 5.8-2. »[X,Y) = mesh grid (-2:0.1:2);
»Z =  X.*exp(-((X-Y.A 2) .A 2+y.A 2)); »mesh(X,Y,Z),x label(‘x’) ,y label(‘y’) ,z label(‘z’) Be careful not to select 100 small a spacing for the x and y values for two reasons: (1) Small spacing creates small grid panels, which make the surface difficult to visualize, and (2) the matrices X and Y can become too large. The surf and surface functions.are similar to mesh and mesh c except that the former create a shaded surface plot. You can use the Camera toolbar and some menu items in the Figure window to change the view and lighting of the figure.

A plot of the surface z = xe-1C1 -.~ I~+.~cIreated with the meshfunction

A plot of the surface z = xe-1C1 -I+.created with the meh function

Contour Plots
Topographic plots show the contours of the land by means of constant elevation lines. These lines are also called contour lines. and such a plot·is called a contour plot. If you walk along a contour line. you remain at the same elevation. Contour plots can help you visualize the shape of a function. They can be created with the
contour function. whose syntax is contour (X,Y, Z) .You use this function the same way you use the mesh function; that is. first use the mesh gr id function to generate the grid and then generate the function values. The following session generates the contour plot of the function whose surface plot is shown in Figure 5.8-2; namely, z = xe-[(x-y1 )2+y 11, for -2 S x S 2 and -2 S y S 2,
with a spacing of 0.1. This plot appears in Figure 5.8-3.
»[X,Y] = mesh grid(-2:0.1:2); »Z = X.*exp(-((X- Y.A2) .A2+y.A2));
»contour(X,Y,Z),x label(‘x’),y label(‘y’) Contour plots and surface plots can be used together to clarify the function. For example, unless the elevations are labeled on contour lines, you cannot tell whether there is a minimum or a maximum point. However, a glance at the surface plot will make this easy to determine. On the other hand, accurate measurements are not possible on a surface plot; these can be done on the contour plot because

A contour plot of the surface z = .re-lCx-y1~+y1) created with the contour function.

A contour plot of the surface z = .re-lCx-y1~+y1) created with the
contour function.

distortion is involved. Thus a useful function is mesh c, which shows the contour lines beneath the surface plot. The mesh z function draws a series of vertical lines under the surface plot, while the waterfall function draws mesh lines in one direction only. The results of these functions are shown in Figure 5.8-4 for the function z = xe-(x1+y2). Table 5.8-1 summarizes the functions introduced in this section. For other 3 D plot types, type help spec graph.

Plots of the surface z = xe-<x2+yl) created with the mesh function and its variant forms: me she, meshz, and waterfall. a) mesh, b) meshc, c)meshz,d)waterfall

Plots of the surface z = xe-<x2+yl) created with the mesh function
and its variant forms: me she, meshz, and waterfall. a) mesh, b) meshc,

Three-dimensional plotting functions

Three-dimensional plotting functions

Test Your Understanding
T5.8~1 Create a surface plot and a contour plot of the function z = (x – 2)2 +
2xy + y2.

Share This