Upload
clement-french
View
212
Download
0
Embed Size (px)
Citation preview
EGR 115 Introduction to Computing for Engineers
2D Plotting – Part I
Wednesday 17 Sept 2014 EGR 115 Introduction to Computing for Engineers
Lecture Outline
Wednesday 17 Sept 2014 EGR 115 Introduction to Computing for Engineers
• 2D Plotting Additional plotting features
Slide 2 of 13
2D PlottingAdditional Plotting Features – LineSpec Review
Wednesday 17 Sept 2014 EGR 115 Introduction to Computing for Engineers
• Review of Line Specifications
• For Example:>> x=0:0.1:pi;>> plot(x, sin(x), ‘k>:')
Line Color Marker Line Style b blue . point - solid g green o circle : dotted r red x x-mark -. dashdot c cyan + plus -- dashed m magenta * star (none) no line y yellow s square k black d diamond w white v triangle (down) ^ triangle (up) < triangle (left) > triangle (right) p pentagram h hexagram
0 0.5 1 1.5 2 2.5 3 3.50
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Slide 3 of 13
2D PlottingAdditional Plotting Features – Log Scales
Wednesday 17 Sept 2014 EGR 115 Introduction to Computing for Engineers
• Logarithmic Scales Consider the low-pass filter
with transfer function:
o where, is the filter cut-off frequency and f the frequency in Hz.
Assuming , plot the attenuation () of the filter for 0 < f < 1,000 Hz.
>> f = 0:1000;>> fc = 200;>> G = 1 ./ (1 + 1i*(f/fc));>> plot(f, sqrt(G .* conj(G)))>> xlabel('Freq in Hz');>> ylabel('Attenuation')>> title('Attenuation of a Low-Pass Filter')
1
( )1 / c
G fj f f
*( ) ( ) ( )G f G f G f
0 100 200 300 400 500 600 700 800 900 10000.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Freq in Hz
Att
enua
tion
Attenuation of a Low-Pass Filter
Linear / Linear plot
Slide 4 of 13
100
101
102
103
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
Att
enua
tion
Freq in Hz
Attenuation of a Low-Pass Filter
2D PlottingAdditional Plotting Features – Log Scales
Wednesday 17 Sept 2014 EGR 115 Introduction to Computing for Engineers
A log / linear plot>> semilogx(f, sqrt(G .* conj(G)))>> xlabel('Freq in Hz');>> ylabel('Attenuation')>> title('Attenuation of a Low-Pass Filter')
Cut-off freq of 200 Hz
Slide 5 of 13
100
101
102
103
10-15
10-10
10-5
100
Freq in Hz
Att
enua
tion
in d
b
Attenuation of a Low-Pass Filter
2D PlottingAdditional Plotting Features – Log Scales
Wednesday 17 Sept 2014 EGR 115 Introduction to Computing for Engineers
• Plot the attenuation in db = 20 log10(?) A log / log plot>> loglog(f, (sqrt(G .* conj(G))).^20)>> xlabel('Freq in Hz');>> ylabel('Attenuation in db')>> title('Attenuation of a Low-Pass Filter')>> grid
log( ) log( )aa b b
Cut-off freq of 200 Hz at
-3 db
Slide 6 of 13
2D PlottingAdditional Plotting Features – Plot Limits
Wednesday 17 Sept 2014 EGR 115 Introduction to Computing for Engineers
• Controlling the limits for the x and y axes of a plot
axis([xmin xmax ymin ymax]) Sets the limits for the x- and y-axis of the current plot
axis tight Sets the axis limits to the range of the data
axis equal Sets the aspect ratio so that the data units are the same
axis off turns off all axis lines, tick marks, and labels
axis square makes the current axes region square
xlim([xmin xmax]) Sets only the x-axis limits
ylim([ymin ymax]) Sets only the y-axis limits
Slide 7 of 13
2D PlottingAdditional Plotting Features – Plot Limits
Wednesday 17 Sept 2014 EGR 115 Introduction to Computing for Engineers
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 20
0.02
0.04
0.06
0.08
0.1
0.12
0.14Default plot
0 0.5 1 1.5 2 2.50
0.02
0.04
0.06
0.08
0.1
0.12
0.14
0.16
0.18
0.2Using: axis([0 2.5 0 0.2])
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 20
0.02
0.04
0.06
0.08
0.1
0.12
Using: axis tight
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
Using: axis equal
0 0.5 1 1.5 20
0.02
0.04
0.06
0.08
0.1
0.12
0.14Using: axis square
0 0.5 1 1.5 2 2.50
0.02
0.04
0.06
0.08
0.1
0.12
0.14Using: xlim([0 2.5])
Data units the samei.e., 0.2 in x = 0.2 in y Square aspect ratio
Slide 8 of 13
2D PlottingAdditional Plotting Features – Left / Right Axis Plots
Wednesday 17 Sept 2014 EGR 115 Introduction to Computing for Engineers
• Plotting with Both Left and Right Side Axes A Hwk problem with speed and position (MATLAB code)
0 1 2 3 4 5 6 7 8 9 100
200
400
600
800
1000
1200
1400
1600
1800
2000Plot of Height and Speed vs Time
Time (s)
Hei
ght
(m)a
nd S
peed
(m
/s)
height
speed
0 1 2 3 4 5 6 7 8 9 100
1000
2000Plot of Height and Speed vs Time
Time (s)
Hei
ght
(m)
0 1 2 3 4 5 6 7 8 9 10100
150
200
Spe
ed (
m/s
)
height
speed
plotyy(time, height , time, speed)plot(time, height , time, speed)
Labeling the right axis can be tricky!!Slide 9 of 13
2D PlottingAdditional Plotting Features – Creating Multiple Figures
Wednesday 17 Sept 2014 EGR 115 Introduction to Computing for Engineers
• Issuing multiple plot commands will result in overwriting the prior plot window:
The “figure” command creates a new plot window
>> t = 0:0.1:2*pi;>> plot(t, sin(t), 'b')>> plot(t, cos(t), 'r')
Overwrites the Prior Plot
>> t = 0:0.1:2*pi;>> figure(1)>> plot(t, sin(t), 'b')>> figure(2)>> plot(t, cos(t), 'r')
Creates two Plot Windows
Slide 10 of 13
0 2 4 6 8-1
-0.5
0
0.5
1Subplot 1: Sin()
0 2 4 6 8-1
-0.5
0
0.5
1Subplot 2: Cos()
0 2 4 6 8-1
-0.5
0
0.5
1Subplot 3: Sin(2)
0 2 4 6 8-1
-0.5
0
0.5
1Subplot 4: Cos(2)
2D PlottingAdditional Plotting Features – SubPlots
Wednesday 17 Sept 2014 EGR 115 Introduction to Computing for Engineers
• We can also place multiple plots in a single figure:
Subplot(num_rows, num_cols, position)
Position#1 Position#2
Position#3 Position#4
%% An in class example of subplotst = 0:0.1:2*pi; % Array of angles (rad) figure(1); % Open a new figure subplot(2, 2, 1); % Subplot#1 at position 1,1plot(t, sin(t)); % Plot sin(theta)title('Subplot 1: Sin(\theta)') subplot(2, 2, 2); % Subplot#2 at position 1,2plot(t, cos(t)); % Plot cos(theta)title('Subplot 2: Cos(\theta)') subplot(2, 2, 3); % Subplot#1 at position 2,1plot(t, sin(2*t)); % Plot sin(2 theta)title('Subplot 3: Sin(2\theta)') subplot(2, 2, 4); % Subplot#2 at position 2,2plot(t, cos(2*t)); % Plot cos(2 theta)title('Subplot 4: Cos(2\theta)')
Slide 11 of 13
2D PlottingAdditional Plotting Features – SubPlots
Wednesday 17 Sept 2014 EGR 115 Introduction to Computing for Engineers
• Create the following plot:
0 1 2 3 4 5 6 7-1
-0.5
0
0.5
1Subplot 1: Sin()
0 1 2 3 4 5 6 7-1
-0.5
0
0.5
1Subplot 2: Cos()
Slide 12 of 13
Next Lecture
Wednesday 17 Sept 2014 EGR 115 Introduction to Computing for Engineers
• Annotation of Plots• Polar, Bar, Pie, … Plots
Slide 13 of 13