tutorial 1seg7550 introduction to matlab

28
Tutorial 1 SEG7550 Introduction to MATLAB 18 th , SEP. 2009

Upload: kiefer

Post on 06-Jan-2016

44 views

Category:

Documents


0 download

DESCRIPTION

Tutorial 1SEG7550 Introduction to MATLAB. 18 th , SEP. 2009. Announcement. If you are from other departments other than SEEM, leave your student ID, name and department to me before you leave. A temporary account will be created for you. Access MATLAB outside campus. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Tutorial 1SEG7550 Introduction to MATLAB

Tutorial 1 SEG7550Introduction to MATLAB

18th, SEP. 2009

Page 2: Tutorial 1SEG7550 Introduction to MATLAB

Announcement

If you are from other departments other than SEEM, leave your student ID, name and department to me before you leave. A temporary account will be created for you.

Page 3: Tutorial 1SEG7550 Introduction to MATLAB

Access MATLAB outside campus

1 Department VPN service currently is only available to SEEM/AECT Staff, gds and MSc only .

2 Idle timeout of 4 hours 3 First set up a vpn: http://

www.cuhk.edu.hk/itsc/network/vpn/index.html 4 Running X Applications (UNIX) remotely from MS

Windows with VPN

https://www-sl.se.cuhk.edu.hk/seem/index.php/Computer_Facilities:External_Access_Route#Running_X_Applications_.28UNIX.29_remotely_from_MS_Windows_with_VPN

Page 4: Tutorial 1SEG7550 Introduction to MATLAB

Introduction to MATLAB

Current version in lab: MATLAB 7.9.0 (R2009b)

Help: F1 In most cases, we will need to read function

help.

Page 5: Tutorial 1SEG7550 Introduction to MATLAB

Function help

Page 6: Tutorial 1SEG7550 Introduction to MATLAB

Programs

Usually long programs will be written in m file. (File->new->blank m-file)

To run the program, press (save and run), or F5.

Page 7: Tutorial 1SEG7550 Introduction to MATLAB

Initial a matrix

Ones: Create array of all ones

Syntax:Y = ones(n); Y = ones(m,n);

Zeros: Create array of all zeros

Syntax:Y = zeros(n); Y = zeros(m,n);

Page 8: Tutorial 1SEG7550 Introduction to MATLAB

Vector Functions

Addition:

A = [1 2 3]

B = [6 4 7]

A + B = ?

Page 9: Tutorial 1SEG7550 Introduction to MATLAB

Vector Functions

In Matlab

A = [1 2 3];

B = [6 4 7];

A + B ans =

7 6 10

Page 10: Tutorial 1SEG7550 Introduction to MATLAB

Vector Functions

Substraction A = [11 2 13]

B = [7 4 7]

A -B = ?

Page 11: Tutorial 1SEG7550 Introduction to MATLAB

Vector Functions

In Matlab

A = [11 2 13];

B = [7 4 7];

A - B ans =

4 -2 6

Page 12: Tutorial 1SEG7550 Introduction to MATLAB

Vector Functions

Multiplication In matrix calculation, there are two kinds of

multiplication, element by element and matrix multiplication.

Please note that the multiplication of vector must match their size. For example, the size of A and B are m x n and k x j vector respectively. Then m must be equal to k

For element by element multiplication, two matrix must have same size.

Page 13: Tutorial 1SEG7550 Introduction to MATLAB

Vector Functions

A = [1 2 3]

B = [2 2 3]'

A * B' = ?

and

A' * B = ?

Page 14: Tutorial 1SEG7550 Introduction to MATLAB

Vector Functions

In Matlab A = [1 2 3]; B = [2 2 3]; A * B Error using ==> * Inner matrix dimensions must agree A * B' ans = 15 A' * B ans = 2 2 3 4 4 6 6 6 9

Page 15: Tutorial 1SEG7550 Introduction to MATLAB

Vector Functions

A = [1 2 3]

B = [2 3 1]

we want aij * bij, how?

Page 16: Tutorial 1SEG7550 Introduction to MATLAB

Vector Functions

In Matlab

A = [1 2 3];

B = [2 3 1];

A .*B

ans =

2 6 3

Page 17: Tutorial 1SEG7550 Introduction to MATLAB

Vector Functions

Division element by element (i.e. aij / bij): “./” A = [ 4 6 10]

B = [2 3 5]

aij ./ bij = ?

Page 18: Tutorial 1SEG7550 Introduction to MATLAB

In Matlab

A = [4 6 10];

B = [2 3 5];

A./B

ans =

2 2 2

Page 19: Tutorial 1SEG7550 Introduction to MATLAB
Page 20: Tutorial 1SEG7550 Introduction to MATLAB

If

if expression, statements, end If can be used with else, elseif to deal with

complicated cases if expression1 statements1

elseif expression2 statements2 else statements3

end

Page 21: Tutorial 1SEG7550 Introduction to MATLAB

An exercise

Could you use Matlab to implement the following logic?

Case OP_CP > 0 and OP_LO > 0 and CP_HI <= 0 : A = 1

Case OP_CP = 0 and OP_LO > 0 and CP_HI < 0 : A = 2

Case OP_CP = 0 and OP_LO = 0 and CP_HI < 0 : A =3

Otherwise, A=4

Page 22: Tutorial 1SEG7550 Introduction to MATLAB

Answer

Matlab Code: if op_cp > 0 & op_lo >= 0 & cp_hi <= 0 A = 1; elseif op_cp == 0 & op_lo > 0 & cp_hi < 0 A = 2; elseif op_cp = 0 & op_lo = 0 & cp_hi < 0 A = 3; else A = 4; end

Page 23: Tutorial 1SEG7550 Introduction to MATLAB

FOR

Repeat statements a specific number of times.

for x=initval:endval, statements, end Example: 

Assign 1,2,3,4,5 ... 10 to A(1), A(2), A(3), A(4), A(5), ... A(10) use if.

Page 24: Tutorial 1SEG7550 Introduction to MATLAB

A=zeros(10,1);

for j=1:10,

A(j) = j;

end

Page 25: Tutorial 1SEG7550 Introduction to MATLAB

while

Repeatedly execute statements while condition is true.

Syntax: while expression, statements, end Example

Assign 1,2,3,4,5 ... 10 to A(1), A(2), A(3), A(4), A(5), ... A(10) use while.

Page 26: Tutorial 1SEG7550 Introduction to MATLAB

A=zeros(10,1);

j=1;

while j<11

A(j) = j;

j=j+1;

end

Page 27: Tutorial 1SEG7550 Introduction to MATLAB

Example: Manipulation of the specific row of matrix or vectors

A = [1 2 3 6 1 4 7 9 2 ] Subtract the 2nd row from 1st row of 3 x 3 matrix

Page 28: Tutorial 1SEG7550 Introduction to MATLAB

In Matlab A = [ 1, 2, 3;6,1,4;7, 9, 2; ] A =1 2 3 6 1 4 7 9 2

for j=1:3, A(2,j) = A(2,j) - A(3,j); end

A ans = 1 2 3-1 -8 2 7 9 2