fisika komputasi matlab

34
Introduction to Matlab

Transcript of fisika komputasi matlab

Page 1: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 1/34

Introduction to Matlab

Page 2: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 2/34

Outline:

What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display Facilities Flo !ontrol "sing o# M$File

Writing "ser De#ined F%nctions !oncl%sion

Page 3: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 3/34

What is Matlab? Matlab is basically a high le&el lang%age 

hich has many speciali'ed toolboxes #or

maing things easier #or %s

o high?

Assembly

High LevelLanguages such as

C, Pascal etc.

Matlab

Page 4: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 4/34

What are we interested in?

Matlab is too broad #or o%r p%rposes in thisco%rse*

+he #eat%res e are going to re%ire is

Matlab

CommandLine

m-fles

unctions

mat-fles

Commandexecution like !"command #indo#

"e$ies oMatlab

commands

%n&ut!ut&ut

ca&ability

atasto$age

'loading

Page 5: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 5/34

Matlab Screen

!ommand Windo type commands

!%rrent Directory

Vie #olders and m$#iles

Worspace Vie program &ariables

Do%ble clic on a &ariable

  to see it in the Array -ditor 

!ommand istory

&ie past commands

sa&e a hole session

%sing diary

Page 6: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 6/34

 Variables

.o need #or types* i*e*,

 All &ariables are created ith do%ble precision %nlessspeci#ied and they are matrices*

 A#ter these statements, the &ariables are /x/ matrices

ith do%ble precision

