Sie sind auf Seite 1von 17

Department of Engineering Education, Virginia Polytechnic Institute and State University Copyright J.C.

Malzahn Kampe, 2004


______________________________________________

Page 1 of 17 EngE 1024 Fall 2004

MATLAB GRAPHING
Using MATLAB 7.0
We often plot data in a variety of ways in order to see if a common type of graphing scheme will cause the data points to fall in a fairly straight line (as opposed to a curve). When this occurs, we can determine the form of an empirical function that describes the data. We fit data that can be represented by a straight line on a rectilinear plot to a linear function of the y = mx + b form. Data that produce a straight line on a log-log plot we fit to a power function of the y = b xm form. Data that line up on a semilog plot (y-axis logarithmic) we fit to an exponential function of the form y = b emx. Our usual procedure is to plot the data points on all three types of grids (rectilinear, fully logarithmic, and semi-logarithmic,) to determine which of the three grids lines up the data points best. Once we know the type of grid that produces the best line-up of data points, the next order of business is to determine the constants m and b for the equation of the line that represents the data. Then we use our appropriate m and b values to generate the line of best fit and plot it on the best grid along with the data points. This document outlines the basic steps used in MATLAB to produce a plot, to determine the equation constants, and to generate the best-fit line. The general procedure is given below and then illustrated with a guide for the three types of functions (linear, power, and exponential) mentioned above. This might seem somewhat repetitive, but it gives an orderly account of the syntax and output interpretation you will need to use MATLABs polyfit command for these functions. General MATLAB Graphing Procedure Steps 1. 2. 3. 4. 5. 6. Enter the experimental data. Plot the data as points on the three different grids (rectilinear, log-log, and semi-log). Choose the grid that best lines up the data and use only that plot for the rest of the steps. Determine the equation constants (m and b) of the best-fit line using a polyfit command. Calculate new y-values using the determined values of m and b from Step 4. Put the best-fit line on the graph by plotting the calculated y-values versus the experimental x-values. 7. Put the figure in proper graphing format using Steps 8 through 12. 8. Adjust axes limits so data points do not reside on the perimeter of the grid. 9. Insert the grid lines, if desired. 10. Label each axis with the plotted quantity name, variable symbol, and units Example axis label: Area A, m2 11. Add the figure title and move it to a position below the plot. 12. Copy the figure to a Word document, if desired.

To obtain maximum benefit from the time you invest in reading this document, open MATLAB and follow along, entering any indicated commands (designated by >> ) in the command window as you read.

Department of Engineering Education, Virginia Polytechnic Institute and State University Copyright J.C. Malzahn Kampe, 2004
______________________________________________

Page 2 of 17 EngE 1024 Fall 2004

ENTERING THE DATA Suppose you had made measurements and acquired some length and surface area data for a series of prisms (same size and shape cross section) cut to different lengths.
Length L, cm : Surface Area SA, cm2 : 1.00 31.5 2.00 36.5 3.00 44.0 4.00 50.5 5.00 57.0 6.00 63.0 7.00 68.5 8.00 74.5 9.00 82.0 10.0 89.0

These data can be entered in the MATLAB command window as two separate vectors (here called Length and SArea). >> Length = [ 1.00, 2.00, 3.00, 4.00, 5.00, 6.00, 7.00, 8.00, 9.00, 10.0]; >> >> SArea = [ 31.5, 36.5, 44.0, 50.5, 57.0, 63.0, 68.5, 74.5, 82.0, 89.0]; When you enter the data in the command window, remember that commas or spaces can be used to delimit (separate) the elements within a row of a MATLAB matrix, and semicolons are used to delimit the rows. PLOTTING THE DATA After you have entered the data in the Command Window, select the variable in the Workspace that contains the independent variable values; here, the independent variable is length because the surface area was measured for different lengths of the same type of prism. So, select Length and then, in the tool bar for Workspace, open the plotting menu by clicking on the selection arrow to the right of the graphing icon. Click Here

