MATLAB can produce both planar plots, images, and
3-D mesh surface plots. To preview some of these
capabilities and others as well, enter the command demos.
## Planar plotsTheplot command creates linear x-y plots; if x and
y are vectors of the same length, the command plot(
opens a graphics window and draws an x-y plot of the elements of
x,y)x versus the elements of y. You can, for example, draw the
grapho of the sine function over the interval -4 to 4 with the following
commands:
x = -4:.01:4; y = sin(x); plot(x,y)Give it a try. The vector x is a partition of the domain with
meshsize 0.01 while y is a vector giving the values of sine at
the nodes of this partition (recall that sin operates entrywise).
As a second example, we will draw the graph of a exponential function: x = -1.5:.01:1.5; y = exp(-x.^2); plot(x,y)Note that one must precede ^ by a period to ensure that it operates entrywise. Plots of parametrically defined curves can also be made. Try, for example, t=0:.001:2*pi; x=cos(3*t); y=sin(2*t); plot(x,y)
The command The graphs can be given titles, axes labeled and text placed within the graph with the following commands which take a string as an argument. title graph title xlabel x-axis label ylabel y-axis label gtext interactively-positioned text text position text at specific coordinatesFor example, the command title('Best Least Squares Fit')gives the graph a title. The command gtext('The Spot') allows a
mouse or the arrow keys to position a crosshair on the graph, at which
the text will be placed when any key is pressed.
By default, the axes are auto-scaled. This can be overridden by the
command axis freezes the current scaling
for subsequent graphs; entering axis again returns to
auto-scaling. The command axis('square') ensures that the same
scale is used on both axes. For more informations on axis see
help axis.
Two ways to make multiple plots on a single graph are illustrated by x=0:.01:2*pi;y1=sin(x);y2=sin(2*x);y3=sin(4*x);plot(x,y1,y2,y3)and by forming a matrix Y containing the functional values as
columns
x=0:.01:2*pi; Y=[sin(x)', sin(2*x)', sin(4*x)']; plot(x,Y)Another way is with the hold command. The command hold
freezes the current graphics screen so that subsequent plots are
superimposed on it. Entering hold again releases the "hold". The
commands hold on and hold off are also available.
One can override the default linetypes and pointtypes. For example, x=0:.01:2*pi; y1=sin(x); y2=sin(2*x); y3=sin(4*x); plot(x,y1,'--',x,y2,':',x,y3,'+')renders a dashed line and dotted line for the first two graphs while for the third the symbol + is placed at each node. The line- and mark-type are Linetypes: dashed(--), dotted(:), dashdot(-.), and the default solid(-) Marktypes: point(.), plus(+), star(*), circle(o), x-mark(x), square(s), diamond(d), up-triangle(v), down-triangle(^), left-triangle(<), right-triangle(>), pentagram(p), hexagram(h) Colors: yellow(y), magenta(m), cyan(c), red(r), green(g), white(w), black(k), and the default blue(b)See help plot for line and mark colors.
The command
## ImagesMatrices could be viewed as an image, since any digital image is like a matrix in that it has rows and columns of values. The commandimage( will "image" the matrix A. The matrix will be displayed differently
depending on the colormap . The colormap can consist of 16 colors or 256 colors; changing
how many colors are used will alter the way the matrix is displayed.
In order to avoid this alteration due to differences in the number of colors
you can use the command A)imagesc( which will automatically
scale your matrix to span the entire spectrum of colors possible.
A)
## 3-D mesh plotsThree dimensional mesh surface plots are drawn with the functionmesh. The command mesh( creates a
three-dimensional perspective plot of the elements of the matrix
z)z. The mesh surface is defined by the z-coordinates of the points
above a rectangular grid in the x-y plane. Try mesh(eye(10)).
To draw the graph of a function [x,y] = meshgrid(xx,yy);One then computes a matrix z, obtained by evaluating f
entrywise over the matrices x and y, to which mesh
can be applied.
The following example will draws the graph of xx = -2:.1:2; yy = xx; [x,y] = meshgrid(xx,yy); z = exp(-x.^2 - y.^2); mesh(z)Alternatively, you could replace the first three lines of the preceeding example with: [x,y] = meshgrid(-2:.1:2,-2:.1:2);For more details regarding the command mesh, you can refer to
online help or the User's Guide.
Others command related to 3-D graphics are
You can obtain a hardcopy of the graphics screen by using the command
plot first graph print test plot second graph print -append testWhat the above example do is it will first print the first graph that you have drawn in the file test.ps and the second print statement
will append the second graph to the same file. After this, you can print
the file using the standard unix command lpr.
Finally, use the |