Department of Mathematics

Math 300: Mathematical Computing

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.


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.

wwhitevtriangle (down)
^triangle (up)
<triangle (left)
>triangle (right)

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 Monday, 16 October. As with all exams, all paper notes may be used, but no electronic devices are permitted. There is a sample exam available.

Department of Mathematics, PO Box 643113, Neill Hall 103, Washington State University, Pullman WA 99164-3113, 509-335-3926, Contact Us
Copyright © 1996-2015 Kevin Cooper