Note that Length is selected.

Figure 1. The MATLAB desktop.

Department of Engineering Education, Virginia Polytechnic Institute and State University Copyright J.C. Malzahn Kampe, 2004
______________________________________________

Page 3 of 17 EngE 1024 Fall 2004

From the plotting menu that opens, select More Plots, and you will get the dialog box shown below. Note that we have added SArea to the Plotted Variables. In Plotted Variables, the independent variable is listed first and the dependent variable second. Because we had selected Length before opening the Plot Catalog, Length appeared automatically in Plotted Variables. It is important that you remember to add the dependent variable (here SArea) to the Plotted Variables list or you will not get the plot that you want.
SArea added. IMPORTANT!!

A linear plot is selected. You can select a new figure window here.

Figure 2. MATLABs Plot Catalog, the dialog box that opens with More Plots.

Using the Plot Catalog that opens under More Plots , plot the data contained in Length and SArea on a linear plot, and use the Plot a New Figure option. After a bit of editing, you should have something like the plot in Figure 3. To do the editing, the first step is to enable the plot editing features by selecting the Edit Plot option from the Tools pull-down menu, or by clicking on the upward arrow that points to the left in the toolbar. If your figure window toolbar is not in view, select Figure Toolbar from the View pull-down menu. After you have enabled plot editing, double click on the plotted line to open the Property Editor for the plotted data. First change the plotted data from the default line to points after all, this is experimental data and it should be presented as such. To do so, select a marker from the options provided with Marker and select no line from the Line options. Note that you can also choose line color and weight, and you can select marker size and edge and fill colors. It is good to select a dark color for the marker edge so that your data points show up well when the plot is printed. Also, please note that the default line that shows up when you first plot is NOT THE BEST-FIT LINE; it is only the data plotted as a line instead of as point markers. In the Property Editor for the data, you should also select the proper sources for the plotted data. These editing options are indicated in Figure 3.

Department of Engineering Education, Virginia Polytechnic Institute and State University Copyright J.C. Malzahn Kampe, 2004
______________________________________________

Page 4 of 17 EngE 1024 Fall 2004

Click here to enable plot editing.

The data point are shown to be selected so that the Property Editor is visible.

Indicate proper data sources here.

Choose line and marker options here.

Figure 3. MATLABs figure window with the data Property Editor made visible. Note that the data points are selected in order to see the data Property Editor.

Repeat the plotting process to generate a log-log plot and a semilogy plot, and use the Plot in a New Figure option (see Figure 2) each time so that all three plot types are available to you. When you have the three plot types (as in Figure 4), you can make the decision as to which type of plot lines up the data best. After you have chosen the appropriate plot type, use that plot to complete the graphing procedure.

Department of Engineering Education, Virginia Polytechnic Institute and State University Copyright J.C. Malzahn Kampe, 2004
______________________________________________

Page 5 of 17 EngE 1024 Fall 2004

90 10 80
1.8 1.9

70

10

60

1.7

10 50 10 40
1.5 1.6

30

10 0 2 4 6 8 10 10
0

10

a. linear

b.

log-log

1.9

10

1.8

10

1.7

10

1.6

10

1.5

10

10

c. semilogy Figure 4. Three types of plots for the prism length and surface area data: a.) linear, b.) log-log, and c.) semilogy. These are used to determine which type of plot best lines up the data. Use a straight edge to select the plot type.

An examination of the plots provided in Figure 4 indicates that we should proceed with a linear plot of the prism data. This makes sense because, if the size and shape of the prism cross section is identical for all the prisms that were measured, then surface area of the prisms should be a linear function of prism length. Our next job is to determine the equation constants for the linear function. Although MATLAB provides linear fit capabilities under the Basic Fitting option in the Tools menu of the Figure Window, we will use MATLABs polyfit command to obtain a first-order fit of the length and surface area data (with length as the independent variable) because menu options are not available for power function fits or for exponential function fits. And, if you can follow the polyfit procedure for a linear function, it might be easier for you to understand the polyfit procedures for power and exponential functions.

