# 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 MATLAB’s 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.

00. So.5 8.00 63.00 68. 4. PLOTTING THE DATA After you have entered the data in the Command Window.5.0. >> >> SArea = [ 31.00.00.5 2. Figure 1. 36.00. cm2 : 1.C.00. 82. Virginia Polytechnic Institute and State University Copyright J. 10. >> Length = [ 1. 89.0 89.0.0 4.00. in the tool bar for Workspace. here.0 10. 3. select Length and then. 63.5 3.5.5.00. 9.00. 6. remember that commas or spaces can be used to delimit (separate) the elements within a row of a MATLAB matrix.0]. 8. cm : Surface Area SA. the independent variable is length because the surface area was measured for different lengths of the same type of prism.00 74.0].00 57. select the variable in the Workspace that contains the independent variable values. 2.0 These data can be entered in the MATLAB command window as two separate vectors (here called Length and SArea).00 50.Department of Engineering Education.00 82. open the plotting menu by clicking on the selection arrow to the right of the graphing icon. Length L. 7. Click Here Note that Length is selected. 57.00.5. 68. The MATLAB desktop. 50. When you enter the data in the command window.0. 74. 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.0 7.5 9.00 36.0.00 31.5 5.5.0 6. and semicolons are used to delimit the rows. Malzahn Kampe. . 44. 5.00 44.

Repeat the plotting process to generate a log-log plot and a semilogy plot. . Indicate proper data sources here. Malzahn Kampe. 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. you can make the decision as to which type of plot lines up the data best. Note that the data points are “selected” in order to see the data Property Editor. When you have the three plot types (as in Figure 4). Choose line and marker options here. and use the “Plot in a New Figure” option (see Figure 2) each time so that all three plot types are available to you. The data point are shown to be “selected” so that the Property Editor is visible.C. 2004 ______________________________________________ Page 4 of 17 EngE 1024 Fall 2004 Click here to enable plot editing. MATLAB’s figure window with the data Property Editor made visible. Figure 3.

semilogy Figure 4.5 10 0 2 4 6 8 10 c. Use a straight edge to select the plot type.) semilogy.9 70 10 60 1. linear b. 2004 ______________________________________________ Page 5 of 17 EngE 1024 Fall 2004 90 10 80 1. we will use MATLAB’s 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. Three types of plots for the prism length and surface area data: a. And.5 1.7 10 50 10 40 1.7 10 1. if the size and shape of the prism cross section is identical for all the prisms that were measured.6 10 1. An examination of the plots provided in Figure 4 indicates that we should proceed with a linear plot of the prism data. Malzahn Kampe. then surface area of the prisms should be a linear function of prism length. Although MATLAB provides linear fit capabilities under the Basic Fitting option in the Tools menu of the Figure Window. This makes sense because. if you can follow the polyfit procedure for a linear function.Department of Engineering Education. and c.6 30 10 0 2 4 6 8 10 10 0 10 1 a. b.C.9 10 1.) log-log. log-log 1. Our next job is to determine the equation constants for the linear function. These are used to determine which type of plot best lines up the data. .) linear. Virginia Polytechnic Institute and State University Copyright J.8 1. it might be easier for you to understand the polyfit procedures for power and exponential functions.8 10 1.

1182 56.4727 Columns 6 through 10 62.* Length + EQ(2) SAcalc = Columns 1 through 5 31.) To produce the best-fit line. Malzahn Kampe.8273 69.4091 43.5364 81. (Note that we report the equation constants in the proper number of significant figures.7 with SA in cm2 and L in cm. and the second is the coefficient for the independent variable raised to the zero power (which is 1 for any value of the variable).Department of Engineering Education. .1) EQ = 6.. also. enter this command. that SArea contains the dependent variable values. 2004 ______________________________________________ Page 6 of 17 EngE 1024 Fall 2004 A first-order (i.7000 In entering the command above. At this point. >> EQ = polyfit(Length. you have told MATLAB that Length contains the independent variable values.1818 75. You probably know these equation constants better as m and b.3545 24. The first is the coefficient for the independent variable raised to the first power.e. EQ(1) = m and EQ(2) = b and we know that the empirical function that describes the prism data is SA = 6. 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). >> SAcalc = EQ(1). and that the two equation constants returned are to be stored in the matrix called EQ.0545 37.8909 88.2455 You should notice that the value assignment statement we used to generate SAcalc has the y = mx + b form and. we use the equation constants in matrix EQ and the Length values to calculate new surface area values called SAcalc.C. Virginia Polytechnic Institute and State University Copyright J.35 L + 24. that a first order fit is required.7636 50. linear) fit will produce two equation constants. First-order fit: y = m x 1 + b x 0 à y=m x + b In MATLAB’s Command Window.SArea.

