matlab - university of melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ml_intro.pdf · if you are...

30
ML:1 MATLAB ® and Simulink ® Computer programs for technical computation and data visualisation Powerful and very widely used Many textbooks include MATLAB examples Used in Eng Analysis, Systems Modelling, Control, … Introduced in this subject to encourage experimentation and use throughout your course Installed on ECR PCs Student edition $149 at MU Bookroom 436-121 Introduction to Mechanical Engineering

Upload: dangcong

Post on 22-Mar-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

1

MATL

AB®

and

Simulink®

•Co

mpu

ter

prog

ram

s fo

r te

chni

cal c

ompu

tati

on a

nd

data

vis

ualis

atio

n•

Powe

rful

and

ver

y wi

dely

use

d•

Man

y te

xtbo

oks

incl

ude

MA

TLA

B ex

ampl

es•

Use

d in

Eng

Ana

lysi

s, S

yste

ms

Mod

ellin

g, C

ontr

ol, …

•In

trod

uced

in t

his

subj

ect

to e

ncou

rage

ex

peri

men

tati

on a

nd u

se t

hrou

ghou

t yo

ur c

ours

e•

Inst

alle

d on

ECR

PCs

•St

uden

t ed

itio

n $1

49 a

t M

U B

ookr

oom

436-

121

Intr

oduc

tion

to

Mec

hani

cal E

ngin

eeri

ng

Page 2: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

2

Age

nda

•N

ext

Tues

day

12:0

0 EC

R La

b 1

(A21

2)–

hand

s-on

exe

rcis

es

http://www.mathworks.com/academia/

•To

day:

intr

oduc

tion

to

basi

c id

eas

in M

ATL

AB

•Fo

llow-

up–

Get

onto

a P

C in

ECR

•st

art

MA

TLA

B; o

pen

Hel

pbr

owse

r•

go t

o If

You

Are

Usi

ng M

ATL

AB

for t

he F

irst T

ime…

in t

he B

egin

Her

ese

ctio

n•

read

sec

tion

s: W

hat I

s M

ATL

AB

?, M

atric

es a

nd A

rray

s, G

raph

ics

–A

lter

nati

vely

, loo

k at

the

MA

TLA

B tu

tori

al a

t

Page 3: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

3

MATL

AB

•M

ATr

ix L

ABo

rato

ry•

An

inte

ract

ive

syst

em–

an in

terp

rete

dla

ngua

ge

•Ba

sic

data

ele

men

t is

a m

atri

x (a

rray

)–

arra

y di

men

sion

ing

not

requ

ired

•A

lso

a pr

ogra

mm

able

sys

tem

–sc

ript

s, f

unct

ions

•M

any

spec

ialis

ed a

pplic

atio

n to

olbo

xes

–e.

g. c

ontr

ol d

esig

n, s

igna

l pro

cess

ing

Page 4: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

4Si

mulink:

block

diagr

am b

uilder

& a

nalyse

r

Page 5: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

5Ove

rview

of M

ATL

AB/

Simulink

prod

ucts

http

://w

ww.m

athw

orks

.com

/pro

duct

s/pf

o/

Page 6: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

6

>>

MATL

AB

comman

d lin

e inte

ract

ion

•A

n (e

xpen

sive

) cal

cula

tor:

>> sqrt(2)*sin(pi/4)+ 3

ans=4

Com

man

d lin

e pr

ompt

A var

iabl

eans

(sho

rt f

or

‘answ

er’)

is c

reat

ed in

the

M

ATL

AB

work

spac

e

•To

see

wha

t’s in

the

wor

kspa

ce:

>> who

Your variables are:

ans

>> ans* 5

ans= 20

Vari

able

s in

the

wor

kspa

cear

e av

aila

ble

for

subs

eque

ntca

lcul

atio

ns

Page 7: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

7

•Fo

r m

ore

info

rmat

ion

abou

t wo

rksp

ace

vari

able

s:>> whos

Name Size Bytes Class

ans 1x1 8 double array

Grand total is 1 element using 8 bytes

A s

cala

r is

a 1×1

mat

rix

(arr

ay)

Vari

able

s ar

e st

ored

indo

uble

pre

cisi

on

•O

r, lo

ok in

W

orks

pace

win

dow

Page 8: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

8•

A c

alcu

lato

r wi

th n

amed

var

iabl

es:

>> theta = pi/4, amplitude = sqrt(2), offset = 3

theta =

0.7854

amplitude =

1.4142

offset =

3

>> result = amplitude*sin(theta) + offset