Department of Engineering Education, Virginia Polytechnic Institute and State University Copyright J.C. Malzahn Kampe, 2004
______________________________________________

Page 6 of 17 EngE 1024 Fall 2004

A first-order (i.e., linear) fit will produce two equation constants. The first is the coefficient for the independent variable raised to the first power, and the second is the coefficient for the independent variable raised to the zero power (which is 1 for any value of the variable). You probably know these equation constants better as m and b. First-order fit: y = m x 1 + b x 0 y=m x + b

In MATLABs Command Window, enter this command. >> EQ = polyfit(Length,SArea,1) EQ = 6.3545 24.7000

In entering the command above, you have told MATLAB that Length contains the independent variable values, that SArea contains the dependent variable values, that a first order fit is required, and that the two equation constants returned are to be stored in the matrix called EQ. At this point, EQ(1) = m and EQ(2) = b

and we know that the empirical function that describes the prism data is SA = 6.35 L + 24.7 with SA in cm2 and L in cm.

(Note that we report the equation constants in the proper number of significant figures.) To produce the best-fit line, we use the equation constants in matrix EQ and the Length values to calculate new surface area values called SAcalc. >> SAcalc = EQ(1).* Length + EQ(2)
SAcalc = Columns 1 through 5 31.0545 37.4091 43.7636 50.1182 56.4727

Columns 6 through 10 62.8273 69.1818 75.5364 81.8909 88.2455

You should notice that the value assignment statement we used to generate SAcalc has the y = mx + b form and, also, that the multiplication of EQ(1) and Length is an array operation (because you want the multiplication to occur on an element-by-element basis for the vector Length).

Department of Engineering Education, Virginia Polytechnic Institute and State University Copyright J.C. Malzahn Kampe, 2004
______________________________________________

Page 7 of 17 EngE 1024 Fall 2004

Now we want to plot the SAcalc values (i.e., the calculated values of surface area) against Length as a line on the same linear plot that holds the plotted data points. To do this, enter these commands in MATLABs Command Window >> figure(1) >> hold on % Substitute your figure number for your linear plot in place of 1. % This command will retain the plot of points when you plot the line.

>> plot(Length,SAcalc,'-') % This command plots the best-fit line.

The plot obtained after these commands are executed, provided in Figure 5, is now ready to edit into final graphing format.

90

80

70

60

50

40

30 0 2 4 6 8 10

Figure 5. A plot of the best-fit line over the data points on a linear plot of the prism data before editing to complete the graphing format.

To edit the plot into final format, go to the figure window that holds the plot. Double click anywhere on the axes and you will bring up the Property Editor for the axes. Within this Property Editor, you can change the axes limits, label the axes, add a title to the plot, and even reverse the axes or convert them to logarithmic scales. You should edit your plot to obtain a version like the plot provided in Figure 6. For this plot, in order to place the points above the best-fit line and add point highlighting, we have deleted the original set of point markers in the figure window and re-plotted the data twice from the Command Window, the first time as triangle markers and the second time as point markers. The line commands used in the Command Window are as follows.

Department of Engineering Education, Virginia Polytechnic Institute and State University Copyright J.C. Malzahn Kampe, 2004
______________________________________________

Page 8 of 17 EngE 1024 Fall 2004

>> plot(Length, SArea,'^') >> plot(Length, SArea,'.')

100 90

Prism Surface Area SA, cm

80 70

60

50 40 30

20 0 2 4 6 8 10 Prism Length L, cm The influence of prism length on surface area for a series of prisms with constant cross section and varying lengths.

Figure 6. An edited version of the linear plot in final format.

