# chapter 5 review: plotting introduction to matlab 7 engineering 161

Embed Size (px)

TRANSCRIPT

Chapter 5 Review: Plotting

Introduction to MATLAB 7Engineering 161

Plotting Large tables and reams of data are difficult to

interpret. Graphing techniques facilitate seeing trends,

picking out highs and lows, and serve as a quick check on one’s calculations.

Most common plot is the x-y plot, i.e., a two dimensional plot.

MATLAB is rich in both 2 and 3 dimensional graphing capabilities. We are going to study 2 dimensional plotting. We will also practice 3-D plotting in our second program project.

Plotting II

When we complete the material in Chapter 5, we will be able to Create and label 2-dimensional plots. Adjust the appearance of your plots. Use our plots to interpret our results. Have a basis for studying more

complex MATLAB graphical capabilities.

Basic Plotting Concepts In general, through calculations or

experimental measurements, we have amassed data where y = f(x).

We call x the independent variable and y the dependent variable. x and y are usually n dimensional vectors.

Plotting is nothing more than a visual representation of the relationship between x and y.

Basic Plotting Concepts II The basic MATLAB plotting command is

plot (x , y) The graph is generated in the MATLAB

Graphics window and labeled as Figure 1 or 2, etc.

Simple MATLAB commands allow you to label the x and y axes, label your graph, label legends, and format the graph as you want.

Basic Plotting Concepts III

It is easy to plot multiple sets of data in a single graph,

And to generate multiple graphs in a single Graphics Window. MATLAB calls these sub-plots.

Plotting Functions MATLAB supports a number of basic

plotting functions in addition to the line chart. These include Bar Charts (vertical and horizontal, 3

dimensional) Pie Charts (2 and 3 dimensional) Histograms Semilog plots Loglog plots Polar plots

Plotting Commands The basic plotting command is plot (x , y) where x is the

independent variable and y is the dependent variable. plot (x, y1, x, y2) will plot two lines in the graph (Note x is the

same.) For Y = [y1;y2], plot (x , Y) will generate the same plot as

above. When the dependent variable is a matrix, MATLAB plots each row of Y vs x.

Another way to plot 2 lines on one graph is to use the “hold on” command

plot (x,y1) hold on plot (x,y2) hold off Normally when MATLAB executes a plot command it erases the

current figure and then plots anew. The command “hold on” suppresses the erase.

Plotting Commands II Labeling your graphs axes, and titling

your chart is easy. Use the xlabel (‘ . . . .’) and the ylabel (‘ . . . .’)

commands to label your axes appropriately. You must surround your labels in single quotes. Single quotes tell MATLAB that it is a character string.

Use the title (‘ . . . .’) command to title the graph. Use the legend (‘line1’, ‘line2’, ‘line3’) command to

identify a designator with each line on your graph. The order of the entries corresponds to the order that you plotted the lines.

Or use the Insert Tab on the Graphics Window toolbar to customize your graph. NOTE: when you customize a graph interactively, i.e., using the Insert Tab, the next time you run the program, the customization is lost.

Plotting Commands III To open multiple figures use the figure (n) command,

Figure 1 is the default window, but after using this window to plot in, enter the command figure (2) to create a new graphics window. The next time you plot, the 2nd graphics window will be used thereby preserving the results graphed in figure 1. Both windows can be viewed as wanted.

Don’t forget when in need of information about the plot command, enter help plot in the Command Window for information.

Finally, the command plot (y) makes sense to MATLAB. Here MATLAB uses the indices of the vector y as the x values.

Plotting Commands IV The gtext command is another usual tool

for adding text to a graph. Simply enter the gtext command in your program or in the Command Window as

gtext(‘This string will display on the graph’)

A crosshair will appear on the graph, position it appropriately and click the mouse to see the text added to the graph.

Line, Color and Mark Style You can select line type, point type, and color for each of your

plotted lines. See page 157 of the text for Table 5.2 of the option codes to

use. For example, plot (x,y, ‘:og’) will generate a dotted line, with a

circle point type, and color green. Enter 1, 2 or 3 option codes in any order surrounded by single