result =

4

Page 9: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

9

Mat

rice

s•

Ente

ring

mat

rice

s:>> A = [8 1 6; 3 5 7; 4 9 2]

A =

8 1 6

3 5 7

4 9 2

•A

lter

nati

vely

:>> A = [8 1 6

3 5 7

4 9 2]

A =

8 1 6

3 5 7

4 9 2

Rows

del

imit

ed b

y " ;

"

Page 10: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

10

Ope

ration

s on

mat

rice

s•

Tran

spos

e>> B = A'

B =

8 3 4

1 5 9

6 7 2

•D

eter

min

ant

>> Delta = det(A)

Delta =

-360

•In

vers

e>> C = inv(A)

C =

0.1472 -0.1444 0.0639

-0.0611 0.0222 0.1056

-0.0194 0.1889 -0.1028

Page 11: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

11

•M

atri

x el

emen

ts c

an b

e co

mpu

ted:

>> x = [ -1.3, 4/5, 4*atan(1) ]

x =-1.3000 0.8000 3.1416

>> A, B, C = A + B

A =

8 1 6

3 5 7

4 9 2

B =

8 3 4

1 5 9

6 7 2

C =

16 4 10

4 10 16

10 16 4

•M

atri

xad

diti

on

Page 12: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

12

>> ones(1,3)

ans

=1 1 1

•Su

btra

ctio

n>> x, y = x -

1x =-1.3000 0.8000 3.1416

y =-2.3000 -0.2000 2.1416

>> [rows, cols] = size(x)

rows = 1

cols = 3

Not

e im

plied

y = x -

ones(size(x))

MA

TLA

B bu

ilt-in

fun

ctio

ns

>> ones(size(x))

ans

=1 1 1

>> zeros(2,3)

ans=0 0 0

0 0 0

Als

o us

eful

:

>> I = eye(2)

I =

1 0

0 1

Page 13: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

13>> x, A, b_row = x*A

x =-1.3000 0.8000 3.1416

A =

8 1 6

3 5 7

4 9 2

b_row =

4.5664 30.9743 4.0832

•M

atri

xm

ulti

plic

atio

n

•an

d “d

ivis

ion”

: X = B/A

solv

es X

*A =

B;

i.e.

X = B*inv(A)

>> x_soln1 = b_row / A

x_soln1 =

-1.3000 0.8000 3.1416

Page 14: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

14

•Fu

rthe

r "d

ivis

ion"

: X = A\B

solv

es A

*X =

B;

i.e.

X = inv(A)*B

>> x_T = x', b_col

= A*x_T

x_T =

-1.3000

0.8000

3.1416

b_col

=9.2496

22.0911

8.2832

>> x_soln2 = A\b_col

x_soln2 =

-1.3000

0.8000

3.1416

Page 15: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

15

>> A, [U, Lambda] = eig(A)

A =

8 1 6

3 5 7

4 9 2

U =-0.5774 -0.8131 -0.3416

-0.5774 0.4714 -0.4714

-0.5774 0.3416 0.8131

Lambda =

15.0000 0 0

0 4.8990 0

0 0 -4.8990

•Ei

genv

alue

s an

d ei

genv

ecto

rs

Firs

t ei

genv

alue

, λ1=

15

Corr

espo

ndin

g ei

genv

ecto

r, u

(1)=

-0.5

774×

[1 1

1]T

>> U\A*U

ans

=15.0000 0.0000 -0.0000

0.0000 4.8990 -0.0000

0.0000 -0.0000 -4.8990

Asi

de: T

he ‘m

odal

mat

rix’

U

prov

ides

a s

imila

rity

tr

ansf

orm

atio

n wh

ich

diag

onal

ises

A:

Page 16: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

16

Not

e th

at p

olyn

omia

ls r

epre

sent

ed b

y RO

W V

ECTO

R of

coe

ffic

ient

s.Th

at is

,3

2()

1524

360

λλ

λ=

−−

+

•Ch

arac

teri

stic

pol

ynom

ial o

f A

=de

t(λI

-A

):>> p = poly(A)

p =

1.0000 -15.0000 -24.0000 360.0000

•Th

e ro

ots

of t

he c

hara

cter

isti

c po

lyno

mia

l are

the

eig

enva

lues

of

A:

>> r = roots(p)

r =15.0000

-4.8990

4.8990

Not

e th

at t

he r

oots

are

st

ored

in a

CO

LUM

N V

ECTO

R

•Po

lyno

mia

ls c

an b

e m

ulti

plie

