1 / 28

Tutorial 1 SEG7550 Introduction to MATLAB

Tutorial 1 SEG7550 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.

kiefer
Download Presentation

Tutorial 1 SEG7550 Introduction to MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tutorial 1 SEG7550Introduction to MATLAB 18th, 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 • 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

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

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

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

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

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

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

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

  26. Example: Manipulation of the specific row of matrix or vectorsA = [1 2 3 6 1 4 7 9 2 ] • Subtract the 2nd row from 1st row of 3 x 3 matrix

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

More Related