Advanced Plotting
Advanced Plotting
5-2
An Example: The following MATLAB session plots y
04 18x for 0 x 52, where y represents the
height of a rocket after launch, in miles, and x is
the horizontal (downrange) distance in miles.
>>x = 0:0.1:52;
>>y = 0.4*sqrt(1.8*x);
>>plot(x,y)
>>xlabel(’Distance (miles)’)
>>ylabel(’Height (miles)’)
>>title(’Rocket Height as a Function of
Downrange Distance’)
5-3
The autoscaling feature in MATLAB selects tick-mark
spacing.
5-4
The plot will appear in the Figure window. You can
obtain a hard copy of the plot in several ways:
5-5
When you have finished with the plot, close the
figure window by selecting Close from the File
menu in the figure window.
5-6
Requirements for a Correct Plot
(Table 5.1-1, page 221)
(continued …)
5-8
Requirements for a Correct Plot (continued)
5-9
The grid and axis Commands
5-11
The fplot command is a “smart” plotting function. Figure 5.1–3a
See pages 223-224.
5-12
The function in Figure 5.1–3b generated with the plot
command, which gives more control than the fplot
command.
5-13
Plotting Polynomials with the polyval Function.
>>x = -6:0.01:6;
>>p = [3,2,-100,2,-7,90];
>>plot(x,polyval(p,x)),xlabel(’x’), ...
ylabel(’p’)
5-14
Saving Figures
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.
5-15
Exporting Figures
5-17
Hints for Improving Plots (Table 5.1-3, page 226)
(continued …)
5-18
Hints for Improving Plots (continued)
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-19
Hints for Improving Plots (continued)
5-20
Subplots
5-21
The following script file created Figure 5.2–1, which shows
the plots of the functions y e12x sin(10x 5) for 0 x 5
and y x3 100for 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])
5-23
Data Markers and Line Types
5-24
Overlay Plots
You can use the following variants of the MATLAB
basic plotting functions plot(x,y) and plot(y) to create
overlay plots:
■ plot(A) plots the columns of A versus their indices
and generates n curves, where A is a matrix with m
rows and n columns.
■ plot(x,A) plots the matrix A versus the vector x,
where x is either a row vector or a column vector and A
is a matrix with m rows and n columns.
If the length of x is m, then each column of A is plotted
versus the vector x. There will be as many curves as
there are columns of A. If x has length n, then each
row of A is plotted versus the vector x. There will be as
many curves as there are rows of A.
5-24
■ plot(A,x) plots the vector x versus the matrix A. If the
length of x is m, then x is plotted versus the columns of
A. There will be as many curves as there are columns
of A. If the length of x is n, then x is plotted versus the
rows of A. There will be as many curves as there are
rows of A.
■ plot(A,B) plots the columns of the matrix B versus the
columns of the matrix A.
5-24
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--’).
5-25
Specifiers for data markers, line types, and colors.
Table 5.2–1, page 228.
Data markers† Line types Colors
Other data markers are available. Search for “markers” in MATLAB help.
†
5-26
Use of data markers. Figure 5.2–2, page 229.
More?
See
pages
273-274.
5-27
Labeling Curves and Data
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)’)
5-28
Application of the legend command. Figure 5.2–3, page 230
5-29
Application of the hold command. Figure 5.2–4, page 231
5-30
Why use log scales? Rectilinear scales cannot properly
display variations over wide ranges. Figure 5.2-5a, page
233.
5-31
A log-log plot can display wide variations in data values.
Figure 5.2-5b, page 233.
See
pages
233-
234.
5-32
Logarithmic Plots
(continued…)
5-33
Logarithmic Plots (continued)
(continued…)
5-34
Logarithmic Plots (continued)
5-35
MATLAB has three commands for generating
plots having log scales. The appropriate
command depends on which axis must have a
log scale.
5-36
Specialized plot commands. Table 5.2-3, page 236
Command Description
5-37
Exponential and Power Functions Plotted on
Log Scales (Figure 5.2-6, page 235)
See pages
236-237.
5-39
Error Plots, Figure 5.2-8, page 238.
5-41
Interactive Plotting in MATLAB
5-43
The Figure toolbar displayed. Figure 5.3–1, page 241.
5-44
The Figure and Plot Edit toolbars displayed. Figure 5.3–2,
page 243.
5-45
The Plot Tools interface includes the following three
panels associated with a given figure.
5-46
The Figure window with the Plot Tools
activated. Figure 5.3-3, page 244.
5-47
Three-Dimensional Line Plots:
>>t = 0:pi/50:10*pi;
>>plot3(exp(-0.05*t).*sin(t),...
exp(-0.05*t).*cos(t),t),...
xlabel(’x’),ylabel(’y’),zlabel(’z’),grid
5-49
Surface Plots:
The following session shows how to generate the
surface plot of the function z xe[(xy2)2y2], for 2 x 2
and 2 y 2, with a spacing of 0.1. This plot appears in
Figure 5.4–2, page 248.
>>[X,Y] = meshgrid(-2:0.1:2);
>>Z = X.*exp(-((X-Y.^2).^2+Y.^2));
>>mesh(X,Y,Z),xlabel(’x’),ylabel(’y’),...
zlabel(’z’)
5-50
A plot of the surface z xe[(xy2)2y2] created with the mesh
function. Figure 5.8–2
5-51
The following session generates the contour plot of the
function whose surface plot is shown in Figure 5.8–2;
namely, z xe[(xy2)2y2], for 2 x 2 and 2 y 2, with
a spacing of 0.1. This plot appears in Figure 5.4–3, page
249.
>>[X,Y] = meshgrid(-2:0.1:2);
>>Z = X.*exp(-((X- Y.^2).^2+Y.^2));
>>contour(X,Y,Z),xlabel(’x’),ylabel(’y’)
5-52
A contour plot of the surface z xe[(xy2)2y2] created with the
contour function. Figure 5.4–3
5-53
Three-dimensional plotting functions. Table 5.4–1, page 250.
Function Description
contour(x,y,z) Creates a contour plot.
5-54
Function Discovery
Function discovery is the process of finding, or
“discovering,” a function that can describe a particular set of
data.
The following three function types can often describe
physical phenomena.
5-54
Function Discovery(Cont..
Each function gives a straight line when plotted using a
speci c set of axes:
1. The linear function y = mx + b gives a straight line
when plotted on rectilinear axes. Its slope is m and its
intercept is b.
2. The power function y = bxm gives a straight line
when plotted on log-log axes.
3. The exponential function y = b(10)mx and its
equivalent form y = bemx give a straight line when
plotted on a semilog plot whose y axis is logarithmic.
5-54
Function Discovery(Cont..
5-54
Plots of the surface z xe(x2y2) created with the mesh function
and its variant forms: meshc, meshz, and waterfall. a)
mesh, b) meshc, c) meshz, d) waterfall.
Figure 5.4–4, page 250.
5-55
The following slides contain the figures from
the homework problems.
5-56
Graphical solution of equations: Circuit representation of a
power supply and a load. Figure P25, page 257.
5-57
Figure P26 on page 257.
5-58
Figure P35 on page 260.
5-59