tutorial 1seg7550 introduction to matlab

Post on 06-Jan-2016

44 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

Tutorial 1 SEG7550Introduction to MATLAB

18th, 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

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

Introduction to MATLAB

Current version in lab: MATLAB 7.9.0 (R2009b)

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

help.

Function help

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.

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

Vector Functions

Addition:

A = [1 2 3]

B = [6 4 7]

A + B = ?

Vector Functions

In Matlab

A = [1 2 3];

B = [6 4 7];

A + B ans =

7 6 10

Vector Functions

Substraction A = [11 2 13]

B = [7 4 7]

A -B = ?

Vector Functions

In Matlab

A = [11 2 13];

B = [7 4 7];

A - B ans =

4 -2 6

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.

Vector Functions

A = [1 2 3]

B = [2 2 3]'

A * B' = ?

and

A' * B = ?

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

Vector Functions

A = [1 2 3]

B = [2 3 1]

we want aij * bij, how?

Vector Functions

In Matlab

A = [1 2 3];

B = [2 3 1];

A .*B

ans =

2 6 3

Vector Functions

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

B = [2 3 5]

aij ./ bij = ?

In Matlab

A = [4 6 10];

B = [2 3 5];

A./B

ans =

2 2 2

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

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

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

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.

A=zeros(10,1);

for j=1:10,

A(j) = j;

end

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.

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 6 1 4 7 9 2 ] Subtract the 2nd row from 1st row of 3 x 3 matrix

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

top related