Introduction to Matlab

Mathematics University of Queensland Mathematics

7  Graphs

Consider again the problem of graphing the functions, for example the function
f(x) = x|x|/(1+x2)
over the interval [-5,5]. Although we can use ezplot or fplot, we wish to illustrate more powerful methods, and the use of component-wise arithmetic.

7.1  Component Arithmetic

The new method is illustrated in the following lines.

    x = (-5:.1:5)' ;
    y = x .* abs(x) ./ ( 1 + x.^2) ;
    plot( x , y , '-')

The first command produces a vector of x values from -5 to 5 in steps of .1. That is the column vector x=[-5,-4.9,¼,0,¼4.9,5]¢. The vector y contains the values of f at these x values. As there are so many points the graph of x against y looks like a smooth curve.

The novelties here are the operators .*  , ./ , and .^ in the second command. These are the so called component-wise operators. In the above example x is a column vector of length 101 and abs(x) is the column vector whose i th entry is |xi|. The formula x*abs(x) would be wrong, as this tries to multiply the 101×1 matrix x by the 101×1 matrix abs(x). However the component-wise operation x.*abs(x) forms another vector of length 101 with entries xi|xi|. Continuing to evaluate the expression using component-wise arithmetic, we get the vector y of length 101 whose entries are xi|xi|/(1+xi2). More explicitly
x = é
ê
ê
ê
ê
ë
-5
-4.9
-4.8
:
ù
ú
ú
ú
ú
û
,    abs(x) = é
ê
ê
ê
ê
ë
|-5|
|-4.9|
|-4.8|
:
ù
ú
ú
ú
ú
û
,    x.*abs(x) = é
ê
ê
ê
ê
ë
-|-5|
-4.9×|-4.9|
-4.8×|-4.8|
:
ù
ú
ú
ú
ú
û

x.*abs(x)./(1+x.^2) = é
ê
ê
ê
ê
ë
-|-5|/(1+(-5)2)
-4.9×|-4.9|/(1+(-4.9)2)
-4.8×|-4.8|/(1+(-4.8)2)
:
ù
ú
ú
ú
ú
û
.
(Of course we should not become overconfident. This rule for dividing by vectors cannot be used in Mathematics proper.)

Note that p.*q and p*q are entirely different. Even if p and q are matrices of the same size and both products can be legitimately formed, the results will be different.

Exercise 23

 

  1. (Easy) Find two 2×2 matrices p and q such that p.*q ¹ p*q.

  2. (Hard) Find all 2×2 matrices p and q such that p.*q=p*q.

Before more exercises, we show how to add further curves to the graph above. Suppose we want to compare the function we have already drawn, with the functions x|x|/(5+x2) and x|x|/([1/5]+x2). This is done by the following three additional commands. (Remember x already contains the x-values used in the plot. These new commands are most easily entered by editing previous lines.)

y2 = x .* abs(x) ./ (5 + x.^2) ;
y3 = x .* abs(x) ./ ( 1/5 + x.^2) ;
plot(x,y,'-', x,y2,'-.', x,y3,'--')

Exercise 24

(a.)    Use Matlab to graph the functions cos(x), 1/(1+cos2(x)), and 1/(3+cos(1/(1+x2)) on separate graphs.

(b.)    Graph 1/(1+eax), for -4 £ x £ 4 and a = .5,1,2, on the one plot. If you like fine graphics the following command will do a nice label
title( ¢\it 1/(1+e^{\alpha x})for\alpha = .5,1,2.-4¢)

(Use ^ and _ for superscripts and subscripts in labels. Get Greek letters by using their names preceded by \ .)

(c.)    Use plot to draw a graph that shows

lim
x® 0 
sin(x)
x
= 1.
(Hint: Select a suitable range and a set of x values and plot sin(x)/x. If you get messages about dividing by zero, it means you tried to evaluate sin(x)/x when x is exactly 0.)

7.2  Printing Graphs

When the graph has been correctly drawn we will usually want to print it. As many graphs come out on the printer, it is a good idea to label the graph with your name. To do this use the title command.

                                      %Be sure to put quotes
   title('Exercise 3.4 Kim Lee.')     %     around the title.

Bring the plot window to the front. The title should appear on the graph. If everything is okay, click on the `file' menu ® `print' item in the graph window. If everything is set up correctly, the graph will appear on the printer attached to your computer.

After printing a graph, it is useful to take a pen and label the axes (if that was not done in Matlab), and perhaps explain the various points or curves in the space underneath. Alternatively before printing the graph, use the Matlab commands title, xlabel, ylabeland legend for a more professional appearance.

The most recent versions of Matlab let the user add text, lines and arrows interactively once the graph is on the screen. Use the `tools' menu, and tick the `show toolbar' option to make the tools appear. You may then add text, arrows and lines to the plot by pressing the buttons labelled `A',  \nearrow   and / .

Another option is to use the print command in Matlab to print the current graph to a file so that it can be printed later. For example if you have access to a postscript printer use the command

     print -deps mygraph

This will write the current graph to the file mygraph.eps. This file can be stored on your floppy and printed out later.

7.3  Graphs in Reports

Instead of printing graphs directly, it is sometimes easier to copy Matlab graphs into a word processor such as Windows Wordpad or MS Word. (A character editor such as `Notepad' is not good enough). Then the graph can be included as a part of a written report.

Several graphs can be saved in a file this way and incorporated into the final report. Ensure though that the report file is saved on your floppy disk when you leave, not left on the internal hard drive.

If you are working in the computer laboratories, make sure the report file is small enough to fit on a floppy; and make sure you store the report on the floppy and take the floppy when you leave.

7.4  Saving Graphs

The easiest way to save a graph is to save the Matlab command you used to create the graph. These are usually only one or two lines. If the commands are more than one or two lines long, they should probably be saved in an M-file (see later chapters.)

7.5  Advanced Graphics

The commands given are sufficient to produce clear, simple graphs that suffice for most projects. However, may people are moved to do more:- to alter the size and fonts of labels, to change the thickness of lines etc. If you think this is necessary, more information is available.