# Creating 2d Plots in Matlab

One of the most powerful things about Matlab is is graphical capability. This can be used in many ways, varying from simple plots of functions all the way to multilayered diagrams using many different graphical elements. All of this may be programmed. Note that we neglect the symbolic methods of plotting in this document.

Matlab can be used simply to plot functions. For example, to make a plot of the sine function on the interval [-π, π] we could use the following single line.

plot(-pi:.1:pi,sin(-pi:.1:pi))

which produces the plot below.

Ordinarily it would be more advantageous to create vectors to hold the abscissae of the points to be plotted. We also want to control the appearance of the points plotted. Matlab allows this by using a three-tuple to represent every set of points in a plot: the first entry is vector containing the abscissae ('x' coordinates) of the points; the second is a vector containing the ordinates ('y' coordinates); and the third optional argument is a string describing the color, point representation, and line style for the points. Consider the following in which we plot a sine function using a green dotted line along with a cosine function using red diamonds to represent the points.

x=-pi:.1:pi; plot(x,sin(x),'g--',x,cos(x),'rd')

Note that the style string is completely optional - in the first example
we left it off and got a solid curve joining our points, in the default
color.
A complete listing of the symbols for the optional string
can be found in Matlab help, but we reproduce it here for
convenience.
Note that if you specify a point style with no line style,
then no line segments joining the points will be made. If you
want point markers *and* line segments joining them, you must
specify both a point and a line style.

b | blue | . | point | - | solid |

g | green | o | circle | : | dotted |

r | red | x | x-mark | -. | dashdot |

c | cyan | + | plus | -- | dashed |

m | magenta | * | star | ||

y | yellow | s | square | ||

k | black | d | diamond | ||

w | white | v | triangle (down) | ||

^ | triangle (up) | ||||

< | triangle (left) | ||||

> | triangle (right) | ||||

p | pentagram | ||||

h | hexagram |

Once you have created a plot, you can alter various aspects of it using the items in its menu bar. However, if you are creating the plot in a program then you will not want to mess with it manually - you will want the program to do the labels, title, and so on automatically. Matlab provides a number of commands for that, including

`xlabel, ylabel,`and

`title`.

plot(x,sin(x),'g--',x,cos(x),'rd') legend('sin(x)','cos(x)') xlabel('x') ylabel('f(x)') title('sin(x) and cos(x)')

Assignment 6 is posted.

The midterm exam will take place on Friday,
12 October. As always, you are permitted any paper
notes you find useful, but no electronic devices are
allowed. The test is cumulative, but emphasizes
the material covered in the last fours weeks.
A sample exam is
available.

You need to install
Matlab
on your computer by Wednesday.
You do not need Simulink or any particular toolboxes, though
you might find the Symbolic toolbox useful at some time
in the future (not in this class).