d by

"co

nvol

ving

" th

em:

>> p1 = [1 1]; p2 = [2 3]; p3 = conv(p1, p2)

p3 =

2 5 3

()(

)2

12

32

53

xx

xx

++

=+

+

Term

inat

ing

an a

ssig

nmen

t ex

pres

sion

wi

th a

" ;

" su

ppre

sses

dis

play

of

outp

ut

Page 17: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

17

>> d = 1:4

d =

1 2 3 4

The

colon

oper

ator

" :

"•

Gene

rate

equ

ally

spa

ced

elem

ents

>> e = 0 : .1 : .4

e =

0 0.1000 0.2000 0.3000 0.4000

•Se

lect

mat

rix

elem

ents

thi

s wa

y>> A, A(1,:) % select first row

A =

8 1 6

3 5 7

4 9 2

ans

=8 1 6

Any

tex

t fo

llowi

ng a

" %

" is

tre

ated

as

a co

mmen

t

Page 18: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

18>> A, A(2, 1:2:3) % select every second element of 2nd row

A =

8 1 6

3 5 7

4 9 2

ans

=3 7

>> % Generate column vector of 101 angles in steps of pi/100

>> theta = [0 : 0.01 : 1]' * pi;

Any

tex

t fo

llowi

ng a

“ %

“ is

tre

ated

as

a co

mmen

t

>>

% Check first and last few elements of array

>> first_and_last = [ theta(1:3) theta(end: -1: end-2) ]

first_and_last =

0 3.1416

0.0314 3.1102

0.0628 3.0788

Mat

rix first_and_last

form

ed b

yco

ncat

enat

ion

of s

mal

ler

mat

rice

s

The end

func

tion

ret

urns

the

hig

hest

val

ue o

f a

subs

crip

t

Page 19: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

19

Mat

rix

and

arra

y op

erat

ions

•M

atri

x op

erat

ions

obe

ym

atri

x al

gebr

a

>> C = A(1:2,1:2), I

C =

8 1

3 5

I =

1 0

0 1

>> CIm

= C * I, CCm

= C^2

CIm

=8 1

3 5

CCm

= 67 13

39 28

•A

rray

ope

rati

ons

act

elem

ent-

by-e

lem

ent

>> CIa

= C .* I, CCa

= C.^2

CIa

=8 0

0 5

CCa

= 64 1

9 25

Page 20: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

20

Arr

ay o

pera

tion

s•

Arr

ays

mus

t be

of

sam

e si

ze, o

r on

e m

ust

be a

sca

lar

+ Addition

-Subtraction

.* Element-by-element multiplication

./ Element-by-element division

.\Element-by-element left division

.^ Element-by-element power

.' Unconjugated array transpose

>> n = (0:3)'; pows

= [n n.^2 2.^n]

pows

= 0 0 1

1 1 2

2 4 4

3 9 8

Page 21: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

21

MATL

AB

func

tion

s

•M

ATL

AB

prov

ides

ahu

ge r

ange

of

func

tion

s•

See

Hel

p br

owse

r •

Get

help

on

know

n fu

ncti

on (e

.g. size

) wit

h>> docsize

% -> browser window

or>> helpsize

% -> command window

Page 22: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

22

Simple

plot

ting

exa

mple

>> t = [ 0: pi/100: 2*pi ]'; % generate column "time" vector

>> y1 = sin(t);

% and 2 sinusoids ...

>> y2 = 0.5 * sin( 2*t + pi/4 );

>> plot( t, [y1 y2], ':', t, y1+y2 ), grid

>> xlabel('Time (s)')

>> ylabel('Amplitudes')

>> title('Sum of sinusoids')

>> legend('y_1', 'y_2',...

'y_1+y_2',...

'Location', 'Best')

Not

e ho

w th

e sin

func

tion

ope

rate

son

eve

ry e

lem

ent

of t

he t

vect

or--

MA

TLA

B pr

ovid

es n

atur

ally

vect

oris

ed c

ode

Page 23: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

23

3D p

lots

too

…>> t = 0 : pi/50 : 10*pi;

>> figure(2), plot3( sin(t), cos(t), t )

% generates a helix

>> xlabel('x = sin(t)'), ylabel('y = cos(t)')

>> zlabel('z = t')

>> grid on

Page 24: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

24

Surf

ace

and

cont

our

plot

s>> [X,Y] = meshgrid

(-8: 0.5: 8);

>> R = sqrt( X.^2 + Y.^2 ) + eps;

>> Z = sin(R) ./ R;

