Date post: | 07-Jul-2018 |
Category: |
Documents |
Upload: | wiratchadakalaokha |
View: | 222 times |
Download: | 0 times |
of 37
8/18/2019 User Defined Functions7
1/37
User-Defined Functions
charturong.ee.engr.tu.ac.th/CN208
! !" #
# $ # % &' ( )'! ' . !
8/18/2019 User Defined Functions7
2/37
2
Introduction
'%! * )$ top-down design )'!$ + , +- , $. $ + !-!/01 2 - ! - 3, 3 & 13%! )$ $ + !-!/ -% & - % 2 ()$
!-!/ - % & 4 2 , $ + , +-% 2 % , )$ top-down design % & )$
MATLAB , 5 *3%! % ) # 3%! 2(function) 3 )'! 3%! 1 2 2#( 1 2 $ + !-! & 5 *3%!# 6 ( 2 -% & 4 2 $ + , +-% 2 '
M-files % 2 '71 3%! & ' - , - 2 -$.script files( (# 2MATLAB $ ! , , 1 2 )$ )
8/18/2019 User Defined Functions7
3/37
3
1. # ' -! -31 & )$ 4 2 (independent testingof sub-tasks)2. # 6 (, '% ! &(reusable code)3. $ 3 5' ' ''%(isolation fro unintended side effects)
8/18/2019 User Defined Functions7
4/37
4
3%! MATLAB 2 3%! $.M-file 8 % 2( )'!%*$ ' - $ % &
function !outarg1" outarg2" #$ % fna e(inarg1" inarg2" #)
& '1 co ent line& t er co ent lines
(*+ecutable code)
#
(end)
8/18/2019 User Defined Functions7
5/37
5
(# 2function , #' ' 2 3 2 , ' (# 2function - 4 2 3 2 3 * % 2, (input) 5 "
3 2(output)
)'! % 2input argu ent list (inarg1" inarg2"
#) $ 9, 4 2 ! .( ) (parent eses) 4 2 32 output argu ent list (outarg1" outarg2" #) $9, 4 2 ! .#% 2 % 2! ! $ (brackets) ' 0! 43
4 2 ! - 6 % output - output '%! -( $. , 4 2 ! . #% 2 % 2!
- 2 6*0 $.. file )'!, 4 2 3 $. 4 2'%! 4 2 2 246 & 4 2 2-M, fun
2 & 6*0 )'!, 4 2 M, fun.
8/18/2019 User Defined Functions7
6/37
6
, input argu ent list 4 2 ( 3 $ % 2 , ,2 & 4 2 3 $ & $. $ 2 4du ,
argu ent 01 2 ,#( - 3* % 2 % 2 26* %! , 4 2 3 $ -% & 6*(' & % 2 MATLAB$ 5 2 & # .# &
, output argu ent list 4 2 ( 3 $ % 2 2 &
,5 " % 2 2 & ( $ 5 01 2 % ( ! input argu ent list, (# 2end 4 2 -$.'# & #'3 2% 2,
& - MATLAB ./ $. $ 01 2 3%! (# 2end 4 22 & 3%! 4 -3%! . ' 3%! (# 2end 4 22 '! 3%! co ent 4 2 -#( 2
8/18/2019 User Defined Functions7
7/37
7
3%! co ent ' , 2 4% 2 %! -'1co ent line & , 4 2 % 2 4'$ # 3 2 &01 2co ent ' % 8 % 2 6* #' 5
4 2 , (# 2lookfor , - (# 2, MATLAB #-co ent ' % 2 4 % 2 '1 co ent line
261 ' - ' 4(# 2% 2( ' 3MATLAB 6*#' )'!, (# 2elp
8/18/2019 User Defined Functions7
8/37
8
1function distance % dist2 (+1" ,1" +2" ,2)& dist2 calculates t e distance between two points& 0unction dist2 calculates t e distance between& two points (+1" ,1) and (+2" ,2) in a artesian
& coordinate s,ste
& alculate distancedistance % s rt((+2-+1). 2 4 (,2-,1). 2)5
end & function distance
8/18/2019 User Defined Functions7
9/37
9
6 4 2 3%! 2dist2 # . , 0 :'' # )'!,
4 2 dist2.6 - (# 23 MATLAB (co and window) # 6 , (# 2lookfor 4elp 4 2 ( ( % 2! 2dist2 01 2MATLAB #' 5 " (# 2% 2%4 2 dist2 ' % &77 lookfor dist2
dist2 calculates t e distance between two points89:T2:T; 8istance con
8/18/2019 User Defined Functions7
10/37
10
dist2
# 6 %! , 2dist2 '# "% 41. , -co and window )'! - ! -'
(/" 1) ' (2" >) . ( ' ' % &77 out % dist2 (/" 1" 2" >)out % 3.?/@?
01 2 .' -5 " 3 2dist2 -( $. , 4 2$ -distance - $. 4 2 $ ,'/ . '
2. 3%! %! , ,script file 4 %! , 2MATLAB 2$
8/18/2019 User Defined Functions7
11/37
11
script file& :cript file test dist2.&& urpose T is progra tests function dist2
& Cet input datadisp( D alculate t e distance between two points D )5a+ % input(D*nter +
8/18/2019 User Defined Functions7
12/37
12
01 25 "3 run 3 script file 3 % 2 4 2 test dist2 %' % &77 test dist2
alculate t e distance between two points*nter +
8/18/2019 User Defined Functions7
13/37
13
23%! 2 4 2 $ & '3 3 ( .
% 2 ( ', ( ', 2%*$ %! , 4fac %find factor(nu ) )'! % 2 nu $. 3 ( . fac $. % 2 % $ 3 3nu $.
#
8/18/2019 User Defined Functions7
14/37
14
function fac % find factor(nu )
& find factor finds all of t e factors of a positi
8/18/2019 User Defined Functions7
15/37
15
!"#!$
)$ MATLAB , % 2 %! - pass-b,-
8/18/2019 User Defined Functions7
16/37
16
! !- 5-- $ pass-b,-.2f &>.2fFnD" a" b)5a % b(1) 4 2Ha5
b % a .H b5out % a 4 b(1)5fprintf( D9n function sa ple a % &>.2f" b % &>.2f &>.2fFnD" a" b)5
# 6 ' 5-- $ )'! 3%! script file 4 2test sa ple. ' % &a % 25 b % !? >$5
fprintf( DBefore function sa ple a % &>.2f" b % &>.2f &>.2fFn D" a" b)5out % sa ple(a" b)5fprintf( DAfter function sa ple a % &>.2f" b % &>.2f &>.2fFn D" a" b)5fprintf( DAfter function sa ple out % &>.2fFn D" out)5
8/18/2019 User Defined Functions7
17/37
17
5 " % 2 ' $ 5 )$test sa ple , - (# 24
77 test%sa&pleBefore function sa ple a % 2.//" b % ?.// >.//
9n function sa ple a % 2.//" b % ?.// >.//9n function sa ple a % 1/.//" b % ?/.// >/.//After function sa ple a % 2.//" b % ?.// >.//After function sa ple out % /.//
01 2 . ' - - 3 $a b 6* $ % 2! !, 2sa ple - $ % 2! $ % & - % 5 - $ ;
8/18/2019 User Defined Functions7
18/37
18
'sorting(
%! 3(sorting) 4 ( 3 & ', < 3* '(' $! 4 ! $ 01 2 %! 3 % & %$ )! !-71 %! 3 !- *$. $ ( 1 (,$ + % &'*$. 4 2 % 2- !
%! !1/ 3 -1 @ I$ ! $ $.!-1 3 @ I 1/$ % &(' !- J3 & %! 3 % 2 ! 4 3 % 2 %- ! % 2#'
3 &@ - 01 2 24 3-1 & 13 % 2 ! % 2#' - $, 3 % 2 4!1/ 3 @ I$ 01 2 4
3 ( !- % & -$ 4 2 ! 23 & '6*%!%! 3 % & %! -selection sort
8/18/2019 User Defined Functions7
19/37
19
)election )ort%! 3 % 2 %! -:election :ort 4 %3 2
-! % 2#' 01 2 %! ! $ (' !- % 2 ' - ,4 &1. 3 % 2 % -! % 2#', (- 3 &
$. #'3 6 )'!# $ % 2! 3 & 3 36 -6 - 3 6 % -!#' !- * -(
2. 3 & -% 2 2 $. $ 4 2 3 % 2%- !% 2#' - $ ( # $ % 2! 3 & 3 % 2# 36 -6 - % 2# 3 6 % -! % 2#' $. % 2# !- *
- (3. ( % & $ 4 2 !/ 24 - 2 #' ! ( # $ % 2! 3# #' !, $.
%! 3
8/18/2019 User Defined Functions7
20/37
20
1/ 3 -1 @ I
-1 3 1/ @ I
#
- #
-1 3 1/ @ I
#-1 3 @ 1/ I
-1 3 @ I 1/
#
8/18/2019 User Defined Functions7
21/37
21
selection%sort'arr(
# 6 3%! pseudocode #( selection sort '' -$ % &
for (eac inde+ position ii in t e arr)find kk (t e inde+ t e s allest
8/18/2019 User Defined Functions7
22/37
22
function out % selection sort(arr)& selection sort sorts data in ascending order
n
8/18/2019 User Defined Functions7
23/37
23
function ind % find s allest nu ber(arr" ii)& find s allest nu ber finds t e inde+ of t e s allest nu ber in arr between ii
& and t e end of t e arr
n
8/18/2019 User Defined Functions7
24/37
24
$u**le )ort
%! 3 bubble sort 2 %! )'! $ %! %!32 , < 3 * # $ % 2! ( - 32 % &,!- * , ( - % 26* & ( $ %! %! 3 % 22
% 23 , < 3 * - $ ( #$ % 2! ( -6( $. ( % & -$ 4 2 !/ 6 & 2$ %! %! 32 , < 3*, - % & -$ 4 2 !/- & -% # $ % 2! '31 & 1$. !
3 bubble sort
8/18/2019 User Defined Functions7
25/37
25
!- %! 3 bubble sort 3 !1/ 3 -1 @ I$ ( '' % &%! % 21
!1/ 3 -1 @ I$ !3 1/ -1 @ I$ ' '2 3!3 1/ -1 @ I$ !3 -1 1/ @ I$ ' ' 2 3 -$!3 -1 1/ @ I$ !3 -1 @ 1/ I$
!3 -1 @ 1/ I $ !3 -1 @ I 1/ $%! % 2 1 % # $ % 2! ( -> &%! % 22
!3 -1 @ I 1/$ !-1 3 @ I 1/$
!-1 3 @ I 1/$ !-1 3 @ I 1/$!-1 3 @ I 1/$ !-1 3 @ I 1/$!-1 3 @ I 1/ $ !-1 3 @ I 1/ $
%! % 2 2 % # $ % 2! ( -1 &
8/18/2019 User Defined Functions7
26/37
26
%! % 23!-1 3 @ I 1/$ !-1 3 @ I 1/$!-1 3 @ I 1/$ !-1 3 @ I 1/$!-1 3 @ I 1/$ !-1 3 @ I 1/$
!-1 3 @ I 1/ $ !-1 3 @ I 1/ $ %! % 2 3 % # $ % 2! ( -0 & ; &%! bubble sort
8/18/2019 User Defined Functions7
27/37
27
*u**le%sort'arr(
# 6 3%! pseudocode #( selection sort '' - $ % &w ile swap occurs
& co pare two ad acent nu bers and swap t eir positions if necessar,for ii % 1 (n
8/18/2019 User Defined Functions7
28/37
28
function out % bubble sort(arr)& bubble sort sorts data in ascending order
swap % 15n
8/18/2019 User Defined Functions7
29/37
29
+ptional !rgu&ents
2MATLAB % , $ % 4 -% . ' (optional input and output argu ents) ! !- - , 2 plot01 2 %$ & -2 $ 61 - $. 01 2MATLAB * ' !- - 2-% & %$ 4 % 2
MATLAB % 2 8#( - ( $ , 2 #' (4 ' - $ % &
6 nargin N , -( 3 $ , 26 nargout N , -( 3 $, 26 nargc k N #' 3 -% 5 ' ')'!) 6 % 26*
%! %( $ 4 4! $6 error N #' 3 - %3 5 ' '(error essage) ! %26 warning N #' ( 4 - , 2( - $6 inputna e -- , 4 2 3 $ ( -% 26*
8/18/2019 User Defined Functions7
30/37
30
1. 2nargin nargout 6* %! , !, 3%!2 01 2, -( $ % 2 26* %! ,
2. 2nargc k # 3 % 2 #' 3 5' '6 2%($ 4 ! $ % *$ , 4essage % nargc k( in args" a+ args" nu args)5
)'! % 2 in args $. ( ! % 2#'3 $ % 2 $. $ ' a+ args$. ( % 2#'3 $ % 2 $. $ ' nu args $. (
$ % 2 % 26* %! , 6 ( $ -!- * !, ( % 2( ' 3 5' ' 6*#' ) , $ essage3. 2error , #' 3 -%3 5' ' ! %!
2 & )'! % %! , 4error(O sgP) )'! % 2 sg 43 2( ', #' 3 5 ' ''31 &
8/18/2019 User Defined Functions7
31/37
31
>. 2warning , #' ( 401 2 4 2 2 ' % 2$ + '31 & -$ -!, 2( -$ )'! % ,4warning(O sgP) )'! % 2 sg $. 3 % 2 #' ( 4@. 2inputna e , #' 4 2 3 $ % 2 26* %!
, %*$ ,4na e % inputna e(argno)5 )'! % 2argno $.( -3 $ -
function ,fun(+" ," Q)na e % inputna e(2)5disp(! DT e second argu ent is na ed D na e$)5
4 2 2% &6* %! , , - (# 2 5 "477 dog % 15 cat % 2577 ,fun(dog" cat)T e second argu ent is na ed cat
8/18/2019 User Defined Functions7
32/37
32
optional argu&entsfunction ! ag" angle$ % polar
8/18/2019 User Defined Functions7
33/37
33
& eck for (/"/) input argu ents" and print out a warning essageif + %% / R , %% /
sg2 % DBot + and , are Qero angle is eaningless.D5warning( sg2)5
end
& Sow calculate t e agnitude.
ag % s rt(+. 2 4 ,. 2)5
& 9f t e second output is present" calculate angle in degrees.if nargout %% 2
angle % atan2(," +) H 1I/Gpi5end
end & function polar
8/18/2019 User Defined Functions7
34/37
34
polar%,alue
%! , 2 polar
8/18/2019 User Defined Functions7
35/37
35
2. '# ,(# 2warning 4 2 + % / , % /77 ! ag angle$ % polar
8/18/2019 User Defined Functions7
36/37
36
3. '# , 2 polar
8/18/2019 User Defined Functions7
37/37
37
>. '# , 2 polar