Graphics


Matlab has outstanding graphics capabilities (you must be using a terminal which supports graphics to use them). However, graphing in Matlab is conceptually different than graphing in Maple or Mathematica.

Graphics Concepts

Before looking at the plotting capabilities of Matlab, consider what a graph really is. A graph is a collection of points, in 2,3 or even 4 dimensions, that may or may not be connected by lines or polygons. Most math software packages hide this from the user by sampling a continuous function to generate the points.

Matlab is designed to work with matrices, rather than functions. Matrices are a convenient way to store a collection of numbers - which is exactly what is needed when graphing. Thus all graphing commands in Matlab accept matrices as their argument, rather than a function. If you are used to function-style plotting, Matlab may take some getting used to. On the other hand, Matlab's approach makes it very easy to visualize data and to create graphics based on lists of points.

Another unique feature of Matlab's graphics engine is the way in which it displays graphical output. In Matlab, there is (usually) only one plotting window. Subsequent plotting commands will overwrite the old plot, unless you request a new one be made. This allows a plot to be made, then adjusted later to suit your needs.

Basic 2-D Graphics

Now that you understand a bit more about graphing, try these examples. Be sure to follow along exactly, or you may not get the same results.
Input Output Comments
x = -10:0.5:10;
y = x .^ 2;
(none) Create a 1x41 matrix that counts from -10 to 10,
and a 1x41 matrix made by squaring the entries of the first matrix.
plot(x,y) y=x^2 Plot the points. Each pair is plotted, so <x(1), y(1)> is a point, <x(2), y(2)> is a point, etc.
x = -10:0.5:10;
plot(x,sin(x))
hold on
plot(x,cos(x))
hold off
y=x^2 Plot one plot over another.
t = 0:0.1:2*pi;
x = cos(t);
y = sin(t);
(none) Generate some new 1x63 matrices.
plot(x,y) Unit circle Plot the points. This is called a parametric plot. Notice that it replaces the previous plot.
t = 0:pi/5:2*pi;
u = cos(t);
v = sin(t);
(none) Generate some new 1x11 matrices. This shows how to control the "resolution" of the plot.
figure
plot(u,v)
Unit circle, coarse Create a new plot window, and plot the points.
Notice how jagged the circle is, since we only used 11 sample points.

plot(x,y, 'ro-')
Unit circle Plot the hi-res version again in red, with circles at the data points, connected by lines.
plot(x,y, 'r-', u,v,'b*:') Unit circle On the same plot, plot the hi-res version in red, and plot the lo-res version with blue stars at the data points and dotted lines.
figure
subplot(1,2,1)
plot(x,y)
title('Fine Mesh')
subplot(1,2,2)
plot(u,v)
title('Coarse Mesh')
Unit circle, side-by-side Create a new figure.
Divide it into one row, two columns, and pay attention to the first cell.
Plot.
Give the current plot a title.
Pay attention to the second cell.
Plot.
Give it a title.
See the following help files for more options and ideas: help plot, help comet, help semilogy and help fill .

Matlab provides very powerful features in the figure window. Use the toolbar at the top to add arrows, lines, and text comments to your plot.


Basic 3-D Graphics

InputOutputComments
t = -4*pi:pi/16:4*pi;
x = cos(t);
y = sin(t);
z = t;
(none) Generate the data...

plot3(x,y,z)
Unit circle ... and draw a helix.
[x, y] = meshgrid(-3:0.1:3,-3:0.1:3);
z = x.^2 - y.^2;
(none) Generate data for a surface plot.

mesh(x,y,z)
Unit circle Draw the surface using a mesh.

surf(x,y,z)
Unit circle Draw the surface as a "patched" surface.

plot3(x,y,z)
Unit circle Notice that it still plots, but as a set of arches.

Advanced Plotting

Of course, Matlab can do a lot more than these simple exercises. If you'd like to know more, try these help files:
help slice , help movie , help getframe , help graph2d , help graph3d , help graphics .
Next: Programming
Previous: More on Matrices