An interactive introduction to MATLAB

(Jeff_L) #1

2 plotting


MATLABis very powerful for producing both 2dand 3dplots. Plots can be
created and manipulated interactively or by commands.MATLABoffers a
number of different formats for exporting plots, including EPS (Encapsulated
PostScript), PDF (Portable Document Format) and JPEG (Joint Photographic
Experts Group), so you can easily includeMATLABplots in your reports.

2.1 Simple 2dplotting


The simplest and most commonly used plotting command isplot(x,y), where
xandyare simply vectors containing thexandycoordinates of the data to
be plotted. Listing 2.1 demonstrates the commands used to create a plot of
the function,f(x) =e−^10 xsin(x), which is shown in Figure 3.
Listing 2.1: A simple plot
1 >> x = 0:0.1:20;
2 >> y = exp(−x/10).*sin(x);
3 >> plot(x,y), grid on, xlabel('x'), ...
4 ylabel('f(x) = e^{−x/10} sin(x)'), title('A simple plot')


Comments:


  • The vectors containing thexandydata must be the same length.

  • The plot command can be used to plot multiple sets of data on the same
    axes, i. e.plot(x1,y1,x2,y2).

  • The dot-dot-dot...(ellipsis) notation is used to indicate that Lines 3
    and 4 are one long line. The ellipsis notation just allows the line to be
    broken to make it more readable. Each comma-separated command could
    also have been typed on a separate line.


WhenMATLABexecutes a plotting command, a new Figure Window opens
with the plot in it. The following list gives the most common commands for
changing plot properties.


  • grid ondisplays the grid!


19
Free download pdf