# Graphs

To draw a graph, you first need to open a graphic screen. This is done with the command:
>> figure

Graphs are drawn with the command plot. The command
>> plot(v)

where is a row vector containing the numbers until , generates a plot where the points (1, ), (2, ) until (n, ) are connected by straight lines. The command
>> plot(v,w)

with and two row vectors with the same number of elements, plots the points ( , ), and connects them by straight lines. The command
>> plot(v,w,x,y)

plots the points ( , ) and ( , ) in the same figure.

To determine the appearance of your graph, after plotting a vector you can add the color and linestyle of graph. For example, the command

>> plot(x,y,'r--')

will plot versus , with a red dashed line. Type help plot to see other line, marker and color options.

Suppose that we want to draw the graph of the function on the interval consisting of 100 linearly spaced points. Note that the smoothness of the graph is of course determined by the number of point. The graph can be plotted by using the following commands:
>> x = 0:2*pi/99:2*pi;
>> y = (sin(x)+2*x)./(1+x.^2);
>> figure
>> plot(x,y)


When drawing a graph, the axes are chosen automatically. You can choose the lengths of the axes yourself with

>> axis([XMIN XMAX YMIN YMAX])

which takes care that the graph is drawn in the rectangle XMIN XMAX and YMIN YMAX.

After each new plot command, the old plot will in general disappear. If you want to draw different plots with different plot commands in the same figure, you have to give the following command after the first plot command
>> hold on

MATLAB then holds the graphs. The command
>> hold off

restores the state in which every new plot command makes the previous plot disappear.

The following commands are also important for handling the graphical screen.
You can make the graphical screen visible by either activating it, or by using the command
>> shg

The graphical screen remains the the same until either a new plot command is given or until it is erased. The command for erasing the graphical screen is
>> clf

The command
>> grid

draws a grid in the graph.
>> title('title')

puts the title indicated by the string between the quotes above the plot. In the example above, the title will be the word title'.
>> text(x,y,'string')

puts the text string' at the point (x,y) of the last plot.
>> xlabel('text')

writes information along the x-axis.
>> ylabel('text')

does the same for the y-axis.

Other graph types
You can use other plot functions in MATLAB as well. The most important are given in Figure 1.1.

• bar(x) produces a bar-plot of vector .
• plot3(x,y,z) produces a three-dimensional curve between the points, whose coordinates are given in the vectors , , and .
• mesh(X,Y,Z) produces a three-dimensional surface between the grid points, whose coordinates are given in the matrices , , and .
• contour(X,Y,Z) produces level curves in plane, such that all points on a line have the same value for z. Since the inputs in this functions are gridpoints, the inputs , , and should be matrices.

You can save a figure by selecting "File Save as" and select the required file directory, filename and file type. An other option is to use the print command. Type help print for additional information. When you want to be able to open and modify your figures again in MATLAB, you should save your figure as a .fig file.

Esteur 2010-03-22