ece 351 m atlab i ntroduction ( by t eaching a ssistants )

37
ECE 351 MATLAB INTRODUCTION (BY TEACHING ASSISTANTS)

Upload: jocelin-spencer

Post on 17-Jan-2018

223 views

Category:

Documents


0 download

DESCRIPTION

MATLAB is a high-level language and interactive environment that enables you to perform computationally intensive tasks. High-level language for technical computing Development environment for managing code, files, and data

TRANSCRIPT

Page 1: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

ECE 351

MATLAB INTRODUCTION(BY TEACHING ASSISTANTS)

Page 2: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

WHY MATLAB? The name MATLAB (matrix laboratory), is

Developed by Mathworks.http://www.mathworks.com/products/matlab/index.html

While the software has progressed well beyond its original goal as a tool dedicated to performing matrix computations, it is still based on the notation of arrays and matrices.

The latest version is R2013b

Page 3: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

MATLAB is a high-level language and interactive environment that enables you to perform computationally intensive tasks.

High-level language for technical computing Development environment for managing

code, files, and data

Page 4: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

Interactive tools for iterative exploration, design, and problem solving

Mathematical functions for linear algebra, statistics, Fourier analysis, filtering, optimization, and numerical integration

2-D and 3-D graphics functions for visualizing data

Page 5: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

MATRICES, ARRAYS AND VECTORS A matrix is simply a rectangular list of

numbers The "size" of a matrix is given as two numbers,

the first is traditionally the number of rows in the matrix while the second is the number of columns in the matrix.

Matrices are usually written in tabular form contained between two large parentheses or square brackets.

Page 6: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

Arrays/vectors: matrices with only ONE row or column

Example A 2×3 ( two by three)matrix 34 56 31 -45 6 43

A 1×3 row vector 34 56 31

A 2×1 column vector 34 -45

Page 7: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

TO START MATLAB Command window Single line commands, results Editor Edit scripts Workspace Store variables Current Folder Store files

Page 8: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

TO START MATLAB

Page 9: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

TO START MATLAB Command window Only one line a time Create a new .m file File->New->Script Check the usage of ‘;’, ‘%’, ‘%%’ Save and Run

Page 10: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

PRESENT MATRICES IN MATLAB >> a=[1 2 3; 4 5 6]Or>> a=[1, 2, 3; 4, 5, 6]

Try on your own computer if you haven’t done so.

Page 11: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

ARITHMETIC OPERATIONS Scalar operations: There are four scalar operations

addition: + subtraction: - multiplication: * division: /

Page 12: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

Example>> a=[1 2 3; 4 5 6]>> b=3*a>> b=[3 6 9; 12 15 18]

Page 13: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

Matrix Addition and Subtraction For two matrices to be added or

subtracted they must be of the same size. The entries are computed by adding or

subtracting the corresponding entries in the two original matrices.

Example >> a=[1 2 3; 4 5 6] >> b=[2 4 6; 3 5 7] >> c=a-b c=[-1 -2 -3; 1 0 -1]

Page 14: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

Multiplication and Division 2 different types: componentwise and

conventional

Recall: How to multiply matrices?

2 1 1 03 0 4 2

Page 15: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

Normal multiplication: A, B, C is n by n

In MATLAB >> C=A*B

A B C

1

n

jk ji iki

c a b

Page 16: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

Componentwise multiplication: Same problem as before,

In MATLAB >> C=A.*B

jk jk jkc a b

Page 17: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

FUNCTIONS AND SHORTCUTS

Functions: operations that can be called in a scripts

Zeros() Ones() Eye() Diag() Linspace() …

Page 18: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

Zeros(n) to create an n by n matrix with all entries

zero. Zeros(n, m) create an n by m one. Ones(n) to create an n by n matrix with all entries 1.

Used as the same fashion as zeros(n) Eye(n) to create an n by n identity matrix. Diag([]) to create a diagonal matrix. Vector given

shows as diagonal elements.

Page 19: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

Two shortcuts for row vectors:1. vector=a:n:bthe vector starts with a and end with b, n is the

step sizeExample>> t=1:0.5:3 t=1 1.5 2 2.5 3

If n is not given, default value is 1>> t=1:3 t=1 2 3

Page 20: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

2 linspace(a, b, n) to create a row matrix with n elements, start

from a and end with b, with equal step size.Example: >> t=linspace(0,10,11)t=0 1 2 3 4 5 6 7 8 9 10

Page 21: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

size() inv() []’ fft()

Functions can be self defined. Most functions work componentwise

Page 22: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

SELF-DEFINED FUNCTION File->New->Function Input arguments Output arguments Function name How to call function

Page 23: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

HOW TO PLOT FIGURES?Plot(x,y):2 vector input, x will be horizontal axis and y

be the vertical one.x, y must be the same size

s: applicable parameters: color, plot symbol, line type used as character strings.

Page 24: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

t=0:0.1:5 y=sin(2*pi*t) Plot(t,y)

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 25: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

t=0:0.001:5 y=sin(2*pi*t) Plot(t,y)

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 26: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

plot(t,y,'--')

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 27: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

plot(t,y,'g')

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 28: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

Legend, title and label

legend(‘strings1’,’strings2’…..’location’,’orientation’)

e.g legend('sin function') TitleTitle(‘text’) e.g title('function') LabelXlabel(‘text’) e.g xlabel('time')Similar: ylabel, zlabel

Page 29: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

time

Am

plitu

de

function

sin function

Page 30: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

Example 1 t=0:0.001:5 x=cos(2*pi*t) y=sin(2*pi*t) plot(t,x,'g') hold on plot(t,y,'r')

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 31: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

Example 2 t=0:0.001:5 x=cos(2*pi*t) y=sin(2*pi*t) subplot(2,1,1) plot(t,x,'g') subplot(2,1,2) plot(t,y,'r')

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-1

-0.5

0

0.5

1

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-1

-0.5

0

0.5

1

Page 32: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

HW1 t = (-2:0.01:2)/1000; a1 = 500; x1 = 20 * sin(2*pi*1000*t - pi/3) .* exp(-a1*t); a2 = 750; x2 = 20 * sin(2*pi*1000*t - pi/3) .* exp(-a2*t); a3 = 1000; x3 = 20 * sin(2*pi*1000*t - pi/3) .* exp(-a3*t); %Plot Resutls figure(1); clf; plot(t,x1,'b'); hold on plot(t,x2,'k:'); plot(t,x3,'r--'); hold off xlabel('time (sec)') ylabel('Amplitude') title('Exponentially Damped Sinusoid') axis([-2/1000 2/1000 -120 120]) legend('a = 500','a = 750','a = 1000')

Page 33: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

HW1

-2 -1.5 -1 -0.5 0 0.5 1 1.5 2

x 10-3

-100

-50

0

50

100

time (sec)

Am

plitu

de

Exponentially Damped Sinusoid

a = 500a = 750a = 1000

Page 34: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

2. semilogy(x, y, s) logarithmic (base 10) scale is used for the Y-

axis. Used as the same fashion as plot. Similar: semilogx, loglog.

Example:

Page 35: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

BASIC PROGRAMMING SYNTAX FOR ‘for’ is used for repeating statements Example: t=0; for i=1:5 t=t+i; end

Also see WHILE

Page 36: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

IF If Conditionally execute statements. Example t=0; for i=1:5 if i>3 t=t+i; end end

Page 37: ECE 351 M ATLAB I NTRODUCTION ( BY T EACHING A SSISTANTS )

QUESTIONOS