add a title to the plot.. Double click anywhere on the axes and you will bring up the Property Editor for the axes. go to the figure window that holds the plot. in order to place the points above the best-fit line and add point highlighting.SAcalc. provided in Figure 5. Malzahn Kampe. is now ready to edit into final graphing format. enter these commands in MATLAB’s Command Window >> figure(1) >> hold on % Substitute your figure number for your linear plot in place of 1. 2004 ______________________________________________ Page 7 of 17 EngE 1024 Fall 2004 Now we want to plot the SAcalc values (i. 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. label the axes.'-') % This command plots the best-fit line.Department of Engineering Education. The line commands used in the Command Window are as follows. >> plot(Length. the calculated values of surface area) against Length as a line on the same linear plot that holds the plotted data points. Virginia Polytechnic Institute and State University Copyright J.e. % This command will retain the plot of points when you plot the line. .C. To do this. To edit the plot into final format. the first time as triangle markers and the second time as point markers. For this plot. you can change the axes limits. we have deleted the original set of point markers in the figure window and re-plotted the data twice from the Command Window. You should edit your plot to obtain a version like the plot provided in Figure 6. 90 80 70 60 50 40 30 0 2 4 6 8 10 Figure 5. and even reverse the axes or convert them to logarithmic scales. The plot obtained after these commands are executed. Within this Property Editor.

two things should be noted. An edited version of the linear plot in final format.'. After all.Department of Engineering Education. for example. might misread dials when adjusting the independent variable value.'^') >> plot(Length. cm The influence of prism length on surface area for a series of prisms with constant cross section and varying lengths. To get . while the dependent values that are measured may be subject to a wide variety of uncertainty sources. The second thing to note is that MATLAB’s figure window offers Basic Fitting under the Tools menu. Why? Because we made the assumption that there is less uncertainty in our independent variable values and. In our procedure to plot the best-fit line. it is worthwhile to look at some other line commands for plotting. 2004 ______________________________________________ Page 8 of 17 EngE 1024 Fall 2004 >> plot(Length. and a linear fit is available there. First.') 100 90 Prism Surface Area SA. These will be useful to you if you use MATLAB programming to solve problems and wish to produce a plot from your MATLAB code. SArea. we calculated new dependent (y-axis) values. You might use this feature to check your work. Before we move on to power and exponential functions. we control the independent variable values. even for linear plots.C. Malzahn Kampe. This assumption of less uncertainty in the independent values is likely to be true. therefore. but we kept our independent (x-axis) values to plot the best-fit line. but there are no guarantees. Experimenters. SArea. Figure 6. cm 2 80 70 60 50 40 30 20 0 2 4 6 8 10 Prism Length L. but always provide your instructor with the polyfit work you do to obtain m and b and to plot the best-fit line. Virginia Polytechnic Institute and State University Copyright J. they should be kept.

Create axes in tiled positions. the “hold” command by itself is a toggle command. plot . polar .Printer defaults. by lines from the Command Window or with the Property Editors in the figure window. texlabel . title . axis zoom grid box hold axes subplot - Control axis scaling and appearance.C.Polar coordinate plot.Graphs with y tick labels on the left and right. Hold current graph.X-axis label. xlabel .Semi-log scale plot. The “xlabel” statement labels the x-axis with argument text enclosed within the single quotes and parentheses.Semi-log scale plot. Axis control.Graph title. 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”). Malzahn Kampe. Zoom in and out on a 2-D plot. Grid lines.Linear plot. Whichever way you choose to edit the plot. plotyy .Department of Engineering Education.Log-log scale plot.Tools for editing and annotating plots.Place text with mouse.Set paper orientation. Virginia Polytechnic Institute and State University Copyright J. . Axis box. specgraph. >> help graph2d Two dimensional graphs. Graph annotation. semilogx . ylabel . and you should note that “grid” is a toggle command – turning the grid on and off with each sequential execution. plotedit .Text annotation. The “ylabel” statement similarly labels the y-axis. The “axis” command allows adjustments to the axes limits. gtext . However. The “grid” command inserts grid lines along both axes. Elementary X-Y graphs. or save graph to M-file. . Create axes in arbitrary positions.Print graph or Simulink system. .Y-axis label. you should eventually achieve something similar to the plot in Figure 6. See also graph3d. loglog . . 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. Hardcopy and print printopt orient printing. The “title” command places the argument text enclosed in single quotes and parentheses at the top of the plot. semilogy .Produces TeX format from a character string text . the title should then be moved to a location beneath the x-axis label.

