MATLAB provides users with a powerful array of functions for creating sophisticated graphics. In CPSC 303 we will probably only use the 2-D plotting features, but you should familiarize yourself with the breadth of MATLAB's capabilities.
For our work the two plotting functions plot and fplot will be especially useful. plot permits the plotting of vectors and matrices, while fplot simplifies the plotting of functions.
Let's start by plotting
between 0 and pi
using plot. First we must create
two vectors containing the x and the y coordinates to the points we will use
to create our plot.
>> x=0:pi/100:2*pi;
>> y=sin(x);
To create our plot of
we enter the following statement.
>> plot(x,y,'-')
The minus sign in single quotes tells MATLAB we want a solid line.
Other line types (and colors) can be specified in this fashion.
Enter help plot for more information.
MATLAB permits the addition of another plot to the same graph by entering the
command
>> hold on
Let's also plot the value of
at
0,pi/4,pi/2,
,2*pi on our graph, but this
time instead of a solid line we will just mark each point with a plus sign.
>> plot(0:pi/4:2*pi,sin(0:pi/4:2*pi),'+')
Perhaps we also want to add a title and axis labels to the graph.
>> title('Our sine plot from 0 to pi')
>> xlabel('x')
>> ylabel('sin(x)')
Text can also be added to a plot at user specified coordinates with the
commands text and gtext.
Your final graph should look something like Figure 2.
When you are finished with the graph enter the command
>> hold off
so that subsequent graphics do not include the current plot.
The MATLAB command fplot is similar to plot but accepts the name
of a function in single quotes and an abscissa range. It adaptively
samples the function at enough points to give a representative graph
and then plots the results.
As an example, let's plot
again.
>> fplot('sin', [0,2*pi])
To create a hardcopy of the current figure we use the MATLAB command
print. For example, to create an PostScript version of
our figure enter the following command.
>> print file_name.ps -dps
The resulting file can be printed on
any PostScript printer using the Unix command lpr or previewed using
ghostview. To create encapsulated PostScript suitable for
inclusion into documents substitute -deps for
-dps and file_name.eps for file_name.ps.
(WARNING: PostScript files can be quite large and eat up a lot of your
disk quota.)
In CPSC 303 we may often create data outside MATLAB and want to import it for plotting. This is accomplished by placing the data pairs (or triples) into an ASCII file with each pair (or triple) of data points on a separate line separated by blanks. (In other words, each vector of data to be imported is placed in a separate column of the file.) Be sure that numbers in scientific notation are compatible with MATLAB's format. (See Section 5.)
To import the data enter the following command.
>> load file_name
Variable file_name is now a matrix containing your data. To check
if it was properly imported, simply enter file_name. To extract
the vectors of data from the columns of the matrix use the colon notation
as previously discussed.