Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Tutorial 1SEG7550 Introduction to MATLAB 18 th, SEP. 2009."— Presentation transcript:

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

2 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.

3 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 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_Acce ss_Route#Running_X_Applications_.28UNIX.29_remotely_from_MS_ Windows_with_VPN https://www- sl.se.cuhk.edu.hk/seem/index.php/Computer_Facilities:External_Acce ss_Route#Running_X_Applications_.28UNIX.29_remotely_from_MS_ Windows_with_VPN

4 Introduction to MATLAB Current version in lab: MATLAB 7.9.0 (R2009b) Help: F1 In most cases, we will need to read function help.

5 Function help

6 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.

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

8 Vector Functions Addition: A = [1 2 3] B = [6 4 7] A + B = ?

9 Vector Functions In Matlab A = [1 2 3]; B = [6 4 7]; A + B ans = 7 6 10

10 Vector Functions Substraction A = [11 2 13] B = [7 4 7] A -B = ?

11 Vector Functions In Matlab A = [11 2 13]; B = [7 4 7]; A - B ans = 4 -2 6

12 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.

13 Vector Functions A = [1 2 3] B = [2 2 3]' A * B' = ? and A' * B = ?

14 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

15 Vector Functions A = [1 2 3] B = [2 3 1] we want aij * bij, how?

16 Vector Functions In Matlab A = [1 2 3]; B = [2 3 1]; A.*B ans = 2 6 3

17 Vector Functions Division element by element (i.e. aij / bij): “./” A = [ 4 6 10] B = [2 3 5] aij./ bij = ?

18 In Matlab A = [4 6 10]; B = [2 3 5]; A./B ans = 2 2 2

19

20 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

21 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

22 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

23 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.

24 A=zeros(10,1); for j=1:10, A(j) = j; end

25 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.

26 A=zeros(10,1); j=1; while j<11 A(j) = j; j=j+1; end

27 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

28 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


Download ppt "Tutorial 1SEG7550 Introduction to MATLAB 18 th, SEP. 2009."

Similar presentations


Ads by Google