Tutorial 1 SEG 7550 Introduction to MATLAB 18
Tutorial 1 SEG 7550 Introduction to MATLAB 18 th, SEP. 2009
Announcement l 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 l l l 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 l 4 Running X Applications (UNIX) remotely from MS Windows with VPN https: //wwwsl. se. cuhk. edu. hk/seem/index. php/Computer_Facilities: External_Acce ss_Route#Running_X_Applications_. 28 UNIX. 29_remotely_from_MS_ Windows_with_VPN
Introduction to MATLAB l l l Current version in lab: MATLAB 7. 9. 0 (R 2009 b) Help: F 1 In most cases, we will need to read function help.
Function help
Programs l l Usually long programs will be written in m file. (File->new->blank m-file) To run the program, press (save and run), or F 5.
Initial a matrix l l 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);
Vector Functions l Addition: A = [1 2 3] B = [6 4 7] A+B=?
Vector Functions l l In Matlab A = [1 2 3]; B = [6 4 7]; A+B ans = 7 6 10
Vector Functions Substraction l A = [11 2 13] B = [7 4 7] A -B = ? l
Vector Functions In Matlab A = [11 2 13]; B = [7 4 7]; A-B l ans = 4 -2 6 l
Vector Functions l l 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.
Vector Functions l A = [1 2 3] B = [2 2 3]' A * B' = ? and A' * B = ?
Vector Functions l l l 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 = 223 446 669
Vector Functions A = [1 2 3] B = [2 3 1] we want aij * bij, how?
Vector Functions l In Matlab A = [1 2 3]; B = [2 3 1]; A. *B ans = 263
Vector Functions Division element by element (i. e. aij / bij): “. /” l A = [ 4 6 10] B = [2 3 5] aij. / bij = ? l
l In Matlab A = [4 6 10]; B = [2 3 5]; A. /B ans = 222
If l l l if expression, statements, end If can be used with else, elseif to deal with complicated cases if expression 1 statements 1 elseif expression 2 statements 2 else statements 3 end
An exercise l Could you use Matlab to implement the following logic? l 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
Answer l 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
FOR l l l 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.
l A=zeros(10, 1); for j=1: 10, A(j) = j; end
while Repeatedly execute statements while condition is true. l Syntax: while expression, statements, end l Example Assign 1, 2, 3, 4, 5. . . 10 to A(1), A(2), A(3), A(4), A(5), . . . A(10) use while. l
l A=zeros(10, 1); j=1; while j<11 A(j) = j; j=j+1; end
Example: Manipulation of the specific row of matrix or vectors A = [1 2 3 614 792] l Subtract the 2 nd row from 1 st row of 3 x 3 matrix l
In Matlab A = [ 1, 2, 3; 6, 1, 4; 7, 9, 2; ] A =1 2 3 614 792 for j=1: 3, A(2, j) = A(2, j) - A(3, j); end l A ans = 1 2 3 -1 -8 2 7 9 2
- Slides: 28