Matlab 5 Slide
Matlab 5 Slide
Introduction to MATLAB 7
for Engineers
William J. Palm III
Chapter 5
Advanced Plotting and Model Building
Z.R.K
Z.R.K
5-2 Z.R.K
More? See pages 260-261.
Requirements for a Correct Plot (continued) The grid and axis Commands
The grid command displays gridlines at the tick
6. Sometimes data symbols are connected by marks corresponding to the tick labels. Type grid
lines to help the viewer visualize the data, on to add gridlines; type grid off to stop
especially if there are few data points. plotting gridlines. When used by itself, grid
However, connecting the data points,
toggles this feature on or off, but you might want to
especially with a solid line, might be use grid on and grid off to be sure.
interpreted to imply knowledge of what occurs
You can use the axis command to override the
between the data points. Thus you should be
MATLAB selections for the axis limits. The basic
careful to prevent such misinterpretation. syntax is axis([xmin xmax ymin ymax]).
7. If you are plotting points generated by This command sets the scaling for the x- and y-axes
evaluating a function (as opposed to measured to the minimum and maximum values indicated.
data), do not use a symbol to plot the points. Note that, unlike an array, this command does not
Instead, be sure to generate many points, and
use commas to separate the values.
connect the points with solid lines.
5-9 Z.R.K
5-10 Z.R.K
More? See pages 264-265.
The effects of the axis and grid commands. Figure 5.1–3 The plot(y) function plots the values in y versus
the indices. Figure 5.1–4.
x = 0.1+0.9j; n = [0:0.01:10]; y = x.^n;
plot(y), xlabel(‘Real’), ylabel(‘Imaginary’)
5-11 Z.R.K
5-12 Z.R.K
5-13 Z.R.K
5-14 Z.R.K
polyval Function.
To plot the polynomial
3x5 + 2x4 – 100x3 + 2x2 – 7x + 90
over the range –6 d x d 6 with a spacing
of 0.01, you type
>>x = [-6:0.01:6];
>>p = [3,2,-100,2,-7,90];
>>grid on
>>plot(x,polyval(p,x)), ...
xlabel(’x’), ylabel(’p’)
More? See page 268. 5-16
5-15 Z.R.K Z.R.K
Subplots
You can use the subplot command to obtain
On Windows systems, you can also copy several smaller “subplots” in the same figure.
a figure to the clipboard and then paste
The syntax is subplot(m,n,p). This
it into another application (like Paint):
command divides the Figure window into an
1. Select Copy Options from the Edit
array of rectangular panes with m rows and n
menu. The Copying Options page of the
Preferences dialog box appears. columns. The variable p tells MATLAB to place
the output of the plot command following the
2. Complete the fields on the Copying
subplot command into the pth pane.
Options page and click OK.
For example, subplot(3,2,5) creates an array
3. Select Copy Figure from the Edit menu.
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).
5-19 Z.R.K
5-20 Z.R.K
The following script file created Figure 5.2–1, Application of the subplot command. Figure 5.2–1
Data Markers and Line Types Data plotted using asterisks connected with a dotted line.
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.2–1
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,’:’).
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-23 Z.R.K
5-24 Z.R.K
help.
5-25 Z.R.K
5-26 Z.R.K
More? See pages 273-274.
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-27 Z.R.K
5-28 Z.R.K
The gtext and text commands are also useful. Figure 5.2–5 Graphical solution of equations: Circuit representation of
a power supply and a load. Figure 5.2–6
i1 = 0.16 (e0.12v2 – 1)
R1 = 30 :, v1 = +15V. & v2 =(0 to 20)V
5-29 Z.R.K
See page 276. 5-30 Z.R.K
Plot of the load line and the device curve for Application of the hold command. Figure 5.2–8
Previous circuit. Figure 5.2–7
5-31 Z.R.K
5-32 Z.R.K
See page 279.
Why use log scales? Rectilinear scales cannot properly display A loglog plot can display wide variations in data values.
variations over wide ranges. Figure 5.3–1 Figure 5.3–2
>> x=[0:.01:100]; y=sqrt((100*(1-0.01*x.^2).^2+0.02*x.^2)...
./ ((1-x.^2).^2+0.1*x.^2)); plot(x,y)
5-33 Z.R.K
5-34 Z.R.K
See page 282.
Two data sets plotted on four types of plots. Figure 5.3–3 Application of logarithmic plots: An RC circuit.
Figure 5.3–4
vo /vi = (1 / | 1 + RCs | )
s = jw, Xc = 1/jwC, Xc = 1/sC .
5-39 Z.R.K
See page 285. 5-40 Z.R.K
5-41 Z.R.K
See pages 286-287. Z.R.K
5-43 Z.R.K
5-44 Z.R.K
The Figure window with the Figure toolbar displayed. The Figure window with the Figure and Plot Edit toolbars displayed.
Figure 5.4–1 Figure 5.4–2
The Figure window with the Plot Tools activated. Figure 5.4–3
The Plot Tools interface includes the following
three panels associated with a given figure.
x The Figure Palette: Use this to create and
arrange subplots, to view and plot workspace
variables, and to add annotations.
x 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.
x The Property Editor: Use this to set basic
properties of the selected object and to obtain
access to all properties through the Property
Inspector.
5-53 Z.R.K
5-54 Z.R.K
>>[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-57 Z.R.K
More? See pages 334-335. 5-58 Z.R.K
See the next slide.
22 2
A plot of the surface z = xe-[(x-y ) +y ]
created with the mesh function. Figure 5.8–2 The following session generates the contour
plot of the function whose surface plot is shown
2)2+y2]
in Figure 5.8–2; namely, z = xe-[(x-y ,
for 2 d x d 2 and 2 d y d 2, with a spacing
of 0.01. This plot appears in Figure 5.8–3.
>>[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-59 Z.R.K
More? See pages 335-336. 5-60 Z.R.K
5-61 Z.R.K
More? See page 337. 5-62 Z.R.K
2 2
Plots of the surface z = xe-(x +y ) created with the mesh Example to plot 3D
function and its variant forms: a) mesh, b) meshc,
c) meshz, and d) waterfall. Figure 5.8–4 If you want to see the famous Mexican
hat , type the following commands :
>> [x y] = meshgrid(-8:0.5:8);
>> r = sqrt(x.ˆ2 + y.ˆ2) + eps;
>> z = sin(r)./r;
>> mesh(z);
Why + eps ?
Try surf(z) to generate a faceted (tiled)
view of the surface.
Then try with surf(z) the shading
interp command.
5-63 Z.R.K 5-64 Z.R.K
Gradients and level surfaces Example to plot 3D with surface and colormap:
See Primer in Matlab
figure(1) ; clf
t = linspace(0, 2*pi, 512);
[u,v] = meshgrid(t) ;
a = -0.2 ; b = .5 ; c = .1 ;
n = 2;
x = (a*(1-v/(2*pi)) .* (1+cos(u)) + c) .* cos(n*v);
y = (a*(1-v/(2*pi)) .* (1+cos(u)) + c) .* sin(n*v);
z = b*v/(2*pi) + a*(1-v/(2*pi)) .* sin(u);
surf(x,y,z,y)
shading interp
axis off
axis equal
colormap(hsv(1024))
material shiny
lighting gouraud
lightangle(80, -40)
lightangle(-90, 60)
view([-150 10])
Z.R.K Z.R.K
5-70
End of Chapter 5