1. Trang chủ
  2. » Công Nghệ Thông Tin

signals and systems matlab tutorial

142 601 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 142
Dung lượng 1,71 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

ELE 532 – Signals and SystemsMATLAB Tutorial Friday, September 14th, 2007 5 Introduction – 2 • MATLAB is selected as a numerical analysis tool over languages like C and Java because: –

Trang 1

ELE 532 – Signals and Systems

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

Trang 2

Outline of Tutorial – (1)

Introduction

Getting Started

Variables and Starting Basics

Vectors and Matrices

Basic Operations

Script Files

Function Script Files

Trang 3

ELE 532 – Signals and Systems

Trang 4

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

Trang 5

ELE 532 – Signals and Systems

MATLAB Tutorial

Friday, September 14th, 2007

5

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

Trang 6

Outline of Tutorial – (1)

Introduction

Getting Started

Variables and Starting Basics

Vectors and Matrices

Basic Operations

Script Files

Function Script Files

Trang 7

ELE 532 – Signals and Systems

• 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

Trang 8

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

Trang 9

ELE 532 – Signals and Systems

MATLAB Tutorial

Friday, September 14th, 2007

9

Getting Started – (3)

– MATLAB Interface: >> means it’s ready for input from you

Trang 10

Outline of Tutorial – (1)

Introduction

Getting Started

Variables and Starting Basics

Vectors and Matrices

Basic Operations

Script Files

Function Script Files

Trang 11

ELE 532 – Signals and Systems

– 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

Trang 12

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

Trang 13

ELE 532 – Signals and Systems

Trang 14

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

Trang 15

ELE 532 – Signals and Systems

– 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

pressing ENTER will redisplay the

variable

not assigning it to anything, the

value will be automatically assigned

to a variable called ans (answer)

Trang 16

Variables and Basic Commands –

(6)

Trang 17

ELE 532 – Signals and Systems

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

Trang 18

Variables and Basic Commands –

(8)

Trang 19

ELE 532 – Signals and Systems

– 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

Trang 20

Variables and Basic Commands –

(10)

Trang 21

ELE 532 – Signals and Systems

track of the commands you ran

recently

You can also

double click on

any of the commands

to re-run them again

You can also press

the up & down keys

to cycle through

the commands as

well in the command

prompt

Trang 22

Outline of Tutorial – (1)

Introduction

Getting Started

Variables and Starting Basics

Vectors and Matrices

Basic Operations

Script Files

Function Script Files

Trang 23

ELE 532 – Signals and Systems

MATLAB Tutorial

Friday, September 14th, 2007

23

Vectors and Matrices – (1)

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

handle it this way?

– You’ll see later that handling variables as matrices

makes things A LOT faster and easier to work with

Trang 24

Vectors and Matrices – (2)

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

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

Trang 25

ELE 532 – Signals and Systems

MATLAB Tutorial

Friday, September 14th, 2007

25

Vectors and Matrices – (3)

How do I access elements

– No square brackets when accessing an

element! Use round brackets!

– Elements do not start at index 0, they start at

index 1!

Trang 26

Vector and Matrices – (4)

Trang 27

ELE 532 – Signals and Systems

Trang 28

Vector and Matrices – (6)

– C or Java way:

• int alan = a[2][3];

– MATLAB way:

• alan = a(3,4);

– No separate brackets for each dimension

– Comma is used to separate the dimensions

– All indices to access arrays are offset by 1!

– Remember: 1 st parameter is the row, 2 nd parameter

is the column

Trang 29

ELE 532 – Signals and Systems

MATLAB Tutorial

Friday, September 14th, 2007

29

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?

Trang 30

Vectors and Matrices – (8)

• 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

Trang 31

ELE 532 – Signals and Systems

Trang 32

Vectors and Matrices – (10)

Some more examples:

row, and assign this to sally

4th line: Get a matrix with elements between rows 2 – 4 and give me every possible

Trang 33

ELE 532 – Signals and Systems

MATLAB Tutorial

Friday, September 14th, 2007

33

Vectors and Matrices – (11)

– Joe = a(:,:);

– Joe = a;

entire matrix, a, and assign it

to Joe

You can also do the 2 nd line

too It’s exactly the same

meaning

Trang 34

Vectors and Matrices – (12)

Trang 35

ELE 532 – Signals and Systems

Trang 36

Vectors and Matrices – (14)

Trang 37

ELE 532 – Signals and Systems

Trang 38

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?

Trang 39

ELE 532 – Signals and Systems

Trang 40

Vectors and Matrices – (18)

making vectors / arrays where

consecutive elements are

uniformly spaced

Example:

– Ray = 0 : 0.1 : 1.0;

with 11 elements, such that

– Ray = [0 0.1 0.2 0.3 … 1.0];

Trang 41

ELE 532 – Signals and Systems

– 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

Trang 42

Vectors and Matrices – (20)

Trang 43

ELE 532 – Signals and Systems

MATLAB Tutorial

Friday, September 14th, 2007

43

Vectors and Matrices – (22)

3 rd line: Creates an 10 element

vector

– ron = [1 2 3 … 9 10];

4 th 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.

Trang 44

Vectors and Matrices – (23)

vector / array

commands

– eye(n): Creates an n x n identity matrix

Trang 45

ELE 532 – Signals and Systems

MATLAB Tutorial

Friday, September 14th, 2007

45

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

