matlab tutorial - ut arlington – uta tutoria_jixing...matlab tutorial jixing yao june 22, 2010 ....

22
MATLAB tutorial Jixing Yao June 22, 2010

Upload: phamque

Post on 16-Apr-2018

236 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

MATLAB tutorial Jixing Yao

June 22, 2010

Page 2: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Outline

• Variables and Operations

• Variables and Operations

• Some Basic Matrices

• Plot & Graphics

• Plot

• Labeling

• Complex Numbers

• Basic Operations

• FOR loop

• For loop

• Help

• Help in MATLAB

Page 3: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Variables and Operations

Page 4: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Matrix and Variables

• To create a variable

>> x=5;

>> y=sin(pi/3);

>> z=exp(-.3);

>> k=1j;

Page 5: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Matrix and Variables

• To enter a vector A

• >>A=[1 2 3 4 5];

• >>A=[1,2,3,4,5];

• >>AA=[1;2;3;4;5];

• To pick some elements of a vector

• >> AAA=A(2:5);

• >> AAA=A([1, 3:end]);

• To check the number of elements in a vector, use length

>> length(A)

ans =

5

Page 6: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Matrix and Variables

• To enter a matrix B=3 16 4

• >> B=[3 1; 6 4]; • >>B=[3,1;6,4]; • >> C=[A ; A];

• To check dimension of a matrix: • >> size(B) • ans= • 2 2

• Determinant and inverse • >>det(B) • ans= • 6 • >> inv(A) • ans = • 0.6667 -0.1667 • -1.0000 0.5000

Page 7: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Basic Operations

• Transpose • >>a=A’

• a =

• 1

• 2

• 3

• 4

• 5

• >>a=A(:);

• >> b=B’

• b =

• 3 6

• 1 4

Page 8: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Basic Operations

• Addition

• >> C=B+b

• C =

• 6 7

• 7 8

• Subtraction

• >>D=B-b

• D =

• 0 -5

• 5 0

Page 9: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Basic Operations

• Multiplication

• Matrix Multiplication

• >> E=B*b

• E =

• 10 22

• 22 52

• Element-wise Multiplication

• >>E= B.*b

• E =

• 9 6

• 6 16

Page 10: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Basic Operations

• Division

• Element-wise Division

• >>E= B./b

• E =

• 1.0000 0.1667

• 6.0000 1.0000

• bB-1

• >>F=b/B

• F =

• -4.0000 2.5000

• -3.3333 1.8333

• >>F=b*inv(B)

Page 11: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Generating a Vector of Arithmetic Sequence • v = start:increment:end

• >> v = 0:2:10

• v =

• 0 2 4 6 8 10

• >> v = 0:10

• v =

• 0 1 2 3 4 5 6 7 8 9 10

Page 12: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Some Basic Matrices

• All zero matrix • >> z=zeros(2,3) • z = • 0 0 0 • 0 0 0

• Matrix with ones • >> w = ones(2,3) • w = • 1 1 1 • 1 1 1

• Identity matrix • >> I =eye(3) • I = • 1 0 0 • 0 1 0 • 0 0 1

Page 13: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Plot & Graphics

Page 14: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Plot

• Plot

• x = [x(1), ..., x(N)]

• y = [y(1), ..., y(N)]

• plot(x,y) gives the plot with straight line connecting between the data points {(x(1), y(1)), ...(x(N), y(N))}

• x = [0:5]

• y = [1 7 5 3 2 1]

• plot(x,y)

• stem(x,y) suitable if consider a discrete signal

Page 15: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Subplot

• subplot(r,c,p)

• r is number of rows

• c is number of columns

• p is the position of that plot

• >> subplot(2,2,1)

• >> plot(x,y)

• >> subplot(2,2,2)

• >> stem(x,y)

• >> subplot(223)

• >> plot(x,y)

• >> subplot(224)

• >> stem(x,y)

Page 16: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Label the Plot

• Title title(’.......’)

• X axis xlabel(’.......’)

• Y axis ylabel(’.......’)

Page 17: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Plot Tools

Page 18: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Complex Numbers

Page 19: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Basic Operations

• Real part and imaginary part of z = 1 + j2 • >> real(z) • ans = • 1 • >> imag(z) • ans = • 2

• Find the magnitude and the phase(in radian) • >> abs(z) • ans = • 2.2361 • >> angle(z) • ans = • 1.1071

• Conjugate of z = 1 + j2 • >> conj(z) • ans = • 1.0000 - 2.0000i

Page 20: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

FOR Loop

Page 21: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

FOR loop

• FOR loop

• for i = start:increment:end

• <do P(i)>

• end

• Example: Find the summation 1+2+...+10

• >> summ=0;

• >> for i =1:10

• summ = summ+i;

• end

• >> summ

• summ =

• 55

Page 22: MATLAB tutorial - UT Arlington – UTA tutoria_Jixing...MATLAB tutorial Jixing Yao June 22, 2010 . Outline •Variables and Operations •Variables and Operations •Some Basic Matrices

Need Help?