int a(double b()oat c(

*xam&le+x(

x/0(

Page 7: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 7/34

 Array, Matrix

a &ector  x = [1 2 5 1]

x =

  1 2 5 1

a matrix x = [1 2 3; 5 1 4; 3 2 -1]

x =

  1 2 3

  5 1 4

  3 2 -1

transpose y = x’ y =

  1

2

  5

1

Page 8: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 8/34

Long Array, Matrix

t =1:10 

t =

  1 2 3 4 5 6 7 8 9 10

k =2:-0.5:-1 

k =

  2 1.5 1 0.5 0 -0.5 -1

B = [1:4; 5:8]

x =

  1 2 3 4

  5 6 7 8

Page 9: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 9/34

Generating Vectors ro!unctions 'eros(M,.) Mx. matrix o# 'eros

ones(M,.) Mx. matrix o# ones

rand(M,.) Mx. matrix o# %ni#ormly

distrib%ted randomn%mbers on (0,/)

x = zeros(1,3x =

0 0 0

x = o!es(1,3x =

1 1 1

x = r"!#(1,3x =

 0.9501 0.2311 0.6068

Page 10: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 10/34

Matrix Index

+he matrix indices begin #rom / (not 0 (as in !)) +he matrix indices m%st be positi&e integer 

1i&en2

 A($3), A(0)

-rror2 ??? S%bscript indices m%st either be real positi&e integers or logicals*

 A(4,3)

-rror2 ??? 5ndex exceeds matrix dimensions*

Page 11: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 11/34

"oncatenation o Matrices

x = [1 2], y = [4 5], z=[ 0 0]

$ = [ x y]

  1 2 4 5

  B = [x ; y]

  1 2

  4 5

  ! 6 7x y 8'9

-rror2

??? -rror %sing 66: &ertcat !A+ arg%ments dimensions are not consistent*

Page 12: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 12/34

O#erators $arith!etic%

; addition$ s%btraction

< m%ltiplication

= di&ision> poer 

complex con@%gate transpose

Page 13: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 13/34

Matrices O#erations

1i&en A and 2

 Addition S%btraction Brod%ct +ranspose

Page 14: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 14/34

O#erators $&le!ent by&le!ent%

*< element$by$element m%ltiplication

*= element$by$element di&ision*> element$by$element poer 

Page 15: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 15/34

'he use o ()* + (&le!ent*O#eration

C6 x>3

-rorr2 ??? -rror %sing 66: mpoer Matrix m%st be s%are*

6x<y

-rorr2

??? -rror %sing 66: mtimes 5nner matrix dimensions m%st agree*

 A 6 7/ 3 8 E / 48 3 /9

  A 6  / 3

  E / 4

  3 $/

y 6 A( ,2)

y6

4 $/

b 6 x *< y

b6

  $

c 6 x * = y

c6

0* 0*E $

d 6 x *>3

d6

/ 4 G

x 6 A(/,2)

x6

  / 3

Page 16: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 16/34

asic 'as-: .lot the unctionsin$x% between /≤x≤4π  !reate an x$array o# /00 samples beteen 0 an

4π*

!alc%late sin(*) o# the x$array

Blot the y$array

 

xlins&ace12,34&i,/225(

ysin1x5(

&lot1y50 10 20 30 40 50 60 70 80 90 100-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Page 17: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 17/34

.lot the unction e0x12sin$x%between /≤x≤4π  !reate an x$array o# /00 samples beteen 0

and 4π*

!alc%late sin(*) o# the x$array

!alc%late e$x=D o# the x$array

M%ltiply the arrays y and y/

xlins&ace12,34&i,/225(

ysin1x5(

y/ex&1-x'65(

y0y4y/(

Page 18: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 18/34

.lot the unction e0x12sin$x%between /≤x≤4π  M%ltiply the arrays y and y/ correctly

Blot the y3$array

y0y.4y/(

&lot1y05

0 10 20 30 40 50 60 70 80 90 100-0.3

-0.2

-0.1

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

Page 19: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 19/34

3is#lay 4acilities

plot(*)

stem(*)

*xam&le+xlins&ace12,34&i,/225(ysin1x5(&lot1y5&lot1x,y5

*xam&le+stem1y5stem1x,y5

0 10 20 30 40 50 60 70 80 90 100-0.3

-0.2

-0.1

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0 10 20 30 40 50 60 70 80 90 100-0.3

-0.2

-0.1

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

Page 20: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 20/34

3is#lay 4acilities

title(*)

xlabel(*)

ylabel(*)

title178his is the sinus unction95

xlabel17x 1secs595

ylabel17sin1x5950 10 20 30 40 50 60 70 80 90 100-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1This is the sinus function

x (secs)

      s        i      n        (      x        )

Page 21: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 21/34

O#erators $relational,logical% 66 -%al to

H6 .ot e%al to

I Strictly smaller 

: Strictly greater 

I6 Smaller than or e%al to

:6 1reater than e%al to J And operator 

 K Or operator 

Page 22: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 22/34

4low "ontrol

i#

#or

hile

brea

L*

Page 23: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 23/34

"ontrol Structures

5# Statement Syntax

i# (!ondition/)

Matlab !ommandselsei# (!ondition3)

Matlab !ommands

elsei# (!ondition)

Matlab !ommandselse

Matlab !ommands

end

"ome ummy *xam&les

i 11a65 : 1b55  "ome Matlab Commands(end

i 1a;65  "ome Matlab Commands(elsei 1b<5

"ome Matlab Commands(end

i 1a;65  "ome Matlab Commands(else

"ome Matlab Commands(end

Page 24: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 24/34

"ontrol Structures

For loop syntax

#or i65ndexArrayMatlab !ommands

end

"ome ummy *xam&les

o$ i/+/22  "ome Matlab Commands(end

o$ =/+6+022  "ome Matlab Commands(end

o$ m/6+-2.0+-0/  "ome Matlab Commands(

end

o$ k>2./ 2.6 -/6 /0 ? [email protected]  "ome Matlab Commands(end

Page 25: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 25/34

"ontrol Structures

While Noop Syntax 

hile (condition)

Matlab !ommands

end

ummy *xam&le

#hile 11a65 : 1b55  "ome Matlab Commands(end

Page 26: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 26/34

5se o M04ile

!lic to createa ne M$File

 -xtension P*mQ A text #ile containing script or #%nction or program to r%n

Page 27: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 27/34

5se o M04ile

5# yo% incl%de P8Q at the

end o# each statement,

res%lt ill not be shon

immediately

Sa&e #ile as Denem430 *m

Page 28: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 28/34

Writing 5ser 3e6ned 4unctions

F%nctions are m$#iles hich can be exec%ted byspeci#ying some inp%ts and s%pply some desired o%tp%ts*

+he code telling the Matlab that an m$#ile is act%ally a

#%nction is

Ro% sho%ld rite this command at the beginning o# the m$

#ile and yo% sho%ld sa&e the m$#ile ith a #ile name same

as the #%nction name

unction out/unctionname1in/5unction out/unctionname1in/,in0,in65unction >out/,out0unctionname1in/,in05

Page 29: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 29/34

Writing 5ser 3e6ned 4unctions

-xamples

Write a #%nction 2 o%t6s%arer (A, ind)

Which taes the s%are o# the inp%t matrix i# the inp%t

indicator is e%al to /

 And taes the element by element s%are o# the inp%t

matrix i# the inp%t indicator is e%al to 3

"ame Bame

Page 30: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 30/34

Writing 5ser 3e6ned 4unctions  Another #%nction hich taes an inp%t array and ret%rns the s%m and prod%ct

o# its elements as o%tp%ts

+he #%nction s%mprod(*) can be called #rom command indo or an m$#ile as

Page 31: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 31/34

7otes: PQ is the neglect sign #or Matlab (e%ai&alent

o# P==Q in !)* Anything a#ter it on the same line

is neglected by Matlab compiler*

Sometimes sloing don the exec%tion is

done deliberately #or obser&ation p%rposes*Ro% can %se the command Ppa%seQ #or this

p%rpose

&ause #ait until any key&ause165 #ait 6 seconds

Page 32: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 32/34

5seul "o!!ands

+he to commands %sed most by Matlab

%sers are

hel& unctionname

looko$ key#o$d

Page 33: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 33/34

8uestions

?

?

?

?

?

Page 34: fisika komputasi matlab

7/23/2019 fisika komputasi matlab

http://slidepdf.com/reader/full/fisika-komputasi-matlab 34/34

'han- 9ou