Trang 46

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

Trang 47

ELE 532 – Signals and Systems

MATLAB Tutorial

Friday, September 14th, 2007

47

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!

Trang 48

Outline of Tutorial – (1)

Introduction

Getting Started

Variables and Starting Basics

Vectors and Matrices

Basic Operations

Script Files

Function Script Files

Trang 49

ELE 532 – Signals and Systems

MATLAB Tutorial

Friday, September 14th, 2007

49

Basic Operations – (1)

– 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)

a good summary of all of the basic operations you can

perform on matrices and

vectors / arrays

Trang 50

Basic Operations – (2)

vectors / arrays can be real or

complex

and subtract two matrices normally

component gets added or subtracted

Trang 51

ELE 532 – Signals and Systems

MATLAB Tutorial

Friday, September 14th, 2007

51

Basic Operations – (3)

Trang 52

Basic Operations – (4)

Multiplication (this kind) only works

for matrices

* is for matrix multiplication

Division is a little bit more

complicated

We have left division and right division

• Left Division == A B

Trang 53

ELE 532 – Signals and Systems

• Real valued entries: Normal Transpose

• Complex valued entries: Tranposes and performs complex

conjugate

– ’ operator

• Real and Complex valued entries: Normal Transpose

Trang 54

Basic Operations – (6)

Trang 55

ELE 532 – Signals and Systems

MATLAB Tutorial

Friday, September 14th, 2007

55

Basic Operations – (7)

with itself n times

there is something called element operations for vectors /

element-by-arrays and matrices

– You put a dot operator ( ) before the operation

• Example: * or / or \ or ^

– The 1st element of A is *, /, \ or ^ with the 1st element of

B, and that result gets stored

– … don’t get it? Here’s an example

Trang 56

Basic Operations – (8)

Trang 57

ELE 532 – Signals and Systems

– If you wanted to multiply or divide every number in a matrix

or vector / array by a constant, you can just simply do the following:

All you have to do is multiply or divide

by the desired number

– No need to create another matrix or vector / array and do

point-by-point multiplication or division!

Trang 58

Basic Operations – (10)

Special Case #2:

– If you wanted to add or subtract every number in a matrix or

vector / array by a constant, you can just simply do the

– No need to create another matrix or vector / array and add or

subtract that way!

Trang 59

ELE 532 – Signals and Systems

MATLAB Tutorial

Friday, September 14th, 2007

59

Basic Operations – (10)

matrices and vectors / arrays:

length and M is a matrix of arbitrary size

– Gives the number of elements the vector V has, and

stores it into len

– Gives the number of rows and columns and stores

them into rows and cols respectively

– Don’t worry about the square braces for now, we’ll

deal with them later.

Trang 60

Basic Operations – (11)

sum(V): Returns the sum of

all elements contained in

vector V

sum(M): Returns a row vector,

where each element is the sum

of the columns of matrix M

… confused? Don’t worry,

check out this example

Trang 61

ELE 532 – Signals and Systems

MATLAB Tutorial

Friday, September 14th, 2007

61

Basic Operations – (12)

Trang 62

Basic Operations – (13)

prod(V): Returns the product of

all elements contained in vector V

– We multiply every single element in the vector with each other

prod(M): Returns a row vector,

where each element is the

product of the columns of matrix M

… confused? Don’t worry, check out this example

Trang 63

ELE 532 – Signals and Systems

MATLAB Tutorial

Friday, September 14th, 2007

63

Basic Operations – (14)

Trang 65

ELE 532 – Signals and Systems

Anyway, let’s keep going

max(V) / min(V): Returns the maximum /

minimum of all elements contained in

vector V

max(M) / min(M): Returns a row vector,

where each element is the maximum /

minimum of the columns of matrix M

… confused again? No worries, here’s another example

Trang 66

Basic Operations – (17)

Trang 67

ELE 532 – Signals and Systems

Variables and Starting Basics

Vectors and Matrices

Basic Operations

Script Files

Function Script Files

Trang 68

Script Files – (1)

So far, you’ve seen that MATLAB can

accept and execute commands

interactively through the command prompt

– … what happens if you’ve got A LOT of commands to

execute?

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

don’t have to input all of the commands individually.

Trang 69

ELE 532 – Signals and Systems

MATLAB Tutorial

Friday, September 14th, 2007

69

Script Files – (2)

file or M-file (M for MATLAB!)

– You place all commands you want to execute in a

file with extension m at the end, and you run the

script file

– MATLAB will run all of these commands in

sequence for you

you set the working directory to be where the script file is located

(remember I said we’d get back to

this earlier?)

Trang 70

Script Files – (3)

Trang 71

ELE 532 – Signals and Systems

MATLAB Tutorial

Friday, September 14th, 2007

71

Script Files – (4)

that you can use where you can create your scripts

editor Just make sure the extension of the file is m

Trang 72

Script Files – (5)

Trang 73

ELE 532 – Signals and Systems

MATLAB Tutorial

Friday, September 14th, 2007

73

Script Files – (6)

Once a script file has been created,

type in the name of the file (without

the m) in the command prompt to execute the script

– Make sure you set the proper working directory!

When you execute a script file, all of the variables created in the script file get stored in the workspace for future use

Let’s do an example:

– Let’s make the factorial example into a script file

Ngày đăng: 24/10/2014, 23:36

TỪ KHÓA LIÊN QUAN