>> meshc(Z), grid on

>> xlabel('x'), ylabel('y')

>> zlabel('z')

>> title('z = sin(r)/r where r = sqrt(x^2 + y^2)')

Page 25: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

25

Prog

ramming

•Sc

ript

s–

a co

llect

ion

of M

ATL

AB

com

man

ds in

an

“M-f

ile”

–ex

ecut

e by

typ

ing

nam

e of

scr

ipt

at c

omm

and

line

–ca

n us

e an

y te

xt e

dito

r, b

ut in

-bui

lt M

ATL

AB

edit

or

has

powe

rful

deb

uggi

ng c

apab

iliti

es–

shar

e wo

rksp

ace

with

com

man

d lin

e en

trie

s–

Mat

lab

prov

ides

FO

R, W

HIL

E, I

F an

d SW

ITCH

-CA

SE p

rogr

amm

ing

cons

truc

ts

•Fu

ncti

ons

–al

so w

ritt

en a

s M

-file

s–

have

inpu

t an

d ou

tput

arg

umen

ts–

have

pri

vate

wor

kspa

ce

Page 26: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

26

Exam

ple

script

>> demo_script

What is your name? Sam

Hello Sam, The date and time is 01-Aug-2005 23:44:50

Wow. That's a short name!

Page 27: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

27

function

[mean, stdev] = stat(x)

% STAT Mean and standard deviation

% For a vector x, stat(x) returns the mean of x;

% [mean, stdev] = stat(x) returns both mean and std dev

% For a matrix x, stat(x) acts columnwise

[m, n] = size(x);

if

m == 1

m = n; % handle case of a row vector

end

mean = sum(x)/m;

stdev

= sqrt(sum(x.^2)/m -

mean.^2);Ex

ample

func

tion

>> data = rand(3,2)

data =

0.8132 0.2028

0.0099 0.1987

0.1389 0.6038

>> [mu, sigma] = stat(data)

mu =0.3206 0.3351

sigma =

0.3522 0.1900

Page 28: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

28Ve

ctor

ised

cod

e•

Supp

ose

we w

ante

d to

fin

d th

e si

ne o

f 10

,001

num

bers

fr

om 0

to

10.

•La

ngua

ges

such

as

C, F

ortr

an, B

asic

, etc

. wou

ld r

equi

re

a sc

alar

app

roac

h, u

sing

a F

OR

or D

O lo

op. L

et’s

tim

e th

is a

ppro

ach

usin

g M

ATL

AB

code

:

% Script time_scal.m

tic

i = 0;

for

t = 0: .001 : 10

i = i + 1;

y(i) = sin(t);

end

toc

•N

ow, u

sing

the

nat

ural

Mat

lab

vect

oris

edap

proa

ch:

tic

t = [0: .001 : 10];

y = sin(t);

toc

Elapsed time is 0.082611 seconds.

Elapsed time is 0.001809 seconds.

Page 29: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

29

Pros

and

Con

s of

MATL

AB

•A

dvan

tage

s–

Ease

of

use

–Pl

atfo

rm in

depe

nden

ce–

Pred

efin

ed f

unct

ions

–D

evic

e-in

depe

nden

t pl

otti

ng–

Grap

hica

l Use

r In

terf

ace

–M

ATL

AB

com

pile

r–

Use

r co

mm

unit

y

•D

isad

vant

ages

–Sp

eed

–Co

st

•Fr

ee a

lter

nati

ves

–O

ctav

e–

Scila

b

Page 30: MATLAB - University of Melbournepeople.eng.unimelb.edu.au/mcgood/436-121/ML_intro.pdf · If You Are Using MATLAB for the First Time ... signal processing. ML:4 ... Generate equally

ML:

30

Age

nda

•N

ext

Tues

day

12:0

0 EC

R La

b 1

(A21

2)–

hand

s-on

exe

rcis

es

http://www.mathworks.com/academia/

•To

day:

intr

oduc

tion

to

basi

c id

eas

in M

ATL

AB

•Fo

llow-

up–

Get

onto

a P

C in

ECR

•st

art

MA

TLA

B; o

pen

Hel

pbr

owse

r•

go t

o If

You

Are

Usi

ng M

ATL

AB

for t

he F

irst T

ime…

in t

he B

egin

Her

ese

ctio

n•

read

sec

tion

s: W

hat I

s M

ATL

AB

?, M

atric

es a

nd A

rray

s, G

raph

ics

–A

lter

nati

vely

, loo

k at

the

MA

TLA

B tu

tori

al a

t