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 $ v$ is a row vector containing the numbers $ v_1$ until $ v_n$ , generates a plot where the points (1,$ v_1$ ), (2,$ v_2$ ) until (n,$ v_n$ ) are connected by straight lines. The command
>> plot(v,w)
with $ v$ and $ w$ two row vectors with the same number of elements, plots the points ($ v_i$ ,$ w_i$ ), and connects them by straight lines. The command
>> plot(v,w,x,y)
plots the points ($ v_i$ ,$ w_i$ ) and ($ x_i$ ,$ y_i$ ) 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 $ y$ versus $ x$ , 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 $ f(x) = \frac{\sin(x)+2x}{1+x^2}$ on the interval $ [0, 2\pi]$ 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

which takes care that the graph is drawn in the rectangle XMIN $ \leq x \leq$ XMAX and YMIN $ \leq y \leq$ 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.

Figure 1.1: Plot functions

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

You can save a figure by selecting "File $ \backslash$ 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.

Previous      Next      Up      Contents

Esteur 2010-03-22