Mat Lab Chapter 5
Mat Lab Chapter 5
Introduction to MATLAB
for Engineers, Third Edition
Chapter 5
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)
(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
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;
[(xy2)2y2]
namely, z xe , 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-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