You are on page 1of 5

Matlab Lecture 3 In-class Problems/Solutions

1250
S 14

1.

Data for cell phone received signal strength versus distance.


We have the following data for cell phone signal strength versus the number of rows
from the back of the room. (Actually, the data is accurate for the first and last values
but made up for points in between because the update rate on the cell phone was too
slow.)
Distance (row #, back Cell Phone received signal
of room = 1)
strength (dB)
1

-93

-95

-96

10

-100

12

-101

16

-106

The following is the script file created in MATLAB in class to plot the above data.
Comments have been added.
% PhoneRSS.m

by Neil E Cotter, 02/05/14

% Plots cell phone received signal strength (RSS) in dB in WEB L104


%

as a function versus the number of rows from back of room.

% When plotting in MATLAB, all the x values go in one array, and


%

all the y values go in another array.

pairs.

this array 'd'.

We do not use ordered

Our x values are distances (in rows), and we will call

d = [1, 4, 7, 10, 12,16] % rows (dist from back of room)


% Our y values must be equal in number to the x values, and we must
%

be sure we put them in the proper order to match up with the

corresponding x values.

rss = [-93,-95,-96,-100,-101,-106] % (db) RSS at dist d

% Making a plot is simple.


%

We use a third argument, 'r+', to

plot red (r) + signs (+) data points.

plot(d,rss,'r+')

% Plot the actual data

% Now we find a straight line fit to the data points.

In the lab,

we must decide what kind of function to fit to the data.

This is

a judgement call based on the physics of the problem, the data,

and an appreciation of the hazard of having too many parameters

for the function we fit to our data.

function to fit our data, but then our ability to predict things

from our function.

there is no structure, and we just use the data points!

We could use a complicated

In the extreme case, we would just say there

% The polyfit function gives the coefficients of a polynomial of


%

any desired order to fit data.

the function is a 1, meaning we want a linear (1st order poly-

nomial) fit to the data.

p = polyfit(d,rss,1)
rss_hat = polyval(p,d)

The last argument of our call to

% Returns array: [a, b] for y = a*x + b.


% This plots the values on the line.

% Note that the polyval function uses the polynomial coefficients


%

and the x values on our plot.

line fit.

It generates the y values for the

% We want to superimpose the linear fit on the existing plot.


% We use the hold on command to keep the existing plot instead
%

of starting a new one.

hold on
% We plot our x and y values, which are d and rss_hat (hat means
%

'estimate' in EE)

plot(d,rss_hat,'b-')

% This plots a blue (b) line (-).

% Now add labels to the plot.

ALWAYS do this!

xlabel('dist (row #)')

% Label for x-axis.

ylabel('RSS (dB)')

% Label for y-axis.

legend('data','fit')

% Adds a legend saying which plot symbols


%

are which.

title('RSS vs Position in L104 WEB for Cell Phone')

% Plot title.

% Our last step is to turn off the plot hold so the next time we
%

run this script file, we will start a new plot rather than

% getting more and more plots on top of each other.


hold off

Here is our plot:

Fig. 1. Linear fit to cell phone received signal strength.

2.

Creating a large array

1
0
1 1
0
1
0 0
2 0
8 4
0 2 0 4
0
0 2 4
7
6
5 4

1
0
1
2
3
3

The following script file creates the above array. We could just enter all the numbers,
but that becomes impractical for very large matrices. The approach taken here is to
create pieces of the matrix. Think of the pieces as rectangles that we piece together.
As we construct the matrix, we must be sure that we are creating a rectangle at every
step.

% MakeArray.m

by Neil E Cotter, 02/05/14

% Creates the following array:

% A = 1

-2

0 -2

0 -2

% Create the first two rows of the matrix.


%

The eye() creates

the 2 x 2 identity matrix in the upper left-hand corner.

% The ones and zeros arrays complete the first two rows.
%

Note that we must bracket the ones and zeros to create a

rectangular array with same number of rows as the eye(2).

% Arrays side-by-side must have the same number of rows.


A = [eye(2),[ones(1,3);zeros(1,3)]]
% Add the next three rows to the existing A matrix.
%

We are

adding rows after the existing A matrix, hence the semicolon.

% Note that the ones matrix has 3 rows and 1 column.

The last

part uses the colon operator to create an array containing

1, 2, and 3.

the quote mark to make it vertical.

The array is horizontal, however, so we use

A = [A;-2*eye(3),4*ones(3,1),(1:3)']
% Add the last row to the existing A matrix.
%

We use the colon

operator with a -1 increment to count down instead of up.

A = [A; 7:-1:3]
% Our last step is to fix the missing 8 in the center of the
%

matrix.

to 8.

We address the 3rd row and 3rd column and set it

A(3,3) = 8

Here is what the output looks like:


Trial>> MakeArray
A =
1

A =

-2

-2

-2

-2

-2

-2

-2

-2

-2

A =

A =

You might also like