cm3 : 0. Radius R. semilogy Figure 7. 4900].0 4. we will use the experimental data for the volume of a sphere as radius changes that are given here. 4900.5 2000.0.00.50 63. Plotting in MATLAB as we did for the prism data example.5].0 2.750. 10.20 1. >>Radius = [0. 32. 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. linear b. 2000. 8.50.50.00 275 6. 1. 63. and c. As expected. MATLAB plots of the sphere volume as a function of radius on the three types of grids: a.20. Malzahn Kampe. 4. Virginia Polytechnic Institute and State University Copyright J.) log-log.) semilogy. >>Volume = [1.00. the log-log plot produces the best line-up of the data points. b. 1.C. 6.Department of Engineering Education.90.) linear. .50 1. 275. cm: Volume V. 8.90 32.75.0.750 1. log-log 4 10 3 10 2 10 1 10 10 0 0 2 4 6 8 10 12 c. 2004 ______________________________________________ Page 10 of 17 EngE 1024 Fall 2004 LOG-LOG PLOTS and POWER FUNCTIONS (y = b xm ) To learn how to use MATLAB’s polyfit command to find the equation constants for a power function. 7. Of course.00 900.00. 2.75 7.00 10.. 4 5000 4500 4000 3500 3000 10 3 10 2 2500 2000 1500 1000 500 10 1 10 0 0 0 2 4 6 8 10 12 10 -1 10 0 1 2 10 10 10 a.. 900. you should check the line-up of data points on the three types of grid (see Figure 7).

enter the following MATLAB command. This is surmised by comparing log y = m log x + log b to Y = MX +B 0. 1) EQLL = 2. So.C.98 with V in cm3 and R in cm. >> EQLL = polyfit(log10(Radius). Malzahn Kampe. In comparison to the theoretical expression. V=4 π r3 /3 = 4.9811 >> The two numbers MATLAB returns must be interpreted properly.0^EQLL(2) b = 4. we must first calculate the line-values of volume (Vcalc) for our experimental values of radius. >> b=10. is log b. 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. which is “log10” in MATLAB syntax. . 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. The first. 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.Department of Engineering Education. We note here that “log” means log10 (logarithm for a base 10 system). To obtain the value for b. the resulting expression looks like this.31 R 2. Virginia Polytechnic Institute and State University Copyright J. To do this. is m. we want to plot the best-fit line on the log-log plot that has the sphere data plotted as points.19 r 3 .6344 and examining the “polyfit” command that was entered. If this is done. log10(Volume). EQLL(2). and the second. our empirical formula from the experimental data is V = 4. our experimental data is fairly good. we can determine the equation constants using MATLAB’s polyfit command. Next. you must raise 10 to the EQLL(2) power. EQLL(1).3095 So. 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).