In our procedure to plot the best-fit line, two things should be noted. First, we calculated new dependent (y-axis) values, but we kept our independent (x-axis) values to plot the best-fit line. Why? Because we made the assumption that there is less uncertainty in our independent variable values and, therefore, they should be kept. After all, we control the independent variable values, while the dependent values that are measured may be subject to a wide variety of uncertainty sources. This assumption of less uncertainty in the independent values is likely to be true, but there are no guarantees. Experimenters, for example, might misread dials when adjusting the independent variable value. The second thing to note is that MATLABs figure window offers Basic Fitting under the Tools menu, and a linear fit is available there. You might use this feature to check your work, but always provide your instructor with the polyfit work you do to obtain m and b and to plot the best-fit line, even for linear plots. Before we move on to power and exponential functions, it is worthwhile to look at some other line commands for plotting. These will be useful to you if you use MATLAB programming to solve problems and wish to produce a plot from your MATLAB code. To get

Department of Engineering Education, Virginia Polytechnic Institute and State University Copyright J.C. Malzahn Kampe, 2004
______________________________________________

Page 9 of 17 EngE 1024 Fall 2004

a list of line commands for two-dimensional plotting, enter the following command in the Command Window. >> help graph2d
Two dimensional graphs. Elementary X-Y graphs. plot - Linear plot. loglog - Log-log scale plot. semilogx - Semi-log scale plot. semilogy - Semi-log scale plot. polar - Polar coordinate plot. plotyy - Graphs with y tick labels on the left and right. Axis control. axis zoom grid box hold axes subplot -

Control axis scaling and appearance. Zoom in and out on a 2-D plot. Grid lines. Axis box. Hold current graph. Create axes in arbitrary positions. Create axes in tiled positions.

Graph annotation. plotedit - Tools for editing and annotating plots. title - Graph title. xlabel - X-axis label. ylabel - Y-axis label. texlabel - Produces TeX format from a character string text - Text annotation. gtext - Place text with mouse. Hardcopy and print printopt orient printing. - Print graph or Simulink system; or save graph to M-file. - Printer defaults. - Set paper orientation.

See also graph3d, specgraph.

The axis command allows adjustments to the axes limits. The grid command inserts grid lines along both axes, and you should note that grid is a toggle command turning the grid on and off with each sequential execution. The xlabel statement labels the x-axis with argument text enclosed within the single quotes and parentheses. The ylabel statement similarly labels the y-axis. The title command places the argument text enclosed in single quotes and parentheses at the top of the plot; the title should then be moved to a location beneath the x-axis label. The hold on command that we used early retains the current active plot for additional plotting or for editing by line command (until you disable it by typing hold off). However, the hold command by itself is a toggle command. Whichever way you choose to edit the plot, by lines from the Command Window or with the Property Editors in the figure window, you should eventually achieve something similar to the plot in Figure 6.

Department of Engineering Education, Virginia Polytechnic Institute and State University Copyright J.C. Malzahn Kampe, 2004
______________________________________________

Page 10 of 17 EngE 1024 Fall 2004

LOG-LOG PLOTS and POWER FUNCTIONS (y = b xm ) To learn how to use MATLABs polyfit command to find the equation constants for a power function, we will use the experimental data for the volume of a sphere as radius changes that are given here. Radius R, cm: Volume V, cm3 : 0.750 1.20 1.75 7.50 1.90 32.0 2.50 63.0 4.00 275 6.00 900. 8.00 10.5 2000. 4900.

Plotting in MATLAB as we did for the prism data example, you should check the line-up of data points on the three types of grid (see Figure 7). Of course, you should expect the data to line up best on a log-log plot because the volume of a sphere (V=4 r3 /3) is a power function of radius. >>Radius = [0.750, 1.20, 1.90, 2.50, 4.00, 6.00, 8.00, 10.5]; >>Volume = [1.75, 7.50, 32.0, 63.0, 275., 900., 2000, 4900];
4

5000 4500 4000 3500 3000

10

10

2500 2000 1500 1000 500

