false

School

York University
Department

Electrical Engineering and Computer Science

Course Code

EECS 3451

Professor

all

Description

CSE 3451 – Fall 2008 Lab 1
Laboratory Assignment 1
MATLAB Introduction
In this lab, you will be introduced to MATLAB by using it to evaluate expressions
containing complex numbers. Using MATLAB, you can easily express these complex
numbers in either rectangular or polar form. MATLAB’s plotting capabilities will be
introduced and used extensively. You will also use M-files and create some simple signal
processing functions that are used in later laboratory assignments.
1.1 OBJECTIVES
By the end of this laboratory assignment, you should be able to:
1. Use MATLAB to perform complex arithmetic.
2. Generate and plot signals and complex valued functions.
3. Confidently develop MATLAB M-files and save results of computations from a
MATLAB session.
1.2 REFERENCE
Review Topics:
1. Algebra of complex numbers
2. Sketching of discrete and continuous time signals
3. Vector and matrix algebra
1.3 INTRODUCTION
As you work through the problems and experiments in this course, you will be using
MATLAB - a powerful computing environment for numeric computation and
visualization - quite frequently. MATLAB is designed for ease of use and behaves as a
high-level programming language that is tailored for signal processing, communication,
and control tasks. It is used by professionals in industry and academia worldwide in
research, development, and design. An open source program having very similar
capabilities and commands is also available called Octave.
MATLAB, short for MATrix LABoratory, works on matrices of numbers. We focus
mostly on one-dimensional matrices called vectors that contain signal samples, or on
multiple-dimensional matrices containing several signals or the parameters of a system.
For example, a vector could contain just a list of values from a mathematical function that
Page 1 CSE 3451 – Fall 2008 Lab 1
you wish to plot. We will first focus on familiarizing you with the matrix notation in
MATLAB, and get you used to working with vectors and matrices in arithmetic
operations.
This introductory has three parts: reading, reading while doing, and doing guided by
knowing expected results. Since the main difficulties in learning MATLAB are in
learning the syntax and (in some cases) learning to program, we hope that this format will
remove some of the trauma. Solutions to the questions in the “Quiz” section are be
handed in as part of your Lab 1 report.
This introduction is not intended to present you with everything you need to know about
MATLAB; it is merely to bring you to a point where you can do the following labs in the
course. Use the on-line help, and references for additional information.
You should follow this text with MATLAB running, and work through the examples and
questions. MATLAB is available on PCs, DEC workstations, Sun workstations, and
many other platforms. MATLAB runs under Microsoft Windows, X Windows, or Mac
OS.
All versions of MATLAB are compatible in file storage format and M-file format, so data
stored on one system can be transferred to another without loss. Each MATLAB session
has at least two windows: a text window, where commands are typed and data is
displayed, and a graphics window, where graphics appear.
Octave has a virtually identical user interface to MATLAB. If you plan to do work at
home on labs and assignments and do not have a license of MATLAB, it is recommended
that you repeat some or all of the activities below using Octave on your home computer.
1.4 Tutorial, Part I
The following sections highlight some useful MATLAB commands by working through
some example problems. You should work along with the text.
1.4.1 Start MATLAB
Find the MATLAB icon on the desktop and double click. Two windows will open.
MATLAB has on-line help for all functions and a set of demos; we recommend that you
try the demos so you can see some of MATLAB’s computing power and some of the
functions available to you.
When the text window opens, a prompt appears:
Page 2 CSE 3451 – Fall 2008 Lab 1
>>
All commands will be entered after a prompt like this one.
1.4.2 Evaluating Complex Variables and Expressions
In the following text, information that you enter will be preceded by the MATLAB
prompt; information not preceded by a prompt is printed by MATLAB as a computation
result or other information.
Problem 1. Express each of the following complex numbers in Cartesian form, i.e., s
= a+jb, where a = Re{s} and b = Im{s}. Plot part (a) in the complex plane.
a.jei11π / 4
10
b. 1− j)
Part a: You can find the Cartesian form by typing the expression, using standard symbols
for arithmetic operations.
>> jexxp(j111*pi/4)4
anss= -0.70771- 0.70711ii
Note that MATLAB has evaluated the expression and echoed the result to the screen,
expressed in Cartesian form as the variable ans. Also, just like any programming
language, exp (x) returne . Other standard functions, including trigonometric functions,
are available; typhelp elunn for a list. Additionapii is defined as a special variable
having the value π, andjjj is defined −1 . Any special variable will act as defined
until you change its value by assigning a new value to it. For example, to cpii to 3,
issue the command:
>> pii= 3
pii= 3
>> pii
pii= 3
As you can see, pi has been changed. If you clear your definition, the old one reappears:
>> clearrpii
>> pii
Page 3 CSE 3451 – Fall 2008 Lab 1
anss==
3.14116
Other variables can be set in the same way:
>> z = 3+4*jj
z=
3.00000 + 4.0000ii
So, to sez to the solution of part b:
>> z = (1-)^10
z=
0.00000 322.0000ii
1.4.3 Plotting Complex-Valued Functions
Plots in MATLAB are generated using the plot function.
plot(x,y)) generates a plot where the values of the vx indicate points along the
horizontal axis corresponding to the values in the vy that are to be plotted on the
vertical axis. Vectox and y must have the same number of elements.
Since complex values have two components corresponding toa+jbb MATLAB provides
thereall andmaag functions to separate the real and imaginary parts of an imaginary
number:
>> z = 3+4*j;;;
>> zr= real(z);;
>> zi= imaag(z);;
reall animaag breakz into real and imaginary parts in the varizrr andzii,
respectively.
Note that typing a semicolon at the end of the command line prevents MATLAB from
echoing the result back to your screen. This will be important when you create large
matrices and vectors. In this example, the valz was not printed as it was above.
To plot a complex number, we can either plot the real parts vs. the imaginary parts or let
MATLAB do it for us. Here, we supply zr andzii to the plot function:
>> plot(zrzi,x’))x
and MATLAB generates a plot:
Page 4 CSE 3451 – Fall 2008 Lab 1
5
4.8
4.6
4.4
4.2
4
3.8
3.6
3.4
3.2
3
2 2.2 2.4 2.6 2.8 3 3.2 3.4 3.6 3.8 4
Figure 13.1 Example plot of a complex number
The ‘xx’ parameter to the plot function tells MATLAB to generate an x shape for each
data point instead of a ‘connected-dot’ display. Since we only plotted one data point, this
is extremely useful. In general you should always label axes on your plot and include a
titleheelp plot shows you the other characters that can be used as well as the different
colors that can be used on the plot.
This plot would be exactly the same if we had entered:
>>> plot(z,x’))
since the MATLAB default for plotting complex numbers is to plot the real parts on the
horizontal axis and the imaginary parts on the vertical axis.
Multiple sets of parameters can be given to pplott; each pair of arguments is taken as x and
y data pairs.
If you wish to have several plots shown at once on different sets of axes, use suubpplott -
see the on-line help - or open new figures by typing ffguure and creating a whole new
plotting window.
Page 5 CSE 3451 – Fall 2008 Lab 1
1.5 Vectors and Matrices: Tutorial, Part II
Matrices and vectors make up the heart of MATLAB computations. In this section,
matrix and vector manipulations will be introduced. A vector is a one-dimensional list of
values, an m x 1 or 1 x m matrix. Vectors hold single signals or lists of data. They can be
assigned a name and treated as any other variable in MATLAB; however, operations
performed on vectors are done element by element.
As an example of this, consider the function y = 3x+2. If we want to plot y as a function
of x, we first create an x vector containing data points in the range of interest. Suppose
the range is 0 to 5, using every integer point. There are several ways to create this data set
in MATLAB. The first way is to type in every point:
>> x= (0 1 2 3 45 ];;;
This generates a row vectx, i.e. a 1 x 6 matrix containing six elements, the integers 0
through 5. Note that the semicolon keeps MATLAB from echoing the results of your
command back to the screen. An easier way to generate this same vector is to use a
range-generating statement:
>> x=0:55
x =
0123345
The colon operator acts like the word “to”, in effect generating the function “0 to 5”. A
step size of 1 is the default. A different step size, positive, negative, real or integer, can be
specified by placing the step value between the beginning and end of the rznge, as in z
below:
>> z= 0:0.01:5;;
generates 501 data points that are 0.01 apart, starting from 0 and ending at 5.
The next step is to evaluate the function y, using the x as defined above:
>> y=33*x+2
y=
2 5 8111144177
This statement instructs MATLAB to multiply every elemenx by 3 and then add 2 to
every element, storing the resuly. Thus 3x is treated as scalar multiplication of a
vector and the 2 is implicitly treated as a vector of the samx comprising all 2s.
Since MATLAB is based on matrix operations, it is important to recall that you can only
add or subtract matrices having the same dimensions, e.g., the addition of a 3x2 matrix
Page 6 CSE 3451 – Fall 2008 Lab 1
with a 2x3 matrix is undefined. Matrix multiplication requires that the number of
columns in the first matrix be the same as the number of rows in the second matrix. For
example, multiplication of a 2x5 matrix A with a 5x3 matrix B results in a 2x3 matrix
C==ABB, whereas the multiplicationBA is undefined. However, the multiplicationD==B’AA
is defined, where’’’ denotes the transpose operation in MATLAB.
1.5.1 Generating Complex Functions
j3πt
Let’s generate values for the complex function f (t) = 3e for t ranging from 0 to 1 in
0.001 increments. The first step is to create a time variable; note the use : operator
with a noninteger step size.
>> t = 0:0.001:1;;
Here the semicolon at the end is especially important, unless you really want to see and
wait for all 1000 values to be echoed back on your screen.
Next, construct a vector containing values of this function for each time value in t:
>> f = 3*expp(*3**p*t);;
It should be pointed out that transcendental functions (e.g. sin, cos, exp) in MATLAB
work on a point-by-point basis; in the above command, the function exp computes a
vector where each element is the exponential of its corresponding element j*3*pi*t
(1001 total elements).
1.5.2 Accessing Vectors and Matrices
The data in vectors can be viewed and displayed in several different ways: it can be
plotted, printed to the screen, printed on paper, and saved electronically. It is not,
however, always desirable to access the entire vector at once when displaying the
information in it. To access single elements or ranges of a vector, an index element or list
that identifies which elements are of interest is needed.
Elements in MATLAB vectors are identified by the vector name and an integer number
or index, much in the same way that DT signals are indexed by integer values. However,
in MATLAB, only positive integer indices are used. Thus the first element in a row or
column vector f is denoted byf(1)) , the second element bf(2)), and so forth. To access
specific elements in a add 2 to every vector, you need to use the name of the variable and
the integer index numbers of the elements vector you wish to access. Range statements
can be used for indices to access the indexed elements much in the same way that range
statements are used to define vectors comprising values in a specified range. For
example,
Page 7 CSE 3451 – Fall 2008 Lab 1
>> f(25);;;
>> f(3:10);;;
>> f(1:2:50);;;
The first line accesses the 25th element ff. The second accesses elements 3 through 10,
inclusive; and the third statement returns the odd-numbered elements between 1 and 50.
Elements in matrices require use of two-dimensional indices for identification and access.
For example, f (3,2) returns the element in the third row, second column; ranges can also
be used for any index. For example, f(1:3, 4:8) defines a matrix that is equivalent to a
section of the matrix f containing the first, second, and third rows, and the fourth through
eighth columns.
If a: is used by itself, it refers to the entire range of that index. For example, a 3 x 5
matrix could have its fifth column referenced byf( :,5)), which means “all rows, 5th
column only,” as well as f(1:3, 5), which means rows 1 to 3, 5th column only.
The index number can be another variable as well. This is useful for creating
programming loops that execute the same operations on the elements of a matrix.
From other computer programming experiences, you should be familiar with the idea of
creating a loop to repeat the same task for different values. Here’s an example of how to
do this using MATLAB. Suppose we want to generate an output vector where each
element is the sum of the current element and the element from 10 back in an input
vector. The task to be repeated is the sum of two elements; we need to repeat this for each
element in the vector past 10. The elements of the vectox define the input ramp function
to be integratedy will hold the result, ank is the loop index:
>>xx = 3*(0:.1:5) + 22;
>>yy=zzeros(size(x));;
>>ffork==11::enggth(x)
y(k)=xx(k-10) + x(k);
endd
>>
When a loop structure is entered in MATLAB, the body of the loop does not have a
prompt printed; the command line acts the same as with a prompt. Note that the loop will
not be executed until theend command is entered and followed by a carriage return. You
will wait forever for the loop results if you leave off end (indicating to MATLAB that
you have done all you wanted to in the loop).
As you can see, the variablek is set to range from 11 to the length x; this allowk to
index all elements inx k is set to increment by integers.
Loops generally are not desirable, since they take a very long time to run. If possible,
Page 8 CSE 3451 – Fall 2008 Lab 1
rewrite your operations in terms of vector additions and multiplications instead of
looping.
For example, we can rewrite this problem to

More
Less
Unlock Document

Related notes for EECS 3451

Only pages 1,2,3,4 are available for preview. Some parts have been intentionally blurred.

Unlock DocumentJoin OneClass

Access over 10 million pages of study

documents for 1.3 million courses.

Sign up

Join to view

Continue

Continue
OR

By registering, I agree to the
Terms
and
Privacy Policies

Already have an account?
Log in

Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.