^ m Vcalc = 1.7721 To put the best-fit line on the plot.0292 0.8999 2.2687 Columns 6 through 8 0. 10 4 10 3 10 2 10 1 10 0 10 -1 10 0 10 1 10 2 Figure 8.0018 0. Malzahn Kampe. Virginia Polytechnic Institute and State University Copyright J. '-') The generated plot is provided in Figure 8. the calculated volume values (Vcalc) are plotted against Radius values using the following commands. .* Radius . 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.0074 0.9811 >> Vcalc = b . Vcalc. >> figure(2) >> hold on >> plot(Radius.0e+003 * Columns 1 through 5 0.C.Department of Engineering Education. 2004 ______________________________________________ Page 12 of 17 EngE 1024 Fall 2004 >> m = EQLL(1) m = 2.1215 4. The log-log plot (before editing to final format) of the experimental sphere data and the best-fit line for that data.0662 0.

We added grid lines to the plot in Figure 9 because they facilitate reading the log scales. 4 10 3 Sphere Volume V. Figure 9. in the use of the polyfit command that takes log10 of the data vectors.0) point among our data. The log-log plot of the sphere data in final format. 0. 2004 ______________________________________________ Page 13 of 17 EngE 1024 Fall 2004 With proper editing using the techniques discussed earlier. cm 3 10 10 2 10 1 10 0 -1 0 1 2 10 10 10 10 Sphere Radius R. If the data set you are plotting has the (0. 0.Department of Engineering Education. . In the sphere example.0. we did not have the (0. However. Malzahn Kampe. MATLAB will automatically omit this point to plot the data on a log-log grid. your final plot should appear as in Figure 9. We should make a note before we move on to the fitting of exponential functions. Virginia Polytechnic Institute and State University Copyright J. cm The influence of radius on sphere volume.0).0. you must eliminate the zero from each data vector or MATLAB will respond with an error message.C.

Malzahn Kampe.5 14 1. >> time = [0:0. 20.0 150 Following the procedure established with the prism and sphere data.) linear.5 2 2.5 26 2.) semilogy. N: 0. semilogy Figure 10.0 20 1. we will use the experimental data given here on bacterial growth in a Petri dish. 100.Department of Engineering Education. 150].5 3 3. Virginia Polytechnic Institute and State University Copyright J.5 54 3. 40. 26.0 40 2.5 1 1.5 1 1. 14.5 3 3. 150 10 3 100 2 10 50 0 0 0. MATLAB plots of the number of bacteria in the Petri dish as a function of time for the three types of grids: a. 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. 76.5:4]. linear b. .5 2 2. 2004 ______________________________________________ Page 14 of 17 EngE 1024 Fall 2004 SEMILOG PLOTS and EXPONENTIAL FUNCTIONS (y = b e mx) To learn the use of MATLAB’s polyfit command in fitting exponential functions of the form y = b e mx.) log-log.5 4 10 1 0 10 a.0 76 3. and c.0 10 0. log-log 3 10 2 10 1 10 0 0. 54. >> number=[10. b. Time t.C.5 100 4.5 4 c. hr: Number of bacteria.

7526 .9227 146. You should note that in MATLAB syntax “log” is the natural logarithm to the base e. the growth of bacteria follows an exponential growth function (as long as the nutrient source is available). log(number). and the semilogy plot in Figure 10 lines up the data best.C. 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. there are some twists. EQSL(1) = m and EQSL(2) = ln b.0159 104. 1) EQSL = 0. Virginia Polytechnic Institute and State University Copyright J. These EQSL values and time are used to calculate bacteria number values of the best-fit line. >> EQSL = polyfit(time.6710 2.* exp( m . but. the equation constants will be determined with MATLAB’s polyfit command. again.3046 So.6015 27.* time) numbercalc = Columns 1 through 5 10.6710 >> b = exp(EQSL(2)) b = 10. In order to plot the best-fit line with the data points.3461 Columns 6 through 9 53. >> m = EQSL(1) m = 0.0197 14. in this case.0143 19. 2004 ______________________________________________ Page 15 of 17 EngE 1024 Fall 2004 As expected for a natural process.4161 38.0197 >> numbercalc = b . Malzahn Kampe.Department of Engineering Education.6336 75. 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.

Department of Engineering Education. Malzahn Kampe. Virginia Polytechnic Institute and State University Copyright J. Figure 12.5 4 Figure 11. The semilogy plot of the bacterial growth data in final format. The semilogy plot (before editing to final format) of the bacterial growth data and the best-fit line. After proper editing to final format.5 1 1.5 3 3.C.5 2 2. hr The influence of time on bacterial growth.5 2 2.'-') The plot so generated is provided in Figure 11. 10 3 Number of Bacteria 2 10 10 1 0 0.numbercalc. >> figure(3) >> hold on >> plot(time. 10 3 10 2 10 1 0 0.5 3 3. 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.5 Time t. your plot for the bacterial growth data should appear as the plot provided in Figure 12.5 4 4.5 1 1. .

Malzahn Kampe. Virginia Polytechnic Institute and State University Copyright J.Department of Engineering Education.C.67 t with N being the number of bacteria and t measured in hours. . N = 10 e 0. We used grid lines in Figure 12 to make reading of the log scale easier. 2004 ______________________________________________ Page 17 of 17 EngE 1024 Fall 2004 The empirical function for the bacterial growth data should be reported as follows.

Sign up to vote on this title