You are on page 1of 3

W.R.

Wilcox, Clarkson University, 1 May 2005

2. Plotting data using MATLAB


It is assumed that you have imported the CO vapor pressure data into MATLAB as described in 2. Importing data into MATLAB. Now you are going to plot these data. MATLAB plots one vector versus another, so you must extract the T and P vectors from the data array. Do this by the following commands: >> T = data(: , 3), P = data(: , 1) The colon (:) in each command tells MATLAB to take all rows from variable data, the "3" indicates the third column, and 1 the first column. Note that we now have variables T and P listed in the Workspace window. It is customary to plot the dependent variable (here, P) on the vertical y-axis versus the independent variable (here, T) on the horizontal x-axis. That is, one changes T in order to change P and not vice versa. Normally it is also good practice to plot data as points rather than connecting the values by straight lines, as is done when plotting functions in MATLAB. The scatter command could be used, but here you will use the plot command with 'o' telling MATLAB to show the points as circles: >> plot(T, P, 'o'); xlabel('temperature (K)'); ylabel('Vapor pressure (Pa)'); >> legend('Experimental values'); title('The vapor pressure of CO versus temperature'); Note that on this graph the vapor pressure values at low pressure are indistinguishable from 0. This happened because MATLAB automatically uses linear scales (Cartesian coordinates). Because the vapor pressures at higher temperatures are many orders of magnitude larger than those at lower temperatures, the linear y-scale cannot show small deviations from 0 -- even though these are very important. (To show the T and P values for a point, click on the Data Cursor icon and then on the point.) To display the smaller vapor pressures so they can be read from the graph, we need to expand the y-axis at low values and contract it at high values. This is readily done using a logarithmic scale. In the Figure window, click on Edit, Axes Properties. On the Property EditorAxes window, click on the Y Axis tab and change the Y Scale to Log. Save your figure by clicking on File, Save as. Close the Figure window and return to the Command window. Go to File, Open to retrieve your figure. Click on the legend and use your mouse to move it to a more convenient location. Save it again. To place your figure in a Word document, click on Edit, Copy Options and click on Metafile and Transparent background; then click on Edit, Copy Figure. This places the figure on the Windows clipboard. Open Word and click on Edit, Paste Special, Picture (Windows Metafile), to produce Figure 2.1 on the next page. If you want to convince the reader that this figure was generated in MATLAB, then while in the Figure window press the keys Alt, Print Screen at the same time. This places the entire window on the clipboard. Then, in Word, click on Edit, Paste Special, Bitmap. This yields Figure 2.2. You are now ready to proceed to fit the CO vapor pressure data using 3. Use of MATLAB to fit data to a polynomial.
1

.
The vapor pressure of CO versus temperature

10

10

Vapor pressure (Pa)

10

10

Experimental values 10
3

10

50

60

70

80

90 100 temperature (K)

110

120

130

140

Figure 2.1. Semi-log plot of the vapor pressure of CO versus temperature. The y-axis uses a logarithmic scale while the x-axis is linear.

Figure 2.2. Copy of the MATLAB Figures window showing the semi-log CO vapor pressure plot. Made using Alt, Print Screen.

You might also like