quotes to define the style you want to use with your line graph. The option codes need to immediately follow the x,y pair where they specify the style.

When multiple lines are being plotted using a single plot command, follow each x,y pair with its option code in single quotes and separated by commas.

When no option codes are present, MATLAB uses its default code, solid black line with no point type.

Axes Scaling Unless you choose to scale your axes, MATLAB

will scale them automatically based upon the values in x and y. This is the easiest method.

But, you can scale your axes with the axis (v) command where v = [xmin, xmax, ymin, ymax], that is, v is a 4 element row vector provided by you with the min and max values for each axes.

The command axis without the argument v freezes the current axes scale for subsequent plots. Enter axis again to unfreeze.

Axes Scaling II Sometimes you will want to have two different

vertical axes scaling, one on the left hand side of the graph and the other on the right hand side. To do this

Use the plotyy (x, y1, x, y2) command to force MATLAB to scale the vertical axes independently for the two lines in your chart.

Then use the Insert Tab in the graphics window to label the right hand vertical axis.

The chart title, and x and left hand y axes labels can be added using the title, xlabel and ylabel commands. Only the right hand label needs to be added using the Insert Tab.

Subplots The subplot command allows you to split the

graphics window into a number of sub-windows.

You can choose two sub-windows arranged horizontally or vertically, or

You can choose an array of sub-windows, say two rows with two columns or 4 subplots displayed at one time.

More than 4 isn’t too practical. Things get too small.

Subplots II The subplot command is specified in the following

manner, subplot (m,n,p) where m, n, and p are integers; m and n

specify the size of the array of subplots and p specifies which subplot is being referenced. Subplots are numbered from left to right, top to bottom.

subplot (2,2,3) would specify a 2 by 2 array of subplots with the 3 indicating the lower left hand plot in the array.

To use this feature, you will do the following;

>> subplot (2,1,1); % The subplot command proceeds

>> plot (x,y) % the plot command.

Here two subplots are specified, one over the other, the plot takes place in the upper chart.

Subplots III Chart titling, axes labeling, etc., following the

plot command will refer to the last subplot indicated. Any additional plot commands will also refer to the last subplot indicated. To select a new sub-window to plot in, you have to select that sub-window with a subplot command before you use the plot (x,y) command. You can graph multiple x,y pairs in each subplot, select the style, add legends, etc., just as you have learned how to do with a single graph.

Plotting from the Workspace Window MATLAB 7 allows you to create plots from the

Workspace window. This lets you view intermediate results or any variable in the Workspace window.

In the Workspace window, select the variable you want to plot. If more than one, hold down the Ctrl key and select the additional ones that you want plotted.

Next click on the “plotting icon”, the squiggly line in the the Workspace toolbar. MATLAB selects what it thinks would be good plotting options based upon the data.

Select your graph type and the graph is generated. Use More plots to see a complete list of plot options.

Saving Plots Often you may want to save a plot to

include in another document, for example a Word document for a project report. From the figure window, save the plot in a file using the jpeg format. The jpeg format is pretty universal and compatible with MicroSoft Word and Powerpoint applications. It’s easy to do, give it a try.

The fplot function I A very useful built in function to help

visualize functions, y = f(x), is the fplot function. Consider the following example; suppose you want to see what the function

y = 5sin2(t) + tcos2(t)

looks like for –2π < t < +2π

The fplot function II Use the following MATLAB statement;

fplot(‘5*sin(t)^2 + t*cos(t)^2’,[-2*pi, 2*pi])

to create the graph. Note the function we want to plot is enclosed in single quotations.fplot is a very useful function for Calculus, to help visualize functions of a single variable.

3_D plotting Our book provides you with the elements

for 3-dimension plotting. We wont cover them explicitly but we get some experience with our second programming assignment this semester as it requires you to make a 3-dimensional plot.

Go ahead and read through the remaining parts of the chapter to acquaint yourself with the key elements of 3-D plotting.

Chapter 5 Assignments

Chapter 5 assignments let you practice plotting. They are, 5.11, 5.13, 5.20, 5.25, 5.27.