10

10

10

12

10 -1 10

10

10

10

a. linear

b. log-log

10

10

10

10

10

10

12

c.

semilogy

Figure 7. MATLAB plots of the sphere volume as a function of radius on the three types of grids: a.) linear, b.) log-log, and c.) semilogy. As expected, the log-log plot produces the best line-up of the data points.

Department of Engineering Education, Virginia Polytechnic Institute and State University Copyright J.C. Malzahn Kampe, 2004
______________________________________________

Page 11 of 17 EngE 1024 Fall 2004

Now that we know we are dealing with data from a power function and a log-log plot of that data, we can determine the equation constants using MATLABs polyfit command. The first thing to realize is that a power function of the form y = b x m has a linear form that can be obtained by taking the log of both sides of the equation. If this is done, the resulting expression looks like this. log y = m log x + log b This form indicates (by comparison with y = mx + b) that a linear fit between log x and log y data will yield the values m and log b. We note here that log means log10 (logarithm for a base 10 system), which is log10 in MATLAB syntax. So, enter the following MATLAB command. >> EQLL = polyfit(log10(Radius), log10(Volume), 1) EQLL = 2.9811 >> The two numbers MATLAB returns must be interpreted properly. The first, EQLL(1), is m, and the second, EQLL(2), is log b. This is surmised by comparing log y = m log x + log b to Y = MX +B 0.6344

and examining the polyfit command that was entered. To obtain the value for b, you must raise 10 to the EQLL(2) power. >> b=10.0^EQLL(2) b = 4.3095 So, our empirical formula from the experimental data is V = 4.31 R 2.98 with V in cm3 and R in cm.

Note that the empirical function is reported with the equation constants written in the proper number of significant figures and with the units of the variables reported (outside the function). In comparison to the theoretical expression, V=4 r3 /3 = 4.19 r 3 , our experimental data is fairly good. Next, we want to plot the best-fit line on the log-log plot that has the sphere data plotted as points. To do this, we must first calculate the line-values of volume (Vcalc) for our experimental values of radius.

Department of Engineering Education, Virginia Polytechnic Institute and State University Copyright J.C. Malzahn Kampe, 2004
______________________________________________

Page 12 of 17 EngE 1024 Fall 2004

>> m = EQLL(1) m = 2.9811 >> Vcalc = b .* Radius .^ m Vcalc = 1.0e+003 * Columns 1 through 5 0.0018 0.0074 0.0292 0.0662 0.2687

Columns 6 through 8 0.8999 2.1215 4.7721

To put the best-fit line on the plot, the calculated volume values (Vcalc) are plotted against Radius values using the following commands.

>> figure(2) >> hold on >> plot(Radius, Vcalc, '-')

The generated plot is provided in Figure 8. Note that use of the plot line command above will put the best-fit line on the log-log plot that is being held in Figure Window 2.
10
4

10

10

10

10

10

-1

10

10

10

Figure 8. The log-log plot (before editing to final format) of the experimental sphere data and the best-fit line for that data.

Department of Engineering Education, Virginia Polytechnic Institute and State University Copyright J.C. Malzahn Kampe, 2004
______________________________________________

Page 13 of 17 EngE 1024 Fall 2004

With proper editing using the techniques discussed earlier, your final plot should appear as in Figure 9. We added grid lines to the plot in Figure 9 because they facilitate reading the log scales.

10

Sphere Volume V, cm

10

10

10

10

0 -1 0 1 2

10

10

10

10

Sphere Radius R, cm The influence of radius on sphere volume.

Figure 9. The log-log plot of the sphere data in final format.

We should make a note before we move on to the fitting of exponential functions. In the sphere example, we did not have the (0.0, 0.0) point among our data. If the data set you are plotting has the (0.0, 0.0), MATLAB will automatically omit this point to plot the data on a log-log grid. However, in the use of the polyfit command that takes log10 of the data vectors, you must eliminate the zero from each data vector or MATLAB will respond with an error message.

