1.48k likes | 1.74k Views
ELE 532 – Signals and Systems Fall 2007 MATLAB Tutorial. Raymond Phan Distributed Multimedia Computing Research (DMCR) Lab Ryerson University – EPH 237 rphan@ee.ryerson.ca. http://www.ee.ryerson.ca/~rphan/ele532/MATLABTutorial.ppt. Outline of Tutorial – (1). Introduction Getting Started
E N D
ELE 532 – Signals and SystemsFall 2007MATLAB Tutorial Raymond Phan Distributed Multimedia Computing Research (DMCR) Lab Ryerson University – EPH 237 rphan@ee.ryerson.ca http://www.ee.ryerson.ca/~rphan/ele532/MATLABTutorial.ppt
Outline of Tutorial – (1) • Introduction • Getting Started • Variables and Starting Basics • Vectors and Matrices • Basic Operations • Script Files • Function Script Files
Outline of Tutorial – (2) • Flow Control • Basic Graphics Commands • Other Useful Commands • Final Words
Introduction – (1) • So… what the hell is MATLAB and what’s 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
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, you’re 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
Outline of Tutorial – (1) • Introduction • Getting Started • Variables and Starting Basics • Vectors and Matrices • Basic Operations • Script Files • Function Script Files
Getting Started – (1) • Where can I find and use MATLAB? • Method #1: On the EE undergraduate network labs: • ENG 406, 407, 408 and 409 • Log onto an EE undergraduate terminal • A) Go to Applications – Accessories – Terminal and type in ‘matlab’ (without the quotes) • B) Go to Applications – Math – MATLAB 2007a
Getting Started – (2) • Method #2: If you don’t feel like using the computers at school, you can install your own copy of MATLAB on your PC or laptop • There are many ways to obtain your own copy: • Buy a student version at the Ryerson Bookstore • Obtain a trial version online via: http://www.mathworks.com • “Borrow” from a friend • 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 uses the JRE as a backbone to run the whole application
Getting Started – (3) • What happens next!? • MATLAB Interface: >> means it’s ready for input from you
Outline of Tutorial – (1) • Introduction • Getting Started • Variables and Starting Basics • Vectors and Matrices • Basic Operations • Script Files • Function Script Files
Variables and Basic Commands – (1) • One GREAT thing about MATLAB: • MATLAB is a programming language that is dynamically typed… what does this mean? • You can declare variables and initialize them without specifying what type they are • MATLAB automatically figures this out for you, or you can choose to manually override the type • Example: • C or Java way: int nikhil = 1, double jenny = 2 • MATLAB way: nikhil = 1, jenny = 2
Variables and Basic Commands – (2) • When you want to assign something to a variable, use the = sign • When you assign something to a variable, MATLAB initializes & automatically declares it • Guidelines for variable names: • All must be single words, no spaces • Must begin with a letter, numbers or the underscore character ( _ ) • Variable names are case sensitive • i.e nikhil is NOT the same as Nikhil • i.e muffin is NOT the same as mUFfin • Names can be up to 19 characters in length
Variables and Basic Commands – (3) • Some valid variable names: • voltage • valueOfR1 • Ron_and_Mauro • _Alan2007_ • Some invalid variable names (why are these invalid?): • 123 • value of R1 • 3v • X#*()$#$!!!
Variables and Basic Commands – (4) • Left panel: Current Directory / Workspace • A) Shows you directory structure to access working directory (more on this later) • B) Shows you all of the variables that have been created and can be accessed • Right: Command Prompt • Enter commands and variable declarations here • Commands without a semicolon ( ; ) echo your command to screen • Commands with a semicolon suppress that output
Variables and Basic Commands – (5) • Can enter commands either: • One at a time: The end of each command, press ENTER (carriage return) • Multiple commands in one line: • Suppress echoing: Use semicolons to separate each command on the line • Enable echoing: Use commas ( , ) to separate each command on the line • 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)
Variables and Basic Commands – (7) • who command: Shows you all of the variables created by you • You can also check the workspace as well • 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
Variables and Basic Commands – (9) • Can also declare complex numbers too: • Can add, subtract, multiply and divide • 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, we’ll get into addition, subtraction, multiplication and division in another way later
Variables and Basic Commands – (11) • Command History window: Used to keep track of the commands you ran recently • You can alsodouble click on any of the commandsto re-run them again • You can also pressthe up & down keysto cycle throughthe commands aswell in the commandprompt
Outline of Tutorial – (1) • Introduction • Getting Started • Variables and Starting Basics • Vectors and Matrices • Basic Operations • Script Files • Function Script Files
Vectors and Matrices – (1) • Unless otherwise defined, MATLAB treats ALL variables as 2D matrices… how is this possible? • Arrays and Vectors: N x 1 or 1 x N matrix • Single value: 1 x 1 matrix • Why does MATLAB decide to handle it this way? • You’ll see later that handling variables as matrices makes things A LOT faster and easier to work with
Vectors and Matrices – (2) • How do you declare a vector / array in MATLAB? • C or Java way: int a[4] = {1, 2, 3, 4}; • MATLAB way: • a = [1 2 3 4] – row vector • Spaces mean to move to the next column • 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
Vectors and Matrices – (3) • How do I access elements in a vector / array? • C or Java way: • int jenny = a[0]; • 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!
Vector and Matrices – (5) • How do I create a matrix in MATLAB? • C or Java way: int a[4][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; • MATLAB way: • #1: a = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]; • #2: a = [1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16];
Vector and Matrices – (6) • How do I access elements in a matrix? • C or Java way: • int alan = a[2][3]; • MATLAB way: • alan = a(3,4); • What’s the difference here? • No separate brackets for each dimension • Comma is used to separate the dimensions • All indices to access arrays are offset by 1! • Remember: 1st parameter is the row, 2nd parameter is the column
Vector and Matrices – (7) • Here’s something to mess you up… how do I access a range of values in a matrix? • Suppose I had a matrix already created called ray • How do I get all of the elements in the 1st row? • C or Java way: • int i;for(i = 0; i < 4; i++) ray[i] = a[0][i]; • This is a pain in the butt!... There’s gotta be an easier way to do this!
Vectors and Matrices – (8) • MATLAB way: • ray = a(1, 1:4); • ray = a(1, :); • What’s the difference here?! • NO for loop! • The colon ( : ) operator is used to access a range of values • There is a more general use for this, but we’ll get into this later • 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!
Vectors and Matrices – (10) • Some more examples: • sally = a(2,3); • sally = a(1:3,3:4); • sally = a(:, 1:3); • sally = a(2:4,:); • 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
Vectors and Matrices – (11) • Here’s a curve ball! • Joe = a(:,:); • Joe = a; • What does this mean? Copy the entire matrix, a, and assign it to Joe • You can also do the 2nd line too. It’s exactly the same meaning
Vectors and Matrices – (12) • Example Time! • 1) How do we define M in MATLAB syntax? • 2) How do we execute a), b), c) and d)?
Vectors and Matrices – (15) • Example #2!
Vectors and Matrices – (16) • In other words, with matrices A, B and C, make me a matrix that looks like this! • What do we need to do? • How do we define matrices A, B and C? • How do we create M?
Vectors and Matrices – (18) • MATLAB has a really cool way of making vectors / arrays where consecutive elements are uniformly spaced • Example: • Ray = 0 : 0.1 : 1.0; • This generates a vector / array with 11 elements, such that • Ray = [0 0.1 0.2 0.3 … 1.0];
Vectors and Matrices – (19) • General Form: • new_array = first_value : increment : last_value • Make note of the colons ( : )! • 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 don’t include this value, it is automatically assumed to be 1
Vectors and Matrices – (20) • Examples: • jenny = 0 : 2 : 10; • eman = 3 : 3 : 30; • ron = 1 : 10; • mauro = 2.0 : -0.2 : 0.4; • 1st line: Creates a 6 element vector • jenny = [0 2 4 6 8 10]; • 2nd line: Creates a 10 element vector • eman = [3 6 9 12 … 27 30];
Vectors and Matrices – (22) • 3rd line: Creates an 10 element vector • ron = [1 2 3 … 9 10]; • 4th line: Creates a 8 element vector • mauro = [2.0 1.8 … 0.6 0.4]; • Pretty easy don’t 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.
Vectors and Matrices – (23) • Some useful matrix and vector / array commands • eye(n): Creates an n x nidentity matrix
Vectors and Matrices – (24) • Some useful matrix and vector / array commands • ones(n,m): Creates an n x m matrix full of ones • ones(1,n) or ones(n,1): Creates an array / vector that has n elements, full of ones
Vectors and Matrices – (25) • Some useful matrix and vector / array commands • zeros(n,m): Creates an n x m matrix full of zeros • zeros(1,n) or zeros(n,1): Creates an array / vector that has n elements, full of zeros
Vectors and Matrices – (26) • Last important note: • MATLAB evaluates expressions to the right of the equals sign first • After, it assigns this result to the variable to the left of the equals sign • Here’s an example:sum = 2;sum = sum + 3; • What happens here? sum gets assigned the value of 2 first, then it gets added with 3, and stored back into sum • … and that’s it for this section… whew!
Outline of Tutorial – (1) • Introduction • Getting Started • Variables and Starting Basics • Vectors and Matrices • Basic Operations • Script Files • Function Script Files
Basic Operations – (1) • Let’s assume the following: • A and B: Are matrices or vectors / arrays of compatible dimensions • Assume they can be added, subtracted, multiplied and divided properly • n is a scalar (single value number) • Here’s a table that provides a good summary of all of the basic operations you can perform on matrices and vectors / arrays
Basic Operations – (2) • 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