Embedded Communications in Wireless Sensor Network

Download Report

Transcript Embedded Communications in Wireless Sensor Network

2D Graphics
Numerical Computing
with
.
MATLAB
for Scientists and Engineers
You will be able to




Draw various graphs using plot,
Set properties of plots using graphics handles,
Visualize data using various plots including bar,
stem, stairs, pie, histogram and polar plots,
Use patch plots with colors.
2
Plotting a Single Vector

plot( y )



The index is used as
the x-axis.
The range of plot is
determined
automatically.
The line color starts
from 'blue'.
basic_plot1.m
% basic plot 1
y = [ 2 4 3 6 ];
figure(1), plot(y);
6
5.5
5
4.5
4
3.5
3
2.5
2
1
1.5
2
2.5
3
3.5
4
3
Plotting x, y Vectors

plot( x, y )


basic_plot2.m
x is used the
corresponding x-value
for y.
axis command
controls the viewing
range.
x = [ 1
y = [ 2
plot(x,
axis([0
2.5 6 3 ];
-1
3 2 ];
y);
7 -2 +4]);
4
3
2
1
0
-1
-2
0
1
2
3
4
5
6
7
4
Multiple Plots

plot(x,y,u,v,t,h)

plot(x,y,'r-',u,v,'b:',t,h,'k--')

plot(x,y);
hold on;
plot(u,v);
plot(t,h);
hold off;

plot(X,Y);
% X - matrix
% Y - matrix
% column-wise plot
5
Plotting a Matrix

squares.m
plot( X, Y )


