Word

advertisement
ENGR 1181 | MATLAB 13: 2D Plots 2
Preparation Material
Learning Objectives
1. Create other 2D graphing options in MATLAB (e.g., log, bar, subplot, fplot)
2. Select the proper opportunities to utilize aforementioned 2D graphing options
Topics
Students will read Chapter 5 of the MATLAB book before coming to class. This preparation
material is provided to supplement this reading.
Students will learn more advanced techniques and syntax for creating and formatting twodimensional (2D) plots in MATALB. This material contains the following:
1.
2.
3.
4.
Organize Multiple Plots on the Same Page
Logarithmic Plots
Plots with Special Formats
The fplot() Command
1. Organize Multiple Plots on the Same Page
Multiple plots on one page can be created with the subplot() command.
subplot(m, n, p)
This command creates mxn plots in the Figure Window. The plots are arranged in m rows and n
columns. The variable p defines which plot is active. The plots are numbered from 1 to mxn. The
upper left plot is 1 and the lower right plot is mxn. The numbers increase from left to right within a
row, from the first row to the last.
For example, to create 6 plots arrange in 3 rows and 2 columns, enter the following:
1
ENGR 1181 MATLAB 13: 2D Plots 2
Preparation Material
subplot(3, 2, p)
2
ENGR 1181 MATLAB 13: 2D Plots 2
Preparation Material
The following script files is used to execute the plots from the previous page:
3
ENGR 1181 MATLAB 13: 2D Plots 2
Preparation Material
2. Logarithmic Plots
The following commands are used to plot with logarithmic scales (axes):
Important Facts about Logarithmic Plots
Remember the following about logarithmic plots:
1. Negative numbers cannot be plotted on log scales (because the log of a negative number is
not defined).
2. The number zero (0) cannot be plotted on a log scale.
3. The tick-mark labels on a log scale are the actual values being plotted (they are not the
logarithms of the numbers).
4. Equal distances on a log scale correspond to multiplication by the same constant (as
opposed to a linear scale, where equal distances correspond to the addition of the same
constant).
5. Tick marks are not evenly spaced.
4
ENGR 1181 MATLAB 13: 2D Plots 2
Preparation Material
Plotting with Linear and Log Scales
Plots can look very different based on scale of numbers, but also on the type of scale. The following
shows the variation of graphs for the formula:
y  81 x 
5
ENGR 1181 MATLAB 13: 2D Plots 2
Preparation Material
3. Plots with Special Formats
The following commands are used for plots with special geometry:
bar(x,y)
Creates a bar chart of y vs. x.
stairs(x,y)
Creates a stairs chart of y vs. x.
stem(x,y)
Creates a stem chart of y vs. x.
polar(theta,r)
Creates a polar plot. The vectors theta and r contain the
polar coordinates  and r, respectively.
Below are examples of each of these type of plot formats:
30
30
25
20
Sales (million $)
Sales (million $)
25
15
10
20
15
10
5
0
1988
1990
1992
Year
5
1988
1994
1989
1990
1991
Year
1992
1993
1994
stairs(x,y)
bar(x,y)
30
90
20
120
60
25
10
Sales (million $)
150
30
20
180
15
0
10
210
330
5
240
0
1988
300
270
1989
1990
1991
Year
1992
stem(x,y)
1993
1994
polar(theta,radius)
6
ENGR 1181 MATLAB 13: 2D Plots 2
Preparation Material
4. The fplot() Command
The fplot() command can be used to plot a function with the form: y = f(x)
fplot(‘function’,limits)
Remember the following about the fplot() command:


The function is typed in as a string.
The limits is a vector with the domain of x, and optionally with limits of the y axis:
[ xmin , xmax ]

or
[ xmin , xmax , ymin , ymax ]
Line specifiers can be added, just like with plot command
Plotting with the fplot() Command
For example, let’s say we want to plot the following equation and domain:
y  x 2  4 sin( 2 x)  1
for
3  x  3
The MATLAB syntax would be the following equation and the output is below:
>> fplot('x^2 + 4 * sin(2*x) - 1', [-3 3])
7
Download