Title: Class Prep
1Class Prep
- Bring to Class
- In-Class Exercise
- Paper for Classroom Printer
- Load Files
- M-Files
- Run MATLAB
- switch Current Directory
2Copy Files
- Copy waves_a.m, waves_b.m from the course folder
to your directory - Run MATLAB, set correct Current Directory
- run waves_a, waves_b
3Week 08-b(11.1-11.2)
- 2-Dimensional PlotsSub-Plots
4Hands-On DEMO Creating a Plot
- The plot function has different forms, depending
on the input arguments. - If y is a vector, plot(y) produces a piecewise
linear graph of the elements of y versus the
index of the elements of y. - gtgt y32 55 64 23 88 62 9
- gtgt plot(y)
- If you specify two vectors as arguments,
plot(x,y) produces a graph of y versus x. - gtgt x00.12pi ysin(x)
- gtgt figure(2),plot(x,y)
5Hands-On DEMO Adding Title Axis Labels
- xlabel('x 02\pi') Note use of special symbol
p - ylabel('Sine of x')
- title('Plot of the Sine Function','FontSize',14)
6To view ALL special symbols available
- \epsilon, \theta, \pi, \uparrow, \newline, etc.
- gtgt doc text
- Then look for text properties in the help index,
then scroll down to list of symbols (see next
slide) - Note that "" will superscript, "_" will
subscript - title('(x_1x_2)3(y_1y_2)3')
- yields
- (x1x2)3(y1y2)3
7(No Transcript)
8Plotting Multiple Curves , Same Axes (1 of 2)
- Problem How can you compare several curves?
- Lets start with the following
- We could plot these using
gtgt X 0.0pi/1002pi gtgt Y1 cos(X) gtgt Y2
3cos(X) gtgt Y3 cos(2X) gtgt Y4 sin(X)
gtgt plot(X,Y1) gtgt hold on gtgt plot(X,Y2) gtgt
plot(X,Y3) gtgt plot(X,Y4)
9Plotting Multiple Curves, Same Axes (2 of 2)
- Or we could do
- Or we could do this
gtgt plot(X,Y1,X,Y2,X,Y3,X,Y4)
gtgt Z Y1Y2Y3Y4 gtgt plot(X,Z)
10Hands-On DEMO LineWidth
- gtgt x00.12pi ysin(x)
- LineWidth (in pixels) of a plot can be changed
- gtgt plot(x,y,'r','LineWidth',4)
11The Legend!
- Multiple x-y pair arguments create multiple
graphs with a single call to plot. - MATLAB automatically cycles through a predefined
(but user settable) list of colors to allow
discrimination between each set of data. - The legend command provides an easy way to
identify the individual plots.
12Hands-On DEMO Multiple Data Sets
- gtgt x 00.056 y1 sin(x) y2 sqrt(x)
- gtgt plot(x,y1, x,y2)
- gtgt legend('sin(x)','sqrt(x)')
- This creates a plot with 2 lines and adds a
2-line legend that describes the lines. See the
help information on legend for more ways to use
this function.
13Multiple Data Sets in One Graph
14Labeling with the text command
waves_a.m Plot Bi-chromatic Wave Profile a1
1 a2 1.5 c1 2.0 c2 1.8 time
00.1100 wave1 a1 sin(c1time) wave2 a2
sin(c2time) a1 wave3 wave1
wave2 plot(time,wave1,time,wave2,time,wave3) axis
(0 100 -2 4) xlabel ('time') ylabel ('wave
elevation') title ('Bi-chromatic Wave
Profile') text(42,-1.2, 'wave 1') x, y
position text(42, 2.7, 'wave 2') text(59, 3.6,
'waves 12')
15(No Transcript)
16Highlighting the Plot
COLOR of the Point or Curve
Marker of the Data Points
Plot Line Styles
17Using Lines or Markers or Both
- Plots should follow the following logic
- Lines whenever plotting analytical functions
like sin(x) where you can compute y for any value
of x - Markers whenever plotting discrete experimental
data or whenever the data are known only
discretely - Both connecting markers with straight lines is
appropriate when you want to show a sequence
gtgt x0.021 gtgt yx.1.5 gtgt yrrandn(size(x)) gt
gt yyy0.1.yr gtgt plot(x,yy,'ro',x,yy)
gtgt x00.021 gtgt yx.1.5 gtgt yrrandn(size(x))
gtgt yyy0.1.yr gtgt plot(x,yy,'rx')
gtgt plot(x,cos(x),'r') gtgt hold on gtgt
plot(x,sin(x),'b--')
18Using Both Markers Lines
- Use lines to show analytical fit through discrete
data
gtgt x0.021 gtgt yx.1.5 gtgt yrrandn(size(x)) gt
gt yyy0.1.yr gtgt plot(x,yy,'x') gtgt
ppolyfit(x,yy,1) p 1.0159 -0.0927 gtgt
hold on gtgt plot(x,polyval(p,x),'r')
gtgt x00.22.pi gtgt ysin(x) gtgt
yrrandn(size(x)) gtgt plot(x,10.yyr,'ro') gtgt
hold on gtgt plot(x,10.y)
19Hands-On DEMO Plotting Lines and Markers
- In general, use MARKERS for sampled points, and
LINES for continuous functions - The statements
- x1 0pi/1002pi
- x2 0pi/102pi
- plot(x1,sin(x1),'r',x2,sin(x2),'bo')
- Plot a red dotted line and places blue o sign
markers at each "sampled" data point. - Next slide displays the graph.
20Plotting Lines and Markers, Example
21Hands-On DEMO What NOT To Do
- Using previous values for x, y1, y2
- gtgt x 00.056 y1 sin(x) y2 sqrt(x)
- gtgt plot(x,y1,'', x,y2, '')
- Note that we are plotting a continuous function
(sqrt) as individual points (using ). - Not Good! Notice how the plus-signs run
together...
22Setting Axis Limits
- By default, MATLAB finds the maxima and minima of
the data to choose the axis limits to span this
range. - The axis command enables you to specify your own
limits - axis(xmin xmax ymin ymax)
- axis(xmin xmax ymin ymax zmin zmax)
- (needed for ex8b)
- Use the command
- axis auto
- to re-enable MATLAB's automatic limit selection.
23Summary of Basic Plot Commands
- axis(xmin, xmax, ymin, ymax) sets axis limit
values (note use of ) - axis off turns off display of axes (plot
unchanged) - axis on turns on display of axes
- grid on/off turns on/off display of a grid
- text(x,y,'string') - places horizontal text
starting at (x,y) - line(x,y) adds line specified by x y vectors
24Specialized 2D Plots
- There are a number of other specialized 2D plots
- area(x,y) builds a stacked area plot
- pie() creates a pie chart (with options)
- bar(x,y) creates a vertical bar chart (with many
options) - stairs(x,y) similar to bar() but shows only
outline - errorbar(x,y,e) plots x vs y with error bars
defined by e - scatter(x,y) creates a scatter plot with options
for markers - And many others (explore these yourself you may
find a good use in a later course)
25Hands-On DEMO Bar, Pie Charts
- gtgt y3 7 2 9 1
- gtgt figure(1), bar(y)
- gtgt title('Bar Chart','FontSize',14)
- gtgt figure(2), pie(y)
- gtgt title('Pie Chart','FontSize',14)
26subplot Command
- There are times when it is better to create
several smaller plots arranged in some kind of
grid subplot(m,n,k) does this - mrows, ncolumns in the grid
- kcurrent focus (numbered row-wise)
- Lets define a 2x3 subplot grid for
subplot(2,3,1) with the focus on the first plot.
3
1
2
3
2
6
4
5
27MATLAB SubPlots
subplot ( m, n, p ) breaks the figure window
into m by n small figures, select the p-th figure
for the current plot
subplot (3, 2, 1) plot (t,wv1) subplot (3,
2, 2) plot (t,wv2) subplot (3, 2, 4) plot
(t, wv1wv2) subplot (3, 2, 6) plot (t,
wv1-wv2)
1
2
3
4
5
6
28 x00.110 y1sin(pix) y2sin(0.5pix)
y3y1y2 z1cos(pix) z2cos(0.5pix)
z3z1-z2 subplot(3,2,1) H1plot(x,y1,'b')
set(H1,'LineWidth',2) subplot(3,2,2)
H2plot(x,z1,'b') set(H2,'LineWidth',2)
subplot(3,2,3) H3plot(x,y2,'m')
set(H3,'LineWidth',2) subplot(3,2,4)
H4plot(x,z2,'m') set(H4,'LineWidth',2)
subplot(3,2,5) H5plot(x,y3,'r')
set(H5,'LineWidth',2) subplot(3,2,6)
H6plot(x,z3,'r') set(H6,'LineWidth',2)
Multiple Plots,Same Figure
29Labeling SubPlots
waves_b.m Plot Bi-chromatic Wave Profile
Display the results in three subplots close all
closes the graphics window a1 1 a2 1.5
c1 2.0 c2 1.8 time 00.1100 wave1 a1
sin(c1time) wave2 a2 sin(c2time) wave3
wave1 wave2 subplot(3,1,1) top
figure plot(time,wave1,'m') axis(0 100 -3 3)
ylabel('wave 1') subplot(3,1,2) middle
figure plot(time,wave2,'g') axis(0 100 -3 3)
ylabel('wave 2') subplot(3,1,3) bottom
figure plot(time,wave3,'r') axis(0 100 -3
3) xlabel('x') ylabel('waves 12')
30(No Transcript)
31Example of Log Plots
- Using a log scale can reveal large dynamic ranges
gtgt xlinspace(.1,10,1000) gtgt damp0.05 gtgt
y1./sqrt((1-x.2).2 (2.damp.x).2) gtgt
plot(x,y) gtgt semilogx(x,y) gtgt loglog(x,y)
Describes the behavior of vibrating systems
32Hands-On DEMO SubPlots using Log Scale
- Plot polynomial function y 2x2 7x 9
Generate the polynomial x linspace (0, 10,
100) y 2x.2 7x 9 plotting the
polynomial subplot (2,2,1), plot (x,y) title
('Polynomial, linear/linear scale') ylabel
('y'), grid on subplot (2,2,2), semilogx
(x,y) title ('Polynomial, log/linear
scale') ylabel ('y'), grid on subplot (2,2,3),
semilogy (x,y) title ('Polynomial, linear/log
scale') xlabel('x'), ylabel ('y'), grid on
33Results on Log Linear Scales
34In-Class Exercise 8b 2-D Plots
- Write a script to produce
- Title in 14-point
- Line red, width2
- Don't forget legend!
- To Print directly from Figure
- File Menu ? Print
35END