Each column of the
matrices X and Y is
interpreted as x, y
coordinates of each
graph.
Different colors are
assigned to each
graph.
x = [ 1 -1 -1 1 1]';
y = [ 1 1 -1 -1 1]';
s = 1:0.21:2;
X = x * s ;
Y = y * s;
plot(X,Y,'LineWidth',2);
2
1.5
1
0.5
0
-0.5
-1
-1.5
-2
-2
-1.5
-1
-0.5
0
0.5
1
1.5
2
6
Example: Joining Circles
6
4
2
0
-2
-4
-6
-15
-10
-5
0
5
10
15
7
Script: Joining Circles
circles.m
%
t
N
x
y
Joining Circles
= 0:0.1:2*pi+0.1; % angles
= length(t);
= cos(t');
= sin(t');
c = -5.5:5.5; % center of the circles
C = repmat(c,N,1);
r = abs(c);
% radius of the circles
X = x*r + C;
Y = y*r;
plot(X,Y) % plot the circles
8
Exercise 1: Basic Plot

Plot the following graph using basic plot
command.

Determine the path of the plot without revisiting
the point again.
Provide x and y.
Height is 3
7


6
5
4
trangle_tower.m
% plot the triangle tower.
x = [ ...];
y = [ ...]*sqrt(3);
plot(x, y);
3
2
1
0
0
1
2
3
4
5
6
7
8
9
Exercise 2: Square Spiral

Plot the following graph using basic plot
command.

Find a rule to generate
x and y coordinates
of connecting points.
20
15
10
5
square_spiral.m
...
x = ...;
y = ...;
plot(x, y, 'b-o');
0
-5
-10
-15
-20
-20
-15
-10
-5
0
5
10
15
20
10
Properties of Graph

plot(x, y,'LineSpec', ...
'PropertyName','PropertyValue')
4
x = [ 1 2.5
y = [ 2 -1
6
3
3 ];
2 ];
3
plot(x, y,'r-.o', ...
2
'LineWidth', 2, 'MarkerEdgeColor', 'b');
1
axis([0 7 -2 +4]);
0
-1
-2
0
1
2
3
4
5
6
7
11
The plot Command: LineSpec



LineStyle
solid
dashed --
red
LineColor green
blue
cyan
Marker
+
o
*
x
dotted
:
dash-dot -.
r
g
b
c
magenta
yellow
black
white
s
d
p
h
m
y
k
w
v
^
<
>
12
Properties and Values







'LineWidth', 2
'MarkerSize', 6
'MarkerEdgeColor', 'b'
'MarkerFaceColor', 'r'
'LineStyle', '-'
'Color', [ 0 1 0 ]
'Marker', 'o'
13
Handles and Properties






get(h(1))
set(h(1),'LineWidth',2);
get(gca)
set(gca,'FontSize',12, ...
'FontWeight','bold');
xl=get(gca,'XLabel');
set(xl,'FontSize',14)
14
Text Formatting

abc \fontname{times} abc
Font a{\bfb}c
abc
a{\itb}c
abc \fontsize{12}
\ita{\rmb}c abc

Subscript and Superscript

A^{2-x}+b_i
Greek Letters
\alpha
\beta
\gamma
\theta




\pi
\sigma
\Phi
\Delta
A2-xbi




\Gamma
\Lambda
\Omega
\Sigma




15
Anatomy of Plot
Plot Title
Light Intensity as a Function of Distance
1200
Y Axis Label
Grid
Instensity (lux)
1000
800
Text Box
Annotation
Computed Values
600
400
Arrow
Annotation
Marker
Major Tick
Legend
Theory
Experiment
200
Measured
Minor Tick
0
8
10
12
14
16
18
20
22
24
Distance (cm)
X Tick Label
X Axis Label
16
Anatomy of Plot - Script
light_intensity.m
% Sample Plot for Showing Various Graph Elements
d = 10:0.1:22;
dd = 10:2:22;
y = 1000*exp(-0.15*(d-10));
yy = 1000*exp(-0.16*(dd-10));
h = plot(d,y,'b-',dd,yy,'r-.o');
axis([8 24 0 1200]);
set(h(1),'LineWidth',2);
set(h(2),'LineWidth',2);
xlabel('Distance (cm)');
ylabel('Intensity (lux)');
title('Light Intensity as a Function of Distance');
legend('Theory','Experiment');
grid;
17
Exercise 3 – Custom Plot

Write an m-script to plot the following graph.
18 point, bold
16 point, normal
Light Intensity 1=f ()=1000 e -10
1200
Theory
Experiment
Intensity  (lux)
1000
Text at (16.2,670)
800
2
10
i=1 xi  Ai,2
2-i
LineWidth=2
600
400
200
yellow color
0
Start
P1
P2
Distance  (cm)
Edge
End
12 point, bold
16 point, normal
18
Solution 3

Script and Screenshot
custom_plot.m
19
Function Plot

Optional
fplot( 'expression', range, LineSpec)


fplot('x^2+4*sin(2*x)-1', [-3 3], 'r-')
Function expression can be your m-file.
myfun.m
function y = myfun(x)
y = x^2+4*sin(2*x)-1;
10
x  4 sin(2 x )  1
2
fplot( @myfun, [-3 3] , 'r-')
or fplot('myfun', [-3 3], 'r-')

5
ezplot('expression, range);
0


ezplot('x^2+4*sin(2*x)-1')
The default range is  2  ~ 2  .
-5
-3
-2
-1
0
1
2
3
20
Equivalent Normal Plot



fplot('x^2+4*sin(2*x)-1', [-3 3], 'r-')
Need to generate x and y values
The range of y should be determined.
fplot_eq.m
% Equivalent plot of fplot
%
x = -3:0.01:3;
y = x.^2+4*sin(2*x)-1;
figure(1), plot(x,y,'r-');
minY = floor(min(y));
maxY = ceil(max(y));
axis([-3 3 minY maxY]);
10
5
0
-5
-3
-2
-1
0
1
2
3
21
x 2-x y - y 2=2
Implicit Plot
6
4
ezplot('x^2-x*y - y^2=2')
0
y

2
implicit_ex1.m
-2
figure % new figure window
h = ezplot('x^2-x*y - y^2=2');
set(h, 'LineWidth',4)
grid;
-4
-6
-6
-4
-2
0
x
2
4
6
x = abs(t) sin(t), y = abs(t) cos (t)
6
ezplot('abs(t)*sin(t)', ...
'abs(t)*cos(t)',[-2*pi 2*pi]);
4
3
2
y

5
1
Parametric Plot
0
-1
-2
-3
-6
-4
-2
0
x
2
4
6
22
y2
Logarithmic Axes
 0.2 x 10
x=linspace(0.1,60,100); y = 2.^(-0.2*x+10);
1200
10
plot(x,y)
1000
10
4
semilogy(x,y)
3
800
10
2
600
10
1
400
10
200
0
0
10
20
30
40
50
60
1200
10
10
semilogx(x,y)
1000
10
0
-1
0
10
20
30
40
50
60
4
3
800
10
2
600
10
1
400
10
200
0 -1
10
10
0
10
1
10
2
10
0
loglog(x,y)
-1
10
-1
10
0
10
1
10
2
23
Special Graphs: Bar Charts
year = 2000:2006;
sales = [8 12 20 22 18 24 27 ];
bar( year, sales,'r');
xlabel('Year');
ylabel('Sales (Millions)');
barh( year, sales,'b');
ylabel('Year');
xlabel('Sales (Millions)');
30
2006
25
20
2004
Year
Sales (Millions)
2005
15
2003
2002
10
2001
5
2000
0
2000
2001
2002
2003
Year
2004
2005
2006
0
5
10
15
20
25
30
Sales (Millions)
24
Multiple Bars
Each column represents a bar chart.
Different colors are assigned to different
columns.


1
0.9
0.8
0.7
0.6
0.5
0.4
0.3
a=rand(4);
0.2
bar( a );
0.1
0
1
2
3
4
25
Special Graphs: Stairs & Stem
year = 2000:2006;
sales = [8 12 20 22 18 24 27 ];
stem( year, sales,'filled');
xlabel('Year');
ylabel('Sales (Millions)');
30
30
25
25
20
20
Sales (Millions)
Sales (Millions)
stairs( year, sales);
xlabel('Year');
ylabel('Sales (Millions)');
15
10
5
0
2000
15
10
5
2001
2002
2003
Year
2004
2005
2006
0
2000
2001
2002
2003
2004
2005
2006
Year
26
Exercise 4: KOSPI of the Last 3Mth.

Draw a plot of the KOSPI for the past 3
months. Use sensible labels for x-axis and yaxis as in the graph below.

Get the raw data
from KOSCOM site.
http://stock.koscom.co.kr/kse_sise/kse_daily_jisu.jsp

Visit the site and
select the data and
paste it in Excel.
Read in B column
of the Excel file into
MATLAB and plot it.
27
Solution 4

Script and Screenshot
kospi_3month.m
28
Pie Chart
Class Grades
grades = [ 11 18 26 9 5];
7%
16%
pie(grades,[ 0 0 0 1 0]);
13%
F
A
title('Class Grades');
D
B
26%
C
38%
29
Exercise 5: World Pop. in Y2100
Represent the predicted world population in
year 2100 in a bar char and a pie chart.




Get the raw data from Wikipedia.
http://en.wikipedia.org/wiki/World_population
Draw a bar char and a pie char based on the data.
Use sensible legend or tick label.
World Population in Y2100
< 1% 4%
6000
9%
24%
5000
5%
4000
Population in Millions

3000
2000
Africa
Asia
Europe
L. America
N. America
Oceania
1000
0
57%
Africa
Asia
Europe L. America N. America Oceania
Continent
30
Solution 5

Script and Screenshot
world_population.m
31
Histogram (1/2)


Number of occurrences in each bin.
The default number of bins is 10.
250
140
120
200
100
150
80
60
100
40
50
20
0
-4
-3
-2
-1
0
1
2
3
4
0
-4
-3
-2
-1
0
1
s=randn(1,1000);
s=randn(1,1000);
hist(s)
hist(s, 20)
2
3
4
32
Histogram (2/2)


Histogram of a Matrix
Use of histogram data
450
400
300
250
200
350
150
300
250
100
200
50
150
100
0
-3
50
0
-6
-2
-1
0
1
2
3
s=randn(1,1000);
-4
-2
0
s=randn(1000);
hist(s)
2
4
6
[n x ] = hist(s);
stem(x,n,'filled'); hold on;
plot(x,n,'r-.');
33
Polar Plot

r

polar(theta, radius, 'line spec' )
90
1
120
60
0.8
0.6
150
30
0.4
polar_pie.m
t = 0:0.01:pi/4;
th = [t 0 0 ];
r = [ones(1,length(t))...
0 1 ];
h = polar( th, r, 'r-');
set(h,'LineWidth',3);
0.2
180
0
210
330
240
300
270
34
More Polar Plots
  0 ~ 20 
  0 ~ 4
r 
r  sin(4.5 )
90
90
80
120
60
1
120
60
0.8
60
0.6
40
150
30
150
30
0.4
20
0.2
180
0
210
330
240
300
270
polar_spiral.m
th = 0:0.01:20*pi;
h = polar(th,th,'r-');
set(h, 'LineWidth',3);
180
0
210
330
240
300
270
polar_petal.m
th = 0:0.01:4*pi;
r = sin(4.5*th);
h = polar(th,r,'r-');
set(h, 'LineWidth',2);
35
Polar / Plot / Patch
90
1
120
60
0.8
0.6
150
30
0.4
0.2
180
0
210
330
240
300
270
Polar
1
1
0.8
0.8
0.6
0.6
0.4
0.4
0.2
0.2
0
0
-0.2
-0.2
-0.4
-0.4
-0.6
-0.6
-0.8
-0.8
-1
-1
-0.5
0
Plot
0.5
1
-1
-1
-0.5
0
0.5
1
Patch
36
Script for Polar / Plot / Patch


polar(  , r ) plot( r co s  , r sin  )
patch( r co s  , r sin  , Color)
polar_patch_demo.m
% Polar, Plot and Patch demo
%% a wing
t1 = 0:0.01:pi/3; % counter-clockwise
tr = t1(end:-1:1); % reverse direction
nt = length(t1);
th = [t1 tr 0];
r1 = [ones(1,nt) 0.3*ones(1,nt) 1];
%% a circle
t2 = 0:0.01:2*pi;
r2 = ones(1,length(t2));
%% Polar
figure(1);
h1 = polar( th, r1, 'r-'); hold on;
h2 = polar( th+2/3*pi, r1, 'r-');
h3 = polar( th-2/3*pi, r1, 'r-');
h0 = polar( t2, 0.25*r2, 'r-');
set([h0 h1 h2 h3], 'LineWidth', 3);
hold off;
%% Plot
figure(2);
h1 = plot( r1.*cos(th), r1.*sin(th), 'r-'); hold on;
h2 = plot( r1.*cos(th+2/3*pi), r1.*sin(th+2/3*pi), 'r-');
h3 = plot( r1.*cos(th-2/3*pi), r1.*sin(th-2/3*pi), 'r-');
h0 = plot( 0.25*r2.*cos(t2), 0.25*r2.*sin(t2), 'r-');
set([h0 h1 h2 h3], 'LineWidth', 3);
axis square;
hold off;
%% Patch - filled polygons
figure(3);
h1 = patch( r1.*cos(th), r1.*sin(th), 'k'); hold on;
h2 = patch( r1.*cos(th+2/3*pi), r1.*sin(th+2/3*pi), 'k');
h3 = patch( r1.*cos(th-2/3*pi), r1.*sin(th-2/3*pi), 'k');
h0 = patch( 0.25*r2.*cos(t2), 0.25*r2.*sin(t2), 'k');
set(gca,'Color','yellow');
axis square;
hold off;
37
Coloring Patches
8
(2,8)
7
patch_demo.m
6
xdata = [2
2
0
2
5; 5
2
8
2
4
5; 4
8
8
2
4
8];3
ydata = [4
4
4
2
0;
2
8
4
6
2
2;
4
0
4
0
0];1
cdata = [9
0
4
6
10; 00
1
2
3
15
2
5
7
9;
8
3
0
8
3];
% colormapeditor
figure(1);
p = patch(xdata,ydata,cdata, ...
'Marker','o','MarkerFaceColor','flat', ...
'FaceColor','none');
set(p,'EdgeColor','interp');
4
5
6
7
8
colormapeditor
38
Patch Pyramid
cdata 0
patch_pyramid.m
(4,4)
xdata = [0
0
0
8;
8
0
8
8;
4
4
4
4];
ydata = [0
0
8
8;
0
8
8
0;
4
4
4
4];
cdata = [4
8
12
15;
4
8
12
15;
0
0
0
0];
% Select the current colomap
colormap('Cool');
figure(1);
p = ...
patch(xdata,ydata,cdata,'FaceColor','interp');
set(p,'EdgeColor','interp');
39
Exercise 6: Color Ball

Write a script for plotting the color ball below
using patch.
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
-1
-0.5
0
0.5
1
40
Solution 6

Script and Screenshot
patch_color_ball.m
41
Subplot - Example
15
15
15
10
10
10
5
5
5
0
0
1
2
3
4
5
0
0
1
plot
2
3
4
5
0
0
1
stem
3
90
18%
5
10
120
23%
10
4
stairs
8%
15
2
60
5
150
30
180
0
20%
5
8%
210
330
240
0
0
1
2
3
bar
4
5
25%
pie
300
270
polar
42
Subplot - Script
sbuplots.m
% Subplot Demo
y = floor(10*rand(1,6))+3;
x = [1:length(y)] - 1;
subplot(2,3,1);
plot(x,y,'b-'); axis([-0.5 5.5 0 15]);
subplot(2,3,2);
stem(x,y,'filled'); axis([-0.5 5.5 0 15]);
subplot(2,3,3);
stairs(x,y,'b-'); axis([-0.5 5.5 0 15]);
subplot(2,3,4);
bar(x,y,0.75); axis([-0.5 5.5 0 15]);
subplot(2,3,5);
pie(y,[0 0 0 0 0 1]);
subplot(2,3,6);
polar([x x(1)]*2*pi/6,[y y(1)],'b-');
43
Summary

Recognize the following commands?
h = plot(x,y,'r-o');
figure
figure(1)
axis square
xlabel('{\it t}');
stairs(x,y)
pie(data, [0 0 1 0])
colormap('Cool')
reshape(M,1,[])
hold on
set(gca, 'Color', 'yellow')
title('KOSPI');
stem(t,y,'filled')
box off
set( h, 'LineWidth',2)
barh(X)
polar(theta,rho)
patch(x,y,'k');
legend('Theory','Data');
repmat(N,2,1)
grid
ezplot('cos(x)')
[s xbin] = hist(X)
44
Solution to Exercise 1
7
6
5

Plot the graph on the right.
4
3
triangle_tower.m
% Plotting Triangle Tower
%
% (x, y) coordinates of a series
% of points
2
1
0
0
1
2
3
4
5
6
7
8
s = sqrt(3); % height of a small triangle
x = [2 0 2 1 3 2 4 2 6 4 6 5 7 6 8 4 3 5 4 3 2 ];
y = [2 0 0 1 1 0 0 2 2 0 0 1 1 0 0 4 3 3 2 3 2 ]*s;
figure(1), plot(x,y,'LineWidth',4);
45
20
Solution to Exercise 2

Plot the square spiral plot.
square_spiral.m
15
10
5
0
-5
-10
% Square Spiral
-15
x0 = 1:20;
-20
x1 = x0 .* (-1).^(x0+1);
-20
-15
-10
x2 = reshape(repmat(x1,2,1), 1,[]);
x = [ 0 x2 ];
y0 = 2:2:20;
y1 = reshape(repmat(y0,2,1), 1, []);
n1 = 1:length(y1);
y2 = y1 .* (-1).^(n1+1);
y3 = reshape(repmat(y2,2,1), 1, []);
y = [0 0 y3(1:end-1)];
figure(1);
plot(x,y,'b-o', 'MarkerFaceColor', 'blue');
-5
0
5
10
15
20
46
Note
47
Note
48