Академический Документы
Профессиональный Документы
Культура Документы
Raymond Phan Distributed Multimedia Computing Research (DMCR) Lab Ryerson University EPH 237 rphan@ee.ryerson.ca
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 1
http://www.ee.ryerson.ca/~rphan/ele532/MATLABTutorial.ppt
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
Introduction (1)
So what the hell is MATLAB and whats it all about?
MATLAB: MATrix LABoratory
Created in 1970 by a dude named Cleve Moler Was (and still is) used extensively at Stanford University and the University of New Mexico
Why? To make calculating the following things a lot easier!
Matrix Theory Linear Algebra Numerical Analysis
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 4
Introduction (2)
MATLAB is selected as a numerical analysis tool over languages like C and Java because:
Very EASY programming language Powerful graphics capabilities Very sleek and interactive interface Great for general scientific and engineering computation
Later in your courses, youre going to start to use this heavily, especially in:
ELE 639: Control Systems ELE 792: Digital Signal Processing any signal processing and controls course in 4th year
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 5
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
Version of MATLAB needed for these labs: MATLAB 7.0 and up NOTE!: You MUST have the Java Runtime Environment (JRE) installed on your system
At LEAST 5.0 and up
MATLAB Interface: >> means its ready for input from you
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
Example:
C or Java way: int nikhil = 1, double jenny = 2 MATLAB way: nikhil = 1, jenny = 2
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 11
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
Typing in a variable by itself and pressing ENTER will redisplay the variable Entering a value, pressing ENTER, and not assigning it to anything, the value will be automatically assigned to a variable called ans (answer)
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 15
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
16
clear command: Clears all of the variables shown in the workspace, and you start from scratch clc command: Flushes the command prompt
Variables will still be in the workspace, but it clears the command prompt screen
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 17
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
18
You can use i or j to declare complex numbers Of course you can also add, subtract, multiply and divide normal numbers too!
Too lazy to make a slide for it However, well get into addition, subtraction, multiplication and division in another way later
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 19
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
20
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
a = [1 2 3 4]. (. operator means to transpose a vector) - column vector a = [1;2;3;4] - column vector
Semicolon means to move to the next row
You do not have to specify how big the vector is first before you make it
Beauty of dynamically typed languages! MATLAB automatically figures out how big it is and you go from there
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 24
MATLAB way:
jenny = a(1);
NOTE!:
No square brackets when accessing an element! Use round brackets! Elements do not start at index 0, they start at index 1!
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 25
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
26
MATLAB way:
alan = a(3,4);
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
MATLAB way:
1 : 4 means a range from 1 through 4 for a dimension : by itself means give me all possible values in a dimension Doing 1 : 4 in the 2nd parameter means give me columns 1 through 4 Doing : in the 2nd parameter means give me all of the columns!
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 30
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
31
1st line: Access 2nd row, 3rd column element, and assign it to sally 2nd line: Get a matrix with elements between rows 1 3, and columns 3 4 and assign this to sally 3rd line: Get a matrix with elements between columns 1 3 and give me every possible row, and assign this to sally 4th line: Get a matrix with elements between rows 2 4 and give me every possible column, and assign this to sally
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 32
What does this mean? Copy the entire matrix, a, and assign it to Joe You can also do the 2nd line too. Its exactly the same meaning
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 33
1) How do we define M in MATLAB syntax? 2) How do we execute a), b), c) and d)?
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 34
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
35
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
36
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
37
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
39
first_value: The first value in the new vector / array last_value: The last value in the new vector / array increment: The step size
If you dont include this value, it is automatically assumed to be 1
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 41
Pretty easy dont you think!? Remember how I told you about that colon operator?... Well, here you go! To do this in C and Java, it requires a bit more work.
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 43
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
44
ones(1,n) or ones(n,1): Creates an array / vector that has n elements, full of ones
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 45
zeros(1,n) or zeros(n,1): Creates an array / vector that has n elements, full of zeros
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 46
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
Heres a table that provides a good summary of all of the basic operations you can perform on matrices and vectors / arrays
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 49
Here, the elements in the matrices or vectors / arrays can be real or complex Addition and Subtraction will just add and subtract two matrices normally For vectors, each corresponding component gets added or subtracted
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 50
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
51
. operator
Real and Complex valued entries: Normal Transpose
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 53
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
54
The 1st element of A is *, /, \ or ^ with the 1st element of B, and that result gets stored dont get it? Heres an example
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 55
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
56
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
g = t + 2; g = [2 4 6 20 22];
h = t - 4; h = [-2 0 2 16 18];
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
58
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
60
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
61
prod(M): Returns a row vector, where each element is the product of the columns of matrix M confused? Dont worry, check out this example
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 62
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
63
sum = prod(V);
What is the above operation equivalently called in mathematics? youve seen this before. Think MTH 314
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
64
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
66
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
Think C or Java: You put all commands or syntax into a file and execute that file. You can do this with MATLAB, and you dont have to input all of the commands individually.
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 68
To execute a script file, make sure you set the working directory to be where the script file is located (remember I said wed get back to this earlier?)
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 69
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
70
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
72
When you execute a script file, all of the variables created in the script file get stored in the workspace for future use Lets do an example:
Lets make the factorial example into a script file
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 73
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
74
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
75
The script file is now set to compute 4! What if I wanted to do 9!, or 12!, or 5! ?
Just change the n parameter accordingly
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 76
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
The function performs MATLAB commands with these input variables The function returns output variables
Difference: You can return more than one variable! You dont have to define the variable type either!
Remember, with a script file, all you had to do was enter in the commands in sequence
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 80
If you want to comment out a block of code, do the following: %{ commands that are commented out %} When youre coding a function script file, its a good idea to put an authors block at the beginning of the file, that tells someone how the function works, what inputs you need, what outputs come out, and how to use it
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 83
Some examples of function script files that you have seen already:
eye(n)
Input: number n, n > 0 Output: n x n identity matrix
prod(V), prod(M)
Input: Vector V or Matrix M Output: A number or a vector
sum(V), sum(M)
Input: Vector V or Matrix M Output: A number or a vector
max(V), max(M)
Input: Vector V or Matrix M Output: A number or a vector
min(V), min(M)
Input: Vector V or Matrix M Output: A number or a vector
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 84
When you call this function script file in MATLAB, make sure the square braces are there This is required if the function script file returns more than 1 variable Failing to do this will result in the very last output variable to be saved instead
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 85
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
87
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
88
Instead of changing the parameter n, as we did in a script file, we simply changed the input parameter of the function If your function returns more than one variable, make sure you save them to a vector!
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 89
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
91
i is assigned 1 for the 1st iteration, 2 for the 2nd iteration, and n for the nth iteration For each iteration, we take the previous value of f and multiply by the current value of i
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 95
Take a close look at what I did in the for loop The 1st iteration, i = 1, the 2nd iteration, i = 4, the 3rd iteration, i = 8, and so on Pretty cool eh!? You dont have to have uniform steps in your for loops!
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 96
Everything here is the same as C and Java, except for the not equals to operator. MATLAB has it as ~=, and C or Java has it as != be careful not to mix these up!
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 99
100
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
Lets do an example!
The program will read in an array of continuous input voltages, and output an array of the same size Each element will take 1 of 3 possible values: 0, 1 and -1
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 104
Naturally, you want to use a for or while loop for this program because you want to check through every element in this input array You use an if-else statement to check to see what the voltage inputs are, then you classify accordingly
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 105
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
106
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
107
Above, I declared an array of 7 input voltages and ran the function The 2nd element is valid as logic 0 because its between 0V and 0.5V The 5th and 6th element is valid as logic 1 because theyre between 2.7V and 5V The rest are invalid
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 108
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
109
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
113
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
117
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
119
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
120
title() lets your graph have a title xlabel(), ylabel() labels the x and y axes accordingly
Put the labels inside the quotations Dont forget the quotations !
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
124
plot(x,y,bo);
This will plot a blue line with circles at each point
plot(x,y,'rx);
This will plot a red line with crosses at each point
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 125
You do this by using the subplot command The subplot command treats the window as if it had multiple slots
Each slot takes in a graph
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 127
In order to properly use subplot, you must call this function first After, you code the syntax to plot something normally
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 128
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
131
Use the subplot command to specify the number of rows and columns and which plot you want to draw first
Number ordering convention Left to right, top to bottom
Code the normal syntax you need to produce a plot. Use the plot command when youre done Use the subplot command again to go to the next area, and code the plot syntax again Repeat until youre done and heres what the window looks like!
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 132
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
133
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
135
command is the command you want to look up and to see how it works
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 136
Where command is the function youre looking for MATLAB searches all of its libraries that are related to command
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 137
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007
139
Always use the help and lookfor commands when youre learning a new function If all else fails, ask me!
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 140
I also wrote a tutorial for the Ryerson IEEE Student Branch website 3 years ago, and can be found here:
http://www.ee.ryerson.ca/~ieee/articles/MATLAB.html
ELE 532 Signals and Systems MATLAB Tutorial Friday, September 14th, 2007 141