Department of Engineering Education, Virginia Polytechnic Institute and State University Copyright J.C. Malzahn Kampe, 2004
______________________________________________

Page 14 of 17 EngE 1024 Fall 2004

SEMILOG PLOTS and EXPONENTIAL FUNCTIONS (y = b e mx) To learn the use of MATLABs polyfit command in fitting exponential functions of the form y = b e mx, we will use the experimental data given here on bacterial growth in a Petri dish. Time t, hr: Number of bacteria, N: 0.0 10 0.5 14 1.0 20 1.5 26 2.0 40 2.5 54 3.0 76 3.5 100 4.0 150

Following the procedure established with the prism and sphere data, you should generate the MATLAB plots for the three types of grids (see Figure 10) and select the grid that best lines up the data. >> time = [0:0.5:4]; >> number=[10, 14, 20, 26, 40, 54, 76, 100, 150];
150 10
3

100

10

50

0.5

1.5

2.5

3.5

10

1 0

10

a. linear

b. log-log

10

10

10

0.5

1.5

2.5

3.5

c.

semilogy

Figure 10. MATLAB plots of the number of bacteria in the Petri dish as a function of time for the three types of grids: a.) linear, b.) log-log, and c.) semilogy.

Department of Engineering Education, Virginia Polytechnic Institute and State University Copyright J.C. Malzahn Kampe, 2004
______________________________________________

Page 15 of 17 EngE 1024 Fall 2004

As expected for a natural process, the growth of bacteria follows an exponential growth function (as long as the nutrient source is available), and the semilogy plot in Figure 10 lines up the data best. In order to plot the best-fit line with the data points, the equation constants will be determined with MATLABs polyfit command, but, again, there are some twists. Exponential functions of the y = b e mx form are put into linear form by taking the natural log of both sides of the equation. This yields the expression ln y = m x + ln b which indicates that a linear fit between x and ln y will generate m and ln b values. You should note that in MATLAB syntax log is the natural logarithm to the base e. >> EQSL = polyfit(time, log(number), 1) EQSL = 0.6710 2.3046

So, in this case, EQSL(1) = m and EQSL(2) = ln b. These EQSL values and time are used to calculate bacteria number values of the best-fit line. >> m = EQSL(1) m = 0.6710 >> b = exp(EQSL(2)) b = 10.0197 >> numbercalc = b .* exp( m .* time) numbercalc = Columns 1 through 5 10.0197 14.0143 19.6015 27.4161 38.3461

Columns 6 through 9 53.6336 75.0159 104.9227 146.7526

Department of Engineering Education, Virginia Polytechnic Institute and State University Copyright J.C. Malzahn Kampe, 2004
______________________________________________

Page 16 of 17 EngE 1024 Fall 2004

The best-fit line is added to the semilogy plot of data points using the following MATLAB commands. >> figure(3) >> hold on >> plot(time,numbercalc,'-') The plot so generated is provided in Figure 11.
10
3

10

10

0.5

1.5

2.5

3.5

Figure 11. The semilogy plot (before editing to final format) of the bacterial growth data and the best-fit line.

After proper editing to final format, your plot for the bacterial growth data should appear as the plot provided in Figure 12.
10
3

Number of Bacteria

10

10

0.5

1.5

2.5

3.5

4.5

Time t, hr The influence of time on bacterial growth.

Figure 12. The semilogy plot of the bacterial growth data in final format.

Department of Engineering Education, Virginia Polytechnic Institute and State University Copyright J.C. Malzahn Kampe, 2004
______________________________________________

Page 17 of 17 EngE 1024 Fall 2004

The empirical function for the bacterial growth data should be reported as follows. N = 10 e 0.67 t with N being the number of bacteria and t measured in hours.

We used grid lines in Figure 12 to make reading of the log scale easier.

Das könnte Ihnen auch gefallen