Lecture On Plotting Functions For Matlab
Lecture On Plotting Functions For Matlab
CHAPTER LAYOUT
1. 2. 3. 4. 5. 6. 7. 8. XY plotting functions Subplots and Overlay plots Special plot types Plot Editor Function Discovery Regression Basic fitting interface 3-D plots
7/1/2012
xy PLOTTING FUNCTIONS
7/1/2012
xy PLOTTING FUNCTIONS
7/1/2012
xy PLOTTING FUNCTIONS
7/1/2012
xy PLOTTING FUNCTIONS
7/1/2012
xy PLOTTING FUNCTIONS
xy PLOTTING FUNCTIONS
7/1/2012
xy PLOTTING FUNCTIONS
7/1/2012
xy PLOTTING FUNCTIONS
7/1/2012
xy PLOTTING FUNCTIONS
The fplotCommand
fplot similar to plot. It automatically analyzes the function to be plotted and decides how many plotting points to use so that the plot will show all the features of the function. Syntax: fplot(string, [xmin xmax ymin ymax])
>>f= cos(tan(x))-tan(sin(x)); >>fplot(f,[1,2])
7/1/2012
10
5-13
xy PLOTTING FUNCTIONS
7/1/2012
11
xy PLOTTING FUNCTIONS
The fplotCommand
Compare with: >>x = [1:0.01:2]; >>y = cos(tan(x))-tan(sin(x)); >>plot(x,y)
Another syntax for fplot : >>[x,y] = fplot(string,limits)
7/1/2012
12
5-14
xy PLOTTING FUNCTIONS
7/1/2012
13
xy PLOTTING FUNCTIONS
7/1/2012
14
5-16
7/1/2012
xy PLOTTING FUNCTIONS
15
xy PLOTTING FUNCTIONS
Saving Figures
To save a figure that can be opened in subsequent MATLAB sessions, save it in a figure file with the .fig file name extension.
To do this, select Save from the Figure window File menu or click the Save button (the disk icon) on the toolbar. If this is the first time you are saving the file, the Save As dialog box appears. Make sure that the type is MATLAB Figure (*.fig). Specify the name you want assigned to the figure file. Click OK.
7/1/2012
16
xy PLOTTING FUNCTIONS
Exporting Figures
To save the figure in a format that can be used by another application (i.e. .TIFF or .EPS format), perform these steps. 1. Select Export Setup from the File menu. 2. Select Export from the Export Setup dialog. A standard Save As dialog appears. 3. Select the format from the list of formats in the Save As type menu. 4. Enter the name you want to give the file, less the extension. Then click Save.
7/1/2012
17
xy PLOTTING FUNCTIONS
Copy a Figure
On Windows systems, you can also copy a figure to the clipboard and then paste it into another application: 1. Select Copy Options from the Edit menu. The Copying Options page of the Preferences dialog box appears. 2. Complete the fields on the Copying Options page and click OK. 3. Select Copy Figure from the Edit menu.
7/1/2012
18
xy PLOTTING FUNCTIONS
Subplots
On Windows systems, you can also copy a figure to the clipboard and then paste it into another application: 1. Select Copy Options from the Edit menu. The Copying Options page of the Preferences dialog box appears. 2. Complete the fields on the Copying Options page and click OK. 3. Select Copy Figure from the Edit menu.
7/1/2012
19
xy PLOTTING FUNCTIONS
5-20
Subplots You can use the subplot command to obtain several smaller subplots in the same figure. The syntax is subplot(m,n,p). This command divides the Figure window into an array of rectangular panes with m rows and n columns. The variable p tells MATLAB to place the output of the plot command following the subplot command into the pth pane. For example, subplot(3,2,5) creates an array of six panes, three panes deep and two panes across, and directs the next plot to appear in the fifth pane (in the bottom-left corner).
7/1/2012
20
xy PLOTTING FUNCTIONS
5-21
The following script file created Figure 5.21, which shows the plots of the functions y = e-1.2x sin(10x + 5) for 0 x 5 and y = |x3 - 100| for -6 x 6. x = [0:0.01:5]; y = exp(-1.2*x).*sin(10*x+5); subplot(1,2,1) plot(x,y),axis([0 5 -1 1]) x = [-6:0.01:6]; y = abs(x.^3-100); subplot(1,2,2) plot(x,y),axis([-6 6 0 350])
7/1/2012
21
5-22
xy PLOTTING FUNCTIONS
7/1/2012
22
xy PLOTTING FUNCTIONS
5-23
Data Markers and Line Types To plot y versus x with a solid line and u versus v with a dashed line, type plot(x,y,u,v,--), where the symbols -- represent a dashed line. Table 5.21 gives the symbols for other line types. To plot y versus x with asterisks (*) connected with a dotted line, you must plot the data twice by typing plot(x,y,*,x,y,:).
7/1/2012
23
5-24
xy PLOTTING FUNCTIONS
To plot y versus x with green asterisks (*) connected with a red dashed line, you must plot the data twice by typing plot(x,y,g*,x,y,r--).
7/1/2012
24
5-25
xy PLOTTING FUNCTIONS
7/1/2012
25
xy PLOTTING FUNCTIONS
Data markers Dot (.) Asterisk (*) Cross () Circle ( ) Plus sign (+) Square ( ) Diamond ( ) Five-pointed star (w)
Other
Line types . * + s d p Solid line Dashed line Dash-dotted line Dotted line . .
5-26
7/1/2012
26
5-27
xy PLOTTING FUNCTIONS
7/1/2012
27
xy PLOTTING FUNCTIONS
5-28
Labeling Curves and Data The legend command automatically obtains from the plot the line type used for each data set and displays a sample of this line type in the legend box next to the string you selected. The following script file produced the plot in Figure 5.24. x = [0:0.01:2]; y = sinh(x); z = tanh(x); plot(x,y,x,z,--),xlabel(x), ... ylabel(Hyperbolic Sine and Tangent), ... legend(sinh(x),tanh(x))
7/1/2012
28
5-29
xy PLOTTING FUNCTIONS
7/1/2012
29
5-30
xy PLOTTING FUNCTIONS
7/1/2012
30
5-33
xy PLOTTING FUNCTIONS
7/1/2012
31
xy PLOTTING FUNCTIONS
Hints for Improving Plots The following actions, while not required, can nevertheless improve the appearance of your plots: 1. Start scales from zero whenever possible. This technique prevents a false impression of the magnitudes of any variations shown on the plot. 2. Use sensible tick-mark spacing. If the quantities are months, choose a spacing of 12 because 1/10 of a year is not a convenient division. Space tick marks as close as is useful, but no closer. If the data is given monthly over a range of 24 months, 48 tick marks might be too dense, and also unnecessary. (continued )
7/1/2012
5-34
32
xy PLOTTING FUNCTIONS
3. Minimize the number of zeros in the data being plotted. For example, use a scale in millions of dollars when appropriate, instead of a scale in dollars with six zeros after every number. 4. Determine the minimum and maximum data values for each axis before plotting the data. Then set the axis limits to cover the entire data range plus an additional amount to allow convenient tick-mark spacing to be selected. For example, if the data on the x-axis ranges from 1.2 to 9.6, a good choice for axis limits is 0 to 10. This choice allows you to use a tick spacing of 1 or 2. (continued )
5-35
7/1/2012
33
xy PLOTTING FUNCTIONS
5-36
34
Why use log scales? Rectilinear scales cannot properly display variations over wide ranges.
5-37
xy PLOTTING FUNCTIONS
7/1/2012
35
5-38
xy PLOTTING FUNCTIONS
7/1/2012
36
xy PLOTTING FUNCTIONS
Logarithmic Plots It is important to remember the following points when using log scales: 1. You cannot plot negative numbers on a log scale, because the logarithm of a negative number is not defined as a real number. 2. You cannot plot the number 0 on a log scale, because log10 0 = ln 0 = -. You must choose an appropriately small number as the lower limit on the plot.
(continued)
5-39
7/1/2012
37
xy PLOTTING FUNCTIONS
5-40
7/1/2012
38
xy PLOTTING FUNCTIONS
5-41
5. Equal distances on a log scale correspond to multiplication by the same constant (as opposed to addition of the same constant on a rectilinear scale). For example, all numbers that differ by a factor of 10 are separated by the same distance on a log scale. That is, the distance between 0.3 and 3 is the same as the distance between 30 and 300. This separation is referred to as a decade or cycle.
The plot shown in Figure 5.32 covers three decades in x (from 0.1 to 100) and four decades in y and is thus called a four-by-three-cycle plot.
7/1/2012
39
xy PLOTTING FUNCTIONS
5-42
MATLAB has three commands for generating plots having log scales. The appropriate command depends on which axis must have a log scale. 1. Use the loglog(x,y) command to have both scales logarithmic. 2. Use the semilogx(x,y) command to have the x scale logarithmic and the y scale rectilinear. 3. Use the semilogy(x,y) command to have the y scale logarithmic and the x scale rectilinear.
7/1/2012
40
xy PLOTTING FUNCTIONS
Description Creates a bar chart of y versus x. Produces a plot with two y-axes, y1 on the left and y2 on the right.
polar(theta,r,type) Produces a polar plot from the polar coordinates theta and r, using the line type, data marker, and colors specified in the string type. stairs(x,y) stem(x,y) Produces a stairs plot of y versus x. Produces a stem plot of y versus x.
5-43
7/1/2012
41
5-44
xy PLOTTING FUNCTIONS
7/1/2012
42
5-45
xy PLOTTING FUNCTIONS
7/1/2012
43
5-46
xy PLOTTING FUNCTIONS
7/1/2012
44
5-47
xy PLOTTING FUNCTIONS
7/1/2012
45
5-48
xy PLOTTING FUNCTIONS
7/1/2012
46
xy PLOTTING FUNCTIONS
5-49
Interactive Plotting in MATLAB This interface can be advantageous in situations where: You need to create a large number of different types of plots, You must construct plots involving many data sets, You want to add annotations such as rectangles and ellipses, or You want to change plot characteristics such as tick spacing, fonts, bolding, italics, and colors.
7/1/2012
47
xy PLOTTING FUNCTIONS
5-50
The interactive plotting environment in MATLAB is a set of tools for: Creating different types of graphs, Selecting variables to plot directly from the Workspace Browser, Creating and editing subplots, Adding annotations such as lines, arrows, text, rectangles, and ellipses, and Editing properties of graphics objects, such as their color, line weight, and font.
7/1/2012
48
5-51
7/1/2012
xy PLOTTING FUNCTIONS
49
The Figure window with the Figure and Plot Edit toolbars displayed.
xy PLOTTING FUNCTIONS
7/1/2012
50
xy PLOTTING FUNCTIONS
5-53
The Plot Tools interface includes the following three panels associated with a given figure. The Figure Palette: Use this to create and arrange subplots, to view and plot workspace variables, and to add annotations. The Plot Browser: Use this to select and control the visibility of the axes or graphics objects plotted in the figure, and to add data for plotting. The Property Editor: Use this to set basic properties of the selected object and to obtain access to all properties through the Property Inspector.
7/1/2012
51
5-54
xy PLOTTING FUNCTIONS
7/1/2012
52