Sei sulla pagina 1di 677

Matlab - !

"#$ %$&"%%
'&!$

..##1*"!% .##"-, .#'%/#0 $%&#'( )*$%#+! )#,+)+ !"-%) !"#! MATLAB


.6 85-+% ,)#25#"#( !2'6(5' )%#$' 2%)7( #*- ,.#$'/! )%"/ 4%1' ,!"-%)! !')-" 2%3+'
.#(- :8%6- )%71%" )%'2 )%#&7%5 !"-%)! )((%- .%#- .MATrix-LABoratory - !+/ )%$+/+
*%'#$ )2/75+ !"-%)! .*%$% ,)%9%20+ ,.#+%"#(%7 ,.#"%)" *%'#$ ,)%#(5#&"27#* )%5%%/+' (%7#,(
.)%#&+#"5% .#*++ )() ,.#*++-%* .#726 )2#&# ;!!%'6 )#(5%9#%% !+2' )%5&%) )6&!% .#"%)"
!"-%)( 4#1%!( 8)#" .*%5+ )('6%+ .##(%'+#1 .#'%/#0 $%&#'( )%2/75!% ,#2+%" !"-%)! #7%5
,)%)%5 *%'#$ ,!23' 8%6- .#"%/ .#5/%"' )%*%37! 2&%5 (/ !'02! .#%%!+! TOOL-BOXES
.*%$% )%#3(0 )%#(5#&"27#* )%5%%/+
(#0)) !9 8%(0' '%)-"/ !2%/ (- .MATLAB -! (/ )%*%37! 8%(0 0)7" !"-%)( !1#"-! .$
%2+/# !9 :1+' 2#*6"/ .#")/+!% )%(%$7! (- .()#,+%,%5 8+1+ MATLAB -!) >> 8+#1'
"/27+" (/ !,#/' *'%$ MATLAB -! .(WORKSPACE -!) MATLAB-! (/ !*%'$! '02+'
%5 !*%37! )5 $&'( MATLAB -( .%26) RETURN /3+ ($ !&#0( 8-( ,(INTERPRETER)
()#"-%)) )%*%37 (/ !-%25 !2*#1 $&'( .#"#"%$+ 2/5- .!"%205! !2%/' %')-"/ )%*%37! 4&2
)126 .ASCII ,+2%7' 2+%/! :2%$ (-' /+)/!( 8)#" .#"%&#0 (editor) :2%$' !)%5 '%)-( ;(+%+
.#+#"7 :2%$ )((%-! ,PC )126 !"#! .#*'%$ %"5 !' MATLAB
)%-#2& )%#"-%)! PC '/0+' .#*'%$ 2/5- . filename.m )+%#1 .$ 2%+/( /# )%#"-%)! )5
8)#" .205 /%7#0 (%(1+ MATLAB -( 2#*6!( %5 MATLAB !"-%)! )5&+" !' !#271' 2+/#!(
.DOS !($7!! )-2$+' %+- dir-% cd )%*%37' /+)/!(
:)%5'! )%#%2/75!+ )05 #"$ )$&%'+ MATLAB-' )#"-%) )&2!
F5 /3+ ($ !&#0( .1
'(,+ (/ :2%$! 8%(0' .#5)+! icon -! ($ !&#0( .2
.(m )+%#1! 5(() )%*%37! 8%(0' )#"-%)! ;'%3 ./ )'#)- .3

1
(%)!* +&,%- .&/&*0
File Management Functions
cd Show present working directory or folder
p=cd Return present working directory in p
cd path Change to directory or folder given by path
chdir Same as cd
chdir path Same as cd path
delet test delete the M-file test.m
dir List all files in the cornet directory or folder
ls Same as dir
matlabroot Return directory path to MATLAB executable program
path Display or modify MATLAB’s search path
pwd same as cd
type test Display the M-file test.m in the Command window
what Return a listing of all M-file test.m and MAT-files in the
current directory or folder
which test Display the directory path to test.m

.!*%'$ )#271 #%"#/ - cd


.)#0-%" !#271' .#&'3 )+#/2 - ls %5 dir
.m - #&'3 (/ /%7#0 '#," - path
./%7#0 '#," #%"#/ - cd path
.;'%3 )3#0+ - delet f-name
.:1+! ($ ;'%3 )6&! - type f-name

.&%1%1! .&+&20

Operation Symbol Example


addition, a + b + 5+3
subtraction, a - b - 23 - 12
multiplication, a ! b * 3.14 * 0.85
division, a " b / or \ 56 / 8 = 8 \ 56
power, a b ^ 5^2

2
.\ #(5+/ 3%(#0% / #"+# 3%(#0 :.#(#$7 3%(#0 #"+#1 #"/
)5 )%"/( 8)#" .2%1#0 %5 2%'#0 ,3%(#0 %5 (7- ,)%390 :5'! 2*1' 8#+#( (5+/+ $&%'# '%/#0!
..#"%/52 %'/%0# 2)%#' .##+#"7! .##26%1! ..##26%1' /%+#/ #"$ 2*1!
:5+6%*( .#0%%2' '/0)+ %"#5 MATLAB -!
» 2*4+2 * 5
ans =
18
.ans (*0+! )2#2' !")/+' /+)/# MATLAB -! !5&%)! 2%'$ !")/+ 2*6%! 5( .5
:.#")/+ )2*6! #"$ !+%* '%/#0 $&'( 2/75
» a=4,b=5;
a=
4
» c=2*a+2*b
c=
18
/%+#/ .')-" 5( b (/ %-2$ )59 )+%$( ,:1+! ($ a (/ %-2$ ')-" !"%/52! !2%/! $%&#' #205
.(8%2-#9' )2+/" !5&%)! !17*! 5(( .6 ,!23+ (-') :1+! ($ !5&%)! (/ !17*! $"%+ ; )+%#1'

..5)!' !")/# c .5! 3%*'"% a (/ %-2$ )5 !"/"


» a=10;
»c
c=
18

.'%/#0! )(%$7 )5 /*0+ $&'( :#2& c (/ %-2$ )5 8-*$( #*-


» c=2*a+2*b
c=
30
)%#)%5 271+ /#3" .5 . # $ .#&0' /%+#/ #"$ )%*%37! 8%(0' !')-" 2'-/ !2%/ 290/( 2/75
.!*%37! )5 )#*##+ ('3" $ -% !')-" 2'-/ !*%37 (/ )%"%/52

3
(%301$ +" ,4), 5$3&0
#205 )%271 4 5#! .#/++ .#271+ 2%'$ (*0+! )2#2' .)#"%2/$ `3" 5(( %')-# .#+(/ .#271+
.)#"%2/$! `3"!
.!6&!! )2%& )$#'3( )%/+/+ )%5'! )%*%37!
MATLAB Command Comment Example: 215 / 6
format short default display 35.8333
format long 16 digits 35.83333333333334
format short e 5 digits + exponent 35.833e+01
format long e 16 digits + exponent 35.83333333333334e+01
format hex hexadecimal 4041eaaaaaaaaaab
format bank 2 decimal digits 35.83
format + positive negative or zero +
format rat rational approximation 215 / 6

..)6&! 87%5 )5 32 5(5 .#")/+! :2$ )5 !"/+ %"#5 .#%1+ ,+2%7' /%+#/

(%-."$
MATLAB -! .)%5 )%#!( '##0 8%/52! %)! 2/5- ,.#%%) 19 *$ )05 !(++ '-2%# !")/+ (/ ./
.)%",3 )%#)%5' ')-#!( )%'##0 )%*%37 .)%",3% )%(%*6 )%#)%5 8#' 8#0'+
:.#5'! .#")/+( )")#" )*0%#+ )%$+/+

Special variables Value


ans The default variable name used for results
pi The ratio of circumference of a circle to its diameter
eps The smallest number such that when added to one
creates a number grater than one on the computer
flops Count of floating point operations
inf Which stands for infinity, e.g., 1/0
NaN Which stands for Not-a-Number, e.g., 0/0
I (end) j I = j = %1
relmin The smallest usable positive real number
relmax The largest usable positive real number
nargin Number of function input arguments used
nargout Number of function output arguments used

.'/0+! )2#&$( 5(% inf !5&%)( .%26) -0' !3%(0 , 4%1"#5 = inf
.)%$+/+ ($' 5%!/ 2)%#' 8,3! 271+! = eps
."3#2" ,271+ .##3 5( = NaN

4
:)%*%37' /%+#/ #"$ ('3" WORKSPACE -' .#2*6%+! .#")/+! ($ !#&+2%7"#5
» who
Your variables are:
a b c

:,2%7+ 2)%# %5
» whos
Name Size Bytes Class
a 1x1 8 double array
b 1x1 8 double array
c 1x1 8 double array
Grand total is 3 elements using 24 bytesGrand total is 3 elements using 24 bytes
:WORKSPACE -! :%)+ .#")/+ )3#0+( )%/+/+ )%5'! )%*%37!
» clear all
%5
» clear a b . . .

.&32,& *&1%0 /&*%-

.!5&%)! )6&! )5 $"%+ ; -' /%+#/ .)%*%37 8#' !*27!( .#/+/+ "," -% ";" .#"+#1!
.% -( 8#+#+ ')-##/ !+ (-+ .($)# MATLAB -! ,)%2$! )'#)-( /+/+ "%" 8+#1!
:!5'! !2%/' !-#/++ !*%37!/ MATLAB -( 2+%5 ". . ." 8+#1!
» 4*...
5
ans =
20
)%+/ #)/( !")/+ (/ ./ *#27!( .#&%2% !23+') 0%%2 #*# ($ !")/+ (/ ./ $%,3( 8)#" 5(
.first_name) 5+6%*( 8%)0) %3 #*# ($ )59 )%/$( ;(+%+

5
.&)%35$

,'-2%+ %5 #/++ – #271+ 8%)" (- .MATLAB-' )#1#1'! .#"%)"! 8%625 )*#0# !"#! !&#2,+
(/ #,27 !23+ %"! 2%,3% .#*++-%* :2$+ #*#-($ )6&%#+ !&#2,+ .!&#2,+ #*#-($ !/$+( 6&%#+
.)#*++-*0 !&#2,+
($ 2%90( #('+ ,.#2'#5 (/ !(%*6 !&%'3 ($ )%#,+)+ )%(%$7 $%&#' 2/75+ )%&#2,+' /%+#/
.2'#5 (- 2%'$ !(%$7!
8+%1# )%2%/ 8#' 2'$+ .[ . . . ] .#$'%2+ .##26%1' /%+#/ #"$ !/$) MATLAB-' !&#2,+ )2*6!
.","-' %5 0%%2' /+)/!( 8)#" ,!2%/' .#2'#5 8#' !*27! :2%&( .";"-'

,$4&/+ !
(- 2%'$ '/0( 8)#" 5(/ 8%%#-+ . 0 ' X ' & :.#-2$! 2%'$ y = sin(x) #%,#'! )5 '/0( .#&%2
:&0.1 (/ .#$%'3 .#0%%2+' !#&3"%7! )5 .#+6%* ,.%0)' !*%3"
» x=[0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1]*pi
x =
Columns 1 through 7
0 0.3142 0.6283 0.9425 1.2566 1.5708 1.8850
Columns 8 through 11
2.1991 2.5133 2.8274 3.1416

» y=sin(x)
y =
Columns 1 through 7
0 0.3090 0.5878 0.8090 0.9511 1.0000 0.9511
Columns 8 through 11
0.8090 0.5878 0.3090 0.0000

:.##+)#26%( %5 .##25#"#( .##*++-*0 .#-2$+ )2#&#( )%/+/+ )%5'! )%*%37!

x = [2 2 ( pi sqrt(2) 2-3j] .#"%/ .#2'#5 (#-+! !2%/ 2%,3% )2#&#


x = first : last ,first :2$' .#(#0)+ %#2'#5 2/5 ,!2%/ 2%,3% )2#&#
:2$' %5 last :2$' .#+##)1+% ,1 (/ )%$#17' .#+*3)+
.!,+(+ 2)%#' %#(5 '%23!
x = first : increment : last ,first :2$' .#(#0)+ %#2'#5 2/5 ,!2%/ 2%,3% )2#&#
last :2$' .#+##)1+% ,increment (/ )%$#17' .#+*3)+
.!,+(+ 2)%#' %#(5 '%23! :2$' %5
x = linspace ( first ,last ,n ) .#(#0)+ %#2'#5 2/5 ,.#2'#5 n ($' !2%/ 2%,3% )2#&#
.last :2$' .#+##)1+% ,first :2$'
x = logspace ( first ,last ,n ) .#(#0)+ %#2'#5 2/5 ,.#2'#5 n ($' !2%/ 2%,3% )2#&#
.#+)#26%( 0%%#2 ($' ,10last :2$' .#+##)1+% 10first :2$'

6
,$4&/+ !
::- .6 )%"'( 2/75 )+*%3! 5+6%*' $#7%!/ x 2%,3%! )5
» x=[0:0.1:1]*pi;
%5
» x=[linspace(0,1,11)]*pi;

)2*6! 8+9' %)%5 %"2*6!/ #7- ,!&#2,+! ./ 8%#& #"$ !/$) !&#2,+ :%)' .#%1+ 2'#5( !#"7
)5 – #"/! 2'#5!% !2%/! 271+ )5 8##&+ 8%/52! 271+! .2'#5 %)%5 (/ 13*"#5!% ,!&#2,+!
'%)-" .5 .":" 8+#1! #*#-($ $&')) ,!2%/' %5 2%,' ,.#7#&2 .#2'#5 271+( !##"7 .!*%+$! 271+
.!*%+$ / !2%/! #2'#5 (-( )10##)+ !##"7! ,%#*#&+ .#271+ 5(( ":" 8+#1! )5
,$4&/+ !
:8-( .*%3 %"2*6!/ ,2%,3%' /+)/" .5
» x(3)
ans =
0.6283
» x(1:5)
ans =
0 0.3142 0.6283 0.9425 1.2566
» x(3:-1:1)
ans =
0.6283 0.3142 0

.)%+##3 )%&#2,+-))+ !(%*6 !&#2,+ '#-2!( 8)#"


,$4&/+ !
» a=1:5,b=1:2:9
a =
1 2 3 4 5
b =
1 3 5 7 9

» c=[a b]
c =
1 2 3 4 5 1 3 5 7
9

:";" 8+#1! #"$ ,8#%& 2'-/ #7- , )%2%/! %*27%# #*++ -%* :2$+ .#2#*6+ 2/5-
» A=[1 2 3;4 5 6]

A =
1 2 3
4 5 6

7
:!*%+$ 2%,3%% )2*6!
» a=[1; 2 ;3 ];
a =
1
2
3

:(tranpose %)%$+/+/) "'" 8+#1' /%+#/ #"$ %5


» a=[1 2 3 4].'
a =
1
2
3
4

.*%+&( %-7!# .#'-%2+ .#271+ ,/26! #"7( !*%3" 5(( transpose 8+#1' /+)/" .5
,$4&/+ !
» c=a+a*i
c =
1.0000 + 1.0000i
2.0000 + 2.0000i
3.0000 + 3.0000i
4.0000 + 4.0000i
» c'
ans =
1.0000 -1.0000i 2.0000 -2.0000i 3.0000 -3.0000i
4.0000 -4.0000i

.#-2$+! (5 .#10##)+ 5( 2/5- ,.#-2$+ 8#' )%(%$7 8##75+ MATLAB-' "." 8+#1! #((- 87%5'
)#"%'/0! !(%$7! 2+%(- ,()#25#"#( !2'6(5+ %"( )2-%+! )%10##)!! :2*') )%&#2,+ (5-
..#+5%) .#2'#5 )%6%9 ($ )$&%'+
,$4&/+ !
» a=[1 2];b=[3 4];
» c=a.*b
c =
3 8

#*++ '##0+% )#25#"#( !2'6(5' %+- )%&#2,+ )(7-! 6&##+ !*%3" 5(( (7- 8+#1' /%+#/
:.#+5%) )%&#2,+
» c=a*b'
c =
11

8
,)%35$+ 3+*1 6%! .&+&20

:!&#2,+! #2'5+ *05 (- ($ !!9 !(%$7 $&%') !&#2,+% 2(31 8#' )#"%'/0 !(%$7 .#$&'+ 2/5-
» a=[1 2;3 4]
a =
1 2
3 4
» a+2
ans =
3 4
5 6
» 2*a-1
ans =
1 3
5 7

.&)%35$ 6%! .&+&20


:$&'( 2/75 )%&#2,+ 8#' ..#!9 .#*++ )%&#2,+( 2/5- 32% :5 )#2/75 )%&#2,+ 8#' !(%$7
$&%') !(%$7! , !&#2,+! ./ #205 !*%3" 8+1" .5/ %"#52 2'- .!390% 3%(#0 (7- 2%1#0 2%'#0
.!!9 13*"#5 #($' .#2'#5 8#'
,$4&/+ !
» a=[1 2;3 4];
» b=[1 1;2 2];
» a+b
ans =
2 3
5 6
» a.*b
ans =
1 2
6 8
» a./b % ( = b.\a )
ans =
1.0000 2.0000
1.5000 2.0000
» a.^b
ans =
1 2
9 16
» a.^(-1)
ans =
1.0000 0.5000
0.3333 0.2500

9
:)%&#2,+ 8#' )%(%$7 )+-1+ !5'! !(',!
a = [a1 a2 . . . an] , b = [b1 b2 . . . bn]
c = <a scalar>
a + c = [a1+c a2+c . . . an+c] ,)%35$& 3+*1 3&!%#
a ( c = [a1 ( c a2 ( c . . . an ( c] ,)%35$! 3+*1 .+07,
a + b = [a1+b1 a2+b2 . . . an+bn] .&)%35$ 3&!%#
a . ( b = [a1 ( b1 a2 ( b2 . . . an ( bn] .&)%35$ %3!%' .+07,
a . / b = [a1/b1 a2/b2 . . . an/bn] %-$% *&+%#
a . \ b = [a1\b1 a2\b2 . . . an\bn] %+'$" *&+%#
a . ^c = [a1^c a2^c . . . an^c] ,*8#! .&+&20
c . ^a = [c^a1 c^a2 . . . c^an]
a . ^b = [a1^b1 a2^b2 . . . an^bn]

".&$7#" .&+&20
::2$+ :%)' 2'#5 )7(0!
» a=[1 2;3 4]
a =
1 2
3 4
» a(2,2)=5
a =
1 2
3 5
» a(3,3)=6
a =
1 2 0
3 5 0
0 0 6

($' 2'#5 .3+( %"/2*/- ,!9 !23+' 8-( :2$+! 6%1 %5 (*%6 #'6( )%2!&! /2%* 5( MATLAB
..#175 %' %71%"% (*6%! :2$+! 2*2 :2$+' (3,3) 13*"#5

.&)%35$-..& .&)%35$
:)205 !&#2,+ :%)+ !&#2,+ )2*6!
» a=[1 2 3;4 5 6;7 8 9]
a =
1 2 3
4 5 6
7 8 9
» b=a(1:2,2:3)
b =
2 3
5 6

10
:)%205 )%&#2,+' /%+#/ #*#-($ !&#2,+ )'-2!
» a=[1 2;4 5]
a =
1 2
4 5
» b=[3 6]'
b =
3
6
» c=[a b;7 8 9]
c =
1 2 3
4 5 6
7 8 9

:!3#2 !&#2,+' /%+#/ - !*%+$ %5 !2%/ )3#0+


» c(:,2)=[]
c =
1 3
4 6
7 9

:.#+##3! .#13*"#5! (- )5 6&#+ 13*"#5 .%3+' (:) 8+#1' /%+#/


» c(:)’
ans =
1 4 7 3 6 9

:!&#2,+ )2#&#( 2%,3% (%7-/


» c
c =
1 3
4 6
7 9
» d=c(:,[2 2 2 2])
d =
3 3 3 3
6 6 6 6
9 9 9 9
(-' ,!%%/ !*%+$ 13*"#5 #($' .#2'#5! (-( 10##)+ ,":" - .#(%6$! .##26%1' 8%/52! 2'#5!
2'#5! ,(""! !+6%*' .!*%+$! 13*"#5 )5 2#*6+ #"/! 2'#5! .!*%+$ 2%,3% !/$+( – )%2%/!
.2%,3%' 2'#5 (- 2%'$ !*%+$ 2%,3% )##"' :#(!) $&')# :-#7( .2%,3% 5%! #"/!

11
)5 27%1 MATLAB-! .*05 13*"#5' /%+#/ #"$ #*++-%* :2$+( :##/! 2'#5 (5 )%"7( 2/75
.!,+( !($++ ,)%*%+$! 2*1 #7( ,!&#2,+! #2'#5
» a=[1 2 3 4;5 6 7 8]
a =
1 2 3 4
5 6 7 8
» a(5)
ans =
3

*05% (false) 23/ 6&##+ 175 ..##6%( .#-2$+ .#523" *05% 175 .#2'#5! )5 .#(#-+! .#-2$+
.(true) )+5 6&#+
,$4&/+ !
» x=-3:3
x =
-3 -2 -1 0 1 2 3
» abs(x)>1
ans =
1 1 0 0 0 1 1
» y=x(abs(x)>1)
y =
-3 -2 2 3
.#+##3+! .#2'#5( 5#! )%10##)!! ,#"5#(%' #5") #*#-($ )6&%#+ !&#2,+' .#2'#5( !#"7! 2/5-
.#5")! )5

.#13*"#5' /%+#/! )5 )+-1+ !5'! !(',!

.c !*%+$'% r !2%/' 5&+"! A !&#2,+( :##/! 2'#5( !##"7 A(r,c)


)%*%+$! (-' .#5&+" 2/5 A !&#2,+( .#-##/! .#2'#5! (-( !##"7 A(r,:)
.r !2%/'/
)%2%/! (-' .#5&+" 2/5 A !&#2,+( .#-##/! .#2'#5! (-( !##"7 A(:,c)
.c !*%+$'/
2%,3%%- .#2*%1+ .#2'#5! 2/5- A !&#2,+( .#-##/! .#2'#5! (-( !"%7 A(:)
.2*1! #7( !*%+$ !*%+$ !9 2%,3%%( .#03(" .#2'#5! .!*%+$
.(#$( 2'1%+/ #7- !*%+$ 2%,3%%- )2*%1+! A !&#2,+ (/ i 2'#5( !##"7 A(i)

/%7#0 )%*%37
Return indices of the x where its elements are nonzero i=find(x)
Return row and column indices of the array x where its elements are [r,c]=find(x)
nonzero

1 -+ (%*6 ,(0%+! .-2$/ .#271+! (-+ )'-2%+! !&#2,+ !"'" )+*%3! 5+6%*' %+- ,$4&/+ !
:/%7#0 )*%37' /+)/" .3 *$ (3-) :.%0)'

12
» x=-3:3
x =
-3 -2 -1 0 1 2 3
» k=find(abs(x)>1)
k =
1 2 6 7
» y=x(k)
y =
-3 -2 2 3

.&01&- ,)%35$ .&/&*0

.!,+( %5 !($+( !&#2,+ '%'#1 - flipud(A)


.!(5+/ %5 !"#+# !&#2,+ '%'#1 - fliplr(A)
.(.#2'5 m*n (#-!( )'##0 A) A !&#2,+! (/ .#*++! #%"#/ - reshape(A,m,n)
.!(5+/ %5 !"#+# )%($+ 90 (/ )%(%7-' !&#2,+ '%'#1 - rot90(A)

,$4&/+ !
» a=[1 2 3;4 5 6]
a =
1 2 3
4 5 6
» b=rot90(a)
b =
3 6
2 5
1 4

.A !&#2,+! (/ #/52! 8%1-(5!+ !*%+$ 2%,3%% !"%' - diag(A)


.#/52! 8%1-(5! )5 '#-2# V 2%,3%%! ,)#"%1-(5 !&#2,+ !"%' - diag(v)

,$4&/+ !
:!*#0# )&#2,+ )%"'( #*- diag !*%37' /%+#/ !/$"
» v=[1 1];
» I=diag(v)
I =
1 0
0 1

13
(%%/$$-!3 (%732$

!126! .*'(' .##*++ %*% .##*++ *0 .#-2$+' /%+#/ %2/75 MATLAB - (/ )%+*%3 )%5126
- + (%*6 .#*++ '1+ #($' .#-2$+) .##*++ '2 .#-2$+' /%+#/ )2/75+ ,MATLAB - 5 ,)#0-%"!
.(2
,2%15 )%&#2,+ .$ *%'$( )%*$%#+! )#25#"#( !2'6(5 )%*%37' .#*++-'2 .#-2$+' /%+#/
..#*++-%* .#-2$+-))( #*++-'2! :2$+! )5 327( 2/75 !9 !23+'
#2'5+ *05 (- 8#'% 2(31! 8#' !!9 !(%$7 $&%') #*++-'2 :2$+% 2(31 8#' !(%$7 ($7%) .5
.:2$+!
:.#-2* /%(/' )#2/75 #*++-'2 :2$+ )2#&#
..#13*"#5' /%+#/ #"$ "
.(.#*0%#+ .#-2$+) MATLAB )%*%37' /%+#/ "
(#*++-'2 :2$+ )'-2!) cat !*%37' /%+#/ "
..#*++ %* .#-2$+ !/%(/+ )%'-/ /%(/ ($' #*++ )() :2$+ )2#&# - 5+6%*

» a=[1 0;0 1];


» b=[2 2;2 2];
» c=[0 3;3 0];
!"#$$ %&'(&

» d=cat(3,a,b,c)
d(:,:,1) =
1 0
0 3
0 1
3 0
d(:,:,2) = 2 2
2 2 2 2
2 2 1 0
d(:,:,3) = 0 1 d (2,2,1)

0 3
3 0
» size(d)
ans =
2 2 3

14
» A=[1 0 4;3 5 6;9 8 7]; :.#13*"#5' /%+#/
» A(:,:,2)=[1 0 4;3 5 6;9 8 7]
A(:,:,1) =
1 0 4
3 5 6
9 8 7
A(:,:,2) =
1 0 4
3 5 6
9 8 7
» max(A) .#2%,3%% ($ )%($%7! )%*%37
ans(:,:,1) =
9 8 7
ans(:,:,2) =
9 8 7
» 5*ones(2,2,2) :.#*0%#+ .#-2$+ )%2&%#! )%*%37
ans(:,:,1) =
5 5
5 5
ans(:,:,2) =
5 5
5 5
» size(A) :.##*++-'2 .#-2$+ ($ !#&+2%7"#5 )('3
ans =
3 3 2
» ndims(A) #$$
ans =
3
» whos
Name Size Bytes Class
A 3x3x2 144 double array

15
help – ,382

:,#27)! :2* )/2( )%2'0)! #*# ($ 5#! !29$ )('3( 2)%#' ),2%7+!% !0%"! :2*!
Help Desk (HTML)
:5+6%*( ,!*%37! ./% help )/3! #"$ !*%37% !*%37 (- ($ 2'1! ('3( 8)#" 8- %+-
» help sqrt

SQRT Square root.


SQRT(X) is the square root of the elements of X. Complex
results are produced if X is not positive.

See also SQRTM.

.help topics - .#5/%" )+#/2 )6&!( .%26) !+&$(/- help !*%37!


.5/%" %)%5' )%2%/3! )%*%37! )6&! - help topic
.2/3! #7( )%*%37 )+#/2 - lookfor keyword
» lookfor complex
CONJ Complex conjugate.
IMAG Complex imaginary part.
REAL Complex real part.
.....
.....

(%!7&3$ (%301$

)%&#2,+ .! MATLAB -' .#")/+ #((- 87%5' ,!")/+! 6%1 )5 $%'3( :2%& 8#5 MATLAB -'
.#/++ 2(31 )%#!( (%-# #,27 !23+ ..#'-%2+ .#2'#5 )%(#-+!
. j %5 i )%#)%5' .##)1# '-%2+ 271+ (/ !+%*+! :2$!
» c1=1+2i
c1 =
1.0000 + 2.0000i
» c2=1+sin(pi/2)*i
c2 =
1.0000 + 1.0000i

:)#9,23 %5 )#25(%7 !2%&' )#2/75 .#'-%2+ .#271+ )6&!

16
M ), = M ! e j , * a + b i

M = a 2 + b2 , * tan %1 (b / a )
a * Mcos, b * Msin,
:)%5'! )%*%37' /%+#/ #"$ 2'$+! $&%'# MATLAB -'
» M=abs(c1)
M=
2.2361
» teta=angle(c1)
teta =
1.1071
» a=real(c1)
a=
1
» b=imag(c1)
b=
2

..#"5#*2' )%6&%#+ MATLAB -' )%#%%9


.,(0%+ :2$ '%/#0( .6 )/+/+ abs !*%37!

.&01&- .&/&*0
.'-%2+ 271+ (/ *%+&! conj(z)
.)#9,23 !2%&' 25%)+! '-%2+ 271+ )#2(%7 !2%&' 6#&+ [teta,r]=cart2pol(x,y)
.)#2(%7 !2%&' 25%)+! '-%2+ 271+ )#9,23 !2%&' 6#&+ [x,y]=pol2cart(theta,r)

17
.&%5$.$ .&%)*-&0
Mathematical Functions
abs(x) Absolute value or magnitude of complex number
acos(x) Inverse cosine
acosh(x) Inverse hyperbolic cosine
angle(x) Four quadrant angle of complex
asin(x) Inverse sine
asinh(x) Inverse hyperbolic sine
atan(x) Inverse tangent
atan2(x,y) Four quadrant inverse tangent
atanh(x) Inverse hyperbolic tangent
ceil(x) Round toward plus infinity
conj(x) Complex conjugate
cos(x) Cosine
cosh(x) Hyperbolic cosine
exp(x) Exponential: e x
fix(x) round toward zero
floor(x) round toward minus infinity
gcd(x,y) Greatest common divisor of integer x end y
imag(x) Complex imaginary part
lcm(x,y) Least common multiple of integer x end y
log(x) Natural logarithm
log10(x) Common logarithm
real(x) Complex real part
rem(x,y) Remainder after division
rem(x,y) gives the remainder of x / y
round(x) Round toward nearest integer
sign(x) Signum function: return sign of argument e.g.,
sign(1.2)=1, sign(-23.4)=-1, sign(0)=0
sin(x) Sine
sinh(x) Hyperbolic sine
sqrt(x) Square root
tan(x) Tangent
tanh(x) Hyperbolic tangent

:.&'$4&/
» y=sqrt(3^2+4^2)
y=
5
» y=rem(23,4) 23/4=5 (3)
y=
3

18
. x / y 3%(#0 #205 )#25/ - rem(x,y)

» x=-2.6, y1=round(x), y2=fix(x)


x=
-2.6000
y1 =
-3
y2 =
-2

.2)%#' '%23! .(/! 271+! #7(- (6$+ - round(x)


.175 #7(- (6$+ - fix(x)

.%3'%-%+ ,3!4+'& .&)%35$ +2 .&+&20

)%&%7"! )%#$'!+ )05 .)#25#"#( !2'6(5% )%&#2,+ #'%/#0 ,/7( #*- MATLAB !')-" 2%3+'
-' %9 !#$' .#2)%7 *&#- !52" .)#25#"#( )%5%%/+ )-2$+ (/ 8%2)7 5#! )#25#"#( !2'6(5'
.MATLAB
:)%5%%/+ )-2$+ !"%)"
x1 + 2 ! x2 + 3! x3 * 366
4 ! x1 + 5! x2 + 6 ! x3 * 804
7 ! x1 + 8! x2 * 351
:)#"%#&#2,+! !)2%&'%

21 2 3/ 2 x 1 / 2366/
04 5 6- ( 0 x - * 0804-
0 - 0 2- 0 -
017 8 0-. 01 x 3 -. 01351-.

A(x = b
)(7-+( *%6#"') )#25#"#( !2'6(5+ .#2#-+/ #7- ,)%&#2,+ )(7-+ )6&##+ !9 !23+' (7-! )(%$7
.(" .* " 8+#1' MATLAB -' )"+%1+! .#-2$+

5#! A !&#2,+! .5 *#0# 8%2)7 .##3 . x = A ( b #%,#'! #"$ 8%)" )%5%%/+! )-2$+( 8%2)7!
-1

.!-#7! !&#2,+
.()#2(%6"#1 5( !&#2,+) !-#7! A !&#2,+! .5! MATLAB -! )29$' 3%*'"

19
A ,)%35$ +" ,5--%$35/ - det(A)
.!-#7! !&#2,+ 5#! 175+ !"%/ !,""#+2,* )($' )#$%'#2 !&#2,+
» A=[1 2 3;4 5 6;7 8 0]
A =
1 2 3
4 5 6
7 8 0
» det(A)
ans =
27

A ,)%35$, .43/ – rank(A)


.!-#7! !&#2,+ 5#! !5(+ !62* )($' )#$%'#2 !&#2,+
» rank(A)
ans =
3

:.#-2* #)/' MATLAB -' '/0( 2/75 )%5%%/+! )-2$+( 8%2)7! )5


A ,)%35$+ .%70&,, ,)%35$, – inv(A) .1
» A=[1 2 3;4 5 6;7 8 0];
» b=[366;804;351];

» x=inv(A)*b
x =
25.0000
22.0000
99.0000

!)%$+/+% )#25#"#( !2'6(5' )+##3 !"#5 %9- !(%$7 ,)%&#2,+ )3%(0 )((%- !#"/! :2*! .2
.#-7%!' (7- 5#! MATLAB -'
:)#"+# !3%(0( )#(5+/ !3%(0 8#' )%&#2,+ )3%(0' .##3! (*'!( '( .#/( /# #
%%) %*"+ %+,'-% )#(5+/ !3%(0 <= A\b=inv(A)*b
)#"+# !3%(0 <= b/A=b*inv(A)
:,$4&/+ !

» x=A\b
x =
25.0000
22.0000
99.0000

20
2)%# 8-(% )%#,+)+ )%(%$7 )%07 ($ 11%'+ 5%! ,#"/! 8%2)7! 5%! MATLAB -' ;(+%+! 8%2)7!
.2#!+
A\b !3%(0! !9 !23+' .)%"%2)7 4%1"#5 .#+##3 .#+($"! 271++ 8,3 )%5%%/+! 271+ 2/5-
..#175 (/ #(+#13+ 271+ %'/ 8%2)7! )5 8)#)

.&/#&%$ .&)%35$

» a=[1 2 3;4 5 6]; %."/ %,"/0$


» b=find(a > 10)
b =
[]

.175 .#*++ )($' !&#2,+- MATLAB -' )2*6%+ !3#2 !&#2,+


. WORKSPACE -' .##3% !")/+ 5%! (b !9 !23+') !&#2,+! (/ ./!

» zeros(3) !"12+ (& %,"/0$


ans =
0 0 0
0 0 0
0 0 0

» ones(2,4)
!"#3+ (& %,"/0$
ans =
1 1 1 1
1 1 1 1

» ones(3)*pi
ans =
3.1416 3.1416 3.1416
3.1416 3.1416 3.1416
3.1416 3.1416 3.1416

» eye(3)
ans = %#"3" -,"/0$
1 0 0
0 1 0
0 0 1

» rand(3,1)
ans = #3+( 12+ 4"5 !"/5"+ ,(-"($'#*/) -"+/.+ %,"/0$
0.2190
0.0470
0.6789

21
- !#&3"%7' /+)/!( 2/75size -5+6%*( )205 !&#2,+( .#!9 .#*++ )($' !&#2,+ 2%&#( #*-
a =
1 2 3
4 5 6
» ones(size(a))
ans =
1 1 1
1 1 1

22
,*%51%551& (%-&.- /&!%2

!"'+' 5&+" $*#+! 2/5- ,!3#,1#,,1% .#"%)" *%'#$ )%(%$7 $%&#' )2/75+ MATLAB )"-%)
)%#&3"%7 .datafun !&#0+' )%2*6%+ )%#1#1'! )%#&3"%7! .)%&#2,+ (/ )%*%+$' %5 .#2%,3%
!3#,1#,,1 ,)%+%3$ )+5)! ,)%)%5 *%'#$ 8%6-) toolbox-' 5%&+( 8)#" ,)%205 .#"%)" *%'#$
.(*%$% )+*3)+
)6&##+ :2$+' !*%+$ (- ..#-2$+ (/ !"'+' )$&')+ MATLAB -' .*%'#$ :2%&( ,.#"%)" )"#$,
.)05 !+#6* (/ .#"%)" )6&##+ :2$+' !2%/ (-% ,.#"%/ !*#*+ #")/+

:2%&( ..#+# 31 8' *05 /*%0 :/+' .%#( )05 %**+" .#2$ /%(/' !2%,27+,! #"%)" :,$4&/
.)%2%/ 31 -% )%*%+$ 3 - ($' :2$+ (/ !"'+' MATLAB -( .#"%)"! )5 1#"-" .*%'#$
» temps =[19 22 26
5 21 26
20 33 39
7 28 28
23 30 39
18 18 30
12 31 33
13 20 37
9 27 38
12 32 26
5 21 39
23 27 39
21 18 33
13 22 26
20 18 27
22 20 39
23 18 39
8 19 38
12 30 39
24 16 30
12 24 39
12 34 37
7 20 29
16 19 30
21 29 25
22 17 34
15 32 33
21 18 39
18 17 25
14 31 35
12 34 33];

2%'$/ :- ,*27"' !*%+$ 2%,3%% (- ($ *%'#$! )(%$7 )5 $&') .#"%)" *%'#$( !*%37 ((- :2*'
..#2'#5 n ($' !2%/ 2%,3%% 5%!/ ,(7 ('3)# )%*%+$ n ($' :2$+ %"#!/ ,(3

23
V !&#2,+! )%*%+$ (/ %5 V 2%,3% (/ $&%++ '%/#0 – mean(V)

:mean !#&3"%7' /+)/" (temps :2$+' !*%+$ (-') .#2$!+ )05 (-' '7+,! $&%++ '%/#0(

» avg_temps=mean(temps)
avg_temps =
15.4516 24.0645 33.2258

.)%*%+$! #$&%++ .! %#2'#5/ !2%/ 2%,3%% ('3)!


!2%&' )2*%1+ 5#!/- ,temps !&#2,+! ($ mean !#&3"%7! )5 (#$7!( (-%" #((- $&%++ '%/#0(
:!*%+$ 2%,3%% (/
» avg_t=mean(temps(:))
avg_t =
24.2473

:avg_temps !2%/! 2%,3%% (/ $&%++( !!9 !#!) !5&%)!

» avg_t=mean(avg_temps)
avg_t =
24.2473

932$ +" (&$%1*$& (&$%-%$ !&"%#


.X ,)%35$ +" (&$%-%$ / (&$%1*$, .' ,3%8#$, ,/&*0 – [M,I]=min(X) ,[M,I]=max(X)
I !2%/! 2%,3%% .X :2$+' !*%+$ (- (/ #(+#13+ / #(+#"#+! 2'#5! )5 (#-# M !2%/! 2%,3%%
(/ 13*"#5! 5%! I(j) -/ :- ,(#(+#13+ / #(+#"#+) %5&+"/ .#2'#5! (/ .#13*"#5! )5 (#-#
.X !&#2,+! (/ j !*%+$' #(+#13+/#(+#"#+! 2'#5!
:temps !&#2,+' )#(+#"#+! !2%,27+,! )5#&+ ,$4&/

» [min_temps,I]=min(temps)
min_temps =
5 16 25
I =
2 20 25

24
: temps (/ #('%(6! .%+#13+/.%+#"#+! '%/#0( )%#2/75 .#-2* #)/ ,$4&/

» [min_tot,J]=min(min_temps) % first method


min_tot =
5
J =
1
» temps(I(J),J)
ans =
5

» [min_tot,k]=min(temps(:)) % second method


min_tot =
5
k =
2
» temps(k)
ans =
5

:2$+ .#",3 %5 .#(%*6! .#2'#5 :2$+ :%)+ )%7"( 5%! min/max -! )%*%37 (/ 41%" /%+#/
.)%&#2,+ #)/+ 2)%#' .#",3/.#(%*6 .#+#5)+ .#2'#5+ !&#2,+ )%"'( 8#7%(#0( %5 .#%1+
,$4&/
» X=[1 2 3;4 5 6];
» Y=[0 3 0;4 6 1];
» min(X,3)
ans =
1 2 3
3 3 3
» max(X,Y)
ans =

25
1 3 3
4 6 6

.m x n *+#+' #5235 :2$+ )('3 – rand(m,n)

.!*#05 !2%&' .#6(%7+% , 1 -( 0 8#' .#271+ .! %#2'#5/ :- ,m 3 n (*%6' #5235 :2$+ !#!# R

)#(+2%" )%6(7)! .#6(%7+ %#2'#5/ #5235 :2$+ )('3 – randn(m,n)

$&%++ .$ ()#(+2%" )%6(7)!) )#1%56 .#6(%7+ %#2'#5/ :- M 3 N (*%6' #5235 :2$+ !#!# Rn
.1 83) )##,1% 0

:MATLAB-' )%#23#$! .#"%)"! *%'#$ )%#&3"%7 )5 )9-2+ !5'! !(',!


2%5) ,%)*-&0,
2%,3%/!&#2,+ (/ )2',&+ !(7-+ cumprod
2%,3%/!&#2,+ (/ 2',&+ .%-1 cumsum
)2',&+ – 972,! ),#/' )#2+%" !#&26,"#5 cumtrapz
)296" '%2#3% /27! )#&3"%7 diff
#(+#1-+ 2'#5 )5#&+ max
$&%++! '%/#0 mean
#$&+5 :2$ )5#&+ median
#(+#"#+ 2'#5 )5#&+ min
.#2'#5 )(7-+ prod
!(%$ 2*1' 2%,3%/!&#2,+ 8%#+ sort
!(%$ 2*1' )%2%/ 8%#+ sortrows
83) )##,1 '%/#0 std
2%,3%%' .#2'#5 .%-1 '%/#0 sum
972,! ),#/' )#2+%" !#&26,"#5 trapz

26
m -%)!*! .&*1&2, .&/&*0
.MATLAB -! (/ )%*%37! 8%(0' *%'$( "0%"" 5( !-%25 )%(%$7 )2*1 $&'( .#"#"%$+ 2/5-
. *.m )+%#1 .$ ;'%3- !)%5 2%+/(% )#"-%) )5 '%)-( ;(+%+ !9 !23+'

M-File Functions
disp(ans) Display result without identifying variable name
echo Control the Command window echoing of scrip file
commands
input Prompt user for input
keyboard Giev control to keyboard temporarily (Type return to quit)
pause pause until user presses any keyboard key
pause (n) pause for n seconds
waitforbuttonpress pause until user presses mouse key or keyboard key

!6&! ,:1+! ($ )%*%37! )5 6#&+ %"#5 5%! m-file :%)+ )%*%37 $&'+ MATLAB -! 2/5-
:)%*%37' .#/+)/+ .5 )#2/75 )59-
» echo on
» echo off - (%,#'(

5+0-5+*
.%+/ 5(( (.. !&#2,+ / 2%,3%% / 2(31) !")/+ (/ :2$ %5 )9%20+ )17*! - disp
.)*(3+!+ ,(3( !")+!% )9%20+ )17*! - input
» x=input('input x : ');
input x : 3

» disp(x)
3

.%-7&. .3*!
..#2,+27 #%"#/ %5 debugging :2%&( )#"-%)! (/ )#"+9 !2#&$ )%2/75+ )%5'! )%*%37!
)#"-%) (/ )#"+9 !317! 2/75+ keyboard '&+ ..#/3+! 0%(( )#"+9 !23' - keyboard
.(%,#'( )/+/+ return !*%37 . WORSPACE -! :2* )%*%37 )($7!%
.(%,#'( .%26# /3+ (- ,)#"-%)' )#"+9 !317! - pause
.)%#"/ n ,)#"+9 !317! - pause(n)
.2'-$! ($ !&#0( #"$ (%,#' ,)#"+9 !317! - waitforbuttonpress

27
.!(%$7 $%&#' $&+5' MATLAB -! )5 !3#17+ ctrl-c ($ !&#0(

.k_board 523"! m-file '%)-" .keyboard :)%*%37' /%+#/ .#6*"


A=5; -"*6'-
B=5;
keyboard
C=A+B

» k_board .k_board -"*6'-% -(72% 0(2


K» B=10;
K» return
C=
15

K» 8+#1! .10 -( 5 -+ B (/ %-2$ )5 )%"/( !2/7#5% )#"-%)! $%&#' )5 !2&$ keyboard !*%37!
.keyboard '&+' .#5&+" 2/5- $#7%+

.&-7.
:MATLAB #&'3 (/ .#6%1 #"/ .#+##3 .M-FILES ($ 11%'+ MATLAB-' )%"-)
.(script) )#"-%) #&'3 4
.(function) !#&3"%7 #&'3 4
:.! .#6%1! #"/ 8#' .##23#$! .#(*'!!
.,(7- .#,"+%625 .#2#90+% ,(3- .#,"+%625 .#('3+ !#&3"%7 #&'3 .5
.#")/+ %(#5% MATLAB (/ !*%'$! '02+' .#+##3! .#")/+! ($ .#($%7 M-FILES #&'3 .'
5( 2/5 ,.##+%3+ .#")/+ ."#! ,(!+&$ !#&3"%7' .#2*6%+!) !#&3"%7 #&'3' .##+#"7
.!#&3"%7( ;%0+ .#2*6%+

:!#&3"%7 ;'%3 (/ #((- !"'+


function [out1,out2,…,outm]=func_name(in1,in2,…,inn)
% FUNC_NAME Simple function that does nothing
% FUNC_NAME(IN1,IN2,..,) blha, blha, blha …
% … explanation of function …
… program body …

28
.#,"+%625! .#$'%2+ .##26%1' !#205 ,function )#(#+! )5 (%(-) !#&3"%7 (/ !"%/52 !2%/
.##26%1' :2%& 8#5 ,*'(' *05 ,"+%625 ((%- ,(7!% !*#+' .!#&3"%7!+ ,(7- .#290%+!
'%)-( 5( %5 .#3#2 .#$'%2+ .##26%1 2#5/!( 8)#" ,!#&3"%7!+ ,(7 ((- 8#5% !*#+' ..#$'%2+!
)('3+/ .#,"+%625! .#(%6$ .##26%1' %#205% !#&3"%7! ./ $#7%# 8%#%%/! 8+#1 205( .2'*
.*'(' )%#&3"%7' $#7%) %9 !2%/ .,(3- !#&3"%7!
!#&3"%7' /%+#/ !/$" 2/5- ,%9 !2%/ 6#&# MATLAB .(H1) help 1 )2%/ )523" !#"/ !2%/
#"/+ .#&'3' $#7%!( !(%-# %9 !2%/ .!+(/ !&#0+( (help) !29$ )/3')+ 2/5- %5 lookfor
..#6%1!
help ) %9! )+##%1+! !*%37! ($ !29$ /3'))/- H1 )2%/ .$ *0# %6&%# )%5'! )%2$!! )%2%/
.(function_name
.!#&3"%7 / )#"-%)! 4%6 $#7%# )%2$!! )%2%/ 205(

(%%+!&+47 (%-."$ .3/4, – global

#%&2 ..##+%3+ 5(% .##('%(6 .#")/+- .#2-%+ %#!# global !*%37! 205( %$#7%# 2/5 .#")/+
)%#)%5' /+)/!( /# – )#"-%)! )%2#!' 8$+(% )#"-%)! )(#0)' .##('%(6! .#")/+! )5 2#*6!(
.)%(%*6

.&%$%-0 .&%)*-&0
;'%3! /52' $#7%) )#/52! !#&3"%7! .*05 ;'%3' )%#&3"%7 271+ 2#*6!( 8)#" MATLAB ;'%3'
:!&* &.&'! .&3/4&$, .&%)*-&0 .;'%3! :/+!' !"$7%) )%205! )%#&3"%7! .;'%3! ./ )5 5/#)%
(%-."$7 &3/4&, (%-."$, (' '+') .&3#' .&%)*-&0 +" (%%$%-0 (%-."$+ .&-0+ .&+&7% 6-%'
.((%%+!&+4
:5'! 2*1' %9 !#&3"%7 MATLAB /70+ ,5#!/(- !#&3"%7( !#"7'
.)#+#"7 !#&3"%7 4
.(:/+!' 2'1%#) )#,27 !#&3"%7 4
.)#((- !#&3"%7 4

.%530 ,%)*-&0
'5! )&#0+' )%#"-%)+ 32 8!#(5 )/6( 8)#" .private ./' )*0%#+ !&#0+ )0) )%"-%/! )%#&3"%7
/*0+ 2#*6!( 8)#" ,'5! )&#0+( ;%0+ )%/#6" 8"#5 )%#,27 )%#&3"%7/ 8%%#-+ .(parent directory)
.)%205 )%&#0++ )%#&3"%7( !!9 ./ )%($' )%#&3"%7

29
,$4&/+
function [r1,r2]=subroutine(t,u)

e=t+u;
r=t*u;

[r1,r2]=subf(e,r);

function [a1,a2]=subf(k1,k2)
a1=k1+k2;
a2=k1*k2;
t=2; u=3 :.#5'! .#-2$! (/ ,(3 2%'$
e=5; r=6 :%#!# .##+%3+! .#")/+! #-2$ #- ('3"
r1=11; r2=30 :%#!# !#&3"%7! (/ .#,(7!%

(%-."$ %1&0%5
:MATLAB-' .##1#1' $*#+ #1%7#, !/#/ .#+##3
..#)' 8 – MATLAB-' .#271+ (/ #,2*",1! 6%&##! – double 4
..#)' 2 #*#-($ 6&%#+ %) (- .)%9%20+% .#%%) (/ #,2*",1 6%&## – char 4
..#175 8!#2'#5 '%2 2/5 ,)%&#2,+ 8%105( !2%& – sparse 4
($ .##,+)#25 .#'%/#0 $&'( 8)#" 5( .MATLAB (/ )#,37+%3 8%105 )2%& – storage 4
.!9 1%7#,+ .#-2$+
!+%* .#6%( !"'+' .#2%/3! ,.#"%/ .#1%7#,+ .#")/+ 271+ *65+! 8%105 !"'+ – cell 4
.)%#(#$ )%"-) )%7/' struct 1%7#,(
.cell-( !+%* – struct 4
.#*+#+-'2 :2$+ #*#-($ 6&%#+ %(5 .#1%7#,+ *05 (-

(%3&530&'
:.#2%,27%5 (/ .#6%1 !/%(/ .#+##3

(%%5$.%3' (%3&530&' 4
.('%-% .^, : ,.* ,* ,- ,+) )%&#2,+% .#-2$+ ($ )%#,+)+ )%(%$7 $%&#'(

30
(%%1#% (%3&530&' 4
.(~= -% == ,>= ,> ,<= ,<) FALSE %5 TRUE .#2#90+
:2$+! #2'#5+ *05 (- ($ #6%(! 2%,27%5! )5 !(#$7+ .#-2$+ #"/ ($ #6%( 2%,27%5 )($7!
523" !9 :2$+ .)#6%(! !(%$7! )5&%)( .5)!' 1 - % 0 (#-+! ,.#!9 .#*+#+ ($' :2$+ !2#90+%
.%4&+ 932$

:(%%4&+ (%3&530&' 4
3&'. 3&530&',
.#6%( AND &
.#6%( OR |
.#6%( NOT ~
.#6%( EXCLUSIVE OR xor
.)%*%+$! ($ ($%7 2%,27%5! !&#2,+' .175+ .#"%/ 2%,3%! #2'#5 (- .5 1 2#90+ all
.175+ !"%/! 2%,3%' 2'#5 .##3 .5 1 2#90+ any

!+6%*
»A=[1 2 3;4 5 6;7 8 9];
»B=[3 3 2;4 7 6;0 8 1];
»A==B
ans=
0 0 0
1 0 1
0 1 0
»u=[1 0 6 0 9 2];
»v=[4 0 0 1 1 0];
»u & v
ans=
1 0 0 0 1 0
»u | v
ans=
1 0 1 1 1 1

31
»~u
ans=
0 1 0 1 0 0
»all(B)
ans=
0 1 1
»all(u)
ans=
0
»any(v)
ans=
1

32
,$%38 .&3*!
.)#"-%)' )%*%37! .29 '%)#"( )%/+/+! )%*%37 8! (control flow) !+#29 )%23'

for .'+&+
:#((- !"'+
for index = start : increment : end
…statements…
end
(/ (*0+! )2#2' :2$ .increment (/ )%$#17' end :2$( start :2$!+ .*3)# index !")/+!
.1 5%! !$#17!
!+6%*
.M :2$+! (/ )%*%+$! #$&%++ )5 )'/0+ (mloop.m) !5'! )#"-%)!

A=[13 5 10 7;5 6 7 8;15 10 13 12];


M=zeros(1,4);
i=0;
for x=A
i=i+1;
M(1,i)=sum(x)/length(x);
end
:)#"-%)! )&2!
» mloop
» A
A =
13 5 10 7
5 6 7 8
15 10 13 12
» M
M =
11 7 10 9

.!5(%( 5(( !!9 !5&%) ('3( 8)#" 2/5- !5(%(' /%+#/+ $"+#!( ;(+%+ "

33
:)%71%" .#-2* #)/' $&'( (MATLAB -') 8)#" )+*%3! 5+6%*! )5
,$4&/
» [m,n]=size(A); % Method number 1
» M=sum(A)/m
M =
11 7 10 9

» M=mean(A) % Method number 2


M =
11 7 10 9

,.#-2$+ ($ )%(%$7 $&'( MATLAB -! (/ )(%-#! )5 )%(&"+ 8!/ .%/+ .&)+$&$ ,+' .&3&)
.!5(%(! (/ )#10# :%25! !&#2! 8+9 )5 )%-1%0 8-(%
)&#2 #*- :%) .#"'"! ,.#-2$+ (/ /52+ !2*6! 5#! 8+9' 8%-1#0 :2%&( )&(+%+! )71%" !(%$7
.(#(+#13+) #7%1! .(*%6' .#175 #-2$+- !5(%(!

(.-.&$ ,'+&+) while .'+&+


:#((- !"'+
while expression
…statements…
end
.(1) )+5 #%,#'( !%%/ expression *%$ (- )$&'+ !5(%(!

,$4&/
:(MATLAB -( 2-%+! 2)%#' 8,3! 271+!) eps :2$! '%/#0( ,(eps1.m) 5'! )#"-%)! )5 '%)-"

num=0;
eps=1;
while (1+eps)>1
eps=eps/2;
num=num+1;
end

34
:)#"-%)! )&2!
» eps1
num =
53
» eps=2*eps
eps =
2.2204e-016

.+%-1/ :- ,1 271+( 4#1%!( 2/75/ 2)%#' 8,3! 271+! )5 )#'#,2,#5 !2%&' )/70+ )#"-%)!
.1 -+ (%*6 !#!# (MATLAB -')

if, else, elseif %'-. %50"$


:#((- !"'+
if expression1
…statements…
elseif expression2
…statements…
else
…statements…
end

:.#5'! .#23+! %"-)# !9 !23+'


.!"%/52! )%*%37! )2*1 $&%') .##3)+ 1 #%,#' .5 -
)2*1 $&%') - .##3)+ 2 #%,#'% !*#+' .2 #%,#' (/ !3#*' $&')) .##3)+ 5( 1 #%,#' .5 -
.!#"/! )%*%37!
.else -( )-##/! )%*%37! )2*1 $&%') ,.##3)+ %"#5 .##%,#'!+ *05 45 .5 -
.!'%0 %"#5 elseif %5/% else -' /%+#/! .elseif 6%1+ .#%,#' 271+' /+)/!( 8)#"

!+6%*
:(arand.m) [0.9 - 1] %5 [0 - 0.9) , [0 - 0.1) .%0)' 5&+" #5235 271+ .5! 3%*'"

35
a=rand(1)

if a < 0.1

disp('range = [0-0.1)')
elseif (a < 0.9) & (a >= 0.1)
disp('range = [0.1-0.9)')
else
disp('range = [0.9-1]')
end
:)#"-%)! ,(7
» arand
a =
0.8913
range = [0.1-0.9)
!+6%*
:(eps2.m) eps :2$! '%/#0( ,for )5(%( :%)' #5") ,7/+' /%+#/

eps2=1;
for num=1:1000;
eps=eps/2;
if (1+eps)<=1
eps=eps*2;
break
end % {if}
end % {for}
:)#"-%)! ,(7
» eps2
eps =
2.2204e-016

36
switch-case-otherwise
: #((- !"'+
switch expression (scalar or string)
case value1
…statements…
case value2
…statements…
:
otherwise
…statements…
end

%5 !")/+ (/ %-2$( .5)!' ,)%"%/ )%(%$7 $&'( .#"#"%$+ .!' .#23+( )/+/+ switch )*%37
.)9%20+ %5 2(31 6%1+ #%,#'

:(swit1.m) ,$4&/

go=1;
while go
qu=input('input 1,2,3,4,5 or another number to quit');
switch qu
case {2,4}
disp(' 2 or 4')
case {1,2,3}
disp(' 1 or 2 or 3')
case 5
disp(' 5')
otherwise
go=0;
disp(' end');
end
end

37
:;'%3! )&2!
» swit1
input 1,2,3,4,5 or another number to quit 1
1 or 2 or 3
input 1,2,3,4,5 or another number to quit 2
2 or 4
input 1,2,3,4,5 or another number to quit 3
1 or 2 or 3
input 1,2,3,4,5 or another number to quit 4
2 or 4
input 1,2,3,4,5 or another number to quit 5
5
input 1,2,3,4,5 or another number to quit 6
end

.#5)+! case-!+ )#"-%)! !-#/++ switch-! )*%37 205( 8!' ,C 8%6- )%7/' switch -+ !"%/'
.!5(%(!+ )5&%# )#"-%)! .#5)+! case-! 205( MATLAB-' ,!5(%(! 4%1 %5 break )*%37( *$

.&8&3#$ .3$,
.)9%20+' $#7%+! #%,#' )&2! – eval(‘string’)

.)9%20+- !$#7%+! !#&3"%7 )&2! – feval(‘function’)

,$4&/
» t=’i+j’;
» i=2;
» j=3;
» eval(t)
ans=
5
»f=’sin’;
» k=pi/2;
» feval(f,k)
ans=
1

38
.2%*#!( )")#"% eval !#&3"%7!+ 2)%# !(#$# 5#! 8-/ ,feval !#&3"%7' 8)#"/- /+)/!( 4#*$

,%)8%$%50&'
)%5(%( (/ !#&9#2%,3% ..#2%,3%% )%&#2,+ ($ )%(%$7 $%&#'( !""-%)/ )#"-%) !"#! MATLAB
.)#"-%)! $%&#' )5 ;#5)
:(sin1.m) 10*pi *$ -0+ 1%"#1 #-2$ (#-+! 2%,3% )2#&# ,$4&/

tic
i=0;
for t=0:0.01:10
i=i+1;
y(i)=sin(t);
end
toc

:(sin2.m) !#&9#2%,3% 205( %9 )#"-%)

tic
t=0:0.01:10;
y=sin(t);
toc

.!"%/52! )#"-%)!+ !2#!+ !#"/! )#"-%)! #- !52) )#"-%)! #)/ (/ !&2!

39
.%/$$ -&/ ,*%034

.%/$%$-&/ (&*2 5&53" – plot(x,y)

.#2'#5 #*#-($ )6&%#+ 426' !*%3" (- 2/5- ,.x 2%,3% *6"- y 2%,3% (/ 426 )2##&+ %9 !*%37
)%*%3"! .(.#2'#5 271+ %)%5 .$ – !%%/ *++' )%#!( .#'##0 .#2%,3%!) .#2%,3%' .#+#5)+
%"! x-% n 3 m (*%6' :2$+ %"! y %' !23+' ..#2/# .#%%3' )%2'%0+ y-% x .#2%,3%' )%2*6%+!
..#"%/ .#$'&' )%+%3$ n %('3)# m :2%5' 2%,3%
,$4&/
: 0 % 2& .%0)' sin(x) (/ 426 ,%,2/

» x=linspace(0,2*pi,30);
» y=sin(x);
» plot(x,y);

:('3)+! ,(7!

..#2/# .#%%3' )%2'%0+! ,(y-% x .#2%,3%! (*%6) )%*%3" 30 #7( .%3$ ('3)!
*6"- y 2+%(- , i*6"- y(i) #-2$ (/ 426 ('3)# plot(y) 5+6%*( ,*05 .#"%)" 2%,3% 32 8#%&# .5
.(y 2%,3%! (*%6=n , i=1,2,....,n) y (/ .#13*"#5!
:s )9%20+ )71%! #*#-($ %3! $'&% 6%1 )5 $%'3( )2/75+ plot !*%37!

» plot(x,y,s)

40
:)%5'! )%#%2/75! :%)+ 6%1% $'& 2%0'( 8)#"

Symbol Color Symbol Marker Symbol Line Style


b ('36 . %#'.* - 9",/ '.
g .'/" o ('8"7 : -'#'.*
r !'#+ x 1."+ -. %#'.* – '.
c .'/" – ('36 + 1'(2 -- '.''.$
m 4$8/+ – !'#+ * 56'6
y 5'%, s 7'5"/
k /'3& d !'(%"
w 45(

.*05 426' )%+%3$ 271+ ,%,2/( .6 /+/( !(%-# plot !*%37!


,$4&/
» plot(x1,y1,’r’,x2,y2,’b’,x3,y3,’y’)

.'%!&' x3 *6"- y3 % (%0-' x2 *6"- y2 ,.%*5' x1 *6"- y1 :)%+%3$ /%(/ ,,2/) (""! !*%37!

(%"3. !&)%2
x 3%) .3.&7 .3/4, – xlabel(‘string’)

y 3%) .3.&7 .3/4, – ylabel(‘string’)

3!72 .&2)$'! &$&*%$& '3*$ .3/4, – legend(‘string1’,’string2’,…)

(%"3. .3.&7 .3/4, – title(‘string’)

,$4&/
» x=linspace(0,2*pi,30);
» y=sin(x);
» z=cos(x);
» v=cos(x).^2;
» plot(x,y,'g:',x,z,'r-.',x,v,'co');
» grid on;
» ylabel('dependent variables y,z and v');
» xlabel('independent variable x');
» legend('sin(x)','cos(x)','cos(x)^2');
» title('sine and cosine curves');

41
:('3)#/ ,(7!
sine and cosine curves
1

0.8

0.6
dependent variables y,z and v

0.4

0.2

-0.2

-0.4

-0.6
sin(x)
cos(x)
-0.8
cos(x)2

-1
0 1 2 3 4 5 6 7
independent variable x

x,y .%3#+' ,13, .3+# – text(x,y,'text')


.2'-$! ($ !&#0(' !2#0' #7 ($ ,13, .3+# – gtext('text')
.(%%3! #'%$ )5 $'%3% !")/+ 4 :2$!) 426! (/ %3! #'%$ #%"#/ - Plot(x,y,'linewidth',4)
(on/off) .&5-%/3&*, ."3 +" ,31,/,01&, – grid

(%"3., (&#. .2%!* - axis([Xmin Xmax Ymin Ymax])

#)('!% #%()! - .#")/+! (/ .%+#"#+!% .%+#13+! #-2$ #7 ($ 2*6%+ 426! .%0) (*0+ )2#2'-
.Ymin,Ymax -% Xmin ,Xmax #7( ,(7' .%0)! )5 )"-*$+ axis !*%37! .#%()
. $'%2+ #726 8%(0 – axis square
.426! (/ .#2)%#+ .##(%/ )3#0+ – axis tight
..#2#&!% 426! )261+ )3#0+ – axis off/on
(on/off) (%%* %034 6&+#! ;34 +2 ;34 .10/, +&5%!/,+20, – hold

!*%37' /%+#/ .(figure) #726! 8%(0!+ .*%3! ,(7! )5 !3"+ /*0 ,(7 ,%,2/ (*0+ )2#2'-
)2#2' '&+( !2#90+ hold off .426 ($ 426 )17*! 2/75+% (*0+! )2#2' '&+ )5 (,'+ hold on
.(*0+!

42
/#' 6&+#! (%034 301$ – SUBPLOTS

.(subfigures) )%"%(0-)) n 3 m ( 8%(0! )5 3(0( 8)#" *05 #726 8%(0' .#"%/ .#726 271+ )6&!(
(%034 301$ .4),+ %034, 6&+#, .*&+# – subplot(m,n,i)

(5+/+ .#271" )%"%(0! )) 2/5-) i '1+ 8%(0 ))' 1#7*) %9 !*%37 #205 $#7%)/ plot )*%37
.(!,+( !($+(+% 8#+#(
,$4&/
» x=linspace(0,2*pi,30);
» y=sin(x);z=cos(x);
» a=2*sin(x).*cos(x);
» b=sin(x)./(cos(x)+eps);
» subplot(2,2,1);
» plot(x,y),axis([0 2*pi -1 1]),title('sin(x)')
» subplot(2,2,2);
» plot(x,z),axis([0 2*pi -1 1]),title('cos(x)')
» subplot(2,2,3);
» plot(x,a),axis([0 2*pi -1 1]),title('2sin(x)cos(x)')
» subplot(2,2,4);
» plot(x,b),axis([0 2*pi -20 20]),title('sin(x)/cos(x)')

:5'! ,(7 -! ('3)#

43
(figures) .&-&+# 301$! ,*%034

8%(0! (5 )%"7( 8)#" %)%$&+5' ,'1+ 8%(0 (-( ..#*27" )%"%(0' .#726 '1+ ,,2/( 8)" Matlab -'
.%#'6( 32 )%#,"'(2 !"##!)/ )%(%$7 $&'(%
/*0 #726 8%(0 )0#)7 – figure(h)

,$4&/
» figure(2) % creates new figure number 2
» close(2) % close figure number 2
» figure(1)
» figure(3)
» close all % close all figures

..##3 2'- 5%!% !*#+' x 271+ 8%(0 )6&!( )+2%6 figure(x) !*%37!
.#0-%"! #726! 8%(0! )5 2%61) 8%(0! 271+ 8%#& 5(( close !*%37!
.(%#7&-, 6&+#, – +/#$ .3%3!) n 301$ 6&+# %&*%- clf(n)

,-5*, &' ,+/4,


.%/$%$-&/ ;34 +" ,-5*, &' ,+/4, – zoom

.zoom on )*%37' /%+#/ #"$ #"*# 87%5' 426! )5 8#,3!( %5 (#*6!( 8)#"
!"%+)! 9-2+ '3") !#%&2! !*%3"' (2'-$! )%$&+5') 8+1! )5 .3+( /# enter )/3! #205
#"+#! ($ !&#0(% 426! )5 (#*6) 2'-$! (/ #(5+/! 2%)7-! ($ !&#0( .()",3%+ %5 )(*6%+!
.2'-$! (/ !2#26% !&#0( #"$ #"'(+ .%0) $%'3( .6 2/75 .%)%5 8#,3)
.zoom off /#3!( /# zoom on (%,#'(

.&%034 5+* .&/&*0


3!72,$ %034 5+* – [x,y]=ginput(n)

'1+ 8%#& 5(( .y-% x .#")/+' %2+/# 2'-$! )%$&+5' 426! ($ %"+%1#/ )%*%3" n (/ )%,"#*2%3!
.ENTER /3+! ($ !&#0( *$ )%*%3" 8+1( 2/75# '/0+! , n
/3+! (/ ASCII -! 271+ )5 )%,"#*2%5%3( 41%"' 2%+/) [x,y,button]=ginput(n) !*%37!
.;0("/

44
5&53"
&* 5&53" – line(x,y)

.( plot -' %+-) y(i+1) ,x(i+1) )%*%3"( y(i),x(i) )%*%3"!+ .#%%3 ,%,2/
,$4&/
» x=[4 6 8 9]
x =
4 6 8 9
» y=[6 7 10 11]
y =
6 7 10 11
» line(x,y)

:('3)+! ,(7!

6&4%+&0 5&53" – fill([x,y,c])

.c $'&' x,y #"$ 2*6%+! .%0)! )5 )$'%& !*%37!


:y-% x .#2*6%+! .#2%,3%! ($ !*%37! )5 (#$7" ,$4&/

» fill(x,y,'g')
:('3)+! ,(7!

45
(%034 %4&1
%3+&0 ;34 5&53" – polar(theta,rho,s)

.s 8%"61' theta )#%9! *6"- rho1%#*2! (/ #2(%7 426


,$4&/
» r=linspace(1,30,20);
» teta=linspace(0,2*pi,20);
» polar(teta,r,'b')

:('3)+! ,(7!

46
(%&1$ (&#.! ,%)*-&0 3&%) – fplot(‘fun_name’,[xmin amax])

,$4&/

» fplot('sin(1 ./ x)', [0.05 0.1])

:('3)+! ,(7!

.Y 3&5*&&! (%3!%', .&4+0., .' 3'.$, ;34 - hist(Y,N) #

,2%,3%% 5%! N 2/5- .Y -' #(+#13+!% #(+#"#+! .#2'#5! 8#' .#0%%2+! '1+ )5 8##&+ 2(31 N

-+ .#(%*6% Ni + Ni +1 -+ .#",3 Y -' .#2'#5 !+- )"##&+! !*%+$ N(i) 2'#5 (- 2%'$ )('3)+
2

. Ni + Ni %1
2

..&/&$2 N +" .&)&!* M-7 M-by-N ,)%35$ +" .&/&$2, 5&53" – bar(X,Y,'linetype') #

.3%+) y(n) !*%+$! x 2%,3%% 5(( .#37%5! 2#&! #'6 ($ )%*%+$! .%3#+ )5 6&##+ x 2%,3%%
. #37%5! 2#&! #'6 ($ n !*%3"'
./%/! ;34 5&53" – stem(X,Y,'linetype') #

..&43/$ ;34 5&53" – stairs(X,Y) #

,$4&/

47
» x=linspace(-2,2,20);
» y=-x.^2+5;
» yy=[1 1 2 3 2 5 6 3 4 5 6 7 ];
» nn=[1 2 3 5];
» z=x.^3;

» subplot(2,2,1);
» bar(x,y,'g')
» axis([-2 2 0 5])
» title('bar graph');

» subplot(2,2,2)
» stairs(x,y);
» title('stairs graph');

» subplot(2,2,3)
» hist(yy,nn);
» title('histogram');

» subplot(2,2,4)
» stem(x,z);
» title('stem');

:('3)+! ,(7!

48
.&'%4" ;34 – errorbar(X,Y,L,U,SYMBOL)

)%"%)"! )%5#6/! 8%+#1% X 2%,3%% *6"- Y 2%,3%% #"$ 2*6%+! .%3$! 8%+#1 )5 ((%-! 426
.(!($+ #7(- !5#6/) U -% (!,+ #7(- !5#6/) L .#2%,3%%'
.5)!' .#726 '1+ %('3)# )%&#2,+ 2%'$ ,!!9 *++ #($' )%#!( .#'##0 X Y L U .#2%,3%%!
.!*%+$! #2%,3%% '1+(
.(plot -' %+-) .%3$! (/ 8+#1 %5/% $'& )9%20+ 5%! SYMBOL -!
.errorbar(Y,L) !*%37! #*#-($ ('3)# [y-L,y+L] .%0)' !5#6/ )%*%+$% n *6"- y(n) (/ 426

,$4&/
» y=[1:2:40];
» x=rand(size(y))*100;
» errorbar(y,y.^2,x,2.*x,'k');

:('3)+! ,(7!

60)$ ;34 5&53" – compass(X,Y,'S')

%5 X n !i + Yn !j .#5)+! 2%,3%%! (/- 5%! ;0 (- (/ %"%%#-% %(*%6 ,)#/52!+ .#5&%#/ .#&0 426
..#'-%2+ .#271+ %#2'5/ Z *05 2%,3%%' 4#(0!( 2/75 Y -% X )5 . Xn + iYn '-%2+! 271+! (/
feather ;34 5&53" – feather(X,Y,'S')

49
*05 .#%%/ .#0%%2' .#+3%++% #37%5! 2#&!+ .#5&%# .#&0! 85-/ 5(5 compass %+- .#&#0 426
.#"/!+

,$4&/
» z=eig(randn(20,20)) % create vector with 20 random
complex numbers
» subplot(2,1,1)
» compass(z,'r')'
» title('compass');
» subplot(2,1,2)
» feather(z,'g');
» title('feather');

:('3)+! ,(7!

50
.%/$$ .+. ,*%034
%/$%$-.+. !#3$! (&*2 3&%) – plot3(x,y,z,’c’)

%+- .#*++-)() '02+' )%+%3$% .#2/# 2%#&( ,plot !*%37( )#*++ )()! !(#'3+! 5#! %9 !*%37
i - !*%3" ,5+6%*() .'02+' )%*%3" n .#6&##+! ,n (*%6' .#2%,3%% ."! x,y,z ,)#*++-%*! plot -'
)5 $%'3( 8)#" ..#2/# .#%%3' )%*%3"! %2'%0# :1+! ($ 6&%#/ ,(7' .(x(i),y(i),z(i) - 5#! 426'
.c )9%20+ )71%! #"$ .%3$! (/ 8+#1! %5/% $'&!
:'02+' !(2#71 ,%,2#/ ,$4&/
» t=0:pi/50:10*pi;
» x=sin(t);
» y=cos(t);
» z=t;
» plot3(x,y,z,'r');
:('3)+! ,(7!

z=f(x,y) ,3&),$ .&%)*-&0 (%4)%%$, (%#5"$ 5&53" - mesh(x,y,z,c)

:1+! ($ 6&%+! ,(7! .x-y 2%/#+' #"'(+ .%0) ($+ z !,"#*2%5%3- matlab -' 2*6%+ 0,/+
..#2/# .#%%3' )%"-/ )%*%3" 2%'#0 #"$ ('3)+
.n (*%6' 2%,3%% - y-% m (*%6' 2%,3%% - x ,n ($ m (*%6' :2$+ -z

51
. z(i, j) = f 5x(i), y(j)6 - 2/3! #7( ,z -!'%6' )%*%3" (/ )/2- ,,2%/+ 0,/+! mesh !*%37'
2/3! !#!# !9 !23+' ,z (/ (*%6' .#-2$+ .! y -% x 2/5- .6 !*%37' /+)/!( 8)#"
. z(i, j) = f 5x(i, j), y(i, j) 6
$'& 2+%(- .!*%3" (-( $'& .#5)+! c 41%" :2$+ )2*6! #"$ #$#'2 *++- /+/( (%-# 0,/+! $'&
. c(i, j) = g5x(i), y(j), z(i, j) 6 !+%3#+ (/ !#&3"%7 !#!# !*%3"!
.z !'%6! (/ !#&3"%7- $'& $#7%# c :2$+ 5(( mesh )*%37'
)#,#("5 !#&3"%7 %25)#/ :- x,y,z .#-2$+! )5 )%"'( )2/75+ [xi,yi]=meshgrid(x,y) !*%37!
.mesh !*%37! #"$ ,%,2/( %+#5)#% z = f(x, y)
#2%,3%% m (#-+! xi :2$+! )5 !*%37! !"%' m (*%6' y-% n (*%6' x :,(3! #2%,3%% :+1 ($
. y !*%+$ #2%,3%% n (#-+! yi :2$+! )5% x !2%/
.#-2$+ (/ )%(%$7% )%#&3"%7 )%$&+5' z )5 )%"'( 8)#" n ($ m (*%6' yi, xi .#-2$+! )##"' 205(
. - z(i, j) = f 5xi(i, j), yi(i, j) 6 / :- ,

sin <: x 2 + y 2 97
:z * ; 8 !#&3"%7! )5 25)+! 0,/+ ,%,2#/ ,$4&/
x 2 + y2

» x=-8:0.5:8;
» y=x';
» [xi,yi]=meshgrid(x,y);
» r=sqrt(xi.^2+yi.^2)+eps;
» z=sin(r)./r;
» mesh(x,y,z,r)
:('3)+! ,(7!

52
.r !")/+! #"$ 6&%#+! (x,y)=(0,0) !*%3"!+ #37%5! 302+( #10# 426' $'&!
)/2! )%&'/+ !"##!) !9 !23+' :5 ,mesh !*%37! %+- 0,/+ )2##&+ surf(x,y,z,c) !*%37!
:('3)# )+*%3! 5+6%*( .#!9 (x,y,z,r) .#"%)" 2%'$ .)%5(+

» surf(x,y,z,r)

(- !' )%&'/+ )/2- ,)#726 !2%&' .#271+ )&#2,+ /#0+!( )2/75+ pcolor(x,y,z) !*%37!
!/$+( 5%! ('3)+! 0,/+! .2'#5! :2$' #%() )&'/+! $'& .!&#2,+' 2'#5 )6&##+ )&'/+
#7 ($ !&#2,+! (/ !"%+) ('3)) x,y .#2%,3%%! 8%#& 5(( .!($+(+ ,'+' surf(x,y,z)
.(!,+( (5+/ *&' )#/52! 2/5-) !&#2,+' 2'#5 (-( .#+#5)+! .#13*"#5!
.colorbar !*%37! )71%! #"$ $'& )(31 ('3( 8)#"
,$4&/
» [xi,yi]=meshgrid(-1:0.05:1);
» zi=sqrt(xi.^2+yi.^2);
» pcolor(zi) ;
» colorbar;
:,(7! ('3)#

53
($ )#*++-%* !"%+) )2&%# !*%37! .imagesc(z,[cmin cmax]) 5#! !+%* !(%$7 )$&'+! !*%37
.#271+! .%0) 5%! [cmin cmax] -% :2$+! 5%! z 2/5- .$'& .#6&##+! .#271+ :2$+ :+1
.$'& #(*'! %('3)# .2%'$/
:1 -( 0 8#' .##5235 .#271+ )&#2,+ (/ )#726 !6&! ,$4&/
» imagesc(rand(30,30),[0 1]);
» colorbar

(,%034&0&5) ,!&4 %&" (%&&* .&2)$'! #5"$ 3&%) - contour(x,y,z,n)


n ,mesh ' %+- x,y,z .(#*++-)() 0,/+ )25)+!) )#*++-%* !'%6 #%%3 )7+ ),,2/+ !*%37!
(#-+! 2%,3%%- n )2*6! #"$ .#+#%1+ .#!'6 8%#& ;(5( 8)#" .!'%6! #%3 271+ )5 8##&+/ 2(31
..#!'6! )5
.contour3(x,y,z,n) !*%37' /%+#/ #"$ ('3)) )#*++ )() !'%6 #%%3 )7+
,$4&/
» x=-15:1:15; y=x';
» [xi,yi]=meshgrid(x,y);
» r=sqrt(xi.^2+yi.^2)+eps;
» z=sin(r)./r;
» figure(1)
» contour(x,y,z,8);
» xlabel('x'); ylabel('y');
» figure(2)
» contour3(x,y,z,8);
» xlabel('x'); ylabel('y'); zlabel('z');

54
15

1.5 10

1 5

0.5
z

y
0
-5
-0.5

10 -10
15
10
0 5
0
-10 -5
-10 -15
y -15 -15 -10 -5 0 5 10 15
x
x

figure 2 figure 1

:!5'! !2%&' clabel !*%37! )71%! #*#-($ !'%6! :2$ 8%#& )5 .6 .#((%-! ,!'%6 #%%3 ('3( 8)#"

» [c,h]=contour(x,y,z,n);
» b=clabel(c,h);

.h 2%,3%% c !&#2,+ .6 ('3)) x,y,z )%,"#*2%3! #7( !'%6! #%%3 2%#&( 41%"' ,!9 !23+'
(-' 8%#($ #(5+/! 2'#5! .!'%6 #%%3 )%25)+! )%&#2,+ -))+ !#%"' )%2%/ 2 )($' c !&#2,+!
)%2&%#! )%*%3"! 271+ )5 8##&+ 8%)0) #(5+/! 2'#5! ,#7#&71! %3! !'%6 )5 8##&+ !&#2,+ ))
. x,y )%,"#*2%53! )%5&+" !#"/!% !"%/52! !2%/! :/+!' ,!'%6! %3 )5
#%"#/% !'%6! %3 #%!#9 :2%&( ,(handle) !'%6 %3 (-( 271+ :##/+/ n (*%6' !*%+$ 2%,3%% %"#! h
.('%-% .%3#+ ,!2%& ,$'&) %#)%"%-)
)#%%) 42%&) .!+ *05 (-(% .#+#5)+! !'%6! #%%3 %2)%5# clable(c,h) !*%37! )%$&+5'
.!'%6! :2$ )5 )"##&+!
.,"%7! (*%6 #%"#/ 5+6%*( ,)%"%-)! #%"#/ )5 2/75+ b 2%,3%%( ,3,! )%#%%) :%#/
» z=peaks(30); % produces an 30-by-30
matrix
» [c,h]=contour(z,10);
» b=clabel(c,h);
» set(b,'fontsize',7); % change the font size to
7

55
3&/7 3&%) - [x,y,z]=sphere(n)
2%*- 2##&) surf(x,y,z) !*%37!/ :- (n+1)*(n+1) (*%6' x,y,z )%&#2,+ !"%' sphere !#&3"%7!
.:2%&( .5)!' x,y,z )5 )%"/( /# %1%#*2 )(*6!% 2%*-! )99!( .)#/52' %9-2+/ !*#0# (*%6'
.sphere(n) )%$&+5' #2/75 !*#0# 2%*- (/ #*##+ 2%#&
,$4&/
» sphere(20)
» [x,y,z]=sphere(20);
» x=x.*1.5+1;y=y.*1.5+4;z=z.*1.5+1
» axis('equal');

56
.&%+%+4 .&5-%/3&'&*! !&!%1 %0&4 3&%)
z (/ .#2%,3%% !(#0) !"'" z=[a,b] .%0)' R=f(z) !#&3"%7! (/ z -! 2#& '#'1 '%'#1! 4%6 2%#&(
..%0)! #7 ($ R %
'%'#1! 4%6 2%5#)( )%#9,23! )%&#2,+! )5 ('3" [x,y,z’]=cylinder(R,n) !#&3"%7! )%$&+5'
.[0,1] .%0)' z’ 2/5-
(*%6 .( z’=(z-a)/(b-a) ) z=z’*(b-a)+a) !#&+2%71"2, $&'" #2%3+! .%0)' '%'#1! 4%6 2%#&(
. (length( R),n+1) !#!# x,y,z )%&#2,+!
,$4&/
a=3;b=10;
z=3:0.1:10;
r=(0.5+sin(z).^2).*z.^2;
[x,y,zl]=cylinder(r,20);
zm=zl*(b-a)+a;
surf(x,y,zm)
title(' r=0.5+z^2*sin(z)^2 (/ '%'#1! 4%6 ')
»rotbd

57
%/$%$ .+. 5+0! (view point) 5!$, 6&&%7 %&-%"

z
y

Elevation(theta)
x

Azimuth(phi)

:.#-2* #)/' #2/75 ,'+! 8%%#- #%"#/


#(5+/! 2'#5! ,.#2'#5 #"/ ($' 2%,3%% 5%! ,(3! 2/5- ,view([phi,theta]) !*%37' /%+#/ .1
xy-! 2%/#+( )#10# (elevation) !!'6!! )#%9 #"+#!% z 2#& '#'1 (Azimuth) '%'#1! )#%9 5%!
.()%($+' )%#%%9!)
8%%#-/ :- )#/52!+ 5&%#! #*++ )() 2%,3%% 5%! ,(3! 2/5- ,view([x,y,z]) !*%37' /%+#/ .2
.%"%%#-( 32 5(5 2%,3%%! (*%6( )%$+/+ 8#5 ,)#/52! 8%%#-( 2%,3%%! !&3+ 5%! ,'+!
('%'#1! 4%6).*%3! *%+$' 2%#&! 2%'$ ,$4&/
subplot(2,2,1)
mesh(x,y,zm);view([0 1 0]);title('front');
subplot(2,2,2)
mesh(x,y,zm);view([1 1 -1]);title('down isometric ');
subplot(2,2,3)
mesh(x,y,zm);view([0 0 1]);title('top');
subplot(2,2,4)
mesh(x,y,zm);view([1 1 1]);title('up isometric ');
»proj

58
:#*+#+ )() 2%#&' ,'+! 8%%#- (/ (*0+! )2#2' #-2$
Azimuth(phi)=-37.5o
Elevation(theta)=30o
.rotate3d on !*%37! #*#-($ !/$# (matlab5 ') 2'-$! )%$&+5' #"*# 87%5' ,'+! 8%%#- #%"#/
.'&+( '&++ 2'%$ rotate3d .!9 '&++ 5&%# rotate3d off
)#%9 ,!(/ '%'#1! )#%9 ,!+(&+! .%3#+' (%7#, )29$' ,'+! )%"%-) )5 )%"/( 8)#" matlab- 5 '
!*%37! )%$&+5' !"$&')) )%2*6!! .!"%+)! 9-2+ )*%3"% !0#)7!
.set(gca,’property’,N)
.#0-%"! axes ( !2*6!! )5 )-##/+ gca !*%37!

:.&!&"# .&-&7.
. x,y,z !*%3"' !+(&+! )5 .3+) set(gca,’cameraposition’,[x,y,z]) !*%37!
!$#7%) !*%3"!/ :- ,x,y,z !*%3"( !+(&+! )5 8%%-) set(gca,’cameratarget’,[x,y,z]) !*%37!
.,'+! 8%%#- )5 )%*%3"! 2 )%2#*6+ !+(&+! .%3#+ )*%3" .$ *0#' .!"%+)! 9-2+'
,,'+! 8%%#- 2%,3%% '#'1 !+(&+! )5 ''%1) set(gca,’cameraupvector’,[nx,ny,nz]) !*%37!
. !($+ $#'&# ,,'+! 8%%#- 2%,3%%( :"%5+! 2%/#+! #'6 ($ [nx,ny,nz] 2%,3%%! (/ %(,#!/ :-
.!+(&+! (/ ()%($+') deg !0#)7! )#%%9 )5 2#*6) set(gca,’cameraviewangle’,deg) !*%37!
.:7!!% 8,3 ,3##'%5!/ :- (*6 #%1#-! 0,/ !(*6 )#%9!/ (--

59
,$4&/
[x,y,z]=sphere(20);
theta=45;
theta=pi*theta/180;
cp=[2 2 2];
ct=[0 0 0];
n=ct-cp;
b=cross([0 0 1],n);
zt=cross(n,b);
vup=zt.*cos(theta)+b.*sin(theta);%create cameravectorup with 45 degree rotation

vup=2*[vup]/norm(vup);
surf(x,y,z);%draws the sphere ;
axis('equal');
set(gca,'cameraposition',cp);
set(gca,'cameratarget',ct);
set(gca,'cameraupvector',vup);
set(gca,'cameraviewangle',50)
hold on;
plot3([0 vup(1)],[0 vup(2)],[0 vup(3)],'b');
»camer

60
(%$&-%+&0

- n !($++ .%"#(%7 6%&# ,5+6%*( .%#+*3+ )5 (#-+! !2%/ 2%,3% #*#-($ MATLAB -' 6&%#+ .%"#(%7
!#!# P 2%,3%%' ( a 1 X n + a 2 X n %1 + a 3 X n % 2 ...... a n X + a n +1 )
.P=[a1 a2 a3 ..............an an+1]
.175 .#%%/! .#+*3+ .6 (%(-# .#+*3+! 2%,3%

(&-%+&0 +" (%"3&" !&"%# - R=roots(P)


:R !2%/ 2%,3%%' %('3)# P !2%/! 2%,3%%' 6&%#+! .%"#(%7! #/2%/ ,$4&/

» P
P =
1 -12 0 25 116
» r=roots(P)
r =
11.7473
2.7028
-1.2251 + 1.4672i
-1.2251 - 1.4672i

(%$&-%+&0! .&+&20
.#2%,3%% #"/' .)%5 6&##( /# .#+%"#(%7 #"/ 2'0( #*- ..#2%,3%% 2%'#0 #((- #7( )$&')+ !(%$7!
.(!-%+" 2)%#! !($+!+ .%"#(%7( (5+/+ .#175 4#1%!( 2/75 ,:2%& /# .5) !!9 *++ #($'
: 5 X 3 + 2 X 2 + 3X + 46 + 5 X 2 + 16 .#+%"#(%7! 2%'#0 ,$4&/

» [1 2 3 4]+[0 1 0 1]
ans =
1 3 3 5

61
(%$&-%+&0 +07 – C=conv(A,B)
.B -% A .#+%"#(%7! )(7-+ (/ #2%,3%%! 6%&##! !#!) C !2%/! 2%,3%%' !5&%)!
,$4&/
» A=[1 4 9 16 0 1];
» B=[1 2 3 4];
» C=conv(A,B)
C=
1 6 20 50 75 85 66 3 4

(%$&-%+&0 .*&+# - [C,R] = deconv(A,B)


.A=C*B+R / :- R )#25/% C .%"#(%7 8)#) B .%"#(%7' A .%"#(%7 )3%(0
,$4&/
» A=[6 8 19 -3 -3 -24];
» B=[3 4 5];
» [C,R]=deconv(A,B)
C =
2 0 3 -5
R =
0 0 0 0 2 1

(&-%+&0 +" .384- – dP=polyder(P)


d
:
dX
5 X 3 + 2 X 2 + 3X + 46 * 3X 2 + 4 X + 3 #%,#'! '%/#0 ,$4&/

» polyder([1 2 3 4])
ans =
3 4 3

62
a ,/&*-! (&-%+&0, 932 !&"%# – Y=polyval(P,a)

:%-2$ )5 )'/0+% P .%"#(%7' a )5 !'#&+ !*%37!


P * a 1 x n + a 2 x n %1 + a 3 x n %2 ......a n x + a n +1 = Y * a 1a n + a 2 a n %1 + a 3 a n %2 ......a n a + a n +1

.%"#(%7! :2$ !#!# !' 2'#5 (-/ :- ,!!9 (*%6' Y !&#2,+ ('3)) n 3 m (*%6' !&#2,+ 5#! a .5
.a -' !+#5)+! !*%3"'
:[-2,2] .%0)' 2 x 3 + 3 x % 5 .%"#(%7! ,%,2#/ ,$4&/

» a=[-2:0.1:2];
» P=[ 2 0 3 -5];
» Y=polyval(P,a);
» plot(a,Y)
:('3)+! ,(7!

&%"3&" 9$1-+2 (&-%+&0 !&"%# – P=poly(R)


.R !2%/! 2%,3%%' .#$#7%+! .#/2%/! %#!# ,P ('3)+! .%"#(%7(
,$4&/
» p=poly([9 8 1])
p =
1 -18 89 -72
» R=roots(p)
R =
9.0000
8.0000
1.0000

63
(%$3&4+ (%$&-%+&0 .-$ *&3%0 - [R,P,K]=residue(B,A)
.#+%"#(%7 )"+ 3%2#7' :2%& .##3 .#'2 .#23+'
a 1 X n + a 2 X n %1 + ....+ a n X + a n +1
b 1 X m + b 2 X m %1 + ....+b m X + b m +1
:!2%&!+ .%-1(
c1 c2 cm
+ + . . . . . . . .+ +k
X % r1 X % r2 X % rm
..##3(0 .#2'/( 3%2#7 )523" %9 !(%$7

.#('3)+ R !*%+$! 2%,3%%' .!"%+! .%"#(%7 5%! B -% !"-+! .%"#(%7 5%! A ,(""! !*%37'
(r1,r2...rm) .#"-+! #/2%/ .#('3)+ P !*%+$! 2%,3%%' ,(c1,c2..cm) 3%2#7! 205( .#"%+! #+*3+
.(!9- .##3 .5) )#25/! .%"#(%7 ('3)+ k 2%,3%%'%
,$4&/
10X + 20 % 6.6667 5 1.6667
* + + +0 : #%,#'! 3%2#7
5X + 165X + 365X + 46 X+4 X + 3 X +1

» A=[10 20];
» B=poly([-1 -3 -4])
B =
1 8 19 12
» [R,P,k]=residue(A,B)
R =
-6.6667
5.0000
1.6667
P =
-4.0000
-3.0000
-1.0000
k =
[]

64
,!#&3"%7 !)%5 )%$&+5' '/0( 8)#" ,.#+%"#(%7 )"+( .%-1! )-#7! 2+%(- ,!-%7!! !(%$7! )5
:[B,A]=residue(R,P,K) - ,(7 #"%)" #"/% ,(3 #"%)" !/%(/ .$ .$7! :5
,$4&/
» [A,B]=residue(R,P,k)
A =
0.0000 10.0000 20.0000
B =
1.0000 8.0000 19.0000 12.0000

,+07$ +" .384- – Y=polyder(A,B)


.B -% A .#+%"#(%7! )(7-+ (/ )296" Y -' ('3"

,-$ +" .384- – [Q,D]=polyder(A,B)


.A/B (/ )296"! 5#! Q/D -/ :- D !"-+% Q !"%+ ('3"
,$4&/
» A=[2 5 1 -3 0];
» B=[1 7 0 -4 6 5];
» y=polyder(A,B)
y =
18 152 252 -24 -145 144 129 -26 -15

» [Q,D]=polyder(A,B)
Q =
-2 -10 -38 -18 79 100 69 10 -15
D =
1 14 49 -8 -44 94 86 -48 -4
60 25

65
,%)+&035-%'

,f !#&3"%7 )5#&+ #*#-($ ,!*#*+! )%*%3" 8#' !#&3"%7 #-2$ )-2$!( :2* 5#! !#&(%72,"#5
:2+%(- ,.#*%*+! .#-2$( .#%%/ !*#*+! )%*%3"' !#-2$/
Yi * f 5 X i 6
)*%37 (/+( :- .2)%# )3##%*+ !#!) !#&(%72,"#5! 2)%# )%'2 !*#*+ )%*%3" 2%'$/ ,8- .5 2%2'
(-- .)#25#"#( !#&(%72,"#5 #!%9 .!*#*+! )%*%3" 8#' 2/# %3' )2'0+ ,(*0+ )2#2'- plot -!
.2)%# !%'6 !#!# 3%#*! - 2)%# )%'2 )%*%3" (%(-# .%0)!/
:(3(0! %3!) )%*%3" 60 % ($,%3+! %3!) )%*%3" 6 )+#6* )%$&+5' )#25#"#( !#&(%72,"#5 ,$4&/
» x1=linspace(0,2*pi,60);
» x2=linspace(0,2*pi,6);
» plot(x1,sin(x1),'g',x2,sin(x2),'r');
:('3)+! ,(7!

.%/$%$ /# ,%)+&035-%'
.%3'%-%+ ,%)+&035-%' – YI=interp1(X,Y,XI)
)%*%3"' !*#*+! #-2$ 2%,3%% - Y 2%'$ ('3" %9 !*%37 )%$&+5' ,(""! !+6%*'/ plot -' %+- 3%#*'
XI #2'#5 .XI 2%,3%%' )%"#%&+! )%*%3"' !#&3"%7! #-2$ ."#!/ ,YI !*%+$! 2%,3%% #-2$ )5 ,X
..##"%,%"%+ )%#!( .#'##0 X #2'#5 .X #2'#5 .%0)' )%#!( .#'##0

66
,%)+&035-%'+ .&01&- .&5%"
.$%%&1$ ,5%"! ,%)+&035-%' .+20, – YI=interp1(X,Y,XI,'method')
:.#5'! .#&%(#5! :+1 ($ (implicit) )%5%%/+ )-2$+ )#"'" %9 !,#/'
.%"#(%7 '%/#0( /+/) )71%" !*%3" (- .3 2*1+ .%"#(%7 2%'$# )%"%/52! )%*%3"! /%(/ :2*
!*%3"' !9 .%"#(%7 (/ !#"/!% !"%/52! %)296" 2/5- ,!(5+/+/ !*%3"! :2*% !-2* 2'%$!
.6 )%7#&2 )('3)+ :-' .!*%3" !)%5' ((5+/+) .*%3! .%"#(%7! )%296"( )%%/ !"##!) )#(5+/!
..#-%+1 .#+%"#(%7 #"/ (- 8#' !##"/! )296"'
. CUBIC SPLINES !,#/! )523" 3 2*1+ .%"#(%7' 2'%*+% 205+ - 'spline'
.CUBIC )523" !"%/52! )296"! #'6( 32 5%! ;%(#5! !#7( ,)71%" !,#/ - 'cubic'
.2)%#' !'%23! !*%3"! #7( :2$! )5 )'/0+! )71%" !,#/ - 'nearest'

,$4&/
» x=[0:0.1:1];
» y=[1 2 4 5 3 2 4 8 9 7.5 6.4];
» x1=[0:0.01:1];
» y1=interp1(x,y,x1,'spline');
» plot(x,y,'og',x1,y1,'r')
» plot(x,y,'g',x1,y1,'r')
:('3)+! ,(7!

67
(%!&3%*
,)%#&3"%7 (/ '2 271+ .#5)!( 2/75 ,.#*#*' .#'%/#0+ %5 !*#*++ .#"%)" 41%5 (-(
)5 '/0( 2/75 )59- !#&3"%7 (- 2%'$ .!*#*+! #"%)"( !+%* 87%5' )%6!")+ 8!#)%+%3$/
(/ #$%'#2! .%-1- )2*6%+ )#((-! !5#6/! ((- :2*' .)#(+#"#+ !#!) !5#6/! .2%'$ .#+*3+!
. %(5 )%*%3"' .#*%*+! .#-2$! 8#'( !*#*+! )%*%3"' )'2%3+! !#&3"%7! #-2$ 8#' .#/27!!
: 2+%(-
n
e * > 5f ( x i ) % y i 6
2

i *1

*05 (- #7( e )5 2%96( /# ,)#(+#"#+ !#!) !5#6/!/ :- !#&3"%7! #+*3+ )5 5%&+( )"+ ($
. (least squares) .#)%07 .#$%'#2 )523" %9 !,#/ . 175( )%%/!(% .#+*3+!+

(&-%+&0 .$'., – p=polyfit(x,y,n)


#-2$ )5 !%%!+ x 2%,3%%! .n 2*1+ .%"#(%7 #+*3+ '%/#0( least squares )$&'+ %9 !#&3"%7
.#%()! !")/+! #-2$ )5 y -%, #%() #)('! !")/+!
..%"#(%7! #+*3+ .#('3)+ p !2%/! 2%,3%%'
:)%5'! )%5&%)! %('3)! )+##%1+ !*#*+'/ 0#"" ,$4&/
» x=[0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1] ;
» y=[-0.447 1.978 3.28 6.16 7.08 7.34 7.66 9.56 9.48 9.30 11.2];

:)#(+#"#+ !5#6/! %2%'$ 2 2*1+ .%"#(%7! 426 8-% !*#*+! )%5&%)


» n=2;
» p=polyfit(x,y,n);
» xi=linspace(min(x),max(x),100);
» yi=polyval(p,xi);
» plot(x,y,'og',xi,yi,'r')
» xlabel('x')
» ylabel('y=f(x)')
» title('second order curve fitting')

68
2*1 2%'$ ,8- %+- .!*#*+! )%5&%)' #%() 5%!% 2)%#' !%'6! 02-!' %"#5 #(+#,7%5! .%"#(%7! 2*1
:2* 2'%$/ .%"#(%7 ('3)# !9 !23+' ,(.%"#(%7! #+*3+ '1+-) !*#*+ )%*%3" n+1 )%07( .#/%2* n
.#(+#,7%5 02-!' %"#5/ '&+ - )%*%3"! n+1
: (""! !*#*+! #"%)" 2%'$ ,$4&/
» p=polyfit(x,y,max(size(x))-1);
» xi=linspace(min(x),max(x),100);
» yi=polyval(p,xi);
» plot(x,y,'og',xi,yi,'r')
» xlabel('x')
» ylabel('y=f(x)')
» title('second order curve fitting')
:('3)+! ,(7!

69
%5 !#&26,"#5 ,!#&(%72,"#5 :2%&( $%26 5%! :5 )%*%3"! (-' 2'%$ ."+5 '%2#3! )%52( 8)#"/ %+-
.)296"

.%3$&- ,8%+-'
.#'%2#3' /%+#/ :%) ,)%#,+)+ )%#$'( )%"%2)7 .#/70+ !' #,+)+ .%0) !"#! )#2+%" !9#("5
.!&3 #5")'% )%5#6/' )%'/0)!% .##2+%"
:)%5'! )%(%$7( .##2+%" .#(- 371+ MATLAB
!#&3"%7 ,%,2/ 4
8%&#3 )%*%3" '%/#0 4
.#175 )5#&+ 4
!#&26,"#5 4
)%296" 4
)%#(5#&"27#* )%5%%/+ 8%2)7 4

,%)*-&0 5&53"
,%)*-&0 +" ;34 5&53" – fplot

)5 .!+#6*! :#(!)+ !5&%)- .#,27 2#10# 5( ,%,2/!/ 0#,'+! .)#2%6(5 )((%- fplot !*%37!
.M-file function -- %5 )9%20+ !")/+- 2%+/( 8)#" !#&3"%7!
,$4&/
:!#&3"%7! ,%,2#/
1 1
:humps.m )#"-%)! . 0 ' x ' 2 $,3' , f 5 x6 * + %6
5 x - 0.36 + 0.01 5 x - 0.96 2 + 0.04
2

function y = humps(x)

y = 1 ./ ((x-.3).^2 + .01) + 1 ./ ((x-.9).^2 + .04) - 6;

:)#"-%)! )&2!
» fplot('humps',[0 2],'b')
» title('FPLOT of humps')

:('3)+! ,(7!

70
FPLOT of humps
100

80

60

40

20

-20
0 0.5 1 1.5 2

,$4&/

!")/+- )2*6%+ !#&3"%7! ,%9 !+6%*' . 0 ' x ' 8 $,3' f5 x6 * 2! e-x! sin5 x6 !#&3"%7! ,%,2/
.)9%20+
» f='2*exp(-x).*sin(x)';
» fplot(f,[0 8],'b')
» title(f),xlabel('x')

2*exp(-x).*sin(x)
0.7

0.6

0.5

0.4

0.3

0.2

0.1

-0.1
0 1 2 3 4 5 6 7 8
x

((&$%1*$ (&$%-%$) 6&)%* .&/&*- .'%)$


)%#&3"%7 #)/ MATLAB -' .175( )296"! )5%%/! #*#-($ )#,#("5 )'/%0+ 8%&#3 )%*%3" )5#&+
..%+#"#+ )*%3" (/ #2+%" '%/#0 )%2/75+!
/#' ,-."$ .+2! ,%)*-&0 +" %$&*$ (&$%-%$ .'%)$ - fmin

(%-."$ .!&3$ ,%)*-&0 +" (&$%-%$ .'%)$ - fmins

71
2/5- ./+)/+! #*#-($ $'3"! .#%1+ $,3 :%)' .%+#"#+( 5#! #+%3+ .%+#"#+ 6/%+' !"%%-!
. -f(x) (/ .%+#"#+ .#'/0+ ,.%+#13+ 5%&+( .#"#"%$+
.M-file function -- %5 )9%20+ !")/+- 2%+/( 2/75 !#&3"%7! )5

!#&3"%7! (/ .%+#13+% .%+#"#+ )%*%3" '%/#0( )/+/+ !5'! )#"-%)! ,$4&/

:(exp_fmin.m) )#"-%) '%)-" .!"%205! 5+6%*' !6&%!/ f5 x6 * 2! e-x! sin5 x6


% exp_fmin.m

fn='2*exp(-x).*sin(x)'; % define function for min


xmin=fmin(fn,2,5) % search over rang 2<x<5
x=xmin; % need x since fn has x as its variable
ymin=eval(fn) % evaluate at xmin

fx='-2*exp(-x).*sin(x)'; % defin for max: note minus sign


xmax=fmin(fx,0,3) % search over range 0<x<3
x=xmax;
ymax=eval(fn) % evaluate at xmax

:)#"-%)! )&2!
» exp_fmin
xmin =
:') %"75( (."'#$) "0"(*+ 4'/-2
&
3.9270 Xmin = ? 0.785
4
ymin = 5! &
Xmax = ? 3.93
-0.0279
xmax =
0.7854
ymax =
0.6448

.Xo - )%1"-)!( #"%/52 2%,3%% .#"##&+ ,.%0) 8##&( .%3+' .!"%/ ,$+ fmins !*%37' /%+#/!
(x,y) = (1,1) -( '%23! 2%95' z = x 2 + y 2 )#*++-%*! !#&3"%7! (/ .%+#"#+ )5#&+ ,$4&/
:(two_var.m )#"-%))
function z=two_var(v);
x=v(1);
y=v(2);
z=x.^2+y.^2;

72
:)#"-%)! )&2!
» v=[1 1];
» xymin=fmins('two_var',v)
xymin =

1.0e-05 * (x,y) = (0,0) :"0"(*+ 4'/-2

-0.1148 -0.6231

» Zmin=two_var(xymin) % verifing the result


Zmin =
4.0138e-011

(%10' .'%)$
/%#% ,-."$ .+2! ,%)*-&0 +" (%10' .'%)$ - fzero

($ 32% :5 )($%7 fzero !*%37! .175 5%! !#&3"%7! :2$ .!'/ )%+%3+! .! !#&3"%7 (/ .#175
#"%/52 :2$ $%'3( /# !#&3"%7! ./ 8%#&( 41%"' . M-file function -- )2*6%+! !#&3"%7
.)%1"-)!(
: x = 1.2 !*%3"( 2)%#' '%23! ,humps !#&3"%7! (/ 175 )5#&+ ,$4&/

» xzero=fzero('humps',1.2) % look for zero near 1.2


xzero =
1.2995
» yzero=humps(xzero) % evaluate at xzero
yzero =
3.5527e-015

g(x) = !/*0 !#&3"%7 2#*6!( 2/75 , f(x) = c ,$%'3% !#&3"%7 8#' :%)#0 )*%3"' .#"#"%$+ .5
. g(x) (/ .#175 )5#&+ #"$ MATLAB -' 2%)7(% f(x) - c

73
,%)345-%'
2%'#0+ .#2&%"! .#972,! #0,/ .%-#1 #*#-($ !#&3"%7( )0)+ 0,/ )'/0+ trapz !*%37!
.!+#6* )%*%3" (/ #25#"#(
,$4&/
: @ x = 0.2 !+#6* 0%%2+ .-1<x<2 .%0)' humps !#&3"%7( )0)+ 0,/! '%/#0

» x=0:0.2:2;
» y=humps(x);
» area=trapz(x,y) % call trapz just like the plot command
area =
26.4616

:(26,"#5! (/ 16 '%2#3
R o u g h Tra p e z o id a l A p p ro x im a t io n t o A re a U n d e r C u rve
100

80

60
y=humps(x)

40

20

-2 0
-1 -0 . 5 0 0.5 1 1.5 2
x

.#)#+5! 0,/( 2)%# !'%23 !5&%) ('3)) !+#6*! 0%%2+ )5 8#,3"/ (--
@ x = 0.02 20'" 2/5- ('3" '%, 2)%# '%2#3
» x=-1:0.02:2; % better approximation
» y=humps(x);
» area=trapz(x,y)
area =
26.3449

74
:.&01&- ,%)345-%' .&/&*0
Simpson .5%"! ,%)345-%' - quad

Newton Cotes .5%"! ,%)345-%' - quad8


$%'3( :2%& 8#5 !(5 )%*%37' .#/+)/+ 2/5- ..#972,! ),#/+ 2)%# )%3#%*+ )%,#/! #)/
.M-file function -- 2%+/( :#2& !#&3"%7! )5 ,!+#6* #0%%2+

:-1 < x < 2 .%0)' humps !#&3"%7( )0)+ 0,/! '%/#0 ,$4&/

» area=quad('humps',-1,2) % find area between -1 and 2


area =
26.3450

» area=quad8('humps',-1,2) % find area between -1 and 2


area =
26.3450

.&384-
.p - .%"#(%7! )2#96( )/+/+ %9 !*%37 - polyder(p)
2%/#+' )%*%3" (/ !2*#1 !"%)" .#%1#" (/ )%5&%) 41%5 .#(#-+! .#2%,3%% #"/ .! y -% x ,$4&/
dy
. -)296"! )5 '%23' '/0( .#"#"%$+ .x ,y
dx
» x=0:0.1:1;
» y=[-.447 1.978 3.28 6.16 7.08 7.34 7.66 9.56 9.48 9.30 11.2]; % data
» p=polyfit(x,y,2) % find polynomial coefficients
p =
-9.8108 20.1293 -0.0317

» xi=linspace(0,1,100); % drawing the result polynom


» z=polyval(p,xi); % evaluate polynomial
» plot(x,y,'o',x,y,xi,z,':')
» xlabel('x'),ylabel('y=f(x)'),title('Second Order Curve Fitting')

75
S e c o n d O rd e r C u rve F it t in g
12

10

6
y=f(x)

-2
0 0.2 0.4 0.6 0.8 1
x

:MATLAB -' 2%96"


» pd=polyder(p)
pd =
-19.6217 20.1293

» z=polyval(pd,xi); % evaluate derivative


» plot(xi,z)
» plot(xi,z,'b')
» xlabel('x'),ylabel('dy/dx'),title('Derivative of a Curve Fit Polynomial')

D e riva t ive o f a C u rve F it P o ly n o m ia l


25

20

15
dy/dx

10

76
diff ,/&*0, .&2)$'! .384- !&3*

- )296" (/ !2*6!
dx f (x + h) - f (x)
* lim
dy (x + h) - x
hA0
dx f (x + h) - f (x) - ( h > 0 ) '%23
?
dy (x + h) - x

,$4&/
:( cos(x) (/ '%23 5#! !5&%)! ) sin(x) (/ )296"

» x=linspace(-pi,pi,1000);
» y=sin(x);
» dy=diff(y)./diff(x); % compute differences and use array division
» dx=x(1:length(x)-1); % create new x axis since dy is shorter than y
» plot(x,y,dx,dy)
» title('Approximate Derivative Using Diff')
» legend('sin(x)','cos(x)')

Approximate Derivative Using Diff


1
sin(x)
0.8 cos(x)

0.6

0.4

0.2

-0.2

-0.4

-0.6

08

/+)/!( /# !9 !23+' .)#,#("5 2%96( 8)#" 5( 2/5- 32% :5 ;(+%+ diff !*%37' /%+#/!
(/ )%5&%)' 2'%*+ 2/5- ;(+%+ %"#5 diff !*%37' /%+#/ .2/75! (%-- .#",3 !+#6* #0%%2+'
. ()+*%3! !+6%*' %+-) #%1#"

77
.&%+'%)-30%/ .&'&&"$
6&3.0, %!+"

.#'(/! )5 $&'( /# aX"" + bX" + cX + d * 0 :!2%&!+ )#(5#&"27#* !5%%/+ 2%)7( #*-'


:.#5'!
.2%)7( .#"#"%$+ %"5 !)%5 )#(5#&"27#*! !5%%/+( !+#5)+! !#&3"%7 )'#)- (1
.%"')-/ !#&3"%7( )52%3! Matlab (/ ode45 )#&3"%7' /%+#/ (2

(/ 2*1( !!9 )%#!( :#2& !(0)!! #5") 271+ .!(0)! #5")' :2%& /# !5%%/+! 8%2)7 :2%&( $
#5") #"/' :2%& /# #"/ 2*1+ )#(5#&"27#* !5%%/+ 8%2)7' ,!+6%*() .)#(5#&"27#*! !5%%/+!
.(*05 !(0)! #5")' :2%& /# 8%/52 2*1+ )#(5#&"27#* !5%%/+ 8%2)7'% !(0)!
(/ !(0)! #5") !+6%*( .!5%%/+! 2*1+ )%07( *05 2*1' .#-%+" *#+) %#!# !(0)!! #5") $
))( 8)#" 5( . X (t * 0) * ... %5/% X" (t * 0) * ... : %#!# #"/ 2*1+ )#(5#&"27#* !5%%/+
X"" (t * 0) * ... :5'! #5")! )5 %9 )#(5#&"27#* !5%%/+(

,%)*-&0 .!%.7 .1
.#")/+ #"/ (#-# ,%)*-&0, 5+*
(t 8+9 !+6%*() !#&3"%7! !")/+ .%0) (5
((""! !2$!' ')-"/ #7- !(0)! #5") 271+ )%#!( .#(%-#) !(0)!! #5") ('

)%296"! )5 (#-# ,%)*-&0, 5+0


X"" X" )5 (#-# ,(7! #"/ 2*1+ )#(5#&"27#* !5%%/+ 2%'$ : ,$4&/+
X" )5 (#-# ,(7! 8%/52 2*1+ )#(5#&"27#* !5%%/+ 2%'$

%-" 3/1$ .%+'%)-30%/ ,'&&"$ 3&!2 ,%)*-&0+ ,$4&/


X"" + aX" + bX + C * 0 2 2*1+ !5%%/+ 2%'$
function zdot=vdpol(t,x) :!#!) !#&3"%7! ;'%3 )2)%-
:2/5-
[zdot1, zdot2] % [ X" , X"" ] )%296" % output % zdot

. !#&3"%7! !")/+ t= [t 0 , t f ] :2/5- % input % (t,x)

.!(0)! #5") x=[ X (t * 0); , X" (t * 0) ]

78
,%)*-&0, 67&.

Function zdot=vdpol(t,z)
X = Z (1)
X" = Zdot (1) * Z (2)
X"" = zdot (2) * % a ! Z (2) % b ! Z (1) % c ( X"" * % aX" % bX % c)
zdot * [ zdot (1); zdot ( 2)]

6&"'3 3/1$ .%+'%)-30%/ ,'&&"$ 3&!2 ,%)*-&0+


4
v + av + b * 0 1 2*1+ !5%%/+ 2%'$

Function vdot=rc(t,v) :!#!) !#&3"%7! ;'%3 )2)%-


:2/5-
[v"] - )296" % output % vdot

; !#&3"%7! !")/+ t= [t 0 , t f ] :2/5- % input % (t,v)

!(0)! #5") v(t=0)=….

,%)*-&0, 67&.
Function vdot=rc(t,v)
vdot * % a ! v % b
v" * % av % b

.%+'%)-30%/, ,'&&"$, 6&3.0+ ode45 .%)*-&0! "&$%" .2

!5'! !*%37! )5 .%/2" #"/ 2*1+ )#(5#&"27#* !5%%/+ 2%'$

[t , z ] * ode 45(' vdpol ' , [t 0 , t f ], [ x ( t *0 ) ; x" ( t *0 ) ])

.'vdpol' - .#6) #"/ .$ !#&3"%7! )5 .%/2( /# 5.3 !12#6'


:!5'! !2%&' !#&3"%7! ./ #"7( (@-(*%2,/) )#-%2- .$ 45 )59 .%/2( 8)#" 6.5 !12#6'
' vdpol ' = @ vdpol

79
:.! ode45 .%)*-&0 +" (%5+*,

%"')-/ !#&3"%7( !5#23 - ' vdpol ' .1


first input - (t) of ' vdpol ' - [t 0 , t f ] .2

second input - (z) of ' vdpol ' - [ x(t *0) ; x" (t *0 ) ] .3

:.! ode45 .%)*-&0 +" (%5+0,

.#2'#5 n ($' - 8+9! (/ !*%+$ 2%,3% - t t (1) * t ; t (n) * t f .1

.)%*%+$ 2-% )%2%/ n .$ !&#2,+ – z .2

z (1) * x z (2) * `x

(&7%1

: %"( .#")%" ode45 !#&3"%7! (/(outputs) .#,(7!

( !#&3"%7! #*# ($ $'3"/ - n - .#2'#5 271+ .$) t !*%+$ 2%,3% (1


: 2/5- (#"/ 2*1+ '7#* !5%%/+ 2%'$) )%*%+$ 2 -% )%2%/ n .$ !&#2,+ (2
X (/ 8%2)7 5#! !"%/52 !*%+$
X" (/ 8%2)7! 5#! !#"/ !*%+$
: !5%%/+' '#&!(% 2%90( !9 )%/$( :#2&/ !+ (- X"" )5 5%&+( .#"#"%$+ .5 (3
:2/5- X"" * %aX" % bX % c
8%2)7! (/ !"%/52 !*%+$ - X
8%2)7! (/ !#"/ !*%+$ - X"

80
Cell arrays (%'. %732$
-' .##3! !")/+ (/ 6%1 (- (#-!( (%-#! ,5) 5%! %' ,"+(5 (- 2/5 :2$+ 5%! .#5) :2$+
41%" 5)% )9%20+ 205 5) ,!&#2,+ (#-!( (%-# .#5) :2$+ :%)' *05 5) ,5+6%*( .MATLAB
.'-%2+ 271+
:.#5) :2$+ (/ #2/75 !"'+ ,!+6%*(

cell(1,1) cell(1,2) cell(1,3)


[3 4 2 [‘Anne smith’ [0.25+3j 8 - 16j
9 7 6 ‘ 9 / 12 / 94‘] 34 + 5j 7 + 92j]
8 5 1]

cell(2,1) cell(2,2) cell(2,3)


[3 4
[‘text’ [4 2
[1.43 2.98 5.76] 7 6]
1 5]
[4 2 7] [0.02+8j]

: .#-2* #)/' )#2/75 .#5) :2$+ )##"'


.{} .#(1(%1+ .##26%1% !+/! #,7/+' /%+#/ .1
:!($+( 6&%+! :2$+! )##"' ,$4&/
» A(1,1)={[3 4 2;9 7 6;8 5 1]};
» A(1,2)={char('Anne smith','9/12/94')};
» A(1,3)={[0.25 8;34 7]+i*[3 -16;5 92]};
» A(2,1)={[1.43 2.98 5.67]};
» A(2,2)={[3 4;7 6]};
» A(2,3)={{['text'],[4 2 ;1 5];[4 2 7],[0.02+8i]}};
» A
A =
[3x3 double] [2x10 char ] [2x2 double]
[1x3 double] [2x2 double] {2x2 cell }

81
205(% A=cell(m,n) – !*%37! )%$&+5' n -($ m -(*%6' A .#5)! :2$+ (/ )+*3%+ !##"' .2
5)' 2%,3%% .%3#+ ,!+6%*( ..#(1(%1+ .##26%1' .+%3#+ 8%#& #*#-($ .#5)' .#-2$ )+/! 8-+
.A{i,j}=[1 2 3 ] - (i,j)
. )#02-! !"#5 :5 )#"-%)! (/ !&#2! 8+9 )5 )2&3+ )+*3%+! !##"'!
.celldisp(A) - !*%37' /%+#/ #"$ )#2/75 A- .#5) :2$+ (/ %)(%-) )6&!
,$4&/
» celldisp(A)
A{1,1} =
3 4 2
9 7 6
8 5 1
A{2,1} =
1.4300 2.9800 5.6700
A{1,2} =
Anne smith
9/12/94
A{2,2} =
3 4
7 6
A{1,3} =
0.2500+ 3.0000i 8.0000-16.0000i
34.0000+ 5.0000i 7.0000+92.0000i
A{2,3}{1,1} =
text
A{2,3}{2,1} =
4 2 7
A{2,3}{1,2} =
4 2
1 5
A{2,3}{2,2} =
0.0200+ 8.0000i

82
.cellplot(A) !*%37' /%+#/ #"$ )#2/75 A -.#5)! :2$+ (/ )#726 !6&!
» cellplot(A)

(%1*/-%'! "&$%"
..#(1(%1+ .##26%1' %+%3#+ 8%#& #"$ $&%') .#%1+ 5)( !5#23
,$4&/
» name=A{1,2}
name =
Anne smith
9/12/94

.##26%1' 5)! .%3#+ 8%#& #"$ $&')) ,.#%1+ 5) :%)' 5&+"! ,:2$+ :%)+ .#%1+ :2$( !5#23
..#(#62 .#26%1' ,:2$+' :2$! .%3#+ ,:/+!' ."#+#+% ..#(1(%1+
,$4&/
» A{1,1}(2,2)
ans =
7
» A{2,3}{1,2}(2,2)
ans =
5

83
5) (/ .%3#+ 6&## !2%/' 2'#5 (-/ :- ,.#13*"#5 (/ !2%/ 8%#& :2%&( .##)*%3"' /+)/!( 8)#"
.(.!#"#' .#3#17 .$ .#13*"#5! )5 !(#-+! !2%/( (%3/ 2'*!)
,$4&/
» b{1,1}=[1 2 3];
» b{1,2}=[2 9 4];
» b{2,1}=[5 6 7];
» b{2,2}=[8 0 2];
» b
b =
[1x3 double] [1x3 double]
[1x3 double] [1x3 double]
» b{1:2,2}
ans =
2 9 4
ans =
8 0 2
» cross(b{1:2,2})
ans =
18 28 -72

84
underwar@hotmail.com !"# !$%

!"!"# Matlab
$%& $!'

.http://underwar.livedns.co.il !"#$% &!'$ $( )%*%


.!/3%$ "#% ,!'1% !',+# #-- ,+$,-. $+&%/ $( )%*% 0+1$- 2+#
2.' ,)%*%/ 7+1'%$ 7&+%/ ,'%+,$ /47 8!9+, ,6+47 '# !+,+ ,4(5 -.- +#!3# '55+# )%*%$ !/3%
"# 41*- +&. 8+:%#%$ /!+% "# $,7 !/3%$ ,"#( 87 .)%*%/ 8+7+1'%$ 8+#,'5$ -, 2.'"$ "'5'.5-
.!"'+/ #-%$' 4+'&%$ 7&+%$

!"# !$%- "'!'%, "'+'.($ -.

Nir Adar
Email: underwar@hotmail.com
Home Page: http://underwar.livedns.co.il

.!/3%$ -# "'!7$' 8+5'4+" '3-, #5#

UnderWarrior Project http://underwar.livedns.co.il

-1-
underwar@hotmail.com !"# !$%

(!'!!') *+,- .1

2 .................................................................................................................................... &$%$$%' ()*+ .1

4 .............................................................................................................................................. #*,- .2

4 ............................................................................................................................. $!(7 "-/4 .2.1


4 .......................................................................................................... MATLAB-/ 8+:/4 +9'* .2.2
5 .......................................................................................................................... CLC "+:45'1 .2.3
5 ..................................................................................................................... CLEAR "+:45'1 .2.4
5 ......................................................................................................................... 8+5",% "'%, .2.5
5 ............................................................................................................... MATLAB-/ 8+!1*% .2.6
5 ........................................................................................................ MATLAB-/ $&'/7$ 8'+* .2.7

6 .............................................................................................................................. +*.$!/- +!"01 .3

6 ........................................................................................................................... "+5&+ $!&9$ .3.1


6 ........................................................................................................."':+!;% "!+:+- "'+:45'1 .3.2
7 ................................................................................................ 8+/4'7 8+.!7 -7/ !';4'' "!+:+ .3.3

8 ................................................................................................................... +*.$!/- 2' +*$.2*3$%- .4

8 .................................................................................................................. $:+!;%- 8+*4&5+# .4.1


9 ............................................................................................................... 8++:+!;% 8+!';!1'# .4.2
9 ............................................................................................................ Transpose .4.2.1
9 ................................................................................................ !"#$%&' $()* +&#,* .4.2.2
9 ...................................................................................................... !%--#&$%. /'0#1 .4.2.3
10 ........................................................................................................ !"#$%& 2'3#! .4.2.4
10 ......................................................................................................... sum +#")-'3 .4.2.5
10 ................................................................................................... cumsum +#")-'3 .4.2.6
11 ........................................................................................................ prod +#")-'3 .4.2.7
11 ..................................................................................................... !.')-! $'%$3'4 .4.2.8
12 ..........................................................................................................find +#")-'3 .4.2.9
12 ............................................................................................. "+!#+5+- "'#'',% ".!7% "!+"1 .4.3
13 ..............................................................................................................................."'('!3% .4.4

14 ......................................................................................................................................... 14$3!0 .5

14 ................................................................................................................................... +--. .5.1


14 .................................................................................................................. ;',1 6!9- #%9'& .5.2

UnderWarrior Project http://underwar.livedns.co.il

-2-
underwar@hotmail.com !"# !$%

15 .................................................................................................................................... 1!4, $%,- .6

15 ..............................................................................................................................IF "&'41 .6.1


15 ...................................................................................................................... WHILE "#-'- .6.2
15 .......................................................................................................................... FOR "#-'- 6.3.

16 .......................................................................................................................................+*$.4%*3 .7

UnderWarrior Project http://underwar.livedns.co.il

-3-
underwar@hotmail.com !"# !$%

&,#. .2

$+:(+-#'(+' ,$+:-'%+* ,+*&5$' +7&% /',+3- "+/+;4#!;5+# $/+/* "--'.$ "'5." "1, #+$ Matlab
:"'!3# "'1, 2+/- $5+/ 8+-&/$ .8+%"+!'9-# 2'5."'
.8+!-4* #-' ,"':+!;%' 8+!';4'' 85+$ Matlab-/ 8++*+*/$ 8+5",%$ !
&'/+7 '%. 8++1+:1* 8++*&5$ 8+%'3"- "&7'+%$ "'+!1*' "'+:45'1 -, /3! 2''9% "--'. !
.&'7' $!4/ ,"'5'%" &'/+7 ,"'"'#

/$0) -1#2 .2.1

help func_name :$+:45'1 -7 $!(7 -/4- +&.


lookfor keyword 7'&+ #- $%,, $+:45'1 ,13- +&.
helpwin/helpdesk +/+;4#!;5+# $!(7 +-. -/4- +&.
whos $&'/7$ $&,/ 8+%++4 8+5",% $(+# "-/4

Matlab-# (!3#2 !4," .2.2

m +-*$5 .1
.Editor/Debugger-$ 2'-3 )!& "+,75 8":!$' 8".+!7 .Matlab "'&'41 8+7:/% '-# 8+:/4
.workspace-/ #:%5$ 7&+% -7 8+-7'1 – ;-1 8+!+(3% '# ;-4 8+-/4% 85+# – script (#
.F5 +&+ -7 "+,75 0/'4$ ":!$
.$+:45'1- 8++-4'- 8+5",%$ .;-1 8+!+(3%' ;-4 -/4- 8+-'.+ – function (/

mat +-*$5 .2
.8$+.!7' 8+5",% 8+5*.#% '-# 8+:/4

fig +-*$5 .3
.8+1!9 8+5*.#%

UnderWarrior Project http://underwar.livedns.co.il

-4-
underwar@hotmail.com !"# !$%

clc -!32',5 .2.3

.2'-3$ ,#! -# 2%*$ "# $#+/%' command window-$ 2'-3 "# $45% '( $+:45'1

clear -!32',5 .2.4

.workspace-/ 8+!&9'%$ 8+5",%$ -. "# "43'% '( $+:45'1

(!'-6. -,.6 .2.5

.6*'5 '" -.% 8-7"% Matlab !,#. ,$5",% 8, -.- 8+''" 31 &7 8+:4'%
"'-'&9 "'+"'# 2+/ $-+&/% Matlab !,#. ,2'"3" '4' "'+"'# ,8+!1*%% /.!'% "'+$- -'.+ $5",% 8,
."'5;4-

Matlab-# (!$5". .2.6

. 2e5 " 2 #105 - +*467

. i, j " %1 !,#. , 2 $ 3i,3 $ 7 j - &$5423-*4 &$!35-


. inf " & - 8*5%$#
.NaN – !35- #2

Matlab-# /%,#)/ (,!" .2.7

.Ctrl+Q 8+,4%$ 6'!+: '# quit $&'41$ +&+ -7 $,75

UnderWarrior Project http://underwar.livedns.co.il

-5-
underwar@hotmail.com !"# !$%

-,3!$7. -$%4/ .3

-!'%! /$%4/ .3.1

:"'-'4, "'#/$ $/+".$ "'!': .'.'"/ 8+.!7 "/:$' $:+!;% 9'*% $5",% "!&9$ #+$ +$%"$ 1!"01
A=[2, 1, 5; 6, 3, 2] 8+4+*1 .1
A=[215;632] !1*% -. 2+/ 3''! .2

./',+3$ "#:'" "# '/ !'%,"' ans 8,/ $5",% !+&9" $1,$ $5",% 8, !+&95 #- 8# :$!7$
$5",%- ,&3 )!7 *+5."' ans-/ $+$, 8&'4$ )!7$ "# 43%" $1,$ 8, #-- 6*'5 $5",% !+&95 8#
.+'5+, #-- !#,++ ans ,$5",% !+&95 2. 8# .ans

.!',+#- $!:'5, $:+!;%$ "# *+1&$- #- Matlab-- 8'!9" $!', 6'*/ ; (*-$51 !
,m, n :8+!1*% +5, $!+(3% $+:45'1$ .size(A) $&'41$ +&+ -7 +-$$4 1.$!/- 2"*0 +#$.- !
."'&'%7$ !1*% n-' "'!',$ !1*% #'$ m !,#.
.length(b) $&'41$ +&+ -7 !*/4** 9!*# +#$.- !

-,3!$7. -$!3!1 -,!32',5 .3.2

Z=zeros(m, n) :+&+ -7 &$53# 12*): m ' n 1.$!/- +!$.$ !


Z=ones(m, n) :+&+ -7 +*"7# 12*): m ' n 1.$!/- +!$.$ !
:+$%*5)2# 1.$!/- +!$.$ !
.A=[1; 2; 3] :#%9'&- .$:+!;%$ -, 2'*.-#$ #'$, ,A !';4'' "!+:+ .1
.$%+#"%$ "+5'*.-#$ $:+!;%$ "# !:++, – diag(A) $&'41/ ,%",5 .2
.diag(A) +&+ -7 $,7+ A 1.$!/- 2: (*5)2#1 +2,4 !
.,4'/%$ $:+!;%$ -&'9 #'$ n !,#. I = eye(n) :). "+,75 1"$7$ +.$!/- +!$.$ !
."':+!;% a, b, c, d !,#. B=[a b; c d] :). +*.$!/- ++- 1.$!/- +,)!1 !

UnderWarrior Project http://underwar.livedns.co.il

-6-
underwar@hotmail.com !"# !$%

(!#2,) (!+$) 1)# $,72,, -$!3! .3.3

.8+.!& +",/ "+,75 8+/4'7 8+.!7 -7/ !';4'' "!+:+

.X = s:d:f +&+ -7 :+7# 9!"

)!7$ #'$ f-' ,!';4''$ +.!7 "5;4$ '# -'&+9 !';41 #'$ d ,!';4''$ -, +"-3"$$ )!7$ #'$ s !,#.
t%s
.n " $ 1 !,#. X " ( s, s $ d , s $ 2d ,..., s $ (n % 1)d ) :#'$ !:'5$ !';4''$ .+1'*$
d

:"'!7$
.d=1 "7+/4- $$( d !';41 2'+: #-- 8',+! !
.$;% +1-. -9'7+ 8+.!7$ !1*% t * s $ (n % 1) # d 8# !

.linspace "+:45'1 :1$$%: 9!"

)# ,!3# )!7- &7 7'/4 !';41/ (8+5;4 '#) 8+-&9' 8+'*% )!7/ 8+-+3"%, 8+!/+# -7/ !';4'' "!+:+
.+;%';'# 21'#/ /,'3% !';41$' "'':4$ +5, 2+/ 8++':!$ 8+.!7$ !1*% "# 7/'4 ,%",%$ "7.
X = linspace(s, f, n)
.f 2'!3#$ !/+#$' ,s #'$ '/ 2',#!$ !/+#$, 8+!/+# n -7/ !';4'' !':+" '( $&'41

:#%9'&-

>> linspace(2, 10, 8)


ans = 2.0000 3.1429 4.2857 5.4286 6.5714 7.7143
8.8571 10.0000
>> linspace(2, 10, 9)
ans = 2 3 4 5 6 7 8 9 10

UnderWarrior Project http://underwar.livedns.co.il

-7-
underwar@hotmail.com !"# !$%

-,3!$7. 1) -,!31,5!'. .4

/3!$7.1 (!"2%'!& .4.1

2++:% 2',#!$ !/+#$ .8+*4&5+#' "'-'97 8++!9'* "'7:%#/ $:+!;% )'"/ 8+!/+#$% 4-3- ",9- 2"+5
.(1, 1) '5+$ 2'+-7$ +-#%,$ !/+#$ -, *4&5+#$ .$&'%7 !1*% 2++:% +5,$ !/+#$' $!', !1*%

.$++5,$ $&'%7$' "'!',$ -. #+$ A(:, 2) ,-,%- .8+!/+#$ -. #+$ ":" !';!1'#$ -, "'7%,%$
$&+%/' ,A(1, 1:4) +';+/- -'4, A(1, :) +';+/$ ."'&'%7$ -.' $5',#!$ $!',$ #+$ A(1, :)
.A(1, 1:end) +';+/- -'4, 89 #'$ +(# 4 )!'#/ #+$ $:+!;%$'
-7 &+3+ *4&5+# 4+1*% .$&'%7$ "# 89' $!',$ "# 89 ++:- )!': 2+# 8+!';4'' +!/+# -# ",9- +&.
.8$+-# ",9- "5%
,8++-#%+*4%$ $&'%7$ '# $!',$ !1,% "# "+;%';'# $!+(3% *4&5+#$ !';4'' 6'*/ end "/+".
:$,3%$ ."'-'97$ 8++!9'*/ 8%'4+%- 8#"$/

.(2'+5.;/ $,+.!- "5"+5$ Matlab #,'5/ 023 $!*" /"., "!/'3% $3'4- $,3%$$)

UnderWarrior Project http://underwar.livedns.co.il

-8-
underwar@hotmail.com !"# !$%

(!!3!$7. (!$,7$5,& .4.2

Transpose .4.2.1

.A' +&+ -7 $-, Transpose-$ "# -/45 ,A "+,%% $:+!;% !'/7


.$-, Transpose-$ '5+$ A.' '-+#' ,$-, /.'!%$ &'%:$ '5+$ A' ,A "/.'!% $:+!;% !'/7

:#%9'&
>> A = [1 2 3; 4 5 6; 7 8 9]

A =
1 2 3
4 5 6
7 8 9

>> A'

ans =
1 4 7
2 5 8
3 6 9

/3!$7., $12" -.!+" .4.2.2

.$:+!;%$ +!/+#% &3# -. -# !-4*$ "# $1+*'% $:+!;%' !-4* "%+.*

/7''!.$7% #,6!8 .4.2.3

.$-, $;55+%!;&$ "# !+(3" det(A) $&'41$ +(# ,"+7'/+! $:+!;% A +$"

UnderWarrior Project http://underwar.livedns.co.il

-9-
underwar@hotmail.com !"# !$%

/3!$7. 9,5!/ .4.2.4

:$-, "+.1'$$ $:+!;%$ "# #':%- 8+.!& 3 25,+ . n ' n "+7'/+! $:+!;% A +$"
inv(A) .1
A^(-1) .2
eye(n)/A .3

sum -!32',5 .4.2.5

"# $!+(3%' $:+!;% "-/4% #+$ 2+1'-+3- '# ,'+!/+# 8'.* "# $!+(3%' !';4'' "-/4% $+:45'1$
."'&'%7$ 8'.* !';4''

n :!';4'' X
sum( X ) " + X i
i "1

, n n
- :$:+!;% X
sum( X ) " .+ X i ,1 ,..., + X i , n /
0 i "1 i "1 1

cumsum -!32',5 .4.2.6

.!/;:%$ '+!/+# 8'.* #" -./, )!'# '"'#/ !';4'' "# $!+(3%' !';4'' "-/4% $+:45'1$
.!/;:%$ "'&'%7$ 8'.* ":+!;% "# $!+(3%' $:+!;% "-/4% #+$ 2+1'-+3-

UnderWarrior Project http://underwar.livedns.co.il

-10-
underwar@hotmail.com !"# !$%

prod -!32',5 .4.2.7

"# $!+(3%' $:+!;% "-/4% #+$ 2+1'-+3- '# ,'+!/+# "-1.% "# $!+(3%' !';4'' "-/4% $+:45'1$
."'&'%7$ "-1.% !';4''

n :!';4'' X
prod ( X ) " 2 X i
i "1

, n n
- :$:+!;% X
sum( X ) " .2 X i ,1 ,..., 2 X i ,n /
0 i "1 i "1 1

/%,2'/ $,7$5,& .4.2.8

.8+.!7%- "':+!;%$ -, "'7%,%$ "# "'5,% $&'45$ !';!1'# "'7:%#/ "'7:'/%$ "'-'71
.!/+# -'% !/+# $-'71 – 8+.!7% +5, 2+/ $-'71 #+$ "':+!;% +", 2+/ $&'45 "-'71
."+5'/,3$ $-'71$ !';!1'# +51- 84'%% $&'45$ !';!1'#
."'#-'- #-- 8+./'*% 8++;%"% 8++';+/ /,3- 2"+5 $&'45$ !';!1'# "'7:%#/
5
:$#/$ $!':/ "',7+$- -'.+ s " + x x +';+/$ /',+3 ,#%9'&-
x "1

x = 1:5;
s = sum(x.^x);

"# -+1.$- 2+1'-+3- '# ,$&'45$ !';!1'#/ &+%" ,%",$- ,+ !-4*/ !';4'' "4'-3/ :$/',3 $!7$
1
.4-3- 8'4%/ -/ !';4''$
scalar

UnderWarrior Project http://underwar.livedns.co.il

-11-
underwar@hotmail.com !"# !$%

find -!32',5 .4.2.9

==, <, >, ~= (not equal), ~ (not) :Matlab-, &$$0*2 &$!*/!3*#

'"'# "# 8+%++4%, $:+!;%/ *1#% 8+5',$ 8+!/+#$ -. "# $!+(3%' +9'- +#5" "-/4% find $+:45'1$
.+9'- +#5"
.100-% 8+-'&9$ A $:+!;%/ 8+!/#$ -. -, 8+*4&5+#$ "# !+(3" I=find(A>100) $&'41$ -,%-
!1*%/ +'-") (x,y) 8+*4&5+# 9'( '# ("'&'%7$ )!'#- 0!,) $:+!;%/ &+3+ *4&5+# $!+(3% $+:45'1$
.j-/ "'!',$' i-/ "'&'%7$ -, 8+*4&5+#$ "# 8+,+ [I, J] = find(A>100) #%9'&- ,($!(3$ +!;%!1

[I,J] = find(X) :#/$ +';+/$ .0-% "'5', "'!', "!(3$ #'$ find -, 6*'5 ,'%+,
.("'-+-& "':+!;% !'/7 +,'%+,) $:+!;%/ 0-% 8+5',$ 8+!/+#$ -, "'!',$' "'&'%7$ -. "# !+(3%
8-'# ,$:+!;%/ 0-% 8+5',$ 8+!/+#$ -, "'!',$' "'&'%7$ -. "# !+(3% [I,J,V] = find(X) +';+/$
.-"5$ 8+!/+#$ "# -+.%$ +,+-, !';4'' $!+(3% 6*'5/

8"'# 8$ '!(3'+, 8+#"$ .[I,J,V] = find(X~=0) +';+/- [I,J,V] = find(X) 2+/, -&/$- /- 8+,5
."'&3# !';4'' $+$+ +5,$ $!4%/ V -, )!7$ -/# ,8+#"

-!$&!'!1 -,&,,6. -+$). -$!-5 .4.3

. A # x " b :$!':/ $"'# /'".5 .8+%-75 n-' "'#'',% m -, ".!7% $5'"5


:$#/$ $!':/ "'#'',%$ ".!7% "# !'"1- 2"+5' ,"+7'/+! $:+!;% #+$ A (# , n " m -' $&+%/
x=inv(A)*b;

:). 2'!"+1$ "# /,35 ,+7'/+! #-$ $!4%$ !'/7


x=inv(A'*A)*A'*b;

UnderWarrior Project http://underwar.livedns.co.il

-12-
underwar@hotmail.com !"# !$%

-,0,$8. .4.4

.'+&&: +5,% 8++,!9 -7/ ;*4;. "!&9'% "('!3% .&+3+ '" -+.% '/ !/+# -., !';4'' #+$ "('!3%
:#%9'&-
myString = 'Hello, World'

-7 89 "':+!;% -7 "'-7'1, "'+:45'1$ "# -+71$- 89 2"+5' ,"'('!3% -, $:+!;% !':+- 2"+5
."'('!3%
"'', 25+#, "'('!3% !1*%% ,"'('!3% )!7% '# ,$:+!;% !':+- $:!5' $&+%/ :+*6*!7- $)!'- +,$+)
"# &1!" $+:45'1$ .str2mat('one', 'two', 'three') -,%- ,str2mat $+:45'1/ ,%",5 2.!'#/
.$'', "'!', )!'# "-7/ $:+!;% !':+- "5% -7 8+3''!/ !"'+ "'!:4$ "'('!3%$
."'('!3%- 8++!1*% 8+.!7 $!+%% num2str $+:45'1$ :+*6*!7-2 &$!35- +)$31

UnderWarrior Project http://underwar.livedns.co.il

-13-
underwar@hotmail.com !"# !$%

/2!5$4 .5

!11+ .5.1

.6!9$ ;;!',% '/ $4+1!9 2'-3 +;%';'# 21'#/ "!:'+ 8+1!9 "!+:+- $+:45'1 -.
.8&'4$ 6!9$ "4+3%- "%!'9 6!9 8++4 !/. !,#. 8+1!9 "!+:+- $+:45'1 ":!$
$+:45'1$ +&+ -7 ,&3 +1!9 2'-3 3'"1- -.'5 .8+1!9 '-+.+, "'5'-3 !1*% !':+- $:!5 8+/! 8+!4%/
3"1++ 8+!;%!1 #-- figure $&'41$ "# /'".5 8# .n '!1*%, $4+1!9 2'-3 #'$ n !,#. figure(n)
.#/$ +'51$ !1*%$ 87 $4+1!9 2'-3

7,65 :$41 &.4,% .5.2

:#/$ &'4$ "# /'".5


x=0:0.01:5;
y=sin(x).*cos(50*x);
plot(x,y)
title('Example plot')
xlabel('x')
ylabel('y=sin(x).*cos(50*x)')

:$#:'"$

UnderWarrior Project http://underwar.livedns.co.il

-14-
underwar@hotmail.com !"# !$%

/$2# !'#. .6

if -%,25 .6.1

:2-$-. #'$ $-, !+/3"$ ."'!3# "'1,/ if "&'41- $%'& 21'#/ "9$5"% '( $&'41

if (expression),
statement;
elseif (expression),
statement;
elseif expression,
statement;
else,
statement;
end

while -&1,1 .6.2

:!+/3" .$- 2"+5$ +#5"$ 8++4"% &'7 -. ,8+%71 !1*% &'4 7;4 -7 !'(3- "!,1#% while "#-'-

while expression
statements
end

for -&1,1 .6.3

:'5+$ for -, +--. !+/3"$ .8+%71 -, 7'/4 !1*% &'4 7;4 "7:/% for "#-'-
for i = s:d:f
statements
end

!,#. ,"':+!;% -7 "'-+/4% "'+;!&5;* "'+:45'1/ ,%",$-' for "'#-'-/ ,%",$- #- 0-%'%
.$/',3 "'!+$%$
UnderWarrior Project http://underwar.livedns.co.il

-15-
underwar@hotmail.com !"# !$%

-,!32',5 .7

:!+/3" .&+"7/ !('3 ,'%+, 8$/ "',7- "5% -7 "'+:45'1 !'"/ Matlab &'4 +7;4 !+&9$- 2"+5

function <return values> = <function_name>(parameters)


% <function descprition>
<function body>

.$+:45'1$ 8, $%,. '%,, m 0/'4/ $"'# !'%,- '5+-7 $+:45'1 8+!:'+ '535# !,#.

:#%9'&

function [c,d,e]= pyt(a,b)


% returns the hyotensus (yeter) in a right angle
% triangle according to Pythagoras theorem
%c is the hyotensus
% d and e are the two sharp angles
c=sqrt(a.^2+b.^2);
d=atan(b/a);
e=pi/2-d;

UnderWarrior Project http://underwar.livedns.co.il

-16-
Matlab-! "#$%

&"%

'!( )*#+

2002 *$%.+ – 1.0 ,-*)'


Matlab-! "#$%

!"#$"%&'& (&$'

,'.,# &#)%+, ,2+)% +#$)2 &*/("%, 3)-+0,%# 3)40+#4-! &%+5&% &#01& &(/ "), Matlab &(/
.&#".#& 6#&)0! &+2#)%, ,$)$-$# +#5 &#*#/$ 4#2)% &#2.%"$ &0##'% &)&#76
,3, &#*6" &#(/# Matlab 8)$ 3))71*%, 3)!+$,,
"#, Matlab 3/, *#5% ,,/2%! .&#.)*4%# 3)*#45# 3, 3))-)-$, 3)0&/%, Matlab-$ .1
.Matrix Laboratory
./%&/%,% $* &#01& 9-#6, (built-in) &#0$#% &#).50#( !/ $* *(-% .2
toolbox &#)*(-$ 3)*#%/, 3))().(- 3))-+0, 3)%#6&! &#+2#)%, &#).50#( !/ $6*0 *'"% .3
.,%#+% &#").% #!)("# ,5)4-)44- ,,*5$ ,&#0#%& +#$)2 ,&#&#" +#$)2 #%1

').,! 5* "), #7 &*$#6 &*4% .Matlab &(/ !/ $6*, :5),, !1 &" ').,! &*%)&% ,00)" #7 &*$#6
+#%)!, - Matlab-$ ,+)%!! &)&)%", ,4)/, &" */("! )+1 3)/*+0, 3))-)-$, ,0$,,# 2+), &"
.3)$6*0, ,*72, )1)*+% 9*+ )%.2,

.Matlab 6 *#$2 ,$&10 #7 &*$#6

2
Matlab-! "#$%

!"#""#$ %&'(
2........................................................................................................................................ ,).$)4#%# "#$%
6.......................................................................................................................... MATLAB !"#"$ !%&' .1
6........................................................................................................................ 3/#%)/# &#0#!6, 4#*( 1.1
7........................................................................................................................... MATLAB ).$5 )'#- 1.2
7.............................................................................................................. MATLAB !/ HELP-$ /#%)/ 1.3
8................................................................................................................ 3)0&/% &#%/ &2)$5! 3)!!1 1.4
8.......................................................................................................... COMMAND WINDOW-$ ,'#.& 1.5
8...........................................................................................................................MATLAB-$ 3)*(-% 1.6
9............................................................................................................................. MATLAB-% ,").) 1.7
10..................................................................................................................................... !()#%*+ !%,-' .2
10..................................................................................................................................... &)0+) ,*+', 2.1
11.............................................................................................................. !"#$%&' !""()"* $+#,- 2.1.1
11...................................................................................................... !".()/(( $(0".)& '1(2 $1"1& 2.1.2
12................................................................................................................. &#.)*4% &*+',! &#).50#( 2.2
12............................................................................................................... !"134( !"+54 $(0".)& 2.2.1
12...................................................................................................................... $"#(+,'4 -0".)& 2.2.2
13...................................................................................................................... $("4./4 $(0".)& 2.2.3
14...................................................................................................... 3)$5#2 3)1*2 )!2$ 3)*#45# &*).) 2.3
15...................................................................................................................... ARRAY EDITOR 8#!6 2.4
17.......................................................................................................................... !()#%*+ ./ '#).(0#1+ .3
17.......................................................................................................................... &#.)*4%! 3)-5+0)" 3.1
19..................................................................................................................sort-( find $("0/#(5 3.1.1
20......................................................................................................................... 3)).)*4% 3)*#4*(#" 3.2
21............................................................................................................................... -)##"&.)1 3.2.1
21........................................................................................................................... -0".)& 6(5"- 3.2.2
22............................................................................................................................... ,+#50, *#4*(#" 3.3
22.......................................................................................................... -1(/#- .().5(4' !"%(&"% 3.3.1
24......................................................................................................................... !(1# ,%)1- &#).50#( 3.4
25...................................................................... $(0".)&( !".()/( $'5,& $(70&4* !"&(,+ '% !"2(0"" 3.4.1
26..................................................................................................................... prod '5, $"0/#(5 3.4.2
26................................................................................................................................ &#.)*4% !#(1/ 3.5
26....................................................................................................................... repmat $"0/#(5 3.5.1
27....................................................................................................................meshgrid $"0/#(5 3.5.2
29........................................................................................................................... kron $"0/#(5 3.5.2
30............................................................................................................................ ,.)*4% *+- 9#(), 3.6
30.......................................................................................................... &)*"0)! &#"##/% &1*2% &*)&( 3.7
31...................................................................................................................... 3)0&/% &0)24# &*)%/ 3.8
31........................................................................................ COMMAND WINDOW-! 2+)% &'.,# &!$5 3.9
31................................................................................................................................... $(8(.3& 3.9.1
32............................................................................................. Command Window-* 71"& $20- 3.9.2
33........................................................................................... Command Window--& 71"& $'*/ 3.9.3
34............................................................................................................................................. 2,#+ #1"+ .4
34................................................................................................................ 3))+%%-$* 3))*%#0 3)1*2% 4.1
35........................................................................................................................6.7& "1&& "(#"% 4.1.1
37............................................................................................................ (ARRAYS CELL) 3)"& )1*2% 4.2
40......................................................................................................................... STRUCTURE )1*2% 4.3
41............................................................................................................. structure ",.7&' -%"2 4.3.1
43................................................................................................................................... !#,++-(, '3#0%- .5
44..................................................................................................................................PLOT &).50#( 5.1
44................................................................................................................ $(1(/# '% 9.2 )().% 5.1.1
47.................................................................................................................... !"((/ '% 9.2 $."0" 5.1.2
49.......................................................................................................... !"+/'5&(/ !".5+& )().% 5.1.3

3
Matlab-! "#$%

51........................................................................................................ ++#$ :*'$ &#).50#( *(-% 4#4*/ 5.2


51............................................................................ $("0/#(5- ".()/( $4 -'",& -0".)&, .()/( x ."0 5.2.1
52............................................................................$("0/#(5- ".()/(( x "."0 $4 !"20"& !".()/( "1&0 5.2.2
54........................................................................................................................... hold $"0/#(5 5.2.3
55........................................................................................ !(%". $(.(0 '% $(#(.+3( $(#(.$" !(,"+ 5.2.4
56....................................................................................................................... 3)(*'! 3)0#%)- &(-#, 5.3
57................................................................................................................. !"13("& !"#&"+ !(%". 5.3.1
57............................................................................................................................ $.$(, $5+(- 5.3.2
57............................................................................................................ y-( x ."0' $(.$(, $5+(- 5.3.3
57............................................................................................................................. )+/) $5+(- 5.3.4
59..................................................................................................................... 4./& $*"$ $5+(- 5.3.5
59........................................................................................................... $"+"+* -/")+"))+ $5+(- 5.3.6
60..................................................................................................................... !"03( !"((/ $5+(- 5.3.7
60........................................................................................................ !"5.2' !"#(&"+ $5+(- !(,"+ 5.3.8
61........................................................................ $(1(/5 $(70&4* !""5.2 !")/""*(4 '% $(#(,$ $7"*/ 5.3.9
62................................................................................................................................... FILL &).50#( 5.4
63............................................................................................................................. PLOTYY &).50#( 5.5
63............................................................................................................ 3)(*',% &#*)/) 3)1*2 &")*5 5.6
64.............................................................................. SUBPLOT &).50#( &#2.%"$ +6) 3)(*' *(-% &%/, 5.7
66.................................................................................................................................!#,++ !.! '3#0%- .6
66............................................................................................................................. 3))+%% &!& 3)##5 6.1
68......................................................................................................................... 3))+%% &!& 3)64/% 6.2
76....................................................................................................................................... *"&% )##5 6.3
79.........................................................................................................+)6) :*' !2 3)64/% *(-% &'., 6.4
81....................................................................................................................................... 4#0%-" '*#./ .7
81..........................................................................................................................3)(*'$ &)0+) ,4)!/ 7.1
83................................................................................................... HANDLE GRAPHICS !/ 3)45))$#" 7.2
85................................................................... !"5.2 $."0"' $("+"+*- $("0/#(5- $(70&4* !"5.2 $."0" 7.2.1
86........................................................................................ -/"5.2 )/""*(4 '% $(#(,$ $7"*/( $'*/ 7.2.2
88.............................................................................................................. EDITOR/DEBUGGER5(.6 .8
89.................................................................................................................................... &#*2, &*).) 8.1
89...................................................................................................................................... 6&(% &#!% 8.2
89......................................................................................................................... 3)##& &(!6,# &").% 8.3
90.....................................................................................................................(BOOKMARK) ,))0%)- 8.4
90............................................................................................................................ 3))/#%)/ 3)*#.)5 8.5
91...................................................................................................... (FLOW CONTROL) '+#%7 !%3" .9
93......................................................................................................................................... IF &+#5( 9.1
93.................................................................................................................else if-( else $(1(/5 9.1.1
95............................................................................................................................. SWITCH &).50#( 9.2
96.................................................................................................................... CATCH-# TRY &#).50#( 9.3
97.................................................................................................................................. WHILE &"!#! 9.4
98..................................................................................................................................... FOR &#"!#! 9.5
98............................................................................................................................ +/1#"4 .()/( 9.5.1
99........................................................................................ $(4'(' $(70&4* -0".)&* !".*"4 $*0- 9.5.2
101...................................................................................................................................CONTINUE 9.6
102........................................................................................................................................ BREAK 9.7
103......................................................................................................... M. ).$5 &.*,% &%+5#% ,").) 9.8
104................................................................................................................................. !(#)31(0 !"#!& .10
106..................................................................................... NARGCHK ,NARGOUT ,NARGIN &#).50#( 10.1
106..................................................................... (number of input arguments) nargin $"0/#(5 10.1.1
106................................................................. (number of output arguments) nargout $"0/#(5 10.1.2
108................................................................................................................. nargchk $"0/#(5 10.1.3
109................................................................................................................... &#).50#( !/ &#).50#( 10.2
109......................................................................... feval $"0/#(5 $(70&4* :&% +"7 $("0/#(5 7(0"* 10.2.1
112....................................................... !"#$%& '% %4.& .12(& "$'* .5+& $('*/&- $("0/#(5 $.12- 10.2.2

4
Matlab-! "#$%

120.......................................................................................................................... 3))!$#!' 3)0&/% 10.3


121................................................................................................................................ &#).50#( && 10.4
122......................................................................................................................... TOOLBOX &*).) 10.5
123.............................................................................................(DEBUGGING) !(8#-/ 5(3#!( !8#)+ .11
129.............................................................................................................. MATLAB-" 4#2()#" %(0#/ 12
129.................................................................................................................................. PROFILER 12.1
131..................................................................................................................... !(+,3!+ !(#0%- !(.(20 .13
131................................................................................................................................ 3)4*- &*).) 13.1
134........................................................................................ GRAPHICAL USER INTERFACE)) GUI 13.2
135.................................................................................................................. :('3- '1(2 $7"*/ 13.2.1
136....................................................................................................................... !"*",. $5+(- 13.2.2
136.................................................................................................................... !")/""*(4 .(%"" 13.2.3
137..................................................................................................... !")/""*(4 '% $(#(,$ $7"*/ 13.2.4
140.......................................................................................................................... GUI $(#,$ 13.2.5
143......................................................................................................................................... 4#61(+ !+#/%
146.................................................................................................................................................... !(%(3+

5
Matlab-! "#$%

Matlab ()")* (+&, .1


/+6 .m ;$#5 &6)&(
Editor/Debugger 8#!6$ ,*72, 5/%% !#6&"

,+#5(, &*#/ &)61#0 ,)*(- )#0)/


(command line)

,+#$2, $6*%

!1 9#&$ 3)!)% *6" /#()6


&)61#0, ,)*(-$ .m ).$5

$%&'($ )'*'+ :1.1 !"#

)*&'"*& +&,&-.! %&/0 1.1

*$-, 8#!6
&%+#5, ,+#5(, &" !$5! 8&)0 .Enter !2 ,.)6!# 8&+!5, )"2 &#+#5( &.*, Command Window
-$ 3#/)*, &5)6% .,!2%! *#&(1 !2 ,.)6! )"2 command line-$
.clc &).50#( &#2.%"$ &2.#$% Command Window
.3,)$'! 2+)%# #*+'#,/ 3)0&/%, !1 &" ,!)1%, ,+#$2, $6*% &'#.& Workspace
!/ )!"%/, *#&(1 !2 ,!#(1 ,.)6! .&)61#0, ,))*(-$ 3).$5, !1 4#*( Current Directory
:,')/% 3)"$, 3).$5, !2 !"0, 8#!6$ *$12,
.Editor/Debugger 8#!6$ ;$#5, &6)&( - .m ;$#5
.Workspace-! 3)0&/%, &0)24 -.mat ;$#5
.,5)(*', 8#!6 &6)&( - .fig ;$#5
8(#"$ 8&*$2,! &#*/("# Command Window-$ #+!5#,/ &#+#5( +#2)& Command History
.Command Window-! )0+)
.3)(-#0 3)*72# ,demos ,,*72 )1%-%! ,*),% ,/)' Launch Pad

./&0*/& )&1&,2$ -&"3 :1.1 $,'-

6
Matlab-! "#$%

Matlab "#$1 "2&3 1.2

,Matlab-$ 3).$5 !/ 3)'#- *(-% 30/)


.m &%#)- 32 3).$5 .1
8#!6 9*+ &#2.#$% 3&.*,# 3&1)*2 .Matlab &#+#5( 3)2.$% #!" 3).$5
.Editor/Debugger
,.m ).$5 )'#- )0/ 3)%))5
".%0, 2+)% !2 3)!2#( 3, .4!( 3)*)76% #" 4!5 3)!$5% 30)" #!" 3).$5 :script ."
;$#5, &.*, .Command Window-$ &#+#5(, &+!5,! !#5/ .Workspace-$
.( ,4% )(!1 ;6, *#&(1 !2 ,.)6! )"2 #") F5 !2 ,.)6! )"2 &)/20
3, 3))%)0(, 3)0&/%, !1 .4!( *)76,!# 4!5 !$5! 3)!#1) #!" 3).$5 :function .$
function 6&(% &!)%$ 3)/%&/% ,).50#(1 3).$5 *)+',! )+1 .,).50#(! 3))!5#!
.8 5*($ *$-#)/ )(1
.mat &%#)- 32 3).$5 .2
.3,)1*2# 3)0&/% 3)0-1"% #!" 3).$5
.fig &%#)- 32 3).$5 .3
.3)(*' 3)0-1"% #!" 3).$5

Matlab -* help-$ *&'"* 1.3

,,*72, 5/%% &#2.%"$ &#).50#(# 3)"/#0 )$'! ,*72 /(6! 8&)0

,!)% *6" /#()6


,*72, ).$5$

;$#5 &'#.&
8%#-%, ,*72,

!1
).$5
,*72,
3)!)1%,
&!)% &"
/#()6,
,*72, ;$#5
8%#-%,

help 4&,2 :1.2 !"#

7
Matlab-! "#$%

,,).50#( )$'! 3)*$-, !$5! )+1 3)1*+ *(-% &#%)5

2#+) "! ,).50#(, 3/ 2#+) ,).50#(, 3/


-$ ').% 6&(% &!)%# lookfor &$)&1 Command -$ ').% ,).50#(, 3/# help &$)&1
&#).50#(, !1 &%)/* Command Window 8(#"# ,).50#(, &!#2( )$'! *$-, Window
.6&(% &!)%! */5 8,! /)/ .,$ /#%)/,
1
search &)).(#" &#2.%"$ ,*72, 5/%%$ /#()6 ,*72, 5/%% &" 6&#( ,).50#(, 3/# doc &$)&1
. !)1% help &)).(#" &%#2! .!"0, ,).50#($
3)(*' 3' :-#0$# 3)4*#(% *&#) 3)*$-,
.&#"6-#0#

3)".%0 #!" 3)*(- .#!/ 3)-toolbox-$# Matlab-$ /#%)/ )$'! ,1*+, )*(- !/ !#+' *(-% 3))5
.Launch Pad 8#!6$ pdf 4%*#($ #" ,*72, 5/%%$ contents &)).(#" 9*+ html &-*'$
3)-toolbox-, !/ 3))*/(" 3)%#/))$# Matlab-$ /#%)/ 3)*)$-%, $6* demos *'"% 3))5 :-#0$
.#!/

)",+*' +&'* +4"$1- )"--5 1.4

$))6 3/ !1 .:-#0 #& !1% 3!2&% Matlab */"1 ,3)0&/% &#%/! 3)##& 31-! +2 3).5#% Matlab-$
.3/$ 3)6##* #),)/ *#-" .'_' underscore )##&# 3)*(-% ,&#)&#" !#!1! !#1) 3/ .&#"$ !)6&,!
.3)0#/ &#%/ 3, Name-# name 3/, 81!# case sensitive "#, Matlab
:&#%/ 8&%! &#)*!#(#( &#!#1-" )&/ &#%))5 .3)!)% *(-% !/ $#!)/ 3, &#%/ &#$* 3)&)2!
3"&,$ MaxForce #" max_force $&1)& max force !/%! .&#!#+' &#)&#"$ /#%)/# underscore
.)/)" 324!

Command Window-$ !2&#+ 1.5

,8,% /#!/ 5* ').0 9" &#$* &#).(#" &#%))5 .format &).50#( &#2.%"$ ,'#.&, &" &#0/! 8&)0
.3)6##*, $#*% *4(0, -#6+ *&#) '#.)) :format compact.1
.format compact &" !4$% ,/2%! .3)6##*% !2$ '#.)) :format loose .2
.3))!0#).* 3)*(-%1 3)*(-% '#.)) :format rat .3

.format short #-)(+, &#*(- 5 !/ )4*+04-, '#.))! *#76! )+1


.3)0&/%, &*)%/ 8(#" !2 3)2)(/% 30)"# ,'#.&$ 5* 3, !"0, 3))#0)/, !1 )1 *#170

Matlab-$ )"/03' 1.6

,576, *#45( 8)).! )+1 e &#"$ /%&/%, )2+%, 3#/)*$ 3' *)1% Matlab )0#*/2, 3#/)*! :-#0$
. 6 !10"2 "#, 6e-2 -# 2 !105 "#, 2e5 *%#!1 .&)0#*/2,
3)-5!(%#5 3)*(-% 3, 2+4i, 1-3j "%'#+! .j #" i &#", &#2.%"$ 3)0)#.% 3)-5!(%#5 3)*(-%
.Matlab-$ 3))5#6

-! &#1))/ "!/ &#).50#( 3' ,').%, lookfor &).50#( &%#2! ,3)-toolbox-$ &#2)(#%, &#).50#( 5* ,').% #7 &#*/("1
.3)-toolbox

8
Matlab-! "#$%

,Matlab-$ 3)$#/6# 3)2#$5 3)*(-% 3)%))5

.realmax -% !#+', 9*2$ #" -("$ *(-% &5#!6% !$5&% Inf 9*2,
I nf 0
. #" ,5#!6,% !$5&% NaN 9*2,
Inf 0

Matlab-' !("#" 1.7

!2 ,.)6! )"2 #" Command Window-$ quit &$)&1 )"2 &2.#$% Matlab-% &*+#-% ,").)
.Ctrl+Q

9
Matlab-! "#$%

('-"+./ (+01, .2
+",6" !/62! 2.1

.&#.)*4% !/ )4*( ,*5%1 3)*!5-# 3)*#45# !2 $#/6! 8&)0 .,.)*4% "#, Matlab-$ )-)-$, ,0&/%,
,)!!1 8(#"$ &*+'#%# 3)1*2 !/ )0$!% 9*2% "), ,.)*4%

# a11 a12 ! a1n $


%a a 22 ! a 2n &&
A ' % 21
% " " # " &
% &
%( a m1 a m2 ! a mn &)
. * m , n + ,.)*4% "), A *%#!1

3)%6#&% ,.)*4%, )*$)" .,1#&$ 3)1*2 &$.,# ,.)*4% &*).) "), Matlab-$ *&#)$ &)-)-$, ,!#2(,
.&#*#/, 8)$ +)*(% ";" 8%)-# ,.)*4%, !/ ,*#/, )*$)" 8)$ +)*(% "," 8%)- ,&#2$#*% 3))*'#- 9#&$
,"$, 8(#"$ A ,.)*4%, &" *)+'0 !/%!

,*#/)", 2)(#) ,!#2(, 8#!6$

.,0&/%, '#- &" Matlab-$ /"*% *)+',! 9*#. 8)" )1 $! 3)/0


.,*#/$ *$)" !1 8)$ 6##* &%/, "), ,.)*4% !/ ,*#/, )*$)" 8)$ +)*(,! &(-#0 9*+
,,+#5(, *%#!1

.5#)+$ ,.)*4% ,&#" &" &*.#)

2)(#% &21 .3))61#0, 3)0&/%, !1 !/ 3)!+',# &#%/, 3)2)(#% (Workspace) ,+#$2, $6*%$
. * 2 , 3+ !+#'$ ,.)*4% "#,/ A 3/$ ,0&/%

?$/#6%, 9*2, $.#) #)!" ,0&/% 3/ "!! 9" ,0&/% *)+'0 */"1 ,*#5 ,%
,0&/% *+'#) 3" .,0&/%, 9*2 $.#% #1#&!/ ans 3/$ ,0&/% *)+'% &)4%#4#" Matlab ,71 ,*5%$
.56%) 3+#5, 9*2,# ans "*5) 3' ,7 ,0&/% ,3/ "!! :-#0
,"%'#+!

.A ,.)*4%! ,,7, ans 3/$ * 2 , 3+ ,.)*4% ,*.#0

10
Matlab-! "#$%

:)-#0 ,+#5(, :#-$ 81!# 3)1*2, &$., &!#2( *6"! ,.)*4%, 81#& &" &#"*! ,.*0 "! 3)$* 3)*5%$
,,+#5(, *%#!1 .";"

.,.)*4%, 81#& &" ,').% ,00)" 9" A ,.)*4% ,&#" &" &*.#)

!"#(2/3 !""'.") (*#&, 2.1.1

.$#/)6 3)/*#+, 3))#4)$ 3' &#),! 3)!#1) ,.)*4% )*$)" ,3)*(-%! :-#0$
,"%'#+!

v - *3 49 9 7 8 2+

32 *#45# *)+',! 3).#* 32 81! .3))!#$%)- 3)#4)$$ "!# 3)*(-%$ 5-#2 Matlab )1 *#17! $#/6
,#%1 3)!!1 3))#4)$

.)*(-% 9*2$ #!6&#" (w, x, y, z) 3)0&/%, !1 3" "!" !"0, )#4)$$ *)1% #00)" Matlab 7"

!"+'.4'' ('-"+./ 30'1 (0"0/ 2.1.2

&#*#/, *(-% ,)*5) ,.)*4%, )+%% &" !$5! )+1 . * 2 , 3+ ,.)*4% "), #0*+',/ A ,.)*4%, *#171
,"$, 8(#"$ size ,).50#($ /%&/0 (&#+#%2, *(-%#

m-2
n -3

8&)0 (,*#/ #" ,+#%2 *#45# "#, *#45#, 3" ,0/% ,7 8)") *#45# 9*#" +#+%! )+1 ,&"7 &%#2!
,"$, 8(#"$ length ,).50#($ /%&/,!

L-3

)1 size -# length &#).50#($ /%&/,! )#.* 8))+2 ,,.)*4%, !+#' #" *#45##, 9*#" #0! 2#+) 3" 3'
.*&#) )!!1 ,*5%! &#$6*#% )().(- ,*5% *#$2 &#$&10, &#+#5( &#$* 3)&)2!

11
Matlab-! "#$%

+&#"/%' +/62!- +&"#1,&0 2.2

.&#+6#)% &#.)*4% &#*.#), &#).50#( *(-% &#%))5 &#.)*4% !/ &)0+), ,*+',! :-#0$

!"056' !"*76 ('-"+./ 2.2.1

.,.)*4% !#6&"! &#.#(0 &#.)*4% 8, 3)+6"# 3)-(" &#.)*4%


,3)-(" 3, ,)*$)" !1/ * m , n + ,.)*4% &*).)

,3)+6" 3, ,)*$)" !1/ * m , n + ,.)*4% &*).)

("#'*&36 ,-"+./ 2.2.2

8#-1!"! ;#6% 3)*$)", !1$ 3)-(" &!2$1 &*+'#%, * n , n + &)2#$)* ,.)*4% "), &)0#-1!" ,.)*4%
,)/"*,
# a11 0 ! 0 $
%0 a # " &&
A'% 22

%" # # 0 &
% &
%( 0 ! 0 a nn &)

4!51 &!$5% #7 ,).50#( .3)-(", !1 &$)&1 "!! diag &).50#( &#2.%"$ &)0#-1!" ,.)*4% *.)! 8&)0
.3"&,$ &)0#-1!" ,.)*4% *)76%# *#45# *#&$ )/"*, 8#-1!", &"
,!"0, &#+#5(, "%'#+!

,,.)*4%, &" &#*.))%


#7 0 0 0$
%0 1 0 0 &&
B-%
%0 0 "3 0&
% &
(0 0 0 8)

B &)2#$)*, ,.)*4%,% 3))0#-1!", 3)*$)", &" ").#,! *%#!1 ,,1#(,, ,!#2(, &" 2.$! 8&)0
,"$, 8(#"$ ,).50#( ,&#" &#2.%"$

&.)*4% &" *.))! )+1 .,+)6), &.)*4% "#, &)0#-1!" ,.)*4% !/ )/#%)/ )4*( ,*5%
,"$, 8(#"$ eye ,).50#($ 3)/%&/% I[n , n] ,+)6),

12
Matlab-! "#$%

('"6+46 ('-"+./ 2.2.3

rand &).50#(
. U[0,1] ,+)6" &#!(&,% 3)!*'#%, 3)1*2 &!2$ ,.)*4% &*.))% #7 ,).50#(
,"#, ,!/ )!!1, ,0$%,

randn &).50#(
*%#!1 . N (0,1) (&)!%*#0) &)-#"' &#!(&,% 3)!*'#%, 3)1*2 &!2$ ,.)*4% &*.))% #7 ,).50#(
.1 -0")*## ,-(" 2.#%%
,"#, ,!/ )!!1, ,0$%,

13
Matlab-! "#$%

)"$1&4 )"5/4 "-4$ )"/&%1& +/"#" 2.3

.3)$5#2 3)1*2 )!2$ 3)*#45# &*).)! 3)1*+ )&/ &#%))5


. s : d : f 3#/)*, .1
9*2! +2 (d) *+'#% *#45($ (3)045 #") 3)!+'# (s) 3)#-% 9*2% 3)!)6&%/ 3)*$)" !2$ *#45# &*).)
,&$)&1 *%#!1 .(f) 3#)-
x ' s:d:f
,*/"1
)&!6&, 9*2 -s
,045, #" !#+)' *#45( -d
)(#- 9*2 -f

,*#45#, &" *.#)


x ' *s s . d s . 2d ! s . (n " 1)d +

,,),) &#+#50, *(-% ,&)/2% .*+'#% "! (n) &#+#50, *(-% )1 $! 3)/0
f "s
n' .1
d
,"%'#+

x - *3 5 7 9+
9"3
.n ' . 1 ' 4 "#, &#+#50, *(-%#
2
,"%'#+

x - *10 7 4 1+
1 " 10
.n ' . 1 ' 4 "#, &#+#50, *(-%#
"3

!(%2'
.d=1 ,2)$5! ,,7 d *#45(, 8#). "!! 3#/)* -
,"%'#+

x - *3 4 5 6 7 +
? f / s . (n " 1)d */"1 ,*#5 ,% -
,,4% )(!1 !'#2) &#+#50, *(-% ,71 ,*5%$
%f "s &
n$ ' % . 1&
( d )
,,),) !$5&)/ )(#-, 9*2, 7"#
f$ ' s . (n$ " 1)d
,*#45#, *.##)) ,/2%! *%#!1
x ' *s s . d s . 2d ! s . (n$ " 1)d +
,"%'#+!

14
Matlab-! "#$%

% f " s & % 8.999 " 1 &


n$ ' % . 1& ' % . 1& ' %( 4.9995&) ' 4
( d ) ( 2 )
$f ' s . (n$ " 1)d ' 1 . (4 " 1) ! 2 ' 7
,*#45#, *.#0 *%#!1
x - *1 3 5 7 +
.#71 ,!5& !/ &#*/(" 20#% linspace &).50#($ /#%)/,

linspace &).50#( .2
9" ,*6" 9*2! +2 2#$5 *#45($ (3)045 #") 3)!+'# 3)#-% 9*2% 3)!)6&%/ 3)*$)" !2$ *#45# &*).)
.)4%#4#" 8(#"$ $/#6% *#45(,# &##.5, )1*2 )0/ 8)$ 3))#.*, 3)1*2, *(-% &" 2$#5 /%&/%, &21
,*%#!1
x ' linspace 0 s, f , n 1
f "s
,"$, 23)##/, 3)6##*%, !2$ *#45#, *.#0 *%#!1 . d ' *#45( !2$ n 9*#"$ *#45# *#.)
n "1
x ' #(s s . d s . 2d % s . 0 n " 11 d $)
,"%'#+

10
,%)'+ 6##*%$ 100-! 0 8)$ 3)1*2 !2$ 100 9*#"$ *#45# *.#)
.
99
.$#/6 #0)" n 3)*$)", *(-% #!)"# $#/6 #" 2#+) d *#45(, */"1 8#/"*, 3#/)*$ /%&/,! ;!%#%
d *#45(, #!)"# ,$#/6 #" 2#+) n 3)*$)", *(-% */"1 )0/, 3#/)*$ /%&/,! ;!%#% ,&"7 &%#2!
.$#/6 #0)"

Array Editor 7&-. 2.4

,.)*4%, !2 *$12, !/ )!"%/, *#&(1, !2 3))%2( ;#6!! "), &#.)*4% )*$)"$ 80#$&,! ,6#0 9*+
!2 ,!#(1 ,.)6! "%'#+! .Array Editor 8#!6$ 2)(#& ,.)*4%,# Workspace-, 8#!6$ ,)#.*,
.2.1:*'$ 8#!6, &" 6&#( A,B,I,L &#.)*4%

&#0/! 8&)0 :-#0$ .,%)"&%, &)0#/!, !2 ,.)6! )"2 Array Editor-$ ,0&/%! ,0&/% 8)$ *#$2! 8&)0
.)0+) 8(#"$ ,.)*4%, )*$)" )1*2 &"

.3)-5+0)"$ /#%)/# ,$., &!#2( &#2.%"$ "), ,.)*4%$ 3)*$)" &#0/! &(-#0 9*+

,"$, 8(#"$ logspace ,).50#($ 3)/%&/% &)%&)*'#! ,!"5-$ 3)##/ 3)6##*% !2$ *#45# *#.)! )+12
. x ' #(10
s
10s . d 10s . 2d ! 10f $) "#, *.#))/ *#45#,# x ' log space(s, f , n)

15
Matlab-! "#$%

Array Editor 4&,2 :2.1 !"#

16
Matlab-! "#$%

('-"+./ 32 ,"-3'7"#/ .3
+&#"/%'- )"316,"( 3.1

8)).% 8#/"*, *$)", .3)-5+0)"# &#!#'2 3))*'#- &#2.%"$ ,.)*4% 9#&$ 3)*$)",% 5!6! &/'! 8&)0
.3,+#%2 *(-% 8)).% )0/, *$)",# ,*#/ *(-%
,3, ,.)*4%$ 3)-5+0)",

$5*"-0' .*+6%1*7 :3.1 !"#

,"%'#+!

# 1 1 1 "2 $
A - %% 1 8 6 0 &&
%( "1 2 5 8 &)

A(1:2,2:4)

# 1 1 1 "2 $
A ' %% 1 8 6 0 &&
%( "1 2 5 8 &)

A(3,1) A(:,2)

A(1,:) &#2%/% .,)0/, ,+#%2,# &#*#/, !1 "), A(:,2) *%#!1 .3)*$)", !1 "), ":" !/ &#2%/%,
)#4)$! #" A(1,1:4) )#4)$! !#5/ A(1,:) )#4)$, *%#!1 .&#+#%2, !1# ,0#/"*, ,*#/, "),
.4A(1,1:end)

.3))#.*, 3)*$)"$ ,$., &!#2( &#2.%"$ ,.)*4%, 9#&$ 3)1*2 &#0/! 8&)0
,"%'#+
,,.)*4%$ +)6) *$)" &(!6,

.+)6) -5+0)" 5)(-% ,,+#%2, &" 3'# ,*#/, &" 3' &" 8)).! 9*#. 8)" 3)*#45# )*$)"! &/'! )+13
3))*'#-$ 3#5)%! 3"&,$) 3))!%)-1%, ,+#%2, #" ,*#/, *(-% &" ,*)76% &)4%#4#" -5+0)", *#45# :#-$ end &$)&14
.(&#!#'2,

17
Matlab-! "#$%

,"%'#+
,,.)*4%$ ,.)*4%-&& &(!6,

,"%'#+
,*!5- &$., &!#2( )"2 ,.)*4% &&$ ,,7 9*2 &$.,

.[] &#5)* &#2$#*% 3)*'#- - ,5)* ,.)*4% 8#%)- &$., )"2 &#+#%2 #" &#*#/% *4(),! 3' 8&)0
,"%'#+!
,,.)*4% *#.)0 ,/6%, 3/! .,.)*4% !/ ,*#/ +)*#,! #00#.*$ )1 6)00

#17 24 1 8 15 $
% 23 5 7 14 16 &
% &
A - % 4 6 13 20 22 &
% &
%10 12 19 21 3 &
%(11 18 25 2 9 &)

,"$, 8(#"$ ,0%% *4()0 .#00)2$ 86 &".#% ,00)" &)/)!/, ,*#/, )1 6)00

#17 24 1 8 15$
% 23 5 7 14 16 &
A-% &
%10 12 19 21 3 &
% &
(11 18 25 2 9 )

.,")'/ &2+#, !$5&& ,,.)*4%, &#!#$'! ;#6% ".%0, 9*2$ /%&/,! ,-00 3"
,"%'#+!

"!/ 3)/+6, 3)*$)"$/ 91 $6*#& "), 7" ,.)*4%, &#!#$'! ;#6% 9*2 $).,! ,-00 3" ,&"7 &%#2!
.3)-(" #),) #*+'#,
,"%'#+!

#17 24 1 8 15 0 $
% 23 5 7 14 16 0 &
% &
A - %10 12 19 21 3 0 &
% &
%11 18 25 2 9 0 &
%( 0 0 0 0 0 2 &)

18
Matlab-! "#$%

sort-' find ('"-4#'7 3.1.1

&).50#( ,!+6% &*)*$ *#&$ .53)*#45# #" &#.)*4% 9#&$ 3)%)#-% 3)1*2 &").%! ,+2#0 find &).50#(
)!!1, 3#/)*, &#.)*4% *#$2 .-("% 3)0#/, ,.)*4%$ 3)*$)", !1 !/ 3)-5+0)", &" ,*)76% find
,"#, ,).50#(, !/

,"#, 3#/)*, 3)*#45# *#$2#

,&#.)*4%! "%'#+
,A ,.)*4% *)+'0

# 0 0 0 3 0$
A - %% 0 0 0 0 0 &&
%( "2 0 0 0 0 &)

.3))#.*, 3)*$)", 9*2 &" 3' *)76,! find &).50#(! &%*#' )/)!/ 40%#'*" &(-#,
,"%'#+!

,3)*#45#! "%'#+

.,5)* ,.)*4% ,*)76% ,).50#(, 7" 3)/5#$%, 3)1*2, 3)".%0 "! 3"5

19
Matlab-! "#$%

a - * 0 0 0 0 8 0 0 "3 0+
index - [5,8]

,a *#45#$ -("% 3)0#/, 3)*$)", !1 &" !)1%, *#45# !$5! "#, ,7 -5+0)" !/ )*/(" /#%)/

a _ no _ zero - [8, "3]


,)45(%#5 *&#) 3#/)*$ #"

&#2.%"$ 3)*6" 3)40%!" *#6$! 8&)0 .-("% 3)0#/, 3)*$)" &").%! 5* &!$'#% ,00)" find &).50#(
,find-, &).50#( !/ 3))*'#-$ )#.* )"0& &$)&1

index - [10 11 12 13]

3,)*$)"/ 91 3)*#45# /+6% &*+-%, sort &).50#( "), 3)-5+0)" &!$5! &(-#0 &)/#%)/ ,).50#(
./+6% *#+)-, 2#.)$! /%/%, -5+0)", &"# !#+'! +2 845,% #),)
,"%'#+!

m _ sorted - * "5 "3 0 1 5 7 93+


index - * 4 3 6 5 1 7 2+

)""#"/%' )"/&%/0&( 3.2

,' 8%)-, &#2.%"$ &2.#$% (,.)*4% &#+#%2# &#*#/ 8)$ :#!)6) 6transpose &!#2(

# 2 3$
C - %% 1 8 &&
%( 5 4 &)

"*","-","+" 3)*#4*(#", &#2.%"$ &#.)*4% !(1# *#-)6 ,*#$)6 !/ &#)-)-$ &#!#2( 2.$! 8&)0
.&)*"0)!, ,*$'!", )5#6! 3"&,$
,&#.)*4% *#$)6

#10 6 6 $
C-% &
( 3 17 6 )
,&#.)*4% !(1

transpose 2.$! )+1 9" ,2)(/% ,7 8)" &#)/%% &#.)*4% *#$2 .)-5!(%#5 +#%. 3'# transpose 2.$% ' /*', *#4*(#"6
.(.') #)0(! ,+#50 32 /*', *#4*(#"$ 3)/%&/% )-5!(%#5 +#%. "!!

20
Matlab-! "#$%

#16 37 8 $
C - %% 8 77 17 &&
%( 40 61 13&)

,,.)*4%$ *!5- !(1

#16 10 2 $
C-% &
( 0 18 4 )

,!#2( &)*"0)! ,*$'!"$# *6"% Matlab-$ &)5#6 ,00)" 3)0#/ 3)+%% &#!2$ &#.)*4% )&/ 8)$ ,%)1-
,*5%$ .*!5-! ,.)*4% 8)$ ,%)1- -+)6) !!1,% ".#) ,*5% 3))5 Matlab-$ !$" .&*+'#% ,00)" #71
.,.)*4%, )*$)" !1 !2 ,%)1-! ,!#5/ !"0, ,!#2(, ,71
,"%'#+!

# 7 4 0$
C-% &
( "1 8 1 )

,.##"/+.0 3.2.1

.det &).50#( &#2.%"$ &)2#$)* ,.)*4% !/ ,400)%*4+, &" $/6! 8&)0


,"%'#+!

ans - "5

,-"+./ 8'7", 3.2.2

,&#0#/ 3)1*+ )&/$ A[n , n] 7&)2#$)* ,.)*4% 9#(,! 8&)0


.^-1 *#4*(#" #" inv &).50#( .1
,"%'#+!

#"

,#&".#&/ ,.)*4% &1)(, 3&)*#'!" #&#" &" 3)!)2(%


# "0.6 0.4 $
A-% &
( 0.8 "0.2 )
.,+)6) &.)*4% &#2.%"$# / *#4*(#" .2
,"%'#+!

. det(A) / 0 -/ )"0&$ 7

21
Matlab-! "#$%

!6&1,! /&%/0&( 3.3

.3)1*2%! &#.)*4%, !/ &#2%/%, &" &#0/% ,/2%! ,+#50, *#4*(#" &#2.%"$ &#2.#$%, &#!#2(
.*$)" !#% *$)" ,!#2( -3)1*2% )0/ 8)$ ,!#2( "), &#.)*4% )&/ 8)$ ,+#50 &!#2( *%#!1
,&#)!!1 &#.)*4% )&/ *)+'0 ,/6%, 82%!

# a11 a12 a13 a14 $


A ' %% a 21 a 22 a 23 a 24 &&
%( a 31 a 32 a 33 a 34 &)
# b11 b12 b13 b14 $
B ' %% b 21 b 22 b 23 b 24 &&
%( b31 b32 b33 b34 &)
,!(1
# a11 ! b11 a12 ! b12 a13 ! b13 a14 ! b14 $
C ' A.* B ' %%a 21 ! b 21 a 22 ! b 22 a 23 ! b 23 a 24 ! b 24 &&
%( a 31 ! b31 a 32 ! b32 a 33 ! b33 a 34 ! b34 &)
,5#!)6
# a11 b11 a12 b12 a13 b13 a14 b14 $
C ' A./ B ' %%a 21 b 21 a 22 b 22 a 23 b 23 a 24 b 24 &&
%( a 31 b31 a 32 b32 a 33 b33 a 34 b34 &)
,,576
#a 11
b11
a12 b12
a13 b13
a14 b14
$
% &
C ' A. ^ B ' %a 21
b 21
a 22 b22 a 23b23 a 24 b24 &
%a b31
a 32 b32 a 33b33 a 34 b34 &)
( 31

!"#$%
.&)0#$/6, ,!#2(, *#4*(#" )0(! 35#%% ,+#50, *#4*(#" -
.,0#/ "), ,+#50, *#4*(#" "!! 5#!)6,# !(1, &#2%/% &#)2#$)* &#.)*4% *#$2 -

,*&#)% ,+#50, *#4*(#" 3)"$, 3)*5%, *#$2


.*#-)6 #" *#$)6 .1
.(*!5-$ 5#!)6 #" !(1 &#2.%"$ 2#.)$! 8&)0) 2#$5 #&#"$ ,.)*4%, )*$)" !1 5#!)6 #" !(1 .2

,0'4#, +'.+7'63 !"2'/"2 3.3.1

*#4*(#" 7" (2#$5, "#, b #" 2#$5, "#, a 3" 8)$) 2#$5 *(-% 32 ( a b ) ,576, &!#2( &" 2.$! )+1
.)6*1, ,+#50,
,"%'#+

22
Matlab-! "#$%

y - * 2 4 8 16 32 64 128+
,"%'#+

y - *1 4 9 16 25 36 49+

.&#"!#! !1 "!! 3)1$#-% 3))4%&% 3))#4)$ $/6! 8&)0 ,+#50, *#4*(#" &#2.%"$

,"%'#+
5
,8&$)&1 )"2 2.#$% s ' 2 x x )#4)$, $#/)6
x '1

s - 3413

,"%'#+
)#4)$, &" $/6! /*+0# A,B,C ()2#$)* 6*1,$ "!) !+#' #&#" &#!2$ &#.)*4% 3 &#0#&0
3
3 sin(A) 4
.f ' 5 6 .1
7 B^C 8

,/#%)%,

3)$* &#'#7! 5#57 )#4)$ &#$* 3)&)2! .3))*'#-, &))2$ "), Matlab-$ &#.#(0 )1, &#)2$, &6"
"#, *&#) *#%6 ,*5% .,")'/ &2+#, !$5&&# *'#- -(-(! &#!5$ 8&)0 81!# 3))*'#- !/ 3)!$!$%#
$/#6) 9" ,")'/ &2+#, !$5&& "! 7"# 8#10 "! 3#5)%$ *'#- #0$., 9" 8)5& 3)*'#-, *(-% */"1
.)#.* "! )#4)$

8)" 3" .*'#-! &6&% 8%-, &77, )"2 3))*'#-, &))2$ !2 *$'&,! 8&)0 Editor\Debugger 8#!6$
)&6& #5 *))#.% 7" 3)!/% *'#- #! 3))5 3" .#1*+ )5(#" #5 44*#/% &)0%7 7" 3)!/% *'#- *'#-!
.#!/ 3)!/%, *'#-,# 8%#-%, *'#-! &6&%

.3.4 5*(! #0( 3)4*(! .*#45# )*$)" !2 ,%)1- &2.$% sum &).50#(8

23
Matlab-! "#$%

-05& !'"53 +&"#1,&0 3.4

,,%)1- &#).50#( )&/ &#%))5


sum &).50#( .1
,*#45#, )*$)" 3#1- &" !)1%, *!5- ,*)76%# *#45# &!$5% #7 ,).50#(
n
s ' sum(x) ' 2 x i
i '1
,"%'#+!

s - 10

,".#&, &*76,# ,.)*4%, !/ &#+#%2, 9*#"! ,%)1- )"2 &#.)*4% !2 &!2#( #7 ,).50#(
.&#+#%2, *(-%1 #1*#"/ *#45#!
,3)"$, 3)1*2, &" ,*)76% sum &).50#( X * m , n + ,.)*4% *#$2 )!!1 8(#"$

# x11 x12 x13 ! x1n $


%x x 22 x 23 ! x 2n &&
X ' % 21
% " " " " &
% &
(% x m1 x m2 x m3 ! x mn )&

#m m m
$
s ' sum(X) ' % 2 x i1 2 x i2 ! 2x in &
( i '1 i '1 i '1 )

,9"%'#+!

s - *15 18 21 24+
,3#/*0 X ,.)*4%, !/ &#*#/, )0( !2 3#1-! )+1

,!$5&%
s - *10 26 42+

cumsum &).50#( .2
#&#"$ *#45# ,*)76%# *#45# &!$5% #7 ,).50#( .*$4.%, 3#1-, &).50#( "), #7 ,).50#(
.*$4.%, 3#1-, 9*2 $.#% *$)" !1$ */"1 9*#"
,3)"$, 3)1*2, &" ,*)76% cumsum &).50#( x * n , 1+ *#45# *#$2 )!!1 8(#"$

# 1 2 n
$
s ' cumsum(x) ' % 2 x i 2x i ! 2 x &) i
( i '1 i '1 i '1

.+#5$ &#*),$ 3/!# ,+!5, &#6#0 3/! +2#0 ,7 *#4*(#" .&#+#50, /#!/ *#4*(#" &*72$ &*+'#% ,.)*4%, #7 "%'#+$9

24
Matlab-! "#$%

,"%'#+!

s - *1 3 6 10+

&*76,# ,.)*4%, !/ &#+#%2, 9*#"! *$4.% 8(#"$ ,%)1- )"2 &#.)*4% !2 &!2#( #7 ,).50#(
.4!5, &.)*4% #%1 !+#' ,&#"$ ,.)*4%! ,".#&,
,&)!!1 ,.)*4% *#$2

# x11 x12 x13 ! x1n $


%x x 22 x 23 ! x 2n &&
X ' % 21
% " " " " &
% &
(% x m1 x m2 x m3 ! x mn )&

,3)*$4.%, 3)%#1-, &.)*4% &" ,*)76% cumsum &).50#(

# 1 1 1
$
% 2 x i1 2 x i2 ! 2x & in
% i '1 i '1 i '1
&
% 2 2 2
&
2x
S ' cumsum(X) ' % i '1 i1
2x i2 ! 2 x in &
% i '1 i '1
&
% " " " &
%m m m &
% x ! 2 x in &
%( 2
i '1
i1 2x
i '1
i2
i '1 &)

('-"+./' !"+'.4' (37&/ ('$-/6) !"/'&* 32 !"1'-"" 3.4.1

,&#"%'#+ )&/ 8!,! .&#.)*4%# 3)*#45# &!(1% &#2.%"$ 3)%#1- '.))! 8&)0
3)*#45# )0/ 8)$ &)*!5- ,!(1% .1
,8#&0
* n ,1+ 3)*#45# – a, b
# b1 $
n %b &
a, b ' 2 a i ! bi ' * a1 a 2 ! a n + ! % 2 & ' a9 : b
i '1 % &
% &
(% b n )&
&)2#$)* &)0$& .2
,8#&0
* n ,1+ *#45# – x
* n , n + ,.)*4% – C
n n
f ' 22 x i cij x j ' x 9 : C : x
i '1 j'1

25
Matlab-! "#$%

prod 37& ("-4#'7 3.4.2

,*#45#, )*$)" &!(1% &" !)1%, *!5- ,*)76%# *#45# &!$5% #7 ,).50#(
n
p ' prod(x) ' ; x i
i '1
,"%'#+!

p - 24
.sum &).50#( &!#2( 8(#"! ,,7 &#.)*4% !2 prod &).50#( &!#2( 8(#"

+&#"/%' -&05* 3.5

,3)*!5- #" 3)*#45# ,&#.)*4% &#!(1/%, &#).50#( /#!/ &#%))5

repmat ("-4#'7 3.5.1

,"#, repmat &).50#( !/ )!!1, ,0$%,


repmat(X, r, c)

X ,.)*4%,/ 3)%2(, *(-% "#, r .!#(1/! +2#)%, *!5- #" *#45# ,,.)*4% "), X */"1
.&#+#%2, 8##)1$ !(1#/& X ,.)*4%,/ 3)%2(, *(-% "#, c-# &#*#/, 8##)1$ !(1#/&
,*!5- !#(1/! "%'#+

#2 2 2 2$
Z - %% 2 2 2 2 &&
%( 2 2 2 2 &)
,"$, 8(#"$ 3' &"7 ,!#2( 2.$! 8&)0

,*#45## !#(1/! "%'#+

#5 9 1 7 2 5 9 1 7 2 $
Z - %%5 9 1 7 2 5 1 9 7 2 &&
%(5 9 1 7 2 5 1 9 7 2 &)

,,.)*4% !#(1/! "%'#+

26
Matlab-! "#$%

#5 0 5 0 5 0$
%0 5 0 5 0 5 &&
Z-%
%5 0 5 0 5 0&
% &
(0 5 0 5 0 5)

meshgrid ("-4#'7 3.5.2

, Y &#.)*4%! x, y 3)*)., &" 3)'.)%, 3)*#45# '#7 &*%, "#, #7 ,).50#($ )*5)2, /#%)/,
.3))+%%-&!& 3)(*' &*).)! &#).50#(! &#/%/%# 3)*)., &" &#'.))%, X
,&#+#5(,
x ' * x1 x 2 x 3 x 4 + ;
y ' * y1 y2 y3 + ;
[X, Y] ' meshgrid(x, y);
,&#.)*4%, &" &*.#)
# x1 x 2 x 3 x 4 $
X - %% x1 x 2 x 3 x 4 &&
%( x1 x 2 x 3 x 4 &)
# y1 y1 y1 y1 $
Y - %% y 2 y 2 y 2 y 2 &&
%( y3 y3 y3 y3 &)

3))#0)/ 3)%))5 Y ,.)*4%$ #!)"# 'x *).' 8##)1$ 5* 3))#0)/ 3)%))5 X ,.)*4%$ )1 $! 3)/0
.'y *).' 8##)1$ 5*
,"%'#+!

#1 2 3 4 $
X - %%1 2 3 4 &&
%(1 2 3 4 &)
#1 1 1 1 $
Y - %% 2 2 2 2 &&
%( 3 3 3 3 &)

.,).50#(, !/ 4!5$ +6" *#45# 5* 3#/*0 3),7 y *).# x *). *#.)! #00#.*$ 3"
,"%'#+!

27
Matlab-! "#$%

#2 4 6 8$
%2 4 6 8&&
X2 - %
%2 4 6 8&
% &
(2 4 6 8)
#2 2 2 2$
%4 4 4 4 &&
Y2 - %
%6 6 6 6&
% &
(8 8 8 8)

.3))+%% &!& 3)64/% 4#4*/$ 8+, 6.2 5*($ 4#*)( *&)$ *$-#) #7 ,).50#($ /#%)/,

28
Matlab-! "#$%

kron ("-4#'7 3.5.2

.&#.)*4% )&/ 8)$ )*#704 !(1 &2.$% #7 ,).50#(


!1$ X ,.)*4%$ *$)" !1 &!(1% "), X[m1,n1] , Y[m2,n 2] &#.)*4% )&/ 8)$ )*#704 !(1 &#2%/%
. #(0 m1! m2 1 , 0 n1! n2 1 $) !+#'$ ,/+6 ,.)*4%$ #!", &#!(1%, !1 *#+)-# Y ,.)*4%,
,"%'#+!
#1 2$
X'%
(3 4 &)
#1 1 1$
Y ' %%1 1 1&&
%(1 1 1&)
# #1 1 1$ #1 1 1$ $ #1 1 1 2 2 2$
% % & % &
%1! %1 1 1& 2 ! %1 1 1&& & %1 1 1 2 2 2 &&
%
% %(1 1 1&) %(1 1 1&) & %1 1 1 2 2 2&
K ' kron(X, Y) ' % &'% &
% #1 1 1$ #1 1 1$ & %3 3 3 4 4 4&
%3 ! %1 & %
1 1& 4 ! %1 1 1&& & %3 3 3 4 4 4&
% % & % &
(% %(1 1 1&) %(1 1 1&) )& %(3 3 3 4 4 4 &)

. #(0 2 ! 31 , 0 2 ! 31 $) 3)+%%, &!2$ "), K ,.)*4%# X[2,2] , Y[3,3] #7 "%'#+$

.3)+6" &.)*4% #" *#45#1 X &2)$5 )"2 &#.)*4% !(1/! /%% 8&)0

29
Matlab-! "#$%

!#"/%' /63 8&0"! 3.6

,&#.)*4% 9#(),! &#).50#( )&/ &#%))5


(flip left-right) fliplr &).50#( .1
,&#+#%2, *+- &" &1(#, #7 ,).50#(

# 4 3 2 1$
X _ lr - %% 4 3 2 1&&
%( 4 3 2 1&)
(flip up-down) flipud &).50#( .2
,&#*#/, *+- &" &1(#, #7 ,).50#(

# 3 3 3 3$
Y _ ud ' %% 2 2 2 2 &&
%( 1 1 1 1 &)

+"/(,"- +&(&&*' +5/4' +/"+0 3.7

,3)%!20 n-# &#"##/% m !/ ,1*2% ,0#&0


<a11x1 . a12 x 2 . % . a1n x n ' b1
=a x . a x . % . a x ' b
= 21 1 22 2 2n n 2
>
="
=?a m1x1 . a m2 x 2 . % . a mn x n ' b m

,&).)*4% ,*#.$ ,$)&1! &0&)0 #7 &#"##/% &1*2%


A!x ' b

&)2#$)* ,.)*4% "), A ,.)*4%, 7" 0 m ' n 1 ,,7 3)%!20, *(-%# &#"##/%, *(-% #$/ ,*5%$
,"$, 8(#"$ inv &).50#( &#2.%"$ !"0, &#"##/%, &1*2% &" 10*#&(! 8&)0

,pseudo-inverse &" $/6! 9*#. /) 0 m / n 1 )2#$)* "!, ,*5%, *#$2

,"$, 8(#"$ \ *#4*(#", &#2.%"$ "), !"0, &#"##/%, &1*2% &" *#&(! ,()+2 9*+

.,1)(, A ,.)*4% *#$210

30
Matlab-! "#$%

)",+*' +,"4%& +/"'* 3.8

,"$, 8(#"$ save ,).50#($ /%&/0 Workspace-$ 3)0&/%, !1 &*)%/

.Workspace-$ 3)0&/%, !1 &" !)1%, name.mat ;$#5 &*.#) #7 ,).50#(


;$#5 3/ *6"! ,*)%/! 3)0&/%, &#%/ &" :)-#0# 3+#5%1 3#/*0 3))().(- 3)0&/% 5* *#%/! )+1
.mat-,
,2.$0 name 3/$ .mat ;$#5! x yy Z 3)0&/%, &" *#%/! )+1 !/%!

3)0&/% 5#6%! )+1 .clear ,).50#($ /%&/0 ,+#$2, $6*%$ #0*.)/ 3)0&/%, !1 &" 5#6%! )+1
.,5)6%! 3)0&/%, &#%/ &" 8)).0# clear ,).50#($ /%&/0 3))().(-
,2.$0 x Z 3)0&/%, &" 5#6%! )+1 !/%!

,"$, 8(#"$ load ,).50#($ 3)/%&/% ,+#$2, $6*%! mat ;$#5% 3)0&/% 8#24! #00#.*$ 3"

.(Current Directory) &)61#0, ,))*(-, 8#!6$ )#.*, .mat ;$#5 !2 ,!#(1 ,.)6! 4#/( #"

Command Window-- 46"' +2#!& +-$1 3.9

('9'+5/ 3.9.1

.4-54 &'.,! &#7#*6%$ /#%)/, "#, *#*$ 8(#"$ 2+)% &'.,$ )71*% )!1
4-541 &*+'#% &7#*6% .+)6) #& "#, #$ *$)" !1/ *#45# ,/2%! "), Matlab-$ (String) &7#*6%
.#)++. )0/% 3))/*' !2$
,"%'#+!

,)1 $! 3)/0 .(3)##& 9*2%) char array '#-% 4 9*#"$ *#45# *.#)

s(2) @ a
fliplr(s) @ eman

.&#7#*6% !/ ,.)*4% *#.)! !/%! 8&)0 81! .Matlab-$ 9*2% !1! ,%#+$ 9*2% "), &7#*6%
,"%'#+!

s2(1,:) @ name
s2(2,:) @ eman

31
Matlab-! "#$%

,.*0 3" 9" .,.)*4% 8,% *#.)! ,)2$ ,&), "! 81!# 9*#" #&#" &#!2$ #), &#7#*6%, )&/ !"0, ,*5%$
&).50#( 32 #7 ,)2$ !2 *$'&,! 8&)0 .,")'/ &2+#, !$50 3)0#/ 3)1*#" &#!2$ &#7#*6% -)01,!
.str2mat

,&$)&1 "%'#+!

,Command Window-$ !$5&%# 3)##& 9*2% !/ *3 , 5+ ,.)*4% *.#)

.*&#) &#*.5, &#7#*6%, :#-$ 3)6##* &*.))% #7 ,).50#(

.deblank &).50#($ #/%&/, 6##* "!! ,.)*4%,% &7#*6% :#!/! )+1

Command Window-) $0"/ (1-, 3.9.2

,Command Window-$ 3)0&/% !/ 3)1*2 &'.,! 3)1*+ *(-% 80/)


.&*+#-%# ,() &#6( )1, 9" ,,4#/( )1, 9*+, ),#7 .";" &" 4)%/,! .1
.disp &).50#( .2
.fprintf &).50#( .3

.,!/ &#!#'2, 3))*'#-$ &".%0, &7#*6%, &" ,').% disp &).50#(


,,+#5(, &$)&1 "%'#+!

,4-54, &" Command Window 8#!6$ ,').%

&#".#& &'.,$ !/%!) 3))*(-% 3)0&/% )1*2 &'.,# 2#$5 4-54 !/ $#!)/$ 9*#. /) 3)$* 3)*5%$
,+$!$ &#7#*6% ').,! &#!#1) disp #%1 4-54 &'., &#).50#(# *6"% .(:*' !/ &*&#1$ #" ,+)+% !/
.&#7#*6%! 3)*(-%, 3)1*2, &" *)%,! num2str &).50#($ 3)/%&/%
,"%'#+
.Command Window-$ &"7 ').,! 3).#* #0"# %"- 9 "#, 45))$#" 9*#" )1 !$5&,

,'.#% Command Window-$

32
Matlab-! "#$%

.:-#0 40%#'*" &(-#, )"2 &7#*6%! &#*%#%, 11&#*(-, *(-%$ 4#!/! 3' 8&)0
,"%'#+!

81! .5 "#, &7#*6%! #*%#)/ &#*(-, *(-% )1 2$5 ,7 40%#'*" .5 9*2 !2$ :-#0 40%#'*" #0(-#,
,Command Window-$ 2)(#% "$, 4!(,

,,#$' ,4)!/ &*/("% 9" &$1*#% *&#) &)-6) #7 ,).50#( .fprintf "), disp-! &)$)40*4!" ,).50#(
.Matlab !/ ,*72$ #/(6 3)4*(! .,'#.&$ *&#)

Command Window-,/ $0"/ (3)4 3.9.3

input &).50#( &#2.%"$ Command Window 8#!6$ /%&/%,% ,0&/% !/ )*(-% 9*2 !$5! 8&)0
,"$, 8(#"$

+)!5) /%&/%,/ ,16%# 3))*'#-$ &".%0, &7#*6%, &" Command Window-$ ,').% #7 ,).50#(
.x 3/$ ,0&/%! *6$ /%&/%,/ 9*2, -01#% ,,*#5 ,7/ 2'*$ .Enter !2 ;6!)# 9*2

,"$, 8(#"$ ,0&/% 9#&! &7#*6% 3' -)01,! 8&)0

!(%2'
.4-54$ ,*#/ &+*! )+1 \n :)-#,! 8&)0 -
#7 ,).50#( .pause &).50#( "), /%&/%,% &#*)/) &#"*#, &!$5%, &(-#0 ,).50#( -
.#,/!1 *#&(1 !2 ;6!) /%&/%,/ +2 ,.*,, &" ,,/%

.&)0#*/2, ,+#50, )0(! &#*(- !!#111

33
Matlab-! "#$%

$0"/ "#)/ .4
,3)"$, 2+)%, )0$% )'#- 3)%))5 Matlab-$ .&#7#*6%# &#.)*4% #%1 2+)% )0$% *(-%! #0(/60 ,&2 +2

Matlab ,/ (%*0 *1'0 *#&+ :4.1 !"#

3))+%%-#+ 3))*%#0 3)1*2% !/%! :3)1*2% 3, Matlab-$ 2+)%, )0$% !1 4.1 :*'% &#"*! 8&)0/ )(1
3)'#- *(-% *)1,! +%!0 ,7 5*($ .(character array) 3)##& !/ 9*2% 8, &#7#*6%# &#.)*4% 3)"*50
.3)(-#0 2+)% )0$% !/

)""6''-$/ )""/'&, )"5/4' 4.1

!/ :-#"1 )+%% &!& 9*2% !2 $#/6! 8&)0 .3)+%% )0/% *&#) )!2$ 3)1*2% *#.)! )#.* &#$* 3)&)2!
&#.)*4% /#!/% &$1*#% RGB &0#%& ,"%'#+! .4.2 :*'$ *))#.%/ )(1 ,)0/, )*6" &6" &#.)*4%
3)1*2% !2 $#/6! 8&)0 .&)0#2$. ,0#%&$ !#61# ,5#*) ,3#+", &%.#2 &" &#0)).%, (8$#%1 !+#' #&#"$)
9*2% &##,% *+6$ ,*#4*(%4 !/ &#)+%% &!& &#+)+% "%'#+! ,)+%% $* 2+)% *#&$ 3' 3))+%%-$*
.)+%% &!&
zeros, ones, rand, #%1 &#)-)-$ ,.)*4% &*).) &#).50#( &#2.%"$ 3))+%% $* 3)1*2% *#.)! 8&)0
.123)40%#'*" )0/% *&#) &$., )"2 randn
,,+#5(, "%'#+!

A(i,j,k) *$)", !2 $#/6! 8&)0 .-(" 31*2/ 3)*$)" 3 ! 4 ! 5 ' 60 !2$ 5 !2 4 !2 3 9*2% &*.#)
.k ,.)*4%$ (i,j) *$)" !21

,"#, A !/ !+#',

.cat &).50#($# repmat &).50#($ /%&/,! 3' 8&)012

34
Matlab-! "#$%

,&)(*' ,/6%,

.*+37 ,/ 5 ,( 4 ,( 3 8"(0 :4.2 !"#

)-)-$ 8(#"$/ &#).50#( 80/) .3))+%% $* 3))*%#0 3)1*2% !2 &#$* Matlab &#).50#( 2.$! 8&)0
&).50#( !/%!) *$)" *$)" *%#!1 ,3)*!5- !2 &#!2#( #" (sum &).50#( !/%!) 3)*#45# !2 &#!2#(
.(sin
.8&!#2( &" 2.$! +%% ,7)"$ 8,! #0)).)/ &#1)*. 3)*#45# !2 &#!2#(, &#).50#(
,"$, 8(#"$ d ,%)1- +%%# A )+%%-$* 9*2% !$5! ,!#1) sum &).50#( !/%!

.4.2 :*'$ &#.)*4%, !2 ,%)1-, ,/2%! "#,/ *3 , 4+ ,.)*4% $)0) sum(A,3) ,!/%!
.3))+%%-#+# 3))+%%-+6 3)1*2% !2 #!2(/ 8(#" #&#"$ &#!2#( *$)" *$)" &#!2#(, &#).50#(
,!/%!

.A 9*2%! ,,7 !+#'$ 9*2% &*.))%

8+$/ "0// "'#"2 4.1.1

3%.2 3)+%%, &" &#0/! #" permute &).50#( &#2.%"$ 9*2% !/ 3)+%%, *+- &" &#0/! 8&)0
.reshape &).50#( &#2&%"$

3)+%%, *+- )#0)/

-, &!#2( !/ ,$6*, ,/2%! "), #7 ,).50#( .3)+%%, *+- &(!6, &*/("% permute &).50#(
.3))+%%-$* 3)1*2%! transpose

9*2%, !/ 3)+%%, *+- &" 9#(,! )+1 . [7 , 13 , 14 , 8] !+#'$ B )+%% $* 9*2% 8#&0 )1 6)00 !/%!
,3#/*0 C [7 , 8 ,13 ,14] -!

35
Matlab-! "#$%

3)+%%, )#0)/

#)+%% &"# 9*2%, &" 4!51 &!$5% #7 ,).50#( .9*2% !/ 3)+%%, &" ,0/% reshape &).50#(
,%)'+, *+- .,")'/ &2+#, &!$5&% 4!5$ 9*2%,% ,0#/ /+6, 9*2%$ 3)*$)", *(-% 3" .3)/+6,
.*+-, )(! 3)+%%, *"/# &#*#/ ,&#+#%2 "#, 4!(, 9*2% !/ /+6% *#+)-, *+-# 4!5$ 9*2%, !/
,"%'#+!

# 1 4 7 10 $
A ' %% 2 5 8 11&&
%( 3 6 9 12 &)
A
# 1 3 5 7 9 11$
A'% &
( 2 4 6 8 10 12 )

.3))+%% $* 3)1*2% *#.)! 3' 8&)0 #7 ,).50#( &#2.%"$


,"%'#+!

5 ,( 4 ,( 3 8"(0 :4.3 !"#

5)*4

!/ /+6% *#+)-# ,%)'+ *+- #&#"$ 9#*" +6" ,+#%2 *#45#! )+%% $* )*%#0 9*2% !1 9(#, A(:)
*.#0 "#, #0%%/ )+%% $*, 9*2%, &" #7 ,4)/$ *.#0, *#45#% *76/! 8&)0 3' 81! .reshape &).50#(
.reshape &).50#( &#2.%"$

,"%'#+!

.4.3 :*'$ *"#&%, A )+%% $* 9*2% #&#" &" $)0%

36
Matlab-! "#$%

(cell arrays) )"(+ "5/4' 4.2

.3)0#/ 3)!+'# 3)-#()4% 3)0&/% 8#-1" */("% cell 9*2%


,.)*4%! ,%#+, 3#/)* )"2 *.#0 3)"& 9*2% .9*2% !)1%, "& "#, #$ *$)" !1/ 9*2% "#, cell 9*2%
.&#2$#*%$ 3#5%$ 13&#!-!#-% 3))*'#-$ /#%)/ 42%!
,"%'#+!

,"$, [2 , 2] !+#'$ cell array *.#)

,8, 3)"& 9*2% ').,! &#(-#0 3)1*+ )&/


."& !1 4#*)( ,').%, celldisp &).50#( .1
,"%'#+!

,Command Window-$ *.))%

.{} 8%#-% 5)* 3)"& 9*2%13

37
Matlab-! "#$%

.3)"&, 9*2% )$)1* !/ )(*' '#.)) &44*/%, cellplot &).50#( .2


,"%'#+!

,:*', &" &*.))%

string 7

C .*7) 8"(0 ,/ #&5** :4.4 !"#

,0/) "! 3)*6", 3)1*2%, )#0)/ 81!# 3)*6" 3)1*2% !/ 3)5&#2 5* !)1% 3)"& 9*2% )1 *#17! $#/6
.3)"&, 9*2% &"
.C 3)"&, 9*2% &" ,0/% #0)" A ,.)*4%, )#0)/ !"0, "%'#+$ !/%!

#7 ,0#1& .9*2% #&#"$ 3)0#/ 3)'#-% 3)45))$#" !)1,! 3&!#1) "), 3)"& )1*2%$ )71*%, 8#*&),
3))*%#0 3)1*2%$ )*/(" )&!$ *$+ ,,0&/% !+#'$ &#.)*4% *#%/! 9*#. /) */"1 ,!/%! ,&)/#%)/
.3))+%% $*

3))*'#-$ 3)/%&/% &21/ 5* ,.)*4%$ 3)*$)"! ,/)'! ,%#+ 3)"&, 9*2%$ *$)" !1! ,/)',
.&#!-!#-%
,"%'#+!

38
Matlab-! "#$%

,3#/*0# 3)-5+0)", &" */*/0 (1,1) "&$ (2,3) *$)"! &/'! )+1

,"$, 8(#"$ &2.#$% 3)"& &5)6%

,"%'#+!

,"#, C 9*2%, &21 .,)0/, 3)"&, &+#%2 &" 56#%

.14cell &).50#( &#2.%"$ 2.#$% &#5)* &#.)*4%$ 3)"& 9*2% !#6&"


,,+#5(, "%'#+!

,&#5)* &#.)*4% 3)!)1%, 3)"& !/ [3 , 4] 9*2% &*.#)

.3)40%#'*" +#2 &(-#, )"2 3))+%%-$* 3)"& )1*2% *#.)! 8&)0 #7 ,).50#( &#2.%"$
,"%'#+!

. [3 , 4 , 5] 3)"& 9*2% *.#)

.cat &).50#( &#2.%"$ "), &(-#0 9*+14

39
Matlab-! "#$%

Structure "5/4' 4.3

3)!+'# 3)-#()4% 3)0&/% !/ 8#-1" 3)*/("% 3, 3'/ 91$ cell )1*2%! 3)%#+ structure )1*2%
"!# &#%/ !/ &#+/ 9#&$ 3)0-1#"% structure-$ 3)1*2%, ,cell )1*2%! +#')0$ 9" .3)0#/%# 3)0#/
.2+)% !/ ,0#/ '#- !)1,! !#1) ,+/ !1 .9*2%$ 3)*$)" 9#&$
,"%'#+!
patient

.name ‘John Doe’

.billing 135.00

120 39 78
.test 98 154 12

structure 8"(0, 70#&% :4.5 !"#

,3)0(#" )0/$ &)/20 structure )1*2% &))0$


,"%'#+! .,$., &#2.%"$ .1

,"$, patient 3/$ *1,1+ !+#'$ structure 9*2% &" $)0%

,"$, 8(#"$ 9*2%, &" $)6*,! 9)*. 6#5! +#2 *#.)! )+1

, *1, 2+ !+#'! $6*#, patient 9*2% &21

40
Matlab-! "#$%

,)!!1, 3#/)*, .struct &).50#( &#2.%"$ .2


%

,3#/*0 *1, 2+ !+#'$ patient 9*2% #&#" &" *#.)! )+1 "%'#+!

structure "&+$/3 ,2"1 4.3.1

,"$, structure-, 9*2% 8#&0 )1 6)00


patient array

patient(1) patient(2) patient(3)

.name ‘John Doe’ .name ‘Jane Doe’ .name ‘Elvis’

.billing 135.00 .billing 112.00 .billing 135.00

120 39 78 163 67 71 52 43 121


.test 98 54 12 .test 13 18 191 .test 156 12 79

. [1, 3] ,%&#' structure 8"(0, 70#&% :4.6 !"#

.(&#+/, !1! 3)1*2 !)1%,) structure 9*2%$ *$)" !1! &/'! 8&)0
,"%'#+!

,,+/, 3/# ,+#50 #()-#, )().(- ,+/! &/'! )+1


,"%'#+!

41
Matlab-! "#$%

.3)"&% -5+0)" #()-#, )().(- ,+/ 9#&$ *$)"! &/'! )+1


,"%'#+!

.)#.*, ,+/, 3/# ,,+#50 ,9*2%, 3/ &" &#2$#*% 3))*'#-$ 3#6&0 3!/ ,+/ !$5! )+1
,"%'#+!

,"#, ,$ )!!1, /#%)/, 8(#" .rmfield ,).50#($ /%&/0 structure-% ,+/ 5#6%! )+1

;#6 struct_array1-$ 30/)/ &#+/, !1 &" !)1%, struct_array2 3/$ /+6 structure 9*2% *+'#%
.field ,+/%

,"%'#+!
,,"$, ,+#5(, &" 2.$0 patient 9*2%% billing ,+/ &" +)*#,! #00#.*$ 3"

42
Matlab-! "#$%

("0//-'0 ,4"7+1 .5
-&!&# 3))+%%-#+ 3)(*' *.))! 8&)0 ,$/ ,*),%,# ,6#0, ,*#., "), Matlab !/ &#0#*&), +6"
.3))+%%
,3)'#- ,/#!/! &#5!6&% ,5)(*', &#).50#(

3)0#)("# 3)0#%)- 3)(*' &*).) 3)(*'$ ,4)!/


legend (2D only) 2-D figure
text plot subplot
title fill zoom
box plotyy hold
grid
axis 3-D alpha
set, get plot3 shading
clabel surf, surfc
xlabel mesh, meshz 3-D
ylabel contour, contour3, contourf view
waterfall rotate3d
3-D cylinder
zlabel
colorbar
colormap

$6*3"# )&*561&3 :5.1 $,'-

,).50#( &.*, .:*', 44*#/% #$/ ,5)(*' 8#!6 )4%#4#" 8(#"$ &*.#) 3)(*' &*).)! ,).50#( !1
)#.* 3)$* 3)*5%$/ )0(%# #7 ,$)-% .3+#5, :*', &5)6%! &%*#' :*' 3))5 *$1 */"1 3)(*' &*).)!
figure(n) 3#/)*, .,5)(*' 8#!6 *.))! )+1 figure ,).50#($ 3)/%&/% 3)+*(0 3)(*' *(-% *.))!
."$, 3!/, *(-%, !2$ 8#!6 *.#) *#(-% "!! figure 3#/)*, .n *#(-% !2$ ,5)(*' 8#!6 *.))%

!(%2'
*%/) :*',# File 4)*(&$ Save as… #" Save &).50#( &*)6$ )"2 &2.#$% :*' &*)%/ -
.fig ;$#51
Copy &).50#( &#2.%"$ Word #%1 3)!)!%& +$2%! Matlab !/ 3)(*' *)$2,! 8&)0 -
25* )#0)/ .Matlab-$ 3)(*'! !+6%, &*)*$ "#, *#(" 25* .Edit 4)*(&$ &".%0, Figure
3)*6#$ 8$! 25* 6)*1,! )+1 !/%! .Copy Options ,Edit 4)*(&$ ,*)6$ )"2 2.#$%
.Force white background ,).(#"$

43
Matlab-! "#$%

plot +"#1,&0 5.1

3)(*' 4#4*/! &+2#)% "),# 3))+%%-#+ 3)(*' 4#4*/$ *&#)$ &)-)-$, ,).50#(, "), plot &).50#(
,"#, ,!/ )!!1, 3#/)*, .3)##5 #" &#+#50 !/
plot(x1 , y1 , x 2 , y 2 ,%)

)+%. #" 9*#" #&#" )!2$ 3)*#45# )+%. ,3)*!5- )+%. &#),! 3)!#1) Bx i , yi C ,i ' 1, 2,% */"1
#" &#+#50, &*#.$# 2$.$ ,4)!/ &*/("% +%. !1 *6"! ,*.5 &7#*6% &(-#, .15!+#' #&#"% &#.)*4%
,"#, 3#/)*, )!!1 8(#"$ .3)##5,
plot(x1 , y1 , c1 , x 2 , y 2 , c 2 ,%)

.16#5/,+#50, '#-# 2$. &" 3)0)).%, (3)##& ,2$*"! +2) 3)##& *(-%% $1*#% ci
.( ...,3#+" ,5#*) ,!#61 "), !+6% &*)*$) 5#*) 2$.$ #),) #5/,+#50, )1 &2$#5 'g' &7#*6%, !/%!
)1 &2$#5 'k--' &7#*6%, !/%! .,%#+" &)$1#1 ,),& :*'$ ,+#50 )1 &2$#5 'r*' &7#*6%, !/%!
.*#6/ 24#5% #5 44*#/)
#5 44*#/) )1 &2$#5 'g--s' &7#*6%, !/%! .&7#*6%$ 3,)0/ 8#). )"2 3)##5 32 &#+#50 $!/! 3' 8&)0
.3)2#$)* 32 24#5% 5#*)

.&#)*/(", !1 &" &%1-% 5.2 ,!$4

2$. ,+#50 '#- #5 '#-

.*3"#' )&%&61& .*&&6 4&0*+ )&*"/37 .&9*+ :5.2 $,'-

('0'4# 32 :+1 .'.+2 5.1.1

,3,)0)$ 3)##5 *#$)6 "!! &#+#50 !/ 3)(*' 44*/! 3)1*+ )&/ &#%))5

3)*!5- 3, Bx i , yi C ,i ' 1, 2,% 3)+%., .1


,+#5, "%'#+!

.5*(, 9/%,$ 8#+)/ +6#)% ,*5% #,7 9" ,.)*4%# *#45# !/ ,).0)$%#5 3' &%))515
.,0/% #00)" c &7#*6%$ 3)##&, *+- 16

44
Matlab-! "#$%

.5.1 :*' &" $)0%

1
0 1 2 3 4 5 6 7 8

)&%&61 !"# :5.1 !"#

81!# !#$', !2 5#)+$ 8, &#+#50,% 5!6/ 91 3)0#&0, !2 +5%&, Matlab )1 &#"*! 8&)0 5.1 :*'$
&" &2$#5, axis &).50#($ /#%)/ )"2 3)*)., &1*2% &'#.&$ &)0+) 4#!/! 8&)0 .8,$ 8)6$,! ,/5
.:*', &'., 3#6&
,"#, )!!1, ,%#/)*
axis([x min , x max , y min , y max ])
,*/"1
:*'$ 2)(#), x *). !/ )!%)0)% 9*2 - x min
:*'$ 2)(#), x *). !/ )!%)-1% 9*2 - x max
:*'$ 2)(#), y *). !/ )!%)0)% 9*2 - y min
:*'$ 2)(#), y *). !/ )!%)-1% 9*2 - y max

,+#5, &" :)-#0 5.1 :*'$ ,'#.&, &" "6##*"! )+1

.!$5&% 5.2 :*'#

,17"$, 8(#"$ &"7 2.$! 8&)0 3)*)., &#!#$'% 5!6 5* &#0/! #00#.*$ 3"
,3)*)., &#!#$' &" #,/!1 *#45#$ $).,! ,!)6&

tmp @ [x min , x max , y min , y max ]

,81+#2%, *#45#, &" axis-! *)76,!# &#)#.*, &#40)+*#"#5, &" *#45#$ &#0/!

.z *). &" 3#6&! )+1 z min , z max 3)0&/% )0/ +#2 &(-#, )"2 )+%%-&!& :*'! 3' !"0, "5)*4", &" 2.$! 8&)0 17

45
Matlab-! "#$%

0
-1 0 1 2 3 4 5 6 7 8 9

axis )*561&3 )&(507' $)1&/ &,/ .*"*5$ )9"(0 )#&5)/ )&%&61 !"# :5.2 !"#

18
#5, '#- 8#). )" 9" ,+#50, '#- 8#). + 3)*#45# 3, Bx i , yi C ,i ' 1, 2,% 3)+%., .2
.y-, &#40)+*#"#5 &" ,##,% )0/, *#45#,# x-, &#40)+*#"#5 &" ,##,% +%.$ 8#/"*, *#45#,
,+#5, "%'#+!

.5.3 :*' &" $)0%

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2

)&%&61$ #&+ 4&*5 *"( )&%&61 !"# -&-"/ :5.3 !"#

.3)##5 #" &#+#50 :*' ,),) :*', 3" ,2)$5 !2 2)(/% #00)" 2$., 8#). 18

46
Matlab-! "#$%

+*(0$ *#45#$ *$)" !1 3#/*! 9*#. 8)"/ "#, )*!5-, 3#/)*, &%#2! )*#45#, 3#/)*, !/ 8#*&),
,*#., !2 ,4)!/ *-#6 "#, )*#45#, 3#/)*, !/ 8#*-6, .(3)1#*" 3)*#45# *#$2 )*/(" )&!$! 9(#,)
.+*(0$ ,+#50 !1 !/ 2$.,#

!"''4 32 :+1 (+"-" 5.1.2

3, Bx i , yi C ,i ' 1, 2,% 3)+%., */"1 81! .3)##5 !/ 3)(*' "), 3)(*' &*).)! !+6%, &*)*$
3)*/) 3)##5 *$6) Matlab 7" (5.1.1 5*($ ,)0/, &#*/(",) ,+#50, '#- /*#(%$ 8)#.% "!# 3)*#45#
.&#+#50, 8)$
,+#5, "%'#+!

.5.4 :*' &" $)0%

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2

.*&&6 ,/ !"# :5.4 !"#

,+#5, .&(-#0 "%'#+

.5.5 :*' &" $)0%

.!'2% *#). "), &6" "%'#+ .,+)6"# &)*"0)! ,*#.$ 3#'+ x-*). +'01 5* "! 3)(*' 44*/! 3' 8&)0
,"), &#)74*5 &#40)+*#"#5! &)4%&%, ,).%*#(-0*4,
x ' a . r cos(D)
y ' b . r sin(D)

47
Matlab-! "#$%

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7 8 9 10

&6&60 &6' $*561&3 -&-"/ :5.5 !"#

. r "#, #-#)+*# (a, b) ,+#50$ "#, !'2%, 71*% . 0 E D E 2F */"1


,+#5,

.5.6 :*' &" $)0%

!/ &#).*#(#*(, &" &2$#5 axis square &).50#( .3)*).! 3)!)$5%, 3)##5 &/* &*.#) grid &).50#(
&).50#( &#2.%"$ 3)*)., &1*2% &'#.& &" +)*#,! 3' 8&)0 .!+#' #&#"$ #"*) 3)*)., )0// 91 :*',
.19axis off

.axis equal-# axis tight #%1 &#)/#%)/ &#)*/(" *(-% +#2 &#4*#(% axis &).50#( !/ help-$ 19

48
Matlab-! "#$%

-1

-2

-3
-4 -2 0 2 4 6

,#(0 )"*5* :5.6 !"#

!"*437/'4 !"+7*/ .'.+2 5.1.3

&44*/%# ,%#+%, 5!6,% &%!2&% ,).50#(, 7" 3)-5!(%#5 3)*(-% "#, plot &).50#( !/ 4!5, 3"
&).50#( 7" +)6) )-5!(%#5 40%#'*" "#, 4!5, 3" .+)6) 8(#+ ".#) ,*5% 42%! ,)/%%, 5!6, &" 5*
.,%#+%, 5!6, &%#2! )/%%, 5!6, &" &44*/% plot
,*%#!1

&

,+#5, "%'#+!

.5.7 :*' &" $)0%

49
Matlab-! "#$%

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1 -0.5 0 0.5 1

.*+6,30&6 .*9"( -&-"/ :5.7 !"#

50
Matlab-! "#$%

66&$ 9/2$ +&"#1,&0 /03' %&%/* 5.2

.++#$ :*'$ &#).50#( *(-% 4#4*/! &#)*/(" 3 &#%))5

('"-4#'7, "+'.4' (6 ,3"&/ ,-"+./, +'.4' x +"- 5.2.1

,"#, )!!1, 3#/)*,


plot(x, * y1 , y 2 ,% , y N +)
#"
plot(x, * y1 ; y 2 ;% ; y N +)
.9*#" #&#"$ 3)*#45# 3, Bx i , yi C ,i ' 1, 2,% 3)+%., */"1

40%#'*"$ -)01,! 8&)0 &"7 2.$! )+1 .x *). #&#" &%#2! &#).50#( *(-% 44*/! )#.* &#$* 3)&)2!
plot &).50#( .,.)*4%$ &#*#/ #" &#+#%2 )*#45# *#&$ &#).50#(, &"# ,*#45#1 x-, *). &" 8#/"*,
.x-, *). *#45# &" #1*#"$ 3"#&/ +%%, )(! &#).50#(, &" &44*/%

,"%'#+
,+#5, .( 0, F, 2F ) 3)0#/ 3)*+&$ 3)-#0)- *(-% +)6) :*' !2 4#4*/

.5.8 :*' &" $)0%

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7 8 9 10

)7 $,*90 $5*"-0, "&-6& x "*5) x "*5 &)&7 )0&(, )&*561&3 "3+0 -&-"/ :5.8 !"#
()&*561&3$ *"&-6&

51
Matlab-! "#$%

,,$)&1 )"2 +)6" 2$.! 3)2$., !1 &" 2#$5! 8&)0

'%2'
:*' *#&$ ,+#%2 *#45# !1! &-6))&% "), ,.)*4% 5* 4!5$ &!$5% plot &).50#( 3" -
-*). &1*2% !2 ,4)!/ 8)" ,7 3#/)*$ .:*', #&#" !2 ,0#/ 2$.$ ,+#%2 *#45# !1 &44*/%#
.(1:length(y) "#, 7" *+'#% #00)" x *). */"1) x

('"-4#'7, "+'.4'' x "+"- (6 !"1-"/ !"+'.4' "0/- 5.2.2

,"#, )!!1, 3#/)*,


plot(x1 , y1 , x 2 , y 2 ,%)
.9*#" #&#"$ 3)*#45# 3, Bx i , yi C ,i ' 1, 2,% 3)+%., */"1

,"%'#+!
,&#).50#( /#!/ &#0#&0
<f (x) ' 3 . sin(Fx)
=
= x
>g(x) ' . 1
= 2
=h(x) ' 0 ln(x . 1) 13
?
. 0 E x E 5 3#6&$

,,!)6& $/60

x *#45# !/ +%. *#&$ ,).50#( !1 3#/)* )"2 :*', #&#"$ &#).50#(, &/#!/ &" 44*/0
,+#5, .#!" &#+#50$ ,).50#(, )1*2 *#45##

.5.9 :*' &" $)0%

52
Matlab-! "#$%

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

x *"*5 )7 .*#5*0 .*"&-6& *%05) x "*5 &)&7 )0&(, )&*561&3 "3+0 -&-"/ :5.9 !"#
()&*561&3$ *"&-6&&

,3#/)*, 81!# x *#45# #&#" &%#2! &#44*#/% &#).50#(, !1 ,7 ,*5%$

.:*' #&#" &" $)0%

.3)0#/ 3)0&/% &%#2! &#).50#( *(-% 44*/! 3' 8&)0 ,7 3#/)*$


,!"0, "%'#+! 9/%,$

<f (x) ' 3 . sin(Fx) , 0 E x E 3


=
= y
>g(y) ' . 1 , 2 E y E 4.5
= 2
=h(z) ' 0 ln(z . 1) 13 , 1 E y E 6
?

,+#5,

.5.10 :*' &" $)0%

53
Matlab-! "#$%

0
0 1 2 3 4 5 6

.*1&/ x *"*5 )0&(, )&*561&3 "3+0 -&-"/ :5.10 !"#

,0#/ &#+#50 *(-% ,,0#/ 3#6&$ 3)*). &1*2%$ ,).50#( !1 #044*/ !"0, :*'$ )1 $! 3)/0
.3)0#/ &#*#.# 3)2$.#

hold ("-4#'7 5.2.3

,hold &).50#($ /#%)/, 8(#"


plot(x1 , y1 )
hold on
plot(x 2 , y 2 )
"
plot(x N , y N )
hold off

hold on ,+#5(, .3+#5, :*', &" :)!6& :*' &*.))%, &).50#( !1 hold &).50#($ /#%)/ "!!
&%/, 5)-(,! )+1 .3)*). &1*2%# :*' #&#" !2 /+6 :*' !1 &%/,# 3+#5 :*' &*)%/! &%*#'
$.%% *#$2! 8&)0 ,81 #%1 .hold off &+#5($ 3)/%&/% 3)*). &1*2%# :*' #&#" !2 3)"$, 3)(*',
.32( !1$ $.%, &" :)!6%, hold &$)&1 &#2.%"$ 9(,!# off-! on

,+#5, "%'#+!

.5.10 :*' &" $)0%

54
Matlab-! "#$%

!'2"+ ('+'- 32 ('#'+*5' ('#'+(" !'&"* 5.2.4

3)(*' )'#- !/ $#!)/ 3)#!& )&!$ 3)0&/% ,,7 )#!& )&!$ ,0&/% 3#/)* &*#.
3)0#/ &#).50#(! 3)0#/ &#).50#(, !1!
!"0, 3#/)*$ )*/(" "! !"0, 3#/)*$ )*/(" "! )45(%#5 3#/)* :8#*&) ,.)*4%# *#45# .1
4#!/! */(" )" :8#*-6
!1 !/ 2$.$# '#-$
+*(0$ :*'$ ,).50#(
!"0, 3#/)*$ )*/(" "! )45(%#5 3#/)* :8#*&) '#-$ 4#!/! 8&)0 :8#*&) 3)*#45# )+%. .2
,).50#( !1 !/ 2$.$#
+*(0$ :*'$
!$*#-% 3#/)* :8#*-6
#7, 3#/)*, &*#.$ 5* !$*#-% 3#/)* :8#*-6 '#-$ 4#!/! 8&)0 :8#*&) hold &).50#( .3
)*/(" ,).50#( !1 !/ 2$.$#
+*(0$ :*'$
!$*#-% 3#/)* :8#*-6

.&/*" )&"&5 ,/ )&1&"+2& )&1&")* .&9*+ :5.3 $,'-

55
Matlab-! "#$%

)"0/2- )",&'"3 +03&! 5.3

*/("% Matlab .&#".#&, &*,$,! 3)0#%)- :)-#,! 3' !$#5% 3)(*' &#2.%"$ &#".#& &'., &2$
,3)"$, 3)0#%)-, &(-#,
:*'! &*&#1 -
y-# x 20)*).! &#*&#1 -
"*5% &$)& -
:*'$ 3#5% !1$ 4-54 -
:*'$ 3#5% !1$ 3).6# 3)##5 -
2.#%%# 3#%)0)% ,3#%)-5% #%1 &)-)-$ ,5)4-)44- -

.3)(*'! 3)0#%)- &(-#, /)6%,! ,"$, "%'#+$ /%&/0 5.3 5*($


,&#).50#( )&/ &#0#&0
<f (x) ' sin(Fx) . 4
>
?g(x) ' x
2

. "3 E x E 3 3#6&$
,+#5,

.3)0#%)- &(-#& "!! !"0, &#).50#(, )&/ &" *"&% ,7 :*' .5.11 :*' &" $)0%

0
-3 -2 -1 0 1 2 3

":( *1&0*+ 7,, !"# :5.11 !"#

.z *). *#$2 3' )+%% &!& :*' *#$2 20

56
Matlab-! "#$%

!"05'"/ !"#/"* !'2"+ 5.3.1

$&1 .3)!%-# )&6& $&1 ,)!)2 $&1 ,&#)0##) &#)&#" *#.)! 8&)0 4-54 &%/,# &#*&#1 &(-#,! &#+#5($
&)0##), &#", 3/ &$)&1# \ &#2.%"$ &#)0##) &#)&#" ._ &#2.%"$ )&6& $&1 ,^ &#2.%"$ *.#)% )!)2
&#)&#" &$)&1 #%1 "), 21&#!#+' &#)0##) &#)&#" &*).) .\alpha &$)&1 )"2 &*.#)% G "%'#+! .&)!'0"$
&*.#)% H "%'#+! .,!#+' &)!'0" &#" &#),! 9)*. &)0##), &#", 3/ !/ ,0#/"*, &#",/ 5* ,&#045
.\ &#2.%"$ *#.)! 3))*/(", 3)!%-, !1 &" &%1-% 5.4 ,!$4 .\Gamma &$)&1 )"2

3))*'#- &#2.%"$ &"7 &#/2! 8&)0 .400#(-5" !/ )#4)$ #%1 ,+6) 3)!%- *(-% +'"! )#.* &#$* 3)&)2!
2
.e^{\alphax_1^2} $#&10 eGx1 )#4)$, "%'#+! .{} &#!-!#-%

(+('& (7*', 5.3.2

.title &).50#( !/ 4!5$ &7#*6% &$., )"2 &2.#$% &*&#1 &(-#,


,"%'#+!

y-' x +"-3 ('+('& (7*', 5.3.3

,"%'#+! .xlabel &).50#( !/ 4!5$ &7#*6% &$., )"2 &2.#$% x *).! &*&#1 &(-#,

,"%'#+! .ylabel &).50#( !/ 4!5$ &7#*6% &$., )"2 &2.#$% y *).! &*&#1 &(-#,

.*4. (7*', 5.3.4

&7#*6%# 4-54, &$)& &#40)+*#"#5 &$., )"2 &2.#$% :*'$ (x, y) &#40)+*#"#5$ 4-54 &(-#,
.text &).50#( !/ 4!5$ ,)#.*, 4-54,
,"%'#+!

.y &40)+*#"#5 71*%$# x &40)+*#"#5! 8)%)% &7#*6%, &" ,$).% 4-54 &).50#( 22!+6% &*)*$ *#&$
(x,y)

4-54 &$)&

.,))24# )#-)0 42% 2.$! "!" ,*)*$ 8)" #7 ,*#.$ 4-54, &" $).,! 5#)+$ ,()" &2+! )+1

.A "), ,!#+' "(!" &#", !/%! .&#!#+' &#)!'0" &#)&#"! &#,7/ &#!#+', &#)0##), &#)&#", &#0)#.% "! ,!$4$ 21
. VerticalAlignment-# HorizontalAlignment &#).50#( &#2.%"$ &#*6" &#)*/(" 22

57
Matlab-! "#$%

\ )&(507' "&5*, .*1)*1$ .*,0+ .&9*+ :5.4 $,'-

58
Matlab-! "#$%

6+4/ ()"( (7*', 5.3.5

!/ 4!5$ ,:*'$ &#).50#(, &#%/ &" &#'.)%, ,&#7#*6% &$., )"2 &2.#$% :*'! "*5% &$)& &(-#,
,"%'#+! .4#4*/, 8#0'- !/ "%'#+ ,).50#( 3/ !1 +)! '.#) "*5%, &$)&$ .legend &).50#(

)#.* 3)&)2! .,0#)!2, &)0%), ,0)($ "*5%, &$)& &" ,$).% !+6% &*)*$ *#&$ legend &).50#(
8(#"$ "*5%, &$)& &" 7)7,! 8&)0 .:*'$ 2+)%, &" 3)*)&-% &#6(/ :*'$ 3)*6" &#%#5%$ ,$).,!
,3)"$, 3)1*2, &" !$5%, ,0&/% +#2 4!5$ :)-#,! #" (Unlock Axes Position &#2.%"$) )0+)

,!#2( 9*2
(!+6%, &*)*$) ,0#)!2 &)0%), ,0)($ "*5%, &$)& 3#5)% 1
,0#)!2 &)!"%/, ,0)($ "*5%, &$)& 3#5)% 2
,0#&6& &)!"%/, ,0)($ "*5%, &$)& 3#5)% 3
,0#&6& &)0%), ,0)($ "*5%, &$)& 3#5)% 4
&#6(/ ,%1 ,*)&-% ,$)&, #$/ 3#5)%$ ,:*', &#!#$' 9#&$ "*5%, &$)& 3#5)% 0
,0#)!2 &)0%), ,0)($ ,+*(0$ :*', &#!#$'! ;#6% "*5%, &$)& 3#5)% -1

legend .&6*0 )&*"/37 :5.5 $,'-

,+#5, "%'#+!

.*$+ *)&-& "! "*5%, &$)&/ 91 8#)!2 )71*%, 5!6$ "*5%, &$)& &" $).%

("*"*) ,4".*"..* (7*', 5.3.6

3()-#,! &#!5$ 8&)0 #&#2.%"$# &#).50#(, !/ 3))4-44- 3))#4)$ *(-% $/6% Data Statistics 8#!6
.5.12 :*'$ 3'+#%/ )(1 Data Statistics-# Tools !2 ,.)6! )"2 6&(0 ,7 8#!6 .:*'!

Data Statistics 4&,2 :5.12 !"#

59
Matlab-! "#$%

!2 !"0, 2+)%, &" 44*/! )+1 .+#2# 2.#%% ,3#%)0)% ,3#%)-5% )1*2 :*'$ ,).50#( !1! ').% ,7 8#!6
.,%)"&%, ,$)&, &" #0%- 4#/( :*',
max-# min )1*2 8%-0 f (x) ' sin(Fx) . 4 ,).50#(, 9*2 ,0&/% #$ 3#6&, &" 8%-! )+1 ,"%'#+!
.#7 ,).50#( !/ Y *#$2

!"-5' !"''4 (7*', 5.3.7

,,0)%) 8##)1$ &#),! 3)!#1) #!" 3).6 9" .\ *#4*(#", &#2.%"$ text &).50#($ 3).6 *.))! 8&)0
.(7.1 5*( #"*) )0+) 8(#"$ 2.#$% 3,/!1 3).6# 3)##5 4#4*/ .,4%!# ,!2%! ,,!"%/

!"7+13 !"#'/"* (7*', !'&"* 5.3.8

.235.3 5*($ 3)0#%)-, &#(-#& !1 &" ').% 5.13 :*'

.,0/% #0)" 8&$)&1 *+-# plot &).50#( *6"! $&1),! &#$))6 3)0#%)-, &(-#, &#+#5( !1

An example of graph annotation


9
sin(F!x)+4
8 sin(F!x)+4 min
sin(F!x)+4 max
7 2 x2
@x

5
sin(F!x)+4I
y
4

0
-3 -2 -1 0 1 2 3
x

":( *1&0*+ .( !"# :5.13 !"#

.)0+) 8(#"$ #0#/ "*5%, &$)&$ f (x) ' sin( Fx) . 4 ,).50#(, !/ 3#%)-1%# 3#%)0)% )1*2! 4-54, 23

60
Matlab-! "#$%

('0'47 ('$-/6) !""7+1 !".4"")'6 32 ('#'&( ($")4 5.3.9

9*+, .)#0)/! &#0&)0, &#0#1& !/ $* *(-% 45))$#" !1!# ,3))(*' 3)45))$#" *(-%% $1*#% :*' !1
,).50#( !/ 4!5$ :)-#,! "), &#+#5( &#2.%"$ ,5)(*' !/ 3)45))$#" !/ &#0#1& &#0/! ,4#/(,
.#7 ,0#1& !/ /+6, 9*2# ,0#1&, 3/ !/ &7#*6% :3)(-#0 3)0&/% )0/ )(*' 45))$#" &*.#),
,+#5, "%'#+!

.5.14 :*' &" $)0%

An example of graph annotation


9
sin(F!x)+4
8 x2

2
7
@x
6

y
sin( F!x)+4 I
4

0
-3 -2 -1 0 1 2 3
x

&1&/ &,/ $6*3"# ,/ .*-6**'&7 )&1&9)0 6,2/ !"# :5.14 !"#

!(%2'
.7.2 5*($ 4*#(%/ )(1 set-# get &#).50#($ /#%)/ "), *&#) &1$#-%, 9*+, -
,*72, ;$#5$ #00#$&, 3,)&#0#1&# ,5)(*' !/ 3)45))$#" !1 !/ ,%)/* &#"*! )+1 -
.(html 4%*#($) Handle Graphics Online Documentation

61
Matlab-! "#$%

fill +"#1,&0 5.4

&" "!%! )#.* !"0, "%'#+$ !/%! .fill &).50#( &#2.%"$ &#).50#( )&/ 8)$ :*'$ *#7" "!%! 8&)0
.3#+" 2$.$ "2 E x E 2 *#7"$ ,!#$*(,# -#0)-, 8)$ *#7",
,+#5,

.5.15 :*' &" $)0%


An example of graph annotation
9
sin(F!x)+4
8 x2

2
7 @x

5
sin(F!x)+4I
y
4

2 sin(F!x)+4 &&xx22
area between sin(pi*x)+4

0
-3 -2 -1 0 1 2 3
x

fill )*561&3 )0#%$ :5.15 !"#

81!) 3#6&, *#7", !#$' 9*#"! &#+#50 *%#!1 .*#'- *#7" &#*.#), &#+#50 "#, fill &).50#( !/ 4!5,
,).50#(, &-01, )"2 2.#$ y *).$ *#/*/, #7 "%'#+$ .(3#)-, &+#50! ,,7 ,),& ,!6&,, &+#50
*#45# &%/, )"2 2.#$ x *).$ *#/*/, 81! .,0#&6&, ,).50#(, &" 9#(, *+-$ 1"6"# ,!)6& ,0#)!2,
.9#(, *+-$ x *#45# &" #)*6"# x

62
Matlab-! "#$%

plotyy +"#1,&0 5.5

)*/(" /#%)/ .:*' #&#" !2 3)0#/ y )*). )0/ &#!2$ &#).50#( 4#4*/ &*/("% plotyy &).50#(
)&!$ ,0&/% #&#" !/ ,).50#(1 (&#0#/ &#+)6)) 3)0#/ 3))!5-)( 3)!+' )0/ 4#4*/ "#, ,7 '#-% :*'!
.)#!&
,+#5, "%'#+!

.5.16 :*' &" $)0%

40 1

20 0

0 -1
0 2 4 6 8 10 12 14 16 18 20
x

plotyy )*561&3 )0#%$ :5.16 !"#

#" 8#0'- &" &#0/! #" ,+6" !1! ,0#/ ylabel 8)).! )+1 .&#0#/ y 2 !/# y1 !/ ,!5-, )1 $! 3)/0
.plotyy !/ help-$# set &).50#($ *72),! /) 3)##5, 2$.

)"0/2!' +&/"*" )"5/4 +("/1 5.6

,&$)&1 )"2 &2.#$% :*'% &#*)/) 3)1*2 &")*5

:*', ,!#2 (script ;$#5$ &2.#$% #7 ,*#// #") Command Window-$ Enter !2 3).6#!/ 2'*$
,+#50$ )!"%/, #" )0%), *$12, *#&(1 !2 ,.)6! .*$12, 8%- $)$- $!. 2)(#%# &)4%#4#" 8#*6",
+%. +#2 ,-)01% &(-#0 ,.)6! !1 .x,y 3)*#45#! #7 ,+#50 !/ &#40)+*#"#5, &" ,$).% :*'$ &%)#-%
.Enter !2 3).6#! 3))-! )+1 .x,y 3)*#45#! &#40)+*#"#5

63
Matlab-! "#$%

subplot +"#1,&0 +&4#'($ 6." )"0/2 /03' +'*! 5.7

,subplot &).50#( &#2.%"$ ,5)(*' 8#!6 #&#"$ 3)(*' *(-% *#.)! 8&)0

.,5)(*', 8#!6 9#&$ 3)(*'-&&, !/ &#+#%2,# &#*#/, *(-% &" 3)2$#5 3)0#/"*, 3)40%#'*", )0/
.,4%! ,!2%!% 1"6"# 8)%)! !"%/% "#, *+-, */"1 3)(*'-&&, !/ -5+0)", "#, )/)!/, 40%#'*",
#)*6" "$, subplot-! +6" subplot 8)$ &#+#5(, !1 )1 "), ,$#/6 ,+#50 .,/6%,! 5.17 :*' #"*
.8#+)0, :*',-&&, !2 5* &#2)(/%

Figure No.1

"
"
"

"
"

"

subplot )*561&3 :5.17 !"#


,+#5, "%'#+!

64
Matlab-! "#$%

.5.18 :*' &" $)0%

Bar Plot Stacked Area Plot


1 15
3D Pie Plot
0.8
10 22%
0.6 11%
28%
0.4 33% 6%
5
0.2

0 0
-5 0 5 1 2 3 4

Stem Plot Stairs Plot Scatter Plot


1 1 -47.8

0.5 0.5 -48

0 0 -48.2

-0.5 -0.5 -48.4

-1 -1 -48.6
0 100 200 0 5 10 210 211 212

subplot )*561&3' /&0*/, 70#&% :5.18 !"#

65
Matlab-! "#$%

("0// (3( ,4"7+1 .6


.243))+%% &!& 3)64/%# 3))+%% &!& 3)##5 &*).) *5#- ,7 5*(

)""6'' +-+ )"&&1 6.1


.
,"), plot &).50#( !/ &)+%%-&!&, ,-*',
plot3(x1 , y1 , z1 , x 2 , y 2 , z 2 ,%)

#&#" )!2$ 3)*#45# !/ &#)/)!/ ,3)*!5- !/ &#)/)!/ &#),! 3)!#1) Bx i , yi , z i C ,i ' 1, 2,% */"1
,4)!/ &*/("% ,)/)!/ !1 *6"! ,*.5 &7#*6% &(-#, .!+#' #&#"% &#.)*4% !/ &#)/)!/ #" 9*#"
,"#, 3#/)*, )!!1 8(#"$ .3)##5, #" &#+#50, &*#.$# 2$.$
plot3(x1 , y1 , z1 , c1 , x 2 , y 2 , z 2 , c2 ,%)

text &).50#($ /#%)/$/ 3, 3)+)6), 3)!+$,, .,!/ &)+%% #+, ,!)$5%! ,%#+$ &+5(&% #7 ,).50#(
.zlabel &).50#( &#2.%"$ &2$50 z *). !/ &)##&,# z *). !)$/$ ,40)+*"#5 +#2 :)-#,! 9)*.
,"%'#+
,+#5, .,)0/, +)! &6" ,!*)(-# ,+)%*)( 44*/0

.6.1 :*' &" $)0%

:*', '#- &*)6$# )0%), *#&(1, !2 ,.)6! ,,.)*4%, 8#%)- )"2 Workspace 8#!6% *)/) 8(#"$ 3)(*' 44*/! 8&)0 24
.&#!$'#% #1*+ ,'#.&, &#)*/(" #2$4% 9" +#5 &#*#/ *(-% &$)&1 9-#6 ,7 5)*4 .)#.*,

66
Matlab-! "#$%

Spiral

Pyramid
40

1 top
top
0.5 20
z z
0
2
2 0
1 1
1
0.5
y 0 0 0
x 0
y -1 -0.5
x

plot3 )*561&3' /&0*/, 70#&% :6.1 !"#

67
Matlab-! "#$%

)""6'' +-+ )".%*' 6.2

,*+'#% )+%% &!& 64/%


z ' f (x, y)

!/ )!!1 3#/)* .surf -# mesh &#).50#( 8, 3))+%% &!& 3)64/% &*).)! &#)-)-$, &#).50#(,
,#!" &#).50#(

-#

&).50#( &#2.%"$ x,y &#40)+*#"#5 )*#45#% &#5(#%, &#40)+*#"#5, &#.)*4% 8, X,Y */"1
.X,Y &#.)*4% !/1 3)+%% 3&#" &!2$ ,.)*4% "), Z ,.)*4% .meshgrid
Z ,$#', &.)*4% )"2 2$50 32$./ 3)##5 &/* &*.#) mesh &).50#(/ "#, &#).50#(, )&/ 8)$ !+$,,
.25Z ,$#', &.)*4% )"2 2$50 32$./ 3)"!4% 3)$1*#%, 3)64/% &*.#) surf &).50#( &"7 &%#2!#
.Z ,$#', &.)*4% )"2 2$50 3&*'-% 2$./ 3)0$! 3)"!4 &*.#) mesh &).50#( !#1)$1

,"%'#+
,,).50#(, &" 44*/! )#.*
2
3F 4 "0 x 2 . y 2 1
f (x, y) ' 5sin 5 xy 6 . 10e .1
7 15 8
<-3 E x E 3
.> 3#6&$
?-3 E y E 3

,+#5,

.3"&,$ 6.3-# 6.2 3)(*' &" $)0%

.(*#6/ !+6% &*)*$) +)6" 2$.$ 3)##5, &/* surf &).50#($ 25

68
Matlab-! "#$%

15

10

z 5

0
4

0
3
-2 2
1
0
y -1
-4 -2
-3
x

mesh )*561&3 )&(507' -&-"/ :6.2 !"#

15

10

z 5

0
4

0
3
-2 2
1
0
y -1
-4 -2
-3
x

surf )*561&3 )&(507' -&-"/ :6.3 !"#

'%2'
!1 ,#!" 3))+%% &!& 3)(*' &*).) &#).50#( &#2.%"$ *&#) 5!6 64/% 44*/! #00#.*$ 3" -
.,%)'+ &#+#50 *&#) &(-#, &#2.%"$ ,).#!#7*, &" 8)45,! "#, &#/2! #0)!2/

69
Matlab-! "#$%

meshgrid &).50#(! &*#17&

,4!51 0 x, y 1 3)*#45# )0/ &!$5% #7 ,).50#(

,*/"1
[n ,1] *#45# - x
[m ,1] *#45# - y

,"$, 8(#"$ [m , n] !+#'$ X,Y ,,7 !+#'$ &#.)*4% )&/ &*.#)# 3&#" &!(1/% #7 ,).50#(
,&#+#5(,
x ' * x1 x 2 x 3 x 4 + ;
y ' * y1 y2 y3 + ;
[X, Y] ' meshgrid(x, y);
,&#.)*4%, &" &#*.#)
# x1 x 2 x 3 x 4 $
X - %% x1 x 2 x 3 x 4 &&
%( x1 x 2 x 3 x 4 &)
# y1 y1 y1 y1 $
Y - %% y 2 y 2 y 2 y 2 &&
%( y3 y3 y3 y3 &)

,3))5&%#
Jj, X(i, j) ' x(i)
Ji, Y(i, j) ' y( j)

*$)" Z &.)*4% $#/)6 #%1 &#".#& 8&#" &" $)0% Y-# X 3)1*2%, 32 ,+#50, *#4*(#"$ /#%)/ 81!
.,$*,$ *.5 8%7$ !$" ,&#"!#! )&/ &#2.%"$ *$)"
,!)2) "! +"% 8(#"$ *$)" *$)" Z &.)*4% $#/)6 &/6%, 8!,!

&" &#0/! 8&)0 Matlab-$ .view &).50#( ,0#/"*, 32($ ,'.#, 6.3-# 6.2 :*' &*).)! +#5$
.rotate3d &).50#( &#2.%"$ #" (5.15 :*' #"*) *#&(1, !2 ,.)6! )"2 :*', !/ ,).40)*#",
view &).50#($ 3)/%&/% #7 ,)(. &)#7$ '.#) :*', 32( !1/ 3).#*# ,)#.* ,)(. &)#7 3)".#%/ 2'*$
,"$, 8(#"$
.6.4 :*' ,"* .,,$',# 4#%)7" &#2.%"$ ,).40*#", &" 3)*)+'%
,+#5, 2#.)$

70
Matlab-! "#$%

!/ 4!5$ #!" 3)1*2 &$., )"2 &2.#$% :*', !/ ,).40*#", &2)$5 .!"0, 3)0&/%$ #!" 3)1*2 $).%
,26view &).50#(

view )*561&3, $$'#$& -&0*:7 )&(0/0 :6.4 !"#

-# surfc &#).50#($ 8()!60 mesh-# surf &#).50#( *#$2 :*'! &6&% *"&% )##5 3' !$5! #00#.*$ 3"
.(*"&% )##5 ,&#2%/%/ contour ,!)%, &" &'.)% c &#",) 3"&,$ meshc
,+#5, "%'#+!

.6.5 :*' &" $)0%

15

10

z 5

0
4

0
3
-2 2
1
0
y -1
-4 -2
-3
x

surfc )*561&3 )&(507' -&-"/ :6.5 !"#

.az=-37.5 el=30 3, )+%% &!& :*' !/ !+6%, &*)*$ )1*2# az=0 el=90 3, )+%% #+ :*' !/ !+6%, &*)*$ )1*2 26

71
Matlab-! "#$%

,"$, 8(#"$ 3)##5, &/* &" +)*#,! 8&)0


,+#5, "%'#+!

.6.6 :*' &" $)0%

15

10

z 5

0
4

0
3
-2 2
1
0
y -1
-4 -2
-3
x

.*&6$ )/" 7,, -&-"/ :6.6 !"#

camlight &).50#( &#2.%"$ )#.* 8##)1$ 27,*#"& :)-#,! 3' 8&)0 )-)( 45)$#" !/ ,/#6& &*).) 82%!
,!"0, &#*#/, &(-#, ,"%'#+! .lighting &).50#( 32 ,*#"&, '#- &" *#6$!#

.6.7 :*'! 6.6 :*' &" &#0/%

.surfl &).50#( &#2.%"$ 3' ,!!.,# ,*#"& )!2$ 3)(*' *#.)! 8&)0 27

72
Matlab-! "#$%

!"#, $"&7) )3+&$ :6.7 !"#

2#$5! 3' 8&)0 .,).50#(, !/ )-6), ,$#'! 3"&,$ 3)0&/% ,).50#(, !/ 3)2$., !+6% &*)*$ *#&$
,,).50#(, &#2.%"$ ,$#'$ &#!& "!! +)6" ,),) 2$., )1

3)2$., -6) &" 3)2$#5 #!" 3)*$)" .1-! 0 8)$ 3)1*2 )!2$ 3)*$)" 3 !2$ *#45# "#, c */"1
5!6 ,').%, ,!$4 8!,! .!"0, *+-, )(! )#.*, 2$., &" 3)$)1*%/ !#61 ,5#*) ,3#+" 3))-)-$,
,&#)*/(",%

.$*9"(& .*(&%* .*('5 "3+0 :6.1 $,'-

73
Matlab-! "#$%

,+#5, &(-#, "%'#+!

.6.8 :*' &" ,$)0%

2-/0, %*27 ('5 )(*'6 :6.8 !"#

,8, &#(-#0 &#)*/("


.grid on &$)&1 )"2 ,*76 ,&5!+,# grid off &$)&1 )"2 3)*)., &/* )#$)1 -
.box off &$)&1 )"2 ,&+*#,# box on &$)&1 )"2 :*', $)$- "-(#5 &$., -
.axis on &$)&1 )"2 ,*76 ,&5!+,# axis off &$)&1 )"2 3)*)., &1*2% )#$)1 -

,waterfall-# meshz 3/$ ,surf-# mesh &#).50#(! &#).")*# &##,%, &#).50#( )&/ +#2 &#%))5
.3)0#/ 42% 3)(*' &#*.)%,
,+#5,

.3"&,$ 6.10-# 6.9 3)(*' &" $)0%

74
Matlab-! "#$%

15

10

z 5

0
4

0
3
-2 2
1
0
y -1
-4 -2
-3
x

meshz )*561&3 :6.9 !"#

15

10

z 5

0
4

0
3
-2 2
1
0
y -1
-4 -2
-3
x

waterfall )*561&3 :6.10 !"#

75
Matlab-! "#$%

/(+' "&&1 6.3

,*"&% )##5 !/ 3)(*' &*).)! &#).50#( 3 &#%))5


3))+%% #+ *"&% )##5 &*).) .1

,+#5, "%'#+!

.6.11 :*' &" $)0%

9*#. 8)" 3" .*"&%, )##5 !/ 3)1*2, &" ').% !"0, 8(#"$ clabel &).50#($ /#%)/,
.!)'*, 8(#"$ ,).50#($ 3)/%&/% 7" #!" 3)1*2$

3))+%% &!& *"&% )##5 &*).) .2

,+#5, "%'#+!

.6.12 :*' &" $)0%

.contour ,).50#(! ,!#2(&$ ,,7 #7 ,).50#(

3)"!% 3))+%% #+ *"&% )##5 &*).) .3

.2$.$ #"!#% *"&%, )##5 8)$ 3)6##*%, &21/ 5* contour ,).50#(! ,%#+ #7 ,).50#(
,(#!61 81!# 3)*#*$ 30)" 3)*(-%, 3)&)2! 9" ,)*/(" clabel ,).50#($ /#%)/ #71 ,'#.&$
9*2, !2 ,+)2%, :*', +.! 2$. &+#%2 ,$).% #7 ,).50#( .colorbar &).(#"$ /%&/,! 8&)0
3)(*'$ &)$)45(" "), 9" )+%% &!& :*' !1$ #7 ,).50#($ /%&/,! 8&)0 .2$. !1 !/ )*%#0,
.(surf, surfc, contourf !/ 3)(*') 3)"!%

,+#5, "%'#+!

.6.13 :*' &" $)0%

76
Matlab-! "#$%

5 5
4 4
2 2

1 6 2
2 8
10
0 9
y 7 4
5 3
-1 3 3

4 4
-2
5 5

2 2
-3
-3 -2 -1 0 1 2 3
x

clabel-& contour )&*561&3 )&(507' "7)0 *&&6 -&-"/ :6.11 !"#

10

8 10

6 9 8 5
4
z
6
4 5 7
3
4 4
2 3
5 2
2 3 4
5
2 3
5 2 2
4
3
0 2

3
2
-2 1
0
y -1
-2
-3
x

clabel-& contour3 )&*561&3 )&(507' "7)0 *&&6 -&-"/ :6.12 !"#

77
Matlab-! "#$%

3 10

9
2
8

1 7

6
0
y
5

-1 4

3
-2
2

-3
-3 -2 -1 0 1 2 3
x

colorbar-& contourf )&*561&3 )&(507' "7)0 *&&6 -&-"/ :6.13 !"#

78
Matlab-! "#$%

6"." 9/2 -4 )".%*' /03' +2#! 6.4

.hold &).50#( &#2.%"$ &2.#$% +)6) :*' !2 3)64/% *(-% &'.,

,"%'#+!
. g(x, y) ' x . y . 4 ,!#$*( +#2 6.3-# 6.2 5*(% "%'#+, 64/%! :)-#0
2 2

,+#5,

.6.14 :*' &" $)0%

%*2* !"# ,( .*2-/0 "3+0 -&-"/ :6.14 !"#

79
Matlab-! "#$%

.,5!6$ &*&-#% ,0#&6&, ,).50#(, 91% ,".#&1# )0/, &" +6" 3)1&#6 3)64/%, )0/ 6.14 :*'$
&'*+ &" 2#$5! 8&)0 .&%))#-% ,+)%$ 3)(#5/ &#),! 3)64/%! 3#*'! "), &)/#%)/ &#*/(" 81!
0 8)$ 3)1*2 !2$ *!5- !$5! !#1) ,7 *4%*( .alpha *4%*( &#2.%"$ :*'$ 3)64/%, !/ &#()5/,
.&4!6#% &#()5/ 0-# ,&4!6#% &#%)4" '.))% 1 */"1 1-!
,"%'#+!
.+$!$ 8#)!2, 64/%, &#()5/ &" ,0/0
,+#5,

.6.15 :*' &" $)0%

alpha "-0"3 .( )&3*6/ )"*5*& %*2* !"# ,( .*2-/0 "3+0 -&-"/ :6.15 !"#

80
Matlab-! "#$%

!"7+1) ,."32 .7
3))(*' 3)45))$#" !/ &#0#1&, .+#2# 4-54 ,3)##5 ,3)*). !/ 3)45))$#" 3)!)1% Matlab-$ 3)(*'
.Handle Graphics &#2.%"$ #" )0+) 8(#"$ )#0)/! &#0&)0

)"0/2$ +",6" !%"-* 7.1

2)(#%/ )(1 (toolbar) ,+#$2, !'*- 9*+ #" Insert 4)*(& 9*+ ,2)$5! &#0&)0 5.3 5*($ &#+#5(,
.7.1 :*'$

8#!6 '-% ,3)##5 &(-#, ,!+',


,1)*2 ;6 4-54# ,3).6 ,045,# :*', $#$)-

45))$#"
8%#-%
7)7,! 8&)0
4-54 !1
,.)6! )"2 !2 ,.)6!
,2#$5 *#&(1,
,1)/%# !/ )0%),
!2 *$12,
45))$#"
8%#-%
&6&#(
4)*(&
&#)*/("

8#!6 6&#(
&#0#1& &1)*2
*+&3*- *3"# 4&,2 :7.1 !"#

.Edit 4)*(& 9*+ )#0)/! &#0&)0 3)*)., 45))$#"# :*', 45))$#" !/ &#0#1&,
!2 ,.)6! .28)#.*, 45))$#", !2 ,.)6!# ,1)*2, ;6 !2 ,.)6! )"2 2.#$% ,1)*2! 45))$#" 8#%)-
.7.1 :*'$ 3'+#%/ )(1 ,1)*2 &#)*/(" &6&#( 8%#-% 45))$#" !2 *$12, !/ )0%), *#&(1,
8&)0 ,7 8#!6$ .&#0#1&, &1)*2 8#!6 &" &6&#( Properties !2 ,.)6! ,45))$#", &#0#1& !1! &/'! )+1
45))$#" !/ &#0#1& &1)*2 8#!6 '.#% 7.2 :*'$ .+#2# 8#0'- ,#5 )$#2 ,2$. #%1 3)$* 3)*4%*( &#0/!
.#5

.,1)*2, ;6 *#&(1 !2 ,.)6! )"2 ,1)*2 $.%% 3)".#) 28

81
Matlab-! "#$%

!1$ 4#4)/
45))$#", '#- :*', !/ 3)45))$#",

#5 '#-% )(*' 45))$#" &#0#1&

)&1&9) )9*"( 4&,2 :7.2 !"#

!/ )0%), *#&(1, !2 ,.)6! ,,1)*2, ;6 &#2.%"$ #0#%)- )"2 :*', &" ,0$% &" &)0+) &#0/! 8&)0
.:*', &" ;##1! ,6#&%! ,7)7,! 8&)0 &21.Unlock Axes Position &*)6$# :*', !#$' !2 *$12,
.:*' !2 :*' &%/, "#, #7 ,0#1& !/ )*/(" /#%)/
,"%'#+
&".%0 #!/ *+&, &$#'&/ *%#!1 .&#/)*+$ +%#2 "#, )1 5#+$! 3).#* Low Pass Filter &))0$$
)#.* 81!# &)02$#& )1, "), ,/)*+, *$2%, *#7" ,.5$ .7.3 :*'$ 3)5#*), 3)##5$ 8%#-%, 3#6&$
:*', !/ 3)*)., *#*6/ )"2 ,7 ,*5%$ ,/20 *$+, .*#7", !/ $)*5& &0#%& 3' :*' #&#" !2 3)/!
3#5)%! #&77,# 3)(*', +6"$ zoom in 2#.)$ ,paste-# copy &#2.%"$ :*', !#(1/ ,*$-#,/ )(1
.)0/, :*'$ )#.*,

)*1%* $"&5' 4&,2 ,( 4&,2 )6'%$ )0#%$ :7.3 !"#

82
Matlab-! "#$%

Handle Graphics -* )"%1""$&( 7.2

-# 3)(*' *.#)# 2+)% ').% Matlab 3&*72$ 3))-)-$, 4#4*/, )$)1* 3, Handle Graphics )45))$#"
.45))$#", !/ &#0#1&, &" &#0/! 8&)0 #1*+/ handle */#5% ,5)(*' 45))$#" !1! .3)-GUI
,7.4 :*'$ *"#&%1 "), Handle Graphics 45))$#" !/ ,)1**),,
Root

Figure

Axes Uicontrol Uimenu Uicontexmenu

Image Light Line Patch Rectangle Surface Text

Handle Graphics -6**'&7 ,/ $*9""*$ :7.4 !"#

)+1 !/%! .:*', )45))$#" !/ &#!&,% 2$#0 Matlab-$ Handle Graphics !/ )1**),, 2$4,
,5)(*' 8#!6 3)$))6 3)*). *)).! )+1 .(Axes) 3)*). &1*2% 9)*. Matlab-$ (Line) #5 44*/!
.(Figure)
.3)0#/ 3)'#-% ,5)(*' )45))$#" *(-% 3)$!#/% +.)1 ,/)6%%, "%'#+ &'.#% 7.5 :*'$

$6*3"# *-6**'&7 "3+0 '&,*/ :7.5 !"#

83
Matlab-! "#$%

.,5)(*' 45))$#" )'#- 3)4*#(% 7.1 ,!$4$

,5)(*' 45))$#" +#5(& 4#*( 45))$#" '#-


,5)(*'
3)".". 3, 3)45))$#", *"/ !1# ,71 +6" 45))$#" 5* 3))5 .9-%, &'#.&$ 4!#/ Root
.#)&#0#1& &" &#0/! 8&)0# )4%#4#" 8(#"$ ,7, 45))$#", &" *.#) Matlab .#!/
.,5)(*', &#0#!6 *(-% &" !)$'% #00)" Matlab .,5)(*' &#0#!6 &*).)$ 3)4!#/ Figure
3, 3)45))$#", *"/ !1# root-, !/ (children) 3)+!) 3, 3)-Figure-, !1
.Figure-, !/ 3)".".,
popup ) 3)6&(0 3)4)*(& !/%! .)(*' 5/%%$ 4!5 3)!$5%, 3)*5$$ 3)4!#/ Uicontrol
45))$#"! axes 45))$#" 8)$ &#!& 8)" ,)1**),, )(! )1 $! 3)/0 ,!/%! .(menu
.Uicontrol
.7.6 :*'$ '.#%/ )(1 ,5)(*' 8#!6 /"*$ 3)%5#%%, 3)4)*(&$ 3)4!#/ Uimenu
*#&(1 &#2.%"$ ,5)(*' 45))$#" !2 3)/)5% */"1 3).., 3)4)*(&$ 4!#/ Uicontextmenu
.)0%), *$12,
image, ) #!/ 3)+!), &" *+-%# :*', '.#) #$/ ,5)(*', 8#!6$ 3#6& *)+'% Axis
.!"0, 3#6&, 9#&$ (light, line, patch, rectangle, surface, text
3)%))5 .colormap 3' )!#"# ,0#%& &.)*4%% $1*#% Matlab-$ ,0#%& 45))$#" Image
indexed, -3)!-5)(, 2$.1 &/*#(% ,0#%&, &.)*4% 8,$ 3)1*+ )'#- ,/#!/
.intensity, truecolor
.surface-# patch )45))$#" !2 3)2)(/%, ,*#"& &#*#5% *)+'% Light
.&)+%%-&!&# &)+%%-#+ ,5)(*'! 3)##5 3)*.#) 3&" 3))-)-$, 3)40%!", Line
&#/%&/% fill #%1 &#).50#( .&##.5 )!2$ 3)"!#%% (3)2!#.% $*) 3)0#')!#( Patch
.#!" 3)45))$#"$
.,-()!"! 8$!% 8)$ 2#0! &#!#1), &#*#. )!2$ 3))+%%-#+ 3)64/% Rectangle
*#/)% !2% ,$#'1 ,.)*4% !/ 40%!" !1 4#4*/ )"2 ,.)*4% !/ )+%%-&!& '#.)) Surface
.x-y
&#/%&/% title, xlabel, ylabel #%1 &#)!2 &#).50#( .3)##& &#7#*6% Text
.#!" 3)45))$#"$

$6*3"# -6**'&7 *#&+ :7.1 $,'-

Uimenu -6**'&7 *"( .*-,/1$ .*-*"3) :7.6 !"#

84
Matlab-! "#$%

!"7+1 (+"-"3 ('"*"*), ('"-4#'7, ('$-/6) !"7+1 (+"-" 7.2.1

45))$#" *.))! )+1 !/%! .3/ #&#" &!2$ 45))$#", &*).)! ,).50#( &%))5 ,5)(*' 45))$#" '#- !1!
&#/%&/% '#1# surf ,contour #%1 *&#) "&#)!)2" &#).50#( .surface &).50#($ 3)/%&/% Surface
*.))! )+1 #!", &#)-)-$, &#).50#($ /%&/,! 8&)0 81! .3)(*' *.))! )+1 #!", &#)-)-$, &#).50#($
.&#/+6 ,5)(*' &#).50#(
,)!!1 8(#"$ 2.#$% &#)-)-$, &#).50#(, &#2.%"$ :*' 45))$#" &*).)

handle = function('propertyname',propertyvalue,...)

,+#5, "%'#+!

7.7 :*' &" *.#)

7.7 !"#

,+#5, &#0/*(
8#!6 )+%%# 3#5)% &" &2$#5 figure 45))$#" !/ position &0#1& .#!/% handle 45))$#" !1!
,3)*$)" 4 !2$ *#45# -"2 ,5)(*',
*left, bottom, width, height +

85
Matlab-! "#$%

,0)(,% ,5)(*', 8#!6 !/ ,0#&6&, &)!"%/, ,0)(, !/ 3#5)%, &" &#'.))% bottom-# left */"1
)"2 &#2$50 &#+)6), .,5)(*', 8#!6 )+%% &" &#2$#5 height-# width .9-%, !/ ,0#&6& &)!"%/,
.Units &0#1&
Axes-$ Color &0#1& ,&"7 &%#2! .,5)(*', 8#!6$ 25*, 2$. &" &0)).% Figure-$ Color &0#1&
.3)*)., &1*2% !/ 25*, 2$. &" &0)).%
.3)*)., &1*2%$ 3)5)4, &" &#2$#5 Ytick-# Xtick &#0#1&
.x-y-z *#/)%$ 64/%, &#2$#5 Zdatat ,Ydata ,XData &#0#1&

,4"7+1 .4"")'6 32 ('#'&( ($")4' (3)4 7.2.2

!1 4#*)( &#*/("% 3' 8, :-#0$ .,5)(*' 45))$#" !/ &#0#1& &#*)76%# &#2$#5 get-# set &#).50#(
.2945))$#", &#0#1& !/ 3))*/(", 3)1*2,
,,5)(*' 45))$#" &0#1& !/ 9*2 &2)$5! )-)-$, 3#/)*,

,,5)(*' 45))$#" &0#1& !/ 9*2 &!$5! )-)-$, 3#/)*,

,0#1&$ $.#)/ 9*2, &"7 &%#2! .&#7#*6% +)%& 3, PropertyName &#0#1& !/ &#%/ )1 $! 3)/0
.)-)-$, 3#/)*$ 8)#.%/ )(1 &7#*6% 6*1,$ &#),! $))6 "! PropertyName

,+#5, "%'#+!

.!#')2 "#, )61#0, marker-, 3" 8#'40(! surface 64/% !/ marker-, &" ,0/%

,"#, ,)#0)/ "!! &%)#-% ,0#1& !/ 3))*/(", 3)1*2, &%)/* &'.,! )-)-$, 3#/)*,

,3#/*0 marker &0#1& !/ 3))*/(", 3)1*2, &" ').,! )+1 !/%!

,!$50#

.!+6% &*)*$ 9*2 &" &#0)).% &#!-!#-%, 3))*'#-,


,+#5, !/%! .set, get &#).50#( &#2.%"$ #*.#0/ 3)(*'$ 3))#0)/ 2.$! 8&)0

Handle Graphics Online Documentation ,*72, ;$#5$ &%))5 3,)&#0#1&# ,5)(*' !/ 3)45))$#", !1 !/ ,%)/*29
.(html 4%*#($)

86
Matlab-! "#$%

.7.8 :*' &" *.))%


.&*.))% plot &).50#(/ line 45))$#"! handle "#, h

7.8 !"#

,+#5,

.7.9 :*'! 7.8 :*' &" ,0/%


.line-, 45))$#" &#0#1& &" !)1%, structure 9*2% "#, a

7.9 !"#

87
Matlab-! "#$%

Editor/Debugger %'35 .8

Editor/Debugger 4&,2 :8.1 !"#

,7 8#!6$ .Matlab-$ &#01&, !/ 2)*1%, $#*, 2.#$% #1*+ 8#!6, "#, Editor/Debugger 8#!6
.Matlab-$ +#5, ).$5 3,/ .m ).$5 3)$&#1
)*6" &6" &#2.#$% &#+#5(, *%#!1 ,script ).$5 "#, +6", .m ).$5 &$)&1! 3)4%*#( )0/ 3)%))5
,*76 &1)-6$ "#, script 4%*#($ )71*%, /#%)/, .Command Window-$ #$&10 8,/ #!)"1 ,)0/,
4!( &#*)76%# 4!5 &#!$5%, &#).50#( "#, )0/, 4%*#(, .Command Window-$ &#+#5( :.* !2
&#).50#(, !1 ,C-$ &#!(%#5%, &#)-)-$ &#).50#( *(-%% ;#6 .3))!$#!' "!# 3))%)0( 3, ,)0&/%/
.30type &).50#( &#2.%"$ #!" &#).50#( &#"*! 8&)0# .m ;$#5$ ,).50#(1 &#)#0$ Matlab !/
9*+ #" Editor/Debugger 8#!6$ F5 *#&(1 !2 #" 8%)-, !2 ,.)6! )"2 &2.#$% .m ).$5 &.*,
.Command Window-,

!(%2'
Dock &)).(#" &*)6$ )"2 &#0#!6, *"/ 32 +6) Editor/Debugger 8#!6 &" $!/! 8&)0 -
.+*(0$ #&#" &6&#( #,/!1 8#!6$ ,0#)!2 &)0%), ,0)($ 8%)-, !2 ,.)6! .View 4)*(&$
.,*#%/ ,00)" ;$#5, !/ &)61#0, ,-*',/ 91 !2 +)2% .m ;$#5 !/ &*&#1$ * 8%)- -

.(C-$ &#!(%#5%, &#).50#( *(-%% ;#6) Matlab !/ &#)4*+04- &#).50#( 81#& &" ,').% type &).50#(30

88
Matlab-! "#$%

+&/4! +/"#" 8.1

2$.$ &#0%#-% &#*2, .#)*6"! )#.*, 4-54, &$)&1# % 3)7#6", 8%)- &#2.%"$ &2.#$% &#*2, &*).)
.8)4#!6! 8,% 3!2&% Matlab -# 5#*)
8&)0 .Text 4)*(&$ Comment &)).(#" &*)6$ #" Ctrl+R &+!5, )"2 ,*2,! 3!/ 245% 9#(,! 8&)0
Uncomment &)).(#" &*)6$ #" Ctrl+T &+!5,# )#.*, 245%, 8#%)- )"2 3)7#6", &" *)-,!
.Text 4)*(&$

.+0' +&-' 8.2

6&(% &#!% .Matlab !/ &#*+',, )(! "!" 8,$ /%&/,! ,*5% !1$ *#-"/ 6&(% &#!% *(-% &#%))5
,6&(%, &#!% 8!,! .!#61 2$.$ &#0%#-%

,"#, 6&(%, &#!%$ !#-( /#%)/! "%'#+

)"&&+ +0-.!& +("#' 8.3

8&)0 #&#2.%"$/ Find & Replace 8#!6 &" &6&#( Ctrl+F !2 ,.)6! #" 8%)-, !2 ,.)6!
,3)##& :)!6,!# "#.%!

.*&&) $3,2$& /&3*2 4&,2 :8.2 !"#

89
Matlab-! "#$%

(Bookmark) !"",'"3 8.4

&)).(#" &*)6$ #" Ctrl+F2 !2 ,.)6!# ,)#.*, ,*#/$ 8%-, &%/, )"2 2.#$% ,))0%)- &+*#,# &(-#,
.Edit 4)*(&$ Set/Clear Bookmark
.,"$, ,)0%)-! 8%-, &" ,*)$2% Edit 4)*(&$ Next Bookmark &)).(#" &*)6$ #" F2 !2 ,.)6!
,)0%)-! 8%-, &" ,*)$2% Edit 4)*(&$ Prev Bookmark &)).(#" &*)6$ #" Shift+F2 !2 ,.)6!
.&%+#5,

&#5#6*, &#)().(- &#*#/ 8)$ *#$2! #00#.*$ 3,$ 3)1#*" .m ).$5 *#$2 *5)2$ &)/#%)/ #7 ,).(#"
.,)0/,% &6"

'%2'
.3).$5 &*)'- &2$ &#*%/0 80)" &#)0%)- -

)""*&'"* )"/&#"1 8.5

,3))/#%)/ 3)*#.)5 *(-% !/ 3#1)- 8!,!

,!#2( *#.)5
copy Ctrl+C
paste Ctrl+V
cut Ctrl+X
undo Ctrl+Z
redo Ctrl+Y
select all Ctrl+A
Balance Ctrl+B
Delimiters31
close file Ctrl+W
new file Ctrl+N
go to line Ctrl+G
find & replace Ctrl+F
add remark Ctrl+R
remove remark Ctrl+T
Set/clear Bookmark Ctrl+F2
Next Bookmark F2
Prev Bookmark Shift+F2
Save Ctrl+S
exit execution Ctrl+C
exit execution Ctrl+Break
exit Matlab Ctrl+Q

3))/#%)/ 3)*#.)5 *(-% :8.1 ,!$4

.8%-, ".%0 8,$/ 3))*'#-, 8)$ ".%0, +#5, !1 8#%)- "), #7 ,).(#" &#2%/% 31

90
Matlab-! "#$%

(Flow Control) ,/"+9 (+4) .9


,Matlab-$ ,%)*7, &*5$ &#+#5( 8!,!

/#%)/ ,+#5(
3))'#! 3)"0& 9%- !2 &#+#5( ;$5% &2.$% else-# elseif &#+#5( 32 +6) &#+#5(
,0&/% !/ 9*2 -"2 &#0#/ &#+#5( ;$5% &2.$% otherwise-# case &#+#5( 32 +6) switch ,)0&,
2#.)$, 8%7$ &#24 ,!'&% 3" &#+#5(, $#&)0 &" ,0/% try…catch &#+#5(
)'#! )"0& -"2 ,3)%2( !/ !$'#% )&!$ *(-% &#+#5( ;$5% &2.$% ,"!#!
2#$5 3)%2( *(-% &#+#5( ;$5% &2.$% for
,"!#!$ &#*&#0, &#+#5(, 2#.)$ "!! while #" for &"!#! !/ ,"$, ,).*4)"! '!+% continue &#+#5(
while #" for &"!#! 2#.)$ &" 5)-(% break ,").)
,! ,"*5/ ,).50#(! *#76! ,.*,! 3*#' return &%+5#%

9.1 $,'-

&2$#5, end &+#5( &(*#.% ,)0&,,# ,"!#!, &#+#5(! .+#5, 2#.)$ *+-$ &#4!#/ !"0, &#+#5(, !1
32
&%" &#),! !#1) )'#! )"0& .)'#! )"0& "#, +#5, 2#.)$ )"0& while-# if &#+#5($ .,+#5(, 3#)- &"
"#, )'#!, )"0&, 3" 5* +#5 &#2.$% #!" &#+#5( )&/ *%#!1 .(0 )*%#0 9*2) *5/ #" (1 )*%#0 9*2)
.&%"
.9.2 ,!$4$ 3)4*#(%, 3))-6) #" 3))'#! 3)*#4*(#"% )'#! )"0& &#0$! 8&)0

3))'#! 3)*#4*(#" 3))-6) 3)*#4*(#"


&#2%/% *#4*(#" &#2%/% *#4*(#"
)'#! AND & 845 K
)'#! OR | ,##/ 845 <=
)'#! NOT ~ !#+' >
)'#! XOR xor ,##/ !#+' >=
33
3)*$)", !1 3" 8#10 all ,##/ ''
-("% 3)0#/
&#6(! 3))5 3" 8#10 any (,0#/) ,##/ #0)" ~=
-("% ,0#/ +6" *$)"

9.2 $,'-

isempty ,).50#(, !/%! .is* 8#0'-,% 3)$.% &#5+#$, &#).50#( &#2.%"$ )'#! )"0& &#0$! 3' 8&)0
.,5)* ,.)*4% "), ,.)*4% 3" &5+#$

,&#"%'#+
,)"0&,

.0 9*2 $)0%

,)"0&,

.1 9*2 $)0%

.)'#! &%"! $/60 -("% ,0#/, )*%#0 9*2 !1 Matlab-$ 32


.,"##/, "), == &#2%/%# ,$., "), = 8%)- &#2%/% )1 *#17! $#/6 .== 3#5%$ = 8%)- &$)&1 "), +"% ,.#(0 &#24 33

91
Matlab-! "#$%

?)'#! )"0&$ 3)*!5- 3#5%$ &#.)*4%$ 3)/%&/% */"1 ,*#5 ,%


34
. !+#' #&#"% &#.)*4% B ,A )1 6)00 ,!/%! .#&#" 3)%))5% 3)*$)", !1 3" 5* &%" "#, )'#!, )"0&,
.3)%"#&, B )*$)" !1% 3)!#+' A )*$)" !1/ "#, A>B )"0&, &#2%/%

.,")'/ &2+#, &!$5&% 7" !+#' #&#"$ 80)" &#.)*4%, 3"34

92
Matlab-! "#$%

if +6&10 9.1

,"), if &+#5( !/ *&#)$ &)-)-$, ,*#.,

*6"! &#+#5(, 2#.)$$ 9)/%) Matlab-# #2.#$) end-, +2 &#+#5(, !1 7" true "#, )'#!, )"0&, 3"
2#.)$$ 9)/%%# end-, +2 &#+#5(, !1 !2 '!+% Matlab 7" false "#, )'#!, )"0&, 3" .end-,
.end-, *6"! &#+#5(,
,"%'#+!

,3)!#5/ 3)"$, 3)%#/)*, )0/ .-("% ,0#/ 9*2 !11 *+'#% true 9*2 Matlab-$

)*$)" !1 */"1 &%" "#, )'#!, )"0&, ,7 ,*5%$ .,.)*4% 3' &#),! !#1) if &+#5( !/ )'#!, )"0&,
.-("% 3)0#/ ,.)*4%,
,!/%!

,*!5-! ,.)*4% 8)$ &"##/,

else if-' else ('0'47 9.1.1

.&(-#0 ,)0&, &#()-#% elseif-# else &#+#5(

else &+#5(

(else if &+#5( #") if-, &+#5( !/ )'#!, )"0&, 3" &#2.#$% ,! &#*#/5, &#+#5(, .)'#! )"0& ,! 8)"
.false ,),
,"%'#+

93
Matlab-! "#$%

else if &+#5(

-, &+#5( !/ )'#!, )"0&, 7" false ,), &%+#5, (else if &+#5( #") if-, &+#5( !/ )'#!, )"0&, 3"
.&#2.#$% ,! &#*#/5, &#+#5(, 7" true "#, 3"# $/#6% else if
,"%'#+!

.else if &#+#5( !/ !$'#% )&!$ *(-%# ,+)6) else-$ /%&/,! 8&)0 if &+#5( 32 +6)$
,&$1*#% "%'#+

!5,! )+1 ,*#/ !1 :#-$ Enter !2 ,.)6! )"2 )4%#4#" 8(#"$ 2.#$%, 5#-)($ /%&/,! +"% ;!%#%
.8)4#!6! 8$#% )&!$! 9(#, +#5, +"% *,% Matlab !/ indenting-$ /#%)/, "!! .$&10, &0$, !2
,indenting "!! +#5 #&#" !/%!

'%2'

,&#0%#-% +#5 &#*#/ !/ (Indenting) 6##)*! &#)*/(" *(-% 80/)


.Ctrl+[ -,!"%/ +6" tab-$ 8%#-%, +#5, &77, .1
.Ctrl+] -,0)%) +6" tab-$ 8%#-%, +#5, &77, .2
.Ctrl+I -6&(%, &#!% )(! 6##)* .3

94
Matlab-! "#$%

switch +"#1,&0 9.2

.(&7#*6% #" *!5-) ,0&/% !/ 9*2 -"2 &#+#5( &2.$% switch &).50#(
,"#, switch &).50#( !/ )!!1, ,0$%,

"

"! "#, expression 9*2 3" .expression !/ 9*2! 3"&,$ 2.#$% +6" case 5* switch &).50#($
,*5%$ &#$* 3)&2! .otherwise-$ &#+#5(, &#2.#$% 7" 3)-case-$ 3)0)#.%, 3)1*2,% +6"
.expression !/ )#(. #" )#.* "! 9*2 !$5&,/ !!'$ 35,.*,, &" 3)5)-(% otherwise
,"%'#+
,*!5- "#, a

,"%'#+
,&7#*6% "#, name

.3)"& 9*2% "#, )"0&, )#4)$ */"1 &)0%7 #$ 3)"0& *(-% 5#+$! ,!#1) switch &).50#(
,"%'#+!

.,.*,, 2#.)$ &" ,5)-(%, error &).50#( 3)$).% 35

95
Matlab-! "#$%

catch-& try +&"#1,&0 9.3

,"#, catch-# try &#).50#( !/ )!!1, ,0$%,

&#+#5($ ,")'/ ,*.#0 "! 3" .,")'/ &*.#0 */" +2 &#2.#$% statements_try &#+#5(, ,7 ,0$%$
&#+#5($ ,")'/ ,*.#0 3" .statements_catch &#+#5( 2#.)$ "!! &5-(0 ,.*,, ,#!"
-$ 3' 3" .statements_catch &#+#5(, 2#.)$! *$#2 Matlab 7" statements_try
.,.*,, &" 5)-(% Matlab 7" ,")'/ ,*.#0 statements_catch

,"%'#+

,")'/! +2 catch &#+#5( &#2.#$% 81! .,")'/ ),#7# 0 -5+0)"$ *#45##! $).,! ,-0% try-$ ,+#5(
.statements_catch-$ ,0#/"*,
&#+#5( 2#.)$ &2$/ $#4 )#1)- /)/ #0! 2#+) */"1 "#, catch-# try &#).50#($ )*5)2, /#%)/,
#2.#$) ,5-()& ,.*,,/ 3#5%$ ,,71 ,*5%$ .+#5, 2#.)$ &" 5)-(&/ ,!5& ,*5& statements_try
.statements_catch &#)$)40*4!", &#+#5(,

96
Matlab-! "#$%

while +(-&- 9.4

3)%2( !/ !$'#% )&!$ *(-% end-! +2 while &+#5( 8)$ &#".%0, &#+#5(, !2 &*7#6 while &"!#!
,while &"!#! !/ )!!1, 3#/)*, .true-! 9(#, )'#!, )"0&,/ +2

.statements &#+#5(, 2#.)$ :#-$ 32( !1 $/#6% )'#!, )"0&$ ,+)%2,-)" */"1
9*2 $).,! )#.* 81!# ,"!#!, 9#&! ,-)01 )0(! &6" 32( $/#6% )"0&, while &"!#!$ )1 $! 3)/0
.,"!#!, 9#&! -01),! )+1 3))5&) "! )'#!, )"0&, ,0#/"*, 32($/ 91 )&!6&,

,"%'#+
.(!!#1 "!) )!)!/, 8#/"*, *$)"! +2 *#45#, )*$)" &" 31-% "$, +#5,

5-()& "! 3!#2! ,"!#!, #$/ $.% 81&) &*6" ,#,/)&% 3))5&) ,*).2, )"0& )1 $! 3)/! $#/6
.Ctrl+Break #" Ctrl+C !2 ,.)6! )"2 &2.#$% ,%#)- )0(! ,.*, &*).2 .,%.2$

97
Matlab-! "#$%

for +&(-&- 9.5

,&)!!1, ,&*#. .3)%2( /"*% 2#$5 *(-% &#+#5( ;$5% &#2.$% for &#"!#!

&#+#5(,% 5!6 &#$#*5 3)&2! ,3!/ *(-% #!)(" #" -5+0)" &#),! $))6 "! i ,0&/%,/ &#*%!
.&#.)*4% #" 3)*#45#! -5+0)" *#&$ i ,0&/%$ &#/%&/%
,"%'#+
.x *#45#$ num *(-%, 2)(#% 3)%2( ,%1 /(6% "$, +#5,

!2 !)2)# *),% 8(#"$ &!2#(, ,(/ "), Matlab .*/(", !11 for &#"!#!% 20%),! ,8+ ;!%#%
.!+#' )*+-$ +#5, &#*),% &" *(/) &#.)*4% !2 &#!2#(, &#)4*+04- &#).50#($ /#%)/ .&#.)*4%
,"$, 8(#"$ !"0, +#5, !1 &" 3/))! 8&)0 ,"%'#+!

*40#"6 +'.4' 9.5.1

3#/)*, &#2%/% .for-, &+#5( &*+',$ ,.)*4%$ /%&/0 ,).*4)" !1$ -5+0)" *#45# !$5! )+1
,)!!1,

.A(:,k) *%#!1 ,A ,.)*4% !/ &)-k-, ,+#%2, "#, -5+0)", *#45# k ,).*4)" !1$/ "),
,"%'#+!
,,.)*4%, ,0#&0
#1 5 9 13 17 $
%2 6 10 14 18 &&
A'%
%3 7 11 15 19 &
% &
(4 8 12 16 20 )
,+#5,

98
Matlab-! "#$%

,Command Window-$ *.))%

.,.)*4%$ &#+#%2, *(-%1 "#, &#).*4)", *(-% )1 $! 3)/0

('63'3 ('$-/6) ,-"+./) !"+)"6 ()-, 9.5.2

.-5+0)" 8%-% i */"1 &#.)*4%# 3)*#45# 9#&$ 3)1*2 3)$).% &#$* 3)&2! for-# while &#"!#!$
,&#"!#! &#2.%"$ ,.)*4%$ 3)*$)" $).,! &#)*5)2 3)1*+ /#!/ &#%))5

,$6*, + &#5)* 3))*'#-$ !#6&)" .1


&#+#5(! 3"&,$ 81+2&)# ,2#+) "! ,0&/%, !+#'/ "#, &#5)* 3))*'#-$ ,0&/% !#6&" &#2%/%
.&#"$,
,"%'#+
.for &"!#! &#2.%"$ abs &).50#( 3#/))

.+6" *$)"$ !+' x_abs ,).*4)" !1$

99
Matlab-! "#$%

#,/!1 !#6&" "!! ,*)/) ,$., .2

.3)1*2, 3)$.#% ,)!" ,.)*4%, !+#' ,),) ,% /"*% 2#+) "!/ &#6)0%# &#!#5/ !"0, 3)1*+, )&/

/"*% 2#+) !+#'! ,.)*4%, !#6&)" .3

&" !6&"! 8&)0 81! .x )*#5%, *#45#, 9*#"! ,,7 &#),! 9)*. x_abs *#45#, 9*#" )1 *#*$ #7 "%'#+$
,"$, 8(#"$ #01+2!# 3)-(" *#45#1 x_abs

?/%&/,! ,4)/ #7)"$

,$)-, .3)-(" &.)*4%$ #&#" !6&"! :)+2 7" 3)1*2 -)01,! 3).#* #)!" ,0&/%, !+#' /"*% 2#+) 3"
#! &#.5,! 9)*. "#, 8#*1)7 ,%1 2+#) "#, ,0&/%, !+#' &" /"*% 2+#) Matlab */"1/ "), 91!
.#! ,.5% "#,/ 8#*1)7, &#%1 &" #" ,0&/%, !+#' &" 81+2! 9)*. #0)"#

100
Matlab-! "#$%

Continue 9.6

&)61#0, ,).*4)"$ &#*&#0, &#+#5(, *"/ !2 &'!+% while #" for &"!#! 9#&$ continue &+#5(
,"!#! 9#&$ &".%0, ,"!#! 9#&% &2.#$% continue &+#5( 3" .,"$, ,).*4)",% ,1)/%% ,.*,,#
.+$!$ &)%)0(, ,"!#!, 9#&$ 5* &#*&#0, &#+#5(, !2 &'!+% continue &+#5( 7" &)0#.)6

,"%'#+
,,"$, X [5 , 7] ,.)*4% ,0#&0

#1 4 2 3 5 3 2$
%1 2 3 4 5 3 1 &&
%
X ' %1 3 4 3 3 3 5&
% &
%4 3 2 1 2 5 4&
%( 3 2 2 5 5 3 3 &)

#)01/ )0/ 32 X-$ 3"#&, *$)", 2.#%% 3, ,)*$)" */" X_new ,/+6 ,.)*4% $/6! 3).#* #0"#
,"), ,!" 3)*$)" $#/)6! "6-#0, .,*#/ ,&#"%
X(i, j " 1) . X(i, j) . X(i, j . 1)
X _ new(i, j) '
3

,,),) X_new ,.)*4%$ (3,3)-, *$)", !/%!

X(i, j " 1) . X(i, j) . X(i, j . 1) 3 . 4 . 3


X _ new(i, j) ' ' ' 3.3333
3 3

*$)", &" &/*#+ ,"##/%, X _ new(4,1) &" $/6! )+1 !/%! .&##.5, *#7"$ "), ,+)6), ,)2$,
!/ *&#)$ &)0%),# *&#)$ &)!"%/, ,+#%2, )1 *)+'0 #7 ,)2$ *#&(! )+1 .3))5 "! 8$#%1/ X(4, 0)
81!# !"0, $#/)6, &" 2.$! 9*#. 8)" #!" &#+#%2 *#$2 .X &#+#%2% &#*)/) #$1*#) X_new ,.)*4%
,3)$#/)6, 2#.)$ !2 '!+! )+1 continue &+#5($ /%&/0

101
Matlab-! "#$%

Break 9.7

&2.#$% break &+#5( 3" .,0%% &".#)# ,"!#!, &" ,5)-(% while #" for &"!#! 9#&$ break &+#5(
3" .+$!$ &)%)0(, ,"!#!! ;#6% &".#) break-, &+#5( 7" &)0#.)6 ,"!#! 9#&$ &".%0, ,"!#! 9#&%
.*.20 ;$#5, 2#.)$ 7" ,"!#! 9#&$ "! 9" .m ;$#5 9#&$ &2.#$% break &+#5(

,"%'#+
.,+#%2 ,&#"% 3)$#*5 3)01/ )(! 2#.)%, &" 2.$! )#.* &21 )1 6)00

#1 4 2 3 5 3 2$
%1 2 3 4 5 3 1 &&
%
X ' %1 3 4 3 3 3 5&
% &
%4 3 2 1 2 5 4&
%( 3 2 2 5 5 3 3 &)

,"), ,"##/%, *%#!1

X(i " 1, j) . X(i, j) . X(i . 1, j)


X _ new(i, j) '
3

&#*#/, !2 '!+! 3).#* #0"/ +#2$ (j) &#+#%2, !2 ,.*/ &"!#! 9#&$ &%5#%% 2#.)%, &"##/% &21
,&#+#%2, &"!#!% &".! )+1 break &+#5($ /%&/0 91 3/! .&#)0#.)5,

102
Matlab-! "#$%

.m "#$1 +#/!' +'61&' !("#" 9.8

3" Command Window-! #" )61#0, .m ;$#5! "*5/ .m ;$#5! ,.*,, &" ,*)76% return &+#5(
.#1*+ ;*#, )61#0, .m ;$#5

"), ()61#0, ;$#5! "*5/ ;$#5! ,").) 5* "! *%#!1) !)!1 .m ;$#5 &.*,% &".! &)/#%)/ 9*+
Command -$ ,').% "), ,&#"/ &7#*6% 4!51 &!$5% #7 ,).50#( .error &).50#( &#2.%"$
.,.*,, &" ,5)-(,/ *6"! Window
,"%'#+
3)$))6 ,&*6" . 0 K a K 10 3#6&$ &#),! $))6 a 9*2, !$" b ,0&/%$ a 9*2, &" $).,! /*+0
.,.*,, 2#.)$ 9/%, &" 5)-(,!
,+#5,

!(%2'
.,).50#( &!$5%/ 4!5 &5)+$! "), #7 ,).50#( !/ ;#(0 /#%)/ -
Command -! ,*,7" &#2+#, 6#!/! )+1 warning &).50#($ /%&/,! 8&)0 81 #%1 -
.+#5, 2#.)$ &" 5)-(,! "! 9" Window

103
Matlab-! "#$%

('"-4#'7 ()"(& .10


,&)!!1 ,).50#( !/ ,0$% !/ )*/(" 4#*)( 8!,!
.,!/ 4!(,# 4!5, )0&/% &"# ,).50#(, 3/ &" ,*)+'% #7 ,*#/ – ,*+', &*#/ .1
.(&#2%/% ,!+#' ,,0&/% '#-) 4!(,# 4!5, *#")& ,,).50#(, &*4% - 3)*$-, .2
.)#.*1 +5(&& ,).50#(,/ )+1 8)5&# )5#6 "#, 4!5, )1 "+##% ,7 5!6 - 4!5 &5)+$ .3
.3)0&/%! 3)&!6&, 3)1*2 &2)$5 - !#6&" .4
.3)$#/)6, !1 3)2.#$% #$ )71*%, 5!6, - $#/)6 .5

,,"$, ,*#.,% 8, Matlab &).50#( !/ &#0#/"*, &#*#/,

6&(% &!)% 4!(, )0&/% ,).50#(, 3/ 4!5, )0&/%

H1 &*#/

!/ help-,
,).50#(,

3/# help &+!5, *%#!1 .,).50#(, !/ help-, &" &#$)1*% ,*+',, &*#/ *6"! +)% &#*2,
,00)"/ ,0#/"*, ,*#/! +2 H1 &*#/% &#*2,, &" &#').% Command Window-$ ,).50#(,
*#&$ Current Directory 8#!6$ 2)(#% ,7 *$-, .,).50#(! *.5 *$-, ,##,% H1 &*#/ .36,*2,
*%#!1 .,).50#( !1 !/ *$-,$ 6&(% &!)% *6" &/(6% lookfor &).50#( :-#0$# Description
.,).50#(, !/ H1 ,*#/$ ,2)(#%, ,!)% &/(6% lookfor &).50#(
,"%'#+

.,5)* ,*#/ !!#136

104
Matlab-! "#$%

,,$)0% Command Window-$ lookfor Calculates &$)&1

"

,,$)0% Command Window-$ help poly_sine &$)&1

.,).50#(! 3)"*#5 3&#2.%"$/ 3)0&/%, &#%/! 3),7 &#),! 3)$))6 30)" ,).50#($ 3)0&/%, &#%/
,&$)&1 !/%!

,#%1 &#".#&, 8&#" &" ,$)0%

,3#/)*, ,/2%!

,,).50#(, !/ 373))!5#!, 3)0&/%! 3))*'#-$ 3)0&/%, &$., 2.$%

.3))*'#-, 9#&$ 3)1*2, &" &#*)/) -)01,! 3' 8&)0 81!


,"%'#+!

.&#".#&, 8&#" &" $)0%

.,).50#(! ;#6% 3)*+'#% "! 3))!5#! 3)0&/%% )1 *#17037

105
Matlab-! "#$%

nargin, nargout, nargchk +&"#1,&0 10.1

(number of input arguments) nargin ("-4#'7 10.1.1

.,).50#( !/ 4!5$ 3)0&/%, *(-% &" ,*)76% nargin &).50#(


,"$, 8(#"$ poly_sine ,).50#(! ,")*5 ,"%'#+!

nargin @ 4
,3)*5% )0/$ *5)2$ ,$#/6 #7 ,).50#(
,")'/ **#' )+% !#+' 3)0&/% *(-% &$., .4!5, &#0)5& &5)+$ "#, ,).50#( !1 !/ $#/6 5!6 .1
.*$-,#
,4!5$ 3)0&/% 5 &$., "%'#+!

!2 4#!/! #00#.*$ 3" .3)*+'#% "! 3)0&/% !2 ,")'/ &#2+#, *#*') )+% 845 3)0&/% *(-% &$.,
*(-% &" ,*)76%, ,).50#(! 3)5#57 #0" #0%24! &#*#*$ *&#) ,")'/ &#2+#, *)+',!# ,")'/, &#2+#,
.,).50#( !/ 4!5$ 3)40%#'*",

.3)0&/% !/ ,0#/ *(-% 32 ,.*,! &#0&)0 Matlab !/ &#)4*+04-, &#).50#(, !/ *1)0 5!6 .2
,4!5$ 3)40%#'*" 1-3 32 ,.)*,! 8&)0 )1 3)+%! #0" diff &).50#( !/ help-,% "%'#+!

&*)*$ )1*2 3)!$5% #*+'#, "!/ 3)1*2, 7" 3#%)-5%,% 3)0&/% &#6( 32 ,).50#( 3).)*% */"1
&#)*/("$ 3)/%&/% 3, 3" &#)*/(", !1 &" 4*(! 3)1)*. 30)" 3)/%&/% 91 .(default) !+6%
,).50#(! 3)5#57 #0" 4!5 )0&/% !/ ,0#/ *(-%$ !(4! &#).50#(! */("! )+1 81! .!+6%, &*)*$
.,).50#( !/ 4!5$ 3)40%#'*", *(-% &" ,*)76%,

(number of output arguments) nargout ("-4#'7 10.1.2

.,).50#(, !/ 4!($ 3)0&/%, *(-% &" ,*)76% nargout &).50#(


,"$, 8(#"$ poly_sine ,).50#(! ,")*5 ,"%'#+!

nargout @ 3
.3)*&#)% 3)$#/)6$ 9#-6! 3).#* */"1 *5)2$ ,$#/6 #7 ,).50#(
?3)0&/% &#6( /5$% /%&/%, 9" 3)0&/% *(-% ,*)76% "),/ 91 &*+'#% ,).50#( */"1 ,*#5 ,%

106
Matlab-! "#$%

.#*76#) 3)0#/"*, 3)0&/%, 5* 9" ,#2.#$) ,).50#($ 3)$#/)6, !1/ "), ,$#/&,
,3#/*0 3#0)!#(, &" 5* !$5! #00#.*$ 3" ,"%'#+!

.(p-$ 3+#5% !$5&,) 3#0)!#(, 9*2 $.#) w ,0&/%$#


./5)$ "! !!1 /%&/%,/ 4!( )0&/% )1*2 $#/)6! 3)$"/% 3)25/#%# *6"% &)07$7$ "), #7 ,/)' 9"
$#/6 3,$ 3)*5% )0/ 3)%))5 ,)!!1 8(#"$ 9" &6" +#5 &*#/ 5* 9#-6) nargout-$ /#%)/ #7 "%'#+$
,nargout-$ /%&/,!
.$#/)6 -%#2 &0)6$% )02$#& 4!(, )0&/%% 5!6 $#/)6 .1
.+"% !#+' ,).50#(! &#")*5, *(-% 9" ,)02$#& "! $#/)6, .2

,3#1)-!
)1*2 &#)*/(" 8)$ *#6$!# 8)5& 4!5$ 3)0&/%, *(-%/ "+##! )+1 nargin &).50#($ 3)/%&/% 1"+$
/5)$ "! /%&/%,/ 3)0&/% $#/)6$ 9#-6! )+1 nargout &).50#($ 3)/%&/% 1"+$ .!+6% &#*)*$
.3&#"
,3"&,$ poly_sine &).50#( &" ,0/0

107
Matlab-! "#$%

'%2'

if, else-$ /#%)/ "), &)$)45!- ,*#.$ 3)$#/)6 2#.)$! switch &).50#($ /#%)/! &)$)40*4!" 9*+
.,).50#(,% &%+5#% ,").)! return 32 +6)
,"%'#+!

nargchk ("-4#'7 10.1.3

20 4!5$ 3)40%#'*", *(-% 3" &5+#$ #7 ,).50#( .nargchk &).50#( "), nargin !2 ,$)$6 ,).")*#
,)!!1, ,%#/)* .high 9*2! low 9*2 8)$

.warning #" error &).50#(! ,2+#,, &" 3)*)76%# nargin-, &).50#(% 3)!$5% number *(-%, &"

,,).50#(, ,0#&0 "%'#+!

,3#/*0 .4-! 2 8)$ "#, ,!/ 4!5$ 8)5&, 3)0&/%, *(-% )1 2$50

,,")'/, &2+#, &" !$50 4!5$ 3)0&/% 5 -)010 ,!/%! ,3"

,,")'/, &2+#, &" !$50 4!5$ +)6) ,0&/% 5* -)010 ,!/%! ,3"

.nargin 3#5%$ nargout &$., )"2 4!($ 3)0&/%, *(-% &5)+$! 3' #7 ,).50#($ /%&/,! 3' 8&)0
,&(-#, "%'#+!

,,")'/ &2+#, &**#' 3)40%#'*" )+% *&#) 32 ,).50#(! ,")*5# poly_sine &).50#($

108
Matlab-! "#$%

+&"#1,&0 -* +&"#1,&0 10.2

$#&1! 3#5%$ &!$5% "),/ ,).50#( !1 !2 3)$#/)6 &2.$%, ,).50#( $#&1! )#.* 3)$* 3)*5%$
8,/!1 &#)4%&% &#).50#( !/ 3)-(", &" &$/6%, ,).50#( "), ,4#/( "%'#+ .,*5% !1! ,).50#(
.('#1# 3)%#0)!#( ,3)-#0)-)
,)!!1 +#5 &#2.%"$/ 91 $&1),! ,1)*. &#).50#( !/ ,).50#(
.,).50#(, 3/ -"2 &#*6" &#).50#( &#2.#$% .1
4!5$ 3)40%#'*" !/ /"*% 2#+) )&!$ *(-% &#!2$ &#*6" &#).50#( &#2.#$% .2
./"*% 3)2#+) )&!$ 3)'#-%

feval ("-4#'7 ('$-/6) %/2 *"$ ('"-4#'7 $'-") 10.2.1

.@ 8%)- &#2.%"$ Matlab-$ *+'#%/ function handle38 '/#%$ &/%&/% feval &).50#(
function -$ &/%&/% feval &).50#( .,).50#(, )$'! 2+)% !)1%, 2+)% ,0$% "#, function handle
.*+-, )(! 3)40%#'*", *"/ &" ,$ ,$).%# ,)#.*, ,).50#(! "#*5! )+1 (8#/"* 40%#'*") handle
,3)!#5/ 3)"$, 3)%#/)*, )0/ ,"%'#+!

&

,81#&% 845 *6$% 8!,! .feval &).50#($ &#/%&/%, &#)4*+04- &#).50#( *(-% &#%))5
. f (x) ' 0 ,"##/%! &#0#*&( &".#% -fzero
.*+'#% !##*40)"$ f (x) !/ &)!5#! 3#%)0)% &+#50 &".#% -fminbnd
.)*%#0 8(#"$ &#)*"0)! "! &#"##/% &1*2% &*&#( -fsolve
.)*%#0 8(#"$ &#!)'* &#)!".0*()+ &#"##/% &1*2% &*&#( -ode45
.)*%#0 8(#"$ f (x) !2 ,).*'40)" &2.$% -quadl

inline *3##"(8

.inline 45))$#" &#2.%"$ "), ,).50#( &*).)! &*.#5% ,*#.


,*)76%# ,+)6) +#5 &*#/ &$/6%, Matlab &).50#( &*.))%# &#7#*6% &!$5% inline &).50#(
&" 3)'.))% 3)40%#'*", *"/ .2.#$&/ +#5, &*#/ &" '.))% 8#/"*, 40%#'*", .+)6) 4!( 40%#'*"
&" &5*#- inline &).50#( .&)4*+04- ,).50#( !/ 3#/)*$ 4!5 )0&/% #%1 3)+5(&% 3,# 3)0&/%,
(...,*,+,sin #%1) &#*1#% &#).50#(# 3)*#4*(#" *6" &/(6%# ,!/ 8#/"*, 4!5, 40%#'*"$ &7#*6%,
.#*+'#,/ 3)0&/%, *6"#

,,,7 8(#"$ &#+5(&%, &#).50#( 3)*.#) 3)"$, 3)%#/)*, )0/ "%'#+!


,).50#( !/ )4*+04- 3#/)* .1

inline 45))$#" .2

help-$ &#4*#(%, &#$)-% 9" function handle-$ /%&/,! 3#5%$ &7#*6% *#&$ ,).50#(, 3/ &" $#&1! 3' 8&)0 38
.function handle-$ /%&/,! :)+2

109
Matlab-! "#$%

,!)'*1 3#/*0 !"0, &#/)', )&/$ -#0)-, &).50#( &" $/6! )+1

#%1 ++#$ $#/)6 &#!2$ &#).50#( &$)&1 *#$2 *&#) *),% ,7 3#/)*/ 91$ "#, inline 45))$#"$ 8#*&),
.+*(0 .m ;$#5$ ,).50#( *)+',! 9*#. 8)"# ,6-#0
45))$#"$ /%&/,! #" ,&6" ,+#5(% *&#) &!2$ ,).50#( $#&1! */(" )"/ "#, ,7 3#/)*$ 8#*-6,
.+6" ,0&/%% *&#) 4!($ *)76,! #" ,*6" inline

,&)4*+04- Matlab &).50#( *#$2 inline 45))$#"$ /#%)/! "%'#+


,&)4%&%, ,).50#(, ,0#&0
ecos( Fx )
f (x) ' , x L [0,5]
x .1

3#%)0)%, &+#50 &" "#.%! )+1 .!"0, 3#6&$ #7, ,).50#(, !/ 3#%)0)% &+#50 "#.%! "), #0)&*4%#
,,"$, ,"##/%, &" *#&(!# -("! &*7'0, &" &##/,! 9)*. )4)!0" 8(#"$

"ecos( Fx ) * F(x . 1) sin(Fx) . 1+


f 9(x) ' '0
0 x . 11
2

fminbnd &).50#( &#2.%"$ )*%#0 8(#"$ 3#%)0)%, &+#50 *6" /(60 81!# +"% ,/5 #7 ,"##/% &*)&(
,+#5, .

.10.1 :*' &" $)0%

,&#),! !#1) feval &).50#( !/ 8#/"*, 40%#'*", ,3#1)-!


.(+*(0 .m ;$#5$ ,).50#( ,*+'#,) function handle .1
.(+*(0 .m ;$#5$ ,).50#( ,*+'#,) &7#*6% .2
.3))/*' "!!# inline 45))$#" .3

'%2'
,!/%! .,!/ 3))*'#-$ &".%0, &7#*6% !1 &2.$%, eval 3/$ ,).50#( &%))5 -

.clear ,+#5(, &" &2.$%

110
Matlab-! "#$%

f(x)=ecos( Fx)/(x+1)
3
ecos(Fx) /(x+1)
x min

2.5

y
1.5

0.5
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
x

)*,6&, .&0*1*0 )%&61 $750 fminbnd )*561&3 :10.1 !"#

111
Matlab-! "#$%

!"#(2/ 32 26+/ +01'/ "(3) +7*/ ('3)4/, ('"-4#'7 (+01, 10.2.2

)1, "%'#+, .3)0&/% !/ /"*% *+'#% )&!$ *(-% 4#!5! &#!'#-% &#),! &#$))6, &#).50#( 80/)
#7 &#*/(" .394#4*/! 3)(*' !/ #,/!1 *(-% !$5! ,!#1), plot &).50#( "), #71 ,).50#( !/ ,4#/(
.feval &).50#( &#2.%"$ ,).50#( !1 !2 &!2#(, &)!!1 ,).50#( !/ ,*+',$ &)0#)6
,,/6%,
,&#"$, (&#)4%&%,) &#).50#(, )&/ !/ 3)-(", &" "#.%! 3).#* #0"

<f (x) ' A sin(Mx . N)


>
?f (x) ' ax . bx . cx . dx . e
4 3 2

x, A, M, N :4!5 !/ 3)*4%*( 4 30/) -#0)-, &).50#(!


x, a, b, c, d, e :4!5 !/ 3)*4%*( 6 30/) !"0, 3#0)!#(, &).50#(!
,"$, 8(#"$ &#++#5% #!" &#).50#(

,3#/*0 -#0)-, &).50#( *#$2 feval-$ /%&/,! )+1

,,"$, ,$.,, &2.$&%#


arg1 @ x arg 3 @ w
arg 2 @ A arg 4 @ phi
.3)40%#'*" )0/ +#2 9)*. 3#0)!#(, &).50#( &" $/6! )+1 9"
,3#/*0 3#0)!#(, &).50#( *#$2 feval-$ /%&/,! )+1 *%#!1

,,"$, ,$.,, &2.$&%#


arg1 @ x arg 3 @ b arg 5 @ d
arg 2 @ a arg 4 @ c arg 6 @ e

feval-$ /%&/,! )+1 .405#)+$ 3)40%#'*" 4 3)$))6 -#0)-, &).50#( 32 feval-$ /%&/,! )+1 *%#!1
&).50#( $#&1! "!" ,*)*$ !1 8)" #7 ,/)'$ 81! .5#)+$ 3)40%#'*" 6 3)$))6 3#0)!#(, &).50#( 32
.&)!!1 ,).50#( "), feval &).50#($ /#%)/, &*4% )1 *#170 9" .,*5% !1! '1(/ feval

,#7 ,)2$ 8#*&(! &#/)' )&/ &#%))5


varargin &).50#( .1
structure 9*2%$ /#%)/ .2

.varagin-$ &/%&/% ,00)" 81!# C-$ ,$#&1 #7 ,).50#( ,/2%!39


.Function Handle !!#1 "!40

112
Matlab-! "#$%

varargin !#)31(0

,"#, varargin &).50#($ /#%)/, 8(#"

+2 4!5, !/ 3)40%#'*", !1 .varargin "#, 8#*6", 40%#'*",# 3)40%#'*" *(-%% $1*#% 4!5,
varargin !/ 3#5)%,% 4!5 40%#'*" !1 .3)!)'* 4!5 )40%#'*" 3, varargin !/ 3#5)%$ 40%#'*"!
.varargin 3/$ 3)"& 9*2% 9#&$ $.#% ,"!,#
,"%'#+
,"$, 8(#"$ &*+'#%, ,).50#( ,0#&0

,"$, 8(#"$ !"0, ,).50#(! ,")*5

,3)1*2, !2$ varargin 3/$ 3)"& 9*2% &*.))%# 2 9*2 x ,0&/%$ ,$).%

.varargin 3/$ 3)"&, 9*2%$ #$.#, )0/, 40%#'*",% !6, 3)40%#'*", !1 *%#!1
9*2%$ 3)/%&/% +.)1 9" .4!5 )40%#'*" !/ #,/!1 *(-% &!$5%, ,).50#( *)+',! 8&)0 ,7, 8(#"$
?,).50#(, 9#&$ varargin 3)"&,
3$).,!# varargin-% 3.!6! 8&)0 7" varargin 3)"&, 9*2%$ #),)/ 2+)%, )0$% )'#- 3)2#+) 3"
.3)%)"&% 3)0&/% 9#&!

,,).50#(, &*+'#% "%'#+!

113
Matlab-! "#$%

,*%#!1 .,*#/ *#45# 9#&! 3)*!5- ,*)%%,

x - *1 2 3 4 5+

&" 4#!5)/ *#45# "#, x )1 *)+',! ,), 8&)0 81!# 3)*!5- #$.#) varargin-$ )1 2#+) ,), !"0, ,*5%$
3)2#+) 30)" varargin-$ #$.#)/ 2+)%, )0$% )'#- */"1 ,*#5 ,% 9" .varargin )"&% 3)*!5-,
?/"*%
,3)"&, 9*2% 9#&% 3)0#/ 3)'#-% 3)0&/%, &" ;!6! ,1)*., feval &).50#( "#, 4!#$ )1, ,*5%,
.3#1)-, &%'#+$ 3'+#)/ )(1 {:} )+#6), *#4*(#", "#, #7 ,)2$! 8#*&(, .,!/ 3))*'#-, 9#&!#

&$/6%, ,"$, ,).50#(, &" 86$0 feval, varargin, function handles &#).50#( !/ 3#1)- &%'#+1
,.m ).$5 ,/#!/ #$&10 3/! .-#0)- &).50#( *#$2 3)'+0# &)!!1 ,).50#( !/ 3)-(", &"
.script ;$#5 .1
.-#0)-, &).50#( .2
.find_zeros_varargin &).50#( .3

.1

!/ 4!5$ 3)40%#'*",
9*2%$ #$.#)/ ,).50#(,
varargin 3)"&,

.2

114
Matlab-! "#$%

.3

115
Matlab-! "#$%

.!$5&% 10.2 :*'

0.8

0.6

0.4

0.2

0
y
-0.2

-0.4

-0.6

-0.8

-1
0 2 4 6 8 10 12 14 16 18 20
x

3F F4
0 E x E 20 .&2)' f (x) ' sin 5 x . 6 +&1*+$ )*561&3 :10.2 !"#
74 38

,&#"$, &#".#&, &#'.#% Command Window-$

,*%#!1 .,).50#( !/ 3))*'#-, 9#&$ *$)" *$)" 3)"&, 9*2% &$.,! !#5/ varargin{:} 5)*4,

&
%

!(%2'
.varargout 3/$ 4!(, )0&/% *#$2 ,%)!/% ,).50#( &%))5 -
.varargout -# ,varargin &#).50#($ &#/%&/% &#)4*+04- Matlab &#).50#( -

116
Matlab-! "#$%

structure 9%2+" /(+#/

4!5$ cell array 9#&$ 3)0#/ 3)'#-% 3)0&/% &$., "), varargin &).50#($ /#%)/, &#2%/%
.structure 9*2%$ ,$., "), &)$)40*4!" ,/)' .,).50#(, 9#&$ /#%)/! 3)"&, 9*2% 9#&% 3.#!)6#

&#2.%"$ 2.#$) 3&)*#'!",/ 91 varargin &).50#($ #/%&/,/ .m ).$5 &/#!/ &" $&1/0 ,7 5!6$
,$&1#/%, +#5, 4#*)( 8!,! .structure 9*2%$ /#%)/
,3, 3)$&1#/%, .m ).$5 ,/#!/
.script ;$#5 .1
.-#0)-, &).50#( .2
.find_zeros &).50#( .3

.1

.2

117
Matlab-! "#$%

.3

118
Matlab-! "#$%

,3, 3).$5$ 3))#0)/,


script-, +#5 .1
3/$ 9*2% *+'#, .3)%)"&% 3)1*2$ structure-, 9*2% !#6&)" find_zeros ,).50#(! ,")*5 )0(!
,,).50#( !1 !/ 3)*4%*(, &" !)1%, params

-#0)-, &).50#( .2
,!"0, structure-, 9*2% 9#&% 413)0&/%, ;#!)6

find_zeros &).50#( .3
./"*% 2#+) 4!5, !/ 3)40%#'*", *(-%# *6"% feval-$ !)'* /#%)/

.varargin &).50#( &#2.%"$ #%1 &#".#&# :*' #&#" !$5&%

!(#)31(0 ./ !(#)31(0 3%0 4(&#$

,)!!1 +#5 &#2.%"$/ 91 $&1),! ,1)*. &#).50#( !/ ,).50#(


function handle) + feval &#2.%"$ ,).50#(, 3/ -"2 &#*6" &#).50#( &#2.#$% .1
.(inline 45))$#" #" &7#*6% #"
4!5$ 3)40%#'*" !/ /"*% 2#+) )&!$ *(-% &#!2$ &#*6" &#).50#( &#2.#$% .2
.structure 9*2% #" varargin &).50#( &#2.%"$ /"*% 3)2#+) )&!$ 3)'#-%

*)/) 8(#"$ structure-,% 3)0&/%, )1*2 &" $).,! 8&)0# )6*1, #00)" structure-, 9*2% 9#&% 3)0&/%, ;#!)6 $!/ 41
.,+/, 3/# ,+#50 8#). 32

119
Matlab-! "#$%

)""-$&-2 )",+*' 10.3

,! #"*5/ &#).50#($ "!# ,).50#( ,&#" 9#&$ 5* 3)*+'#% *%#!1 ,3))!5#! 3, ,).50#( 9#&$ 3)0&/%
!1$ #1*2 &" &#0/! 8&)0# ,).50#( !1$ *+'#% )!$#!' ,0&/% &"7 &%#2! .Command Window-$ #"
.(#%.2 ,0&/%, &" "!" ,0&/%, !/ 5&#2 5* ,0/% #0)" )!$#!' ,0&/% !/ 9*2 )#0)/ *%#!1) 3#5%
.global &).50#( &#2.%"$ &2.#$% 3))!$#!' 3)0&/% &*+',

,0&/%1 ,Command Window-$ #" script ;$#5$ 1"+$ ,#*)+',! 9)*. )!$#!' ,0&/%$ /#%)/ )0(!
*#17! )+1 &#!#+' &#)&#"$ )!$#!' ,0&/% 8)).! ;!%#% ,)6*1, ,7 8)"/ &#*%! .9*2 #$ $).,!# )!$#!'
.)!$#!' ,0&/% 81" #,7 )1

8)"# &#0/! #00#.*$ 8)"/ 3)2#$5 3)!+' &2)$5 ,!/%! .3))!$#!' 3)0&/%! 3)/#%)/ *(-% 30/)
3)0&/%$ /%&/,! 3' 8&)0 .(*#", &#*),%1 )!5)-)( 2#$5 #%1) ,).50#( !1! 3*)+',! #00#.*$
,"%'#+! .,).50#(! 3)"*#5/ 3)%2(, *(-% &" ,!/%! ,3)*(#-, 3)0#% *#&$ 3))!$#!'
,3#/*0 script ;$#5$

,,).50#( *)+'0#

"#*5! 9)*. ,).50#($ )1 $! 3)/0 .1-$ !+' NUM_CALL ,0#%, !"0, ,).50#(! "*50/ 32( !1$
.#$ 3)/%&/%/ )0(! )!$#!', ,0&/%!
do_nothing &).50#( 4!5$ -)01,! 3)1)*. #0)), ,7 ,*5%$ )!$#!' ,0&/%$ 3)/%&/% #0)), "! 3"
,*%#!1 .4!($ #").#,!# ,0#%, &"

,&#$)- )&/% 3))!$#!' 3)0&/%$ /#%)/% ,$#4 &#6( #7 9*+


.,).50#(! ,")*5, 8(#" &" &#0/! 9)*. ,%/))! )+1 .1
&#).50#(/ ,).50#( "), &#")*5, *(-% &" *#(-! 3).#* #0" ,!/ ,).50#(, 3" .2
&#"*#5, &#).50#(, !1 !/ 4!5,# 4!(, &" 3' &#0/! 9)*. 7" ,,! &#"*#5 &#*6"
.,!

'%2'
"! 9" Workspace-,% 5* #&#" 56#% )!$#!' ,0&/% 32 clear &).50#($ 4#/( /#%)/ -
,3#/*0 NUM_CALL#%1 )!$#!' ,0&/% 5#6%! )+1 .8#*1)7,%

120
Matlab-! "#$%

+&"#1,&0 ++ 10.4

&)71*%, ,).50#(, "), ,0#/"*, ,).50#(, .&6" ,).50#(% *&#) !)1,! !#1) ,).50#( '#-% .m ;$#5
,).50#(, )*6" &*+'#%, ,).50#( !1 .,).50#(, 3/$ 3)"*#5 */"1 &2.#$%/ ,).50#(, "),#
,).50#( ,&#" !/ &#*6" &#).50#( &&!# &)71*%, ,).50#(! 5* ,/)'0, ,).50#( && "), &)71*%,
*6"! &#*+'#% 8, +#2 !1 ,0/% #0)" &#).50#( &&, *+- .,!/% ,*+', &*#/ ,).50#( && !1! .&)71*%
.&)71*%, ,).50#(,
,"%'#+

newstats "), &)71*%, ,).50#(, .*#45# !/ 8")+%,# 2.#%%, &" &$/6% newstats ,).50#(,
.mean, median 3/$ ,!/ &#).50#( && )&/ #*+'#,#
&#).50#( 91 *6" ,3/ #&#" &#!2$ &#).50#( && *6" ,!)6& /(6% Matlab ,,).50#(! ,")*5 &2$
,).50#( && #0*+', */"1 !"0, "%'#+$ 81! .search path-$ &#)4*+04- &#).50#( :#-$!# 42&#)4*(
#&#" &!2$ &)4*+04-, ,).50#($ 3#5%$ ,).50#(, &&$ /%&/& newstats &)71*%, ,).50#(, mean
.3/

.private functions "/#0, *6" help-$ #/(6 &#)4*( &#).50#( )$'! *$-,!42

121
Matlab-! "#$%

toolbox +/"#" 10.5

)/"*, 8#!6$ .&#)#.*, &#).50#(, !1 &" ,!)1%, ,)5)& *#.)! ,!)6& 9)*. toolbox *#.)! )+1
!1 3)'.#% ,7 8#!6$ .10.3 :*'! ,%#+$ 2)(#) Set Path 8#!6# Set Path... !2 #.6! File 4)*(&$
#*6$ 31!/ ,/+6, toolbox-, &))5)& &" :)-#,! )+1 .&#).50#( *6" /(6% Matlab 8,$/ &#)5)&,
.,)#.*, ,))5)&, &" #*6$# Add Folder… &)).(#"$

.*-toolbox-$ )"%#$ 4&,2 :10.3 !"#

&).50#($ /%&/,! )+1 !/%! .$)&0 !1% toolbox-$ &#).50#(! ,/)' &*/("% toolbox &*).)
toolbox-$ 3)%/ 1"+$ .mean &).50#( &*+'#% ,$ ,)5)&! &/'! 9*#. 8)" &)4*+04-, mean
,)5)& !1! 8&#" 5)&2,! "!/ )+1 &#0#/ &#)5)&$ 3)".%0, 3)45)#*( *(-%! &#)/#%)/, &#).50#(
./+6%

122
Matlab-! "#$%

(Debugging) ('6"12 %'4"(' (6"-/ .11


,&#")'/ !/ 3)'#- )0/ 3)%))5
.'#1# ,045 &#" 3#5%$ ,!#+' &#" 32 3/ &$)&1 ,*'#- &6)1/ !/%! - syntax &#")'/ .1
8&)0 .,.*,, 8%7 &" 9#-6! )+1 ,01#&, !1 &.*, "!! $)&1 &#")'/ 5#+$! )#.* &#$* 3)&)2!
.m-, ;$#5 3/# pcode &$)&1 &#2.%"$ &"7 &#/2!
.3))5#6 "! 3)1*2 3)!$5&% 3)$#/)6% ,".#&1 !/%! .(runtime) ,.*, &#")'/ .2

&#")'/ 8, runtime &#")'/ &"7 &%#2! .85&!# "#.%! (&)-6)) !5/ &#")'/ 8, syntax &#")'/
-$ 3))!5#!, 3)0&/%, !1# &5-(#% ,.*,, runtime &#")'/ &#!$5&% */"1 ,:-#0$ .*&#) &#5%5%6
-, &)).(#"$ /%&/,! 8&)0 81! .,)2$, &$)- &" 6&0! +"% ,/5 91# 3)56%0 Workspace
.3))!5#!, 3)0&/%! &#/)'0 )+1 9#& ,*#/ ,*#/ +#5, &.*, &" 2.$! )+1 Debugging

5*($ ,'.#,/ 3)-(", &").% &).50#( !/ ,)#'/ ,-*' ').0 Debugging-, 9)!,& &" /)6%,! )+1
8#!6$ */"1 #" Command Window-$ ;$#5, 3/ &+!5, )"2 &2.#$% 3).$5 &.*, .10
.( ) Run *#&(1 !2 #" F5 !2 3).6#! &0%#-% ,)#.*, ,).50#(, Editor/Debugger

,script-, ;$#5

123
Matlab-! "#$%

,,).50#(,

124
Matlab-! "#$%

,,"$, ,".#&, &" ,$)0% script-, ;$#5 &.*, 9" 8)5& ,"*0 +#5, 8)2 &)"*%!

.,")'/ &2+#, ,!$5&,

3#%*
&#*)/) 2)',! 8&)0 .Command Window-$ 3#+" 2$.$ ,")'/ &"+#, ,2)(#% ,,")'/ &*.#0 3"
&2+#,$ 438#&6& #5 32 8%#-%, ,")'/, 3#5)% !2 *$12, 8%- &%/, )"2 8#10, ;$#5$ ,")'/, &*#/!
.*$12, !/ )!"%/, *#&(1, !2 ,.)6!# ,")'/,

,!!#6&, ,$/ ,*#/! 2)'0# *$12, !/ )!"%/, *#&(1, !2 ;6!0# ,2+#,, 3#5)% !2 8%-, &" 3)/0
.find_zero_wrong "!# find_zeros_wrong "), ,).50#(, 3// ,&)), ,7 ,*5%$ &#24, .,")'/,
;6!0 ,.*, &" *#.2! )+1 .*)$- 8%7$ 3))-& "! &)01#&,# script-, ;$#5 &" ;)*0 &#24, 8#5)& *6"!
.Command Window 8#!6$ Ctrl+Break #" Ctrl+C !2
,7)"$ #!)(" #" ,,)2$, ),% &#"*! +$!$ +#5$ &#!1&-,% +"% ,/5 ,3+#5, syntax-, '"$ &%#2!
.,)2$, ;$#5
3)/0 .5-()& ,).50#(, &.*, 8,$/ (breakpoints) ,*).2 &#+#50 &2)$5 "#, '#$)+$ 8#/"*, $!/,
&%))5 ,0)%)%# ,,*#/, *(-% &" ,').% *&#)$ &)!"%/, .&#+#%2 )&/ &#%))5 +#5! !"%/% )1 $!
!"0, 3)##5, !2 *$12, !/ )!"%/, *#&(1, !2 ,.)6! .- 3)##5 3)2)(#% ,)&#*#/% 5!6$/ ,+#%2
&#*#/ 8, ,*).2 &+#50 2#$5! */(" )" 8,$ &#*#/, .44,*).2 &+#50 8)).%, 3#+" !#')2 &2(#,! 3*#'
.&#*2, &#!)1%, &#*#/ #" &#5)*

,5 ,*#/$ ,*).2 &+#50 $).0

,5 ,*#/$ *.2)& ,.*,, &21# script-, ;$#5 &" $#/ ;)*0

.Debug !/ $.%$ "#, Matlab )1 8)).! )+1 K>> 8%)-, 2)(#% &21 Workspace-, 8#!6$

&" 2.$! )+1 .8))+2 ,&#" ,2.)$ "! 9" &)61#0, ,*#/$ &".%0 ,.*,, &21 )1 8)).% "!%, 5#*), ;6,
. Step *#&(1 !2 ;6!0 ,"$, ,*#/! *#$2!# &0%#-%, ,*#/,
&21 Workspace-$ .6 ,*#/! "!%, 5#*), ;6, &" &%+5%# 5 ,*#/ &" &2.$% Step !2 ,.)6!
.x_range ,0&/%, 2)(#%
$).0 .,*#/ ,*#/ !"0, &#*#/, !1 !2 *#$2! *&#)% 81!# 3)0&/% &*+', &2.#$% 5* 10 ,*#/! +2
+2 &#*#/, !1 &" 2.$% ,7 *#&(1 . Continue 45*#&(1 !2 ;6!0# 10 ,*#/$ ,*).2 &+#50
.,*).2 &+#50 +#2 8)"# ,*5%$ ,.*,, !1 3#)- +2 #" ,"$, ,*).2, &+#50!
.3)0)5& 3,/ "+##!# #*+'#,/ 3)0&/%, !1 !/ 31*2 &" &#0/! :"# 8#6$! 8&)0 &21 )1 $! 3)/0
*$12, &77, )"2 #" Array Editor 8#!6 &#2.%"$ ,Command Window-$ 3)0&/%, &" 8#6$! 8&)0
)$'! 2+)% 3)0)).%, data tips 3)"*50, &#0#!6 #.#.) 91# Editor/Debugger 8#!6$ 3)0&/%, !2
.,0&/%,

.40*40)"$ hyperlink-! ,%#+ #7, 3#/)*, &*#. 43


.,*).2, &+#50 &" ,+)*#% 3#+", !#')2, !2 ,.)6! 44
.Continue *#&(1! 9(#, Run *#&(1 Debug $.%$ )1 $! 3)/045

125
Matlab-! "#$%

,"%'#+!

&).50#( 9#&$ &".%0 ,)2$,/ 3))#1)-, $#* 81!# 3))#(. )&!$ 3)*$+ #(.0 "! ,&2 +2
81!# !"0, ,).50#(, 81#& &" 8#6$! #0! */("& "! Step *#&(1 !2 ,.)6! .find_zeros_wrong
."!%, 5#*), ;6, !/ ,*#/$ &)%)0( ,).50#( 9#&! -010 ,7 *#&(1 . Step in *#&(1$ /%&/0
Step in *#&(1 !2 ,.)6! )0(! .2'*1 &".%0 ,.*,, ,).50#( ,7)" 9#&$ 8)).% Stack 8#!6 )1 $! 3)/0
,')., Stack 8#!6

8#!6# find_zeros_wrong &).50#( !/ 46,0#/"*, ,*#/! *#$20 Step in *#&(1 !2 ,.)6! *6"!
,5)*! 9(, ;6, find_zeros_script_wrong ;$#5 !/ 8#!6$ )1 $! 3)/0 .&"7 8)).% Stack

.&0%#-%, ,*#/$ ,! #"*5/ &)%)0(, ,).50#(, 9#&$ &".%0 ,.*,, &21/ "#, ,7 8#%)- !/ &#2%/%,

3))!5#! 3)0&/% 3, #!" 3)0&/% .3)0&/% *(-% #(-#0 )1 $! 3)/0 .40 ,*#/! +2 &#*#/ *(-% 2.$0
.,).50#(! "*5/ script-, ;$#5$ 3)*+'#% 30)"# find_zeros &).50#( !/
3)0&/%, )0/ 81"# stack 8#!6$ /%&/,! 8&)0 script-, ;$#5$ 3)*+'#%, 3)1*2, &" 8#6$! )+1
func, params, 3)0&/%, &/#!/ )1 $! 3)/0 .script-, ;$#5$ 3)*+'#% "! dx, tol 3)/+6,
3)0&/% 3, !"0, 3)0&/%, find_zeros_wrong ,).50#($ 9" 3).$5, )0/$ 3)*+'#% x_range
#0/) "! ,).50#(, 9#&$ 3))#0)/ 81!# script-, ;$#5$ 3)0&/%, !/ 3)5&2, 5* 3, *%#!1 .3))!5#!
.script-, ;$#5$ 31*2 &"
$).0 8%7$ 9#-6! )+1 .8)2! &#"*0, &#)2$ &#!2% 80)" 8#/"*, -(", &").%! +2 &#+#5(, !2 *$2%
9*2 !2 *"/0 x 9*2 &21 )1 $! 3)/0 .(40 ,*#/$ ,*).2, &+#50 &" +)*#0) 53 ,*#/$ ,*).2 &+#50
,0&/%, &6)&( )"2 ,$ 8)6$,! 8&)0 .+"% ,045 "), 9" &#%+5&, &%))5 ,,/2%! .&#%+5&, 8)" 81!# 2
.longG '#.) &*)6$# Array Editor 8#!6$ x
845 "#, &21 )1 0.01 )*#5%, #1*2! #*)76,! 9)*. dx 3#+)5, 9*2 3.%#./ *6"!/ "), '"$! ,$)-,
&" *)+',! "), ,)2$, &" *#&(! ,4#/( 9*+ .,).*4)" !1$ 3+5&% #0)"# 42%1 x 9*2 81!# 10"6 -%
.-(" ".%0 */"1 dx-$ #$).,!# dx_original-1 dx !/ )*#5%, 9*2,
. Exit Debug Mode *#&(1 !2 ,.)6! )"2 Debug Mode-% &".! 9)*. 3))#0)/, &" 2.$! )+1
. Clear all breakpoints *#&(1 !2 ;6!0 ,*).2, &#+#50 !1 &" +)*#,! )+1
,3, 3))#0)/, )0/
"

"

"

.&#*2, #" &#5)* &#*#/ !!#1 "! ,0#/"*, ,*#/, 46

126
Matlab-! "#$%

./+6% script-, ;$#5 &" ;)*0/ )0(! ,).50#($ #02.)$/ 3))#0)/, &" *#%/! #0)!2 )1 $! 3)/0
x &" 3+5% 3&)*#'!", &21# 2 "#, 8#/"*, -(",/ ".%0 ,.*, *6"!# 54 ,*#/$ ,*).2 &+#50 :)-#0
Step out *#&(1 !2 ;6!0# ,*).2, &+#50 &" +)*#0 ,).50#(, &.*, &" 3))-! )+1 .)#.*, 8(#"$
&.*, &" 3))-! )+1 .Step in *#&(1 9*+ ,)!" #0-010/ &)%)0(, ,).50#(, &.*, 3))-! "#, #+)5(&/
.&#%)"&%, &#".#&,# 10.2 :*' !$5&) 7"# Continue *#&(1 !2 ;6!0 ,!#1 ,).50#(,

.)#"*1 !2#( 3&)*#'!", )1 "+##! )+1 4!5 )0&/% !/ 3)0#/ 3)1*2 *(-% 5#+$! )#.* +)%&
,).50#(! ,")*5, &2$ 3" ,*5) ,% .,01#&$ +6" '"$ +#2 *&#0 8)+2 9" 8)5& ,"*0 !1,
, tol-# dx !/ !+6%, &*)*$ )1*2 &" 3' -)010 script-, ;$#5$ find_zeros_wrong

,,")'/, &!$5&%

,*.#0 2#+% *#*$ "! 9" script-, ;$#5 !/ 10 ,*#/! 8%-, &" ")$% ,")'/, 3#5)% !2 ,.)6!
"!! ,")'/, &2.$&% ,$/ ,*#/! +2 ,01#&, &" ;)*,! "), &)/#%)/ +"% ,).(#" 81! .,")'/,
&!!#6&%/ )0(! 2'* 3)0&/%, )1*2 !1 &"# ,")'/, ,2.$&, ,()" 5#)+$ &#"*! !1#0 91 .,2#.)$
.,")'/,
,"$, 8(#"$ Stop if Error &#*/(" !2 ;6!0 Breakpoints 4)*(&$

,,*#/! #0&#" ,!)$#% &21 ,01#&, &.*,

,),) 3)0&/% 5 !/ 4!5/ #001&/ &#*%! 4!5 &")'/ &2+#,! #0&#" ,")$, switch &).50#( *%#!1
,switch &).50#(! &(-#0 ,).(#" &(-#, )"2 "#, ,)2$, 8#*&( .8)5& 4!5

127
Matlab-! "#$%

.8)5& 8(#"$ +5(&% +#5, &21

128
Matlab-! "#$%

Matlab-) !"$'-") +'7"2 12


,Matlab +#5 !/ 3)2#.)$, &" *(/! 3)1*+ *(-% &#%))5
:)!6,! ,/5 ,/2%! .8&)0, !11 while-# for &#"!#! 3#5%$ &#).)*4% &#).50#($ /#%)/ .1
for &#"!#! :)!6,! 8&)0 3)$* 3)*5%$ &"7 &%#2!# &#).)*4% &#!#2($ while &#"!#!
.&#).)*4% &#!#2($
7" !6&#"% #00)" ,0&/% */"1/ "), 91! ,$)-, ./"*% 2#+) 3!+#'/ 3)0&/% !6&"! :)+2 .2
)*%#0 9*2% *#$2 .#!)$/$ 8#*1)7, &".5, )#0)/# #!+#' )#0)/$ 3)$"/% 7$7$! 9)*. Matlab
,).50#($ structure 9*2% *#$2# cell &).50#($ 3)"& 9*2% *#$2 ,zeros &).50#($ /%&/0
.struct
.clear &).50#( &#2.%"$ 3).#60 "! 3)0&/% &5)6%# 3)!#+' 3))0%7 3)0&/% &*).)% &#20%), .3

,,*2,
,,,7 "#, 3)"$, 3)*5%, )0/ $#/)6! 3)/#*+, 3)$"/%,# 8#*1)7, &#%1
.1

.2

Profiler 12.1

&").% "), Profiler-, &*4% .,0%7 &" ,!$% ,01#&, 81), &+)+%! ,4)/ "), Profiling-, &4)/
&#!7#' &#).50#( ,7)" ,"*% Profiler-, .*&#) 3)*),% 3)2#.)$ */("! )+1 &#01#& !/ 5#$5$, )*"##.
.#!" &#).50#( :#52! 3)1*+ "#.%! &#-0! !#1) /%&/%,# ,.*,, 8%7 &"
&#"*#5 8,# 81&) ,!)2) ,*#.$ &#$#&1 8,)0( !2 8,/ &#01#& 81!# &#$1/ *(-% /) 1"+$ +#5!
.3)$"/% &#7$7$%/ ,1#%0 *&#) ,$1/$ &#).50#(!
&.*, !/ &#".#&, :*' 8!,! ,,/6%, 82%! .doc profile #$&1 ,7, )/#%)/, )!1, !2 *&#) +#%!! )+1
.3)-(" &").%! ,01#&,
,+#5,

.12.1 :*'$ 8#!6, &" $)0%

'%2'

&#).50#(, +%.# ,cputime ,).50#(, &#2.%"$ ,).50#( 6*1,$ "! "#,/ +#5 &.*, 8%7 +#+%! 3' 8&)0
.toc-# tic

129
Matlab-! "#$%

profiler 4&,2 :12.1 !"#

130
Matlab-! "#$%

('/04(/ ('"7+1 ('3'$7 .13


)"%/3 +/"#" 13.1

91 3/! .3)1)!,& !/ ,5)%0)+ /)6%,! 3)6)!.% 30)" 3))+%%-&!&# 3))+%%-#+ 3)(*' 3)$* 3)*5%$
.,5!6 ,*#.$ )0/, *6" +6" 3)(*', &.*, )"2 3)(*'% 3)4*- *.))! 8&)0
,Matlab-$ 3)4*- &*).)! 3)1*+ )&/ &#%))5
*/"1 ,!)2) #7 ,4)/ .on-line 3)56%0# 3)44*#/% :*'$ 3)45))$#" :Erase Mode &)).(#" .1
.845 "#, 3)%))*(, 8)$ 3))#0)/,
3)4*- &*.#) #7 ,4)/ .off-line 4*-1 +6) 3&*)%/# 3)(*' *(-% &*).) :movie &).50#( .2
.,$ +5%&0 81!# 3)5!6 *&#)

9#&! getframe &).50#( &#2.%"$ #&#" #*%)/ ,:*' #*.) :*&#)$ 4#/( movie &).50#( !/ 8#)2*,
.movie &).50#( &#2.%"$ 4*-, &" #').,# ,structure '#-% 4*- ,0&/%
4*- &#0$! )+1 (3#&1 2$.$ &#0%#-% &#(-#&,) 3)-(" &").% 3&)*#'!"! &#(-#& *(-% 2.$0 "%'#+!
,3&)*#'!", &#%+5&, &" /)6%%,
,script-, ;$#5

,,).50#(,

131
Matlab-! "#$%

132
Matlab-! "#$%

while &"!#! !/ ,).*4)" !1 9!,%$ .4*-, !/ 3)%))*(, !1 &" !)1%, structure 9*2% "#, M
*6"! .getframe &).50#( &#2.%"$ M-$ $.#% :*', ,,"!#!, !/ ,).*4)" !1 3#)-$ .:*' 44*#/%
Matlab-, 4*- &" *)%% script-, ;$#5 .script-, ;$#5! M &" ,*)76% "), ,,).50#(, &.*, 3#)-
,+#5, :-#0$ .Matlab-! ;#6% ,))(.! 8&)0, avi 4%*#(! M

.47&6" 32( M 4*-, &" ;)*%

0.8

0.6

0.4

0.2

0
y
-0.2

-0.4

-0.6

-0.8

-1
0 2 4 6 8 10 12 14 16 18 20
x

-"+$ ,/ 4&"27$ .**"3$ :13.1 !"#

.:.*$ '.#) 4*-,/ 3)%2(, *(-% &" 2$#5 )0/, 40%#'*", 47

133
Matlab-! "#$%

GUI (Graphical User Interface) 13.2

&" #"*5 ,5)%2% *&#) &#*1),! .)(*' 5/%% &*).) &#)*/(" &" ,*.5$ 5* *5#- ,7 5*(
.,7 5*( $&10 #)( !2/ buildgui.pdf

&#2.%"$ 2.#$% )(*' 5/%% 8#01& .3)-toolbox-, &)$*%$ 3))(*' 3)5/%% Matlab-$ 3)%))5
.13.2 :*'$ *"#&%, 48)(*', 5/%% 8#01& 8#!6 &" &6&#(, guide &).50#(

(Layout Editor) *3"# 6/00 4&19) 4&,2 :13.2 !"#

,3)$!/ ,/#!/! 5!6&% GUI &*).) 9)!,&


.,"*) )(*', 5/%%, +.)1 /"*% 801&! ;!%#% -GUI-, 8#01& .1
.Layout Editor-, &#2.%"$ )(*', 5/%%, !/ )0#.)6, ,42%, &*).) -GUI :*' &*).) .2
!/ +#5(&, &" 801&! 8&)0 #1*+/ .m ;$#5 *.#)% GUI-, :*' &*)%/ &2$ -GUI-, 8#01& .3
.(3)(*' ,3)4)*(& ,3)*#&(1) 45))$#" !1

,"%'#+ &#2.%"$ GUI &*).) !/ 3))-)-$, &#0#*52, &" /)6%0 ,7 5*($


3)(*' &*).)! &#).50#(, &#2.%"$ peaks, membrane, sinc &#)+%%-&!&, &#).50#(, 4#4*/
.)#.*, ,5)(*', 5/%% !/ )&%1- 3)/*& "#, 13.3 :*' .surf, mesh, contour

.Show names in component palette &#*/("$ Preferences-$ *#6$! File 4)*(&$ 9)*. ,7 8#!6 !$5! )+148

134
Matlab-! "#$%

*&5"$ 6/00$ ,/ *)09+ -&-"/ :13.3 !"#

8&)0 Component Palette-,% .13.2 :*'$ 8#!6, &" &6&#( Command Window-$ guide &.*,
.)(*', 5/%%, 8#!6 &" ,##,) "#,/ "Layout Area"-1 13.2 :*'$ 8%#-%, 64/$ 3)45))$#" $).,!

%'35, 30'1 ($")4 13.2.1

,8#!6, !+#' &2)$5! &#)*/(" )&/ &#%))5


.Layout Area-, !/ ,0#&6& &)0%), ,0)($ *#6/, 2#$)*, !2 ,*)*'# ,.)6! -&)0+) .1
.13.4 :*'$ 8#!6, &" &6&#( Property Inspector *#&(1 !2 ,.)6! -Property Inspector .2

Property Inspector 4&,2 :13.4 !"#

135
Matlab-! "#$%

8#!6, $6#* &" &#0/! 8&)0 .45))$#" !1 !/ &#0#1&, &" &#0/! 8&)0 Property Inspector 8#!6$
&#)#.*, &#+)6), &" 2#$5! )+1 .position &0#1&$ height-# width &#0#1& && )#0)/ )"2 #%.2
.Units &0#1&$ 3)*6#$

!/ &#0#1& 2#$5! 8&)0 (Handle Graphics !/ 3)45))$#" 7.2 5*($ *17#,/ )(1) )1 $! 3)/0
)(*' 5/%% "#, Property Inspector 8#!6 ,,/2%! .get-# set &#).50#( &#2.%"$ ,5)(*' 45))$#"
&#2.%"$ #!" &#0#1& ,0/%# get &).50#( &#2.%"$ ,5)(*' 45))$#" !/ &#0#1&, !1 &" ').%,
.set &).50#(

!")"&+ (7*', 13.2.2

Layout -$ )#.*, 3#5)%! #&*)*'# Component Palette -,% $)1* &*)6$ )"2 &2.#$% 3)$)1* &(-#,
,)44- 4-54 ,(push buttons) ,.)6! )*#&(1 ,/#!/ Layout Area -$ $).0 #0!/ "%'#+$ .Area
.&6" 3)*). &1*2%# ,(popup menu) 6&(0 4)*(&
.13.5 :*'$ &'.#% ,".#&,

*29&1 GUI :13.5 !"#

!".4"")'6 +'2"" 13.2.3

8#!6 6&#(, Align Objects *#&(1 !2 ,.)6!# 3)45))$#", &.#$5 &*)6$ )"2 2.#$% 3)45))$#" *#/))
6##*%, &" 2#$5! 8&)0 .)5(#"# )10" *#/))$ 4#!/! 8&)0 ,7 8#!6$ .13.6 :*'$ #%1 Align Objects
.(*#/)) "!! #" 71*% ,!"%/ ,8)%)!) *#/)), '#- &" 3'# 45))$#" !1 8)$ )10",# )5(#",
.!"%/! ,),) )$6#*, *#/)),# 3)!-5)( 20-% $1*#% )10", 6##*%, )1 #02$5 13.6 :*'$ ,"%'#+!

136
Matlab-! "#$%

)10" *#/)) '#-

*(-% )10" 6##)* '#-


3)!-5)(
)10" 6##*%$

)5(#" *#/)) '#-


)5(#" 6##)* '#-
*(-%
3)!-5)(
)5(#" 6##*%$

Align Objects 4&,2 :13.6 !"#

!".4"")'6 32 ('#'&( ($")4 13.2.4

)+1 ,!/%! .Property Inspector-$ 3)*6#$# *$12, !/ )0%), *#&(1, !2 3).6#! ,45))$#"$ 3)*6#$
Property 8#!6 6&()# !"0, &#!#2(, &" 2.$0 ,*&#)$ 8#)!2, Push Button-, !/ &#0#1&, &" &#0/!
.13.7 :*'$ '.#%/ )(1 Inspector

4&*,($ Push Button-$ ,/ Property Inspector 4&,2 :13.7 !"#

137
Matlab-! "#$%

-! 3/, &" ,0/0 String ,0#1&$ .Surf-! *#&(1, 3/ &" &#0/! 3).#* #0" 13.3 :*'$ ,%1-, )(!
4-54, &"# 3)*&#0, Push Button )*#&(1 )0/ &" ,0/0 498(#" #&#"$ .Push Button 3#5%$ Surf
.13.3 :*'$ ,%1-! 3"&,$ )44-,
:*'$ ;6$ 8%#-%) String &0#1& +)! 8.6!, !2 ;6!0 (Popup Menu) 6&(0 4)*(&, &" 2#$5! )+1
.&+*(0 ,*#/$ 8)#.) 6&(0, 4)*(&$ 40%!" !1 .2)(#) String 8#!6# (13.8

4&*,($ Push Button-$ ,/ Property Inspector 4&,2 :13.8 !"#

+#5(&, &" 801&0 #$/ ,"$, $!/, &"*5! .#&#" &0)).%, (Tag) &)##& &%))5 GUI-$ 45))$#" !1!
.)#,)7! &#!5 &)##& 2#$5! )#.* ,45))$#" !1 !/
-,# pushbutton1-1 )4%#4#" 8(#"$ &2$50 Surf ,.)6!, *#&(1 45))$#" !/ &)##&, ,!/%!
GUI 8#!6$ 3)45))$#"! "*#5/ .m ;$#5 *.#)% ,:*', &*)%/ *6"! .<automatic> "#, Callback
.Tag &0#1&$ &7#*6%, &#2.%"$ 2$50 ,%// Callback ,).50#( && &#2.%"$
.13.1 ,!$4 )(! &#)##&, &" ,0/0 "%'#+$

,/+6 &)##& !+6% &*)*$ &)##& 45))$#"


surf_ pushbutton pushbutton1 Surf (Push Button)
mesh_ pushbutton pushbutton2 Mesh (Push Button)
contour_ pushbutton pushbutton3 Contour (Push Button)
data_popup popupmenu1 Popup menu

13.1 $,'-

-! name &0#1& &" ,0/0# :*', !1 &" 8%-0 .GUI-, !/ 3/, "), &#0/! )#.*/ &(-#0 ,0#1&
.simple_gui

;$#5 3' *.#0 ,0#/"*, ,*)%/$ .Activate Figure *#&(1 !2 ;6!0 ,!#2($ GUI-, &" &#"*! )+1
.45))$#" !1 !/ +#5(&, &" 2$#5, .m

.,&0#// ,0#*6", ,0#1&,% 8#%)-, &" +)*#,! /) *%/) )#0)/,/ )+1 )1 $! 3)/049

138
Matlab-! "#$%

!#6&"
!#6&"
&6)&(#
GUI-,

Callback-, &#).50#(-&& &#%/


" &#)##&, 9%- !2 #2$50

&&
&#).50#(
&#2$#5,
+#5(& &"
3)45))$#",
GUI-, !/

139
Matlab-! "#$%

GUI ('#&( 13.2.5

:*', &*)%/ *6"! ./%&/%, )"2 !2(#% 3)"&%, 45))$#", */"1 &#2.#$% Callback &#).50#( &&
.8&#" &01&! /)# &#5)* #!" &#).50#( &&
,&#*4% )&/ ,7 9*2%! .handles 3/$ structure 9*2% "#, GUI-$ $#/6 40%!"
45))$#" !/ handle-, !/%! .GUI-$ 3)45))$#", !1 !/ 3)-handle-, !1 &*)%/ .1
.handles.data_popup "#, data_popup
&& 2#.)$ &2$ 8)%7 ,),) ,7 2+)%/ 91 ,).50#( &&$ 2+)% *#%/! 8&)0 .)!$#!' 2+)% &*)%/ .2
,+#5, !/%! .&#*6" &#).50#(

,-*', &" &*%#/ ,/2%! ,)0/, +#5, &*#/# X 9*2 !2$ handles 9*2%$ /+6 ,+/ *.#)
,4#/( 3#/*0 *6" Callback-$ X-! &/'! )+1 .handles !/ ,/+6,

,popup-, 8#!6 &" &21 801&0

*.))0 ,7 9*2! 3"&,$ ,81! .popup-, 8#!6$ *6$ /%&/%,/ &#*/(", &" !)1% 'Value' 9*2,
.handles-$ *#%/0# $).0 ,3)"&% data

,3)*#&(1, )0.6! &" &21 801&0


3)-Callback-, 81! .popup 8#!6 )"2 2$50, 2+)%,% ,0#/ '#-% :*' *.))! *#%" Push button !1
.3)"&%, :*', &" 3)44*/%# handles-% 2+)%, &" 3)65#! 3,!/

,+#5,

140
Matlab-! "#$%

.Command Line-,% #&#" #.)*, .,!#2(! 81#% )(*', 5/%%, &21

.:*', /"*$ 3)4)*(&$ 4#!/! 3' 8&)0


.13.9 :*'$ 2)(#%/ )(1 New Menu-$ #*6$# Tools 4)*(&$ Menu Editor !2 #.6!

Menu Editor 4&,2 :13.9 !"#

8#). )"2 *.##)&/ Callback-, &).50#(! 3/ #0& .File-1 Label 8#). )"2 4)*(&, 3/ &" #*6$ &21
.Tag-,

141
Matlab-! "#$%

File -*"3) )"*5* -Menu Editor 4&,2 :13.10 !"#

#2$)5 8(#" #&#"$ .New Menu Item !2 #.6!# File-$ #*6$ File 4)*(& !/ 4)*(& && *#.)! )+1
.close 4)*(& && !/ Tag-,# Label-, &"

4)*(& ,/2%! .Close-# File 3)4)*(&, )0/ !/ 3)-Callback-, &" 801&! 5* *&#0 ,*)%/ *6"!
close 4)*(& && !/ Callback-, .#&#" 801&! 9*#. ,7 3#/))$ 8)"# ,.)6! &2$ &)4%#4#" 6&() File
figure1 "#, ,5)(*', 8#!6 !/ Tag-,# *6"% .GUI-, 8#!6 &" *#'-! )+1 delete &).50#($ /%&/%
,"#, +#5, 7"

142
Matlab-! "#$%

!"5#'/ (/"2+
diag 12
disp 33 ,32
A
E abs 100 ,99
add remark 90
Edit 90 ,81 ,43 Align Objects 137 ,136
Editor/Debugger 125 ,123 ,88 ,7 ,6 alpha 80 ,43
else 108 ,94 ,93 ,91 Array Editor 126 ,125 ,15
elseif 93 ,91 avi 133
end 97 ,93 ,91 ,17 axis 74 ,48 ,46 ,45 ,43
Erase Mode 131 Axis 84
error 108 ,103 ,95
Exit Debug Mode 126
exit execution 90
B
exit Matlab 90 Balance Delimiters 90
eye 12 Bookmark 90
box 74 ,43
F break 102 ,91
breakpoints 127 ,125
feval 119 ,114 ,112 ,110 ,109
figure 85 ,43
Figure 86 ,84 ,83
C
fill 84 ,62 ,43 Callback 142 ,141 ,140 ,138
find 127 ,126 ,125 ,119 ,117 ,114 ,90 ,20 ,19 camlight 72
Find & Replace 89 case 95 ,91 ,8
fliplr 30 cat 39 ,34
flipud 30 catch 96 ,91
fminbnd 111 ,110 ,109 cell 129 ,117 ,40 ,39 ,37
for 129 ,102 ,101 ,99 ,98 ,91 celldisp 37
Force white background 43 cellplot 38
format 8 character array 34
fprintf 33 ,32 clabel 77 ,76 ,43
fsolve 109 clc 6
function handle 109 clear 129 ,120 ,110 ,90 ,31
fzero 109 Clear all breakpoints 126
close file 90
G Color 86
colorbar 78 ,76 ,43
get 136 ,86 ,61 ,43 colormap 84 ,43
getframe 133 ,131 Command Window ,88 ,63 ,37 ,33 ,32 ,31 ,9 ,8 ,7 ,6
global 120 135 ,125 ,123 ,120 ,116 ,105 ,104 ,103 ,99
go to line 90 Comment 89
grid 74 ,48 ,43 Component Palette 136 ,135
GUI 142 ,140 ,138 ,136 ,134 ,83 continue 127 ,125 ,101 ,91
guide 135 ,134 contour 138 ,134 ,85 ,77 ,76 ,71 ,43
contour3 77 ,43
contourf 78 ,76 ,43
H copy 90 ,82
handle 140 ,119 ,110 ,109 ,87 ,85 ,83 Copy Figure 43
Handle Graphics 136 ,86 ,83 ,61 Copy Options 43
Handle Graphics Online Documentation 86 ,61 cputime 129
help 146 ,121 ,109 ,106 ,105 ,104 ,63 ,48 ,8 ,7 cumsum 25 ,24
hold 79 ,55 ,54 ,43 Current Directory 104 ,31 ,6
HorizontalAlignment 57 cut 90
cylinder 43
I
D
if 108 ,94 ,93 ,91
Image 84 Data Statistics 59
indenting 94 deblank 32
inline 119 ,110 ,109 delete 142
det 21

143
Matlab-! "#$%

realmax 9 inv 30 ,21


Rectangle 84 isempty 91
redo 90
remove remark 90
repmat 34 ,26
K
reshape 36 ,35 kron 29
return 108 ,103 ,91
rmfield 42
Root 84 L
rotate3d 70 ,43 Layout Area 136 ,135
Run 125 ,123 legend 59 ,43
length 52 ,11
S Light 84
lighting 72
save 90 ,43 ,31 Line 141 ,84 ,83
script ,126 ,125 ,123 ,120 ,119 ,117 ,114 ,88 ,63 ,7 linspace 15
133 ,131 ,127 lookfor 105 ,104 ,8
search 121 ,8
select all 90
set 136 ,86 ,63 ,61 ,43 M
Set Path 122 marker 86
shading 43 Menu Editor 142 ,141
size 11 mesh 138 ,134 ,74 ,71 ,69 ,68 ,43
sort 20 ,19 meshgrid 70 ,68 ,27
Stack 126 meshz 75 ,74 ,43
Step 126 ,125 movie 131
Step in 127 ,126
Step out 127
Stop if Error 127 N
str2mat 32
nargchk 108 ,106
struct 129 ,42 ,41
nargin 108 ,107 ,106
structure ,131 ,129 ,119 ,117 ,112 ,87 ,42 ,41 ,40
nargout 108 ,107 ,106
140 ,133
new file 90
subplot 65 ,64 ,43
num2str 32
sum 35 ,26 ,24 ,23
surf 138 ,134 ,85 ,76 ,74 ,71 ,69 ,68 ,43
surface 86 ,85 ,84 O
Surface 85 ,84
surfc 76 ,71 ,43 ode45 109
switch 127 ,108 ,95 ,91 ones 34
otherwise 95 ,91

T
P
tab 94
Tag 142 ,141 ,138 paste 90 ,82
text 89 ,84 ,66 ,60 ,57 ,43 Patch 84
tic 129 pcode 123
title 84 ,57 ,43 permute 35
toc 129 plot 112 ,87 ,66 ,60 ,52 ,51 ,49 ,44 ,43
transpose 35 ,20 plot3 43
try 91 plotyy 63 ,43
Popup Menu 138
position 136 ,85
U prod 26
Profiler 129
Uicontextmenu 84
Property Inspector 138 ,137 ,136 ,135
Uicontrol 84
Push Button 138 ,137
Uimenu 84
Uncomment 89
undo 90 Q
Unlock Axes Position 82 ,59
quadl 109

V
R
varargin 119 ,117 ,116 ,114 ,113 ,112
VerticalAlignment 57 rand 34 ,13
view 71 ,70 ,43 randn 34 ,13

144
Matlab-! "#$%

Y W
Ydata 86 waterfall 75 ,74 ,43
ylabel 84 ,63 ,57 ,43 while 133 ,129 ,102 ,101 ,99 ,97 ,91
Ytick 86 Workspace 125 ,123 ,120 ,66 ,31 ,15 ,10 ,7 ,6

Z X
Zdatat 86 XData 86
zeros 129 ,127 ,126 ,125 ,119 ,117 ,114 xlabel 84 ,57 ,43
zlabel 66 ,43 Xtick 86
zoom 82 ,43

145
Matlab-! "#$%

('+'4/

OP Matlab help and PDF user manuals


OP Magrab, Azarm, Balachandran, Duncan, Herold, Walsh, An Engineer's Guide
to MATLAB

146
‫שבוע ראשון‬

‫בסיס מטלב‬

‫משתני דאבל‬ ‫‪-‬‬


‫זויות ניתנות ברדיאנים בלבד‬ ‫‪-‬‬
‫‪exp=ex, log10=log10 ,log=ln‬‬ ‫‪-‬‬
‫‪ – format short‬מציג ‪ 5‬ספרות על המסך‬ ‫‪-‬‬
‫‪ – format long‬מציג את כל הספרות בייצוג הפנימי‬ ‫‪-‬‬
‫‪ – format‬מציג את ברירת המחדל‬ ‫‪-‬‬
‫; ‪ -‬ביציוע פקודה בלי הצגה‬ ‫‪-‬‬
‫שם של משתנה חייב להתחיל באות‪ ,‬יכול להיות עד ‪ 31‬אותיות‪ ,‬יש הבדל בין אותיות גדולות‬ ‫‪-‬‬
‫לקטנות‪ ,‬אסור לתת שם של מילים שמורות או ‪ i‬או ‪.j‬‬
‫)(‪ – sqrt‬שורש ריבועי‬ ‫‪-‬‬
‫‪ – inf‬אינסוף ‪1/0‬‬ ‫‪-‬‬
‫‪pi‬‬ ‫‪-‬‬
‫‪ – eps‬אפסילון מספר קטן שאפשר להוסיף לו ‪ 1‬ולראות את השינוי‬ ‫‪-‬‬
‫‪ – NaN, nan‬לא מספר ‪0/0‬‬ ‫‪-‬‬
‫‪ – beep‬עושה צליל של ביפ‬ ‫‪-‬‬
‫‪ – i,j‬שורש של )‪(-1‬‬ ‫‪-‬‬
‫‪53‬‬
‫‪ – bitmax‬מספר שלם מקסימלי ‪2 -1‬‬ ‫‪-‬‬
‫‪ – real min‬מספר ממשי מינימלי‬ ‫‪-‬‬
‫‪ – real max‬מספר ממשי מקסימלי‬ ‫‪-‬‬
‫‪ – who‬שמות המשתנים‬ ‫‪-‬‬
‫‪ – whos‬שמות המשתנים ופרטים עליהם‬ ‫‪-‬‬
‫‪ – save‬שמירת משתנים‬ ‫‪-‬‬
‫‪ %‬הוספת הערת שורה‬ ‫‪-‬‬
‫שורה מעל {‪ %‬ושורה מתחת }‪ %‬נותן קטע של הערה‬
‫‪ – . . .‬המשך בשורה הבאה‬ ‫‪-‬‬
‫‪ – ctrl + c‬מפסיק ריצה‬ ‫‪-‬‬
‫‪ – real‬נותן חלק ממשי‬ ‫‪-‬‬
‫‪ – imag‬נותן חלק דימיוני‬ ‫‪-‬‬
‫‪ – abs‬נותן גודל של מספר מרוכב ‪,‬ושלם‬ ‫‪-‬‬
‫‪ – angle‬נותן זוית של מספר מרוכב ברדיאנים‪ ,‬אם רוצים במעלות צריך לכפול ב‪pi/180‬‬ ‫‪-‬‬
‫]‪A=[x1;y1‬‬
‫]‪B=[x2;y2‬‬
‫)‪ – teta=subspace(A,B‬נותן זווית בין ווקטורים‬ ‫‪-‬‬
‫שם הפונקציה ‪ – help‬עזרה קצרה‬ ‫‪-‬‬
‫שם הפונקציה ‪ – doc‬מסמך עזרה‬ ‫‪-‬‬
‫‪ – clear‬מחיקת משתנים מהזכרון‬ ‫‪-‬‬
‫‪ – pack‬סידור הזיכרון כשהמחשב נגמר לו הזכרון‬ ‫‪-‬‬
‫‪ – pause‬עצירת ריצה עד מקש כלשהו‬ ‫‪-‬‬
‫)‪ – pause(n‬עצירה לכמות שניות‬ ‫‪-‬‬
‫‪ – keyboard‬עצירה לפקודות ידניות עד כתיבת המילה ‪return‬‬ ‫‪-‬‬
‫‪ – return‬החזרה לקובץ‬ ‫‪-‬‬
‫‪ – break‬עצירת תוכנית‬ ‫‪-‬‬
‫‪ – echo on‬מראה כל שורה כשהיא פועלת ‪ echo off‬מפסיק‬ ‫‪-‬‬
‫מערכים‬
‫)(‪ – size‬גודל מערך‬ ‫‪-‬‬
‫)(‪ – length‬גודל סקלרי‬ ‫‪-‬‬
‫)‪ – rand(2,4‬מטריצה של ‪ 2‬שורות ‪ 4‬עמודות של מספרים אקראיים בין ‪ 0‬ל ‪1‬‬ ‫‪-‬‬
‫)‪ – a+(b-a)*rand(5‬נותן מספרים רנדומליים לפי גבולות‬
‫‪ – x(3,2)=3.5‬שים במערך ‪ x‬בשורה ‪ 3‬עמודה ‪ 2‬את ‪3.5‬‬ ‫‪-‬‬
‫)‪ 5 – linspace(0,pi,5‬מספרים מ ‪ 0‬עד ‪ pi‬במרחקים שווים‬ ‫‪-‬‬
‫‪ – 0:5‬מערך של מספרים מ ‪ 0‬עד ‪ 5‬בקפיצות של ‪1‬‬ ‫‪-‬‬
‫‪ - 0:2:10‬מערך של מספרים מ ‪ 0‬עד ‪ 10‬בקפיצות של ‪2‬‬ ‫‪-‬‬
‫)‪ 3 – logspace(0,2,3‬נקודות בין ‪ 100‬לבין ‪102‬‬ ‫‪-‬‬
‫‪a= 5 6 7 8 9 10‬‬ ‫‪-‬‬

‫)‪ 5 <=a(1‬האיבר במיקום הראשון‬

‫)‪ 6 7 8<=a(2:4‬שליפת האיברים מהאיבר ה ‪ 2‬עד האיבר ה ‪4‬‬

‫’)‪6<=a(2:4‬‬
‫‪7‬‬
‫‪8‬‬
‫מטריצות‬
‫=‪A‬‬
‫‪1‬‬ ‫‪2‬‬ ‫‪3‬‬ ‫‪4‬‬ ‫‪5‬‬
‫‪1‬‬ ‫‪41‬‬ ‫‪106‬‬ ‫‪111‬‬ ‫‪616‬‬ ‫‪221‬‬
‫‪2‬‬ ‫‪82‬‬ ‫‪1.27‬‬ ‫‪912‬‬ ‫‪417‬‬ ‫‪2522‬‬
‫‪3‬‬ ‫‪7.23‬‬ ‫‪58‬‬ ‫‪713‬‬ ‫‪118‬‬ ‫‪1123‬‬
‫‪4‬‬ ‫‪04‬‬ ‫‪0.59‬‬ ‫‪414‬‬ ‫‪519‬‬ ‫‪5624‬‬
‫‪5‬‬ ‫‪235‬‬ ‫‪8310‬‬ ‫‪1315‬‬ ‫‪020‬‬ ‫‪1025‬‬

‫)‪A(3,1‬‬
‫‪A(3)=> 7.2‬‬
‫)‪A(1:5,5‬‬
‫)‪A(:,5‬‬
‫)‪A(21:25‬‬
‫)‪A(1:end,end‬‬
‫)‪A(:,end‬‬
‫>=)‪A(21:end‬‬ ‫‪2‬‬
‫‪25‬‬
‫‪11‬‬
‫‪56‬‬
‫‪10‬‬

‫)‪A(4:5,2:3‬‬ ‫‪0.5‬‬ ‫‪4‬‬


‫>=)]‪A([9 14 ; 10 15‬‬ ‫‪83‬‬ ‫‪13‬‬
‫]‪>>a=[1 2 ;3 4‬‬
‫=‪a‬‬ ‫‪1‬‬ ‫‪2‬‬
‫‪3‬‬ ‫‪4‬‬

‫‪>>a(1,4)=5‬‬ ‫הרחבה‬
‫=‪a‬‬ ‫‪1‬‬ ‫‪2‬‬ ‫‪0‬‬ ‫‪5‬‬
‫‪3‬‬ ‫‪4‬‬ ‫‪0‬‬ ‫‪0‬‬

‫שים בעמודה ‪ 3‬את העמודה ‪>>a(:,3)=[4;4] 4 4‬‬

‫שיקוף של ‪>>b=a(:,4:-1:1) a‬‬


‫=‪b‬‬ ‫‪5‬‬ ‫‪4‬‬ ‫‪2‬‬ ‫‪1‬‬
‫‪0‬‬ ‫‪4‬‬ ‫‪4‬‬ ‫‪3‬‬
‫פריסת עמודה )‪b(:‬‬
‫‪5‬‬
‫‪0‬‬
‫‪4‬‬
‫‪4‬‬
‫‪2‬‬
‫‪4‬‬
‫‪1‬‬
‫‪3‬‬

‫מחיקת שורה ][=)‪b(1,:‬‬


‫=‪b‬‬ ‫‪0‬‬ ‫‪4‬‬ ‫‪4‬‬ ‫‪3‬‬

‫‪ – sub2ind(size(a),2,3) -‬מעבר מאינדקס מטריצי לאינדקס יחיד‬


‫‪ – [r,c]=ind2sub(size(a),4) -‬מעבר מאינדקס יחיד לאינדקס מטריצי‬
‫‪1<=true -‬‬
‫‪0<=false -‬‬

‫‪>>a=-3:3‬‬
‫=‪a‬‬ ‫‪-3‬‬ ‫‪-2‬‬ ‫‪-1‬‬ ‫‪0‬‬ ‫‪1‬‬ ‫‪2‬‬ ‫‪3‬‬
‫‪abs(a)>1‬‬
‫‪ð‬‬ ‫‪1‬‬ ‫‪1‬‬ ‫‪0‬‬ ‫‪0‬‬ ‫‪0‬‬ ‫‪1‬‬ ‫‪1‬‬

‫כל האיברים שגודלם גדול מ ‪>>a(abs(a)>1) 1‬‬

‫‪ð‬‬ ‫‪-3‬‬ ‫‪-2‬‬ ‫‪2‬‬ ‫‪3‬‬

‫))]‪>>a(logical([1 1 0 0 0 1 1‬‬ ‫לתת את כל האיבים שיש בהם ‪1‬‬

‫‪ð‬‬ ‫‪-3‬‬ ‫‪-2‬‬ ‫‪2‬‬ ‫‪3‬‬


‫‪ – ones‬מטריצה של ‪1‬‬ ‫‪-‬‬
‫‪ – zero‬מטריצה של ‪0‬‬ ‫‪-‬‬
‫‪ – eye‬מטריצת יחידה‬ ‫‪-‬‬
‫‪ – rand‬מטריצת מספרים רנדומליים‬ ‫‪-‬‬
‫‪ – randn‬מטריצת מספרים רנדומליים בפילוג גאוסיאני‬ ‫‪-‬‬
‫)]‪ – C=randint(6,6,[10,20‬מייצר מטריצה של מספרים רנדומליים שלמים בתחום ] [‬ ‫‪-‬‬
‫‪ – diag‬יוצר מטריצה אלכסונית מווקטור‬ ‫‪-‬‬
‫)‪ – size(a‬מימדים של מטריצה‬ ‫‪-‬‬
‫)‪ – size(a,1‬מספר השורות‬ ‫‪-‬‬
‫)‪ – size(a,2‬מספר העמודות‬ ‫‪-‬‬
‫)‪ – length(a‬נותן את אורך המימד הגדול ביותר‬ ‫‪-‬‬
‫)‪ – numel(a‬מספר האיברים במטריצה‬ ‫‪-‬‬
‫)‪ – reshape(a,2,5‬לסדר מחדש את ‪a‬‬ ‫‪-‬‬
‫)‪ repmat(a,1,2‬או )]‪ – repmat(a,[1 2‬שכפל את מטריצה ‪ a‬פעם אחת למטה ו ‪ 2‬פעמים‬ ‫‪-‬‬
‫ימינה‬
‫)‪ repmat(a,2‬שכפל ‪ 2‬למטה ו ‪ 2‬ימינה‬ ‫‪-‬‬
‫’‪ – a‬טרנפוס ל‪ a‬למספר מרוכב הופך לצמוד שלו‬ ‫‪-‬‬
‫)‪ – x=randperm(6‬סידור אקראי של מספרים מ‪ 1‬עד ‪6‬‬ ‫‪-‬‬
‫)‪ xs=sort(x‬סידור מערך ’‪ ‘descend‬יורד ’‪ ‘ascend‬עולה‬ ‫‪-‬‬
‫)‪ – [xs,ixs]=sort(x‬סידור מערך ושמירת מטריצת האינדקסים‬ ‫‪-‬‬
‫))‪ – [tmp,ind]=sort(m(:,2‬סידור של עמודה ‪2‬‬ ‫‪-‬‬
‫)‪ – m(ind,:‬סידור לפי האינדקס של עמודה ‪ 2‬את העמודות‬
‫)‪ – sort(m,2‬סדר את המטריצה לפי המימד השני )העמודות(‬ ‫‪-‬‬
‫)(‪ – ind=find‬נותן את וקטור האינדקסים שמתאימים לחיפוש‬ ‫‪-‬‬
‫)‪ – x(ind‬נותן את האיברים שנמצאים באינדקסים‬
‫)(‪ – [ii,jj]=find‬נותן אינדקסי מטריצה שמקיימים את התנאי‬ ‫‪-‬‬
‫;)‪>>[row,col]=find(M~=nan‬‬
‫‪>>H=(reshape(row,5,5))+(reshape(col,5,5))+M‬‬
‫מוסיף לאיברי המטריצה את העמודות והשורות שלהם‬

‫‪ – [mx,ind]=max(v) -‬נותן אינדקס ומקסימום בוקטור או את המקסימום בכל עמודה‬


‫כנל ‪min‬‬
‫))‪ max(max(m‬או ))‪ max(m(:‬מקסימום במטריצה‬
‫‪ – flipud -‬שיקוף על ציר אופקי‬
‫‪ – fliplr‬שיקוף על ציר אנכי‬
‫‪ – rot90‬סיבוב ‪ 90‬מעלות נגד כיוון השעון‬
‫)‪ – rot(m,2‬סיבוב ב‪ 90‬פעמיים‬
‫‪ – diag(vec) -‬הופך וקטור למטריצה אלכסונית‬
‫)‪ – diag(m‬מוציא את האלכסון הראשי ממטריצה‬
‫‪ – triu‬אלכסון עליון‬
‫‪ – tril‬אלכסון תחתון‬
‫‪ – squeeze‬מוציא מימד ‪1‬‬

‫‪AX=b‬‬
‫‪X=A-1b‬‬
‫‪-X1‬‬ ‫‪+‬‬ ‫‪X2‬‬ ‫‪+‬‬ ‫‪2X3‬‬ ‫‪=2‬‬
‫‪3X1‬‬ ‫‪+‬‬ ‫‪-X2‬‬ ‫‪+‬‬ ‫‪X3‬‬ ‫‪=6‬‬
‫‪-X1‬‬ ‫‪+‬‬ ‫‪3X2‬‬ ‫‪+‬‬ ‫‪4X3‬‬ ‫‪=4‬‬
‫]‪>>A=[-1 1 2; 3 -1 1; -1 3 4‬‬
‫]‪>>b=[2;6;4‬‬
‫‪ X=A\b‬או ‪>>X=inv(A)*b‬‬ ‫מוצא את הנעלמים‬
‫=‪ð x‬‬
‫‪1‬‬
‫‪-1‬‬
‫‪2‬‬

‫‪ – help ops‬פעולות על מטריצה‬ ‫‪-‬‬


‫‪help matfun‬‬
‫‪ – inv‬מטריצה הופכית‬ ‫‪-‬‬
‫‪ – det‬דטרמיננט‬ ‫‪-‬‬
‫‪ – rank‬דרגה של מטריצה‬ ‫‪-‬‬
‫‪ – eig‬ערכים עצמיים‬ ‫‪-‬‬
‫‪ – norm‬נורמל‬ ‫‪-‬‬
‫‪ – tic‬התחל זמן ‪ toc‬מפסיק מדידה‬ ‫‪-‬‬
‫)‪ – diff(a‬הפרש בין איברים עוקבים במערך‬ ‫‪-‬‬
‫‪ – cumsum‬סכום עוקב‬ ‫‪-‬‬
‫מערכי תאים‬
‫‪ { } -‬שימושאו מימין או משמאל לשווה מכניס לתוכן של תא‬
‫;}]‪>>a(1,1)={[1 2; 3 4‬‬
‫;}‪>>a(1,2)={2+3i‬‬
‫;}’‪>>a(2,1)={‘A string‬‬
‫;}‪>>a(2,2)={3:10‬‬
‫‪>>a‬‬
‫]‪ð [2x2 double‬‬ ‫]‪[2.000 + 3.000i‬‬
‫’‪‘A string‬‬ ‫]‪[1x8 double‬‬
‫)‪ – celldisp(a‬מראה תוכן של מטריצת תאים‬ ‫‪-‬‬
‫)‪ – b=cell(2,3‬מייצר מטריצת תאים ריקה‬ ‫‪-‬‬
‫))‪ – size(a(1,1‬גודל התא‬ ‫‪-‬‬
‫)}‪ – size(a{1,1‬גודל מטריצה בתא‬
‫}’‪ – b(1,1)={‘hi‬איכלוס תא‬ ‫‪-‬‬

‫מחרוזות‬
‫‪ – double(y) -‬הופכת וקטור אותיות לוקטור מחרוזת‬
‫‪ – char([97,97.2,97.9,97+256]) -‬הופכת וקטור מספרים למחרוזת רק מספרים שלמים‬
‫ובמחזורים של ‪ 256‬המחרוזת היא ‪aaaa‬‬
‫‪ - \\ -‬נותן גרש אחד‬

‫)’‪>>a=strvcat(‘noam’,’yossef’,’’,’moshiko‬‬
‫=‪a‬‬ ‫‪noam‬‬
‫‪yossef‬‬
‫‪moshiko‬‬ ‫שירשור בגדלים שונים מתעלם משורה ריקה ומאריך את המערכים‬
‫)‪>>size(a‬‬
‫‪ð 3 7‬‬
‫‪ – strcat(a,b) -‬חיבור מחרוזות ומצמיד אותן‬
‫‪ – deblank(a(1,:)) -‬מעיף רווחים מיותרים בסוף‬
‫‪ – fix(rem(a,256)) -‬שימוש בערכי הסקי‬
‫‪ – int2str -‬הפיכת מספר שלם למחרוזת‬
‫‪ – num2str‬הפיכת מספר כלשהו למחרוזת‬
‫‪mat2str‬‬
‫)‪ – m=str2num(a‬החזרה של מחרוזת למספר מאבדים דיוק פנימי‬
‫‪ ischar(a) -‬בדיקה האם משתנה של תווים‬
‫‪ – class() -‬נותן סוג משתנה‬
‫‪ – eval(‘cos(pi/2)’) -‬מחשב את הפונקציה ואז שומר מספרית‬
‫)‪ – eval(try,catch‬מחשב את התנאי השמאלי לא הצליח מנסה את הבא אחריו‬
‫‪ – evalc‬אותו דבר אבל שומר מחרוזת‬
>>x=sqrt(input('enter num : ')); ‫קליטת מספר מהמשתמש והצגתו בצירוף מחרוזת‬
>>st=['the square root of your number is '];
>>stx=[st,num2str(x)];
>>disp(stx)
‫שבוע שני‬

‫גרפים דו מימדיים‬

‫‪ plot(xdata,ydata,’color_linestyle_making’) -‬סרטוט של גרף אחד‬


‫)‪ plot(x1,y1,’clm1’,x2,y2,’clm2’,….‬סרטוט של כמה גרפים‬

‫;)‪>>x=linspace(0,2*pi,50‬‬
‫;)‪>>y=sin(x‬‬ ‫ייצוג וקטורי‬
‫)‪>>plot(y‬‬ ‫מייצר גרף פשוט בצבע כחול)ברירת מחדל(‬
‫או‬
‫)‪>>plot(x,y‬‬ ‫ביחד עם ציר ‪x‬‬
‫‪----------------------------------------‬‬
‫;)‪>>xx=linspace(0,4*pi,50‬‬
‫;)‪>>yy=cos(xx‬‬
‫)‪>>plot(x,y,xx,yy‬‬ ‫ציור של שני גרפים‬
‫‪-----------------------------------------‬‬
‫;)’)‪>>y=(sin(x)’,cos(x‬‬ ‫ייצוג מטריצי של ‪ y‬ויצוג וקטורי של ‪x‬‬
‫)‪>>plot(x,y‬‬
‫‪------------------------------------------‬‬
‫;]‪>>x=[x,x+2*pi‬‬
‫;]))‪>>y=[sin(x(:,1)),cos(x(:,2‬‬
‫)‪>>plot(x,y‬‬ ‫שני המשתנים מטריצות‬

‫‪ - hold‬משנה אם הגרף יימחק או לא בפעם הבאה שעולה גרף כל פעם שנכתב משנה מצב‬ ‫‪-‬‬
‫‪ on‬ו ‪ off‬גם משנים‬
‫‪ – grid‬קווי עזר לגרף אותו הדבר הפעלה כמו ‪hold‬‬ ‫‪-‬‬
‫‪ – zoom‬ניתן לשנות דרך העכבר בזכוכית מגדלת ומופעל כל פעם כמו ‪hold‬‬ ‫‪-‬‬
‫)]‪ – axis([xmin xmax ymin ymax‬זום דרך המקלדת לגרף‬ ‫‪-‬‬
‫‪ – v=axis‬נותן גבולות גרף‬
‫‪ – axis auto‬מצב ברירת מחדל של הגודל‬
‫‪ – axis manual‬מקפיא את הגודל הנוכחי שלא ישתנה‬
‫‪ – axis normal‬מחזיר למצב רגיל‬
‫‪ – axis on off‬מוריד ומחזיר מארכת צירים‬
‫ניתן לשנות דרך ‪ plot tools‬דרך הסרגל כלים‬
‫‪ – axistight‬מתאים את הגרף בדיוק על הסרטוט‬ ‫‪-‬‬
‫‪ – axisfill‬בדיוק להפך‬
‫‪ – axis equal‬מחלק את מערכת הצירים בצורה שווה‬ ‫‪-‬‬
‫‪ – axis square‬מגדיל את הגבולות בצורה שווה‬ ‫‪-‬‬
‫)’‪ – title(‘string‬כותרת ראשית‬ ‫‪-‬‬
‫)’‪ – xlabel(‘string‬כותרת לציר ‪X‬‬
‫)’‪ – ylabel(‘string‬כותרת לציר ‪Y‬‬
‫)’)‪ – text(pi/3,sin(pi/3),’<--sin(\pi/3‬ציון נקודה על הגרף \ מציג את האות פאי‬
‫)’‪ – legend(‘string1’,’string2‬מקרא לגרף‬
‫כל אלה ניתן גם להוסיף דרך העכבר עם אפשרות ‪insert‬‬
‫‪ – figure‬חלון גרפי נוסף‬ ‫‪-‬‬
‫הפעלה של חלון דרך לחיצה עליו עם העכבר או )‪figure(n‬‬
‫)‪ – close(n‬סגירה של חלון‬ ‫‪-‬‬
‫‪ – close all‬סגירת כל החלונות‬
‫‪ – clf‬מוחק תוכן של חלון וממלא אותו בפעם הבאה‬ ‫‪-‬‬
‫)‪ – subplot(rows,cols,index‬מספר גרפים כאשר נותנים להם מימדים ומיקום‬ ‫‪-‬‬
‫חייב להיות זהה המימדים בכל הגרפים‬
‫)‪ x – loglog(1:1000‬לוגריתמי וגם ‪ y‬לוגריתמי‬ ‫‪-‬‬
‫)‪ – semilogy(1:1000‬רק ‪ y‬לוגריתמי‬
‫)‪ – semilogx(1:1000‬רק ‪ x‬לוגריתמי‬
‫)‪ plotyy(x,y,x,z‬שני צירים אנכיים ניתן רק עבור ‪ 2‬עקומות בלבד‬ ‫‪-‬‬
‫‪ – drownow‬צייר גרף ותמשיך לא מחכה לקומפילציה ניתן כך לראות עדכוני גרף‬ ‫‪-‬‬

‫‪>>plot(x,y),axis square,grid‬‬ ‫מריץ כך גרפים יותר מהר‬

‫‪ – ezplot(string,[-15,15]) -‬לוקח מחרוזת והופך אותה לפונקציה ומצייר את הגרף בתחום‬


‫בין ‪ 15‬ל‪-15‬‬
‫‪ ^{super} -‬או }‪ – _{sub‬נותן חזקה ובסיס את מה שכתוב בתוך הסוגריים‬
‫‪ – \bf‬הדגשה‬
‫‪ – \it‬נטוי‬
‫‪ \rm‬הסרה של פקודה‬
‫הפניה דרך ‪latex‬‬
‫‪ – help plot -‬טבלת צבעים‬
‫צבע‬ ‫סוג קו‬ ‫צורת קו‬
‫כחול ‪b‬‬ ‫‪.‬‬ ‫נקודות‬ ‫‪-‬‬ ‫קו רציף‬
‫ירוק ‪g‬‬ ‫עיגולים ‪o‬‬ ‫קו מנוקד ‪:‬‬
‫אדום ‪r‬‬ ‫איקסים ‪x‬‬ ‫קו נקודה ‪-.‬‬
‫תכלת ‪c‬‬ ‫פלוסים ‪+‬‬ ‫קו מקוטע ‪--‬‬
‫סגול ‪m‬‬ ‫כוכביות *‬
‫צהוב ‪y‬‬ ‫ריבועים ‪s‬‬
‫שחור ‪k‬‬ ‫יהלומים ‪d‬‬
‫משולשים תחתונים ‪v‬‬
‫משולשים עליונים ^‬
‫משולשים שמאליים <‬
‫משולשים ימניים >‬
‫כוכבים מחומשים ‪p‬‬
‫מגן דוד ‪h‬‬
‫תנאים לוגיים‬
‫‪ – zero = false -‬בכניסה אפס נותן שקר כל דבר אחר נותן אמת ביציאה שקר נותן ‪ 0‬אמת נותן‬
‫אחד‬
‫‪ - <,<=,>,>=,==,~= -‬פעולות השוואה‬
‫‪ – (a.*(a>4)) -‬משאיר רק איברים שמקיימים את התנאי‬
‫‪>>x=(-3:3);sin(x)/x‬‬
‫‪x=x+(x==0)*eps‬‬ ‫כאשר יש בעיית גבול לוקח הרבה זמן במערך גדול‬
‫‪---------------------------‬‬
‫;)‪>>x=(-3:3‬‬
‫))‪>>y=ones(size(x‬‬
‫=‪y‬‬ ‫‪1‬‬ ‫‪1‬‬ ‫‪1‬‬ ‫‪1‬‬ ‫‪1‬‬ ‫‪1‬‬ ‫‪1‬‬
‫‪>>tf=x~=0‬‬
‫=‪tf‬‬ ‫‪1‬‬ ‫‪1‬‬ ‫‪1‬‬ ‫‪0‬‬ ‫‪1‬‬ ‫‪1‬‬ ‫‪1‬‬
‫)‪>>x(tf‬‬
‫‪ans= -3‬‬ ‫‪-2‬‬ ‫‪-1‬‬ ‫‪1‬‬ ‫‪2‬‬ ‫‪3‬‬
‫ככה לא מבצע את הגבול יותר מהיר )‪y(tf)=sin(x(tf))./x(tf‬‬

‫‪AND - & -‬‬


‫‪OR - | -‬‬
‫‪NOT - ~ -‬‬
‫סדר קדימויות‬
‫ראשון למעלה אחרון למטה‬
‫)(‬
‫^או ^‪ .‬או ' או '‪.‬‬
‫~‬
‫\ או ‪ /‬או * או *‪.‬‬
‫‪ +‬או ‪-‬‬
‫‪:‬‬
‫> או <‬
‫&‬
‫|‬

‫)‪ OR – xor(x,y‬בין התנאים‬ ‫‪-‬‬


‫)‪ OR – any(x‬לכל עמודה בוקטור יחיד בודק את הכל‬ ‫‪-‬‬
‫)‪ AND – all(x‬לכ עמודה‬ ‫‪-‬‬
‫‪ – isglobal‬האם משתנה גלובלי‬ ‫‪-‬‬
‫‪ – isempty‬האם משתנה ריק‬ ‫‪-‬‬
‫‪ – isequal‬האם משתנה זהה‬ ‫‪-‬‬
‫‪ – isfinite‬האם משתנה סופי‬ ‫‪-‬‬
‫‪ – isprime‬האם מספר ראשוני‬ ‫‪-‬‬
‫בבדיקת שאלה ניתן לבדוק ‪ inf‬אך ‪ nan‬לא ניתן רק בפקודה ‪isempty‬‬ ‫‪-‬‬
‫בקרת זרימה לולאות ותנאים‬
for x=array
commands
end ‫פקודות מבוצעות פעם אחת לכל עמודה במערך‬
‫מומלץ להגדיר את המשתנה של התוצאה בחוץ חוסך זמן‬
-------------------------------------
while expration
commands
end
--------------------------------------
if expration
commands
end
----------------------------------------
if expration
commands
else
commands
end
----------------------------------------
if expration1
commands
elseif expration2
commands
elseif expration3
commands
……
else
commands
end
----------------------------------
switch expration ‫סקלר או מחרוזת‬
case expration1
commands
case {expration1,expration2} ‫הפסיק הוא או ביניהם‬
commands
……………..
otherwise
commands
end
------------------------------------
try
commands1
catch
commands2
end ‫מנסה אפשרות ראשונה ואז שנייה אם יש בעיה בריצה של הראשון‬

>>lasterr ‫מציג את השגיאה שהוציאה האפשרות הראשונה‬

‫ – קפיצה לסוף לולאה‬continue -


‫ – יציאה מלולאה‬break -

‫דוגמאות‬
‫· ניוטון ראפסון‬

xi=input('enter xi to start from : ');


points=input('enter how many numbers aqurate you want after the point : ');
flag=0;

for count=1:100000
fx=2*xi+exp(xi/pi)+sin(xi*pi)-20; % ‫הפונקציה‬
ftx=2+(1/pi)*exp(xi/pi)+pi*cos(pi*xi); % ‫הנגזרת‬
xin=xi-(fx/ftx);
if abs((xin*10^points)-(xi*10^points))<1
flag=1;
break
end
xi=xin;
end
if flag==0
disp('no solution can be found for your input')
else
xin
count
end
0 ‫סידור מטריצות לפי גודל סכומן עד סכום הכנסה‬ ·

first=1;
ind=0;
while first | msum(ind)
first=0;
ind=ind+1;
m{ind}=input('Enter matrix : ');
msum(ind)=sum(sum(m{ind}));
end
msum(ind)=[];
[x,xi]=sort(msum);
MM=m(xi);
celldisp(MM)
‫שבוע שלישי‬

‫פונקציות‬

‫הכרזה על פונקציה – בתחילת הקובץ )…‪function res=dosomthing(exp1,exp2,‬‬ ‫‪-‬‬


‫‪ – exp‬ביטויים שמקבלים בקריאה לפונקציה‬
‫‪ – res‬משתנה כלשהו שיחזיר את התשובה מהפונקציה )לא חייב להיות החזר(‬
‫‪ – dosomething‬שם הפונקציה וגם שם הקובץ‬
‫‪ - %‬כל מה שלפניו אחוז אחרי ההכרזה של הפונקציה יופיע ב‪ help‬של הפונקציה‬ ‫‪-‬‬
‫קריאה לפונקציה נעשית לפי שם הקובץ‬ ‫‪-‬‬
‫הפונקציה לא שומרת משתנים פנימיים‬ ‫‪-‬‬
‫‪ - lookfor‬חיפוש עזרה‬ ‫‪-‬‬
‫‪ – return‬מוציא מפונקציה‪ ,‬לא חייב להופיע‬ ‫‪-‬‬
‫‪ – nargin‬כמה משתנים הוכנסו לפונקציה‬ ‫‪-‬‬
‫‪ – nargout‬כמה משתנים המשתמש ביקש‬
‫)‪ – function y= myfunc(x,n,b,t,vargin‬אינסוף משתני כניסה‬
‫]‪ – function[a,b,vargout‬אינסוף משתני יציאה‬
‫)’‪ – error(‘string‬מציג אזהרה באדום למשתמש‬ ‫‪-‬‬
‫)’‪ – warning(‘string‬מציג אזהרה יותר ידידותית בשחור‬
‫משתנים בסביבת הפונקציה ובסביבת הבסיס‬ ‫‪-‬‬
‫‪ – global a‬משתנה גלובלי‬
‫‪ – persistent b‬משתנה בפונקציה ששומר על ערכו אחרי ההרצה של הפונקציה‬
‫‪ – clear all‬מחיקת כל המשתנים ואיפוס של המשתנים בופונקציות‬
‫)’‪ – a=evalin(‘caller’,’expration‬ביצוע ‪ eval‬בפונקציה או בסביבת בסיס‬ ‫‪-‬‬
‫)’‪a=evalin(‘base’,’expration‬‬
‫)’‪a=evalin(‘caller’,’try’,’catch‬‬
‫)’‪a=evalin(‘base’,’try’,’catch‬‬
‫‪ – inmem‬לבדוק איזה פונקציות נמצאות בזכרון‬ ‫‪-‬‬
‫‪ – pcode‬שמירת פונקציה בלי אפשרות גישה לפונקציה להרצה בלבד‬ ‫‪-‬‬
‫חיפוש אחר שם‪ ,‬משתמש בראשון שמוצא‬ ‫‪-‬‬
‫‪ (1‬משתנה בסביבת בסיס הנוכחית‬
‫‪ (2‬פונקציה בנויה‬
‫‪ (3‬תת פונקציה בקובץ הנוכחי‬
‫‪ (4‬בודק אם פונקציה פרטית בקובץ הנוכחי‬
‫‪ (5‬בודק עם שם של קובץ בספריה הנוכחית‬
‫‪ (6‬בודק אם זה שם של ספריה‬

‫)‪function y=test(a,b‬‬
‫;‪y=a+b‬‬

‫)‪>>test(21,22‬‬
‫= ‪ans‬‬
‫‪43‬‬ ‫מבצע את הפונקציה כמו שצריך‬
>>test 21 22
ans =
100 99 ‫הופך לערכי הסקי מכניס מחרוזת‬

‫קבצים וספריות‬

*.m -
*.mat
‫* – קבצים רק של מטלב‬.fig
matlab.mat ‫ – שמירת כל המשתנים ב‬save -
save filename
save filename var1 var2
save(‘file name’)
save(‘file name’,’var1’,’var2’)
‫ ספרות‬8 ‫ – בפורמט‬save …-ascii
save … -ascii – double
-tabs
-double –tabs
-mat
load ‫ – מוסיף משתנים לקובץ כנ"ל‬-append
‫ – בדיקה של שם קובץ‬exist(‘A’) -
‫ – לא קיים‬0
‫ – משתנה בסביבת בסיס‬1
MAT. ‫ – שם של קובץ‬2
MEX – 3
MOL – 4
‫ – פונקציה בילט אין‬5
p file – 6
‫ – ספריה‬7
java – 8
exist(‘A’,’var’)
exist(‘A’,’builtin’)
exist(‘A’,’dir’)
‫ – חיפוש לפי סוג משתנה כלשהו‬exist(‘A’,’class’)
‫ הסבר בקריאה לקבצים‬help fileformat -
‫ – מציאת שטח של מצולע‬polyarea(vecx,vecy) -
‫דוגמאות‬
‫פונקציה ליצירת מצולע‬

function obj(m,ro)
x=m(1,:);
y=m(2,:);
xmin=min(x)-3;
xmax=max(x)+3;
ymin=min(y)-3;
ymax=max(y)+3;
[xm,ind]=max(x);
up=find(y>=y(ind));
leftx=x(up);
lefty=y(up);
[xls,xli]=sort(leftx);
yls=lefty(xli);
down=find(y<y(ind));
if isempty(down)
xls(length(xls)+1)=xls(1);
yls(length(yls)+1)=yls(1);
xt=xls;
yt=yls;
else
rightx=x(down);
righty=y(down);
[xrs,xri]=sort(rightx,'descend');
yrs=righty(xri);
xt=[xls,xrs];
yt=[yls,yrs];
xt(length(xt)+1)=xt(1);
yt(length(yt)+1)=yt(1);
end
area=polyarea(xt,yt);
mass=area/ro
plot(xt,yt);
title(['the area of the poligon is : ',num2str(area)])
axis([xmin xmax ymin ymax]);
--------------------------------

‫ציור גרף לפי פונקציה שהמשתמש נותן‬

function savefunc(filename,fx,gridyn)
if nargin<2
return
end
x = 100* rand(1,1000);
x=sort(x);
y=eval(fx);
xmin=min(x);
xmax=max(x);
plot(x,y);
title(['the function range is between: ',num2str(xmin),'<=x<=',num2str(xmax)]);
xlabel('x');
ylabel(fx);
grid off
if nargin<3 | strcmp(gridyn,'on')
grid on
else
grid off
end
save(filename)
‫שבוע שביעי‬
‫אינטגרציה נומרית‬

‫‪૚‬‬ ‫‪૚‬‬
‫‪ࢌሺ࢞ሻ ൌ‬‬ ‫‪൅‬‬ ‫‪െ૟‬‬
‫‪૛‬‬
‫‪ሺ࢞ െ ૙Ǥ ૜ሻ ൅ ૙Ǥ ૙૚ ሺ࢞ െ ૙Ǥ ૢሻ૛ ൅ ૙Ǥ ૙૝‬‬
‫)‪function y=humps(x‬‬
‫;‪y=1./((x-.3).^2+.01+1./((x-.9).^2+.04)-6‬‬
‫‪-----------------------------‬‬
‫מרחקים קטנים יותר מדוייק ;‪x=-1:0.2:2‬‬
‫;)‪y=humps(x‬‬

‫‪ – area=trapz(x,y) -‬מחשב אינטגרל בשיטת הטרפזים לא שואף מהר‬


‫‪ – quad(‘humps’,-1,2) -‬שיטת סימפסון לאינטגרלים נותנים גבולות שואף מהר ניתן לקבוע‬
‫גבול שגיאה ברירת בחדל ‪1x10-6‬‬
‫)‪ – quadl(‘humps’,-1,2‬שיטת לובטו‬
‫)‪ – dblquad(‘x*y’,0,2,0,1‬אינטגרל לשני משתנים‬

‫גזירה נומרית‬

‫‪ – pd=polyder(p) -‬גזירת פולינום‬


‫‪ - dy=diff(y)./diff(x); -‬גזירת פולינום‪ ,‬רגיש לרעשים בפונקציות לא חלקות‬

‫פתרון מישדיף‬
‫‪ – [t,y]=ode45(‘func’,tspan,y0) -‬רונגה קוטה סדר בינוני נגזרות לפי ‪ func ,t‬פונקציית עזר‬
‫שמורידה סדר צריך פונקציה מסדר ראשון‪ tspan ,‬טווח פתרון וקטור שורה‪ y0 ,‬וקטור עמודה‬
‫של תנאי התחלה‬
‫קיימים גם ‪ode23 ode113 ode15s ode23s :‬‬

‫דוגמא‬
‫‪2‬‬
‫‪d x‬‬
‫‪+ y =1‬‬
‫‪dt 2‬‬
‫‪d2x d2y‬‬
‫‪+‬‬ ‫‪=0‬‬
‫‪dt 2 dt 2‬‬
‫·‬ ‫·‬
‫‪y(t = 0) = x (t = 0) = y(t = 0) = x (t = 0) = 0‬‬
‫הערות‬
‫‪y(1)=x‬‬
‫’)‪y(2)=x’=y(1‬‬
‫‪y(3)=y‬‬
‫’‪y(4)=y‬‬
‫’)‪x’’=1-y=1-y(3)=y(2‬‬
‫’)‪y’’=-x’’=y-1=y(3)-1=y(4‬‬
‫פונקציית העזר צריכה לתת‬
[x’;x’’;y’;y’’] = [y(1)’;y(2)’;y(3)’;y(4)’]
‫פונקציית עזר‬
function ydot=ex8p3(t,y)
ydot=[y(2);1-y(3);y(4);y(3)-1];
‫תוכנית‬
tspan=[0 5];
y0=[0;0;0;0];
[t,y]=ode45(@ex8p3,tspan,y0);
subplot(2,1,1)
plot(t,y(:,1)); grid on; title('Solution for ''x'''); ...
xlabel('t'); ylabel('x');
subplot(2,1,2)
plot(t,y(:,3)); grid on; title('Solution for ''y'''); ...
xlabel('t'); ylabel('y');
‫ – מוצאת אפסים מקבלת פונקציה וניחוש ראשוני‬fzero -
‫ – מוצאת מינימום של פונקציה בין התחומים למימד אחד‬fminbnd[‘humps’,0.5,0.8] -
–f ‫מקסימום הוא מינימום של‬
‫ מימדים מקבלת ניחוש התחלתי לפי כמות מימדים‬n ‫ – ל‬fminsearch(‘xy’,[1;1])
‫שבוע חמישי‬

‫גרפים דו מימדיים מתקדמים‬

‫לשונית ‪ – insert‬הוספת דברים לגרף‬ ‫‪-‬‬


‫ציור יד – הזזה של הגרף‬ ‫‪-‬‬
‫ציור חץ – אחרי בחירה לחיצה כפולה על מה שרוצים לשנות‬ ‫‪-‬‬
‫כפתור ‪ – data cursor‬נותן נקודות בגרף איפה שלוחצים‬ ‫‪-‬‬
‫‪ – text arrow‬הכנסת טקסט דרך לשונית ‪insert‬‬ ‫‪-‬‬
‫)‪ – area(a‬מלא את הגרף בצבע‪ ,‬אם יש כמה עמודות נבנה אחד על השני‬ ‫‪-‬‬
‫)’‪ – fill(x,y,’c‬מייצר פוליגון לא מסדר איברים וסוגר במקרה שאין רכיב חוזר‬ ‫‪-‬‬
‫)‪ – pie(a,b‬מייצר דיאגרמת עיגול ‪ a‬חלקים ‪ b‬מוציא החוצה חלק לוגי‬ ‫‪-‬‬
‫)}’‪ – pie3(a,b,{‘text‬ציור של עיגול ב ‪ 3‬מימדים‬ ‫‪-‬‬
‫)‪ – bar(x,y‬ציור דיאגרמת בר‬ ‫‪-‬‬
‫)‪ – bar3(x,y‬ציור דיאגרמת בר ב ‪ 3‬מימדים‬
‫)‪ – barh(x,y‬ציור דיאגרמת בר אופקית‬
‫)‪ – barh3(x,y‬ציור דיאגרמת בר אופקית ב ‪ 3‬מימדים‬
‫)‪ – bar(x,[y1’,y2’],’grouped‬ציור בר של ‪ 2‬עמודות צמודות ’‪ ‘stacked‬אחת מעל השניה‬
‫כנל ‪bar3‬‬
‫)‪ – hist(y‬מצייר היסטוגרמה )כמה איברים בכל תחום( ברירת מחדל ‪ 10‬תחומים‬ ‫‪-‬‬
‫)‪ – hist(y,n‬מצייר היסטוגרמה ל ‪ n‬חלוקות‬
‫)‪ – hist(y,x‬ס מציין מרכז של על גבול‬
‫)’‪ – stem(x,y,’linespecs‬מצייר נקודות עם גבעול‬ ‫‪-‬‬
‫)‪ – stairs(x,y,’linespecs‬ציור דיאגרמת מדרגות‬ ‫‪-‬‬
‫)‪ – errorbar(x,y,l,u‬ציור קו שגיאה של סטיית תקן לא סימטרי‬ ‫‪-‬‬
‫)‪ – errorbar(x,y,e‬סימטרי‬
‫גרף עמודות של שגיאה‬
‫)’‪bar(x,y,’r‬‬
‫‪hold on‬‬
‫)’‪errorbar(x,y,e,’.‬‬
‫)’‪ – polar(ang,rad,’linespec‬ציור גרף פולרי‬ ‫‪-‬‬
‫)‪ – compass(z‬מצייר מספרים מרוכבים עם זווית ורדיוס בגרף פולרי‬ ‫‪-‬‬
‫)‪ – feather(z‬כנל בגרף קרטזי‬ ‫‪-‬‬
‫)‪ – rose(v‬היסטוגרמה פולרית ברירת מחדל ‪ 20‬חלוקות‬ ‫‪-‬‬
‫)‪ – scatter(x,y,area,color‬גרף בועות השטח של כל מעגל ביחידות פוינט‬ ‫‪-‬‬
‫)‪color=64*rand(40,1‬‬
‫)‪ – [x,y]=ginput(n‬לקיחת קורדינטות מהגרף ‪ n‬פעמים‬ ‫‪-‬‬
‫‪ – copy figure‬מלשונית ‪ edit‬העתקה של הגרף‬ ‫‪-‬‬
‫‪ – copy option‬נותן אפשרות לשנות הגדרות העתקה‬ ‫‪-‬‬
‫‪ – save as‬נותן לשמור את הגרף‬ ‫‪-‬‬
‫גרפים תלת מימדיים‬

‫‪ – plot3(x,y,z,’color_linestyle_shape’) -‬גרף תלת מימדי‬


‫;)‪t=linspace(0,5*2*pi‬‬
‫)‪plot3(sin(t),cos(t),t‬‬ ‫ציור של קפיץ‬
‫)‪ – [X,Y]=meshgrid(x,y‬הפיכת ‪ x‬ו ‪ y‬בצורת מטריצה‬ ‫‪-‬‬
‫‪ – Z=(X+Y).^2‬פונקציה של משטח‬
‫)‪ – mesh(X,Y,Z‬ציור גרף רשת‬ ‫‪-‬‬
‫)‪ – [x,y,z]=sphere(n‬מייצר ‪ n+1‬איברים למטריצה שתייצר כדור יחידה‬ ‫‪-‬‬
‫‪ – hidden on/off‬שקוף או לא‬ ‫‪-‬‬
‫‪ - hidden‬משנה מצב‬
‫)‪ – meshc(x,y.z‬גרף רשת ומוסיף מפה טופוגרפית‬ ‫‪-‬‬
‫)‪ – meshz(x,y,z‬גרף רשת ומוסיף בסיס‬ ‫‪-‬‬
‫)‪ – waterfall(x,y,z‬מוריד בסיס רק לפאה אחת‬ ‫‪-‬‬
‫)‪ – surf(x,y,z‬ציור משטח‬ ‫‪-‬‬
‫‪ – shaing faceted/flat/interp‬מוסיף הצללה בריבועים של הגרף‬
‫העלמה של חלק מהמשטח ע"י נתינת ‪ nan‬לקורדינתות ב ‪z‬‬ ‫‪-‬‬
‫‪ – surfc‬משטח עם טופוגרפי‬ ‫‪-‬‬
‫‪ – surfnorm‬משטח עם ציור של הנורמל בנקודות‬ ‫‪-‬‬
‫‪ – surfl‬משטח עם הדמייה של מקור אור‬ ‫‪-‬‬
‫‪ – colormap pink‬שינוי צבע של משטח‬ ‫‪-‬‬
‫)‪ – view(az,el‬שינוי נקודת מבט ‪ az‬הזזה ‪ el‬הגבהה ברירת מחדל )‪(-37.5,30‬‬ ‫‪-‬‬
‫)]‪view([az,el‬‬
‫כפתור סיבוב‬
‫)‪ – contour(x,y,z,20‬מחשב קווים שווי גובה כמו מפה טופוגרפית‬ ‫‪-‬‬
‫)‪ -contour3(x,y,z,20‬כנל בתלת מימד‬
‫)‪ – pcolor(x,y,z‬מציין אזורי גובה בצבעים שונים‬ ‫‪-‬‬
‫)‪ – contour(x,y,z,12‬כנל עם הפרדה ברורה‬
‫)‪ c=contour(x,y,z,12‬או ]‪[c,h‬‬ ‫‪-‬‬
‫)‪ – clabel(c‬ציון קווי גובה‬
‫)‪ – ribbon(z‬ציור גרף בסרטים‬ ‫‪-‬‬
‫)‪ – quiver(x,y,dx,dy‬ציור גרף שדה עם וקטור כיוון בגודל וזווית‬ ‫‪-‬‬
‫;)‪ - [dx,dy]=gradient(z,.5,.5‬חישוב גרדיאנט‬
‫)‪quiver3(nx,ny,nz‬‬ ‫‪-‬‬
‫)‪stem3(z,’ro’,’filled‬‬ ‫‪-‬‬
‫דוגמאות‬
‫ציור ספירלה ומגן דוד‬

‫;)‪ang1=linspace(0,8*pi,6*50‬‬
‫;‪r1=0.03975.*ang1‬‬
‫;)‪ang2=linspace(0,2*pi,13‬‬
‫;]‪r2=[1 2 1 2 1 2 1 2 1 2 1 2 1‬‬
‫)‪subplot(1,2,1‬‬
polar(ang1,r1)
subplot(1,2,2)
polar(ang2,r2)
‫שבוע שישי‬

‫ניתוח נתונים‬

‫‪size(temps)=31x3‬‬

‫)‪ – mean(temps‬חישוב ממוצע של כל עמודה לא מתעלם מתא ריק)‪ (nan‬ייתן אפסים‬ ‫‪-‬‬
‫)‪ – plot(temps‬גרף שונה לכל עמודה‬ ‫‪-‬‬
‫)‪ – avg_temps=means(temps,2‬ממוצע לפי המימד השני )השורות(‬ ‫‪-‬‬
‫)‪ – temps-avg_temps(ones(31,1),:‬סטייה ממהמוצע‬ ‫‪-‬‬
‫)‪temps-repmat(avg_temps,31,1‬‬
‫)‪ – max(temps‬מקסימום מתעלם מתא לא מאוכלס)‪(nan‬‬ ‫‪-‬‬
‫)‪ – min(temps‬מינימום‬
‫)‪[max_temp,max_day]=max(temps‬‬
‫)‪ – std(temps‬סטיית תקן‬ ‫‪-‬‬
‫)‪ – median(temps‬חציון‬ ‫‪-‬‬
‫)‪ – cov(temps‬קונבולוציות בין זוג עמודות‬ ‫‪-‬‬
‫)‪ – corrcoef(temps‬מקדם קורלציה בין זוג עמודות‬ ‫‪-‬‬
‫)‪ – diff(temps‬הפרש בין כל שני איברים עוקבים קירוב של ניגזרת‬ ‫‪-‬‬
‫)‪ydot=diff(y)./diff(x‬‬
‫)‪ – diff(temps,n‬חישוב בצורה רקורסיבית ‪ n‬פעמים‬
‫)‪ – diff(temps,n,d‬מבצע ‪ n‬פעמים לאורך מימד ‪d‬‬

‫;)‪>>data=rand(6,1‬‬
‫)‪>>[sdata,sidx]=sort(data‬‬
‫=‪sdata‬‬ ‫=‪sidx‬‬
‫‪0.2311‬‬ ‫‪2‬‬
‫‪0.4860‬‬ ‫‪4‬‬
‫‪0.6068‬‬ ‫‪3‬‬
‫‪0.7621‬‬ ‫‪6‬‬
‫‪0.8913‬‬ ‫‪5‬‬
‫‪0.9501‬‬ ‫‪1‬‬

‫‪ – ridx(sidx)=1:6 -‬מראה מיקום של תאים במערך החדש‬


‫‪ – sidx=sidx(end:-1:1) -‬הפיכת המיון אינדקסים‬
‫)‪ – sdata=sdata(end:-1:1‬נתונים‬

‫)‪>>a=diff(data‬‬
‫=‪a‬‬
‫‪-0.7190‬‬
‫‪0.3757‬‬
‫‪-0.1209‬‬
‫‪0.4053‬‬
‫‪-0.1292‬‬

‫‪ mono=(all(a>0) | all(a<0))) -‬בדיקת מונוטוניות‬


‫‪ – all(diff(diff(data))==0) -‬בדיקה האם המרווחים זהים אם נכון נותן ‪ 1‬אחרת ‪0‬‬
‫‪ – del2(a) -‬לפלאסיאן‬
‫אינטרפולציה‬

‫;)‪>>x1=linspace(0,2*pi,60‬‬
‫;)‪>>x2=linspace(0,2*pi,30‬‬

‫‪– plot(x1,sin(x1),x2,sin(x2),’- -‘) -‬אינטרפולציה לינארית קווית מחבר נקודות בקו ישר‬

‫;]‪Hz=[20:10:100,200:100:1000,1500,2000:1000:10000‬‬
‫… ‪spl=[76 66 59 54 49 46 43 40 38 22 14 9 6 3.5 2.5 1.4‬‬
‫;]‪0.7 0 -1 -3 -8 -7 -2 2 7 9 11 12‬‬

‫‪ – s=interp1(Hz,spl,2.5e3) -‬אינטרפולציה במימד אחד נותן ערך של ‪ spl‬בנקודה שביקשנו‬


‫ברירת מחדל קווית מבצע כל עמודה בנפרד‬
‫)’‪ – interp1(Hz,spl,2.5e3,’linear‬קווי‬
‫)’‪ – interp1(Hz,spl,2.5e3,’cubic‬קובי‬
‫)’‪ – interp1(Hz,spl,2.5e3,’spline‬ספליין קירוב הכי טוב‬
‫)’‪ – interp1(Hz,spl,2.5e3,’nearest‬מחפש את הנקודה הכי קרובה לערך ונותן אותה‬

‫להסתכל מקרוב על נקודות בין תחום ‪Hzi=linspace(2e3,5e3); -‬‬


‫אינטרפולציה ליד המינימום ‪spli=interp1(Hz,spl,Hzi,Hzi,’spline’) -‬‬
‫מציאת הנקודות של התחום הישן ‪ind=find(Hz>=2e3 & Hz<=5e3) -‬‬
‫ציור הגרף הישן והחלק ‪semilogx(Hz(ind),spl(ind),’- -o‘,Hzi,spli) -‬‬
‫נותן את המינימום סביב הנקודה – )‪[spl_min,min_ind]=min(spli‬‬

‫‪ – zi=interp2(x,y,z,2.2,3.3) -‬אינטרפולציה בשני מימדים אותו דבר כמו ‪ interp1‬רק שאין‬


‫ספליין‬

‫;)‪>>xi=linspace(0,4,30‬‬
‫;)‪>>yi=linspace(0,6,40‬‬
‫;)‪>>[xxi,yyi]=meshgrid(xi,yi‬‬
‫אינטרפולציה של כמה נקודות ‪>>zzi=interp(x,y,z,xxi,yyi,’cubic’); -‬‬

‫‪ meshgrid – ndgrid‬לכמה מימדים‬ ‫‪-‬‬


‫)‪ – tri=Delaunay(x,y‬מייצר משולשים מקושרים‬ ‫‪-‬‬
‫‪ – tsearch,dsearch‬נותן משולש שמכיל את הנקודה הרצוייה‬ ‫‪-‬‬
‫)’‪ – griddata(x,y,z,xi,zi,’cubic‬מחזיר אינטרפולציה מנקודות פזורות יוצר פונקציה‬ ‫‪-‬‬

‫פולינומים‬

‫‪x4-12x3+0x2+25x+116‬‬
‫ייצוג פולינום – ]‪>>p=[1 -12 0 25 116‬‬

‫)‪ – r=roots(p‬שורשי הפולינום נותן וקטור עמודה‬ ‫‪-‬‬


‫)‪ – poly(r‬הופך מעמודה לפולינום שורה‬ ‫‪-‬‬
‫)‪ – c=conv(a,b‬קונבולוציה מקבלים פולינום חדש מהכפלת פולינומים‬ ‫‪-‬‬
‫פונקציית חיבור פולינומים ‪,‬פולינומים חייבים להיות באותו אורך‬ ‫‪-‬‬
‫)‪function p =mmpadd(a,b‬‬
‫‪if nargin<2‬‬
‫)’‪error(‘not enough input argument‬‬
‫‪end‬‬
‫;’‪a=a(:).‬‬
‫;’‪b=b(:).‬‬
‫;)‪na=length(a‬‬
‫;)‪nb=length(b‬‬
‫ריפוד באפסים – ]‪p=[zeroes(1,nb-na) a] + [zeroes(1,na-nb) b‬‬

‫‪ – [q,r]=deconv(a,b) -‬חלוקת פולינומים ‪ q‬תוצאה ‪ r‬שארית‬


‫‪ – polyder(d) -‬גזירת פולינום‬
‫‪ – polyint(p,10) -‬אינטגרל אם אין מקדם )‪ (10‬אז ברירת מחדל ‪0‬‬

‫;)‪x=linspace(-3,3,40‬‬
‫;]‪p=[1 3 5 5 3 1‬‬

‫‪ – polyval(p,x) -‬מחשב ערכי נקודות בפולינום‬

‫מונה – ]‪n=[1 -10 100‬‬


‫מכנה – ]‪d=[1 10 1000‬‬

‫‪ – [nn,dd]=polyder(n,d) -‬נותן וקטור תוצאה מונה ומכנה של גזירה‬


‫‪ – [r,p,k]=residue(n,d) -‬פירוק לשברים חלקיים‪ r ,‬מקדמי מונה‪ p ,‬מקדמי מכנה‪ k ,‬מקדמי‬
‫שארית‬
‫)‪ – [nn,dd]=residue(r,p,k‬נותן מונה ומכנה של פונקציה רציונלית‬
‫‪ – p=polyfit(x,y,n) -‬הופך לפולינום ‪ n‬קובע את סדר הפולינום‬
‫שבוע שביעי‬
‫אינטגרציה נומרית‬

‫‪૚‬‬ ‫‪૚‬‬
‫‪ࢌሺ࢞ሻ ൌ‬‬ ‫‪൅‬‬ ‫‪െ૟‬‬
‫‪૛‬‬
‫‪ሺ࢞ െ ૙Ǥ ૜ሻ ൅ ૙Ǥ ૙૚ ሺ࢞ െ ૙Ǥ ૢሻ૛ ൅ ૙Ǥ ૙૝‬‬
‫)‪function y=humps(x‬‬
‫;‪y=1./((x-.3).^2+.01+1./((x-.9).^2+.04)-6‬‬
‫‪-----------------------------‬‬
‫מרחקים קטנים יותר מדוייק ;‪x=-1:0.2:2‬‬
‫;)‪y=humps(x‬‬

‫‪ – area=trapz(x,y) -‬מחשב אינטגרל בשיטת הטרפזים לא שואף מהר‬


‫‪ – quad(‘humps’,-1,2) -‬שיטת סימפסון לאינטגרלים נותנים גבולות שואף מהר ניתן לקבוע‬
‫גבול שגיאה ברירת בחדל ‪1x10-6‬‬
‫)‪ – quadl(‘humps’,-1,2‬שיטת לובטו‬
‫)‪ – dblquad(‘x*y’,0,2,0,1‬אינטגרל לשני משתנים‬

‫גזירה נומרית‬

‫‪ – pd=polyder(p) -‬גזירת פולינום‬


‫‪ - dy=diff(y)./diff(x); -‬גזירת פולינום‪ ,‬רגיש לרעשים בפונקציות לא חלקות‬

‫פתרון מישדיף‬
‫‪ – [t,y]=ode45(‘func’,tspan,y0) -‬רונגה קוטה סדר בינוני נגזרות לפי ‪ func ,t‬פונקציית עזר‬
‫שמורידה סדר צריך פונקציה מסדר ראשון‪ tspan ,‬טווח פתרון וקטור שורה‪ y0 ,‬וקטור עמודה‬
‫של תנאי התחלה‬
‫קיימים גם ‪ode23 ode113 ode15s ode23s :‬‬

‫דוגמא‬
‫‪2‬‬
‫‪d x‬‬
‫‪+ y =1‬‬
‫‪dt 2‬‬
‫‪d2x d2y‬‬
‫‪+‬‬ ‫‪=0‬‬
‫‪dt 2 dt 2‬‬
‫·‬ ‫·‬
‫‪y(t = 0) = x (t = 0) = y(t = 0) = x (t = 0) = 0‬‬
‫הערות‬
‫‪y(1)=x‬‬
‫’)‪y(2)=x’=y(1‬‬
‫‪y(3)=y‬‬
‫’‪y(4)=y‬‬
‫’)‪x’’=1-y=1-y(3)=y(2‬‬
‫’)‪y’’=-x’’=y-1=y(3)-1=y(4‬‬
‫פונקציית העזר צריכה לתת‬
[x’;x’’;y’;y’’] = [y(1)’;y(2)’;y(3)’;y(4)’]
‫פונקציית עזר‬
function ydot=ex8p3(t,y)
ydot=[y(2);1-y(3);y(4);y(3)-1];
‫תוכנית‬
tspan=[0 5];
y0=[0;0;0;0];
[t,y]=ode45(@ex8p3,tspan,y0);
subplot(2,1,1)
plot(t,y(:,1)); grid on; title('Solution for ''x'''); ...
xlabel('t'); ylabel('x');
subplot(2,1,2)
plot(t,y(:,3)); grid on; title('Solution for ''y'''); ...
xlabel('t'); ylabel('y');
‫ – מוצאת אפסים מקבלת פונקציה וניחוש ראשוני‬fzero -
‫ – מוצאת מינימום של פונקציה בין התחומים למימד אחד‬fminbnd[‘humps’,0.5,0.8] -
–f ‫מקסימום הוא מינימום של‬
‫ מימדים מקבלת ניחוש התחלתי לפי כמות מימדים‬n ‫ – ל‬fminsearch(‘xy’,[1;1])


 


 
 


   


 



 
!  


 "#

("& ')")"* % +


* $,*"-&$%)%*#(-** $%& $'  
"*#%#/+ 
 # *0$ -(#  $,$ -(#'&(%#$%##.&)$"*

#*%)"*
-(.$# -* )%(&
+*
*#)
)"*--(.&  
1
#&
*$"&'5,,3,4,12)"*#$'*#-(.

5,,,3,4,1,6,7,8,9,2 .( )
 $*-(.

.( )
%+"$*-(#$&.&
#(*

 ')-($#' )
&"&"(
:+#$"&'#  

:+#$  $''&#.(& 
*
%+) $#'$)'"& -&$# 
  
 $.*'&

  
     


"&"#$.&

;<=>?
@AB,
*+$.@AB%($##&
#"*;<=>?.   #%($##&
$##
#


 C"&


  

?@#%(#+.*$
# #


  

:+#$"% -*#0$#
#C" #%(&(" '&$)$)$,#
C" #%(&("  *&$ -*
#&
*#
#*%
DD1E3
 %"%#$ ',#%(&("&*


DD1E3


8
"%#
:+# ',#%(&("&*
$ $'


 

    


 

 

 
 


 !"#$%&! # 

'

'()*
!*# $%&! $!* ## 
''+!*#$!* #
,

 -

/2 3  4 5  ./01 


 36 .-

 26./ 
2    
-6
  /3
1 


50!  --605 /436#          31 
 
' 0-
.6
   
 7  6
  0268* 
657 
. 2

'()!9**:*++#;


<

* =
-0     /2    


7  6

9>?@
>*@
'26 6 = A-1 

2 /3

'B%),!#
C(9! 5'#
A-/
 /35D.-.2 36  
   
  
' = ./  4

E FD+, ! #

 

     


G 3
.  6
 2

=.   10.   32
260. 2

2

2.6
E .

/.5E  0.  6
32
  6 E  05
0
 32
0


E1.6A-5H0050 0
05  03
.6  0/=5.6 0. 266- 0 


 

 
 


    





 

 








%#$   


  !"
  
 
% !"
'() 
& 
%$ 
    & 

%+
#$#*

  

,"
-   

    
 
   

%!
" !./0)
 

3! 
 4 %2  + *  1 

% 4 
 $ 
* 
 
-
%*  
4 !  
 
./0)+25(5
-(67*!*  
./0)+25205
%* " 26//08*  


%90/7  
*!," ",," *


90/70## *  ,"4* 


90/70:
%," * *  * 


  
   
 

 
 

  

 

 
  
 


 

#$ !"


  

     
*+, 
 
% ! 
&"

 
 '   ( 
)
'("

" 


! 
 
*+#,
"!



 
&"

/0 # "  



-!  

 (.

*/, 

"

 
 
&"

20 

 (  (
"
 &"

1 
-(!
*'(",


 !  
&"
20
 
130+40 #
2+

567
44
*20,4  

'

!
!( 
  


 '(  #' 
'
  

#
  
 
  
  
  

 
    


     
'(! (


 

-(! "
  ' 
"( 
 

# 
! 

 
(
9:;7*86, 8)6 '(


;67*86, 8)6 "




<*86,
 .'"!


:56=*86, 
 


#


'
 
(
5*,  
(
5*#++, 
!    

(
5*##+, 

!!  
(

#
#
  
  '



#
'('   
1>  (





 


#


+!? 
 !
5    
0   (+
 (' 04 .  
 '"0
A  '%
"
 !
&@'    3
 
 
A5 (
A % 0/3
 
  $
% !
 (?

 
"

 
@'
A
! (
 %
"

('

B

  
 

 


 
 
  

 
 



!"

#$####%

&'(#)*+(%+%

!"

#,#,$#,#,%

 -#*$.+%

!"

.#//

01!2$)+%

!"

####)####%




       

 








   



 ! 


!

"!
#
$!  %

"!
$!  %$
"%!
 
 4 5
3 34 
 4 3   / 3
  6    *3
44 4>
?4 0!334 =<11( ;! 9:783


65  94   0!
A 4=6   4=0! 6  33:@3
    33 4    4= 
###  4 
 4:#3
 
635  4:)3
 .7# 9  3=  !   !)
3 3  =634?  4:).3
3 4   3   93& <%B C  4
 3



 !  
A?
 
5=(<1 44
  
   4 43 4&B 4
9

9A?
4
" 4  4 3 3 4

  
 

 
   

 
 
 
 

  
 
    


  

 

    


   

 
  
 
   

 !"#  
  


  
  

 

 

   
 



  

  

$  

 
!"#%&'"('!%)!%*+,&+-./012 !"#3"+/4&")!%*+,&+-./05


/"6

$
 
 

 

    


    
 

 
 
 

 
 


>=
 < 
    78'90:&;;/, 


 

  #     <  ,/%!,"
      

-&;;/,
 
 


78'90:&;;/, 

  
   



$
$
 
     ! "# $
   $
      $




 
<   

B 
<
 
 &"6?%6@"6A+,


$
 ! "# $



%&'%%

%&(%)
      


 
  

  =
 
 
   

 >
 
 C
4

 E
E  
 5   D


  
 
 * 
 +
 (
,  *- 


,  *$
, $-+.(

 *&/
  

 
 <  *
 






 * 

 *



4F
 5


     




$

  #
012 #   /#&
 /34#
 
  

&&&



  G


 HH 
 


    


   



    . 




 
<   .    
."+$01 –!"#&*'/!"#,-.*!"#$#%&'!()*+
U

(2008-2009) 0" )2'!',2


#&3!$0$!'*4##2!'*#*456!$4

8*19 ')1 +&71

2 !+$1!:*&'/ + 1!$#%&'
10 !+$1!:*&'/ + 2!$#%&'
25 !+$1!:*&'/ + 3!$#%&'
36 !+$1!:*&'/ + 4!$#%&'
43 !+$1!:*&'/ + 5!$#%&'
52 !+$1!:*&'/ + 6!$#%&'
60 !+$1!:*&'/ + 7!$#%&'
69 !+$1!:*&'/ + 8!$#%&'
76 !+$1!:*&'/ + 2005!;#7!:-*.
82 !+$1!:*&'/ + 2006!(&*-!:-*.
87 !+$1!:*&'/ + 2006!.#.+!:-*.
92 !+$1!:*&'/ + 2006!;#7!:-*.
!3!01%,!"$)!.$20"!/
!"#$%&'(!)*+,!"#'-#".!('&% U

!
!>="+7!(&#;1!"0#*)!<$8%!.-:!Matlab!5)!6$7)(!6$)#*$+(!"7!89) 43

10 ( 2
1.
4.2 ) 10 05
4
/ 42 6 ,5
2. -- 0 *
.6(3
2
475 *+
! "
3. cot 2 30 ! ( cos 4 67.5 ! ! "
! "
! 4. ln ' 2 0 arccos h/- 5 ,*
.' +
! "
5. sin !3' " ( log 2 ' 2 ( log10 !' ( e "
6. e 5' (sinh #ln !5 "$
7. !1 ( 5i " ( #!8 ( 7i " ) !7 ( 8i "$e
tanh 3 %exp#ln !3' " ( log10 !' "i $&
8.
78
!
!6$$0,#'(!6$'#"'( !0#)8 !a,b,c,d,… !6$'"*,)!*,"*(!681(!B7 !A3 !01%,!(.7*!.8!0#@+ !4?
!4(79#"(!0#)8!y!('"*,)#
!
!.*!E.+#,(!B08(# !="#.8,): !"$##@(!"7!)*+!"#,$7", !Matlab !"#&#;1)!*#,$*!$D8 !4C
!>6$7)(!6$)-#0,(!6$01%,(
!
1. 8 ( 12 j
! 2. ln !7 ( 6 j "
#
3. 1 ( sin e 2 j! "$ 5

!
!"#.);",!67( !format long ! 5)!*,"*(!681(!B7 !C !5# !? !A3 !6$1$8%!.*!)#*$+(!.8!0#@+
!F!"#'#*!"#79#"
!
!.* !a,b 6$,&;, !? !=input !(&#;1)!*#,$*!$D8: !*,"*,(,!".);,!0*7!"$'-#"!)#"- !4G
!0+)$!*,"*,(*!+$'(.!H"$' !A E.;!";$&))!B0#9!H$7: ! ax 2 0 bx 0 1 1 0 !> "$8#)$0(!(7##*,(
!(7##*,(!$*0#* !"7!")*+, !"$'-#"( !A= (7##*,(!0#)8!"#'#0"1 !? 5.!#.$)#$*!6$,&;,)!&$,"

b 2 b 2 ( 4a
!"#$0,#'(!" #79#"(!"7!;0#!B7!B%,(!.8!%$1&(.!(-$09!"$'-#"( !4 x1, 2 1 !>$1.
2a
4=disp!(&#;1)!*#,$*!$D8:!"#,$7",!"#0"#-)!"##.,
1!"#$%

:&'()"
./'+'%./)01%(2)*$%*)#'*%'",%$"!"#$%&''"#(#%&'$)*'+%,)-'$

.$
:"*'+,
(sqrt(10+2))/(4.2*10^-5)
:-#,
ans =

8.2479e+004

..
:"*'+,
((4^2)/(6+3^2)-6/475)^(4/5)
:-#,
ans =

1.0430

./
:"*'+,
(cot(30*pi/180))^2+(cos(67.5*pi/180))^4
:-#,
ans =

3.0214

.*
:"*'+,
log(pi^2)-sqrt(acosh(5/pi))
:-#,
ans =

1.2696
."
:"*'+,
abs(sin(3*pi)+log2(pi^2))+log10(pi+exp(1))
:-#,
ans =

4.0709
.'
:"*'+,
exp(5*pi+sinh(log(5)))
:-#,
ans =

7.3146e+007

.0
:"*'+,
(1+5i)+((8+7i)*(7+8i))^exp(1)
:-#,
ans =

-1.6312e+005 -3.4423e+005i

.1
:"*'+,
(tanh(exp(log(3*pi)+log10(pi)*i))^3)/(7^8)
:-#,
ans =

1.7347e-007 +2.7977e-014i

2!"#$%

:&'()"
(&'2"#21%&,%02)%./'+'%./)01%(2)*$%*)#'*%'",%$"!"#$%&''"#(#%&'$)*'+%,)-'$

.$
:"*'+,
a=10; b=2; c=4.2; d=10; e=-5; y=(sqrt(a+b))/(c*d^e)
:-#,
y=

8.2479e+004

..
:"*'+,
a=4; b=2; c=3; d=6; e=5; f=475; y=((a^b)/(d+c^b)-d/f)^(a/e)
:-#,
y=

1.0430
./
:"*'+,
a=2; b=4; c=30; d=67.5; e=180; y=(cot(c*pi/180))^a+(cos(d*pi/e))^b
:-#,
y=

3.0214

.*
:"*'+,
a=2; b=5; y=log(pi^a)-sqrt(acosh(b/pi))
:-#,
y=

1.2696

."
:"*'+,
a=3; b=2; c=1; y=abs(sin(a*pi)+log2(pi^b))+log10(pi+exp(c))
:-#,
y=

4.0709

.'
:"*'+,
a=5; y=exp(a*pi+sinh(log(a)))
:-#,
y=

7.3146e+007

.0
:"*'+,
a=5;b=8;c=7;d=1; y=(d+a*i)+((b+c*i)*(c+b*i))^exp(d)
:-#,
y=

-1.6312e+005 -3.4423e+005i
.1
:"*'+,
a=3; b=7; c=8; y=(tanh(exp(log(a*pi)+log10(pi)*i)))^a/(b^c)
:-#,
y=

1.7347e-007 +2.7977e-014i

'3%0!+ – 3!"#$%

:&'()"
2-0%"#2)1$%&'$4)2#%&'215#%!*%()'))6)%"!+)#%72,%$)*'+

.$
:&'*'+,
angle(8+12i)*180/pi
abs(8+12i)
:-#,
%('))6
ans =

56.3099
"!+)#%72,
ans =

14.4222

..
:&'*'+,
angle(log(7+6i))*180/pi
abs(log(7+6i))
:-#,
('))6
ans =

17.6932
"!+)#%72,
ans =

2.3316
./
:&'*'+,
angle((1+sin(exp(2))*i)^5)*180/pi
abs((1+sin(exp(2))*i)^5)

:-#,
('))6
ans =

-151.0395
"!+)#%72,
ans =

4.3407

%.# ,./)08.%2+3!%()215 15%/,%'8)2*,%2/5%&'!$0#%)8''. format long-%$%&'$*+#%)8''.%&3*


.2()'%..)$9%0)'/%(#2%+'"$#*

'$%0!+ – 3!"#$%

:&'()"
7)23%"#2)1$%&'$4)2#%&'215#%!*%()'))6)%"!+)#%72,%$)*'+

.$
:&'*'+,
angle(8+12i)*180/pi
abs(8+12i)
:-#,
('))6
ans =

56.309932474020215
"!+)#%72,
ans =

14.422205101855956
..
:&'*'+,
angle(log(7+6i))*180/pi
abs(log(7+6i))

:-#,
('))6
ans =

17.693230615793556
"!+)#%72,
ans =

2.331617194191256

./
:&'*'+,
angle((1+sin(exp(2))*i)^5)*180/pi
abs((1+sin(exp(2))*i)^5)
:-#,
('))6
ans =

-1.510395256239016e+002
"!+)#%72,
ans =

4.340740609504277
4!"#$%

:&'()"
2
ax " bx " 1 ! 0%.3))*#.%!*%&'*2)*.%(3%(3-)#)%*#(*#.#%&'8(*# 2%(!$0#%2*3%('84)(

:"*'+,
a=input('please put number into "a": ');
b=input('please put number into "b": ');
x1=(b+sqrt(b^2+4*a))/(2*a);
x2=(b-sqrt(b^2+4*a))/(2*a);
disp('The roots are: ');
disp(x1);
disp(x2);
:-#,
please put number into "a": 3
please put number into "b": 2
The roots are:
1

-0.3333
!3!01%,!"$)!.$20"!/!"#$%&'(!)*+,!"#'-#".!('&% U

!
!54
!65ones!(&#A1)!*#,$*!$@?>!!=69!;(!($0)$<!.-*!:!X!9!.&#2)!(7$08,!(') 6a
!65zeros!(&#A1)!*#,$*!$@?>!B!X!B !.&#2)!;$%1<!"7$08,!(') 6b
!$@?> !E !;(!(.*!D#%-.<(!$0)$<!.-* !C !X !C !.&#2)!"$'#%-.<!(7$08,!(') 6c
65eye!(&#A1)!*#,$*
&A A#
!(&$+$!"7$08,!<$( !A !0*<- !! $ ! !F (<)(!(0#7(, !C ! (7$08,!(') 6d
%B A "
!6 .&#2!#"#<)!;$%1<!"7$08,!BG#!3!X!3!.&#2)
!4H !D$) !;$,.* !;$$<0A<!;$01%,!;( !($0)< !.-* !9!X !9 !.&#2) !(7$08,!(') 6e
!;$,.* !;$$<0A<!;$01%,!;( !0*< !$'*,( !D#%-.<)!;$0)$<(!&)., !I3H !G.
!6B!%#'$,.!%1<!D$)
!
!F<)(!(0#*(!0#8A#!"<!Matlab! G)!"#').!"#0*1<,(!;$-0&!?)0< !"#+1.!<7,!!53
! '1 2 3 4 5 4 3 2 1(
!
!FD#"'!5E
& 1 2# & 5 6# &11#
!A ) $ ! B) $7 8! C ) '9 10( D ) $12!
% 3 4" % " % "
!
!F5Matlab! G)!*#,$*!$@?>!)*+
!
a) A , B , 5
b) A T , B . A , B T
! c) A , *C T - D+
d) 5
C, D
& B A #
'
e) F ) $ D C T
$ ( & C #!
$D T ! !
% % ""
!
!
!
!? 7).!*$!6HG4HH! ;#+")!;$,.*!;$$<0A<!;$01%,!.*!B!X!B!.&#2)!M! J0?,!(')!5C
!FMatlab!"#&#A1)!*#,$*!$@?!D#*<0(!M!J0?,!.?!"#<)(!"#.#?1(!.-!"<
!6 (.#?!0&%!$1.!*&+,!J0?,(!"#&#,?!"<!0&% 6a
!0 &%!$1.!"#&#,?(!0<*!.-!"<#!(.#?!0 &%!$1. !($'*( !(&#,?(!"<!0&% 6b
!6($'*(!(&#,?)!*&+(!;$0)<(
!6&0#$!0&%!$1.!*&+,!J0?,(!"#0#*!"<!0&% 6c
!;$'8A(!;$01%,(!('#*<0(!(&#,?)> !(. #?!0&%)!J0? ,(!$0)<!.-!"<!0&% 6d
65(<.(!J-#!;$<)(!;$01%,(!($$'*(!(&#,?)!I0"#$)
6 (0#*!.-)!0"#$)!.#&2(!01%,(!"<!<7, 6e
6 J0?,(!.-)!0"#$)!.#&2(!01%,(!"<!<7, 6f
6464!"AK+)!J0?,(!$0)<!.-!"<!(.?( 6g
!J0?,)!#,#A$,!"<!;$0<",(!;$%A&'$<(!;#-%!"<!J0?,)!0)$<!.-.!L%#( 6h
65 ;$%A&'$<!$'*)!;$*,"*,!0*<->
!0*<-> !J0?,)!#,#A$,!"<!0<",(!%A&'$<(!"<!J0?,)!0)$<!.-.!L%#( 6i
65 &$+$!;$%A&'$<)!;$*,"*,
!;#A,)# !IBH G.!;$##*!#<!;$'8A(!;$0)$<!A0!;$<7,'!#)* !*&+ !J0?,!(') 6j
6BB!01%,(!?$1#,!;$0+<(!;$0)$<(
!M ! J0?,(!.* !E G)!;$A.+",* !;$0)$<(!A0!;$<7,'!#)*!*&+!J0?,!(') 6k
!63H!01%,(!?$1#,!;$0+<(!;$0)$<(!;#A,)#
!
!F(<)(!"#<##*,(!"-0?,!"<!0#"1!5B
!
x - y - z - w ) 12
x - 2y - 3z - 4w ) 15
!
x - 5z - 6w ) 0
y - w ) 18
!
!",*#0#!5input!(&#A1)!*#,$*!$@?>!01%, !*,"*,(,!"8.#A!0*<!"$'-#"!)#"-!59
!F(<)(!(0#*(!"<!5 &).)!"+<!(0#*)>!J%,(!.?
The square root of your number is X
!6 *,"*,(!;*0*!01%,(!.*!$?#)$0(!#*0#*!?$1#,!X!;#A,)!0*<-
!
!F;$<)(!;$0)$<(!"<!..#-(!3!X!3 !.&#2)!;$<"!J0?,!(')!5=
!6"$'-#"(!)"#-!;*!F54I4>!0)$<
!!!01%,,!(01%!.$-,!#-#")!<"!.-* !M !J0#<)!(0#*!0#8A# !F54I3> !0)$<
!6"$'-#"(!)"#-!.*!"#(K(
!6E!X!E!.&#2)!(&$+$!"7$08,!F53I4>!0)$<
!63+2i!!F)-#0,(!01%,(!F53I3>!0)$<
!
!6 J%,(!.?!J0?,(!$0)$<!.-!"<!27( 6a
!6 J0?,(!$0)$<!.-!"<!27(!)#*#!53I3>!G#!54I4>!;$0)$<(!D$)!L.+( 6b
1!"#$%!&'()
U

a)
:*+",&
7.6!/%+"!$0+)!"$%)$1!02.!,-3-'+ 6!*+!,-%-. 8!"#$%&'!"()
:&-+.#
a=7.6*ones(8,6)
:/'#
a=

7.6000 7.6000 7.6000 7.6000 7.6000 7.6000


7.6000 7.6000 7.6000 7.6000 7.6000 7.6000
7.6000 7.6000 7.6000 7.6000 7.6000 7.6000
7.6000 7.6000 7.6000 7.6000 7.6000 7.6000
7.6000 7.6000 7.6000 7.6000 7.6000 7.6000
7.6000 7.6000 7.6000 7.6000 7.6000 7.6000
7.6000 7.6000 7.6000 7.6000 7.6000 7.6000
7.6000 7.6000 7.6000 7.6000 7.6000 7.6000
b)
:*+",&
5!0+ 5!03-4)!*$561!,#$%&'!"()
:&-+.#
a=zeros(5)
:/'#
a=

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
c)
:*+",&
3!*"!"0.!7-5201"!$%)$1!02. ,4!0+ 4!"#$%&'!"()
:&-+.#
a=eye(4)*3
:/'#
a=

3 0 0 0
0 3 0 0
0 0 3 0
0 0 0 3
d)
:*+",&
&A A#
"3$8$!,#$%&'!1$" A!%.12 $ ! !"%-#"'!"#$%&'!"()
%B A "
!03-4 "!-,-1)!*$561!,#$%&'!1$" B
:&-+.#
>> A=eye(2);
>> B=zeros(2);
>> c=[A A; B A]
:/'#
c=

1 0 1 0
0 1 0 1
0 0 1 0
0 0 0 1
e)
:*+",&
,$(.'"!7-5201)!*$%65'"!3)0' 20-0 10!7$)!*$'0.-!*$$1%91!"$%)$1!02. 6!0+ 6!"#$%&'!"()
.(-5)-0 0!7$)!*$$1%91!*"!%.1
:&-+.#
a=10; b=20; c=0; d=-5;
A=round(a+(b-a)*rand(6));
B=round(c+(d-c)*rand(1,6));
A(1:7:end)=B(1:1:end);
A=fliplr(A)
:/'#
A=

14 13 11 12 17 -3
18 15 19 17 -3 18
15 15 18 -3 15 10
14 18 -1 14 15 10
19 -2 14 16 13 12
-2 16 14 18 17 16
2!"#$%!&'()
U

:*+",&
'1 2 3 4 5 4 3 2 1(!%-&9--"!,1!,-(-.!*$2%3 4-)!"()
&0+)("!1"-
:&-+.#
a=[1:5, 4:-1:1]
:/'#
a=

1 2 3 4 5 4 3 2 1

&220)!1"-
:&-+.#
[1 2 3 4 5 4 3 2 1]
:/'#
ans =

1 2 3 4 5 4 3 2 1

*2)2')!1"-
:&-+.#
>> a=[1:4];
>> b=[a,5,fliplr(a)]
:/'#
b=

1 2 3 4 5 4 3 2 1

*2,23"!1"-
:&-+.#
a=[linspace(1,4,4) , linspace(5,1,5)]
:/'#
a=

1 2 3 4 5 4 3 2 1
3!"#$%!&'()
U

:*+",&
& 1 2# & 5 6# &11#
:*$1)"!,1!)"0&')!*,%:+)!).80!/$%# A ) $ ! B) $7 8! C ) '9 10( D ) $ ! !7-,(
% 3 4" % " %12"

a)
:*+",&
A * B * 5 !).8
:&-+.#
A*B*5
:/'#
ans =

95 110
215 250

b)
:*+",&
A * B + A * BT !).8
T

:&-+.#
A'*B-A*B'
:/'#
ans =

9 7
-1 -9
c)
:*+",&
A * , C . D - !).8
T

:&-+.#
A*(C'+D)
:/'#
ans =

64
148
d)
:*+",&
5
C * D !).8
:&-+.#
(C*D)^(1/5)
:/'#
ans =

2.9383

e)
:*+",&
& B A #
$ !
F )$ & C # !).8
& D CT # $ T ! !
$% % " D !
% ""
:&-+.#
F=[B A; [D C'] [C;D']]
:/'#
F=

5 6 1 2
7 8 3 4
11 9 9 10
12 10 11 12

4!"#$%!&'()
U

:*+",&
0-100!7$)!*$'0.!*$$1%91!*$%65'!0. 5!0+ 5!03-4) M!/%+'!"()
:&-+.#
M=round(100*rand(5))
:/'#
M=

76 71 82 44 49
74 3 69 38 45
39 28 32 77 65
66 5 95 80 71
17 10 3 19 75
a)
:*+",&
"0-+!%35!$60!/%+'"!,-3-'+!,1!%35
:&-+.#
sort(M)
:/'#
ans =

17 3 3 19 45
39 5 32 38 49
66 10 69 44 65
74 28 82 77 71
76 71 95 80 75

b)
:*+",&
!"3-'+)!.38"!*$%)1"!%35!$60!,-3-'+"!%1.!02!,1-!"0-+!%35!$60!"$$(."!"3-'+"!,1!%35
"$$(."
:&-+.#
>> [sm,ism]=sort(M(:,2));
>> a=M(ism, 1:5)
:/'#
a=

74 3 69 38 45
66 5 95 80 71
17 10 3 19 75
39 28 32 77 65
76 71 82 44 49
c)
:*+",&
3%-$!%35!$60!.38'!/%+'"!,-%-.!,1!%35
:&-+.#
>> a=rot90(M);
>> b=sort(a);
>> c=rot90(b,3)
:/'#
c=

82 76 71 49 44
74 69 45 38 3
77 65 39 32 28
95 80 71 66 5
75 19 17 10 3
d)
:*+",&
(/0$1-!"(-.1%!"3-'+') "0-+!%35)!/%+'"!$%)1!02!,1!%35
:&-+.#
a=reshape(sort(M(:)),5,5)
:/'#
a=

3 19 44 69 76
3 28 45 71 77
5 32 49 71 80
10 38 65 74 82
17 39 66 75 95

e)
:*+",&
"%-.!02)!%,-$)!0-34"!%65'"!,1!1#'
:&-+.#
>> a=rot90(M);
>> b=rot90(max(a),3)
:/'#
b=

82
74
77
95
75
f)
:*+",&
/%+'"!02)!%,-$)!0-34"!%65'"!,1!1#'
:&-+.#
max(max(M))
:/'#
ans =

95
g)
:*+",&
1.1!,9:8)!/%+'"!$%)1!02!,1!"0+"
:&-+.#
a=M.^(1.1)
:/'#
a=

117.1912 108.7387 127.4076 64.2389 72.3129


113.8033 3.3484 105.3741 54.6716 65.8467
56.2563 39.0727 45.2548 118.8885 98.6744
100.3456 5.8731 149.7945 123.9935 108.7387
22.5680 12.5893 3.3484 25.5052 115.4961

h)
:*+",&
/%+')!-'-9$'!,1!*$%1,'"!*$593($1"!*-25!,1!/%+')!%)$1!020!;5-"
:&-+.#
>> I=reshape(ceil(find(M)./5),5,5);
>> J=rot90(I,3);
>> a=M+I+J
:/'#
a=

78 74 86 49 55
77 7 74 44 52
43 33 38 84 73
71 11 102 88 80
23 17 11 28 85

i)
:*+",&
/%+')!-'-9$'!,1!%1,'" 593($1"!,1!/%+')!%)$1!020!;5-"
:&-+.#
>> inde=reshape(find(M),5,5);
>> a=inde+M
:/'#
a=

77 77 93 60 70
76 10 81 55 67
42 36 45 95 88
70 14 109 99 95
22 20 18 39 100
j)
:*+",&
!+$6-'!*$%81"!*$%)$1"!*-9')- ,50-0!*$--.!-1!*$(&9"!*$%)$1!9%!*$1#'(!-).!.38!/%+'!"()
55!%65'"
:&-+.#
>> a=M;
>> ind=find(M>50);
>> a(ind)=55
:/'#
a=

55 55 55 44 49
55 3 55 38 45
39 28 32 55 55
55 5 55 55 55
17 10 3 19 55

k)
:*+",&
20!%65'"!+$6-'!%1.)- 3-)!*$908,'"!*$%)$1"!9%!*$%1.(!-) ,.38!/%+'!"()
:&-+.#
>> a=M;
>> b=mod(a,3);
>> c=find(b>0);
>> a(c)=20
:/'#
a=

20 20 20 20 20
20 3 69 20 45
39 20 20 20 20
66 20 20 20 20
20 20 3 20 75
5!"#$%!&'()
U

:*+",&
:"1)"!,-1--.'"!,2%+'!,1!%-,6
x . y . z . w ) 12
x . 2y . 3z . 4w ) 15
x . 5z . 6w ) 0
y . w ) 18

:&-+.#
>> a=[1 1 1 1; 1 2 3 4; 1 0 5 6; 0 1 0 1];
>> b=[12;15;0;18];
>> x=inv(a)*b
:/'#
x=

19.5000
-0.0000
-25.5000
18.0000

6!"#$%!&'()
U

:*+",&
:"%-."!,65-,)!-0.!$+-)$%"!.%-."!,1!"%$:8'-!"(,.'"'!%65'!,&0-9"!,$(2-,!)-,2
The square root of your number is X
:&-+.#
x=input('Please input any number: ');
disp(['The square root of your number is ',num2str(sqrt(x))])
:/'#
Please input any number: 100
The square root of your number is 10
7!"#$%!&'()
U

:*+",&
:*$1)"!*$%)$1"!,1!00-2!%.1 2!0+ 2!03-4)!*$1,!/%+'!"()
,$(2-,"!),-2!*. :7-.1%!%)$1
,$(2-,"!),-2!0.!:",'!"%65!02!0$2'!1,!02!%.12 ,*$1, 9!/%-1)!"%-.!%-&9- :$(.!%)$1
3!0+ 3!03-4)!"3$8$!,#$%&' :$.$0.!%)$1
3 . 2i !)2-%'"!%65'" :$+$)%!%)$1
:1",%&!*04&' &-+.#
>> a(1,1)={'Tal Tsory'};
>> a(1,2)={[0 4 6 2 2 9 6 2 1]};
>> a(2,1)={eye(3)};
>> a(2,2)={2+3i};
>> a
:/'#!*.2-3
a=

'Tal Tsory' [1x9 double]


[3x3 double] [2.0000 + 3.0000i]

a)
:*+",&
/%+'"!$%)$1!02!,1!4#"
:&-+.#
celldisp(a)
:/'#
a{1,1} =

Tal Tsory

a{2,1} =

1 0 0
0 1 0
0 0 1

a{1,2} =

0 4 6 2 2 9 6 2 1
a{2,2} =

2.0000 + 3.0000i

b)
:*+",&
/%+'"!02!,1!)-.!4#"-!$+$)%"-!7-.1%"!*$%)$1"!7$)!;08"
:&-+.#
>> b=a;
>> b{1,1}=a{2,2};
>> b{2,2}=a{1,1};
>> celldisp(b)
:/'#
b{1,1} =

2.0000 + 3.0000i

b{2,1} =

1 0 0
0 1 0
0 0 1

b{1,2} =

0 4 6 2 2 9 6 2 1

b{2,2} =

Tal Tsory
!3!01%,!"$)!.$20"!/!"#$%&'(!)*+,!"#'-#".!('&% U

!
!$*90!:#%-.9!".6)!"$'#'8!(%1$.9!.*!;02!0$$< !plot !(&#81)!*#,$*!$76 54
!!A (0#<(,!"$0@,01!(9##*,)!*#,$*!$76 !? !=0#9)!$'*,!:#%-.9# !4> !=0#9)
B 0 & # & 2% !0*9-! x $ f !# ", y $ g!# "
BC$0$<(!C6!C$&-.",!($'#%-.9#!"$*90)!(D-0,*!(%1$.9!A"$'#'8!(%1$.9 U U

!
!B5EH3G !(&#8')!#D-0,* !F !0@#8) !.26,!.*!;02!0$$< !ezplot !(&#81)!*#,$*!$76 5E
B5 C$0$<(!$'*!0#)6!&$+9!(&$,!('8)!*,"*(.!*$G

A &+9!$102!:#.+)!"#9)(!"#$<8'#1(!"9!0$$< 53

y1 $ 2 ' x ' e / x / 2
x . % * /x / 2
! y 2 $ . sin-+
1 3
('e
5 4 , 2 )
!0 & x & 10"
!
!H ;$<0!#8)!:('$)!"#0)#+,(!"# ,#&9!"#$)-#- ,!)-0#" !('#*90( !(,#86(G
B5#8#8,!#8)!:('$)!"#0)#+,(!"#8#0$!"#&#8',!($'*(#
!B5908,!;%#(G!"+9!@#@0*!"&#81)!*#,$*!$76 B9
B5"$,"$02#.!$<+!(.8%)!2<(G!hold on!I!@#@0* !"#&#81!$"*)!*#,$*!$76 B)
B5subplot!(&#81)!*#,$*!$76G !"&01'!C$0$<!"-06,)!6$1#"!($<8'#1!.-*!=- B2
!"&#8'!"9!$102!:1#9)!9<, !ginput !(&#81)!*#,$*(# !zoom !6#< $)!$76 B&
B5(&#8'(!$0+9!"#01%!J!.*!8#$&)G!"#$<8'#1(!:$)!('#*90(!=#"$+(
!B908,#!"*0!$##8! x,y!C$0$<(!:#$<( !Y vs. XG!"0"#-!C6!2$<(.!*$!;02!.-!*
!
K:#@#$' ! "@$*)!*#,$*!$76 ! 2x . exp!x / % " . sin!%x " $ 20 !A (9##*,(!"9!0#"1 5J
!(0#<(,!(9##*,!0#"1.!:"$'!#D!"$0,#'!(@$*!$1. !B5Newton-RaphsonG !:#%10
!A $)$@0@$9!:1#9)!(9)(!(+%#'(!".61(!$76 ! f !x " $ 0
df f !x i "
!8#$&.!"%'-",!(0&%(!0*9-!.)8",!:#0"1 !! f ' !x " $ ; x i .1 $ x i /
dx f ' !x i "
!"$'-#"!)#"-.!=$.6 !B x 0 !(.+"((!$9'")!$#."!9#(# ! x i .1 / x i 1 0 !A$#<0(
!(9##*,(!:#0"1!"9!(0$D+,#!$#<0!8#$&#!(.+"(!$9'"!*,"*,(,!"@.#8*
!:#0"1!.)8"(!9.!C9 !B %'-"(.!$&-)!(*0&!"$'-#"(*!"#'#$%'(!01%,!"9#
!0#)6!:#0"1!.)8.!:"$'!9.*!(6&#(!0$D+"!"$'-#"(!"#'#$%' !4>>H>>> !$0+9
!$0+9!"#01% !J !.*!8#$&# !> !A(. +"(!$9'"!0#)6 !A9,2#&. !B #%'-#(*!C$9'"(
U U

!A (.+"(!$9'"!0#)6# !H"#'#$%' !L !$0+9 !FB4L3J !A :#0"1!0$D+"!"$'-#"(!(&#8'(


!(%' !B"#'#$%' !4>>H>>> !$0+9!C2!%'-""!9.!"$'-#"(!8#$&!#"#9!C6 !4>H>>>
B C$.&)((!"9!:+)#!C$'#*!8#$&#!(.+"(!$9'"!C6!"$'-#"(!"9!.$61(.

!&6 !input !(&#81)!0D#+!*#,$*!$7 6!"#< $0@,!*,"*,(,!"@.#8(!"$'-#"!)#"- 5M


!C#-%!"9!(<$0@,!.-!0#)6 !)*+" !"$'-#"( !B> !=06(!"9!%$'-,!*,"*,(!0*9
!('#*90(!(<$0@,(*!=-!(.#6!0&%)!"#<$0@,(!"9!*&+,!0&%"# !C$0)$9(
!C$0)$9(!C#-%!C6!#D!9$(!('#0+9(#!0"#$)!:@8(!C$0)$9(!C#-%!C6!#D!9$(
!.)8",*!&6!C$-06!@#.8.!=$*,"!"$'-# "(*!).!#,$* !A(06( !B 0"#$)!.#&2(
U U

N %19!9#(!(<$0@,)!C$0)$9(,!&+9*!&6!9.#!%19!=06(
!
!A9,2#&.
Enter matrix: [1 2; 3 4]!
Enter matrix: [1 0]!
Enter matrix: [12 15]!
Enter matrix: 7!
Enter matrix: 0!
!
MM {1} =!
!
1 0!
!
!
MM {2} =!
!
7!
!
!
MM {3} =!
!
1 2!
3 4!
!
!
MM {4} =!
!
12 15
1!"#$%!&'()
U

:*+",&
8!*(+',!%2-4!.+#/&'+ 10!*(+',!%-'(!.+#/&'!0&1,!0%2+23!"#$%&'!(%%)
:*+-+.#
>> teta=linspace(0,2*pi,100);
>> x=5*cos(teta);
>> y=4*sin(teta);
>> plot(x,y);
>> grid on;
>> title ('Ellipse');
:/'#
Ellipse
4

-1

-2

-3

-4
-5 -4 -3 -2 -1 0 1 2 3 4 5
2!"#$%!&'()
U

:*+",&
Ezplot "8+3$!%"1 (2,3)!"8+32,!+9/(4- 6!(5+3,!&614!&-!7(6!(%%)
:*+-+.#
>> x=linspace(-4,4,100);
>> y=linspace(-4,4,100);
>> ezplot('(x-2)^2+(y-3)^2-9');
>> grid on;
>> axis equal;
>> axis tight
:/'#
(x-2)2+(y-3)2-9 = 0

5.5

4.5

3.5

3
y

2.5

1.5

0.5

0 1 2 3 4
x
3!"#$%!&'()
U

.(
:*+",&
'(34!7#+" +!0:'!5+5(-!08+3$,!-+4%-!%"1!0+2+02"!0+%)32+$"!0'!(%%)
:*+-+.#
x=linspace(0,10,100);
y1=2*x.*exp(-x./2);
y2=1/5+(3/4)*sin((x+pi)/2).*exp(-x./2);
plot(x,y1,'r*-',x,y2,'g.--')
legend('y1','y2');
xlabel('x');
ylabel('y1,y2');
:/'#

1.5
y1
y2

1
y1,y2

0.5

0
0 1 2 3 4 5 6 7 8 9 10
x
.0
:*+",&
0%40%(6+&!%):!"&'3#,!6)"+ Hold on+0+8+3$!%0-,!!-+4%-!%"1!0+%)32+$"!0'!(%%)
:*+-+.#
x=linspace(0,10,100);
y1=2*x.*exp(-x./2);
y2=1/5+(3/4)*sin((x+pi)/2).*exp(-x./2);
semilogx(x,y1,'r*-');
hold on;
semilogx(x,y2,'g.--');
xlabel('x');
ylabel('y1,y2')
grid on;
:/'#

1.5

1
y1,y2

0.5

0
-1 0 1
10 10 10
x
.1
:*+",&
8($2!.+&:,!1%$+0!"%)32+$!&/-!*/!0+%)32+$"!0'!(%%)
:&-+.#
x=linspace(0,10,100);
y1=2*x.*exp(-x./2);
y2=1/5+(3/4)*sin((x+pi)/2).*exp(-x./2);
subplot(2,1,1);
plot(x,y1,'r*-');
xlabel('x');
ylabel('y1');
subplot(2,1,2);
plot(x,y2,'g.--');
xlabel('x');
ylabel('y2');
grid on;
:/'#

1.5

1
y1

0.5

0
0 1 2 3 4 5 6 7 8 9 10
x

1
y2

0.5

0
0 1 2 3 4 5 6 7 8 9 10
x
.-
:*+",&
*+0%:!08+32!0'%)4& ginput!"8+3$,!-+4%-+ zoom!"8+3$,!-+4%-!%"1!0+%)32+$"!0'!(%%)
:*+-+.#
x=linspace(0,10,100);
y1=2*x.*exp(-x./2);
y2=1/5+(3/4)*sin((x+pi)/2).*exp(-x./2);
plot(x,y1,'r*-',x,y2,'g.--');
xlabel('x');
ylabel('y1,y2');
legend('y1','y2');
[X,Y]=ginput
:/'#
X =

0.4954

Y =

0.7697
4!"#$%!&'()
U

:*+",&
:.+#$(-.+5+%2!05%-!0(91,!"',"!0%(,6&'!"'++-4!&-!.+(0$!0')+4"!0%2/+0!,+0/
2 x $ exp ! x / # " $ sin !# x " % 20
0+2+%#2"!($#4!0'!0($+#+!0+($# 4!&-!3+%8,
:*+-+.#
x=input('Please enter initial condition: ');
ac=input('Please choose accuracy rate: ');
for counter=1:1e5
fx=2*x+exp(x/pi)+sin(x*pi)-20;
dfx=2+(1/pi)*exp(x/pi)+pi*cos(pi*x);
xx=x-fx/dfx;
if abs(xx-x)<eps
disp(['The Solution is: ', num2str(xx)]);
disp(['Number of tries is: ', num2str(counter)]);
break;
end
x=xx;
end
if counter==1e5
disp('Solution is not available!!!');
end
:/'#!*+(%1+-
Please enter initial condition: 987
Please choose accuracy rate: 4
The Solution is: 6.1734
Number of tries is: 318

Please enter initial condition: 10000


Please choose accuracy rate: 4
Solution is not available!!!

Please enter initial condition: 999


Please choose accuracy rate: 4
The Solution is: 6.1734
Number of tries is: 322
5!"#$%!&'()
U

:*+",&
!(8#!%$&!*(14"!0'!(8# .8($2,!04/#4!'%"!")%(54!&/ .0+)%(54!-40-4"4!05&+3"!0%2/+0!,+0/
!"+,6"!;+/#"!0&1,!0'9+!"2+-'(!1%$+0!(0+%,!.53"!;+/#"!0&1,!")%(54"-!*/ ,0+)%(54"!%4+/#
.0!&-!*(1!&,30%!(-'/!3%#$0!0%2/+0" ."2+(:'!(0+%,
:&-+.#
value=1;
Loc=0;
while value~=0
Mat=input('Enter Matrix: ');
if and (sum(sum(Mat))==0, length(Mat(:))==1)
break
end
Loc=Loc+1;
M{Loc}=Mat;
SUM(Loc)=sum(sum(Mat));
end
[SSUM,ind]=sort(SUM);
MM=M;
for I=1:Loc
MM{I}=M{ind(I)};
end
celldisp(MM);

:/'#
Enter Matrix: [1 2 ; 3 4]
Enter Matrix: 123
Enter Matrix: [16 18]
Enter Matrix: 1
Enter Matrix: 0

MM{1} =

MM{2} =

1 2
3 4

MM{3} =

16 18

MM{4} =

123
!3!01%,!"$)!.$20"!/!"#$%&'(!)*+,!"#'-#".!('&% U

!
!<(!<$'#*60(!<$$'*( !; :.7!$'#"' !9 !".)7,!0*6 !cal !"607'(!($87'#1!)#"- 54
!(.#>1(!2#%!"6!>)7"*!#=!6$(!"=#0+,( !5 "=#0+,!6#(!$*$.*(#!<$01%,
!5 BGB!;BF B!;BE B!;B DB!;BCB !A "#'#*!"#$#0*16 !@ !?#",!<$01%,(!.>!>8#)"*!"$'#)*+(
!I%#( !5BCB !A 0#)$+!($("!.&+,(!"0$0)!<$01%,( !H !"6!70!71$%!*, "*,(!<6
5(80(!"#6,2#&!01%,!28(#!help!($87'#1.
!A"#6,2#&
U U

!
>> cal (4,5)
ans =
9

>> cal (4,5,'-')


ans =
-1

!5 )+0,)!.#.%,)!>'(!0#&-!.*!">8#,,!"$>20!"#0$(,!")*+,(!($87'#1!)#"- 5H
!"6!<$..#-!z!D#!y!,x!<$0#:7##(!A<$0#:7#!(>)06!<(!($87'#1(!.*!:.7(!$'#"'
!".$+",!I.+*!J,=(!"6!..#- !t !0#:7#(#!0#&-(!.*!<#7,(!"#:6'$&0#6#7
!..#-( !v !0#:7#!0$=+(.!(-$08!($87'#1( !5 (&#7'!.-.!#">2(.!&>#!0#&-(!">#'"
!t !0#:7#)!J#*60(!0)$6( !A"#0>( !5 (&#7'!.-)!">8#,,(!"$>20(!"#0$(,(!"6
U U

!J##-,!"#+1!&+6!0)$6!.$-, !v !0#:7#( !; ('#*60(!(&#7'(!#=*!J ##-,!%16!6#(


!"#6,2#&!01%,!28( !5 ('#*60(!(&#7')!"#0$(,(!"6!")*+,!('$6!($87'#1(*
5(80(
!
!A6,2#&
U U

>> x = [0 1.5 2.8 4 5.6 7];


>> y = [0 1.1 2.4 3.8 4.5 5.2];
>> z = [0 4 7 9 13 20];
>> t = [0 1 2 3 4 6];
>> velocity(x,y,z,t)!
ans =!
4.4113 3.5185 2.7203 4.3646 3.5864!
!
!
!#'$6!"#&#,>(!01%,K !"#&#,> !n D#!"#0#* !H !.*!(8$0:,!":.#7(!($87'#1 !)#"- 59
!"0&%*!?- ! 0#*$,)!"#&#7'!"0&%!.*!"#:6'$&0#7!(.$-,( !L *60,!>#&$
!<$'#"'(!$-!+$'(.!J"$' !; :.7!"7$&))!?0#8!J$6K !0#,7!>.#8,!(0$&2,!"#&#7'(
!":.#7!($87'#1(!I%#') !5L 0#,7!>.#8,!&$,"!<$08#$!($87'#1.!<$71#%,(
!"6 !; >.#8,(!+:*!"6!0$=+(.!(-$08!($87'#1( !5 "#1$18!.*!I%#'!:',#206
!"0"#-)!<#*0.# !> .#8,(!"6!2$8,(!I02!::0*. !; ?0#6!"&$+$.!>.#8,(!"%,
!"#$,$'1(!#$"#$##=!.-*!>.#8,!6#(!0#,7!>.#8,!A(0>(!5>.#8,(!+:*!"6!I02(
5"#.>,!4MND,!"#':7
!
!"=#0+,( !5 $6076!<#+")!I02!(2$8,#!"#=#0+, !9 !".)7,(!($87'#1!)#"-! 53
! f !x " !('"*,!.*!($87'#1!6$(!($$'*(!"=#0+,( !; O)#7!<*!6$(!('#*60(
!<6!>)7"!6$(# !on/off !<$-0>!.)7"!"$*$.*(!"=#0+,(#!I02)!2$8(. ! *$!("#6
!J$)!$6076!<#+") ! f !x " !.*!$102!:.1!2$8"!($87'#1( !5 6.!#6 !LgridK !"*0!28#"
!.* !workspace ! D(!"6 !O)#7)!0#,*"# !L <#+")!"#&#7' !4NNN !0#)>K !ND4NN
!" 0$0)!<>!:.7!$'#"' !H !<>!($87'#1(!.*!(.>1(!<2!0*16.!*$ !5($87'#1(
5(80(!"#6,2#&!01%,!28(!5grid on!.*!.&+,
!A6,2#&
U U

>> savefunc ('filename','x.*exp (sin(x))','on')!!!

!
1!"#$%!&'()
U

:*+",&
."4.'5&!+(+#(*. ,2+'/3&!2*!2+-.()'*!2++-(* ,1#%!+-."- 3!"#$%&!'()!*+,%-./!$."0
.(^,/,*,-,+)!2+'/3&*!+-(!#6!"+-.$(5*!*#.6/*!")!6$%"!"4.'5&*
.2+'/3&*!+-(!7+$ '+'!*+*"!"+-.$(5*!*#.6/* ,"4.'5&!*%/.3!)#!2)
:&-+.#
function y=cal(a,b,c)
%Cal is a calculator meant to deal with easy mathematical operators.
%The user is inputing number into "a" and "b".
%and inputing the desired operator into "c".
%The variaty of operators is limited to: "+,-,*,/,^"
%if an operator is not inserted, the default operator will be "+".

if nargin<2
error('Not sufficient Data');
elseif nargin==2
c='+';
end
switch c
case '+'
y=a+b;
case '-'
y=a-b;
case '*'
y=a*b;
case '/'
y=a/b;
case '^'
y=a^b;
otherwise
disp('Cal does not know how to use this kind of an operator');
end
end
:&"/,!0'#
help cal
Cal is a calculator meant to deal with easy mathematical operators.
The user is inputing number into "a" and "b".
and inputing the desired operator into "c".
The variaty of operators is limited to: "+,-,*,/,^"
if an operator is not inserted, the default operator will be "+".
:&1"&!*+(%2+-
>> cal(2,3)

ans =

>> cal(2,3,'-')

ans =

-1

>> cal(16,2,'/')

ans =

2!"#$%!&'()
U

:*+",&
.$5'&$!'.80!#(!"+69'*!".'+*&!"$(5&*!*+,%-./!$."0
."+69'*!".'+*&*!#(!:'6*!")!*'+45&.!**4!#8.9!+#6$!(&"(&*&!1#%!+0'6& 4!"#$%&!*+,%-./*
:*+-+.#
function y=velocity(x,y,z,t)
%The function Velocity is calculating the instant velocity in 3D space
%Please note that vectors must have the same length
if and (length(x)==length(y), length(y)==length(z))
if length(z)==length(t)
y=sqrt(diff(x).^2+diff(y).^2+diff(z).^2)./diff(t);
else
disp('Vectors must have the same length for calculation');
end
end
:&1"&!*+(%2+-
velocity([0 1.5 2.8 4 5.6 7], [0 1.1 2.4 3.8 4.5 5.2], [0 4 7 9 13 20], [0 1
2 3 4 6])

ans =

4.4113 3.5185 2.7203 4.3646 3.5864

velocity([0 5.6 7], [0 1.1 2.4], [0 4 20], [0 1 2])

ans =

6.9692 16.1137

velocity([0 5.6 7], [0 1.1 3], [0 4 20], [0 2]) %watch vector t


Vectors must have the same length for calculation
3!"#$%!&'()
U

:*+",&
"./+/,*!")!1.#%"!70!.&0 ,2+0'6!#(!".'.( 2!"#6$!*,+'1&!"1#.%*!*+,%-./!$."0
,:'.)!"8+5+#!6#.,&*!"3& ,6#.,&*!#(!51(*!:'6!")!*'+45&. ,'&.%&!6#.,&!*'+89&!*+,%-./*
.6#.,&*!"'".0!")!;'9*!"'".0$!2.('".!6#.,&*!")!11'("
:*+-+.#
function [area,mass]=convexpoly(m,d);
%function convexpoly(m,d) is getting an array of numbers
%indicating the location of the edges of a convex polygon inside m.
%Also the function is getting the density of the polygon.
%The function is returning the area of the convex polygon and it's
%mass per unit of length.
%Also the program will plot the convex polygon
%and add a title about the area of the polygon
n=length(m);
N=m(:,1);
a=0;
for I=1:(n-1)
a=a+(m(1,I).*m(2,I+1)-m(1,I+1).*m(2,I));
end
a=a+m(1,n).*N(1)-m(2,n).*N(2);
area=abs(a*0.5);
mass=area*d;
m(1,n+1)=m(1,1); m(2,n+1)=m(2,1);
plot (m(1,:),m(2,:));
grid on;
xmax=max(m(1,:)); xmin=min(m(1,:)); ymax=max(m(2,:)); ymin=min(m(2,:));
axis([xmin-0.1 xmax+0.1 ymin-0.1 ymax+0.1]);
title(['The area of the Convex Polygon is: ', num2str(area)]);
:&1"&!*+(%2+-
[area,mass]=convexpoly([0 0 1.5 3 3 ; 0 1.5 3 1.5 0.5], 2)

area =

6
The area of the Convex Polygon is: 6
3

mass =
2.5

12
2

1.5

0.5

0
0 0.5 1 1.5 2 2.5 3
[area,mass]=convexpoly([0 3 3 0 ; 0 0 3 3], 4)

area =

9
The area of the Convex Polygon is: 9
3
mass =

36 2.5

1.5

0.5

0
0 0.5 1 1.5 2 2.5 3

U 4!"#$%!&'()

:*+",&
!;+3.-!2)*.!11'(#!*,'-!*".)!"+1&"&*!*+,%-./*!") ,<$.%*!2(!")!(&"(&*&!1.#%"!*+,%-./*
!*+,%-./*!;'9!")!11'("!*+,%-./* .(".4.'5&$!".+*#!2+0+',!2+1#%*!"(.#(!#0)!)#!.)!9+'3
!"'+'$ ,+(+#(!1#%!*+*!)#.!*'%&$ .;'9*!"'".0$.!2+'+,$!2+&+)"&!2+$."+0!;+3.".!"+1&"&*
.9+'3!"/3.*!)+*!#85&*
:*+-+.#
function savefunc(file,fx,grd)
%The function savefunc(file, fx, grd) is getting between 2 to 3 input data.
%file - is getting the name of the file (string)
%fx- is getting the name of the function f(x) (string)
%grd - is getting value of 'on' or 'off' (string)
%In case the user didn't input into grd, the default mode will give 'on'
%The function is saving in a file it's workspace
if nargin==2
grd='on';
end
range=100*sort(rand(1,2));
x=linspace(range(1),range(2), 1000);
y=eval(fx);
plot(x,y);
switch grd
case 'on'
grid on;
case 'off'
grid off;
end
title(['The function range is between: ',num2str(range(1)),'<=x<= ',
num2str(range(2))]);
xlabel('x');
ylabel(fx);
save(file);

:&1"&!*+(%2+-
savefunc ('Try1','x.^2','on')
The function range is between: 44.5586<=x<= 64.6313
4500

4000

3500
x. 2

3000

2500

2000

1500
40 45 50 55 60 65
x

savefunc ('Try2','x.*exp(sin(x))','on')
The function range is between: 27.6025<=x<= 67.9703
180

160

140

120
x.*exp(sin(x))

100

80

60

40

20

0
25 30 35 40 45 50 55 60 65 70
x
!3!01%,!"$)!.$20"!/!"#$%&'(!)*+,!"#'-#".!('&% U

!
!8(7)(!"#7##*,(!"-06,!('#"' 54
x"y"z !5
! 2 x " 2z ! 6
3x " y " z ! 7

!9"#'#*!:$-0&!*#.*)!"#7##*,(!"-06,!"7!0#"1 97
!"#7##*,(!"-06,!"7!0#"1 ! 3x " 3y " 5z ! 0 !8 "1%#'(!(7##*,(!('#"' 9)
!:61#!$.,$ '$,!($($!"#7$2*(!$6#)$0!:#-%*!;-!"+7!:61 !9:$$,61
9 :$$)#$+!"#'#0"1!<0!#.)<"$!=%#')*!;-!($$'*
!5Least square? !$.,$'$,!"#7$2*(!$6#)$0!:#-%!#0#)6*!>#0"1 !8(06( U U

!6#)$0)!(7@#"(!"7!(.6'!(&#<'!.-)!(7$2*(!"7!)*+'!:7*!#"#6,*,
!!9$.,$'$,!:#-%!.)<'!"#&#<'(!.-!0#)6!#'.)$<*!"#7@#"(!.-!"7!:#-%'#
!
8(7)(!"#7##*,(!"-06,!('#"' 5A
x 1 " x 2 " x 3 " x 4 ! 100
!
2 x 1 " 4 x 2 " 6 x 3 " 8x 4 ! 150
!>#0"1(*!<#&) !B "$.,$'$,!($("!(,0#'(*!;-!"#7###*,(!"-06,!"7!0#"1
!9 (,0#'(!;06!"7!7@,#!"#7##*,(!"-06,!"7!:$$<,
!7#(!>#0"1(!0#C<#!.&#2*!#"#6,*,!"$.,$'$,!#.*!(,0#'(*!>#0"1 !8(06( U U

!9$.,$'$,
!
80#*$,)!"#&#<'!"0&%!('#"' 5D
x 0 0.89 1.79 2.69 3.59 4.48 5.39 6.28
y 2 6.26 11.38 8.21 13.41 13.05 21.89 23.95
!
!:$"#+1(!:$6#)$0(!"C$*)!"#&#<'(!"0&%!"7!")0<,(!"$'-#"!)#"-
( x2 %
! y ! a 1 " a 2 x " a 3 sin && ## !! !82#%,!($@<'#1.
' 2 $
!.*!(,#<6(!"7#!"#&#<'(!"0&%!"7!.$-,(!=02!CC0*"!"$'-#"(
!a !$-06!:6!")0#<,(!($@<'#1(!"7!.$ -"!=02(!"0"#- !9 ")0#<,(!($@<'#1(
!9:$,$7",(
!
!"$*70,!0#*$,(!<+0,!"7!7@, ! 2x " 7 y " z ! 6 !8 0#*$,(!"7##*,!('#"' 5E
95"+7!(&#<1!"0#*)!>#0"1(!"7!)#"-?!:$0$@(
!
!"@$0C,!0#@$"!($@<'#1( !9 $)#$+#!:.* !n !01%,!".)<,(!($@<'#1!)#"- 53
!"%1#"*!>#0-F)!.&#2(!"7!%$1& "#!(.$.&!(&$+$!"@$0C,#!nXn !.&#2)!(&$+$
95whos!(&#<1)!*#,$*!$G6?!(@$0C,!.-
!
!95 C.<!"<$&))!;0#@!>$7? !$(*.-!"$6#)$0!(@$0C,!".)<,(!($@<'#1!)#"- 5H
!.*!:$$,@6(!:$-06(#!(20&( !B (C''$,0C&(!"7!%$1&"#!)*+"!($@<'#1(
!(@$0C,(!.* !M !"$.&#,(!(@$0C,(!7#(*!C',#207!0$F+"#!(@$0C,(
!:$0#C<#(!:(!($"#&#,6*!(@$0C,!7$(!"$.&#,!(@$0C, !8(06( !9"$0#<,(
U U

9(@$0C,(!.*!:$$,@6(
!
8:$7)(!"#&*(!"7!..#-(!structure!2#%,!('"*,!"C.#<(!($@<'#1!)#"- 5I
!9x!"#C7'$&0#7#<!0#C<##!/!Xdata
!9y!"#C7'$&0#7#<!0#C<##!/!Ydata !
!9plot!(&#<1)!:#<6(!>#'2%!"7!0$&2,(!&+7!#"!/!LineStyle !
!9plot!(&#<1)!:#<6(!6)@!"7!0$&2,(!&+7!#"!/!Color !
!9 C.<(!('"*,)!:$'#"'.!:7"()!5plot?!:#<6!CC0*.!(-$0@!($@<'#1(
!
1!"#$%!&'()
U

.(
:*+",&
:!"#!$%&"&&'(!$%)*+($!,&%,
x! y!z "5
2x ! 2z " 6
3x ! y ! z " 7
%&,&'$-.)*/$'&0'#$*&%1
:-'.!*/+.#
>> A=[1 1 1; 2 0 2; 3 1 1];
>> Y=[5 6 7]';

:&/+.# -&0+)("!1"/
x=inv(A)*Y
:-'#
x=

1.0000
2.0000
2.0000

:&/+.# -&220)!1"/
x=A\Y
:-'#
x=

1
2
2

:&/+.# -*2)2')!1"/
x=Y'/A'
:-'#
x=

1 2 2
.3
:*+",&
3 x ! 3 y ! 5 z " 0 :%12&,$!"&&'($!,&%,
-..#&.4$5*$%&,&*%1$%0#5$-+$!..,'$-+1&$.0"(.,.($%&".3'$.+&#.*$-&)2$-+$%4"$-+1 ,-..(+1$*&%1
:-'.!*/+.#
>> A=[1 1 1; 2 0 2; 3 1 1; 3 3 5];
>> Y=[5; 6; 7; 0]
:*+/+.#
x=A\Y!
xx=lsqnonneg(A,Y)
:-'#
x=

3.5000
-1.5000
-1.0000!
!
xx =

1.6522
0
0
2!"#$%!&'()
U

:*+",&
:!"#!$%&"&&'(!$%)*+($!,&%,
x1 ! x2 ! x3 ! x4 " 100
2 x1 ! 4 x2 ! 6 x3 ! 8 x4 " 150
!(*&,!$6*+$%"$"7(& %.0"(.,.($!.!%$!(*&,!'$6)$%)*+(!$%"$*&%1
%&"&&'(!$%)*+($%"$-..5($8&*%.1!'$5&/#0$6.*7
:-'.!*/+.#
>> A=[1 1 1 1; 2 4 6 8];
>> Y=[100;150]
:*+/+.#
xn=pinv(A)*Y;
norma=norm(xn);
checking=A*xn;
:-'#
xn =

77.5000
42.5000
7.5000
-27.5000

norma =

92.8709!

checking =

100.0000
150.0000
3!"#$%!&'()
U

:*+",&
:-.,&%,

X 0 0.89 1.79 2.69 3.59 4.48 5.39 6.28


Y 2 6.26 11.38 8.21 13.41 13.05 21.89 23.95

:-0$!.75,&1!$%"$%#*5(!$%.,)&%$#&%)
(x %
2
y " a 1 ! a 2 x ! a 3 sin && ##
' 2 $
.%#*&5(!$!.75,&1!$9&9*' ,-.%&41!$-.+&#.*!$%9.'#
:-'.!*+/+.#
x=[0 0.89 1.79 2.69 3.59 4.48 5.39 6.28];!
y=[2 6.26 11.38 8.21 13.41 13.05 21.89 23.95];
:*+/+.#
c=[ones(8,1), x, sin((x.^2)./2)];
A=c\y;
xx=0:0.01:6.3;
yy=A(1)*1+A(2)*xx+A(3)*sin((xx.^2)./2);
plot(x,y,'o',xx,yy,'r');
title([num2str(A(1)),'+',num2str(A(2)),'x+',num2str(A(3)),'sin(x^2/2)']);

:-'#
1.9968+2.9958x+4.0632sin(x 2/2)
25

20

15

10

0
0 1 2 3 4 5 6 7
4!"#$%!&'()
U

:*+",&
2x ! 7 y ! z " 6 :"#!$*&'.(!$0'$%.'"*($54*($"7(
:*+/+.#
a=norm(pinv([2 7 1])*6)
:-'#
a=

0.8165

5!"#$%!&'()
U

:*+",&
%00&/($!7.*9(&$!/.4.$%7.*9($0+$*&".%$*&7.%$!.75,&1! .-0'&$.#&.4$*12($%0#5(!$!.75,&1$#&%)
:*+/+.#
function MatSize (n)
%The function is getting input "n" which needs to be a positive integer
%It will make a unit matrix in the size of "n"
%And will create a sparse matrix the same size
%The function will show the data on each of the matrix, using "whos"
A=eye(n);
B=sparse(A);
whos;

:-'#
MatSize(5)
Name Size Bytes Class Attributes

A 5x5 200 double


B 5x5 84 double sparse
n 1x1 8 double
6!"#$%!&'()
U

:*+",&
%.0/&($!7.*9($9,(&3*"&$!3*/! ,!9,,.(*9/!$%"$!*.:4(&$%.+&#.*$!7.*9($%0#5('$!.75,&1$#&%)
:*+/+.#
function M=mat_ops(n)
%The function mat_ops is getting an argument "n"
%Creating nxn matrix, calcuating it's determinant,
%posting it's rank and making a modular matrix consisting
%the eugenvalues of the matrix
disp(['The Determinant is: ', num2str(det(n))]);
disp(['The Rank is: ', num2str(rank(n))]);
[M,Diagon]=eig(n);
disp(['The Eigenvalues are: ']);
disp(diag(Diagon));

:-'#
mat_ops([1 2 3; 4 5 6; 8 9 13])
The Determinant is: -9
The Rank is: 3
The Eigenvalues are:
19.1322
-0.7552
0.6229

ans =

0.1933 0.8991 0.0969


0.4293 -0.2508 -0.8337
0.8822 -0.3588 0.5436
7!"#$%!&'()
U

:*+",&
:-."#!$%&/'$00&)! ,!,#($%90&5!$!.75,&1$#&%)
Xdata, Ydata, Linestyle, Color
'(%'(!$2.,)!'$%&/'0$-"%!#$;*3$%99*'(&
:*+/+.#
function graph_plot(graph)
%The function graph_plot is getting an input graph as a structure
%it needs to use this input and draw a graph acording to the settings
str=[graph.linestyle, graph.color];
plot(graph.Xdata, graph.Ydata,str);
:-'#
>> graph.Xdata=linspace(0,2*pi,200);
>> graph.Ydata=(sin(graph.Xdata).^2);
>> graph.linestyle='o';
>> graph.color='g';
>> graph_plot(graph)

0.9

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

0
0 1 2 3 4 5 6 7
!3!01%,!"$)!.$20"!/!"#$%&'(!)*+,!"#'-#".!('&% U

!
!@ ($;>'#1(!.*!?$102 != !<subplot !"#:;,6)9 !&+6!$102!7#.+)!880* 54
!*#,$*!$D: !< (C!?#+")!"#&#>' !AB !0#;9 ! 0 & x & 10 @?#+") ! y % x $ e sin !x " # 3
!"#&#,:!E02!52!<stairs9 !"#20&,!E02!5)!F<plot9 !.$20!E02!56!@ ?$6)(!?$102(!$2#%)
!$D:9 !jpg !",#$%!?:!('#,"!G)#>-!E02(!"6!0#,* !5<stem9 !&$&)!E02 !5& !<bar9
5<copy figure! H)!*#,$*!6..9!word! H(!I,%,.!#"#6!E%#(#!<export
!
!"#."- !>D, !4 !+1') !0$#6 !.#, !=B !.* !G+. !&&,'!()!?$$#%$'!"0&%!(-0:' 5J
!$#%$'(!"#6;#"!.* !E02!880* !5 (.)8)!#,*0' ! $#%$'(!"#6;#" !F(0#801,8)
5A!K !.*!(6$2*!7-""!(&#>'!.-)!$-!2;#,!E02)!0*6-

!@$#%$'(!"#6;#"!".)8 U

T [K] 297 317 341 398 413 483 512 577


P [kPa] 98.78 105.43 113.41 132.37 137.36 160.64 170.28 191.90

!?$0$;!"-0:,!.: !F"#,$6", !" #$;>'#1)!*#,$*!$D :!"#6)(!"#0#;(!"6!880* 5A


5 76-!?$)#*+!?'$6!?$>$$&,(!?$.&2(!@(0:(!5"$06.#1 U U

90 1 90 2
120 60 120 60

150 0.5 30 150 1 30

180 0 180 0

210 330 210 330

240 300 240 300


270 270

!
!
!n !.*!(0&$%!(') !5 $)#$+#!?.*!01%,!6#(* !n !8',#206!".)>,(!($;>'#1!)#"- 5=
H. !4BB !7$)!?(, !=BK !F4BB !H. !B !7$)!?(, !ABK H*!I- !?$,.* ! ?$$60>6!?$01%,
!(,-!(2$;,(!(,02#8%$(!880*"!($;>'#1( !54BBBH. !3BB !7$)!06*(!.-# !3BB
!54BBB!&:!(6.(!I-#!BH4BBF4B4HJBBFJB4HABB!@ ?$,#+")!?$6;,'!?$01%,
!
!*#,$*!$D: !&+6!$102 !7#.+) !! z % 200 # x 2 # y 2 !@($;>'#1( ! .*!?$102 != !880* 5L
! !7$)*!?#+")!y !H# !x !0*6- !surf, surfl, mesh, contour !@ ?$6)(!?$102(!$2#%)
!.-!()#2!#>!880*. !*$ !contour ! H(!.*!E02) !5< (&$+$!.*!?$+##0,)9 !4B !H. !H4B
! H*!I- !<view9 !8),(!"&#>'!"6!('* !surfl ! H(!.*!E02) !5 ()#2!"#&$+$ !0*:
5shading!E%#(#!azimuth= 60º , elevation= -30º

!4B !#-0#6 !FL !#%#$&0* !"#-$0- !4B !.:) !G$1>!880* !plot3 !(&#>1)!*#,$*!$D: 53
!5"$*60)!#C-0,#
1!"#$%!&'()
U

:*+",&
:'+*51!-."(!"$ ,0.)1&!0.'.2*&!0)!3#4(##%&!3#5.6!3##*+,!0.5."( 4!"#$%&!'()!#*+,!-."(!+./
Plot, Stairs, Bar, Stem
:-'.
x=linspace(1,10,30);
y=x.*exp(sin(x))-3;
subplot(2,2,1);
plot(x,y);
axis tight;
subplot(2,2,2);
stairs(x,y);
axis tight;
subplot(2,2,3);
bar(x,y);
axis tight;
subplot(2,2,4);
stem(x,y);
axis tight;
:-'#

15 15

10 10

5 5

0 0
2 4 6 8 10 2 4 6 8 10

15 15

10 10

5 5

0 0
2 4 6 8 10 2 4 6 8 10
2!"#$%!&'()
U

:*+",&
!#*"!.")!0.)#,6!&)+& ,3%!"6!7+81!9(""!4(#1!&+.:+*%:&!"6!0.)#,6&!.,/.#!.1!;+,!::+6
.62.1%&!#.4#5&!0.)/.0
:-'.
T=[297, 317, 341, 398, 413, 483, 512, 577];
P=[98.78 105.43 113.41 132.37 137.36 160.64 170.28 191.90];
E=0.03.*P;
errorbar(T,P,E,'-');
axis tight;
xlabel('T [K]');
ylabel('P [Kpa]');
title('Error Bar');
:-'#
Error Bar

190

180

170

160
P [Kpa]

150

140

130

120

110

100

300 350 400 450 500 550


T [K]
3!"#$%!&'()
U

:*+",&
.6+'56!#*$!3#*+,&!0)!::+6
:-'.
subplot(1,2,1);
theta=linspace(0, 8*pi, 1000);
r=linspace(0,1,1000);
polar(theta, r);
subplot(1,2,2);
r=[1 2];
r=repmat(r,1,6);
r=[r, 1];
theta=linspace(0,2*pi,13);
polar(theta,r);
:-'#

90 1 90 2
120 60 120 60

150 0.5 30 150 1 30

180 0 180 0

210 330 210 330

240 300 240 300


270 270
4!"#$%!&'()
U

:*+",&
..&6"$!:"2!+*4%!6%06%&%!:."206!&#/25.*!1.0$
(600-1000!-#1!+)6& ,100-600!-#1 40% ,0-100!-#1 30%)!3#$+8 3-"!+*4%&!0)!2"(0!&#/25.*&
(100!"6!0./#*21)!&%+,.:4#&1!&..6!&2."(!#*"!3,#/0.
:&/0.1+#
function n=barplot(n)
%Barplot is getting as input a number N which is positive and an integer
%It is creating a vector with N random numbers
%Plotting a histogram of the randomized values in the vector
a=0.3*n; b=0.4*n; c=0.3*n;
a=round(100*rand(1,a));
b=round(100+500*rand(1,b));
c=round(600+400*rand(1,c));
n=[a b c];
hist(n);
:-'#
barplot(200)

60

50

40

30

20

10

0
0 100 200 300 400 500 600 700 800 900 1000
5!"#$%!&'()
U

:*+",&
:0.)1&!0.#/25.*1!6.%#6!#"8!''.1!#*+,!-."(1!&#/25.*&!&0.)!+.18!3#*+, 4!::+6"!6#
Mesh, Surf, Surfl, Contour
;+,!"$!+.18!0.5.6&!0.6#+'"!1"!3#6"
:-'.
x=-10:10; y=-10:10;
[X,Y]=meshgrid(x,y);
Z=200-X.^2-Y.^2;
subplot(2,2,1);
mesh(X,Y,Z);
subplot(2,2,2);
surf(X,Y,Z);
subplot(2,2,3);
surfl(X,Y,Z);
view(60,-30);
shading interp;
subplot(2,2,4);
contour(X,Y,Z,10);
:-'#

200 200

100 100

0 0
10 10
10 10
0 0 0 0
-10 -10 -10 -10

10

200 5

0
100

10 -5
0
-10 0
0 -10
10 -10 -10 -5 0 5 10
6!"#$%!&'()
U

:*+",&
.<$+%1!3#+#/&!0#6)+!+6)$ ,5!4.#'+.!0.$#+$ 10!"81 ,10!7+.)!"81!9#*2!::+6
:-'.
t=linspace(0,10,1000);
plot3(5*sin(2*pi*t), 5*cos(2*pi*t),t)
grid on
:-'#

10

0
5
5
0
0

-5 -5
!+!"#$%!&'(!)'*"& U

!
!'70&!"#$%(!8''/$'0!/5".0 !6 ./0%!)1!"20')'3!"/(.!"%/4!"/4()!'25( -,
!1/)1(!8'#/"19!8':*9!&"/;"#%;!922%0!89(!./0%9!)1!8'0/1!92/(.
@?ºC>!!&/7(9!=#%;9 !/)(<&9!8''/$'09!&"2$(!-"20')'3(!&/0/1!&/2/<0

!"#$%&'()*
1 2 3 4 5 6 7 8
1 1853 2027 1828 2101 1878 2088 2154 1893
!"#+) 2 1947 1846 1826 2196 1911 1916 1939 2149
3 2012 1932 1810 1968 2139 2112 1927 1813
!
!- 92/<0!)5(!&')%'$<%9/!&')7%'0'%9!&/"/;"#%;9!A9% -7
-92/(.!'70& !)5(!&')%'$<%9/!&')7%'0'%9!&/"/;"#%;9!A9% -(
-&'))59!&')7%'$<%9!9"/;"#%;9 -*
-92/<0!)5(!A<&9!&'';$/!&.3/%%9!9"/;"#%;9 -2
-92/<0!)5(!$/'$)3!&/).%!CDDDB%!9&).!9"/;"#%;9!8'"<%!9%5( -9
!- &'))59!&.3/%%9!9"/;"#%;9%!EFB(!9&).!9"/;"#%;9!8'"<%!9%5( -/
!8'0/&09!)1!"/:'#9!A/'3)!1%1%9!';$';;$!22%!7'9!A<&!&'';$ !@&"/5:& U U

!!-.3/%%9!('($
!
!8/0')/#)!9'3<0/#9!&7!("< !6H 0 $ x $ 2# G ! y " x 2 ! sin( x ) !@ 9'3<0/#9!90/&0 -C
!&47!8.# !&'"%/0!9"/3(!9'3<0/#9!)1 ! 90/17"9!&":*09!&7!(14 !-I!"2$%
!). !polyder !92/<#9!&/.3%7(!9''01!8.#/ !diff !92/<#9!&/.3%7(
!8/0')/#9!)1/!9'3<0/#9 !)1 !247 !J"* !@ 247!'#"*!A/)4( !;;"1 !-8/0')/#9
-!';')079!A/"&#9!8.!&/":*09!&7!9//19!-&/":*09!)1!'01!J"*/

P1 " x 5 % 2.21 ! x 3 & 0.5 ! x ; P2 " x 4 % x 2 !!!@8'7(9!8'%/0')/#9!8'0/&0 -K

!'1"/1!89!/'1"/11 !'1')1 ! 8/0')/#!73%/!8'%/0')/#9 !'01 !'1"/1!&7!(14 -7


!-! % 1.5 $ x $ 1.5 !@8/4&(!8'%/0')/#9!&1/)1 !&7!J"*(!*39!-8'%/0')/#9!'01
-8'0/17"9!8'%/0')/#9!'01!&)#5%!&7!73% -(
!-,!7/9!9'3"*0'79!'"47!./(<9!"175! P2 !8/0')/#)!9'3"*;0'7!.3( -*
!-8'0/1 !8'5&4( !&/'/"'9%/!8'34)!/22%0!"/0'3( !6 8'%!)'(/%9!"/0'3!A/&0 -I
!&/;'1 !I !&/.3%7( !?m/s> !N !7'9!&/"'9%9!/(!L&4(!8'%9!M4)!&7!73%
!&/73/& !-H,,C-K+ !?kPa >!G !&<''/2%9!973/&)!9//19 !-&/ 0/1!9'3)/#";0'7
@97(9!9)(;(!&/;"/#%!&/2'2%9

P [kPa] 101.3 106.6 109.4 114.7 116.9 118.2 123.7


v [m/s] 1.69 3.67 4.37 5.45 5.84 6.05 6.90
!
!9"/;"#%;!922%0!9;)#9!). !6%O$ !KDxKD !8'2%'%( ! &.(/"%!9;)#!90/&0 -N
!&/*)#&9!&7!*'3%9 !HmeshG !'2%'% B&)&!J"*!;;"1 ! !- &/0/1!&/2/<0!"#$%(
!spline !*/$%!&'2%'% B/2!9'3)/#";0'7(!1/%'1!'O. !- 9;)#9!).!9"/;"#%;9
!&7!*'3%9!J$/0!J"*!;;"1/!8'%)1 !x, y !!)5!"/(.!9"/;"#%;9!&7!(14
- 9'3)/#";0'79!'"47!9;)#9!).!9"/;"#%;9!&/*)#&9
@97(9!9)(;(!&/;"/#%!&/2'2%9!&/73/&
!
x [cm] 0 10 20 30 0 10 20 30 0 10 20 30
y [cm] 0 0 0 0 15 15 15 15 30 30 30 30
T [ºC] 20 22 25 23 22 27 30 25 19 20 18 22
!
1!"#$%!&'()
U

:*+",&
:!"#!$%&'()'!$)*#+$!'()'
!&-+.,!/(0*

1 2 3 4 5 6 7 8

1 1853 2027 1828 2101 1878 2088 2154 1893

&-+10 2 1947 1846 1826 2196 1911 1916 1939 2149

3 2012 1932 1810 1968 2139 2112 1927 1813

:%&"#!$%&,&-.!$*-$!'-

.!3(2'$*4#$)&*"/&.2/!($)&*"/&'&/!$)(0(+0,/+!$1!/ ."
.!3(#-$&"')$*4#$)&*"/&.2/!($)&*"/&'&/!$)(0(+0,/+!$1!/ .#
.)&**4!$)&*"/&.2/!$!0(+0,/+! .5
.!3(2'$*4#$12)!$)&&+.($)-6(//!$!0(+0,/+! .3
.!3(2'$*4#$.(&.*6$)(*-/ 2000-/$!)*-$!0(+0,/+!$%&02/$!/4# .!
.)&**4!$)-6(//!$!0(+0,/+!/ 9%-#$!)*-$!0(+0,/+!$%&02/$!/4# .(
:*+-+1#
a=[1853 2027 1828 2101 1878 2088 2154 1893];
b=[1947 1846 1826 2196 1911 1916 1939 2149];
c=[2012 1932 1810 1968 2139 2112 1927 1813];
A=[a; b; c];
max_alef=max(A,[],2) %part alef
min_alef=min(A,[],2)
max_bet=max(A,[],1) %part bet
min_bet=min(A,[],1)
max_tot=max(max(A)) %part gimel
avg_points=mean(A,2) %part daled
teken_point=std(A,0,2)
point1=sum(logical(find(A(1,:)>2000))) %part hei
point2=sum(logical(find(A(2,:)>2000)))
point3=sum(logical(find(A(3,:)>2000)))
aver_tot=mean(mean(A)); %part vav
cases=sum(logical(find(A(:)>aver_tot*1.09)))
:2'#
max_alef = !"#$%&

2154
2196
2139

min_alef =

1828
1826
1810
max_bet = '"#$%&

Columns 1 through 6

2012 2027 1828 2196 2139 2112

Columns 7 through 8

2154 2149

min_bet =

Columns 1 through 6

1853 1846 1810 1968 1878 1916

Columns 7 through 8

1927 1813
max_tot = ("#$%&

2196
avg_points = )"#$%&

1.0e+003 *

1.9777
1.9663
1.9641

teken_point =

128.7054
134.6973
121.7485
point1 = *"#$%&

point2 =

2
point3 =

3
cases = +"#$%&

2!"#$%!&'()
U

:*+",&
2
$)"$#78 .4 $03./$%('&*(,*$!&62'(,!$)"$#02 ,( 0 $ x $ 2# ) y " x ! sin( x ) : !&62'(,!$!'()'
$)(-6/"#$!&&'7$%-,( diff$!3(2,!$)(-6/"#$)8"$%-,$)&0/('$!0(6#$!&62'(,!$*7$!'(7"0!$)095'!
$&'7$:05($%('&*(,!$*7($!&62'(,!$*7$38"$:05 :38"$&,05$1(*8#$++07 .%('&*(,!$*- polyder$!3(2,!
.$&+&*'"!$1(0),!$%-$)(095'!$)"$!((7! .)(095'!$*7
:*+-+1#
x=linspace(0,2*pi,100);
y=x.^2.*sin(x);
p=polyfit(x,y,4);
nig_anal=2.*x.*sin(x)+x.^2.*cos(x);
nig_diff=diff(y)./diff(x);
nig_poly=polyder(p);
subplot(1,2,1)
plot(x,y,x,polyval(p,x),'r--');
legend('func','polynom');
subplot(1,2,2)
plot(x,nig_anal,x,[0,nig_diff],'r--',x,polyval(nig_poly,x),'g-');
legend('analitic','diff','polyder');
:2'#

5 60
func analitic
polynom diff
50
0 polyder

40

-5
30

-10 20

10
-15

-20
-10

-25 -20
0 2 4 6 8 0 2 4 6 8
3!"#$%!&'()
U

:*+",&
5 3 4 2
P1 " x % 2.21 ! x & 0.5 ! x ; P2 " x % x :%&"#!$%&/('&*(,!$%&'()'
$56! . %&/('&*(,!$&'7$&70(7$%!$(&70(77$&7&*7$%('&*(,$"6/($%&/('&*(,!$&'7$&70(7$)"$#78 ."
. % 1.5 $ x $ 1.5 :%(8)#$%&/('&*(,!$)7(*7$)"$:05#
.%&'(7"0!$%&/('&*(,!$&'7$)*,4/$)"$"6/ .#
.1$"(!$!&605+'&"!$&08"$-(#2!$07"4 P2 $%('&*(,*$!&605+'&"$-6# .5
:*+-+1#
p1=[1 0 -2.21 0 0.5 0];
p2=[0 1 0 -1 0 0];
r1=roots(p1) %alef
r2=roots(p2)
r3=[r1',r2'];
p3=poly(r3)
x=linspace(-1.5,1.5,200);
plot(x,polyval(p1,x),x,polyval(p2,x),x,polyval(p3,x));
legend('P1','P2','P3');
c=conv(p1,p2) %bet
integ=polyint(p2,1) %gimel
:2'#
r1 = !"#$%&

0
-1.3979
1.3979
-0.5058
0.5058

r2 =

0
0
-1
1

p3 =

Columns 1 through 7

1.0000 0 -3.2100 0.0000 2.7100 -0.0000 -0.5000

Columns 8 through 10

0 0 0
3
P1
P2
2 P3

-1

-2

-3
-1.5 -1 -0.5 0 0.5 1 1.5

'"#$%&
c =

Columns 1 through 7

0 1.0000 0 -3.2100 0 2.7100 0

Columns 8 through 11

-0.5000 0 0 0
integ = ("#$%&

0 0.2000 0 -0.3333 0 0 1.0000

4!"#$%!&'()
U

:*+",&
$(#$;)8#$%&/!$<8*$)"$"6/ . %&'(7$%&4)8#$)(&(0&!/($%&68*$(33/'$0('&6# , %&/$*&#(/!$0('&6$1()'
[kPa ] ) $)2&&(3/!$!"6()*$!((7! . )('(7$!&6*(,0+'&"$)(+&7 4 $)(-6/"# [m/s] 5 $"&!$)(0&!/!
:!"#!$!*#+#$)(+0(,/$)(3&3/!$)("6() .(112.37
P [kPa] 101.3 106.6 109.4 114.7 116.9 118.2 123.7

v [m/s] 1.69 3.67 4.37 5.45 5.84 6.05 6.90


:*+-+1#
p=[101.3 106.6 109.4 114.7 116.9 118.2 123.7];
v=[1.69 3.67 4.37 5.45 5.84 6.05 6.90];
lin=interp1(v,p,5,'linear')
cub=interp1(v,p,5,'cubic')
spli=interp1(v,p,5,'spline')
near=interp1(v,p,5,'nearest')
:2'#
lin =

112.4917

cub =

112.3724

spli =

112.3591

near =

114.7000

5!"#$%!&'()
U

:*+",&
. )('(7$)(3(2'$0,./#$!0(+0,/+$!33/'$!+*,!$*- ,/". 30x30 $%&3/&/#$)-#(0/$!+*,$!'()'
$7(/&7$&"- . !+*,!$*-$!0(+0,/+!$)(5*,)! $)"$5&6/! (mesh) $&3/&/ -)*)$:05$++07
$:.('$:05$++07($%&/*7 x, y $$*4$0(#-$!0(+0,/+!$)"$#78 spline$5(./$)&3/&/-(3$!&6*(,0+'&"#
.!&6*(,0+'&"!$&08"$!+*,!$*-$!0(+0,/+!$)(5*,)!$)"$5&6/!
:!"#!$!*#+#$)(+0(,/$)(3&3/!$)("6()
x [cm] 0 10 20 30 0 10 20 30 0 10 20 30

y [cm] 0 0 0 0 15 15 15 15 30 30 30 30

T [ºC] 20 22 25 23 22 27 30 25 19 20 18 22
:*+-+1#
x=linspace(0,30,4);
y=linspace(0,30,3);
t=[20 22 25 23; 22 27 30 25; 19 20 18 22];
mesh(x,y,t)
title('Before Interpulation');
xx1=1:30;
yy1=xx1;
[xx,yy]=meshgrid(xx1,yy1);
tt=interp2(x,y,t,xx,yy,'spline');
mesh(xx,yy,tt)
title('After interpulation');
:2'#
After interpulation Before Interpulation

35 30

28
30
26

25 24

22
20
20

15 18
30 30
30 30
20 20
20 20
10 10
10 10
0 0 0 0
!+!"#$%!&'(!)'*"& U

!
!.'/012#.!)4!)"*51'6.!&6!(47 ! y % sin!3 # x " $ 4 # x 4 !3 .'/012#.!.12&1 -,
! =(!&26/2&.!&6!*/. !< &2124!.'/"*51'6!&25'4 !; !&"9:( ! 0 & x & 1 !3827&(
-'5')16.!>2"&#)!.224.2!format long

( n # cos!( n "
!5)0!.1&4%@!&)(0%. !.'/012#!(2&@ !! % 1 !!3 .6224%.!.12&1 -?
(n ' 5

!8')2A*. ! ( n !8'1246".!8'"#$%. !n !&6!"'97&!.'/012#. !-n !'(2'72!8)4!"#$%


!3 .6/2&.!)(0&& !n=2 !"2(: !36%*2A) !- U U .6224%.!&6!8'%''0%2!$#6%
- (1 % 2.6544 ; ( 2 % 4.6331
!
3 .)7&..!'61&2!&2')6'/1"#'A!&26224%!&@":%!.12&1 -;
d2x
$ y %1
dt 2
d2x d2y
! 2 $ 2 %0
dt dt
) )
y!t % 0 " % x !t % 0 " % y!t % 0 " % x !t % 0 " % 0
!&6!.*'/%2 !&2'14 !C!A: !B !827&(!&26224%.!&@":%!&6!&"&2#.!&'1@2&!(2&@
!-D"*(!x,y!"2(:!&212"&#.
!
!3 &2')6'/1"#'A!&26224%!)4!5$!>2&1!-E
x % !!!
y ' 2 # x!
!
y % y ' 2 # t 4 $ 50 # sin(2 # t )
!!
!&6!&"&2#2 !&26224%.!&@":%!)4!.)7&..!'61&!&6!&5)20.!.'/012#!(2&@
!>%9.!)4!.'/012#@ ! x! !"2(:!>2"&#.!&6 ! *'/.)!4' !-&2'14 !C !A: !B !827&(!&@":%.
!.%2A6!&'(@2@!&"9:(!D"*(!>''/)!4' !D$21( !- 8'1&4%.!)@) !B !.)7&.!'61&!"2(:
!-2.CFtF2.6!!"46@!)(0&%.! x! !&6
!
!'142 ! .5%2!.):%!:21)!&)*2$%. ! .$%%!&(@"2%.!&')60'9'#!&@":%!.12&1 !-C
!- 8''2#"!8'/'#0.!'14 ! y!t " % 0 !"46@ ! y!t " !'G :!>%2$%!.$%.!)4!820%. !-8'/'#0
-&21246".!&2'14.!?B=(!.$%.!820'%!&6!D"*(!*/.
!
m % 1 *kg +
y!0" % 0 ! k1
)
! y!t"
! y!0" % 0
*
k 1 % 10 kg / sec 2 + mg
k2 % 2 *kg / sec +
2
! k2
!
1!"#$%!&'()
U

:*+",&
3()/34, 0 & x & 1 : *&+),(!"#$%&'!(-.(-/01%"2!()2(,.+ y % sin !3 # x " $ 4 # x :!"#$%&'!(!%&)%
4

."1"-%2!(5&/)'-(!&&.!& format long -,()&2#&)!()2(0#! ,)&%&.(!"#/01%"2()&1".


:*+-+.#
format long
x=linspace(0,1,20);
sys1=trapz(x,sin(3.*x)+4.*(x.^4))
sys2=quad('sin(3.*x)+4.*(x.^4)',0,1)
sys3=quadl('sin(3.*x)+4.*(x.^4)',0,1)
:/'#
sys1 =

1.465644569112382

sys2 =

1.463330831271255

sys3 =

1.463330827658138
2!"#$%!&'()
U

:*+",&
( n # cos!( n "
.n (",&"+&(*-.(/'76(1-$(!%).68()-,$6!(!"#$%&'(,&)8 %1 : !2&&.6!(!%&)%
(n ' 5

(/&,4 :260&9- .!2&&.6!()2(*"6""$6&(7'26(*"-&90! ( n (*"%&.2/!(*"/'76! n()2(/"3+)(!"#$%&'!


U U

. ( 1 % 2.6544 ; ( 2 % 4.6331 :!2#&)!(-,$)) n=2


:&01.2+#
function ret=funfun(n)
%the function gets an input n which is telling how many solutions
%the user can get from the desired function.
%the function returns if the value is positive
x=linspace(0,n*5,n*150);
y='x.*cos(x)+(5-x)';
urn=[]; tmp=[-1]; ret=[];
for I=1:length(x)
zer=fzero(y,x(I));
tmp=[tmp,zer];
end
for I=2:length(tmp)
if tmp(I)>1.1*tmp(I-1)
urn=[urn, tmp(I)];
end
end
for I=1:n
ret=[ret,urn(I)];
end
:&1"&!*+(%3+-
value=funfun(2)

value =

2.6544 4.6331

value=funfun(3)

value =

2.6544 4.6331 7.5129

value=funfun(7)

value =

2.6544 4.6331 7.5129 11.6009 13.4576 18.0877 24.4822


3!"#$%!&'()
U

:*+",&
:5&)%
d2x
$ y %1
dt 2
d2x d2y
$ %0
dt 2 dt 2
) )
y!t % 0 " % x !t % 0 " % y!t % 0 " % x !t % 0 " % 0
: 5&/)'(:/9
* y1 + * x + * y!1 + * x! + * y2 +
,y - , !- , y! - , !! - ,1 ' y -
, 2- x- d x
Y% % , . Y %, -%, -%, 1-
2

, y3 - , y - dt , y!3 - , y! - , y4 -
, - , - , - , - , -
/ y4 0 / y! 0 y 0 / y1 ' 10
/ y! 4 0 / !!
:"4,!*01.2+#
function dxdy=mishdif(t,y)
dxdy=[y(2)
1-y(1)
y(4)
y(1)-1];
:&-+.#
tspan=[0 5]; %time interval
y0=[0;0;0;0]; %starting terms
[T,Y]=ode45('mishdif',tspan,y0);
plot(T,Y(:,1),T,Y(:,3))
legend('x(t)','y(t)');
xlabel('t [sec]');
ylabel('f(t)');
:/'#
2
x(t)
1.5 y(t)

0.5
f(t)

-0.5

-1

-1.5

-2
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
t [sec]
4!"#$%!&'()
U

:*+",&
:5&)%
x % !!!
y ' 2 # x!
y % y ' 2 # t 4 $ 50 # sin(2 # t )
!!
:5&/)'(:/9
*1 +
* y!1 + * x! + , ! y3 ' 8t $ 100 cos(2t ) ' y1 " -
3
* y1 + * x +
,y - , - , y! - , ! - , 2 -
y d y
Y % , - % , - . Y % , - % , - % , y3
2 2
-
, y3 - , y! - dt , y!3 - , !!
y- , -
, - , - , - , - , 4 y
y0 y0 -
/ y4 0 / !! / y! 4 0 /!!! ,/ y3 ' 8t 3 $ 100 cos(2t ) -0
:!-+)!("2%)
) ))
x !t % 0" % y !t % 0" % y !t % 0" % y !t % 0" % 0
:"4,!*01.2+#
function dx=mishdif1(t,y)
dx=[0.5*(y(3)-8*(t^3)+100*cos(2*t)-y(1));
y(3);
y(4);
y(3)-8*(t^3)+100*cos(2*t)];
:*0)("!&01.2+#
function mainfunc(y)
tspan=[0 5];
[T,Y]=ode45('mishdif1',tspan,y);
t_int=find(T<2.6 & T>2.5);
plot(T,0.5.*(Y(:,3)-8.*(T.^3)+100*cos(2.*T)-Y(:,1)) ,
T(t_int),0.5*(Y(t_int,3)-8*(T(t_int)^3)+100*cos(2*T(t_int))-Y(t_int,1)),'r*-
')
xlabel('t [sec]');
ylabel('dx/dt');
:/'#
mainfunc([0 0 0 0])

100

-100

-200
dx/dt

-300

-400

-500

-600

-700
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
t [sec]
5!"#$%!&'()
U

:*+",&
:5&)%
m % 1 1kg 2
y!0" % 0
)
y!0" % 0
1
k 1 % 10 kg / sec 2 2
k2 % 2 1kg / sec 2
2

:)&+&8(5326()&2&&.66
3 F % mg ' ! k1 $ k2 " y % my!!
y % 9.8 ' 12 y
!!
:5&/)'(:/9
* y + * y+ d * y! + * y! + * y +
Y %, 1-%, -. Y %, 1-%, -%, 2
/ y2 0 / y! 0 dt y 0 /9.8 ' 12 y1 -0
/ y! 2 0 / !!
:&01.2+#
function dy=mishdif2(t,y)
dy=[y(2); 9.8-12*y(1)];
:&-+.#
tspan=[0 20];
y0=[0;0];
[T,Y]=ode45('mishdif2',tspan,y0);
plot(T,9.8-12.*Y(:,1))
xlabel('t [sec]'); ylabel('y(t)');
:/'#

10

2
y(t)

-2

-4

-6

-8

-10
0 2 4 6 8 10 12 14 16 18 20
t [sec]
!-$41/9&!%(#)!-$92$-3!79/1 !
0&D1(-!?4.!>5;1)1!8<)7!"#$%
!
!0-$./!+,!*"#$%&!'()
-4%&!45$84(%!(5/9&!&:3!&)$/!;)5$6%
!0 !word!')1)!'$-%!%$-23!(4!"$5-6&!-7
!0.#)9!?%$.&!42!&98;!3%.--!73 !>"#$%&!'()%!=4)86!561) !save!8<%3!4$<5
!5$)(3!(5/9!/#7!32!&5.)!32% !>"#$%&!'3&)% !$9-944!&(@&&!"6$7!4%@3!-$75$&
!03(!"$%(#%!-46$1&!&7<$-&!-7
$
!0=45%1&$!-$58&%!-$%5&3!(4!MATLAB!A&!-$/$.6!/%3)
!HighLearn!A&!-258)3 !>49$5;.37!57$/3 !>;95;9473!"#$%&!'3&)%!5%#-&3!5$17
!0=49$(!Cusers!=4()-()!"4%!5(.!-5(67)&!-5#7!&%4%1!323!$7
B
/%3%!$3(!"$%(#3!1924&3!(5/9!;9/$;1!32
!0
!049$5;.37!;)5$6%!$7!16/$)!>%$-2!5:8!5)$#!32!5-$)
!0MATLABA&!-92$-!-7!3486&3
! !C&%$#B!5-$)
!03D7$/$!C=(!733B!:-!=$(53!(4!"$5-6&!?%$.!-34#-%
D
!C:D- EIDB!mexm_ID !7%&!;)5$6&!463!=(!"$5-6&!?%$.3!--3!(4
!
!0 y1 # f1 ! $ " , y 2 # f 2 ! $ " !&5$<&)!C-$4<.9$6B !=49$(!=44;)-)!=44$;4%!49(!5#%!CF (30%)
!&.:# !>.$34# !>362 !>5$14# !>5$%4#B !"$%(#&!-$3$86!32!-7 ! 3$323!'45<!4$;4%!32
!-;;5()& !-492$-!%$-2$ !> $ ! &9-()&!5$%8!=$#-!5#% !0C -45;)$9$@45;!&4<.9$6$
!05#%9(! $ ! A&!=$#-%!-$4<.9$6&!4-(!-7!-#7!=454<!-258)!38
!-7!-33$2&B!-5-$2$!>75.)!>!y!A$!x!=454<3!-$4$ $-!=465@3!G41$&3!&245<!-492$-&
!0C=44;)-)&!=44$;4%&
!0CTeX !49)41%!($)4(!4D8B!-5-$2%!846$&3!'45<! $ !")41&!*&58&
!
!0-$/$)8!n!A$!-$5$(!n!38%!4/))!$/!'58)!$94&!A!42!"$-9!!CH (20%)
!H !A$ !F ! =44$;4%&!"4% !CMATLAB !A%B !3/%&&!-7!-$)47-)!-$7)@$/!4$$43%!5%1&
!*J!A$!I!=44$;4%&!"4%!3/%&&!-7$
1. A^2
2. A.^2
3. log(A)
4. log10(A)
!C &.:#&!")41!4963!&/$.9!(4!H !561)!4$;4%%(!'23!%3!$)4(B
!
!49%3)!#;(!496!38!-$1)!3(!5$:46!=457-)&!=49$-9!-;3$.&!&4<.9$6!%$-2 (50%)
!*7%&!"6$7%
y !
yn m n1 mn 2 m nm
!
!
y2 m 21 m 22 m 2m
!
y1 m11 m12 m1m !
x1 x2 xm !
!
x
o !
!*5(72!y!A$!x!=45$;.$$!49($!M!&<45;)!*=&!&4<.9$6&!3(!;3.&!49$-9
' m n1 ! m nm (
x # % x1 ! x m &
! M # )) " # " ** ,
y # % y1 ! y n &
)+ m11 ! m1m *,

!1#4%!-$1)&!-258)!3(!&4<5947&!4;9)$)!-7!%(#3!7$& !& 4<.9$6&!/4.6-


!0y!A$!x!=454<3
!*=47%&!=43/@&!-7!%(#3!(4
n m
0x !54<3!1#4%!&4<5947!;9)$)!A! I xx # -- ! y j " m ji !!!!!!!!!!!!!
2

j i

n m
!0y !54<3!1#4%!&4<5947!;9)$)!A! I yy # -- ! x i "2 m ji !!!!!!!!!!!!!
j i

n m
!0&4<5947!-362)!A! I xy # -- x i y jm ji !!!!!!!!!!!!!!
j i

n m
!0o !&/$.93!1#4%!4%;$.!&4<5947!;9)$)!A! Io # -- .1 ! x i "2 0 ! y j " /2 m ji
2

j i 3 4

!5$#%3!(4B !-%-2(!&4<.9$6&!-3$86!-7!-$)4@/)&!-$4561)!-$7)@$/!4-(!@<&
!0C=5$%8!$3%.-&(!-$7<$-&!-7!@4<&3$!;3.!49$-9
!C =44;9;1419$.B !=44%.8!=947!;3.&!49$-9!3(!=4/))&!$%!&5.)% !*&58&
!0error!&/$.6%!($)4(!4D8!&)47-)!&8/$&!@4<&3!&245<!&4<.9$6&
!
!0&#3<&% !!!!!!!!!
!"#$%&'()*+,(!"&-"!.(/&%$
.'"$*! 5#6 ,01$,$(23,/(4+") U

1!"#$%!&'()
U

:*+,+-#
teta=linspace(1,3*pi,200);
y1=3+(4./teta-teta.^2).*sin(teta);
y2=3+(4./teta-teta.^2).*cos(teta);
plot(teta,y1,teta,y2);
xlabel('\theta');
ylabel('f(\theta)');
legend('f_1(\theta)','f_2(\theta)');
title('f_1(\theta)=3+(4/\theta-\theta^2)*sin(\theta)
f_2(\theta)=3+(4/\theta-\theta^2)*cos(\theta)');
:.'#
2 2
f1(!)=3+(4/ !-! )*sin(!) f2(!)=3+(4/ !-! )*cos(!)
100

80 f1(!)

f2(!)
60

40

20
f(!)

-20

-40

-60

-80
1 2 3 4 5 6 7 8 9 10
!
2!"#$%!&'()
U

: /,7"%. , !#0/#&#.('0)7./)(!"3#01,(.8-(.*(9#..-'(#8.(',32)(!.8-", A('3#01,' A ^ 2 (4"*/0'(#"1#))

$a b %
A)'
$ 2
$a b % $a b % ' a & bc " # " ab & bd #%(
( * A ) A * A ) 'c d ( * 'c d ( ) '
2

+c d , + , + , '" ca & dc #
+ " cb & d # (,(
2

MATLAB -'(:"!,(/,7"%

>> A=[1 2; 3 4]

A=

1 2

3 4

>> A^2

ans =

7 10

15 22

. :02,)(0)#/(.-(.2 (!#0.6$)(!23"),('6;+'(!."28(';('06,) , :02,(.2('."28)(0)"%,(#&*'(#"1#))

$a b % $" a * a # " b * b #%
A)' * ) '
" d * d #(,
( A . ^ 2
+c d , +"c * c#

MATLAB -'(:"!,(/,7"%

>> A=[1 2; 3 4]

A=

1 2

3 4

>> A.^2
ans =

1 4

9 16

(48"/)(!23"),(/#'"(9#-02,(4#)('."28)(0)"%, , !#1,!,'('."28'(#&8.('%"6&('2#8",(0*/-(%#,! :'02'


.9#-02,'(#&*) ( 9#';(9#$6%&#/(#.2)(9#0)#/) (9#,#/!,(9#0)/(4#)(#0.6$

(! ."28)(0)"%,(#2#)0'(#"1#))(".#/"(:02,)(0)#/(.-(.2 ( #2)1($#$)(#8.(7".) (4.(!."28)(0)"%,(#*#.*'(#"1#))


:02,)(0)#/(.-(.2(0*2($#$)(#8.(7".

>> A=[10 1; 100 1000]

A=

10 1

100 1000

>> log(A)

ans =

2.3026 0

4.6052 6.9078

>> log10(A)

ans =

1 0

2 3
3!"#$%!&'()
U

:&/0-1+#
function [Ixx,Iyy,Ixy,Io]=Inertia(M,x,y)

if nargin<3
error ('not sufficient data');
end
[n,m]=size(M);
if or (length(x)~=m, length(y)~=n)
error('Size of vectors does not match size of Matrix');
end
Ixx=0; Iyy=0; Ixy=0; Io=0;
for J=1:n
for I=1:m
Ixx=Ixx+y(J)^2*M(J,I);
Iyy=Iyy+x(I)^2*M(J,I);
Ixy=Ixy+x(I)*y(J)*M(J,I);
Io=Io+(x(I)^2+y(J)^2)*M(J,I);
end
end

:&0"&!*+(%2+,
>> M=[1 2 3; 4 5 6];
>> x=[6 7 8]
>> y=[2 3]
>> [Ixx,Iyy,Ixy,Io]=Inertia(M,x,y)

Ixx =

159

Iyy =

1099

Ixy =

409

Io =

1258
!-$41/9&!%(#)!-$92$-3!79/1 !
0$D1(-!G5$#!>5;1)1!8<)7!"#$%
!
!0-$./!+,!*"#$%&!'()
-4%&!45$84(%!(5/9&!&:3!&)$/!;)5$6%
!0 !word!')1)!'$-%!%$-23!(4!"$5-6&!-7
!0.#)9!?%$.&!42!&98;!3%.--!73 !>"#$%&!'()%!=4)86!561) !save!8<%3!4$<5
!5$)(3!(5/9!/#7!32!&5.)!32% !>"#$%&!'3&)% !$9-944!&(@&&!"6$7!4%@3!-$75$&
!03(!"$%(#%!-46$1&!&7<$-&!-7
$
!0=45%1&$!-$58&%!-$%5&3!(4!MATLAB!A&!-$/$.6!/%3)
!HighLearn!A&!-258)3 !>49$5;.37!57$/3 !>;95;9473!"#$%&!'3&)%!5%#-&3!5$17
!0=49$(!Cusers!=4()-()!"4%!5(.!-5(67)&!-5#7!&%4%1!323!$7
B
/%3%!$3(!"$%(#3!1924&3!(5/9!;9/$;1!32
!0
!049$5;.37!;)5$6%!$7!16/$)!>%$-2!5:8!5)$#!32!5-$)
!0MATLABA&!-92$-!-7!3486&3
! !C&%$#B!5-$)
!03D7$/$!C=(!733B!:-!=$(53!(4!"$5-6&!?%$.!-34#-%
D
!C:D- EIDB!mexm_ID
!7%&!;)5$6&!463!=(!"$5-6&!?%$.3!--3!(4
!
!!CF (25%)
!* 7%&!4;)-)&!4$;4%&!"$-9
#
! y $ ! % sin x "! & cos x "
!0 # !A$! & !> % !=49-()3!C /#7)!$7! 167)!=49$(B !=445)$9!=4258!'9$<52!5#%
!=4258&!=8 !&9$-9& !&4<.9$6&!-7!57-)&!4/))!$/!G5@ !-;;5( )&!-492$-!%$-2
!0 0 ' x ' 10 !5(72!-5#%(
!*&7%&!&5$<&)!-5-$2$!y!A$!x!=454<3!-$4$$-!G5@3!G41$&3!&245<!-492$-&
#
! y $ ! % sin x "! & cos x " , % $ xx , & $ xx , # $ xx .
! & !> % !=49-()3!-5#%(!=445)$9&!=4258&!=8!G43#&3!(4 ! xx !=49)41&!-7!5(72
!0 # !A$
!0TeX!49)41%!()-(&3!(4!-5-$2%!! # !A$! & !> % !=49)41&!-7!@4<&3!4/2!*&58&
!
!!!CH (15%)
!0=47-!'58)!$&)!C-@<)&)!73!5)$32!>-$45$.)B !-$)47-)!-$7)@$/!4$$43%!5%1&
!
!
!!!CI
!* =46481!49()!-%25$)!$:!&37(
!
!* =47%&!=45;)56&!-7!-;3$.&!&4<.9$6!%$-2!07 (40%)
!0 =3(!4())!561)!J!n
!0=3(!4())!561)!J!m
!04())!561)!J!dx
!0x !3(!&4<.9$6!57-)&!-:$5#)!@$1)!&9-()!J!f(x)
!
!*&7%&!&5$<&)!-49%3)!&<45;)!-$9%3!&245<!&4<.9$6&
( f !0" f ! dx " f ! 2dx " f ! 3dx " ! f ! ndx " )
* +
* f ! 0 " , 1 f ! dx " , 1 f ! 2dx " , 1 f ! 3dx " , 1 ! f ! ndx " , 1 +
* f ! 0 " , 2 f ! dx " , 2 f ! 2dx " , 2 f ! 3dx " , 2 ! f ! ndx " , 2 +
!* +
* f ! 0 " , 3 f ! dx " , 3 f ! 2dx " , 3 f ! 3dx " , 3 ! f ! ndx " , 3 +
* " " " " # " +
* +
*-f ! 0 " , m f ! dx " , m f ! 2dx " , m f ! 3dx " , m ! f ! ndx " , m +.

!
!0-#7!CWHILE!$7!FORB !-73$3)!5-$4%!()-(&3!"47!&4<.9$6&!-%4-2%
! 0'-54#%3!;3.&!49$-9!57($! n $ m $ 5 !5(72!7)@$/!@<&
!
!&<45;)&B ! f ! x " !&4<.9$63!&)47-)& !K 7!G481 !3(!-$(45/&!463!&<45;)!&9$-9 !0% (20%)

!C&8$/4!&947!& <45;)&!-449%3!&()4((! f ! x " !&4<.9$6&!>45)$9!"6$7%!&9$-9


!&4<.9$6&!-7!57-)&!4/))!$/ !G5@!;;5(- !C /%3%!&<45;)&!-84/4!141%!38B!/<42
!0 c 0 m !> b 0 n !> a / 0 !> a ! dx " ' x ' b ! dx " !5(72! f ! x " , c
!5<443!(4 !7)@$/% !0 '-54#%3!;3.&!49$-9!57($ ! n $ m $ 50 !5(72!7)@$/ !@<&
!-7 !@4<&3 !73B !G5@&!-7!@4<&3$ !C $5#%9(!=49$-9&!463B !&)47-)!&<45;)
!0C&<45;)&
!"#$%&'()*+,(!"&-"!.(/&%$
.""$*! 50"+ ,01$,$(23,/(4+")
U

1!"#$%!&'()
U

:*+,+-#
alpha=6; beta=6; delta=3;
x=linspace(0,10,200);
y=(alpha*sin(x)).*(beta*cos(x)).^delta;
yy='(\alphasin(x))(\betacos(x))^\delta ,';
alp=(['\alpha=',num2str(alpha),' ,']);
bet=(['\beta=',num2str(beta),' ,']);
del=(['\delta=',num2str(delta)]);
plot(x,y);
grid on;
title([yy,alp,bet,del]);
xlabel('x');
ylabel('y');
:.'#
#
(! sin(x))("cos(x)) ,! =6 , "=6 , #=3
500

400

300

200

100

0
y

-100

-200

-300

-400

-500
0 1 2 3 4 5 6 7 8 9 10
x
2!"#$%!&'()
U

(!"#'.(6#-#03(6&#/(6#&"*'(6#/!)(6#&$+"/,'(6#&"!&'(0*/- ,#&).,(6#&"!&(702,("&#'(6#/!(702,
(.#-'.(6#."-#(6#0+/(6#/! ,!"9"0+,(.#-'.(6#."-#(6#,#"$,(6#/! .('9(%,,(#.2)(/.(5/")(''9(8"$,
.!"3#01,("/(6#0.:$

:(%/+,
A{1,1}='Matlab';
A{1,2}=[1 2 3; 4 5 6];
A{2,1}=1+2i;
A{2,2}='Mechanical Engeeniring';
>> A

A=

'Matlab' [2x3 double]


[1.0000 + 2.0000i] [1x22 char ]

3!"#$%!&'()
U

./
:&01-2+#
function M=BuildMat(n,m,dx,fx)
x=linspace(0,n*dx,n+1);
fdx=eval(fx);
for I=0:m
M(I+1,:)=fdx+I;
end
:&1"&!*%/+,
a=BuildMat(5,5,2,'2*x.^3')

a=

0 16 128 432 1024 2000


1 17 129 433 1025 2001
2 18 130 434 1026 2002
3 19 131 435 1027 2003
4 20 132 436 1028 2004
5 21 133 437 1029 2005
.)
:.'-
>> m=50; n=50; dx=0.5; f='2.*x.^3';
>> A= BuildMat (m,n,dx,f);
>> a=1; b=15; c=20;
>> y=A(c+1,[a:b]+1);
>> x=linspace(a*dx,b*dx,length(y));
>> plot(x,y)
:.'#

900

800

700

600

500

400

300

200

100

0
0 1 2 3 4 5 6 7 8
!-$41/9&!%(#)!-$92$-3!79/1
0G,,H!%4%7!5;1)1!8<)7!"#$%
!
!0-$./!+,!*"#$%&!'()
-4%&!45$84(%!(5/9&!&:3!&)$/!;)5$6%
!0 !word!')1)!'$-%!%$-23!(4!"$5-6&!-7
!0.#)9!?%$.&!42!&98;!3%.--!73 !>"#$%&!'()%!=4)86!561) !save!8<%3!4$<5
!5$)(3!(5/9!/#7!32!&5.)!32% !>"#$%&!'3&)% !$9-944!&(@&&!"6$7!4%@3!-$75$&
!0$3(!"$%(#%!-46$1&!&7<$-&!-7
!0=45%1&$!-$58&%!-$%5&3!(4!MATLAB!A&!-$/$.6!/%3)
!HighLearn!A&!-258)3 !>49$5;.37!57$/3 !>;95;9473!"#$%&!'3&)%!5%#-&3!5$17
!0=49$(!Cusers!=4()-()!"4%!5(.!-5(67)&!-5#7!&%4%
B 1!323!$7
/%3%!$3(!"$%(#3!1924&3!(5/9!;9/$;1!32
!0
!049$5;.37!;)5$6%!$7!16/$)!>%$-2!5:8!5)$#!32!5-$)
!0MATLABA&!-92$-!-7!3486&3
! !C4$<5B!5-$)
!
#cos ! t " % sin ! t " $
! A!t" & ' ( !!!!!!!!!!*&7%&!&<45;)&!&9$-9!CD (50%)
) sin ! t " cos ! t " *

#v $
! v & ' 1 ( !*&5$<&)!C4/))!$/B!5$;.$$!-;3$.&!&4<.9$6!%$-2
)v 2*

!0subplot! A%!($)4(!4E 8!=465@!49(!;;5(3!&245<!&4<.9$6&


!0 4, !/8!0!A)!&9-()!t!5(72! +1 ! t " !@<$4!"$(75&!F5@%
!0 4, !/8!0!A)!&9-()!t!5(72! + 2 ! t " !@<$4!49(&!F5@%
# +1 ! t " $ # v1 $
!' ( & A ! t " ' ( !!!!!* 7%&!"6$7%!=45/@$)! + 2 ! t " !A$! +1 ! t "
)+ 2 ! t "* ) v2 *
!* =49)41&!846$&3!=4245< !y !54<!3(!-$4$$-% !> =454<3!-$4$$- !=465@3!F41$&3!(4
!0CTeX !49)41%!($)4(!4E8B! + 2 ! t " !A$! +1 ! t "
!;36!@4<&3!(4B !'-54#%!463!=49$(!;3.!4 9$-9!5$%8!&4<. 9$6&!3(!&<5&!-)@$/!@<&
!0C465@
# +1 ! t " $
!C A ! t " B !&<45;)&!"4%!362&!-3$86!-7!8<%3!(4 !> -492$-&!'3&)% ! ' ( !%$(4#3
) + 2 ! t " *
!0%3;)%!CvB!5$;.$$&$
U U

!0 45)$9!;36!733!> /%3%!465@!;36!(4!&4<.9$63
!
!!CG (15%)
!=45%47&!32( ! n - n !3/$@% !A !&<45;)!&54:#)$ !n !5;)56!-;3$.&!&4<.9$6!%$-2
!0-$)@$/!@<&!0n!561)&!=&!&%
!0-$73$3%!()-(&3!5$17!$:!-492$-%

!
!*5(72! 2 - 2 !3/$@%!=47-!'58)!-;3$.&!&4<.9$6!%$-2!CI (35%)
0C! n / 2 B!!!> 1 - n !3/$@%!5$;.$$!(4!K1,1J!!7-% .
!! m - ! n % 2 " !3/$@%!&<45;)!(4!K1,2J!!7-% .
!0 ! m 0 1" - ! 2 " !3/$@%!&<45;)!(4!K2,1J!!7-% .
1 - ! n % 2 " !5$;.$$!(4!K2,2J!!7-% .

!0(75)!=48$/4!=947!m!A$!n!=4/)4)&
!'58)!3(!=47-&!-8%57%!=47<)9&B!=49$-9&!-8%57)!%425&3!&245<!&4<.9$6&
!*7)@$/3!>'1 )&!38!$@4<&3$!49%3)!=4561)!'58)!C=47-&
!
=47-!'58)
!&<45;) !
! 11 2 22 #5$
'4( !
#1 2 2$ ) *
'5 3 5 (( !
!' #3 5$
'4 6 2( !
' (
'6 2 ( 182
)8 3 7* ' ( !
') 3 7 (*
!
!0&<5&!-$7)@$/!4-(!@<&
!
!
!
!&#3<&% !!!!!!!!
!"#$%&'()*+,(!"&-"!.(/&%$
.2006 )#)/(01$,$(23,/(4+") U

1!"#$%!&'() U

:&*+,-.#
function matgraph(v)

t=linspace(0,4*pi,200);
for I=1:length(t)
A=[cos(t(I)) -sin(t(I)); sin(t(I)) cos(t(I))];
alpha1(I)=A(1,1)*v(1)+A(1,2)*v(2);
alpha2(I)=A(2,1)*v(1)+A(2,2)*v(2);
end
subplot(1,2,1)
plot(t,alpha1);
grid on
xlabel('t');
ylabel('\alpha_1(t)');
axis tight;
subplot(1,2,2)
plot(t,alpha2);
grid on
xlabel('t');
ylabel('\alpha_2(t)');
axis tight;
:&+"&!/%0.1
>> matgraph([0 1])

0.8 0.8

0.6 0.6

0.4 0.4

0.2 0.2
! 1(t)

! 2(t)

0 0

-0.2 -0.2

-0.4 -0.4

-0.6 -0.6

-0.8 -0.8

0 5 10 0 5 10
t t
2!"#$%!&'() U

:&*+,-.#
function A=Nmatrix(n)

A=ones(n)*n;
:&+"&!/%0.1
>> Nmatrix(5)

ans =

5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5

3!"#$%!&'() U

:&*+,-.#
function B=matArrange(A)

b1=A{1,1};
b2=A{1,2};
b3=A{2,1};
b4=A{2,2};
B=[b1;[b2;b4], b3];
:&+"&!/.(%0.1
>> A{1,1}=[1 2 2];
>> A{1,2}=[5;4];
>> A{2,1}=[3 5; 6 2; 3 7];
>> A{2,2}=[8];

matArrange(A)

ans =

1 2 2
5 3 5
4 6 2
8 3 7

>> A{1,1}=[1 2 3 4];


>> A{1,2}=[6 6; 6 6];
>> A{2,1}=[9 8; 7 6; 5 4];
>> A{2,2}=[5 5];
matArrange(A)

ans =

1 2 3 4
6 6 9 8
6 6 7 6
5 5 5 4
!-$41/9&!%(#)!-$92$-3!79/1
C$M1(-!?4.B0D!561)!&<$%.!>5;1)1!8<)7!"#$%
!
!0-$./!+,!*"#$%&!'()
-4%&!45$84(%!(5/9&!&:3!&)$/!;)5$6%
!0 !word!')1)!'$-%!%$-23!(4!"$5-6&!-7
!0.#)9!?%$.&!42!&98;!3%.--!73 !>"#$%&!'()%!=4)86!561) !save!8<%3!4$<5
!5$)(3!(5/9!/#7!32!&5.)!32% !>"#$%&!'3&)% !$9-944!&(@&&!"6$7!4%@3!-$75$&
!0$3(!"$%(#%!-46$1&!&7<$-&!-7
!0=45%1&$!-$58&%!-$%5&3!(4!MATLAB!A&!-$/$.6!/%3)
!HighLearn!A&!-258)3 !>49$5;.37!57$/3 !>;95;9473!"#$%&!'3&)%!5%#-&3!5$17
!0=49$(!Cusers!=4()-()!"4%!5(.!-5(67)&!-5#7!&%4%1!323!$7
B
/%3%!$3(!"$%(#3!1924&3!(5/9!;9/$;1!32
!0
!0/%3%!-$7<5&!*5-$)!5:8!5)$# U

!0-$37(&!32%!;36&!-7!@4<&3$!MATLAB!A&!-92$-!-7!3486&3!&%$#
!0"258%!-$$(!-$37(&!32
!
cosh 5 %sin#ln! x " ' log 2 !2 x "$&
!5(72 !> f ( x) ( * sin( x) !* &7%&!&4<.9$6!&9$-9 !0D
10
!05#%9(!x !=$#-%!4$;4%&!3(!E5@!-;;5()&!-492$-!%$-23!($5/!0 10 O ) x ) 150 O
!0:0 -$!=(&!-7!-33$2&!-5-$2$ !y!A$ !x !=454<3!-$4$$-!E5@3!E41$&3!&245<!-492$-&
!0-4F;9/$;1&!3(
!
!H =425/!4-(%!&3$86&!-7!8<%3!"-49!=7&!0-$7%&!-$7$$()&!-7!5$-63!(4!0G
w' x' y' z ( 4
3 x * 2 w ' 2 z * y ( *2
!
2z * w ( 0
z * w * 2 y * 4x ( 9
!
!E5@!;;5(3$ !D !/8 !, !=$#-%!=4475.7!=4561) !D,, !3(!&5/41!-$9%3!(4 !0I
!>,0GK!/8 !, !=$#-%!=47<)9!&<$%.&!'$-)!=4561)!&)2!@4<)& !Chist !J !&)5@$;14&B
!=4561)!&)2$ !,0LK !/8 !,0K !=$#-%!=47<)9!&)2 !>,0K !/8 !,0GK !=$#-%!=47<)9!&)2
!0MHistogramM !&4&-!&)5@$;1 4&&!-5-$2 !0D !/8 !,0LK !=$#-%!=47<)9!&<$%.&!'$-)
!0 =49-()2!34@5!"6$7%!&37!=4561)!@4<-!-492$-&!"2!$)2
!
!C =3(!561)B !integer !@$1)!/#7!;3.!"$-9!-3%.)!5(7!&4<.9$6!4F%$-2 !07 !0N
!-$/$)8 ! n ' 1 ! A$!-$5$( ! n ' 1 !38%!'58)!-$9%3!&245<!&4<.9$6& !>n !-$7%!")$1)&
!*&7%&!&5$<&)
+n ! 3 2 1 0 ,
- " # # # # 1.
- .
-3 2 1 0 # 2.
!A ( - .
-2 1 0 1 # 3.
-1 0 1 2 # " .
- .
/0 1 2 3 ! n 0
!
!&245<!&4<.9$6&!=3(!561)!$947(!"$-9!=8!&4<.9$63!75.!()-()&!=7 !*-$58&
!733!&4<.9$63!&745.!&8<$%!=7 !0 '58)&!-7!-$9%3$!&38)!463 2!561)&!-7!3@83
!0'58)&!-449%!'5$<3!M 3/#)!-545%!'58M! A%!5#%-!&4<.9$6&!:7!;3.!"$-9
!
!5%47!32!-36$2!>=4$1)!n !'58!5$%8 !O 7!E481)!&4<.9$63!-75$.&!-492$-!4F%$-2!0%
!0 3%.-)&!'58)%!5-$4%!3$/@&!5%47&!-7!-%(#)$!475.7!561)%!A!'58)%
!0 &9$(!475.7!561)%!'58)%!5%47!32!3462&3!(4!*&58&
!
!
!&#3<&% !!!!!!!!!
!
!
!
!
!
!
!
!
!
!
!
!
!
!"#$%&'()*+,(!"&-"!.(/&%$
(""$*!(7#6).1(05$,('3")6 ,01$,$(23,/(4+")
U

1!"#$%!&'()
U

:*+,+-#
x=linspace(10*pi/180,150*pi/180, 100);
y=((cosh(sin(log(x)+log2(2.*x)))).^5).*sin(x)/10;
plot(x,y);
title(['Tal Tsory ','ID: 9999999']);
xlabel('x');
ylabel('y(x)');
grid on;
:.'#
Tal Tsory ID: 9999999
0.9

0.8

0.7

0.6

0.5
y(x)

0.4

0.3

0.2

0.1

0
0 0.5 1 1.5 2 2.5 3
x
2!"#$%!&'()
U

:*+,+-#
A=[1 1 1 1; 3 -1 -2 2; 0 0 -1 2; -4 -2 -1 1];
b=[4; -2; 0; 9];
X=inv(A)*b;
XX=A\b;
x=X(1); y=X(2); w=X(3); z=X(4);
:.'#
X= XX =

-0.3182 -0.3182
-5.5000 -5.5000
6.5455 6.5455
3.2727 3.2727

3!"#$%!&'()
U

:*+,+-#
x=rand(1,100);
c1=0; c2=0; c3=0; c4=0;
for I=1:100
if x(I)<0.25
c1=c1+1;
end
if and (x(I)>=0.25, x(I)<0.5)
c2=c2+1;
end
if and (x(I)>=0.5, x(I)<0.75)
c3=c3+1;
end
if x(I)>=0.75
c4=c4+1;
end
end
hist(x,4);
title('Histogram');
:.'#
c1 =

14

c2 =

28

c3 =

24

c4 =

34
Histogram
35

30

25

20

15

10

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
4!"#$%!&'()
U

./
:&/0-1+#
function A=mat_fun(n)
if nargin<1
default=5;
n=default;
end
n=ceil(n);
A=zeros(n+1);
for I=1:(n+1)
for J=1:(n+1)
if I~=J
A(I,J)=abs(I-J);
end
end
end
A=flipud(A);
:&0"&!*+(%2+,
b=mat_fun(3)

b=

3 2 1 0
2 1 0 1
1 0 1 2
0 1 2 3

b= mat_fun (3.6)

b=

4 3 2 1 0
3 2 1 0 1
2 1 0 1 2
1 0 1 2 3
0 1 2 3 4

b= mat_fun

b=

5 4 3 2 1 0
4 3 2 1 0 1
3 2 1 0 1 2
2 1 0 1 2 3
1 0 1 2 3 4
0 1 2 3 4 5
.)
:*+,+-#
>> n=5;
>> a=rand(n+1);
>> b= mat_fun(n);
>> c=a.*b;
>> maxi=max(max(c));
:.'#
a= b=

0.6035 0.4044 0.0627 0.2203 0.5513 5 4 3 2 1 0


0.3608 4 3 2 1 0 1
0.1810 0.7257 0.3371 0.3786 0.3215 3 2 1 0 1 2
0.9710 2 1 0 1 2 3
0.8088 0.1540 0.4459 0.4228 0.8998 1 0 1 2 3 4
0.5555 0 1 2 3 4 5
0.8191 0.5091 0.7576 0.9727 0.5421
0.7698
0.4872 0.6680 0.1305 0.1770 0.4629
0.6801
0.7551 0.1526 0.9015 0.8823 0.8080
0.2052

c= maxi =

3.0177 1.6175 0.1881 0.4407 0.5513 3.2319


0
0.7240 2.1770 0.6742 0.3786 0
0.9710
2.4264 0.3080 0.4459 0 0.8998
1.1110
1.6381 0.5091 0 0.9727 1.0842
2.3095
0.4872 0 0.1305 0.3540 1.3887
2.7205
0 0.1526 1.8030 2.6468 3.2319
1.0260
1 *+,)! + &'()&
MATLAB -* '-#%

Matlab !"#$%

+**. $"+%
.!-$1 +3-. *1 !-1+,2 3 -%++0!+ ."&/ •
:!-)10!& +3)2 •
+0/#-4# &%+" –
bykhov@bgu.ac.il –
086428077/86 –

Matlab !"#$%
Matlab /)-0& +'1-5
&"-#$ !#+#/ •
!-(+)3%- 6+)2/% *$ !-*-$2 •
6+(#0 •
debug •
6+2), •
!-+(05-2 •

Matlab !"#$%

MATLAB !"#!$

MATLAB 7.0.1.lnk *$ 0+*0 Matlab !$5!& •


DESKTOP •

Matlab !"#$%
(%&'$)MATLAB !"#!$

:MATLAB Command Window – !-"-02 7-*4 •


Desktop -> Desktop Layout -> Command Window Only

()*+!
/)01 02*3
!,-,.+ !*,&

Matlab !"#$%

Ҝ҇҅҇ҙҖґ҇Ҏ҉

6+*. *,)/ •

Preferences •

Matlab !"#$%
(GLWRUґ҇Ҏ҉

/ &5.-!&_61 <- new/open <- File -' edit !"-02 •


Editor/Debugger 7-*4 !$2-&* &'+#% &+(05-2&_61
.&5.-!& 3/03 6$ •
save 7! 3/03 !)+%1* •

Matlab !"#$%

ҌқҐ҆ (GLWRUґ҇Ҏ҉

Editor •

0& &,+)4
(3.(
*+3'
$*,&

Matlab !"#$%
Path Browser ґ҇Ҏ҉
Set Path... <- File -' pathtool !"-02 •
!-+)-30)+" !-51*- !-')* !)12'%

ҜҖҋғ҇҆
ҏҋҚ҅Ҏ҇Җ

Ҏ҇ҖҍҙҋҎҙ
җ҃҇ҙҜ҉ҋҜҖҎ
Matlab !"#$%

:RUNVSDFHґ҇Ҏ҉

!10& -' Workspace <- View -' workspace !"-02 •


Desktop *$ Workspace
Workspace 7-*4 t=0:pi/4:2*pi;y=sin(t); 6+5!1%* •
:&')5

"(&$- .'
!"#!
#$%&
'("'%#
)*+,

)*+,/ #0 '$1$,+ *%2,$ '$*$0 *%2,


Matlab !"#$%
'HPRVғ҇Ґ҅
DESKTOP TOOLS <- MATLAB <-Demos <- Help •
Run This Demo <- •

Matlab !"#$%

/))*(#'05 /)6,&)4

3"*"0" 3""*,$- 3"4$0"( '*0%., /1$4+/ '4"42


: ENTER '0!/ 3+ 1", '#4!', /.5$'/

» 12.3*(11.34+9.88)/3.3
ans =
79.0927
» 1+sin(pi/6)/(2+cos(pi/12))
ans =
1.1686
» sqrt(1+tan(pi/12)/(1+sin(pi/2)))
ans =
1.0649

Matlab !"#$%
(%&'$) /))*(#'05 /)6,&)4

:60 ,')& •
/ * - + :!-+5-#14 !-*-$2* 6+*#-0%& 6+5%+/&
^ :&084* 7%+/&
:!-).-%&- !-*+,)& !-+(05-2& *. !-%++0
sin,cos,tan,cot,log
.’-.-
!-+(05-2 *1 )!-+# 4!-2% ”*5/)'“ 6++0
pi :&5!1%&
I : +/0*2%-0& &5!1%&
e '-& 6+%!+),-*& /+/#- 6+5'+")# !-+-8&
( log10(x ) : &%,-"* ,!)4' 7+-( 6' '*')

Matlab !"#$%

/))*(#'05 /)6,&)4 )620 !,)001 !,*"$

&0-*4& 7%+/ '-& (SLASH) / •


!*#0!%& &'(-!& -5& ans &5!1%& •
'*' ,&1$51 7-)4'& #-1+4#
.&8 #-1+4* &5!1% )",-& 6'
!,(&* 6-),+ &"-02& 9-/# ”;“ 7%+/& )"$& •
*-.+) .!#1-4%& &'(-!&
(… ”")3%“ !--&*
&5-)4'& &"-02* '+)0 (up arrow)! •

Matlab !"#$%
/))*(#'05 /)6,&)40 !,5'2,-

% - indicates comments
%-- Matlab works in radians

»(sin(23.5))^2 % without ‘’; ‘’ results are


displaying % immediately
ans =
0.9962

» (sin(23.5))^2; % with ’’ ;’’ results are not


displaying

Matlab !"#$%

(%&'$) /))*(#'05 /)6,&)40 !,5'2,-

% - trigonometric functions
%-------------------------------
» a=15.3;
» b=12;
» teta1=tan(b/a)^-1
teta1 =
1.0022
» teta2=1/tan(b/a)
teta2 =
1.0022

Matlab !"#$%
/)#!&' !*-2$ /" /))*(#'05 /)6,&)4

:/,6$1#
3""2!#%,$! 3"0*$0 3+ '"+$4"* /.$$0, 7$*'%

» a=3;
» b=8;
» c=13;
» solution1=(-b+sqrt(b^2-4*a*c))/(2*a)
solution1 =
-1.3333 + 1.5986i
» solution2=(-b-sqrt(b^2-4*a*c))/(2*a)
solution2 =
-1.3333 - 1.5986i

Matlab !"#$%

/)*+3' !2,7! 0& /)('*,+

Integer . "+%! ,(-% Integer •


.&"-05& )4'* !-)2/ 4 # "+%! ,(-% Real •
"+%! ,(-! &'(-!& 6-4!* :-4% ,(-%& )2/%& 6' •
Scientific Notation#
-' format !"-02# 1-%+1 +”$ -*' 6+0-4 9-0$* 7!+5 •
File 3+)2!# preferences 9+$/#
>> format

Matlab !"#$%
( %&'$) /)*+3' !2,7! 0& /)('*,+

• format short • 3.1416


• format long • 3.14159265358979 (15 !-)2/)
• format short e • 3.1416*10^+000 (5 digits +exp)
• format long e • 3.141592653589793*10^+000 (15 d
• format short g +exp)
• format long g • best of format short or format short e
• format hex • best of format long or format long e
• format bank • 234.bbc -hexadecimal, floating point
• format + • 3.14 -two decimal digits
• format rat • positive,negative or zero
• 335/113 rational approximation

Matlab !"#$%

(%&'$) !,5'2,- - /)('*,+

»format short % 5 digits


355/113
ans =
3.1416
» format long % 15 digits
» 355/113
ans =
3.14159292035398
»format short e % 5 digits+exponent
» 355/113
ans =
3.1416e+000
» format long e % 16 digits+exponent
» 355/113
ans =
3.141592920353983e+000
Matlab !"#$%
(%&'$) !,5'2,- - /)('*,+

» format short g % best fixed or floating point format


% with 16 digits + exponent
» 355/113
ans =
3.1416
» format long g % best fixed or floating point format
% with 16 digits + exponent
» 355/113
ans =
3.14159292035398

Matlab !"#$%

$6,&4 )1$ $-,.+

Matlab !"#$%
Matlab !"#$%

)#-) - !,7)*(' !*-2$

" a11 a12 a12 ! a1n #


$a a1n %%
$ 11 a11 a11 " M xN
A & $ a11 a11 a11 " a1n %
$ %
$ # # # $ # %
$' am 1 !-"-%$ )2/%
a11 a11 " amn %( !-)-1 )2/%

A=[1,2,3;5,6,7]

A= .&)-1& +)#+' 7+# "+)2% ,


.!-)-1& 7+# "+)2% ;
3 2 1
7 6 5
Matlab !"#$%
)#-) - !,7)*(' !*-2$

" a11 a12 a12 ! a1n #


$a a1n %%
$ 11 a11 a11 " M xN
A & $ a11 a11 a11 " a1n %
$ %
$ # # # $ # %
$' am 1 !-"-%$ )2/%
a11 a11 " amn %( !-)-1 )2/%

A=[1 2 3;5 6 7] : 2x3 )"/% &(+)3% )+",5

A= .&)-1& +)#+' 7+# "+)2% ,


.!-)-1& 7+# "+)2%
3 2 1
7 6 5
Matlab !"#$%

/)*,(.,, !,7)*(' 0-,2 !-)-'


[m,n]=size(A) &(+)3% *"-, !"+"% •
m=2, n=3 :&#-1!& -5*1 &)0% –
(1xn )"/% &(+)3%) )-30- *"-, !"+"% •
,b=[1 3 5] –
L=length(b) –
.L=3 : &#-1!& –

Matlab !"#$%
!,7)*(' !*-2$0 !,)7.#,+

F=zeros(m,n):6+/2' !(+)3% !)+(+ •


(mxn) 6+/2' &+)#+' *.1 &(+)3% !)+(+ –
O=ones(m,n) :6+"4' !(+)3% !)+(+ •
(mxn) 6+"4' &+)#+' *.1 &(+)3% !)+(+ –
diag !+(05-2 !)8$# !+5-/.*' &(+)3% !)+(+ •
,b=[1 2 3 4 7] 7-/.*'& !' 3*0. !*#0% )1'
B=diag(b) 8'-
I=eye(n) , nxn )"/% &"+4+& !(+)3% !)+(+ •
Matlab !"#$%

!,)5*.5 !,7)*('

:rand !+(05-2 •
:U[0,1] 1-* 0 7+# "+4' 6+,*-2%& 6+.)$ !*),&
R=rand(m,n)
:randn !+(05-2 •
0 7+# (!+*%)-5) !+/-', 6+,*-2%& 6+.)$ !*),&
:1 /5'+)-- ,0 $(-%% )%-*..1-*
R=randn(m,n)

Matlab !"#$%
/)6.," /)1*" )0"6 /)*,(., !*)7)

:6+#0-$ 6+)-30- !)+(+* 6+.)" +!1 !-%++0 •


: )1'. s:d:f &5#% –
+!*4!& ;)$ :s –
.&3%*/&*$%* &(+20 )-302 :d –
6-+/ ;)$ :f –
:'#& )-30-& !' )(-+ ,x=s:d:f )1'. •
x=[s s+d s+2d s+3d……..s+(n-1)d]
n=(f-s)/d+1-* &--1 &+&+ )",-% '* )1' ,n )1'. –
1-* &--1 -.)$ ,7+-(% '* d )1'. •
Matlab !"#$%

%&'$ - /)6.," /)1*" )0"6 /)*,(., !*)7)

? 6"-0& &5#%# &+$#& &% •


:linspace(s,f,n) !+(05-2 •
&(+20 )-302 *$# n ;)-'# )-30- !)+(+ –
d=(f-s)/(n-1)
*$# )-30- )(-+ x=linspace(0,10,100) '%,-"* –
d=10/99 &(+20 4--)%&1 !-%+," 100
6-1+)# 1%!1&* :*%-% #-14 d )-302& )1'. •
#-14 n 6+)#+'& )2/% )1'. .(s:d:f) 6"-0&
.linspace-# 1%!1&* :*%-%
Matlab !"#$%
!,7)*(' !))70,+)#'
"16 3 2 13 #
$ 5 10 11 8 %
A&$ %
$ 9 6 7 12 %
$ %
' 4 15 14 1 (

2 $-,'" 3 $*,&
)3*
+ ,
A(:, 2) & + ,
10
A(3,:) & / 9 6 7 12 0
+6,
++ ,,
- 15 .
Matlab !"#$%

$*,&/$-,'" !.)4'-!,7)*(' !))70,+)#'


&"-%$ !0+4% &)-1 !0+4%

X (3,:) & [ ]

"16 2 13 # "16 3 2 13 #
$ 5 11 8 %
X &$ % X & $$ 5 10 11 8 %%
$ 9 7 12 %
$ % $' 4 15 14 1 %(
' 4 14 1 (

Matlab !"#$%
!+04,&' $7)*('-!,7)*(' !))70,+)#'

"16 3 # "1 2 3i 2 3 3i #
A&$ % B&$ %
' 5 10 ( ' 2 2 i 1 3 2i (
" 1 3 3i 2 3 i #
"16 5 # X & B1 & $ %
1
X &A &$ % ' 2 2 3i 1 2 2i (
' 3 10 (
" 1 2 3i 2 2 i #
1
X & B. & $ %
' 2 3 3i 1 3 2i (
Matlab !"#$%

!,)('!)*5 !,0,"+ - !,7)*(' !))70,+)#'

Matlab !"#$%
!,)('!)*5 !,5'2,- - !,7)*(' !))70,+)#'

Matlab !"#$%

!,)('!)*5 !,5'2,- - !,7)*(' !))70,+)#'

Matlab !"#$%
!,)('!)*5 !,5'2,- - !,7)*(' !))70,+)#'

Matlab !"#$%
M FILES

)& *'+, -#$!.) !(/#" •


0#!1*"#+ -M -
)!1*"#+ -00# )!1*"#+. )$2#- -
fzero 0!1*"#+ -
Debugger -
DISP 0-#*+ :3!"#0"# 4%*4 02#10 -
3!.!2'0 -

!"#$!% &'( 1

!"#$%&"'

M- FILES !'.#-#$ 0#"50 '/+($ MATLAB •


:4$'#+
• function [x,y,w...]= function_name(a,b,c…);
:'/(5
)!1*"#+. )%!"5) !"0/$ … a b c
.)!1*"#+) 6#1!, 0#(1#0 3)/ 3!!$#*$ 3!"0/$ … x y w
:)$2#- •
0( EDITOR-, ,#05"
,#/!7. 0!'4"$.() )!1*"#+)
0!##& '/! /.#/$, '0!)
'0!) 8!,# 6.1) 8!, 0#!##&) ,#/!7.#

!"#$!% &'( 2

1
(()*+) !"#$%&"'

function [h,angle]=pyt(a,b)
% this function calculates hypotenuses ('0!) and angle (0!##&)
% a and b - cathetus length
h=sqrt(a.^2+b.^2);
angle=atan2(b,a);
pyt.m 3/, )!1*"#+) 0( '#$/" •

!"#$!% &'( 3

(2()*+) !"#$%&"'

:0!/(') 0!"5#0)$ )!1*"#+. )(!'* •


» d=3;
» e=4;
» [p,q]=pyt(d,e);
p=
5
q=
0.9273;

.) ,#&!)* :-.-,#)
-" h) pyt !#$%&"'
"/0)& 0. (angle
1"/2#3-

!"#$!% &'( 4

2
(2()*+) !"#$%&"'

3!!.,#.2# 3!!.*#. 3!"0/$ •


local
global
3), /$0/). '/+(-!(# 3!!.*#. 3!"0/$ 3) ,.4$ 01*"#+, 3!"0/$ •
0#'7( 0#!1*"#+, •
0( 0#('.# 0#'7( 0#!1*"#+, 3!"0/$, /$0/). 0'/+($ global 0-#*+ •
3!"0/$)
global 0-#*+ •
function tic1
% TIC simply stores clock in
% a global variable
global TICTOC
TICTOC=clock

!"#$!% &'( 5

(3()*+) !"#$%&"'
(9/$)) global 0-#*+ •
function t= toc1
% TOC Read the stopwatch timer
% TOC uses etime and the value of CLOCK saved by TIC1
global TICTOC
t=etime(clock,TICTOC);
>> tic1
>> t=toc1
t=
7.0320

>> whos global


Name Size Bytes Class
TICTOC 1x6 48 double array (global)
Grand total is 6 elements using 48 bytes

!"#$!% &'( 6

3
+#$%&"' -!!" +#$%&"'. +*4"5
• function y= stat(x) STAT 0!1*"#+
• %STA T - Interesting statistics.
• n = length(x);
• mean = avg(x,n);
• stdev = sqrt(sum((x-avg(x,n)).^2)/n);
• y=[mean,stdev];
%-------------------------
• function mean = avg(x,n) AVG )!1*"#+-00
• %MEAN subfunction
• mean = sum(x)/n;

» x=[1:0.1:10];stat(x) STAT 0(!'*


ans =
5.5000 2.6268

!"#$!% &'( 7

fzero !#$%&"'
:)(##/$, 3.6") 0( (1$ •
x3-2x-5=0 •

x = fzero(fun,x0)
EDITOR-,
function y = myf(x)
y = x.^3-2*x-5;
0#-#*+) 8#.7,
>> x0=2;
(,) :*/, 9/$)

!"#$!% &'( 8

4
(()*+) fzero !#$%&"'

fzero )!1*"#+. 3!('#*


» x = fzero('myf',x0)
8#'0+ 3!.,*$ •
x= 20

2.0946 15

10

-5

-10
0 0.5 1 1.5 2 2.5 3

!"#$!% &'( 9

(2()*+) fzero !#$%&"'

)"5#0 '#4, 8#'0+

>> x=exroot(2)

x=

2.0946

!"#$!% &'( 10

5
DEBUGGER - !"0#4) #"'#&

)"5#0) (debugging) !#+!". 0"5) •


Breakpoints - )"5#0) 0#.!6+ 0*%+) 0-#*" •
Breakpoints 36 (M-File) ;,#* 01') •
;,#*) 9#0, 3!-61, 6#"0 •
3!"0/$, )*!-, •
!#+!") 3#!%# )!6,) 8#*!0 •

!"#$!% &'( 11

(()*+) !"0#4) #"'#&

:)"5#0) (debugging) !#+!". 0"5)


;,#*) 36 -7! Editor/Debugger -) 0( 70+ •
: EDITOR-, ,#05 ,./$. •
function mytodebug(n)
% sinus calculation
% n-step for sinus
x=0:pi/n:2*pi;
y=sin(y); %error, must be y=sin(x);
[x’ y’]
:0#'6)
• Save changes--If you are editing the file, save the changes before
you begin debugging. If you try to run a file with unsaved changes, the
file is automatically saved before it runs.
• Add the files to a directory on the search path or put them in the
current directory.
!"#$!% &'( 12

6
Red is Valid and Gray is Invalid Breakpoints.
!"0#4) #"'#&
(2()*+)
Setting Breakpoints : )*%+) 0#-#*" 06!,*
Set breakpoints to pause execution of the function so you can
examine values where you think the problem might be.
:)$2#-.
In Editor/Debugger,
click in the breakpoint alley
(the column to the right of the line number)
at the line where you want to
set the breakpoint. the column
to the right of the line number.
Set breakpoints at lines that
are preceded by a - (dash). A
breakpoint icon appears

!"#$!% &'( 13

!"0#4) #"'#&
(3()*+)
Breakpoints 36 (M-File) ;,#* 01')
>> mytodebug(10)
• The prompt in the Command Window changes to
K>>
indicating that MATLAB is in debug mode

• The program is paused at the first breakpoint,


which in the example is line 5
• Debug -> Continue

!"#$!% &'( 14

7
!"0#4) #"'#&
(4()*+)
3!"0/$, )*!-,•
• While the program is paused, you can view the value of any
variable currently in the workspace.
Or:
K>> RSHQYDU n

!"#$!% &'( 15

!"0#4) #"'#&
(5()*+)
!#+!") 3#!%# )!6,) 8#*!0 •

• Quit debugging.
Do not make changes to an M-file
while MATLAB is in debug mode.
• Make changes to the M-file. Save the M-file.
• Set breakpoints, if desired.
• Run the M-file again to be sure it produces
the expected results.

!"#$!% &'( 16

8
MATLAB - +%#'/4
1

0 .8

0 .6

0 .4

0 .2

-0 .2

-0 .4

-0 .6

-0 .8

-1
0 0 .2 0 .4 0 .6 0 .8 1

36 0#"5#0) 07( (!) MATLAB :)7!0+ 0'6)


02#10 !"+#(# '0#!, 070#+$) )*!+'2)
0#"5#0) 8!,$ '0#!, 0#$!/'$) 0#(1#0)
.0#!4$0$)

H e li x

3 5

3 0

2 5

2 0

1 5

1 0

0
1
0 .5 1
0 0 .5

-0 .5
-1 -1
- 0 .5
0

!"#$!% &'( 17

MATLAB - +%#'/4

:)& *'+, -#$!.) !(/#" •


3!!-$$ #- 3!+'2 - •
3!!-$$ 0.0 3!+'2 - •
8#0" :'2 !5'6 0%"5) - •
-7( :-, 3!+'2 !#,' - •

!"#$!% &'( 18

9
,##5**-"5 ,#'/4
:(XY :'2) !-$$-#- :'2 0'!1!. )4#/+) )-#*+)
• Plot(x,y)
.3!)& 3!-$$ !.6, 3!!-$$ -7 3!'#4*# Y # X '/(5 •
:)4#/+ )$2#- •
• » x=[0:0.01:1];
• » y=sin(2*pi*x);
• » plot(x,y) 1

-1
0 05 1
!"#$!% &'( 19

grid !)/ !'6"!. +5"%'

>> grid
1

0 .5

-0 .5

-1
0 0 .2 0 .4 0 .6 0 .8 1
!"#$!% &'( 20

10
-"!#2 !'6"!

X '!1. •
• xlabel(‘text’)
Y '!1. •
• ylable(‘text’)
:'2. 0'0#5 •
• title(‘text’)
: )$2#-. •
• » title('Amplitude Vs.
time');label('amplitude');xlabel('time');
(,) :*/, 9/$)
!"#$!% &'( 21

:-"!#2. +*4"5
A m p li tu d e V s . ti m e
1

0 .8

0 .6

0 .4

0 .2
amplitude

- 0 .2

- 0 .4

- 0 .6

- 0 .8

-1
0 0 .2 0 .4 0 .6 0 .8 1
ti m e
!"#$!% &'( 22

11
"-2/+" 7/4+ 8-$ !8#-%
• plot(x,y,’linetype color’)
:)$2#- •
• plot(x,y,’+ r’)
3#-( 6,1,# ”+“ #* 2#%, XY :'2 44'/ •
, #/$0/) 3!##*) !2#%# 3!6,1) !2#% .6 )!1$'#+"!( 0.,*. •
HELP PLOT
1

0 .8

0 .6

0 .4

0 .2

- 0 .2

- 0 .4

- 0 .6

- 0 .8

-1
0 0 .2 0 .4 0 .6 0 .8 1

!"#$!% &'( 23

,#/#$ !2/8* .8 ,##5**-"5 ,#'/4 /'6*


!90
plot(x1,y1,x2,y2)
8#/(') :'2. Y1 -# X1 '/(5 –
!"/) :'2. 0#4"!-'#(#*) 2#& 3) Y2 # X2 –
:)$2#- –
• » x2=x;
• » y2=cos(2*pi*x); 1

• » plot(x,y,x2,y2) 0 .8

0 .6

0 .4

0 .2

-0 .2

-0 .4

-0 .6

-0 .8

-1
0 0 .2 0 .4 0 .6 0 .8 1

!"#$!% &'( 24

12
--/ ,#/#$ !"2/8*. !"#..2 !"/8+
!"#'/4

3!,!!7 3!'!1) 05'6$ .6 3!+'2) .5 '#,6 3!'#4*#) !.-2 •


3!)& 0#!).

)"5#0) 30#( 6,*0 3!+'2) !6,1 0( /('$ 3!6,#* (. 3( •


.6#-! '-% !+.
.3!(.$ 3!#* #44'#/! /'#+$ 8#!1 (.. ,)'*$ .5,

”3!$!!*# 3!'!'/“ 3!$-#*) 3!+*/, #',%#)/ 3!"!!1$) .5 •


3!+'2 '+%$ 0.6, 05'6$. 32

!"#$!% &'( 25

7/4+ ("!. !"#-"!2" !"/8+ !6&2+

gtext(‘text’)
80!"/ ,.1 8$% :'2) !,2 .6 6!+#! )-#*+) 0%"5) '7(. •
.',56) 0#61$(, #6!").
817.) ;7.! #!'7(./ ,,.1) 6,*! #,/ 8#'7() 3#*!$)
.,#0!5) 0,1) 3#*!$ )!)! ',56, !.($/)
1

0 .8
s i n ( x )
0 .6

0 .4
c o s ( x )
0 .2

- 0 .2

- 0 .4

- 0 .6

- 0 .8

- 1
0 0 .2 0 .4
!"#$!%
0 .6
&'( 0 .8 1
26

13
(#5**-"5 7/4-) ,#/#$+ !2/8* #.54 !8#-%

!5"() '!1) '#,6 3!'!1) !.-2 0( 6#,*. 80!" •


.!*+#()#
• axis (auto)
• axis(Xmin Xmax Ymin Ymax)

!"#"$% #&'(&
)"*+&)% ,&)- !. %#/.%0 1)"*
av=[0 1 0 10];
axis(av)
!"#$!% &'( 27

(()*+) ,#/#$+ !2/8* #.54 !8#-%

0.4 -6 X) '!1 :#.,2#! 3-#*) %#"!%) '#!1, 3!'!1) :)$2#- •


.1 9'6. -6 0$ .,2#! Y) '!1#
• » axis([0 0.4 0 1])
1

0 .9

0 .8

0 .7

0 .6

0 .5

0 .4

0 .3

0 .2

0 .1

0
0 0 .0 5 0 .1 0 .1 5 0 .2 0 .2 5 0 .3 0 .3 5 0 .4
!"#$!% &'( 28

14
,#.#4/!
errorbar 0-#*+ .1 •
).#5 )"5#0)# 0#-#*+) '!,%)# *#-, ,,#05 ’(
» x = 1:10;
» y = sin(x);
» e = std(y)*ones(size(x));
» errorbar(x,y,e)
symmetric error bars of unit standard “ 4%*4 ,#05 ’,
deviation”
!o!-, 0#-#*" 8$% !2
polar 0-#*+ .2 •
).#5 )"5#0)# 0#-#*+) '!,%)# *#-, ,,#05 ’(
» t = 0:.01:2*pi;
» polar(t,sin(2*t).*cos(2*t),'--r')
.:!1' #*. #*) 0(# GREEN-. 6,1) 0( )"/ ’,
!"#$!% &'( 29

(()*+) ,#.#4/! hist 0-#*+ .3 •


).#5 )"5#0)# 0#-#*+) '!,%)# *#-,# ,#05 !(
» x = linspace(-2.5,2.5,20);
» y = randn(5000,1);
» hist(y,x)
” Histogram of Gaussian Data ” 0'0#5 ,#05 !,
hist(y,10) )-#*+) 0( '#1!! :'2 )&!( '!,%) !2
2002 .!'+(, 0#!$#!) 0#!.$!%*$) (oF) 0#'#4'+$4) .4 •
:8) Washington DC
89 93 91 84 82 74 63 69 66 73 73 56 48 50 53 73 73 58
69 63 74 64 66 63 64 56 69 59 80 91
3!'!1, 0#'0#5 80# 0#-#$6 6 ./ )$'2#4%!) '#1 •
0#'#4'+$4) '/(5 .!'+(, 0#'#4'+$4) :'2 '#1 •
!"#$!% &'( .%#!%.1 0#.6$, 30

15
(()*+) ,#.#4/!

)!1*"#+) 0( '!!1 .4 •
f(E) = (2*pi/(pi*kT)^1.5)*sqrt(E)*exp(-E/k)

k = 8.6170e-5; % Boltzmann constant (eV/K) •


T = 20; % desired temperature (C) •
T = T+273; % desired temperature (K) •
kT = k*T; % energy associated with given temperature (eV) •
c = 2*pi/(pi*kT)^1.5; % normalization constant in equation for f(E) •
% •
% Define energy vector (eV) •
Eo = 0; Ef = 0.5; NE = 251; E = linspace(Eo,Ef,NE); •

!"#$!% &'( 31

(()*+) ,#.#4/!

T[300 100 20] '#,6 065 )!1*"#+) 0( '!!1 . 5


.-!7! figure !,2 .6 3!+'2) .5 0( 21)
f(E) = (2*pi/(pi*kT)^1.5)*sqrt(E)*exp(-E/k)

k = 8.6170e-5; % Boltzmann constant (eV/K) •


T = 20; % desired temperature (C) •
T = T+273; % desired temperature (K) •
kT = k*T; % energy associated with given temperature (eV) •
c = 2*pi/(pi*kT)^1.5; % normalization constant in equation for f(E) •
% •
% Define energy vector (eV) •
Eo = 0; Ef = 0.5; NE = 251; E = linspace(Eo,Ef,NE); •

!"#$!% &'( 32

16
&##'"$% &#($)#*+ ,-(."
Matlab /(#(!(

4 '!" #$!#%

MatLab( /$0+$+$ &#0%/


1. “ if “
2. “ switch and case ”
3. “ for “
4. “ while ”
5. “ break ”

1
1. “ if “
The if statement evaluates a logical expression and
executes a group of statements when the expression is
true.

A=5; B=3;
if A>B
disp(‘A>B’)
elseif A<B
disp(‘A<B’)
elseif A==B
disp(‘A=B’)
else
disp(‘unexpected situation’)
end

2. “ switch and case ”


The switch statement executes group of statements based on the
value of a variable or expression.

switch ( (-1)^n +1)


case 0
disp(‘n is odd’)
case 2
disp(‘n is even’)
otherwise
disp(‘this is impossible’)
end

2
3. “ for ”
The for-loop repeats a group of statements a
fixed, predetermined number of times.

for i=1:3
for j=1:3
y(i,j)=1/(i+j);
end
end

4. “ while “
The while-loop repeats a group of statements an indefinite number of times under
control of logical condition.

a=0; fa=-inf; b=3; fb=inf;


while b-a>eps*b
x=(a+b)/2;
fx=x^3-2*x-5;
if sign(fx) == sign(fa)
a=x; fa=fx;
else
b=x; fb=fx;
end
end
x

3
5. “ break “
The break statement lets you exit early from a for-loop or while-loop.

a=0; fa=-inf; b=3; fb=inf;


while b-a>eps*b
x=(a+b)/2;
fx=x^3-2*x-5;
if fx==0
break
elseif
a=x; fa=fx;
else
b=x; fb=fx;
end
end
x

MatLab -( )"/)" 3)""


:1$)2 1+3
k = input('Choose a menu option;('...
• Choose a menu option...1

:1$)2 )"/)" 1#'2/ /##%(


K = MENU('Choose a color','Red','Blue','Green')

• ----- Choose a color -----



• 1) Red
• 2) Blue
• 3) Green

• Select a menu number:

4
Polynomial Function Summary
! Conv Multiply polynomials.
! Deconv Divide polynomials.
! Poly Polynomial with specified roots.
! Polyder Polynomial derivative.
! Polyfit Polynomial curve fitting.
! Polyval Polynomial evaluation.
! Polyvalm Matrix polynomial evaluation.
! Residue Partial-fraction expansion (residues).
! Roots Find polynomial roots.

Representing Polynomials

! MATLAB represents polynomials as row


vectors containing coefficients ordered by
descending powers. For example, consider
the equation

! To enter this polynomial into MATLAB, use


p = [1 0 -2 -5];

1
Polynomial Roots
! The roots function calculates the roots of a polynomial.
! r = roots(p)
! r = 2.0946
-1.0473 + 1.1359i
-1.0473 - 1.1359i
! By convention, MATLAB stores roots in column vectors.
The function poly returns to the polynomial coefficients.
p2 = poly(r)
p2 = 1 8.8818e-16 -2 -5
! poly and roots are inverse functions, up to ordering,
scaling, and roundoff error.

Polynomial Evaluation

! The polyval function evaluates a polynomial at a


specified value. To evaluate p at s = 5, use
polyval(p,5)
ans = 110
! It is also possible to evaluate a polynomial in a
matrix sense:
! X = [2 4 5; -1 0 3; 7 1 5];
! Y = polyvalm(p,X)
! Y= 377 179 439
111 81 136
490 253 639
4

2
Convolution

! Polynomial multiplication and division


correspond to the operations convolution and
deconvolution. The functions conv and
deconv implement these operations.
! Consider the polynomials
and
To compute their product, a = [1 2 3]; b = [4 5
6];
c = conv(a,b)
c = 4 13 28 27 18
5

Deconvolution

! Use deconvolution to divide back out of the


product.
[q,r] = deconv(c,a)
q=
4 5 6
r= 0 0 0 0 0

3
Polynomial Derivatives
! The polyder function computes the derivative of any polynomial. To
obtain the derivative of the polynomial
p = [1 0 -2 -5], q = polyder(p)
q = 3 0 -2
! polyder also computes the derivative of the product or quotient of two
polynomials. For example,
a = [1 3 5]; b = [2 4 6];
Calculate the derivative of the product a*b by calling polyder with a single
output argument.
c = polyder(a,b)
c= 8 30 56 38
Calculate the derivative of the quotient a/b by calling polyder with two
output arguments.
[q,d] = polyder(a,b
q = -2 -8 -2
d = 4 16 40 48 36
q/d is the result of the operation.
7

Polynomial Curve Fitting


! polyfit finds the coefficients of a polynomial that fits
a set of data in a least-squares sense.
p = polyfit(x,y,n)
! x and y are vectors containing the x and y data to be
fitted, and n is the order of the polynomial to return.
For example, consider the x-y test data.
x = [1 2 3 4 5]; y = [5.5 43.1 128 290.7
498.4];
! A third order polynomial that approximately fits the
data is
p = polyfit(x,y,3)
p = -0.1917 31.5821 -60.3262 35.3400
8

4
Polynomial Curve Fitting, Cont.

! POLYVAL Evaluate polynomial.


! Compute the values of the polyfit estimate
over a finer range, and plot the estimate over
the real data values for comparison.
x2 = 1:.1:5;
y2 = polyval(p,x2);
plot(x,y,'o',x2,y2); grid

Polynomial Curve Fitting, Graph

10

5
Partial Fraction Expansion

! residue finds the partial fraction expansion of the


ratio of two polynomials. This is particularly useful
for applications that represent systems in transfer
function form.
! For polynomials b and a, if there are no multiple
roots,

! where r is a column vector of residues, p is a column


vector of pole locations, and k is a row vector of
direct terms.

11

Partial Fraction Expansion, Example


! Consider the transfer function

b = [-4 8]; a = [1 6 8];


[r,p,k] = residue(b,a)
r = [-12 8]’ p = [-4 -2]’ k= []
! Given three input arguments (r, p, and k), residue
converts back to polynomial form.
[b2,a2] = residue(r,p,k)
b2 = [-4 8]; a2 = [1 6 8];

12

6
!"#""$% &$ '(%") *"(!+
!'"&#',)+'-
ODE

Numerical Methods for ordinary


differential equations
Many problems that arise in science and engineering require a knowledge of a
function
y = y(t), that satisfies the first order differential equation
y' = f(t, y) and the initial condition y(a) = y0.
where a and y0 are given real numbers and f is a bivariate function that
satisfies certain
smoothness conditions. A more general problem is formulated as follows.
Given function f of n variables, find a function y = y(t) that satisfies the nth
order ordinary differential equation: y( n ) = f(t, y, y', … , y(n – 1)) together with
the initial conditions
y(a) = y0, y'(a) = y0
', … ,
y( n – 1) (a) = y0( n – 1).

The latter problem is often transformed into the problem of solving a system
of the first order differential equations. To this end a term "ordinary differential
equations" will be abbreviated as ODEs.

1
Solving the initial value problems using
MATLAB built-in functions

• ode23 Nonstiff ODEs Explicit Runge-Kutta (2, 3) formula


• ode45 Nonstiff ODEs Explicit Runge-Kutta (4, 5) formula
• ode113 Nonstiff ODEs Adams-Bashforth-Moulton solver
• ode15s Stiff ODEs Solver based on the numerical
differentiation formulas
• ode23s Stiff ODEs Solver based on a modified Rosenbrock
formula of order 2

Simplest form of the syntax for the MATLAB


ODE solvers

[t, y] = solver(fun, tspan, y0]

Fun: is a string containing name of the ODE m-file that


describes the differential equation.
Tspan: is the interval of integration.
Y0: is the vector holding the initial value(s).
If tspan has more than two elements, then solver returns
computed values of y at these points.
The output parameters t and y are the vectors holding the
points of evaluation and the computed values of y at these
points.

2
Example 1:
We will seek a numerical solution y at t = 0, .25, .5, .75, 1 to
the following initial value problem:

y' = -2ty2
y(0) = 1

We will use both the ode23 and the ode45 solvers.

The exact solution to this problem is:

y(t) = 1/(1 + t2)

Solution 1
function dy = eq1(t,y)
% The m-file for the ODE y' = -2ty^2.
dy = -2*t.*y(1).^2;
format long
tspan = [0 .25 .5 .75 1]; y0 = 1;
[t1 y1] = ode23('eq1', tspan, y0);
[t2 y2] = ode45('eq1', tspan, y0);

[t1 y1 y2]
ans =
0 1.00000000000000 1.00000000000000
0.25000000000000 0.94118221525751 0.94117646765650
0.50000000000000 0.80002280597122 0.79999999678380
0.75000000000000 0.64001788410487 0.63999998775736
1.00000000000000 0.49999658522366 0.50000000471194

3
Anonymous Functions
• Creating simple functions without having to
create M-files each time.
• You can construct an anonymous function either
at the MATLAB command line or in any M-file
function or script.
• >>sqr = @(x) x.^2;
• a = sqr(5)
• a=
• 25
• >>sumAxBy = @(x, y) (A*x + B*y);
• sumAxBy(5, 7)

Solution 1-@
• dy =@(t,y) -2*t.*y(1).^2;
• format long
• tspan = [0 .25 .5 .75 1]; y0 = 1;
• [t1 y1] = ode23(dy, tspan, y0);
• [t2 y2] = ode45(dy, tspan, y0);

4
Example 2
y1'(t) = y1(t) – 4y2(t), y2'(t) = -y1(t) + y2(t)

y1(0) = 1; y2(0) = 0.

Solution
dy = @(t,y) [1 -4;-1 1]*y
tspan = [0 1]; y0 = [1 0];
[t,y] = ode23(dy, tspan, y0);

plot(t,y(:,1),t,y(:,2),'--'), legend('y1','y2'), xlabel('t'),


ylabel('y(t)'), title('Numerical solutions y_1(t) and y_2(t)')

The exact solution (y1(t), y2(t)) to this system is


y1, y2
y1 =1/2*exp(-t)+1/2*exp(3*t)
y2 =-1/4*exp(3*t)+1/4*exp(-t)

5
Example 3:

dy = @(t,y) -1000*(y - log(1 + t)) + 1/(1 + t)


Using the ode23s function on the interval
tspan = [0 0.5];
we obtain
[t, y] = ode23s(dy, tspan, 1);

Solution 3:
plot(t, y), axis([-.05 .55 -.05 1] ), xlabel('t'), ylabel('y(t)')
title('Solution to the stiff ODE')

The exact solution to this problem is y(t) = log(1+t) + exp(-1000*t).


Try to plot this function on the interval [-0.05, 0.5].

6
!"#!$ $%#&'
Solve the differential equation for a pendulum with a small displacement.
(This problem is identical to a mass on a spring.) There are two variables, q, and t
corresponding to angle and time. If we assume that the length, mass of the
pendulum, and acceleration of gravity are 1 (in some odd units!) then the ODE for
this system is:

d 2q
2
" !q
dt
In order to find a computer solution it is useful to break
this equation up into three first order equations. This can
be done if you define a new variable ,w ,corresponding to
the angular frequency. The equations for the pendulum
can now be written a:
dw dq w(0) " 0;
1). " !q "w q ( 0) "
#
;
dt dt 4

!"#!$ $%#&'

dw dq w(0) " 0;
2). " ! sin(q) "w q ( 0) "
#
;
dt dt 4

dq w(0) " 0;
dw
3). " ! sin( q ) ! w * Q "w q ( 0) "
#
;
dt dt 4

2 -& 0 *'. /)!$% Q

7
Matlab-! "#$%

1 !"#$!
:!"#$!! %"&'(
2 ............................................................................................................. &'($')#%# "#$%
3 ......................................................................................................Matlab *$'$+ *,-& .1
3 ............................................................................................. 01#%'1# *#2#!.& )#,/ 1.1
4 ....................................................................................................Matlab '($3 '4#+ 1.2
4 ....................................................................................... Matlab !1 help-$ 1#%'1 1.3
5 ..................................................................................... 0'2*1% *#%1 *5'$3! 0'!!- 1.4
5 ................................................................................. Command Window-$ &4#(* 1.5
5 ................................................................................................... Matlab-$ 0',/+% 1.6
6 ......................................................................................................Matlab-% &"'(' 1.7
7 ............................................................................................................. *#(',)% *,64& .2
7 ............................................................................................................ *'26' &,64& 2.1
8 ................................................................................. 0'2*1%! 0''#)'$ *+2-& 2.1.1
8 ......................................................................... 0',#)3## *#(',)% !6#4 *6'6% 2.1.2
8 ......................................................................................... *#(',)% *,64&! *#'(32#/ 2.2
8 .................................................................................. 0'6."# 0'+/" *#(',)% 2.2.1
9 ........................................................................................... *'2#+-!" &(',)% 2.2.2
9 ...........................................................................................*#'",3" *#(',)% 2.2.3
10 ........................................................................... 0'$3#5 0'-,5 '!5$ 0',#)3# *,'(' 2.3
11 ................................................................................................ Array Editor 7#!. 2.4
13 ................................................................................................. *#(',)% !1 &'(!#/'2% .3
13 ................................................................................................ *#(',)%! 0'+362'" 3.1
14 ........................................................................................sort-# find *#6#3/ 3.1.1
16 .............................................................................................. 0''(',)% 0',#),/#" 3.2
17 ................................................................................................... &)22'%,)6 3.2.1
17 .............................................................................................. &(',)% 8#/'& 3.2.2
18 ..................................................................................................... &6#32& ,#),/#" 3.3
18 ........................................................................... &6#32& ,#),/#"! 0'1#%'1 3.3.1
Matlab-! "#$%

!"#$"%&'& (&$'

&4(&# *#'%6& ,56'% 6#$'5 *,1/"%& 0'+62&%# 0')26#)+! *%63*% *#2-* */1 "'& Matlab */1
.*#"(#* .#*'2! *65#'%& &$'$+$# 6#3 *#,#1$ )#5'% *#5(%"$ *2##4% *'*#9.
,0& *#,." *#/1# Matlab 7'$ 0''9-,%& 0'!6$&&
"#& Matlab 01& ,#3% ,&15%! .*#(',)%# 0',#)3# 0& 0''+'+$& 0'2*1%& Matlab-$ .1
.Matrix Laboratory
.1%*1%&% $, *#2-* 8+#.& (built-in) *#2$#% *#'(32#/ !1 $, ,/+% .2
toolbox *#',/+$ 0',#%1& 0''/'(/+ 0''+62& 0'%#.*! *#65#'%& *#6#3/ !1 $.,2 ,4"% .3
.&%#6% *#"'(% #!'/"# &3')+'))+ ,&,3$ ,*#2#%* 6#$'5 ,*#*#" 6#$'5 #%-

4'(&! 3, "'& #9 *,$#. *,)% .Matlab */1 !1 $.,& :3'&& !- *" 4'(&! *,%'*% &22'" #9 *,$#.
6#%'!& - Matlab-$ &6'%!! *'*'%"& &)'1& *" ,1/"! '6- 0'1,62& 0''+'+$& &2$&&# 56'& *"
.0'$.,2& &,95& '-',6% 8,6 '%(5&

.Matlab 6 ,#$5 &$*-2 #9 *,$#.

2
Matlab-! "#$%

Matlab )*%*+ )$,! .1


16. .m ;$#3 *.'*/
Editor/Debugger 7#!.$ &,95& 31%% !#.*"

&6#3/& *,#1 *'.-#2 &',/+ '#2'1


(command line)

&6#$5& $.,%

!- 8#*$ 0'!'% ,." 1#/'.


*'.-#2& &',/+$ .m '($3

$%&'($ )'*'+ :1.1 !"#

)*&'"*& +&,&-.! %&/0 1.1

,$+& 7#!.
*%6#3& &6#3/& *" !$3! 7*'2 .Enter !5 &('.!# 7*6!3& '"5 *#6#3/ *(,& Command Window
-$ 0#1',& *3'.% .&!5%! ,#*/- !5 &('.! '"5 command line-$
.clc *'(32#/ *#5(%"$ *5(#$% Command Window
.0&'$4! 56'%# #,64#&1 0'2*1%& !- *" &!'-%& &6#$5& $.,% *4#(* Workspace
!1 '!"%1& ,#*/- !5 &!#/- &('.! .*'.-#2& &'',/+$ 0'($3& !- )#,/ Current Directory
:&4'1% 0'"$& 0'($3& !5 !"2& 7#!.$ ,$-5&
.Editor/Debugger 7#!.$ ;$#3& *.'*/ - .m ;$#3
.Workspace-! 0'2*1%& *2'5) -.mat ;$#3
.&3'/,4& 7#!. *.'*/ - .fig ;$#3
7/#"$ 7*,$5&! *#,1/"# Command Window-$ #6!3#&1 *#6#3/ 6#5'* Command History
.Command Window-! '26'
.0'/+#2 0',95# ,demos ,&,95 '-%+%! &,'&% &1'4 Launch Pad

./&0*/& )&1&,2$ -&"3 :1.1 $,'-

3
Matlab-! "#$%

Matlab "#$1 "2&3 1.2

,Matlab-$ 0'($3 !1 0'4#+ ,/+% 021'


.m *%#'+ 05 0'($3 .1
7#!. 8,6 *#5(#$% 0*(,&# 0*-',5 .Matlab *#6#3/ 0'5($% #!" 0'($3
.Editor/Debugger
,.m '($3 '4#+ '21 0'%''3
"(%2& 56'% !5 0'!5#/ 0& .)!/ 0','9.% #" )!3 0'!$3% 02'" #!" 0'($3 :script ."
;$#3& *(,& .Command Window-$ *#6#3/& *6!3&! !#31 .Workspace-$
.( &)% '/!- ;.& ,#*/- !5 &('.! '"5 #") F5 !5 &('.! '"5 *'152
0& 0''%'2/& 0'2*1%& !- .)!/ ,'9.&!# )!3 !$3! 0'!#-' #!" 0'($3 :function .$
function .*/% *!'%$ 0'1%*1% &'(32#/- 0'($3 ,'64&! '6- .&'(32#/! 0''!3#!
.8 3,/$ ,$+#'1 '/-
.mat *%#'+ 05 0'($3 .2
.0&'-,5# 0'2*1% 0'2+-"% #!" 0'($3
.fig *%#'+ 05 0'($3 .3
.0'/,4 0'2+-"% #!" 0'($3

Matlab -* help-$ *&'"* 1.3

,&,95& 31%% *#5(%"$ *#'(32#/# 0'"1#2 '$4! &,95 1/.! 7*'2

&!'% ,." 1#/'.


&,95& '($3$

;$#3 *4#(*
7%#+%& &,95&

!-
'($3
&,95&
0'!'-%&
*!'% *"
1#/'.&
&,95& ;$#3
7%#+%&

help 4&,2 :1.2 !"#

4
Matlab-! "#$%

,&'(32#/ '$4! 0',$+& !$3! '6- 0'-,6 ,/+% *#%'3

5#6' "! &'(32#/& 01 5#6' &'(32#/& 01


-$ 4'(% .*/% *!'%# lookfor *$'*- command -$ 4'(% &'(32#/& 01# help *$'*-
*#'(32#/& !- *%'1, Command Window 7/#"# &'(32#/& *!#5/ '$4! ,$+& window
..*/% *!'%! ,13 7&! 1'1 .&$ 1#%'1&
.search *''(/#" *#5(%"$ &,95& 31%%$ 1#/'. &,95& 31%% *" .*#/ &'(32#/& 01# doc *$'*-
!'-% help *''(/#" *%#5! .!"2& &'(32#/$
0'/,4 04 :+#2$# 0'),#/% ,*#' 0',$+&
.*#".+#2#

0'"(%2 #!" 0',/+ .#!1 0'-toolbox-$# Matlab-$ 1#%'1 '$4! &-,6& ',/+ !1 !#64 ,/+% 0''3
.Launch Pad 7#!.$ pdf )%,#/$ #" &,95& 31%%$ contents *''(/#" 8,6 html *+,4$
0'-toolbox-& !1 0'',1/" 0'%#1''$# Matlab-$ 1#%'1 0','$+%& $., demos ,4"% 0''3 :+#2$
.#!1

)",+*' +&'* +4"$1- )"--5 1.4

$''. 01 !- .:+#2 #* !-% 0!5*% Matlab ,1"- ,0'2*1% *#%1! 0'##* 31-! 65 0'(3#% Matlab-$
.01$ 0'.##, #'&'1 ,#+" .'_' underscore '##*# 0',/+% ,*#'*#" !#!-! !#-' 01 .*#"$ !'.*&!
.0'2#1 *#%1 0& Name-# name 01& 7-!# case sensitive "#& Matlab
:*#%1 7*%! *#',!#/#/ *#!#-+" '*1 *#%''3 .0'!'% ,/+% !1 $#!'1 0& *#%1 *#$, 0'*'5!
0"*&$ MaxForce #" max_force $*-'* max force !1%! .*#!#64 *#'*#"$ 1#%'1# underscore
.'1'" 05)!

Command Window-$ !2&#+ 1.5

,7&% 1#!1 3, 4'(2 8" *#$, *#'(/#" *#%''3 .format *'(32#/ *#5(%"$ &4#(*& *" *#21! 7*'2
.0'.##,& $#,% ,)/2& +#.6 ,*#' 4#('' :format compact.1
.format compact *" !)$% &15%! .0'.##,% !5$ 4#(' :format loose .2
.0''!2#'(, 0',/+%- 0',/+% 4#(' :format rat .3

.format short #+'/6& *#,/+ 5 !1 '),62)+& 4#(''! ,#9.! '6-


.0'2*1%& *,'%1 7/#" !5 0'5'/1% 02'"# &4#(*$ 3, 0& !"2& 0'#2'1& !- '- ,#-92

Matlab-$ )"/03' 1.6

&39.& ,#)3/ 7''(! '6- e *#"$ 1%*1%& '56%& 0#1',$ 04 ,'-% Matlab '2#,15& 0#1',! :+#2$
. 6 !10"2 "#& 6e-2 -# 2 !105 "#& 2e5 ,%#!- .*'2#,15&
0'+3!/%#3 0',/+% 0& 2+4i, 1-3j "%4#6! .j #" i *#"& *#5(%"$ 0'2'#(% 0'+3!/%#3 0',/+%
.Matlab-$ 0''3#.

,Matlab-$ 0'$#1.# 0'5#$3 0',/+% 0'%''3

5
Matlab-! "#$%

.realmax -% !#64& 8,5$ #" +/"$ ,/+% *3#!.% !$3*% Inf 8,5&
I nf 0
. #" &3#!.&% !$3*% NaN 8,5&
Inf 0

Matlab-' !("#" 1.7

!5 &('.! '"5 #" Command Window-$ quit *$'*- '"5 *5(#$% Matlab-% *,6#+% &"'('
.ctrl+q

6
Matlab-! "#$%

)'#%$-. )$/0! .2
+",6" !/62! 2.1

.*#(',)% !1 '),/ &,3%- 0',!3+# 0',#)3# !5 $#1.! 7*'2 .&(',)% "#& Matlab-$ '+'+$& &2*1%&
,'!!- 7/#"$ *,64#%# 0'-,5 !1 '2$!% 8,5% "'& &(',)%

# a11 a12 ! a1n $


%a a 22 ! a 2n &&
A ' % 21
% " " # " &
% &
%( a m1 a m2 ! a mn &)
. * m , n + &(',)% "'& A ,%#!-

0'%.#*% &(',)%& ',$'" .&-#*$ 0'-,5 *$(&# &(',)% *,'(' "'& Matlab-$ ,*#'$ *'+'+$& &!#5/&
.*#,#1& 7'$ 6',/% ";" 7%'+# &(',)%& !1 &,#1& ',$'" 7'$ 6',/% "," 7%'+ ,*#5$#,% 0'',4#+ 8#*$
,"$& 7/#"$ A &(',)%& *" ,'642 !1%!

,,#1'"& 5'/#' &!#5/& 7#!.$

.&2*1%& 4#+ *" Matlab-$ 1",% ,'64&! 8,#( 7'" '- $! 0'12
.&,#1$ ,$'" !- 7'$ .##, *%1& "'& &(',)% !1 &,#1& ',$'" 7'$ 6',/&! */+#2 8,6
,&6#3/& ,%#!-

.3#'6$ &(',)% &*#" *" *,(#'

5'/#% *5- .0''.-#2& 0'2*1%& !- !1 0'!64&# *#%1& 0'5'/#% (Workspace) &6#$5& $.,%$
. * 2 , 3+ !6#4$ &(',)% "#&1 A 01$ &2*1%

?$1#.%& 8,5& $(#' #'!" &2*1% 01 "!! 8" &2*1% ,'642 ,1"- &,#3 &%
&2*1% ,64#' 0" .&2*1%& 8,5 $(#% #-#*!1 ans 01$ &2*1% ,'64% *')%#)#" Matlab &9- &,3%$
.3.%' 06#3& 8,5&# ans ",3' 04 &9 &2*1% ,01 "!! :+#2
,"%4#6!

.A &(',)%! &&9& ans 01$ * 2 , 3+ &(',)% &,(#2

7
Matlab-! "#$%

:'+#2 &6#3/& :#+$ 7-!# 0'-,5& *$(& *!#5/ ,."! &(',)%& 7-#* *" *#",! &(,2 "! 0'$, 0',3%$
,&6#3/& ,%#!- .";"

.&(',)%& 7-#* *" &4'(% &22'" 8" A &(',)% &*#" *" *,(#'

1%()&.2 1%%'-%* )+(,! 2.1.1

.$#1'. 0'1,#6& 0''#)'$ 04 *#'&! 0'!#-' &(',)% ',$'" ,0',/+%! :+#2$


,"%4#6!

v - *3 49 9 7 8 2+

05 ,#)3# ,'64&! 0'(#, 05 7-! .0''!#$%'+ 0'#)'$$ "!# 0',/+%$ 3+#5 Matlab '- ,#-9! $#1.
,#%- 0'!!- 0''#)'$

.',/+% 8,5$ #!.*#" (w, x, y, z) 0'2*1%& !- 0" "!" !"2& '#)'$$ ,'-% #22'" Matlab 9"

1%$'-3'' )'#%$-. 2/'0 )/%/. 2.1.2

*#,#1& ,/+% ,',3) &(',)%& '6%% *" !$3! '6- . * 2 , 3+ &(',)% "'& #2,64&1 A &(',)%& ,#-9-
,"$& 7/#"$ size &'(32#/$ 1%*12 (*#6#%5& ,/+%#

m-2
n -3

7*'2 (&,#1 #" &6#%5 ,#)3# "#& ,#)3#& 0" &21% &9 7'") ,#)3# 8,#" 6#6%! '6- ,*"9 *%#5!
,"$& 7/#"$ length &'(32#/$ 1%*1&!

L-3

'- size -# length *#6#3/$ 1%*1&! '#(, 7''65 ,&(',)%& !6#4 #" ,#)3##& 8,#" #2! 5#6' 0" 04
.,*#' '!!- &,3%! *#$.,#% '/'(/+ &,3% ,#$5 *#$*-2& *#6#3/ *#$, 0'*'5!

+&#"/%' +/62!- +&"#1,&0 2.2

.*#6.#'% *#(',)% *#,(#'& *#'(32#/ ,/+% *#%''3 *#(',)% !1 *'26'& &,64&! :+#2$

1%/4"' 1%+5" )'#%$-. 2.2.1

.&(',)% !#.*"! *#(#/2 *#(',)% 7& 0'6."# 0'+/" *#(',)%

8
Matlab-! "#$%

,0'+/" 0& &',$'" !-1 * m , n + &(',)% *,'('

,0'6." 0& &',$'" !-1 * m , n + &(',)% *,'('

)%('+,2" !#%$-. 2.2.2

7#+-!"! ;#.% 0',$'"& !-$ 0'+/" *!5$- *,64#%& * n , n + *'5#$', &(',)% "'& *'2#+-!" &(',)%
,'1",&
# a11 0 ! 0 $
%0 a # " &&
A' % 22

%" # # 0 &
% &
%( 0 ! 0 a nn &)

#9 &'(32#/ .diag *'(32#/ *#5(%"$ 0'+/"& !- *$'*- "!! &,('! 7*'2 *'2#+-!" &(',)% !1 &,3%$
.0"*&$ *'2#+-!" &(',)% ,'9.%# ,#)3# ,#*$ '1",& 7#+-!"& *" )!3- *!$3%
,!"2& *#6#3/& "%4#6!

,&(',)%& *" *#,(''%


#7 0 0 0$
%0 1 0 0 &&
B-%
%0 0 "3 0&
% &
(0 0 0 8)

7*'2 B *'5#$',& &(',)%&% 0''2#+-!"& 0',$'"& *"(#& ,%#!- ,&-#/&& &!#5/& *" 5($! '6-
,"$& 7/#"$ &'(32#/ &*#" *#5(%"$ *"9 *#15!

*(',)% *" ,(''! '6- .&6'.'& *(',)% "#& *'2#+-!" &(',)% !1 '1#%'1 '),/ &,3%
,"$& 7/#"$ eye &'(32#/$ 0'1%*1% I[n , n] &6'.'&

)'%"$3" )'#%$-. 2.2.3

rand *'(32#/
. U[0,1] &6'." *#!/*&% 0'!,4#%& 0'-,5 *!5$ &(',)% *,(''% #9 &'(32#/
,"#& &!1 '!!-& &2$%&

rand *'(32#/
,%#!- . N (0,1) (*'!%,#2) *'+#"4 *#!/*&% 0'!,4#%& 0'-,5 *!5$ &(',)% *,(''% #9 &'(32#/
.1 +2"',## ,+/" 5(#%%
,"#& &!1 '!!-& &2$%&

9
Matlab-! "#$%

)"$1&4 )"5/4 "-4$ )"/&%1& +/"#" 2.3

.0'$3#5 0'-,5 '!5$ 0',#)3# *,'('! 0'-,6 '*1 *#%''3


. s : d : f 0#1',& .1
8,5! 65 (d) ,64#% ,#)3/$ (0'2)3 #") 0'!64# (s) 0'#+% 8,5% 0'!'.*%1 0',$'" !5$ ,#)3# *,'('
,*$'*- ,%#!- .(f) 0#'+
x ' s:d:f
,,1"-
'*!.*& 8,5 -s
&2)3& #" !#6'4 ,#)/ -d
'/#+ 8,5 -f

,,#)3#& *" ,(#'


x ' *s s . d s . 2d ! s . (n " 1)d +

,&'&' *#6#32& ,/+% ,*'15% .,64#% "! (n) *#6#32& ,/+% '- $! 0'12
f "s
n' .1
d
,"%4#6

x - *3 5 7 9+
9"3
.n ' . 1 ' 4 "#& *#6#32& ,/+%#
2
,"%4#6

x - *10 7 4 1+
1 " 10
.n ' . 1 ' 4 "#& *#6#32& ,/+%#
"3

!"#$%
.d=1 &5'$3! &&9 d ,#)3/& 7#'( "!! 0#1', -
,"%4#6

x - *3 4 5 6 7 +
? f / s . (n " 1)d ,1"- &,#3 &% -
,&)% '/!- !4#5' *#6#32& ,/+% &9- &,3%$
%f "s &
n$ ' % . 1&
( d )
,&'&' !$3*'1 '/#+& 8,5& 9"#
f$ ' s . (n$ " 1)d
,,#)3#& ,(##' &15%! ,%#!-
x ' *s s . d s . 2d ! s . (n$ " 1)d +
,"%4#6!

10
Matlab-! "#$%

% f " s & % 8.999 " 1 &


n$ ' % . 1& ' % . 1& ' %( 4.9995&) ' 4
( d ) ( 2 )
$f ' s . (n$ " 1)d ' 1 . (4 " 1) ! 2 ' 7
,,#)3#& ,(#2 ,%#!-
x - *1 3 5 7 +
.#9- &!3* !1 *#,1/" 52#% linspace *'(32#/$ 1#%'1&

linspace *'(32#/ .2
8" ,,." 8,5! 65 5#$3 ,#)3/$ (0'2)3 #") 0'!64# 0'#+% 8,5% 0'!'.*%1 0',$'" !5$ ,#)3# *,'('
.')%#)#" 7/#"$ $1#.% ,#)3/&# *##(3& '-,5 '21 7'$ 0''#(,& 0'-,5& ,/+% *" 5$#3 1%*1%& *5-
,,%#!-
x ' linspace 0 s, f , n 1
f "s
,"$& 10'##1& 0'.##,%& !5$ ,#)3#& ,(#2 ,%#!- . d ' ,#)3/ !5$ n 8,#"$ ,#)3# ,#('
n "1
x ' #(s s . d s . 2d % s . 0 n " 11 d $)
,"%4#6

10
&%'46 .##,%$ 100-! 0 7'$ 0'-,5 !5$ 100 8,#"$ ,#)3# ,(#'
.
99
.$#1. #2'" n 0',$'"& ,/+% #!'"# $#1. #" 5#6' d ,#)3/& ,1"- 7#1",& 0#1',$ 1%*1&! ;!%#%
d ,#)3/& #!'"# ,$#1. #" 5#6' n 0',$'"& ,/+% ,1"- '21& 0#1',$ 1%*1&! ;!%#% ,*"9 *%#5!
.$#1. #2'"

Array Editor 7&-. 2.4

&(',)%& !5 ,$-5& !1 '!"%1& ,#*/-& !5 0''%5/ ;#.!! "'& *#(',)% ',$'"$ 72#$*&! &.#2 8,6
!5 &!#/- &('.! "%4#6! .Array Editor 7#!.$ 5'/#* &(',)%&# workspace-& 7#!.$ &'#(,&
.2.1:,4$ 7#!.& *" .*#/ A,B,I,L *#(',)%

*#21! 7*'2 :+#2$ .&%'"*%& *'2#1!& !5 &('.! '"5 Array Editor-$ &2*1%! &2*1% 7'$ ,#$5! 7*'2
.'26' 7/#"$ &(',)%& ',$'" '-,5 *"

.0'+362'"$ 1#%'1# &$(& *!#5/ *#5(%"$ "'& &(',)%$ 0',$'" *#21! */+#2 8,6

,"$& 7/#"$ logspace &'(32#/$ 0'1%*1% *'%*',4#! &!"3+$ 0'##1 0'.##,% !5$ ,#)3# ,#('! '6-1
. x ' #(10
s
10s . d 10s . 2d ! 10f $) "#& ,(#''1 ,#)3#&# x ' log space(s, f , n)

11
Matlab-! "#$%

array editor 4&,2 :2.1 !"#

12
Matlab-! "#$%

)'#%$-. 2& !%#2'5%(. .3


+&#"/%'- )"316,"( 3.1

7''(% 7#1",& ,$'"& .0'+362'"# *#!#45 0'',4#+ *#5(%"$ &(',)% 8#*$ 0',$'"&% 3!.! *14! 7*'2
.2&6#%5 ,/+% 7''(% '21& ,$'"&# &,#1 ,/+%
,0& &(',)%$ 0'+362'"&

$5*"-0' .*+6%1*7 :3.1 !"#

,"%4#6!

# 1 1 1 "2 $
A - %% 1 8 6 0 &&
%( "1 2 5 8 &)

A(1:2,2:4)

# 1 1 1 "2 $
A ' %% 1 8 6 0 &&
%( "1 2 5 8 &)

A(3,1) A(:,2)

A(1,:) *#5%1% .&'21& &6#%5&# *#,#1& !- "'& A(:,2) ,%#!- .0',$'"& !- "'& ":" !1 *#5%1%&
'#)'$! #" A(1,1:4) '#)'$! !#31 A(1,:) '#)'$& ,%#!- .*#6#%5& !-# &2#1",& &,#1& "'&
.3A(1,1:end)

.0''#(,& 0',$'"$ &$(& *!#5/ *#5(%"$ &(',)%& 8#*$ 0'-,5 *#21! 7*'2
,"%4#6
,&(',)%$ 6'.' ,$'" */!.&

,"%4#6

.6'.' +362'" 3'/+% ,&6#%5& *" 04# &,#1& *" 04 *" 7''(! 8,#( 7'" 0',#)3# ',$'"! *14! '6-2
0'',4#+$ 0#3'%! 0"*&$) 0''!%'+-%& &6#%5& #" &,#1& ,/+% *" &,'9.% *')%#)#" +362'"& ,#)3# :#+$ end *$'*-3
.(*#!#45&

13
Matlab-! "#$%

,&(',)%$ &(',)%-** */!.&

,"%4#6
,,!3+ *$(& *!#5/ '"5 &(',)% **$ &&9 8,5 *$(&

.[] *#3', *#5$#,% 0',4#+ - &3', &(',)% 7#%'+ *$(& '"5 *#6#%5 #" *#,#1% ,)/'&! 04 7*'2
,"%4#6!
,&(',)% ,#('2 &1.%& 01! .&(',)% !1 &,#1 6',#&! #22#(,$ '- .'22

#17 24 1 8 15 $
% 23 5 7 14 16 &
% &
A - % 4 6 13 20 22 &
% &
%10 12 19 21 3 &
%(11 18 25 2 9 &)

,"$& 7/#"$ &2%% ,)/'2 .#22'5$ 7. *"(#% &22'" *'1'!1& &,#1& '- .'22

#17 24 1 8 15$
% 23 5 7 14 16 &
A-% &
%10 12 19 21 3 &
% &
(11 18 25 2 9 )

.&"'41 *56#& !$3** ,&(',)%& *#!#$4! ;#.% "(%2& 8,5$ 1%*1&! &+22 0"
,"%4#6!

"!1 0'16.& 0',$'"$1 8- $.,#* "'& 9" &(',)%& *#!#$4! ;#.% 8,5 $'(&! &+22 0" ,*"9 *%#5!
.0'+/" #'&' #,64#&
,"%4#6!

#17 24 1 8 15 0 $
% 23 5 7 14 16 0 &
% &
A - %10 12 19 21 3 0 &
% &
%11 18 25 2 9 0 &
%( 0 0 0 0 0 2 &)

sort-' find )'/'35 3.1.1

*'(32#/ ,!6.% *,',$ ,#*$ .40',#)3# #" *#(',)% 8#*$ 0'%'#+% 0'-,5 *"'(%! &65#2 find *'(32#/
'!!-& 0#1',& *#(',)% ,#$5 .+/"% 0'2#1& &(',)%$ 0',$'"& !- !1 0'+362'"& *" &,'9.% find
,"#& &'(32#/& !1

.&3', &(',)% &,'9.% &'(32#/& 9" 0'13#$%& 0'-,5& 0'"(%2 "! 0"4

14
Matlab-! "#$%

,"#& 0#1',& 0',#)3# ,#$5#

,*#(',)%! "%4#6
,A &(',)% ,'642

# 0 0 0 3 0$
A - %% 0 0 0 0 0 &&
%( "2 0 0 0 0 &)

.0''#(,& 0',$'"& 8,5 *" 04 ,'9.&! find *'(32#/! *%,#4 '1'!1 )2%#4," */+#&
,"%4#6!

,0',#)3#! "%4#6

a - * 0 0 0 0 8 0 0 "3 0+
index - [5,8]

,a ,#)3#$ +/"% 0'2#1& 0',$'"& !- *" !'-%& ,#)3# !$3! "#& &9 +362'" !1 ',1/" 1#%'1

a _ no _ zero - [8, "3]

15
Matlab-! "#$%

,')3/%#3 ,*#' 0#1',$ #"

*#5(%"$ 0',." 0')2%!" ,#.$! 7*'2 .+/"% 0'2#1& 0',$'" *"'(%! 3, *!$4#% &22'" find *'(32#/
,find-& *'(32#/ !1 0'',4#+$ '#(, '"2* *$'*-

index - [10 11 12 13]

0&',$'"1 8- 0',#)3# 16.% *,6+%& sort *'(32#/ "'& 0'+362'" *!$3! */+#2 *'1#%'1 &'(32#/
.16.% ,#6'+& 5#('$! 1%1%& +362'"& *"# !#64! 65 7)3&% #'&'
,"%4#6!

m _ sorted - * "5 "3 0 1 5 7 93+


index - * 4 3 6 5 1 7 2+

)""#"/%' )"/&%/0&( 3.2

,' 7%'+& *#5(%"$ *5(#$% (&(',)% *#6#%5# *#,#1 7'$ :#!'.) 5transpose *!#5/

# 2 3$
C - %% 1 8 &&
%( 5 4 &)

"*","-","+" 0',#),/#"& *#5(%"$ *#(',)% !/-# ,#+'. ,,#$'. !1 *#'+'+$ *#!#5/ 5($! 7*'2
.*',"2'!& &,$4!"& '3#.! 0"*&$
,*#(',)% ,#$'.

#10 6 6 $
C-% &
( 3 17 6 )
,*#(',)% !/-

#16 37 8 $
C - %% 8 77 17 &&
%( 40 61 13 &)

,&(',)%$ ,!3+ !/-

transpose 5($! '6- 8" ,5'/1% &9 7'" *#'1%% *#(',)% ,#$5 .'+3!/%#3 6#%( 04# transpose 5($% ' 1,4& ,#),/#"5
.(.') #'2/! &6#32 05 1,4& ,#),/#"$ 0'1%*1% '+3!/%#3 6#%( "!!

16
Matlab-! "#$%

#16 10 2 $
C-% &
( 0 18 4 )

&!#5/ *',"2'! &,$4!"$# ,."% Matlab-$ *'3#. &22'" 0'2#1 0'6%% *#!5$ *#(',)% '*1 7'$ &%'-+
&,3%$ .,!3+! &(',)% 7'$ &%'-+ -6'.' !!-&% "(#' &,3% 0''3 Matlab-$ !$" .*,64#% &22'" #9-
.&(',)%& ',$'" !- !5 &%'-+! &!#31 !"2& &!#5/& &9-
,"%4#6!

# 7 4 0$
C-% &
( "1 8 1 )

!-((%.$-/ 3.2.1

.det *'(32#/ *#5(%"$ *'5#$', &(',)% !1 &)22'%,)6& *" $1.! 7*'2


,"%4#6!

ans - "5

!#%$-. 6'5%! 3.2.2

,*#2#1 0'-,6 '*1$ A[n , n] 6*'5#$', &(',)% 8#/&! 7*'2


.^-1 ,#),/#" #" inv *'(32#/ .1
,"%4#6!

#"

,#*"(#*1 &(',)% *-'/& 0*',#4!" #*#" *" 0'!'5/%


# "0.6 0.4 $
A-% &
( 0.8 "0.2 )
.&6'.' *(',)% *#5(%"$# / ,#),/#" .2
,"%4#6!

. det(A) / 0 -1 '"2*$ 6

17
Matlab-! "#$%

!6&1,! /&%/0&( 3.3

.0'-,5%! *#(',)%& !1 *#5%1%& *" *#21% &15%! &6#32& ,#),/#" *#5(%"$ *#5(#$%& *#!#5/
.,$'" !#% ,$'" &!#5/ -0'-,5% '21 7'$ &!#5/ "'& *#(',)% '*1 7'$ &6#32 *!#5/ ,%#!-
,*#'!!- *#(',)% '*1 ,'642 &1.%& 75%!

# a11 a12 a13 a14 $


A ' %% a 21 a 22 a 23 a 24 &&
%( a 31 a 32 a 33 a 34 &)
# b11 b12 b13 b14 $
B ' %% b 21 b 22 b 23 b 24 &&
%( b31 b32 b33 b34 &)
,!/-
# a11 ! b11 a12 ! b12 a13 ! b13 a14 ! b14 $
C ' A.* B ' %%a 21 ! b 21 a 22 ! b 22 a 23 ! b 23 a 24 ! b 24 &&
%( a 31 ! b31 a 32 ! b32 a 33 ! b33 a 34 ! b34 &)
,3#!'.
# a11 b11 a12 b12 a13 b13 a14 b14 $
C ' A./ B ' %%a 21 b 21 a 22 b 22 a 23 b 23 a 24 b 24 &&
%( a 31 b31 a 32 b32 a 33 b33 a 34 b34 &)
,&39.
#a 11
b11
a12 b12
a13 b13
a14 b14
$
% &
C ' A. ^ B ' %a 21
b 21
a 22 b22 a 23b23 a 24 b24 &
%a b31
a 32 b32 a 33b33 a 34 b34 &)
( 31

*#,5&
.*'2#$1.& &!#5/& ,#),/#" '2/! 03#%% &6#32& ,#),/#" -
.&2#1 "'& &6#32& ,#),/#" "!! 3#!'.&# !/-& *#5%1% *#'5#$', *#(',)% ,#$5 -

,,*#'% &6#32& ,#),/#" 0,#$51 0',3% ,/+% 0'%''3


.,#+'. #" ,#$'. .1
.(,!3+$ 3#!'. #" !/- *#5(%"$ 5#('$! 7*'2) 5#$3 #*#"$ &(',)%& ',$'" !- 3#!'. #" !/- .2

!/'3(! $'-$5'"2 1%&'.%& 3.3.1

,#),/#" 9" (5#$3& "#& b #" 5#$3& "#& a 0" 7'$) 5#$3 ,/+% 05 ( a b ) &39.& *!#5/ *" 5($! '6-
.'.,-& &6#32&
,"%4#6

y - * 2 4 8 16 32 64 128+
,"%4#6

18
Matlab-! "#$%

y - *1 4 9 16 25 36 49+

.*#"!#! !- "!! 0'-$#+% 0'')%*% 0''#)'$ $1.! 7*'2 &6#32& ,#),/#" *#5(%"$

,"%4#6
5
,7*$'*- '"5 5(#$% s ' 2 x x '#)'$& $#1'.
x '1

s - 3413

,"%4#6
'#)'$& *" $1.! 1,62# A,B,C ('5#$', .,-&$ "!) !6#4 #*#" *#!5$ *#(',)% 3 *#2#*2
3
3 sin(A) 4
.f ' 5 6 .1
7 B^C 8

,1#%'%&

0'$, *#4#9! 3#39 '#)'$ *#$, 0'*'5! .0'',4#+& *''5$ "'& Matlab-$ *#(#/2 '-& *#'5$& *."
"#& ,*#' ,#%. &,3% .&"'41 *56#& !$3**# ,4#+ +/+/! *#!3$ 7*'2 7-!# 0'',4#+ !1 0'!$!$%#
$1#.' 8" &"'41 *56#& !$3** "! 9"# 7#-2 "! 0#3'%$ ,4#+ #2$(& 8" 7'3* 0',4#+& ,/+% ,1"-
.'#(, "! '#)'$

7'" 0" .,4#+! *.*% 7%+& *99& '"5 0'',4#+& *''5$ !5 ,$4*&! 7*'2 Editor\Debugger 7#!.$
'*.* #3 ,''#(% 9" 0'!1% ,4#+ #! 0''3 0" .#-,6 '3/#" #3 )),#1% *'2%9 9" 0'!1% ,4#+ ,4#+!
.#!1 0'!1%& ,4#+&# 7%#+%& ,4#+! *.*%

.3.4 3,/! #2/ 0'),/! .,#)3# ',$'" !5 &%'-+ *5($% sum *'(32#/7

19
2 !"#$!
:!"#$!! %"&'(
2 ................................................................................................ !"#$ %&'#( )$'*+,$" 3.4
3 ........................................... )$*'-.&$ /'-$.+$ )!"#& )$0*&12 /'&$#( !3 /'4$*'' 3.4.1
4 ............................................................................................ prod !"# )'*+,$" 3.4.2
4 ....................................................................................................... )$*'-.& !$"#3 3.5
7 .........................................................................................................)$*'-.& 5$"'% 3.6
7 ................................................................................. )'-1,'! )$1$$3& )#-0& )-')" 3.7
8 .............................................................................................. /',)3& ),'0.$ )-'&3 3.8
8 ................................................................. Command Window-! 06'& )4*%$ )!2+ 3.9
8 ........................................................................................................... )$7$-8& 3.9.1
9 .................................................................... Command Window-2 06'& )4*% 3.9.2
10 ............................................................... Command Window-%& 06'& )!2+ 3.9.3
11 ................................................................................................................... 06'& ',2& .4
11 ..................................................................................... /''6&&-2- /''-&$, /'#-0& 4.1
12 ............................................................................................. 5-0& '6&& '$,'3 4.1.1
14 ...................................................................................... (cell arrays) /'1) '#-0& 4.2
17 ................................................................................................... Structure '#-0& 4.3
18 ................................................................................... structure '#-0&! %3'4 4.3.1
Matlab-! 1$2&

!"#$ %&'#( )$'*+,$" 3.4

,%&'#( )$'*+,$" ')3 )$&''+


sum )'*+,$" .1
,-$.+$% '-2'1 /$#( )1 !'#&% -!+( %-'78&$ -$.+$ )!2+& $7 %'*+,$"
n
s ! sum(x) ! " x i
i !1
,1&4$6!

s # 10

%1*$)% )-78%$ %*'-.&% !3 )$6$&0% 5-$1! %&'#( '"0 )$*'-.& !0 )!0$" $7 %'*+,$"
.)$6$&0% -"(&# $#-$13 -$.+$!
,/'12% /'#-0% )1 %-'78& sum )'*+,$" X $ m & n % %*'-.& -$20 '!!# 9"$12

' x11 x12 x13 ! x1n (


)x x 22 x 23 ! x 2n **
X ! ) 21
) " " " " *
) *
)+ x m1 x m2 x m3 ! x mn *,

'm m m
(
s ! sum(X) ! ) " x i1 " x i2 ! "x in *
+ i !1 i !1 i !1 ,

,11&4$6!

s # $15 18 21 24%
,/$3-, X %*'-.&% !3 )$-$3% '," !0 /$#(! '6#

,!2+)&
s # $10 26 42%

cumsum )'*+,$" .2
$)$12 -$.+$ %-'78&$ -$.+$ )!2+& $7 %'*+,$" .-2.*&% /$#(% )'*+,$" 1'% $7 %'*+,$"
.-2.*&% /$#(% 5-0 2*$& -2'1 !#2 -31# 5-$1
,/'12% /'#-0% )1 %-'78& cumsum )'*+,$" x $ n & 1% -$.+$ -$20 '!!# 9"$12

' 1 2 n
(
s ! cumsum(x) ! ) " x i "x i ! " x *, i
+ i !1 i !1 i !1

.6$+2 )$-'%2 /3!$ %6!+% )$8$, /3! 60$, %7 -$.-"$1 .)$6$+,% 3$!3 -$.-"$1 )-702 )-64$& %*'-.&% $7 1&4$621

2
Matlab-! 1$2&

,1&4$6!

s # $1 3 6 10%

)-78%$ %*'-.&% !3 )$6$&0% 5-$1! -2.*& 9"$12 %&'#( '"0 )$*'-.& !0 )!0$" $7 %'*+,$"
..!+% )*'-.& $&# !6$4 %)$12 %*'-.&! %1*$)%
,)'!!# %*'-.& -$20

' x11 x12 x13 ! x1n (


)x x 22 x 23 ! x 2n **
X ! ) 21
) " " " " *
) *
)+ x m1 x m2 x m3 ! x mn *,

,/'-2.*&% /'&$#(% )*'-.& )1 %-'78& cumsum )'*+,$"

' 1 1 1
(
) " x i1 " x i2 ! "x * in
) i !1 i !1 i !1
*
) 2 2 2
*
"x
S ! cumsum(X) ! ) i !1 i1
"x i2 ! " x in *
) i !1 i !1
*
) " " " *
)m m m *
) x ! " x in *
)+ "
i !1
i1 "x
i !1
i2
i !1 *,

+'#%$,-' *%$',.' +/01- +'2#-"3 *%-'14 /& *%)'#%% 3.4.1

,)$1&4$6 ')3 9!%! .)$*'-.&$ /'-$.+$ )!"#& )$0*&12 /'&$#( 4*''! 9)',
/'-$.+$ ',3 9'2 )'-!+( %!"#& .1
,9$),
$ n &1% /'-$.+$ – a, b
' b1 (
n )b *
a, b ! " a i . bi ! $ a1 a 2 ! a n % . ) 2 * ! a- / b
i !1 ) *
) *
+) b n ,*
)'0$2'- )',2) .2
,9$),
$ n &1% -$.+$ – x
$ n & n % %*'-.& – C
n n
f ! "" x i cij x j ! x - / C / x
i !1 j!1

3
Matlab-! 1$2&

prod /01 +%#.('0 3.4.2

,-$.+$% '-2'1 )!"#& )1 !'#&% -!+( %-'78&$ -$.+$ )!2+& $7 %'*+,$"


n
p ! prod(x) ! 0 x i
i !1
,1&4$6!

p # 24
.sum )'*+,$" )!$0" 9"$1! %%7 )$*'-.& !0 prod )'*+,$" )!$0" 9"$1

)$*'-.& !$"#/ 3.5

,/'-!+( $1 /'-$.+$ ,)$*'-.& )$!"#3&% )$'*+,$" 3$!3 )$&''+

repmat +%#.('0 3.5.1

,1$% repmat )'*+,$" !3 '!!#% %,2&%


repmat(X, r, c)

X %*'-.&%3 /'&0"% -"(& 1$% r .!$"#3! 60$'&% -!+( $1 -$.+$ ,%*'-.& 1'% X -31#
.)$6$&0% 9$$'#2 !"#$3) X %*'-.&%3 /'&0"% -"(& 1$% c-$ )$-$3% 9$$'#2 !"#$3)
,-!+( !$"#3! 1&4$6

' 2 2 2 2(
Z # )) 2 2 2 2 **
)+ 2 2 2 2 *,
,12% 9"$12 /4 )17 %!$0" 0*2! 9)',

,-$.+$$ !$"#3! 1&4$6

'5 9 1 7 2 5 9 1 7 2 (
Z # ))5 9 1 7 2 5 1 9 7 2 **
)+5 9 1 7 2 5 1 9 7 2 *,

,%*'-.& !$"#3! 1&4$6

'5 0 5 0 5 0(
)0 5 0 5 0 5 **
Z#)
)5 0 5 0 5 0*
) *
+0 5 0 5 0 5,

4
Matlab-! 1$2&

meshgrid +%#.('0 3.5.2

, Y )$*'-.&! x, y /'-'*% )1 /'4*'&% /'-$.+$ 4$7 )-&% 1$% $7 %'*+,$"2 '-+'0% 3$&'3%
./''6&&-)!) /'"-4 )-'*' )$6$+" )1 )$3&3&$ /'-'*% )1 )$4*'&% X
,)$6$+"%
x ! $ x1 x 2 x 3 x 4 % ;
y ! $ y1 y2 y3 % ;
[X, Y] ! meshgrid(x, y);
,)$*'-.&% )1 )-*$'
' x1 x2 x3 x4 (
X # )) x1 x2 x3 x 4 **
)+ x1 x2 x3 x 4 *,
' y1 y1 y1 y1 (
Y # )) y 2 y2 y2 y 2 **
+) y3 y3 y3 y3 ,*

/''$,'3 /'&''+ Y %*'-.&2 $!'1$ 'x -'*' 9$$'#2 +- /''$,'3 /'&''+ X %*'-.&2 '# 2! /'3,
.'y -'*' 9$$'#2 +-
,1&4$6!

'1 2 3 4 (
X # ))1 2 3 4 **
)+1 2 3 4 *,
'1 1 1 1 (
Y # )) 2 2 2 2 **
)+ 3 3 3 3 *,

.%'*+,$"% !3 .!+2 681 -$.+$ +- /$3-, /'%7 y -'*$ x -'* -$*'! $,,$*-2 /1
,1&4$6!

'2 4 6 8(
)2 4 6 8**
X2 # )
)2 4 6 8*
) *
+2 4 6 8,
'2 2 2 2(
)4 4 4 4 **
Y2 # )
)6 6 6 6*
) *
+8 8 8 8,

./''6&& )!) /'8.3& .$.-32 96% 6.2 +-"2 .$-'" -)'2 -2($' $7 %'*+,$"2 3$&'3%

5
Matlab-! 1$2&

kron +%#.('0 3.5.2

.)$*'-.& ')3 9'2 '-$7,. !"# )0*2& $7 %'*+,$"


!#2 X %*'-.&2 -2'1 !# )!"#& 1'% X[m1&n1] , Y[m2&n 2] )$*'-.& ')3 9'2 '-$7,. !"# )$0&3&
. '+1 m1. m2 2 & 1 n1. n2 2 (, !6$42 %368 %*'-.&2 $!1% )$!"#&% !# -$6'($ Y %*'-.&%
,1&4$6!
'1 2 (
X!) *
+3 4 ,
'1 1 1(
Y ! ))1 1 1**
)+1 1 1*,
' '1 1 1( '1 1 1( ( '1 1 1 2 2 2(
) ) *
)1. )1 1 1* 2 . ))1
* 1 1** * )1 1 1 2 2 2 **
)
) )+1 1 1*, )+1 1 1*, * )1 1 1 2 2 2*
K ! kron(X, Y) ! ) *!) *
) '1 1 1( '1 1 1( * )3 3 3 4 4 4*
)3 . )1 1 1* 4 . ))1
* 1 1** * )3 3 3 4 4 4*
) ) * ) *
+) +)1 1 1,* +)1 1 1,* ,* +)3 3 3 4 4 4 *,

. '+1 2 . 32 & 1 2 . 32 (, /'6&&% )!02 1'% K %*'-.&$ X[2&2] , Y[3&3] $7 1&4$62

./'681 )*'-.& $1 -$.+$# X )0'2+ '"0 )$*'-.& !"#3! 3&& 9)',

6
Matlab-! 1$2&

)$*'-.& 0$"'% 3.6

,)$*'-.& 5$"'%! )$'*+,$" ')3 )$&''+


(flip left-right) fliplr )'*+,$" .1
,)$6$&0% -6( )1 )#"$% $7 %'*+,$"

' 4 3 2 1(
X _ lr # )) 4 3 2 1**
)+ 4 3 2 1*,
(flip up-down) flipud )'*+,$" .2
,)$-$3% -6( )1 )#"$% $7 %'*+,$"

' 3 3 3 3(
Y _ ud ! )) 2 2 2 2 **
)+ 1 1 1 1 *,

)'-1,'! )$1$$/& )#-2& )-')" 3.7

,/'&!0, n-$ )$1$$3& m !3 %#-0& %,$),


6a11x1 5 a12 x 2 5 # 5 a1n x n ! b1
3
3a 21x1 5 a 22 x 2 5 # 5 a 2n x n ! b 2
4
3"
37a m1x1 5 a m2 x 2 5 # 5 a mn x n ! b m

,)'*'-.& %-$*2 %2')#! ),)', $7 )$1$$3& )#-0&


A.x ! b

)'0$2'- %*'-.& 1'% A %*'-.&% 71 1 m ! n 2 %%7 /'&!0,% -"(&$ )$1$$3&% -"(& $23 %-+&2
,12% 9"$12 inv )'*+,$" )$0*&12 !",% )$1$$3&% )#-0& )1 2-$)"! 9)',

,pseudo-inverse )1 238! 5-$* 3' 1 m 8 n 2 '0$2'- 1!% %-+&% -$20

,12% 9"$12 \ -$.-"$1% )$0*&12 1'% !",% )$1$$3&% )#-0& )1 -$)"! %"'60 5-6

.%#'"% A %*'-.& -$202

7
Matlab-! 1$2&

3',)/& ),'2.$ )-'&/ 3.8

,12% 9"$12 save %'*+,$"2 3&)3, workspace-2 /',)3&% !# )-'&3

.Workspace-2 /',)3&% !# )1 !'#&% name.mat :2$+ )-*$' $7 %'*+,$"


:2$+ /3 -81! %-'&3! /',)3&% )$&3 )1 ;'($,$ /6$+&# /$3-, /''"'*"( /',)3& +- -$&3! '6#
.mat-%
,0*2, name /32 .mat :2$+! x yy Z /',)3&% )1 -$&3! '6# !3&!

/',)3& +$8&! '6# .clear %'*+,$"2 3&)3, %6$20% 28-&2 $,-*'3 /',)3&% !# )1 +$8&! '6#
.%+'8&! /',)3&% )$&3 )1 9''*,$ clear %'*+,$"2 3&)3, /''"'*"(
,0*2, x Z /',)3&% )1 +$8&! '6# !3&!

,12% 9"$12 load %'*+,$"2 /'3&)3& %6$20% 28-&! mat :2$+& /',)3& 9$0.! $,,$*-2 /1

.(Current Directory) )'8#$,% %''-"(% 9$!82 '$*-% .mat :2$+ !0 %!$"# %*'8! .$3" $1

Command Window-! 24'& )5*%$ )!6+ 3.9

+'5'$6- 3.9.1

..(+. )4*%! )$7$-8&2 3$&'3% 1$% -$-2 9"$12 06'& )4*%2 '7#-& '!#
.(+.# )-64$& )7$-8& .6'8' $) 1$% $2 -2'1 !#3 -$.+$ %30&! 1'% Matlab-2 (String) )7$-8&
.$'66* ',3& /''3-4 !02
,1&4$6!

,'# 2! /'3, .(/'$$) 5-0&) char array 4$(& 4 5-$12 -$.+$ -*$'

s(2) 9 a
fliplr(s) 9 eman

.)$7$-8& !3 %*'-.& -$*'! !3&! 9)', 9#! .Matlab-2 5-0& !#! %&$62 5-0& 1'% )7$-8&
,1&4$6!

s2(1,:) 9 name
s2(2,:) 9 eman

8
Matlab-! 1$2&

%*-, /1 51 .%*'-.& 9%& -$*'! %'02 %)'% 1! 9#!$ 5-$1 $)$1 )$!02 $'% )$7$-8&% ')3 !",% %-+&2
)'*+,$" /0 $7 %'02 !0 -24)%! 9)', .%1'43 )06$% !2+, /',$3 /'#-$1 )$!02 )$7$-8& (',#%!
.str2mat

,)2')# 1&4$6!

,Command Window-2 !2+)&$ /'$$) 5-0& !3 $3 & 5% %*'-.& -*$'

.-)$' )$-*+% )$7$-8&% ;$(2 /'8$$- )-*''& $7 %'*+,$"

.deblank )'*+,$"2 $3&)3% 8$$- 1!! %*'-.&%& )7$-8& ;$!3! '6#

Command Window-3 27%- +)#! 3.9.2

,Command Window-2 /',)3& !3 /'#-0 )4*%! /'#-6 -"(& 9,3'


.)-6$(&$ %"' )$8" '#% 51 ,%.$3" '#% 5-6% '%$7 .";" )1 .'&3%! .1
.disp )'*+,$" .2
.fprintf )'*+,$" .3

.%!3 )$!$40% /''-4$(2 )1*&,% )7$-8&% )1 %4'*& disp )'*+,$"


,%6$+"% )2')# 1&4$6!

,.(+.% )1 Command Window 9$!82 %4'*&

)$1*$) )4*%2 !3&!) /''-"(& /',)3& '#-0 )4*%$ 0$2+ .(+. !3 2$!'32 5-$* 3' /'2- /'-+&2
,62!2 )$7$-8& 4'*%! )$!$#' disp $&# .(+. )4*% )$'*+,$"$ -81& .(;-4 !3 )-)$#2 $1 %6'6& !3
.)$7$-8&! /'-"(&% /'#-0% )1 -'&%! num2str )'*+,$"2 /'3&)3&
,1&4$6
.Command Window-2 )17 4'*%! /'*$- $,1$ &"( 9 1$% .+''2$1 5-$1 '# !2+)%

,4*$& Command Window-2

.;($, .,&$4-1 )"($% '"0 )7$-8&! )$-&$&% 3)$-"(% -"(&2 .$!3! /4 9)',

.)',$-30% %6$+,% ',"! )$-"( !!$#3

9
Matlab-! 1$2&

,1&4$6!

9#! .5 1$% )7$-8&! $-&$'3 )$-"(% -"(& '# 02+ %7 .,&$4-1 .5 5-0 !02 ;($, .,&$4-1 $,"($%
,Command Window-2 0'"$& 12% .!"%

%%$24 %.'!3 )-3"1& 51 )2#-$& -)$' )'(8' $7 %'*+,$" .fprintf 1'% disp-! )'2'.,-.!1 %'*+,$"
.Matlab !3 %-702 $3"8 /'.-"! .%4$*)2 -)$'

Command Window-!- 27%- +/3. 3.9.3

input )'*+,$" )$0*&12 Command Window 9$!82 3&)3&%& %,)3& !3 '-"(& 5-0 !2+! 9)',
,12% 9"$12

6'!+' 3&)3&%3 %#8&$ /''-4$(2 )1*&,% )7$-8&% )1 Command Window-2 %4'*& $7 %'*+,$"
.x /32 %,)3&! -82 3&)3&%3 5-0% (,#$& ,%-$+ %73 04-2 .Enter !0 :8!'$ 5-0

,12% 9"$12 %,)3& 5$)! )7$-8& /4 (',#%! 9)',

!"#$%
..(+.2 %-$3 )6-! '6# \n ;'($%! 9)', -
$7 %'*+,$" .pause )'*+,$" 1'% 3&)3&%& )$-'3' )$1-$% )!2+&% )"($, %'*+,$" -
.$%3!# -$)"# !0 :8!' 3&)3&%3 60 %*-%% )1 %%3&

10
Matlab-! 1$2&

27%- %(3- .4
,/'12% 06'&% ',2& '4$( /'&''+ Matlab-2 .)$7$-8&$ )$*'-.& $&# 06'& ',2& -"(&! $,"38, %)0 60

Matlab $% &'() (*+) (#,- :4.1 !"#

/''6&&-$6 /''-&$, /'#-0& !3&! :/'#-0& /% Matlab-2 06'&% ',2& !# 4.1 ;-4& )$1-! 9)',3 '"#
/'4$( -"(& -'#%! 6&!, %7 +-"2 .(character array) /'$$) !3 5-0& 9% )$7$-8&$ )$*'-.& /'1-+,
./'"($, 06'& ',2& !3

3''4&&-6- 3''-&$, 3'#-2& 4.1

!3 ;($1# '6&& )!) 5-0& !0 2$38! 9)', ./'6&& ',3& -)$' '!02 /'#-0& -$*'! '$*- )$2- /')'0!
)$*'-.& 3$!3& )2#-$& RGB ),$&) ,1&4$6! .4.2 ;-42 -''$*&3 '"# %',3% '-81 )81 )$*'-.&
/'#-0& !0 2$38! 9)', .)',$02* %,$&)2 !$8#$ ,+$-' ,/$61% )&*$0 )1 )$,''*&% (92$&# !6$4 $)$12)
5-0& )$$%& -682 %-$.-"&. !3 )$'6&& )!) )$6'6& 1&4$6! ,'6&& 2- 06'& -$)2 /4 /''6&&-2-
.'6&& )!)
zeros, ones, rand, $&# )$'('(2 %*'-.& )-'*' )$6$+" )$0*&12 /''6&& 2- /'#-0& -$*'! 9)',
.4/'.,&$4-1 ',3& -)$' )2*% '"0 randn
,%6$+"% 1&4$6!

A(i,j,k) -2'1% !0 2$38! 9)', .("1 /#-03 /'-2'1 3 . 4 . 5 ! 60 !02 5 !0 4 !0 3 5-0& )-*$'
.k %*'-.&2 (i,j) -2'1 !0#

,1$% A !3 !6$4%

.cat )'*+,$"2$ repmat )'*+,$"2 3&)3%! /4 9)',4

11
Matlab-! 1$2&

,)'"-4 %38&%

/(-01 $% 5 $& 4 $& 3 ."&) :4.2 !"#

)$!0$" '('(2 9"$123 )$'*+,$" 9,3' ./''6&& 2- /''-&$, /'#-0& !0 )$2- Matlab )$6$+" 0*2! 9)',
.(sin )'*+,$" !3&!) -2'1 -2'1 -&$!# ,/'-!+( !0 )$!0$" $1 (sum )'*+,$" !3&!) /'-$.+$ !0
.9)!$0" )1 0*2! 6&& %7'12 9%! $,''*'3 )$#'-* /'-$.+$ !0 )$!0$"% )$6$+"
,12% 9"$12 d %&'#( 6&&$ A '6&&-2- 5-0& !2+! %!$#' sum )'*+,$" !3&!

.4.2 ;-42 )$*'-.&% !0 %&'#(% %30&! 1$%3 $3 & 4% %*'-.& 2',' sum(A,3) ,!3&!
./''6&&-$6$ /''6&&-68 /'#-0& !0 $!0"3 9"$1 $)$12 )$!0$" -2'1 -2'1 )$!0$"% )$6$+"
,!3&!

.A 5-0&! %%7 !6$42 5-0& )-*''&

8$2- %7-- %'(%& 4.1.1

/&*0 /'6&&% )1 )$,3! $1 permute )'*+,$" )$0*&12 5-0& !3 /'6&&% -6( )1 )$,3! 9)',
.reshape )'*+,$" )$0)&12

/'6&&% -6( '$,'3

-% )!$0" !3 %28-% %30&! 1'% $7 %'*+,$" ./'6&&% -6( )"!8% )-3"1& permute )'*+,$"
./''6&&-2- /'#-0&! transpose

5-0&% !3 /'6&&% -6( )1 5$"%! '6# . [7 & 13 & 14 & 8] !6$42 B '6&& 2- 5-0& 9$), '# 8',, !3&!
,/$3-, C [7 & 8 &13 &14] -!

12
Matlab-! 1$2&

/'6&&% '$,'3

$'6&& )1$ 5-0&% )1 .!+# )!2+& $7 %'*+,$" .5-0& !3 /'6&&% )1 %,3& reshape )'*+,$"
%&'46% -6( .%1'43 )06$% )!2+)& .!+2 5-0&%& %,$3 368% 5-0&2 /'-2'1% -"(& /1 ./'368%
.-6(% '"! /'6&&% -13$ )$-$3 ,)$6$&0 1$% .!"% 5-0& !3 368& -$6'(% -6($ .!+2 5-0&% !3
,1&4$6!

' 1 4 7 10 (
A ! )) 2 5 8 11**
)+ 3 6 9 12 *,
:
' 1 3 5 7 9 11(
A!) *
+ 2 4 6 8 10 12 ,

./''6&& 2- /'#-0& -$*'! /4 9)', $7 %'*+,$" )$0*&12


,1&4$6!

5 $& 4 $& 3 ."&) :4.3 !"#

+'-.

!3 368& -$6'($ %&'46 -6( $)$12 5$-1 681 %6$&0 -$.+$! '6&& 2- '-&$, 5-0& !# 5"$% A(:)
-*$, 1$% $,&&3 '6&& 2-% 5-0&% )1 $7 %.'32 -*$,% -$.+$& -783! 9)', /4 9#! .reshape )'*+,$"
.reshape )'*+,$" )$0*&12

,1&4$6!

.4.3 ;-42 -1$)&% A '6&& 2- 5-0& $)$1 )1 2',&

13
Matlab-! 1$2&

(cell arrays) 3'1) '#-2& 4.2

./',$3 /'!64$ /'($"'.& /',)3& 9$(#1 -3"1& cell 5-0&


%*'-.&! %&$6% /$3'- '"0 -*$, /'1) 5-0& .5-0& !'#&% 1) 1$% $2 -2'1 !#3 5-0& 1$% cell 5-0&
.)$02$-&2 /$+&2 5)$!(!$(& /''-4$(2 3$&'3 .0&!
,1&4$6!

,12% [2 & 2] !6$42 cell array -*$'

,9% /'1) 5-0& 4'*%! )$"($, /'#-6 ')3


.1) !# .$-'" %4'*&% celldisp )'*+,$" .1
,1&4$6!

,Command Window-2 -*''&

.{} 9&$(& +'- /'1) 5-0&5

14
Matlab-! 1$2&

./'1)% 5-0& '2'#- !3 '"-4 4$*'' )..-3&% cellplot )'*+,$" .2


,1&4$6!

,;-4% )1 )-*''&

string 7

C /(12 ."&) $% #,3(( :4.4 !"#

%,3' 1! /'-81% /'#-0&% '$,'3 9#!$ /'-81 /'#-0& !3 /'+)$0 +- !'#& /'1) 5-0& '# -$#7! 2$38
./'1)% 5-0& )1
.C /'1)% 5-0& )1 %,3& $,'1 A %*'-.&% '$,'3 !",% 1&4$62 !3&!

$7 %,$#) .5-0& $)$12 /',$3 /'4$(& /'.+''2$1 !'#%! /)!$#' 1'% /'1) '#-0&2 '7#-&% 9$-)'%
/''-&$, /'#-0&2 '-3"1 ')!2 -26 ,%,)3& !6$42 )$*'-.& -$&3! 5-$* 3' -31# ,!3&! ,)'3$&'3
./''6&& 2-

/''-4$(2 /'3&)3& )0#3 +- %*'-.&2 /'-2'1! %3'4! %&$6 /'1)% 5-0&2 -2'1 !#! %3'4%
.)$!(!$(&
,1&4$6!

15
Matlab-! 1$2&

,/$3-,$ /'(+6,'1% )1 -3-3, (1,1) 1)2 (2,3) -2'1! )34! '6#

,12% 9"$12 )0*$2& /'1) )+'8&

,1&4$6!

,1$% C 5-0&% )0# .%',3% /'1)% )6$&0 )1 +8$&

.6cell )'*+,$" )$0*&12 0*$2& )$+'- )$*'-.&2 /'1) 5-0& !$8)1


,%6$+"% 1&4$6!

,)$+'- )$*'-.& /'!'#&% /'1) !3 [3 & 4] 5-0& )-*$'

./'.,&$4-1 6$0 )"($% '"0 /''6&&-2- /'1) '#-0& -$*'! 9)', $7 %'*+,$" )$0*&12
,1&4$6!

. [3 & 4 & 5] /'1) 5-0& -*$'

.cat )'*+,$" )$0*&12 1'% )"($, 5-66

16
Matlab-! 1$2&

Structure '#-2& 4.3

/'!64$ /'($"'.& /',)3& !3 9$(#1 /'-3"1& /% /43 5#2 cell '#-0&! /'&$6 structure '#-0&
1!$ )$&3 !3 )$63 5$)2 /',(#$1& structure-2 /'#-0&% ,cell '#-0&! 6$4',2 51 ./',$3&$ /',$3
.06'& !3 %,$3 4$( !'#%! !$#' %63 !# .5-0&2 /'-2'1 5$)2
,1&4$6!
patient

.name ‘John Doe’

.billing 135.00

120 39 78
.test 98 154 12

structure ."&)$ 1)#,' :4.5 !"#

,/',"$1 ',32 )'30, structure '#-0& )'',2


,1&4$6! .%2*% )$0*&12 .1

,12% patient /32 $1&1% !6$42 structure 5-0& )1 2',&

,12% 9"$12 5-0&% )1 2'8-%! 5'-* 8$+! 6$0 -$*'! '6#

, $1& 2% !6$4! 28-$% patient 5-0& )0#

17
Matlab-! 1$2&

,'!!#% /$3'-% .struct )'*+,$" )$0*&12 .2


#

,/$3-, $1& 2% !6$42 patient 5-0& $)$1 )1 -$*'! '6# 1&4$6!

structure %1$2-/ !&%) 4.3.1

,12% structure-% 5-0& 9$), '# 8',,


patient array

patient(1) patient(2) patient(3)

.name ‘John Doe’ .name ‘Jane Doe’ .name ‘Elvis’

.billing 135.00 .billing 112.00 .billing 135.00

120 39 78 163 67 71 52 43 121


.test 98 54 12 .test 13 18 191 .test 156 12 79

. [1& 3] $',#+ structure ."&)$ 1)#,' :4.6 !"#

.()$63% !#! /'#-0 !'#&%) structure 5-0&2 -2'1 !#! )34! 9)',
,1&4$6!

,%63% /3$ %6$+, $"'($% '"'*"( %63! )34! '6#


,1&4$6!

18
Matlab-! 1$2&

./'1)& (+6,'1 $"'($% '"'*"( %63 5$)2 -2'1! )34! '6#


,1&4$6!

.'$*-% %63% /3$ ,%6$+, ,5-0&% /3 )1 )$02$-& /''-4$(2 /$8), /!3 %63 !2+! '6#
,1&4$6!

,1$% %2 '!!#% 3$&'3% 9"$1 .rmfield %'*+,$"2 3&)3, structure-& %63 +$8&! '6#

:$8 struct_array1-2 /,3'3 )$63% !# )1 !'#&% struct_array2 /32 368 structure 5-0& -64$&
.field %63&

,1&4$6!
,%12% %6$+"% )1 0*2, patient 5-0&& billing %63 )1 6'-$%! $,,$*-2 /1

19
Matlab-! "#$%

3 !"#$!
:!"#$!! %"&'(
2 ........................................................................................................... -)&%%-#& '()*+, .5
3 ........................................................................................................... plot -).(/#* 5.1
3 ....................................................................................... -#&#(/ !1 2+, 0#0+1 5.1.1
6 ........................................................................................... 3)##( !1 2+, -+).) 5.1.2
8 ................................................................................. 3)4(!*%#( 3)+*4% 0#0+1 5.1.3
10 ............................................................................. &&#$ 2+,$ -#).(/#* +*4% 0#0+1 5.2
10 ................................................-#).(/#*' )+#0(# -" '!)5% '.)+0%, +#0(# x +). 5.2.1
11 .............................................. -#).(/#*' )+#0(## x )+). -" 3),.)% 3)+#0(# )&%. 5.2.2
13 ................................................................................................. hold -).(/#* 5.2.3
14 ............................................................ 3#1)+ -#+#. !1 -#/#+46# -#/#+-) 3#5)4 5.2.4
15 ............................................................................................ 3)*+,! 3)/#%)4 -*4#' 5.3
16 ..................................................................................... 3)&6#)% 3)/%)4 3#1)+ 5.3.1
16 ..................................................................................................-+-#5 -*4#' 5.3.2
16 ................................................................................ y-# x +).! -#+-#5 -*4#' 5.3.3
16 ................................................................................................... 04(0 -*4#' 5.3.3
18 ........................................................................................... "+(% -$)- -*4#' 5.3.4
18 ................................................................................ -)4)4$ '()04)004 -*4#' 5.3.4
19 .......................................................................................... 3).6# 3)##( -*4#' 5.3.5
19 ............................................................................ 3)*+,! 3)/#%)4 -*4#' 3#5)4 5.3.6
20 ............................................................................................ 3)*+, !1 -)/&) '5)+7 5.4
22 ............................................ -#&#(* -#7.%"$ 3))*+, 3)0()$#" !1 -#/#5- -7)$( 5.4.1
23 .......................................................................................................... fill -).(/#* 5.4
24 ..................................................................................................... plotyy -).(/#* 5.5
24 ................................................................................. 3)*+,'% -#+)1) 3)5+7 -")+( 5.6
25 ..................................................... subplot -).(/#* -#7.%"$ &6) 3)*+, +*4% -%1' 5.7
Matlab-! "#$%

-%)..-') !*%+$, .5
--!-# 3))&%%-#& 3)*+, +.))! 8-)/ '$1 '+)'%'# '6#/' '+#.' ")' Matlab !1 -#)6%#%' -6"
.3))&%%
,3),#4 '1#!1! -#(!6-% '()*+,' -#).(/#*

3)/#)*"# 3)/#%)4 3)*+, -+).) 3)*+,$ '0)!1


legend (2D only) 2-D figure
text plot subplot
title fill zoom
box plotyy hold
grid
axis 3-D alpha
set, get plot3 shading
clabel surf, surfc
xlabel mesh, meshz 3-D
ylabel contour, contour3, contourf view
waterfall rotate3d
3-D cylinder
zlabel
colorbar
colormap

!%&'() *+,+%' :5.1 !"#$

').(/#* -.+' .2+,' 00+#1% #$1 '()*+, 8#!6 )0%#0#" 8*#"$ -+.#) 3)*+, -+).)! ').(/#* !5
)#.+ 3)$+ 3)+(%$1 )/*%# #9 '$)4% .3&#(' 2+,' -()6%! -%+#, 2+, 3))( +$5 +1"5 3)*+, -+).)!
figure(n) 3#1)+' .'()*+, 8#!6 +.))! )&5 figure ').(/#*$ 3)1%-1% 3)&+*/ 3)*+, +*4% +.))!
."$' 3!1' +*4%' !7$ 8#!6 +.#) +#*4% "!! figure 3#1)+' .n +#*4% !7$ '()*+, 8#!6 +.))%

!"#$%
+%1) 2+,'# File 0)+*-$ Save as… #" Save -).(/#* -+)6$ )"7 -7.#$% 2+, -+)%1 -
.fig :$#(5
Copy -).(/#* -#7.%"$ Word #%5 3)!)!%- &$7%! Matlab !1 3)*+, +)$7'! 8-)/ -
7(+ )#/)1 .Matlab-$ 3)*+,! !&6%' -+)+$ "#' +#*" 7(+ .Edit 0)+*-$ -".%/' Figure
3)+6#$ 8$! 7(+ 6)+5'! )&5 !1%! .Copy Options ,Edit 0)+*-$ '+)6$ )"7 7.#$%
.Force white background ').*#"$

2
Matlab-! "#$%

plot !"#$%&' 5.1

3)*+, 0#0+1! -&7#)% ")'# 3))&%%-#& 3)*+, 0#0+1$ +-#)$ -)4)4$' ').(/#*' ")' plot -).(/#*
,"#' '!1 )!!5' 3#1)+' .3)##( #" -#&#(/ !1
plot(x1 , y1 , x 2 , y 2 ,!)

)&%. #" ;+#" #-#" )!7$ 3)+#0(# )&%. ,3)+!(4 )&%. -#)'! 3)!#5) !x i , yi " ,i # 1, 2,! +1"5
#" -#&#(/' -+#.$# 7$.$ '0)!1 -+1*"% &%. !5 +6"! '+.( -9#+6% -*4#' .1!&#, #-#"% -#.)+0%
,"#' 3#1)+' )!!5 8*#"$ .3)##('
plot(x1 , y1 , c1 , x 2 , y 2 , c 2 ,!)

.2#(/'&#(/' ,#4# 7$. -" 3)/)).%' (3)##- '7$+"! &7) 3)##- +*4%% $5+#% ci
.( ...,3#&" ,(#+) ,!#65 ")' !&6% -+)+$) (#+) 7$.$ #)') #(/'&#(/' )5 -7$#( 'g' -9#+6%' !1%!
)5 -7$#( 'k--' -9#+6%' !1%! .'%#&" -)$5#5 ')'- 2+,$ '&#(/ )5 -7$#( 'r*' -9#+6%' !1%!
.+#61 70#(% #( 00+#1)
#( 00+#1) )5 -7$#( 'g--s' -9#+6%' !1%! .-9#+6%$ 3')/1 8#). )"7 3)##( 37 -#&#(/ $!1! 3, 8-)/
.3)7#$)+ 37 70#(% (#+)

.-#)+1*"' !5 -" -%54% 5.2 '!$0

7$. '&#(/ ,#4 #( ,#4

-&'()# *+,+%.+ -&++% /+0&1 *+&(2'3 -+4&1 :5.2 !"#$

-')'*( /& 0$, 1'1$& 5.1.1

,3')/)$ 3)##( +#$)6 "!! -#&#(/ !1 3)*+, 00+1! 3)5+& )-1 -#%))(

3)+!(4 3' !x i , yi " ,i # 1, 2,! 3)&%.' .1


,&#(' "%,#&!

.5.1 2+, -" $)/%

.(+*' ;1%'$ 8#&)1 &6#)% '+(% #'9 ;" '.)+0%# +#0(# !1 ')./)$%#( 3, -%))(1
.'/1% #//)" c -9#+6%$ 3)##-' +&4 2

3
Matlab-! "#$%

1
0 1 2 3 4 5 6 7 8

*+,+%. 5() :5.1 5()

85!# !#$,' !7 (#)&$ 8' -#&#(/'% (!61 ;5 3)/#-/' !7 &(%-' Matlab )5 -#"+! 8-)/ 5.1 2+,$
-" -7$#(' axis -).(/#*$ 1#%)1 )"7 3)+).' -5+7% -,#.-$ -)/&) 0#!1! 8-)/ .8'$ 8)6$'! '1(
.2+,' -,.' 3#6-
,"#' )!!5' '%#1)+
axis([x min , x max , y min , y max ])
,+1"5
2+,$ 7)*#)' x +). !1 )!%)/)% ;+7 - x min
2+,$ 7)*#)' x +). !1 )!%)45% ;+7 - x max
2+,$ 7)*#)' y +). !1 )!%)/)% ;+7 - y min
2+,$ 7)*#)' y +). !1 )!%)45% ;+7 - y max

,&#(' -" 2)4#/ 5.1 2+,$ ',#.-' -" "6##+"! )&5

.!$(-% 5.2 2+,#

,3"$' 8*#"$ -"9 7.$! 8-)/ 3)+).' -#!#$,% (!6 (+ -#/1! #//#.+$ 3"
,3)+).' -#!#$, -" #'1!5 +#0(#$ $).'! '!)6-

tmp $ [x min , x max , y min , y max ]

,85&#7%' +#0(#' -" axis-! +)96'!# -#)#.+' -#0/)&+#"#(' -" +#0(#$ -#/1!

.z +). -" 3#6-! )&5 z min , z max 3)/-1% )/1 &#7 -*4#' )"7 )&%%--!- 2+,! 3, !"/' "()+0"' -" 7.$! 8-)/ 3

4
Matlab-! "#$%

0
-1 0 1 2 3 4 5 6 7 8 9

axis *&6%.+' *+7603# !*.+2 +"2 -&(&6! *4(70 *)+6*2 *+,+%. 5() :5.2 5()

4
#(' ,#4 8#). )" ;" '&#(/' ,#4 8#). + 3)+#0(# 3' !x i , yi " ,i # 1, 2,! 3)&%.' .2
.y-' -#0/)&+#"#( -" '##'% )/1' +#0(#'# x-' -#0/)&+#"#( -" '##'% &%.$ 8#1"+' +#0(#'
,&#(' "%,#&!

.5.3 2+, -" $)/%

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2

*+,+%.! )+1 /+&6 &"7 *+,+%. 5() $+$(2 :5.3 5()

.3)##( #" -#&#(/ 2+, ')') 2+,' 3" '7)$( !7 7)*1% #//)" 7$.' 8#). 4

5
Matlab-! "#$%

&+*/$ +#0(#$ +$)" !5 3#1+! ;+#. 8)"1 "#' )+!(4' 3#1)+' -%#7! )+#0(#' 3#1)+' !1 8#+-)'
'+#.' !7 '0)!1 +4#6 "#' )+#0(#' 3#1)+' !1 8#+46' .(3)5#+" 3)+#0(# +#$7 )+1*" )-!$! ;*#')
.&+*/$ '&#(/ !5 !1 7$.'#

2%''* /& 0$, -$%#% 5.1.2

3' !x i , yi " ,i # 1, 2,! 3)&%.' +1"5 85! .3)##( !1 3)*+, ")' 3)*+, -+).)! !&6%' -+)+$
3)+1) 3)##( +$6) Matlab 9" (5.1.1 (+*$ ')/1' -#+1*"') '&#(/' ,#4 1+#*%$ 8)#.% "!# 3)+#0(#
.-#&#(/' 8)$
,&#(' "%,#&!

.5.4 2+, -" $)/%

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2

-&++% "2 5() :5.4 5()

,&#(' .-*4#/ "%,#&

.5.5 2+, -" $)/%

.!,7% +#). ")' -6" "%,#& .'&)6"# -)+"/)! '+#.$ 3#,& x-+). &,/5 (+ "! 3)*+, 00+1! 3, 8-)/
,")' -#)90+( -#0/)&+#"#(! -)0%-%' ').%+#*4/+0'
x # a % r cos(&)
y # b % r sin(&)

6
Matlab-! "#$%

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7 8 9 10

+%+%0 +%# !&6%.+' $+$(2 :5.5 5()

. r "#' #4#)&+# (a, b) '&#(/$ "#' !,7%' 95+% . 0 ' & ' 2( +1"5
,&#('

.5.6 2+, -" $)/%

!1 -#).+#*#+*' -" -7$#( axis square -).(/#* .3)+).! 3)!)$(%' 3)##( -1+ -+.#) grid -).(/#*
-).(/#* -#7.%"$ 3)+).' -5+7% -,#.- -" &)+#'! 3, 8-)/ .!&#, #-#"$ #"+) 3)+).' )/11 ;5 2+,'
.5axis off

.axis equal-# axis tight #%5 -#)1#%)1 -#)+1*" +*4% &#7 -#0+#*% axis -).(/#* !1 help-$ 5

7
Matlab-! "#$%

-1

-2

-3
-4 -2 0 2 4 6

")70 *(&6& :5.6 5()

2%3*/+.'* 2%$+3. 1'1$& 5.1.3

-00+1%# '%#&%' (!6'% -%!7-% ').(/#*' 9" 3)4(!*%#( 3)+*4% "#' plot -).(/#* !1 0!(' 3"
-).(/#* 9" &)6) )4(!*%#( 0/%#,+" "#' 0!(' 3" .&)6) 8*#& ".#) '+(% 07%! ,)1%%' (!6' -" (+
.'%#&%' (!6' -%#7! )1%%' (!6' -" -00+1% plot
,+%#!5

"

,&#(' "%,#&!

.5.7 2+, -" $)/%

8
Matlab-! "#$%

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1 -0.5 0 0.5 1

-&1%"'0+% -&4(7 $+$(2 :5.7 5()

9
Matlab-! "#$%

((&) *+,) !&"#$%&' +'-. /&/+0 5.2

.&&#$ 2+,$ -#).(/#* +*4% 0#0+1! -#)+1*" 3 -#%))(

-'%#*('+! %$'1*' -" !/%4. !#%$1., $'1*' x $%# 5.2.1

,"#' )!!5' 3#1)+'


plot(x, ) y1 , y 2 ,! , y N *)
#"
plot(x, ) y1 ; y 2 ;! ; y N *)
.;+#" #-#"$ 3)+#0(# 3' !x i , yi " ,i # 1, 2,! 3)&%.' +1"5

0/%#,+"$ 4)/5'! 8-)/ -"9 7.$! )&5 .x +). #-#" -%#7! -#).(/#* +*4% 00+1! )#.+ -#$+ 3)-)7!
plot -).(/#* .'.)+0%$ -#+#1 #" -#&#%7 )+#0(# +#-$ -#).(/#*' -"# ,+#0(#5 x-' +). -" 8#1"+'
.x-' +). +#0(# -" #5+#"$ 3"#-1 &%%' )*! -#).(/#*' -" -00+1%

,"%,#&
,&#(' .( 0, (, 2( ) 3)/#1 3)+&-$ 3)4#/)4 +*4% &)6) 2+, !7 0#0+1

.5.8 2+, -" $)/%

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 1 2 3 4 5 6 7 8 9 10

*3 !"&40 !6&($0, (+$%+ x (&6) x (&6 +*+3 *0+7" *+&6%.+' ('10 $+$(2 :5.8 5()
(*+&6%.+'! &(+$%+

10
Matlab-! "#$%

,'$)-5 )"7 &)6" 7$.! 3)7$.' !5 -" 7#$(! 8-)/

%#$%
2+, +#-$ '&#%7 +#0(# !5! -46))-% ")' '.)+0% (+ 0!($ -!$(% plot -).(/#* 3" -
-+). -5+7% !7 '0)!1 8)" '9 3#1)+$ .2+,' #-#" !7 '/#1 7$.$ '&#%7 +#0(# !5 -00+1%#
.(1:length(y) "#' 9" +&,#% #//)" x +). +1"5) x

-'%#*('+! %$'1*'' x %$%# -" 2%,#%. 2%$'1*' %).# 5.2.2

,"#' )!!5' 3#1)+'


plot(x1 , y1 , x 2 , y 2 ,!)
.;+#" #-#"$ 3)+#0(# 3' !x i , yi " ,i # 1, 2,! 3)&%.' +1"5

,"%,#&!
,-#).(/#* 1#!1 -#/#-/
-f (x) # 3 % sin((x)
.
. x
/g(x) # % 1
. 2
.h(x) # + ln(x % 1) ,3
0
. 0 ' x ' 5 3#6-$

,'!)6- $16/

x +#0(# !1 &%. +#-$ ').(/#* !5 3#1)+ )"7 2+,' #-#"$ -#).(/#*' -1#!1 -" 00+1/
,&#(' .#!" -#&#(/$ ').(/#*' )5+7 +#0(##

.5.9 2+, -" $)/%

11
Matlab-! "#$%

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5

x &(&6 *3 -&)6&0 -&(+$%+ &,06) x (&6 +*+3 *0+7" *+&6%.+' ('10 $+$(2 :5.9 5()
(*+&6%.+'! &(+$%++

,3#1)+' 9" x +#0(# -#" -%#7! -#00+#1% -#).(/#*' !5 '9 '+(%$1 !!,$

.2+, #-#" -" $)/%

.3)/#1 3)/-1% -%#7! -#).(/#* +*4% 00+1! 3, 8-)/ '9 3#1)+$


,"%,#& ;1%'

-f (x) # 3 % sin((x) , 0 ' x ' 3


.
. y
/g(y) # % 1 , 2 ' y ' 4.5
. 2
.h(z) # + ln(z % 1) ,3 , 1 ' y ' 6
0

,&#('

.5.10 2+, -" $)/%

12
Matlab-! "#$%

0
0 1 2 3 4 5 6

-&.+2 x &(&6 *0+7" *+&6%.+' ('10 $+$(2 :5.10 5()

'/#1 -#&#(/ +*4% ,'/#1 3#6-$ 3)+). -5+7%$ ').(/#* !5 #/00+1 !"/' 2+,$ )5 $! 3)1/
.3)/#1 -#+#.# 3)7$.#

hold -%#*('+ 5.2.3

,hold -).(/#*$ 1#%)1' 8*#"


plot(x1 , y1 )
hold on
plot(x 2 , y 2 )
#
plot(x N , y N )
hold off

-%+#, hold on -%)1+ .3&#(' 2+,' -" '*)!6% 2+, +#.) -).(/#* !5 hold -).(/#*$ 1#%)1 "!!
2+, #-#" !7 3)"$' 3)*+,' -%1' ()4*'! )&5 .2+,' #-#" !7 1&6 2+, !5 -%1'# 3&#( 2+, -+)%1!
-" 2)!6%' hold -$)-5 -#7.%"$ ;*'!# off-! on $.%% +#$7! 8-)/ ,85 #%5 .hold off 3)%1#+
.37* !5$ $.%'

,&#(' "%,#&!

.5.10 2+, -" $)/%

13
Matlab-! "#$%

2'&%$ -'$'# /& -'('$35' -'('$-% 2'4%3 5.2.4

3)*+, ),#4 !1 $#!)1 3)#!- )-!$ 3)/-1% ''9 )#!- )-!$ '/-1% 3#1)+ -+#.
3)/#1 -#).(/#*! 3)/#1 -#).(/#*' !5!
!"/' 3#1)+$ )+1*" "! !"/' 3#1)+$ )+1*" "! )0(*%#( 3#1)+ :8#+-) '.)+0%# +#0(# .1
0#!1! +1*" )" :8#+46
!5 !1 7$.$# ,#4$
&+*/$ 2+,$ ').(/#*
!"/' 3#1)+$ )+1*" "! )0(*%#( 3#1)+ :8#+-) ,#4$ 0#!1! 8-)/ :8#+-) 3)+#0(# )&%. .2
').(/#* !5 !1 7$.$#
&+*/$ 2+,$
!$+#4% 3#1)+ :8#+46
#9' 3#1)+' -+#.$ (+ !$+#4% 3#1)+ :8#+46 ,#4$ 0#!1! 8-)/ :8#+-) hold -).(/#* .3
)+1*" ').(/#* !5 !1 7$.$#
&+*/$ 2+,$
!$+#4% 3#1)+ :8#+46

-+2&( *+(+6 "2 *+.+(18+ *+.+(*& -+4&1 :5.3 !"#$

14
Matlab-! "#$%

1"'+,2 1"%&."- !'-&3 5.3

+1*"% Matlab .-#".#-' -+'$'! 3)/#%)4 2)4#'! 3, !$#(% 3)*+, -#7.%"$ -#".#- -,.' -7$
,3)"$' 3)/#%)4' -*4#'
2+,! -+-#5 -
y-# x 6)+).! -#+-#5 -
"+(% -$)- -
2+,$ 3#(% !5$ 04(0 -
2+,$ 3#(% !5$ 3).6# 3)##( -
7.#%%# 3#%)/)% ,3#%)4(% #%5 -)4)4$ '()04)004 -

,3)*+,! 3)/#%)4 -*4#' 1)6%'! '"$' "%,#&$ 1%-1/ 5.3 (+* ;+#"!
,-#).(/#* )-1 -#/#-/
-f (x) # sin((x) % 4
/
0g(x) # x
2

. 13 ' x ' 3 3#6-$


,&#('

.3)/#%)4 -*4#- "!! !"/' -#).(/#*' )-1 -" +"-% '9 2+, .5.11 2+, -" $)/%

0
-3 -2 -1 0 1 2 3

(97 &.+0&1 3"" 5() :5.11 5()

.z +). +#$7 3, )&%% -!- 2+, +#$7 6

15
Matlab-! "#$%

2%)5'%. 2%(.%3 2'&%$ 5.3.1

$-5 .3)!%4# )-6- $-5 ,)!)7 $-5 ,-#)/##) -#)-#" +#.)! 8-)/ 04(0 -%1'# -#+-#5 -*4#'! -#&#(*$
-)/##)' -#"' 31 -$)-5# \ -#7.%"$ -#)/##) -#)-#" ._ -#7.%"$ )-6- $-5 ,^ -#7.%"$ +.#)% )!)7
-#)-#" -$)-5 #%5 ")' 7-#!#&, -#)/##) -#)-#" -+).) .\alpha -$)-5 )"7 -+.#)% 2 "%,#&! .-)!,/"$
-+.#)% 3 "%,#&! .'!#&, -)!,/" -#" -#)'! ;)+. -)/##)' -#"' 31 !1 '/#1"+' -#"'1 (+ ,-#/0(
.\ -#7.%"$ +#.)! 3))+1*"' 3)!%4' !5 -" -%54% 5.4 '!$0 .\Gamma -$)-5 )"7

3))+,#4 -#7.%"$ -"9 -#17! 8-)/ .0//#*4(" !1 )#0)$ #%5 ,&6) 3)!%4 +*4% &,"! )#.+ -#$+ 3)-)7!
2
.e^{\alphax_1^2} $#-5/ e2x1 )#0)$' "%,#&! .{} -#!4!#4%

-$-'4 -+3'! 5.3.2

.title -).(/#* !1 0!($ -9#+6% -$.' )"7 -7.#$% -+-#5 -*4#'


,"%,#&!

y-' x $%#/ -'$-'4 -+3'! 5.3.3

.xlabel -).(/#* !1 0!($ -9#+6% -$.' )"7 -7.#$% x +).! -+-#5 -*4#'
,"%,#&!

.ylabel -).(/#* !1 0!($ -9#+6% -$.' )"7 -7.#$% y +).! -+-#5 -*4#'
,"%,#&!

13*1 -+3'! 5.3.3

-9#+6%# 04(0' -$)- -#0/)&+#"#( -$.' )"7 -7.#$% 2+,$ (x, y) -#0/)&+#"#($ 04(0 -*4#'
.text -).(/#* !1 0!($ ')#.+' 04(0'
,"%,#&!

.y -0/)&+#"#( 95+%$# x -0/)&+#"#(! 8)%)% -9#+6%' -" '$).% 04(0 -).(/#* 8!&6% -+)+$ +#-$
(x,y)

04(0 -$)-

.'))70# )#4)/ 07% 7.$! "!" '+)+$ 8)" #9 '+#.$ 04(0' -" $).'! (#)&$ '*)" -7&! )&5

.A ")' '!#&, "*!" -#"' !1%! .-#!#&, -#)!,/" -#)-#"! -#'91 -#!#&,' -#)/##)' -#)-#"' -#/)#.% "! '!$0$ 7
. VerticalAlignment-# HorizontalAlignment -#&#(* -#7.%"$ -#+6" -#)+1*" 8

16
Matlab-! "#$%

\ *+7603# (+6&" -&.*&.! -&"01 -+4&1 :5.4 !"#$

17
Matlab-! "#$%

"$*. -6%- -+3'! 5.3.4

!1 0!($ ,2+,$ -#).(/#*' -#%1 -" -#,.)%' ,-#9#+6% -$.' )"7 -7.#$% 2+,! "+(% -$)- -*4#'
.legend -).(/#*
,"%,#&! .0#0+1' 8#/,4 !1 "%,#& ').(/#* 31 !5 &)! ,.#) "+(%' -$)-$

)#.+ 3)-)7! .'/#)!7' -)/%)' '/)*$ "+(%' -$)- -" '$).% !&6% -+)+$ +#-$ legend -).(/#*
8*#"$ "+(%' -$)- -" 9)9'! 8-)/ .2+,$ 7&)%' -" 3)+)-4% -#6*1 2+,$ 3)+6" -#%#(%$ '$).'!
,3)"$' 3)5+7' -" !$(%' '/-1% &#7 0!($ 2)4#'! #" (Unlock Axes Position -#7.%"$) )/&)

'!#7* ;+7
(!&6%' -+)+$) '/#)!7 -)/%)' '/)*$ "+(%' -$)- -%1' 1
'/#)!7 -)!"%1' '/)*$ "+(%' -$)- -%1' 2
'/#-6- -)!"%1' '/)*$ "+(%' -$)- -%1' 3
'/#-6- -)/%)' '/)*$ "+(%' -$)- -%1' 4
-#6*1 '%5 '+)-4% '$)-' #$1 3#()%$ ,2+,' -#!#$, ;#-$ "+(%' -$)- -%1' 0
'/#)!7 -)/%)' '/)*$ ,&+*/$ 2+,' -#!#$,! :#6% "+(%' -$)- -%1' -1

legend -+%&0 *+&(2'3 :5.5 !"#$

,&#(' "%,#&!

.+$& +)-4- "! "+(%' -$)-1 ;5 8#)!7 )95+%' (!6$ "+(%' -$)- -" $).%

-%3%36 !*%13%113 -+3'! 5.3.4

3*)4#'! -#!($ 8-)/ #-#7.%"$# -#).(/#*' !1 3))04004 3))#0)$ +*4% $16% Data Statistics 8#!6
.5.12 2+,$ 3,&#%1 )*5 Data Statistics-# Tools !7 '.)6! )"7 6-*/ '9 8#!6 .2+,!

Data Statistics /+"8 :5.12 5()

18
Matlab-! "#$%

!7 !"/' 7&)%' -" 00+1! )&5 .&#7# 7.#%% ,3#%)/)% ,3#%)4(% )5+7 2+,$ ').(/#* !5! ,).% '9 8#!6
.'%)"-%' '$)-' -" #/%4 0#1* 2+,'
max-# min )5+7 8%4/ f (x) # sin((x) % 4 ').(/#*' ;+7 '/-1% #$ 3#6-' -" 8%4! )&5 ,"%,#&!
.#9 ').(/#* !1 Y +#$7

2%#5' 2%''* -+3'! 5.3.5

,'/)%) 8##)5$ -#)'! 3)!#5) #!" 3).6 ;" .\ +#0+*#"' -#7.%"$ text -).(/#*$ 3).6 +.))! 8-)/
.(5.4 (+* #"+) )/&) 8*#"$ 7.#$% 3'1!5 3).6# 3)##( 0#0+1 .'0%!# '!7%! ,'!"%1

2%+$,/ 2%('.%3 -+3'! 2'4%3 5.3.6

.95.3 (+*$ 3)/#%)4' -#*4#- !5 -" ,).% 5.13 2+,

.'/1% #/)" 8-$)-5 +&4# plot -).(/#* +6"! $-5)'! -#$))6 3)/#%)4' -*4#' -#&#(* !5

An example of graph annotation


9
sin((4x)+4
8 sin((4x)+4 min
sin((4x)+4 max
7 2 x2
$x

5
sin((4x)+45
y
4

0
-3 -2 -1 0 1 2 3
x

(97 &.+0&1 -7 5() :5.13 5()

.)/&) 8*#"$ #/#1 "+(%' -$)-$ f (x) # sin( (x) % 4 ').(/#*' !1 3#%)45%# 3#%)/)% )5+7! 04(0' 9

19
Matlab-! "#$%

1"'+, 20 !"%(" 34"+5 5.4

)#/)1! -#/-)/ 3))*+, 3)0()$#" !1 -#/#5-' .&#7# 04(0 ,3)##( ,3)+). !1 3)0()$#" 3)!)5% 3)*+,
(toolbar) '&#$7' !,+4 ;+& #" Insert 0)+*- ;+& '7)$(! -#/-)/ 5.3 (+*$ -#&#(*' .)/&) 8*#"$
.5.15 2+,$ 7)*#%1 )*5

,3)##( -*4#' '!&,'


8#!6 '4% 04(0# ,3).6 '/0('#
'5)+7 :6 2+,' $#$)4

0())$#"
8%#4%
9)9'! 8-)/
04(0 !5 !7 '.)6!
'.)6! )"7 +#-*5'
'7#$( !1 )/%)'
'5)1%# !7 +$57'
0())$#"
8%#4%
-6-#*
0)+*-
-#)+1*"

8#!6 6-#*
-#/#5- -5)+7

&1+'&$ &'() /+"8 :5.15 5()

.Edit 0)+*- ;+& )#/)1! -#/-)/ 3)+).' 0())$#"# 2+,' 0())$#" !1 -#/#5-'
10
!7 '.)6! . )#.+' 0()$#"' !7 '.)6!# '5)+7' :6 !7 '.)6! )"7 7.#$% '5)+7! 0()$#" 8#%)4
-1,! )&5 .5.15 2+,$ 3,&#%1 )*5 '5)+7 -#)+1*" -6-#* 8%#4% 0()$#" !7 +$57' !1 )/%)' +#-*5'
-#/1! 8-)/ '9 8#!6$ .-#/#5-' -5)+7 8#!6 -" -6-#* Properties !7 '.)6! ,0()$#"' -#/#5- !5!
.&#7# 8#/,4 ,#( )$#7 ,7$. #%5 3)$+ 3)+0%+*

.#( 0()$#" !1 -#/#5- -5)+7 8#!6 ,.#% 5.1.6 2+,$

.'5)+7' :6 +#-*5 !7 '.)6! )"7 '5)+7 $.%% 3)".#) 10

20
Matlab-! "#$%

!5$ 0#0)1
0())$#"' ,#4 2+,' !1 3)0())$#"'

#( ,#4% )*+, 0()$#" -#/#5-

*+.+4* *4&(7 /+"8 :5.16 5()

!1 )/%)' +#-*5' !7 '.)6! ,'5)+7' :6 -#7.%"$ #/#%)4 )"7 2+,' -" '/$% -" -)/&) -#/1! 8-)/
.2+,' -" :##5! ,6#-%! ,9)9'! 8-)/ -75.Unlock Axes Position -+)6$# 2+,' !#$, !7 +$57'
.2+, !7 2+, -%1' "#' #9 '/#5- !1 )+1*" 1#%)1
,"%,#&
-".%/ #!1 +&-' -$#,-1 +%#!5 .-#1)+&$ &%#7 "#' )5 (#&$! 3).#+ Low Pass Filter -))/$$
)#.+ 85!# -)/7$#- )5' ")' '1)+&' +$7%' +#9" '.($ .5.17 2+,$ 3)(#+)' 3)##($ 8%#4%' 3#6-$
2+,' !1 3)+).' +#+61 )"7 '9 '+(%$ '17/ +$&' .+#9"' !1 $)+(- -/#%- 3, 2+, #-#" !7 3)1!
3#()%! #-99'# 3)*+,' &6"$ zoom in 7#.)$ ,paste-# copy -#7.%"$ 2+,' !#*51 ,+$4#'1 )*5
.)/1' 2+,$ )#.+'

*&.,& !(+6# /+"8 "7 /+"8 *%#,! *0),! :5.17 5()

21
Matlab-! "#$%

-')'*+ -'7#."6 2%%+$, 2%1*%6'" /& -'('4- -7%6* 5.4.1

;+&' .)#/)1! -#/-)/' -#/#5- !1 $+ +*4% 0()$#" !5!# ,3))*+, 3)0()$#" +*4%% $5+#% 2+, !5
-+.#)' ').(/#* !1 0!($ 2)4#'! ")' -#&#(* -#7.%"$ '()*+, )0()$#" !1 -#/#5- -#/1! '0#1*'
.#9 '/#5- !1 1&6' ;+7# '/#5-' 31 !1 -9#+6% :3)*4#/ 3)/-1% )/1 )*+, 0()$#"
,&#(' "%,#&!

.5.18 2+, -" $)/%

An example of graph annotation


9
sin((4x)+4
8 x2

2
7
$x
6

y
sin( (4x)+4 5
4

0
-3 -2 -1 0 1 2 3
x

+.+2 +"2 !%&'()! &$%&#+3 *+.+4*0 %"82 5() :5.18 5()

!"#$%
.set-# get -#&#(*$ 1#%)1 ")' +-#) -5$#4%' ;+&' -
'+97' :$#($ #//#$-' 3')-#/#5-# '()*+,' )0()$#" !5 !1 '%)1+ -#"+! )&5 -
.(html 0%+#*$) Handle Graphics Online Documentation

22
Matlab-! "#$%

fill !"#$%&' 5.4

-" "!%! )#.+ !"/' "%,#&$ !1%! .fill -).(/#* -#7.%"$ -#).(/#* )-1 8)$ 2+,$ +#9" "!%! 8-)/
.3#&" 7$.$ 12 ' x ' 2 +#9"$ '!#$+*'# 4#/)4' 8)$ +#9"'
,&#('

.5.12 2+, -" $)/%


An example of graph annotation
9
sin((4x)+4
8 x2

2
7 $x

5
sin((4x)+45
y
4

2 sin((4x)+4 &&xx22
area between sin(pi*x)+4

0
-3 -2 -1 0 1 2 3
x

fill *&6%.+' *0),! :5.19 5()

85!) 3#6-' +#9"' !#$, ;+#"! -#&#(/ +%#!5 .+#,4 +#9" -#+.#)' -#&#(/ "#' fill -).(/#* !1 0!('
').(/#*' -4/5' )"7 7.#$ y +).$ +#1+1' #9 "%,#&$ .(3#)4' -&#(/! ''9 ')'- '!6-'' -&#(/
+#0(# -%1' )"7 7.#$ x +).$ +#1+1' 85! .'/#-6-' ').(/#*' -" ;#*' +&4$ 5"6"# '!)6- '/#)!7'
.;#*' +&4$ x +#0(# -" #)+6"# x

23
Matlab-! "#$%

plotyy !"#$%&' 5.5

)+1*" 1#%)1 .2+, #-#" !7 3)/#1 y )+). )/1 -#!7$ -#).(/#* 0#0+1 -+1*"% plotyy -).(/#*
)-!$ '/-1% #-#" !1 ').(/#*5 (-#/#1 -#&)6)) 3)/#1 3))!(4)* 3)!&, )/1 0#0+1 "#' '9 ,#4% 2+,!
.)#!-
,&#(' "%,#&!

.5.20 2+, -" $)/%

40 1

20 0

0 -1
0 2 4 6 8 10 12 14 16 18 20
x

plotyy *&6%.+' *0),! :5.20 5()

#" 8#/,4 -" -#/1! #" ,&6" !5! '/#1 ylabel 8)).! )&5 .-#/#1 y 2 !1# y1 !1 '!(4' )5 $! 3)1/
.plotyy !1 help-$# set -).(/#*$ +97)'! 1) 3)##(' 7$.

1"'+,3. !&+"0" 1"4+5 !6"+$ 5.6

,-$)-5 )"7 -7.#$% 2+,% -#+)1) 3)5+7 -")+(

2+,' '!#7 (script :$#($ -7.#$% #9 '+#11 #") Command Window-$ Enter !7 3).6#!1 7,+$
'&#(/$ )!"%1' #" )/%)' +$57' +#-*5 !7 '.)6! .+$57' 8%4 $)$4 $!. 7)*#%# -)0%#0#" 8#+6"'
&%. &#7 '4)/5% -*4#/ '.)6! !5 .x,y 3)+#0(#! #9 '&#(/ !1 -#0/)&+#"#(' -" '$).% 2+,$ -%)#4%
.Enter !7 3).6#! 3))4! )&5 .x,y 3)+#0(#! -#0/)&+#"#(

24
Matlab-! "#$%

subplot !"#$%&' !&5#.6) (7" 1"'+, +'-. !.03 5.7

,subplot -).(/#* -#7.%"$ '()*+, 8#!6 #-#"$ 3)*+, +*4% +#.)! 8-)/

.'()*+,' 8#!6 ;#-$ 3)*+,---' !1 -#&#%7'# -#+#1' +*4% -" 3)7$#( 3)/#1"+' 3)0/%#,+"' )/1
.'0%! '!7%!% 5"6"# 8)%)! !"%1% "#' +&4' +1"5 3)*+,---' !1 4(&/)"' "#' )1)!1' 0/%#,+"'
#)+6" "$' subplot-! &6" subplot 8)$ -#&#(*' !5 )5 ")' '$#16 '&#(/ .'16%'! 5.21 2+, #"+
.8#&)/' 2+,'---' !7 (+ -#7)*1%

Figure No.1

#
#
#

#
#

subplot *&6%.+' :5.21 5()


,&#(' "%,#&!

25
Matlab-! "#$%

.5.22 2+, -" $)/%

Bar Plot Stacked Area Plot


1 15
3D Pie Plot
0.8
10 22%
0.6 11%
28%
0.4 33% 6%
5
0.2

0 0
-5 0 5 1 2 3 4

Stem Plot Stairs Plot Scatter Plot


1 1 -47.8

0.5 0.5 -48

0 0 -48.2

-0.5 -0.5 -48.4

-1 -1 -48.6
0 100 200 0 5 10 210 211 212

subplot *&6%.+'# 2+0&2" 30)+, :5.22 5()

26
Matlab-! "#$%

4 !"#$!
:!"#$!! %"&'(
2 ......................................................................................................... &'(%% &!& )*'+,- .6
2 .................................................................................................... .''(%% &!& .'##* 6.1
4 ................................................................................................ .''(%% &!& .'/01% 6.2
12 ............................................................................................................. ,"&% '##* 6.3
15 .............................................................................. ('/' 2,- !3 .'/01% ,+4% &-5) 6.4
17 ............................................................................................................. .'+,-$ )0'!1 .7
17 ............................................................................................... .'+,-$ &'6(' )0'!1 7.1
19 ............................................................................ Handle Graphics !1 .'0*''$#" 7.2
21 ..................................... .'+,- &,'5'! &#'4'4$) &#'5*6#+) &#35%"$ .'+,- &,'5' 7.2.1
22 ............................................................ )*'+,- 0*''$#" !1 &#6#7& &3'$*# &!$* 7.2.2
Matlab-! "#$%

)%*++ ),) !-%.$/ .6


.1.''(%% &!& .'/01%# .''(%% &!& .'##* &,'5' ,*#4 )8 *,+

!""#$$ %&% !"''( 6.1


.
,"') plot &'5*6#+ !1 &'(%%-&!&) )4,-)
plot3(x1 , y1 , z1 , x 2 , y 2 , z 2 ,!)

#&#" '!3$ .',#0*# !1 &#'1'!1 ,.',!*4 !1 &#'1'!1 &#')! .'!#7' !x i , yi , z i " ,i # 1, 2,! ,1"7
)0'!1 &,1+"% )'1'!1 !7 ,/"! ),5* &8#,/% &+4#) .!(#- #&#"% &#5',0% !1 &#'1'!1 #" 9,#"
,"#) .#1',) '!!7 :+#"$ ..'##*) #" &#(#*6) &,#5$# 3$5$
plot3(x1 , y1 , z1 , c1 , x 2 , y 2 , z 2 , c2 ,!)

text &'5*6#+$ 1#%'1$1 .) .'('/') .'!($)) .)!1 &'(%% #() )!'$*%! )%#($ &(*+&% #8 )'5*6#+
.zlabel &'5*6#+ &#35%"$ &3$*6 z ,'5 !1 &'##&)# z ,'5 !'$1$ )06'(,"#* (#3 2'4#)! 9',5
,"%-#(
,(#*) .)'61) ('! &/" )!,'+4# )('%,'+ 00,16

.6.1 2,- &" $'6%

2,-) -#4 &,'/$# '6%') ,#&+7) !3 )5'/! ,)5',0%) :#%'4 '"3 Workspace :#!/% ,'1' :+#"$ .'+,- 00,1! :&'6 1
.&#!$-#% #7,( )-#5&) &#',1+" #3$0% 9" (#* &#,#1 ,+4% &$'&7 94#/ )8 *',0 .'#5,)

2
Matlab-! "#$%

Spiral

Pyramid
40

1 top
top
0.5 20
z z
0
2
2 0
1 1
1
0.5
y 0 0 0
x 0
y -1 -0.5
x

plot3 $%&'()*+ ,)-%,. /-#)0 :6.1 !"#

3
Matlab-! "#$%

!""#$$ %&% !")*+$ 6.2

,,(-#% '(%% &!& /01%


z # f (x, y)

!1 '!!7 .#1', .surf -# mesh &#'5*6#+ :) .''(%% &!& .'/01% &,'5'! &#'4'4$) &#'5*6#+)
,#!" &#'5*6#+

-#

&'5*6#+ &#35%"$ x,y &#06'(,#"#* ',#0*#% &#*+#%) &#06'(,#"#*) &#5',0% :) X,Y ,1"7
.X,Y &#5',0% !17 .'(%% .&#" &!3$ )5',0% "') Z )5',0% .meshgrid
Z )$#-) &5',0% '"3 3$*6 .3$51 .'##* &1, &,5#' mesh &'5*6#+1 "#) &#(#*+) '&1 :'$ !($))
.2Z )$#-) &5',0% '"3 3$*6 .3$51 .'"!0% .'$7,#%) .'/01% &,5#' surf &'5*6#+ &"8 &%#3!#
.Z )$#-) &5',0% '"3 3$*6 .&,-4% 3$51 .'6$! .'"!0 &,5#' mesh &'5*6#+ !#7'$7

,"%-#(
,)'5*6#+) &" 00,1! '#5,
2
)( * &$ x 2 ' y 2 %
f (x, y) # 5sin + xy , ' 10e '1
- 15 .
0-3 / x / 3
.1 .#/&$
2-3 / y / 3

,(#*)

.."&)$ 6.3-# 6.2 .'+,- &" $'6%

.(,#/1 !(/% &,',$) ('/" 3$5$ .'##*) &1, surf &'5*6#+$ 2

4
Matlab-! "#$%

15

10

z 5

0
4

0
3
-2 2
1
0
y -1
-4 -2
-3
x

mesh $%&'()* $)1&-/+ 2)2", :6.2 !"#

15

10

z 5

0
4

0
3
-2 2
1
0
y -1
-4 -2
-3
x

surf $%&'()* $)1&-/+ 2)2", :6.3 !"#

!"#!
!7 ,#!" .''(%% &!& .'+,- &,'5' &#'5*6#+ &#35%"$ ,&#' *!/ /01% 00,1! #66#5,$ ." -
.)%'-( &#(#*6 ,&#' &+4#) &#35%"$ )'5#!#8,) &" :'0*)! "#) &#13! #6'!31

5
Matlab-! "#$%

meshgrid $%&'()*+ $"),-$

,0!*7 $ x, y % .',#0*# '61 &!$*% #8 )'5*6#+

,,1"7
[n 31] ,#0*# - x
[m 31] ,#0*# - y

,"$) :+#"$ [m 3 n] !(#-$ X,Y ))8 !(#-$ &#5',0% '&1 &,5#'# .&#" &!+71% #8 )'5*6#+
,&#(#*+)
x # 4 x1 x 2 x 3 x 4 5 ;
y # 4 y1 y2 y3 5 ;
[X, Y] # meshgrid(x, y);
,&#5',0%) &" &#,5#'
6 x1 x 2 x 3 x 4 7
X : 88 x1 x 2 x 3 x 4 99
8; x1 x 2 x 3 x 4 9<
6 y1 y1 y1 y1 7
Y : 88 y 2 y 2 y 2 y 2 99
8; y3 y3 y3 y3 9<

,.''*&% :7!
=j, X(i, j) # x(i)
=i, Y(i, j) # y( j)

,$'" Z &5',0% $#1'/ #%7 &#"5#& :&#" &" $'6% Y-# X .'7,3%) .3 )(#*6) ,#0,+#"$ 1#%'1 :7!
.)$,)$ ,5* :%8$ !$" ,&#"!#! '&1 &#35%"$ ,$'"
,!'3' "! ("% :+#"$ ,$'" ,$'" Z &5',0% $#1'/ &1/%) :!)!

&" &#61! :&'6 Matlab-$ .view &'5*6#+ )6#1",) .3+$ )-5#) 6.3-# 6.2 2,- &,'5'! (#*$
.rotate3d &'5*6#+ &#35%"$ #" (5.15 2,- #",) ,#&+7) !3 )5'/! '"3 2,-) !1 )'506',#")
view &'5*6#+$ .'1%&1% #8 )'+5 &'#8$ -5#' 2,-) .3+ !71 .'5#,# )'#5, )'+5 &'#8 .'"5#%1 3-,$
,"$) :+#"$
.6.4 2,- )", .))$-)# 0#%'8" &#35%"$ )'506,#") &" .','(-%
,(#*) 3#5'$

6
Matlab-! "#$%

0!*$ #!" .'7,3 &$5) '"3 &35#$% 2,-) !1 )'506,#") &3'$* .!"6) .'6&1%$ #!" .'7,3 &" $'5%
,3view &'5*6#+ !1

view $%&'()*. 33+#3) 2)-%4/ $)1-,- :6.4 !"#

-# surfc &#'5*6#+$ :+'!/6 mesh-# surf &#'5*6#+ ,#$3 2,-! &/&% ,"&% '##* .- !$*! #66#5,$ ."
.(,"&% '##* )&#3%1%1 contour )!'%) &" &-5'% c &#")) ."&)$ meshc
,(#*) "%-#(!

.6.5 2,- &" $'6%

15

10

z 5

0
4

0
3
-2 2
1
0
y -1
-4 -2
-3
x

surfc $%&'()* $)1&-/+ 2)2", :6.5 !"#

.az=-37.5 el=30 .) '(%% &!& 2,- !1 !(/%) &,',$ '7,3# az=0 el=90 .) '(%% #( 2,- !1 !(/%) &,',$ '7,3 3

7
Matlab-! "#$%

,"$) :+#"$ .'##*) &1, &" (',#)! :&'6


,(#*) "%-#(!

.6.6 2,- &" $'6%

15

10

z 5

0
4

0
3
-2 2
1
0
y -1
-4 -2
-3
x

5%)'3 $," /.. 2)2", :6.6 !"#

camlight &'5*6#+ &#35%"$ '#5, :##'7$ 4),#"& 2'4#)! .- :&'6 '4'+ 0*'$#" !1 )1#/& &,'5' :3%!
,!"6) &#,#1) &+4#) ,"%-#(! .lighting &'5*6#+ .3 ),#"&) -#4 &" ,#/$!#

.6.7 2,-! 6.6 2,- &" &#61%

.surfl &'5*6#+ &#35%"$ .- )!!5)# ),#"& '!3$ .'+,- ,#5'! :&'6 4

8
Matlab-! "#$%

!"#. 3")/$ $*6)3 :6.7 !"#

3#$*! .- :&'6 .)'5*6#+) !1 '4/') )$#-! ."&)$ .'6&1% )'5*6#+) !1 .'3$5) !(/% &,',$ ,#&$
,)'5*6#+) &#35%"$ )$#-$ &#!& "!! ('/" )')' 3$5) '7

.'3$5) 4/' &" .'3$#* #!" .',$'" .1-! 0 :'$ .'7,3 '!3$ .',$'" 3 !3$ ,#0*# "#) c ,1"7
*!/ )-'5%) )!$0 :!)! .!"6) ,(4) '+! '#5,) 3$5) &" .'$'7,%1 !#/7 ,*#,' ,.#(" .''4'4$)
,&#',1+")%

53%7"1) 5%1)0% 5%1+& "*6- :6.1 3.+2

9
Matlab-! "#$%

,(#*) &+4#) "%-#(!

.6.8 2,- &" )$'6%

82,-. 0%8/ 1+& $1%+' :6.8 !"#

,:) &#+4#6 &#',1+"


.grid on &$'&7 '"3 ),8/ )&*!()# grid off &$'&7 '"3 .','5) &1, '#$'7 -
.box off &$'&7 '"3 )&(,#)# box on &$'&7 '"3 2,-) $'$4 "4+#* &$5) -
.axis on &$'&7 '"3 ),8/ )&*!()# axis off &$'&7 '"3 .','5) &7,3% '#$'7 -

,waterfall-# meshz .1$ ,surf-# mesh &#'5*6#+! &#'5"',# &##)%) &#'5*6#+ '&1 (#3 &#%''*
..'6#1 03% .'+,- &#,5'%)
,(#*)

.."&)$ 6.10-# 6.9 .'+,- &" $'6%

10
Matlab-! "#$%

15

10

z 5

0
4

0
3
-2 2
1
0
y -1
-4 -2
-3
x

meshz $%&'()* :6.9 !"#

15

10

z 5

0
4

0
3
-2 2
1
0
y -1
-4 -2
-3
x

waterfall $%&'()* :6.10 !"#

11
Matlab-! "#$%

,-%$ "''( 6.3

,,"&% '##* !1 .'+,- &,'5'! &#(#*+ 3 &#%''*


.''(%% #( ,"&% '##* &,'5' .1

,(#*) "%-#(!

.6.11 2,- &" $'6%

9,#5 :'" ." .,"&%) '##* !1 .'7,3) &" -'5% !"6) :+#"$ clabel &'5*6#+$ 1#%'1)
.!'-,) :+#"$ )'5*6#+$ .'1%&1% 8" #!" .'7,3$

.''(%% &!& ,"&% '##* &,'5' .2

,(#*) "%-#(!

.6.12 2,- &" $'6%

.contour )'5*6#+! )!#3+&$ ))8 #8 )'5*6#+

.'"!% .''(%% #( ,"&% '##* &,'5' .3

.3$5$ #"!#% ,"&%) '##* :'$ .'/##,%) &371 *, contour )'5*6#+! )%#( #8 )'5*6#+
)+#!/7 :7!# .',#,$ .6'" .',+4%) .'&'3! 9" ,',1+" clabel )'5*6#+$ 1#%'1 #87 )-#5&$
9,3) !3 )('3%) 2,-) (5! 3$5 &(#%3 )$'5% #8 )'5*6#+ .colorbar &'5+#"$ 1%&1)! :&'6
.'+,-$ &'$'0*+" "') 9" '(%% &!& 2,- !7$ #8 )'5*6#+$ 1%&1)! :&'6 .3$5 !7 !1 ',%#6)
.(surf, surfc, contourf !1 .'+,-) .'"!%

,(#*) "%-#(!

.6.13 2,- &" $'6%

12
Matlab-! "#$%

5 5
4 4
2 2

1 6 2
2 8
10
0 9
y 7 4
5 3
-1 3 3

4 4
-2
5 5

2 2
-3
-3 -2 -1 0 1 2 3
x

clabel-) contour $)%&'()* $)1&-/+ "/$- %))' 2)2", :6.11 !"#

10

8 10

6 9 8 5
4
z
6
4 5 7
3
4 4
2 3
5 2
2 3 4
5
2 3
5 2 2
4
3
0 2

3
2
-2 1
0
y -1
-2
-3
x

clabel-) contour3 $)%&'()* $)1&-/+ "/$- %))' 2)2", :6.12 !"#

13
Matlab-! "#$%

3 10

9
2
8

1 7

6
0
y
5

-1 4

3
-2
2

-3
-3 -2 -1 0 1 2 3
x

colorbar-) contourf $)%&'()* $)1&-/+ "/$- %))' 2)2", :6.13 !"#

14
Matlab-! "#$%

#")" .,/ &0 !")*+$ ,12$ %/34 6.4

.hold &'5*6#+ &#35%"$ &35#$% ('/' 2,- !3 .'/01% ,+4% &-5)

,"%-#(!
. g(x, y) # x ' y ' 4 )!#$,+ (#3 6.3-# 6.2 *,+% "%-#() /01%! 2'4#6
2 2

,(#*)

.6.14 2,- &" $'6%

0%8% !"# .1 5%82,- "*6- 2)2", :6.14 !"#

15
Matlab-! "#$%

.)*!/$ &,&4#% )6#&/&) )'5*6#+) 97% )"5#&7# '61) &" (/" .'7&#/ .'/01%) '61 6.14 2,-$
&-,( &" 3#$*! :&'6 .&%''#4% )('%$ .'+#*1 &#')! .'/01%! .#,-! "') &'1#%'1 &#,1+" :7!
0 :'$ .'7,3 !3$ ,!*4 !$*! !#7' )8 ,0%,+ .alpha ,0%,+ &#35%"$ 2,-$ .'/01%) !1 &#+'*1)
.&0!/#% &#+'*1 0-# ,&0!/#% &#%'0" -5''% 1 ,1"7 1-!
,"%-#(!
.($!$ :#'!3) /01%) &#+'*1 &" )616
,(#*)

.6.15 2,- &" $'6%

alpha "2-"* 51 $)*%', $"%&%) 0%8% !"# .1 5%82,- "*6- 2)2", :6.15 !"#

16
Matlab-! "#$%

0%.$/1 !2%,& .7
.''+,- .'0*''$#" !1 &#6#7&) .(#3# 04*0 ,.'##* ,.','5 !1 .'0*''$#" .'!'7% Matlab-$ .'+,-
.Handle Graphics &#35%"$ #" '6(' :+#"$ '#6'1! &#6&'6

!"1,/5 %"6#" 4*"&+ 7.1

3'+#%1 '+7 (toolbar) )(#$3) !-,4 9,( #" Insert 0',+& 9,( )3'$*! &#6&'6 5.3 *,+$ &#(#*+)
.7.1 2,-$

:#!/ '4% ,.'##* &+4#) )!(-)


)7',3 ;/ 04*0# ,.'5/ )60*)# 2,-) $#$'4

0*''$#"
:%#4%
8'8)! :&'6
04*0 !7
)5'/! '"3 !3 )5'/!
)3#$* ,#&+7)
)7'1%# !1 '6%')
!3 ,$73)
0*''$#"
:%#4%
&/&#+
0',+&
&#',1+"

:#!/ /&#+
&#6#7& &7',3
%6)*%2 %*"# 9).8 :7.1 !"#

.Edit 0',+& 9,( '#6'1! &#6&'6 .','5) 0*''$#"# 2,-) 0*''$#" !1 &#6#7&)
!3 )5'/! .5'#5,) 0*''$#") !3 )5'/!# )7',3) ;/ !3 )5'/! '"3 35#$% )7',3! 0*''$#" :#%'4
.7.1 2,-$ .-(#%1 '+7 )7',3 &#',1+" &/&#+ :%#4% 0*''$#" !3 ,$73) !1 '6%') ,#&+7)
:&'6 )8 :#!/$ .&#6#7&) &7',3 :#!/ &" &/&#+ Properties !3 )5'/! ,0*''$#") &#6#7& !7! &1-! '(7
0*''$#" !1 &#6#7& &7',3 :#!/ -5#% 7.2 2,-$ .(#3# :#6-4 ,#* '$#3 ,3$5 #%7 .'$, .',0%,+ &#61!
.#*

.)7',3) ;/ ,#&+7 !3 )5'/! '"3 )7',3 $5%% .'"5#' 5

17
Matlab-! "#$%

!7$ 0#0'1
0*''$#") -#4 2,-) !1 .'0*''$#")

#* -#4% '+,- 0*''$#" &#6#7&

$)()7$ $7%"1 9).8 :7.2 !"#

!1 '6%') ,#&+7) !3 )5'/! ,)7',3) ;/ &#35%"$ #6#%'4 '"3 2,-) &" )6$% &" &'6(' &#61! :&'6
.2,-) &" ;##7! ,/#&%! ,8'8)! :&'6 &37.Unlock Axes Position &,'/$# 2,-) !#$- !3 ,$73)
.2,- !3 2,- &%1) "#) #8 )6#7& !1 ',1+" 1#%'1
,"%-#(
&"5%6 #!1 ,(&) &$#-&1 ,%#!7 .&#1',($ (%#3 "#) '7 *#($! .'5#, Low Pass Filter &''6$$
'#5, :7!# &'63$#& '7) "') )1',() ,$3%) ,#8" )5*$ .7.3 2,-$ .'*#,') .'##*$ :%#4%) .#/&$
2,-) !1 .','5) ,#,/1 '"3 )8 ),*%$ )136 ,$() .,#8") !1 $',*& &6#%& .- 2,- #&#" !3 .'1!
.#*'%! #&88)# .'+,-) (/"$ zoom in 3#5'$ ,paste-# copy &#35%"$ 2,-) !#+71 ,,$4#)1 '+7
.'61) 2,-$ '#5,)

$%(0% 3")&+ 9).8 .1 9).8 $'+03 $-#03 :7.3 !"#

18
Matlab-! "#$%

Handle Graphics &+ !"*(""5'- 7.2

-GUI ,5#'# 3('% -'5% Matlab .&,83$ .''4'4$) 0#0,1) '$'7, .) Handle Graphics '0*''$#"
.0*''$#") !1 &#6#7&) &" &#61! :&'6 #7,(1 handle ,1#*% )87 0*''$#" !7! ..'
,7.4 2,-$ ,"#&%7 "') Handle Graphics 0*''$#" !1 )'7,,'))
Root

Figure

Axes Uicontrol Uimenu Uicontexmenu

Image Light Line Patch Rectangle Surface Text

Handle Graphics 2'%%+)/ ., 3%7""%3 :7.4 !"#

'(7 !1%! .2,-) '0*''$#" !1 &#!&)% 3$#6 Matlab-$ Handle Graphics !1 '7,,')) 3$0)
)*'+,- :#!/ .'$''/ .','5 ,''5! '(7 .(Axes) .','5 &7,3% 9',5 Matlab-$ (Line) #* 00,1!
.(Figure)
..'6#1 .'-#4% )*'+,- '0*''$#" ,+4% .'$!#1% (5'7 )1'/%%) "%-#( &-5#% 7.5 2,-$

3'%*"# %2'%%+)/ "*6- +).%, :7.5 !"#

19
Matlab-! "#$%

.)*'+,- 0*''$#" '-#4 .'0,#+% 7.1 )!$0$

)*'+,- 0*''$#" (#*+& 0#,+ 0*''$#" -#4


)*'+,-
.'"5"5 .) .'0*''$#") ,"1 !7# )87 (/" 0*''$#" *, .''* .94%) &-#5&$ 0!#1 Root
.#'&#6#7& &" &#61! :&'6# '0%#0#" :+#"$ )8) 0*''$#") &" ,5#' Matlab .#!1
.)*'+,-) &#6#!/ ,+4% &" !'$-% #66'" Matlab .)*'+,- &#6#!/ &,'5'$ .'0!#1 Figure
.) .'0*''$#") ,"1 !7# root-) !1 (children) .'(!' .) .'-Figure-) !7
.Figure-) !1 .'"5"5)
popup ) .'/&+6 .'0',+& !1%! .'+,- *1%%$ 0!* .'!$*%) .',*$$ .'0!#1 Uicontrol
0*''$#"! axes 0*''$#" :'$ &#!& :'" )'7,,')) '+! '7 $! .'16 ,!1%! .(menu
.Uicontrol
.7.6 2,-$ -5#%1 '+7 )*'+,- :#!/ 1",$ .'%*#%%) .'0',+&$ .'0!#1 Uimenu
,#&+7 &#35%"$ )*'+,- 0*''$#" !3 .'1'*% ,1"7 .'55) .'0',+&$ 0!#1 Uicontextmenu
.'6%') ,$73)
image, ) #!1 .'(!') &" ,(4%# 2,-) -5#' #$1 )*'+,-) :#!/$ .#/& ,'(-% Axis
.!"6) .#/&) 9#&$ (light, line, patch, rectangle, surface, text
.'%''* .colormap .- '!#"# )6#%& &5',0%% $7,#% Matlab-$ )6#%& 0*''$#" Image
indexed, -.'!4*'+) 3$57 &1,#+% )6#%&) &5',0% :)$ .'7,( '-#4 )1#!1
.intensity, truecolor
.surface-# patch '0*''$#" !3 .'3'+1%) ),#"& &#,#*% ,'(-% Light
.&'(%%-&!&# &'(%%-#( )*'+,-! .'##* .',5#' .&" .''4'4$) .'06%!") Line
&#1%&1% fill #%7 &#'5*6#+ .&##5* '!3$ .'"!#%% (.'3!#5% $,) .'6#-'!#+ Patch
.#!" .'0*''$#"$
.)4+'!"! :$!% :'$ 3#6! &#!#7') &#,#5 '!3$ .''(%%-#( .'/01% Rectangle

,#1'% !3% )$#-7 )5',0% !1 06%!" !7 0#0,1 '"3 )5',0% !1 '(%%-&!& -#5'' Surface
.x-y
&#1%&1% title, xlabel, ylabel #%7 &#'!3 &#'5*6#+ ..'##& &#8#,/% Text
.#!" .'0*''$#"$

3'%*"# 2'%%+)/ %#)6 :7.1 3.+2

Uimenu 2'%%+)/ %"1 5%2.,(3 5%2%"*$ :7.6 !"#

20
Matlab-! "#$%

0%.$/ )$%#%, )'%3%31! )'%#-('.! )'4#+"1 0%.$/ )$%#% 7.2.1

0*''$#" ,5''! '(7 !1%! ..1 #&#" &!3$ 0*''$#") &,'5'! )'5*6#+ &%''* )*'+,- 0*''$#" -#4 !7!
&#1%&1% '#7# surf ,plot #%7 ,&#' "&#'!'3" &#'5*6#+ .surface &'5*6#+$ .'1%&1% Surface
,#5'! '(7 #!") &#'4'4$) &#'5*6#+$ 1%&1)! :&'6 :7! ..'+,- ,5''! '(7 #!") &#'4'4$) &#'5*6#+$
..7!1% )*'+,- &#'5*6#+
,'!!7 :+#"$ 35#$% &#'4'4$) &#'5*6#+) &#35%"$ 2,- 0*''$#" &,'5'

handle = function('propertyname',propertyvalue,...)

,(#*) "%-#(!

7.7 2,- &" ,5#'

7.7 !"#

,(#*) &#61,+
:#!/ '(%%# .#*'% &" &3$#* figure 0*''$#" !1 position &6#7& .#!1% handle 0*''$#" !7!
,.',$'" 4 !3$ ,#0*# 4"3 )*'+,-)
4left, bottom, width, height 5
)6'+)% )*'+,-) :#!/ !1 )6#&/&) &'!"%1) )6'+) !1 .#*'%) &" &#-5''% bottom-# left ,1"7
'"3 &#3$*6 &#('/') .)*'+,-) :#!/ '(%% &" &#3$#* height-# width .94%) !1 )6#&/& &'!"%1)
.Units &6#7&

21
Matlab-! "#$%

Axes-$ Color &6#7& ,&"8 &%#3! .)*'+,-) :#!/$ 3*,) 3$5 &" &6''5% Figure-$ Color &6#7&
..','5) &7,3% !1 3*,) 3$5 &" &6''5%
..','5) &7,3%$ .'*'0) &" &#3$#* Ytick-# Xtick &#6#7&
.x-y-z ,#1'%$ /01%) &#3$#* Zdatat ,Ydata ,XData &#6#7&

!-%.$/ 2-%%1'" ,& )'('5) )4%1-' ),1- 7.2.2

!7 0#,'+ &#,1+"% .- :) 24#6$ .)*'+,- 0*''$#" !1 &#6#7& &#,'8/%# &#3$#* get-# set &#'5*6#+
.6&#6#7& !1 .'',1+") .'7,3)
,)*'+,- 0*''$#" &6#7& !1 9,3 &3'$*! '4'4$) .#1',)

,)*'+,- 0*''$#" &6#7& !1 9,3 &!$*! '4'4$) .#1',)

)6#7&$ $5#'1 9,3) &"8 &%#3! .&#8#,/% ('%& .) PropertyName &#6#7& !1 &#%1 '7 $! .'16
.'4'4$) .#1',$ :'#5%1 '+7 &8#,/% /,7)$ &#')! $''/ "! PropertyName

,(#*) "%-#(!

.!#-'3 "#) '/7#6) marker-) ." :#-06+! surface /01% !1 marker-) &" )61%

,"#) )'#6'1 "!! &%'#4% )6#7& !1 .'',1+") .'7,3) &%'1, &-5)! '4'4$) .#1',)

,.#1,6 marker &6#7& !1 .'',1+") .'7,3) &" -'5)! '(7 !1%!

,!$*6#

.!(/% &,',$ 9,3 &" &#6''5% &#!4!#4%) .'',-#4)


,(#*) !1%! .set, get &#'5*6#+ &#35%"$ #,5#61 .'+,-$ .''#6'1 35$! :&'6

.7.8 2,- &" ,5''%


.&,5''% plot &'5*6#+1 line 0*''$#"! handle "#) h

Handle Graphics Online Documentation ),83) ;$#*$ &%''* .)'&#6#7&# )*'+,- !1 .'0*''$#") !7 !1 )%'1,6
.(html 0%,#+$)

22
Matlab-! "#$%

7.8 !"#

,(#*)

.7.9 2,-! 7.8 2,- &" )61%


.line-) 0*''$#" &#6#7& &" !'7%) structure 9,3% "#) a

7.9 !"#

23
Matlab-! "#$%

5 !"#$!
:!"#$!! %"&'(
2 .................................................................................................. Editor/Debugger&#!' .8
3 ........................................................................................................... (#)*+ (),-, 8.1
3 ..............................................................................................................'(.% (#!% 8.2
3 ................................................................................................ /,##( (.!'+# (",-% 8.3
4 .............................................................................................. (Bookmark) +,,0%,1 8.4
4 .................................................................................................... /,,3#%,3 /,)#-,2 8.5
5 ......................................................................................... (Flow Control) +%,)4 ()2$ .9
7 ................................................................................................................ if (#5#2. 9.1
7 ...................................................................................... else if-# else (#5#2. 9.1.1
9 ......................................................................................................... switch (5#2. 9.2
10 ................................................................................................ catch-# try (#5#2. 9.3
11 .........................................................................................................while ("!#! 9.4
12 ............................................................................................................ for (#"!#! 9.5
12 ............................................................................................... 1250," )#62# 9.5.1
13 ....................................................... (#"!#! (#*-%"$ +-,)6%$ /,)$," ($-+ 9.5.2
15 ............................................................................................................ Continue 9.6
16 ................................................................................................................. Break 9.7
17 ................................................................................ m. ,-$2 (-)+% (%52#% +",-, 9.8
Matlab-! "#$%

Editor/Debugger )'*+ .8

Editor/Debugger $%&' :8.1 !"#

+4 &#!'$ .*-#$% Matlab-$ (#07(+ !3 *,)7%+ $#)+ #7)5 &#!'+ "#+ Editor/Debugger &#!'
.Matlab-$ 5#2+ ,-$2 /+3 .m ,-$2 /,$(#7
,)'" ('" (#*-#$% (#5#2.+ )%#!7 ,script ,-$2 "#+ 5'"+ .m ,-$2 ($,(7! /,6%)#. ,03 /,%,,2
+)4' (7,1'$ "#+ script 6%)#.$ ,47)%+ 3#%,3+ .Command Window-$ #$(70 &+3 #!,"7 +,03+
6!. (#),4'%# 6!2 (#!$2%+ (#,-20#. "#+ ,03+ 6%)#.+ .Command Window-$ (#5#2. 8-) !*
(#,-20#.+ !7 ,C-$ (#!.%#2%+ (#,1,1$ (#,-20#. ).1%% :#' ./,,!$#!9 "!# /,,%,0. /+ +,0(3%3
.1type (,-20#. (#*-%"$ #!" (#,-20#. (#")! &(,0# .m :$#2$ +,-20#.7 (#,#0$ Matlab !3
;)5 #" Editor/Debugger &#!'$ F5 )#(.7 !* #" &%,1+ !* +-,'! ,"* (*-#$% .m ,-$2 (-)+
.Command Window-+

!"#$%
Dock (,,-.#" (),'$ ,"* (#0#!'+ )"3 /* 5', Editor/Debugger &#!' (" $!3! &(,0 -
.5).0$ #(#" ('(#. #+3!7 &#!'$ +0#,!* (,0%,+ +0,.$ &%,1+ !* +-,'! .View 6,).($
.+)#%3 +00," :$#2+ !3 (,'7#0+ +1)9+3 ;7 !* 5,*% .m :$#2 !3 ()(#7$ * &%,1 -

.(C-$ (#!.%#2%+ (#,-20#. ).1%% :#') Matlab !3 (#,6)5061 (#,-20#. &7#( (" +9,-% type (,-20#.1

2
Matlab-! "#$%

!"#$% !#&'& 8.1

*$-$ (#0%#1% (#)*+ .#,)'"! ,#-)+ 6126+ ($,(7# % /,4#'"+ &%,1 (#*-%"$ (*-#$% (#)*+ (),-,
.&,6#!'! &+% /!*(% Matlab -# 2#),
&(,0 .Text 6,).($ Comment (,,-.#" (),'$ #" Ctrl+R (5!2+ ,"* +)*+! /!3 *62% ;#.+! &(,0
Uncomment (,,-.#" (),'$ #" Ctrl+T (5!2+# ,#-)+ *62%+ &#%,1 ,"* /,4#'"+ (" ),1+!
.Text 6,).($

(!)* !"+* 8.2

'(.% (#!% .Matlab !3 (#)59++ ,.! "!" &+$ 3%(3+! +)2% !7$ )#1"3 '(.% (#!% ).1% (#%,,2
,'(.%+ (#!% &!+! .!#'7 *$-$ (#0%#1%

,"#+ '(.%+ (#!%$ !#1. 3#%,3! "%9#5

,&""! !)+(%" !-&'* 8.3

&(,0 #(#*-%"$3 Find & Replace &#!' (" ('(#. Ctrl+F !* +-,'! #" &%,1+ !* +-,'!
,/,##( 8,!'+!# "#-%!

()%%* +,&'+% -%,)' $%&' :8.2 !"#

3
Matlab-! "#$%

(Bookmark) %&&.*&/ 8.4

(,,-.#" (),'$ #" Ctrl+F2 !* +-,'!# +,#-)+ +)#3$ &%1+ (%3+ ,"* *-#$% +,,0%,1 (5)#+# (.1#+
.Edit 6,).($ Set/Clear Bookmark
.+"$+ +,0%,1! &%1+ (" +),$*% Edit 6,).($ Next Bookmark (,,-.#" (),'$ #" F2 !* +-,'!
+,0%,1! &%1+ (" +),$*% Edit 6,).($ Prev Bookmark (,,-.#" (),'$ #" Shift+F2 !* +-,'!
.(%5#2+

(#2#')+ (#,.,-.1 (#)#3 &,$ )#$*! #00#-)$ /+$ /,7#)" .m ,-$2 )#$* )2,*$ (,3#%,3 #4 +,-.#"
.+,03+% ('"

%#$%
./,-$2 (),91 (*$ (#)%30 &0," (#,0%,1 -

,&&1"*&1 ,&#"'&0 8.5

,/,,3#%,3 /,)#-,2 ).1% !3 /#7,1 &!+!

+!#*. )#-,2
copy Ctrl+C
paste Ctrl+V
cut Ctrl+X
undo Ctrl+Z
redo Ctrl+Y
select all Ctrl+A
Balance Delimiters2 Ctrl+B
close file Ctrl+W
new file Ctrl+N
go to line Ctrl+G
find & replace Ctrl+F
add remark Ctrl+R
remove remark Ctrl+T
Set/clear Bookmark Ctrl+F2
Next Bookmark F2
Prev Bookmark Shift+F2
Save Ctrl+S
exit execution Ctrl+C
exit execution Ctrl+Break
exit Matlab Ctrl+Q

/,,3#%,3 /,)#-,2 ).1% :8.1 +!$6

.&%1+ "-%0 &+$3 /,,)9#1+ &,$ "-%0+ 5#2+ !7 &#%,1 ",+ #4 +,-.#" (#*%3% 2

4
Matlab-! "#$%

(Flow Control) !,%$- .$/0 .9


,Matlab-$ +%,)4+ ()2$ (#5#2. &!+!

3#%,3 +5#2.
/,,9#! /,"0( ;%1 !* (#5#2. :$2% (*-$% else-# elseif (#5#2. /* 5', if (#5#2.
+0(3% !3 ;)* 1"* (#0#3 (#5#2. :$2% (*-$% otherwise-# case (#5#2. /* 5', switch +,0(+
*#-,$+ &%4$ (#*6 (!9(% /" (#5#2.+ $#(,0 (" +03% try…catch (#5#2.
,9#! ,"0( 1"* ,/,%*. !3 !$9#% ,(!$ ).1% (#5#2. :$2% (*-$% while +"!#!
*#$2 /,%*. ).1% (#5#2. :$2% (*-$% for
+"!#!$ (#)(#0+ (#5#2.+ *#-,$ "!! while #" for ("!#! !3 +"$+ +,-)6,"! 9!5% continue (#5#2.
while #" for ("!#! *#-,$ (" 2,1.% break +",-,
+! +")23 +,-20#.! )#4'! +-)+! /)#9 return (%52#%

9.1 +&./

(*$#2+ end (5#2. (.)#-% +,0(++# +"!#!+ (#5#2.! .5#2+ *#-,$ )51$ (#6!#3 !"0+ (#5#2.+ !7
3
(%" (#,+! !#7, ,9#! ,"0( .,9#! ,"0( "#+ 5#2+ *#-,$ ,"0( while-# if (#5#2.$ .+5#2.+ /#,1 ("
"#+ ,9#!+ ,"0(+ /" 2) 5#2 (#*-$% #!" (#5#2. ,(3 )%#!7 .(0 ,)%#0 ;)*) )23 #" (1 ,)%#0 ;)*)
.(%"
.9.2 +!$6$ /,6)#.%+ /,,1', #" /,,9#! /,)#6).#"% ,9#! ,"0( (#0$! &(,0

+2,9#! ,)#6).#" 1', ,)#6).#"


(#*%3% )#6).#" (#*%3% )#6).#"
,9#! AND & &62 !
,9#! OR | +##3 &62 <=
,9#! NOT ~ !#59 >
,9#! XOR xor +##3 !#59 >=
4
/,)$,"+ !7 /" &#70 all +##3 ""
1."% /,0#3
(#'.! /,,2 /" &#70 any (+0#3) +##3 #0," ~=
1."% +0#3 5'" )$,"

9.2 +&./

isempty +,-20#.+ !3%! .is* &#091+% /,$-% (#25#$+ (#5#2. (#*-%"$ ,9#! ,"0( (#0$! /9 &(,0
.+2,) +-,)6% ",+ +-,)6% /" (25#$

,(#"%9#5
,,"0(+

.0 ;)* $,0%

,,"0(+

.1 ;)* $,0%

.,9#! (%"! $3'0 1."% +0#3+ ,)%#0 ;)* !7 Matlab-$ 3


.+"##3+ ",+ == (#*%3%# +$-+ ",+ = &%,1 (#*%3% ,7 )#74! $#3' .== /#2%$ = &%,1 ($,(7 ",+ 5"% +-#.0 (#*6 4

5
Matlab-! "#$%

?,9#! ,"0($ /,)!21 /#2%$ (#-,)6%$ /,3%(3% )3"7 +)#2 +%


5
. !5#9 #(#"% (#-,)6% B ,A ,7 ',00 ,!3%! .#(#" /,%,,2% /,)$,"+ !7 /" 2) (%" "#+ ,9#!+ ,"0(+
./,%"#(+ B ,)$," !7% /,!#59 A ,)$," !73 "#+ A>B ,"0(+ (#*%3%

.+",93 (*5#+ (!$2(% 4" !5#9 #(#"$ &0," (#-,)6%+ /"5

6
Matlab-! "#$%

if !"2"0) 9.1

,",+ if (5#2. !3 )(#,$ (,1,1$+ +)#-+

)'"! (#5#2.+ *#-,$$ ;,3%, Matlab-# #*-#$, end-+ 5* (#5#2.+ !7 4" (%" "#+ ,9#!+ ,"0(+ /"
*#-,$$ ;,3%%# end-+ 5* (#5#2.+ !7 !* 9!5% Matlab 4" )23 "#+ ,9#!+ ,"0(+ /" .end-+
.end-+ )'"! (#5#2.+
,"%9#5!

,/,!#23 #!"+ /#3,)+ ,03 .1."% +0#3 ;)* !77 )59#% '(%"' ;)* Matlab-$3 !!9$

,)$," !7 )3"7 (%" "#+ ,9#!+ ,"0(+ +4 +)2%$ .+-,)6% /9 (#,+! !#7, if (5#2. !3 ,9#!+ ,"0(+
.1."% /,0#3 +-,)6%+
,!3%!

,)!21! +-,)6% &,$ ("##3+

else if-' else .'1'/2 9.1.1

.(.1#0 +,0(+ (#3%% elseif-# else (#5#2.

else (5#2.

(else if (5#2. #") if-+ (5#2. !3 ,9#!+ ,"0(+ /" (#*-#$% +! (#)#32+ (#5#2.+ .,9#! ,"0( +! &,"
.)23 #,+
,"%9#5

7
Matlab-! "#$%

else if (5#2.

-+ (5#2. !3 ,9#!+ ,"0(+ 4" )23 +,+ (%5#2+ (else if (5#2. #") if-+ (5#2. !3 ,9#!+ ,"0(+ /"
.(#*-#$% +! (#)#32+ (#5#2.+ 4" (%" "#+ /"# $3#'% else if
,"%9#5!

.else if (#5#2. !3 !$9#% ,(!$ ).1%# ,5,', else-$ 3%(3+! &(,0 if (5#2. /* 5',$
,($7)#% "%9#5

!2+! ,57 +)#3 !7 8#1$ Enter !* +-,'! ,"* ,6%#6#" &.#"$ *-#$%+ 2#1,.$ 3%(3+! 5"% :!%#%
.&,6#!'! &$#% ,(!$! ;.#+ 5#2+ 5"% )+% Matlab !3 indenting-$ 3#%,3+ "!! .$(70+ (0$+ !*
,indenting "!! 5#2 #(#" !3%!

%#$%

,(#0%#1% 5#2 (#)#3 !3 (Indenting) '##,)! (#,)3." ).1% &03,


.Ctrl+[ -+!"%3 5'" tab-$ &%#1%+ 5#2+ (44+ .1
.Ctrl+] -+0,%, 5'" tab-$ &%#1%+ 5#2+ (44+ .2
.Ctrl+I -(#)#%3+ (#5#2.+ ,.! '##,) .3

8
Matlab-! "#$%

switch !2"0) 9.2

.((4#)'% #" )!21) +0(3% !3 ;)* 1"* (#5#2. (*-$% switch (5#2.
,"#+ switch (5#2. !3 ,!!7+ +0$%+

"! "#+ expression ;)* /" .expression !3 ;)*! /"(+$ *-#$% 5'" case 2) switch (5#2.$
+)2%$ (#$) /,(*! .otherwise-$ (#5#2.+ (#*-#$% 4" /,-case-$ /,0,#-%+ /,7)*+% 5'"
.expression !3 ,#.- #" ,#-) "! ;)* !$2(+3 !!9$ 6+-)++ (" /,2,1.% otherwise
,"%9#5
,)!21 "#+ a

,"%9#5
,(4#)'% "#+ name

./,"( ;)*%7 ,"0(+ ,#6,$ (%3+ ,"* (,0%4 #$ /,"0( ).1% ;,)*+! +!#7, switch (5#2.
,"%9#5!

.+-)++ *#-,$ (" +2,1.%+ error (,-20#. /,$,-% 6

9
Matlab-! "#$%

catch-" try !"2"0) 9.3

,"#+ catch-# try (#5#2. !3 ,!!7+ +0$%+

(#5#2.$ +",93 +)-#0 "! /" .+",93 ()-#0 )3" 5* (#*-#$% statements_try (#5#2.+ +4 +0$%$
(#5#2.$ +",93 +)-#0 /" .statements_catch (#5#2. *#-,$ "!! (21.0 +-)++ ,#!"
-$ /9 /" .statements_catch (#5#2.+ *#-,$! )$#* Matlab 4" statements_try
.+-)++ (" 2,1.% Matlab 4" +",93 +)-#0 statements_catch

,"%9#5

+",93! 5* catch (5#2. (#*-#$% &7! .+",93 ,+#4# 0 1250,"$ )#62##! $,-+! +10% try-+ (5#2.
.statements_catch-$ +0#3")+
(#5#2. *#-,$ (*$3 $#6 ,#7,1 3,3 #0! *#5, )3"7 "#+ catch-# try (#5#2.$ ,)2,*+ 3#%,3+
#*-#$, ,21.,( +-)++3 /#2%$ ,+47 +)2%$ .5#2+ *#-,$ (" 2,1.(3 +!2( +)2( statements_try
.statements_catch (#,$,60)6!"+ (#5#2.+

10
Matlab-! "#$%

while !-+"+ 9.4

/,%*. !3 !$9#% ,(!$ ).1% end-! 5* while (5#2. &,$ (#"-%0+ (#5#2.+ !* ()4#' while ("!#!
,while ("!#! !3 ,!!7+ /#3,)+ .(%"! ;.#+ ,9#!+ ,"0(+3 5*

.statements (#5#2.+ *#-,$ 8#1$ /*. !7 $3#'% ,9#!+ ,"0($ +5,%*+-," )3"7
;)* $,-+! ,#-) &7!# +"!#!+ ;#(! +1,07 ,0.! ('" /*. $3#'% ,"0(+ while ("!#!$ ,7 $! /,30
.+"!#!+ ;#(! 107,+! ,57 /,,2(, "! ,9#!+ ,"0(+ +0#3")+ /*.$3 ;7 ,(!'(+

,"%9#5
.(!!#7 "!) ,!,!3+ &#3")+ )$,"! 5* )#62#+ ,)$," (" /71% "$+ 5#2+

21.,( "! /!#*! +"!#!+ #$3 $-% &7(, ()'" ,#+3,(% /,,2(, +),-*+ ,"0( ,7 $! /,3! $#3'
.Ctrl+Break #" Ctrl+C !* +-,'! ,"* (*-#$% +%#,1 ,0.! +-)+ (),-* .+%-*$

11
Matlab-! "#$%

for !"-+"+ 9.5

,(,!!7+ +()#- ./,%*. 3")% *#$2 ).1% (#5#2. :$2% (#*-$% for (#"!#!

(#5#2.+% 2!' (#$#)2 /,(*! ,/!3 ).1% #!,." #" 1250," (#,+! $,,' "! i +0(3%+3 (#)%!
.(#-,)6% #" /,)#62#! 1250," )#($ i +0(3%$ (#3%(3%
,"%9#5
.x )#62#$ num ).1%+ *,.#% /,%*. +%7 3.'% "$+ 5#2+

!* !,*,# ),+% &.#"$ (!*#.+ +.3 ",+ Matlab .)3."+ !77 for (#"!#!% *0%,+! &'( :!%#%
.!5#9 ,)51$ 5#2+ (#),+% (" ).3, (#-,)6% !* (#!*#.+ (#,6)5061 (#5#2.$ 3#%,3 .(#-,)6%
,"$+ &.#"$ !"0+ 5#2+ !7 (" /3,,! &(,0 ,"%9#5!

3/1(%" $'4/' 9.5.1

/#3,)+ (#*%3% .for-+ (5#2. ()59+$ +-,)6%$ 3%(30 +,-)6," !7$ 1250," )#62# !$2! ,57
,,!!7+

.A(:,k) )%#!7 ,A +-,)6% !3 (,-k-+ +5#%*+ "#+ 1250,"+ )#62# k +,-)6," !7$3 ",+
,"%9#5!
,+-,)6%+ +0#(0
#1 5 9 13 17 $
%2 6 10 14 18 &&
A"%
%3 7 11 15 19 &
% &
'4 8 12 16 20 (
,5#2+

12
Matlab-! "#$%

,Command Window-$ )-,,%

.+-,)6%$ (#5#%*+ ).1%7 "#+ (#,-)6,"+ ).1% ,7 $! /,30

.'"*'* .'5#,"0 !#%$4,0 6%$0%" .0#! 9.5.2

.1250," &%1% i )3"7 (#-,)6%# /,)#62# ;#($ /,7)* /,$,-% (#$) /,(*! for-# while (#"!#!$
,(#"!#! (#*-%"$ +-,)6%$ /,)$," $,-+! (#,)2,* /,7)5 3#!3 (#%,,2

+$')+ + (#2,) /,,)9#1$ !#'(," .1


(#5#2.! /"(+$ &75*(,# ,*#5, "! +0(3%+ !5#93 "#+ (#2,) /,,)9#1$ +0(3% !#'(" (#*%3%
.(#"$+
,"%9#5
.for ("!#! (#*-%"$ abs (,-20#. /#3,,

.5'" )$,"$ !59 x_abs +,-)6," !7$

13
Matlab-! "#$%

#+3!7 !#'(" "!! +),3, +$-+ .2

./,7)*+ /,$-#% +,!" +-,)6%+ !5#9 +,+, +% 3")% *#5, "!3 (#',0%# (#!#23 !"0+ /,7)5+ ,(3

3")% *#5, !5#9! +-,)6%+ !#'(," .3

(" !'("! &(,0 &7! .x ,)#2%+ )#62#+ ;)#"! ++4 (#,+! ;,)- x_abs )#62#+ ;)#" ,7 )#)$ #4 "%9#5$
,"$+ &.#"$ #075*!# /,1." )#62#7 x_abs

?3%(3+! +6,3 #4,"$

+$,1+ ./,1." (-,)6%$ #(#" !'("! 8,5* 4" /,7)* 1,07+! /,-#) #,!" +0(3%+ !5#9 3")% *#5, /"
#! (#-2+! ;,)- "#+ &#)7,4 +%7 *5#, "#+ +0(3%+ !5#9 (" 3")% *5#, Matlab )3"73 ",+ ;7!
.#! +-2% "#+3 &#)7,4+ (#%7 (" #" +0(3%+ !5#9 (" &75*! ;,)- #0,"#

14
Matlab-! "#$%

Continue 9.6

(,'7#0+ +,-)6,"$ (#)(#0+ (#5#2.+ )"3 !* (9!5% while #" for ("!#! ;#($ continue (5#2.
+"!#! ;#($ ("-%0+ +"!#! ;#(% (*-#$% continue (5#2. /" .+"$+ +,-)6,"+% +7,3%% +-)++#
.5$!$ (,%,0.+ +"!#!+ ;#($ 2) (#)(#0+ (#5#2.+ !* (9!5% continue (5#2. 4" (,0#-,'

,"%9#5
,+"$+ X [5 ) 7] +-,)6% #0! +0#(0

#1 4 2 3 5 3 2$
%1 2 3 4 5 3 1 &&
%
X " %1 3 4 3 3 3 5&
% &
%4 3 2 1 2 5 4&
%' 3 2 2 5 5 3 3 &(

,03 /* X-$ /"#(+ )$,"+ *-#%%% )$," !7$ ($7)#%+ X_new +35' +-,)6% $3'! /,-#) #0"#
,",+ "'1#0+ &7! .+)#3 +(#"% #,073
X(i, j * 1) + X(i, j) + X(i, j + 1)
X _ new(i, j) "
3

,(#,+! ;,)- X_new +-,)6%$ (3,3)-+ )$,"+ &7!

X(i, j * 1) + X(i, j) + X(i, j + 1) 3 + 4 + 3


X _ new(i, j) " " " 3.3333
3 3

)$," (" (3)#5 +"##3%+ X _ new(4,1) (" $3'! ,57 !3%! .(##-2+ )#4"$ ",+ +5,',+ +,*$+
!3 )(#,$ (,0%,+# )(#,$ (,!"%3+ +5#%*+ ,7 ),590 #4 +,*$ )#(.! ,57 ./,,2 "! &$#%73 X(4, 0)
+"##3%+ $#3,' (" *-$! ;)#- &," #!" (#5#%* )#$* .X (#5#%*% (#),3, #$7)#, X_new +-,)6%
,/,$#3,'+ *#-,$ !* 9!5! ,57 continue (5#2.$ 3%(30 &7!#

15
Matlab-! "#$%

Break 9.7

(*-#$% break (5#2. /" .+0%% ("-#,# +"!#!+ (" +2,1.% while #" for ("!#! ;#($ break (5#2.
/" .5$!$ (,%,0.+ +"!#!! :#'% ("-#, break-+ (5#2. 4" (,0#-,' +"!#! ;#($ ("-%0+ +"!#! ;#(%
.)-*0 :$#2+ *#-,$ 4" +"!#! ;#($ "! ;" .m :$#2 ;#($ (*-#$% break (5#2.

,"%9#5
.+5#%* +(#"% /,$#)2 /,073 ,.! *#-,%+ (" *-$! ,#-) (*7 ,7 ',00

#1 4 2 3 5 3 2$
%1 2 3 4 5 3 1 &&
%
X " %1 3 4 3 3 3 5&
% &
%4 3 2 1 2 5 4&
%' 3 2 2 5 5 3 3 &(

,",+ +"##3%+ )%#!7

X(i * 1, j) + X(i, j) + X(i + 1, j)


X _ new(i, j) "
3

(#)#3+ !* 9!5! /,-#) #0"3 5#*$ (j) (#5#%*+ !* +-)3 ("!#! ;#($ (%2#%% *#-,%+ ("##3% (*7
,(#5#%*+ ("!#!% ("-! ,57 break (5#2.$ 3%(30 ;7 /3! .(#,0#-,2+

16
Matlab-! "#$%

.m &'30 !'#%* !*20"* %-&'& 9.8

/" Command Window-! #" ,'7#0+ .m :$#2! ")23 .m :$#2! +-)++ (" +),4'% return (5#2.
.#7)5 :)#+ ,'7#0+ .m :$#2

",+ (,'7#0+ :$#2! ")23 :$#2! +",-, 2) "! )%#!7) !,!7 .m :$#2 (-)+% ("-! (,3#%,3 ;)5
Command -$ +9,-% ",+ +(#"3 (4#)'% 6!27 (!$2% #4 +,-20#. .error (,-20#. (#*-%"$
.+-)++ (" +2,1.+3 )'"! Window
,"%9#5
/,$,,' ,()'" . 0 ! a ! 10 /#'($ (#,+! $,,' a ;)*+ !$" b +0(3%$ a ;)*+ (" $,-+! 3)50
.+-)++ *#-,$ ;3%+ (" 2,1.+!
,5#2+

!"#$%
.+,-20#. (!$2%3 6!2 (2,5$! ",+ #4 +,-20#. !3 :#.0 3#%,3 -
Command -! +)+4" (#*5#+ '#!3! ,57 warning (,-20#.$ 3%(3+! &(,0 &7 #%7 -
.5#2+ *#-,$ (" 2,1.+! "! ;" Window

17
6 !"#$!
:!"#$!! %"&'(
2 ........................................................................................................... !"#$%&"' !(#!) .10
4 .......................................................................nargin, nargout, nargchk !"#$%&"' 10.1
4 ............................................... (number of input arguments) nargin !#$%&"' 10.1.1
4 ........................................... (number of output arguments) nargout !#$%&"' 10.1.2
6 ........................................................................................... nargchk !#$%&"' 10.1.3
7 ............................................................................................. !"#$%&"' *+ !"#$%&"' 10.2
7 ................................................ feval !#$%&"' !",$-.( /-+ 0", !"#$%&"' ,"$#( 10.2.1
10 ............................ 1#&!+- *+ +.2- 234"- #!*( 2'0- !"*(%-5 !"#$%&"' !2345 10.2.2
18 ................................................................................................. 1##*("*4 1#&!+- 10.3
19 ........................................................................................................ !"#$%&"' !! 10.4
20 .................................................................................................... toolbox !2#$# 10.5
Matlab-* ."(-

)'%#*('+ ),%)- .10


,!#**) 5#$%&"' *+ 5&(- *+ #2+'. 6"2#' /*5*
.5*+ 6*'5" 6*%5 #&!+- !." 5#$%&"'5 1+ !. 52#34- "7 52"+ – 52345 !2"+ .1
.(!",-+- ,*3"4 ,5&!+- 4"0) 6*'5" 6*%5 2".#! ,5#$%&"'5 !26- - 1#2(05 .2
.#"$2) 3%'!! 5#$%&"'5+ #3) /#%!" #%"8 ."5 6*%5 #) .3""- 57 %*8 - 6*% !%#3( .3
.1#&!+-* 1#!*8!5 1#)2, !,#(% - *"8!. .4
.1#("+#85 *) 1#,$"(- "( #7)2-5 %*85 - ("+#8 .5

,5.(5 52"$5- /5 Matlab !#$%&"' *+ !"&"+.25 !"2"+5

8!'- !*#- 6*'5 #&!+- 5#$%&"'5 1+ 6*%5 #&!+-

H1 !2"+

*+ help-5
5#$%&"'5

1+" help !3*%5 2-"*) .5#$%&"'5 *+ help-5 !. !"(#)2- 523455 !2"+ 28.* 3#- !"2,5
5&&#.+ 5&"+.25 52"+* 3, H1 !2"+- !"2,55 !. !"4#$- Command Window-( 5#$%&"'5
2"!( Current Directory /"*8( ,#'"- 57 2(05 .5#$%&"'* 2$% 2(05 5""5- H1 !2"+ .152,5
2-"*) .5#$%&"' *) *+ 2(05( 8!'- !*#- 28. !+'8- lookfor !#$%&"' 90"&(" Description
.5#$%&"'5 *+ H1 52"+( 5,#'"-5 5*#- !+'8- lookfor !#$%&"'
,.-4"3

.5%#2 52"+ **")1

2
Matlab-* ."(-

,5(#&- Command Window-( lookfor Calculates !(#!)

,5(#&- Command Window-( help poly_sine !(#!)

.5#$%&"'* 1#.2"% 1!",$-.(+ 1#&!+-5 !"-+* 1#57 !"#5* 1#(##8 1&#. 5#$%&"'( 1#&!+-5 !"-+
,!(#!) *+-*

,"-) !".$"!5 /!". !. 5(#&-

,1"+#25 5+,-*

,5#$%&"'5 *+ 21##*%"*5 1#&!+-* 1##24"0( 1#&!+-5 !($5 ,$(-

.1##24"05 :"!( 1#)2,5 !. !"2#+# 0#&)5* 14 /!#& /)*


,.-4"3*

.!".$"!5 /!". !. (#&-

.5#$%&"'* ;"8- 1#234"- .* 1##*%"* 1#&!+-- #) 2")7&2

3
Matlab-* ."(-

nargin, nargout, nargchk !"#$%&"' 10.1

(number of input arguments) nargin )%#*('+ 10.1.1

.5#$%&"' *+ 6*%( 1#&!+-5 2'0- !. 52#78- nargin !#$%&"'


,.(5 /'".( poly_sine 5#$%&"'* 5.#2% ,.-4"3*

nargin ! 4
,1#2%- #&+( 2%#,( 5("+8 "7 5#$%&"'
5.#4+ 22"4 #3- *"34 1#&!+- 2'0- !($5 .6*%5 !"&#%! !%#3( ."5 5#$%&"' *) *+ ("+8 %*8 .1
.2(05"
,6*%( 1#&!+- 5 !($5 .-4"3*

*, 6"*+* "&&"$2( 1. .1#234"- .* 1#&!+- *, 5.#4+ !",3"5 2"24# #3- /6% 1#&!+- 2'0- !($5
2'0- !. 52#78-5 5#$%&"'* 1#%"%7 "&. "&-,6* !"2"2( 2!"# 5.#4+ !",3"5 2#345*" 5.#4+5 !",3"5
.5#$%&"' *+ 6*%( 1#6&-"42.5

.1#&!+- *+ 5&"+ 2'0- 1, 5$25* !"&!#& Matlab *+ !"#623&605 !"#$%&"'5 *+ 2)#& %*8 .2
,6*%( 1#6&-"42. 1-3 1, 5$#25* /!#& #) 1#3-* "&. diff !#$%&"' *+ help-5- .-4"3*

!2#2( #)2, 1#*(%- "234"5 .*+ 1#)2,5 7. 1"-#0%-5- 1#&!+- !"8' 1, 5#$%&"' 1#$#2- 2+.)
!"#2+'.( 1#+-!+- 15 1. !"#2+'.5 *) !. 62'* 1#)#2$ 1&#. 1#+-!+- :) .(default) *38-
5#$%&"'* 1#%"%7 "&. 6*% #&!+- *+ 5&"+ 2'0-( *'6* !"#$%&"'* 2+'.* #3) /)* .*38-5 !2#2(
.5#$%&"' *+ 6*%( 1#6&-"42.5 2'0- !. 52#78-5

(number of output arguments) nargout )%#*('+ 10.1.2

.5#$%&"'5 *+ 6*'( 1#&!+-5 2'0- !. 52#78- nargout !#$%&"'


,.(5 /'".( poly_sine 5#$%&"'* 5.#2% ,.-4"3*

nargout ! 3
.1#2!"#- 1#("+#8( :"08* 1#$"2 2+.) 2%#,( 5("+8 "7 5#$%&"'

?1#&!+- !"8' +%(- +-!+-5 :. 1#&!+- 2'0- 52#78- .#5+ :) !234"- 5#$%&"' 2+.) 52"% 5-
."278"# 1#&"+.25 1#&!+-5 %2 :. ,",$"(# 5#$%&"'( 1#("+#85 *)+ .#5 5("+!5

4
Matlab-* ."(-

,1"+2& 1"&#*"'5 !. %2 *(%* "&&"$2( 1. ,.-4"3*

.(p-( 13"%- *(%!5) 1"&#*"'5 :2, ($"# w 5&!+-("


.+%#( .* **) +-!+-5+ 6*' #&!+- #)2, ("+#8* 1#(.+- 1#,%+"-" 28.- !#&7(7( .#5 "7 5+#4 :.
("+8 15( 1#2%- #&+ 1#-##% ,#**) /'".( :. !8. 3"% !2"+ %2 :"08# nargout-( +"-#+ "7 .-4"3(
,nargout-( +-!+5*
.("+#8 0-", !&#8(- #&,("! 6*'5 #&!+-- %*8 ("+#8 .1
.3.- *"34 5#$%&"'* !".#2%5 2'0- :. ,#&,("! .* ("+#85 .2

,1")#0*
#)2, !"#2+'. /#( 2"8(*" /#%! 6*%( 1#&!+-5 2'0-+ .3""* #3) nargin !#$%&"'( 1#+-!+- )"3(
+%#( .* +-!+-5+ 1#&!+- ("+#8( :"08* #3) nargout !#$%&"'( 1#+-!+- )"3( .*38- !"2#2(
.1!".
,1.!5( poly_sine !#$%&"' !. 5&+&

5
Matlab-* ."(-

!"#!

if, else-( +"-#+ .#5 !#(#6%*0 52"$( 1#("+#8 ,"$#(* switch !#$%&"'( +"-#+* !#(#6&26*. :23
.5#$%&"'5- !-3%"- 5.#$#* return 1, 38#
,.-4"3*

nargchk )%#*('+ 10.1.3

,& 6*%( 1#6&-"42.5 2'0- 1. !%3"( "7 5#$%&"' .nargchk !#$%&"' .#5 nargin *, 5(#(8 5#$.#2"
,#**)5 5-"+#2 .high :2,* low :2, /#(

.warning ". error !#$%&"'* 5,3"55 !. 1#2#78-" nargin-5 !#$%&"'- 1#*(%- number 2'0-5 !.

,5#$%&"'5 5&"!& .-4"3*

,1"+2& .4-* 2 /#( ."5 5*+ 6*%( /#%!5 1#&!+-5 2'0- #) ,(%&

,5.#4+5 !,3"5 !. *(%& 6*%( 1#&!+- 5 0#&)& ,*+-* ,1.

,5.#4+5 !,3"5 !. *(%& 6*%( 3#8# 5&!+- %2 0#&)& ,*+-* ,1.

.nargin 1"%-( nargout !($5 #", 6*'( 1#&!+-5 2'0- !%#3(* 14 "7 5#$%&"'( +-!+5* 14 /!#&
,!'0"5 .-4"3*

,5.#4+ !,3"5 !22"4 1#6&-"42. #3- 2!"# 1, 5#$%&"'* 5.#2%" poly_sine !#$%&"'(

6
Matlab-* ."(-

!"#$%&"' () !"#$%&"' 10.2

("!)* 1"%-( !*(%- .#5+ 5#$%&"' *) *, 1#("+#8 !,$(-5 5#$%&"' ("!)* #"$2 1#(2 1#2%-(
/5+*) !"#6-!- !"#$%&"' *+ 1#0'.5 !. !(+8-5 5#$%&"' .#5 56"+' .-4"3 .52%- *)* 5#$%&"'
.('")" 1#-"&#*"' ,1#0"&#0)
,#**) 3"% !",$-.(+ :) (!)#5* 5)#2$ !"#$%&"' *+ 5#$%&"'
.5#$%&"'5 1+ 0", !"28. !"#$%&"' !",$"(- .1
#!*( 1#4"0- 6*%( 1#6&-"42. *+ +.2- ,"3# #!*( 2'0- !"*,( !"28. !"#$%&"' !",$"(- .2
.+.2- 1#,"3#

feval )%#*('+ )'.#/", 0/& 1". )'%#*('+ .'#%, 10.2.1

function .@ /-#0 !",$-.( Matlab-( 234"-+ function handle3 4+"-( !+-!+- feval !#$%&"'
function handle-( !+-!+- feval !#$%&"' .5#$%&"'5 #(4* ,3#- *#)-5 ,3#- 5&(- ."5 handle
.2305 #'* 1#6&-"42.5 2.+ !. 5( 5(#$-" 5#"$25 5#$%&"'* ."2%* #3) (/"+.2 6&-"42.)
,1#*"%+ 1#.(5 1#-"+#25 #&+ ,.-4"3*

"

,/)"!- /6% 28(- /*5* .feval !#$%&"'( !"+-!+-5 !"#623&60 !"#$%&"' 2'0- !"-##%
. f (x) " 0 5.""+-* !"&"2!' !.$"- -fzero
.234"- *""26&#.( f (x) *+ !#*%"* 1"-#&#- !3"%& !.$"- -fminbnd
.#2-"& /'".( !"#2.&#* .* !".""+- !)2,- !2!"' -fsolve
.#2-"& /'".( !"*#42 !"#*.$&2'#3 !".""+- !)2,- !2!"' -ode45
.#2-"& /'".( f (x) *, 5#$246&#. !,$(- -quadl

inline $%&&'()

.inline 6%##(". !",$-.( .#5 5#$%&"' !2#$#* !2$"%- 52"$


52#78-" 53#8# 3"% !2"+ !(+8-5 Matlab !#$%&"' !2$##-" !"7"28- !*(%- inline !#$%&"'
!. 1#4$##- 1#6&-"42.5 2.+ .,$"(!+ 3"%5 !2"+ !. 4$##- /"+.25 6&-"42.5 .3#8# 6*' 6&-"42.
!. !%2"0 inline !#$%&"' .!#623&60 5#$%&"' *+ 1"+#2( 6*% #&!+- "-) 1#3%'!- 15" 1#&!+-5
(...,*,+,sin "-)) !"2)"- !"#$%&"'" 1#2"62'". 28. !+'8-" 5*+ /"+.25 6*%5 6&-"42.( !7"28-5
."234"5+ 1#&!+-5 28."

,557 /'".( !"3%'!-5 !"#$%&"' 1#2$"# 1#.(5 1#-"+#25 #&+ .-4"3*


5#$%&"' *+ #623&60 1"+#2 .1

inline 6%##(". .2

help-( !"62"'-5 !"(#0- :. function handle-( +-!+5* 1"%-( !7"28- 2"!( 5#$%&"'5 1+ !. ("!)* 14 /!#& 3
.function handle-( +-!+5* 9#3,

7
Matlab-* ."(-

,*#42) 1"+2& *"&5 !"+#45 #!+( 0"&#05 !#$%&"' !. (+8* #3)

"-) 33"( ("+#8 !"*,( !"#$%&"' !(#!) 2"(, 2!"# 2#5- 57 1"+#2+ :)( ."5 inline 6%##(".( /"2!#5
.32'& .m ;("%( 5#$%&"' 2#345* :2"$ /#." 580"&
6%##(".( +-!+5* ". ,!8. 53"%'- 2!"# !*,( 5#$%&"' ("!)* 2+'. #.+ ."5 57 1"+#2( /"2085
.38. 5&!+-- 2!"# 6*'( 2#785* ". ,28. inline

,!#623&60 Matlab !#$%&"' 2"(, inline 6%##(".( +"-#+* .-4"3


,!#6-!-5 5#$%&"'5 5&"!&
ecos( #x )
f (x) " , x $ [0,5]
x %1

1"-#&#-5 !3"%& !. ."$-* #3) .*"&5 1"8!( "75 5#$%&"'5 *+ 1"-#&#- !3"%& ."$-* .#5 "&#!26-"
,5.(5 5.""+-5 !. 2"!'*" 0'.* !274&5 !. !""+5* :#2$ #6#*&. /'".(

*ecos( #x ) & #(x % 1) sin(#x) % 1'


f +(x) " "0
( x % 1)
2

fminbnd !#$%&"' !",$-.( #2-"& /'".( 1"-#&#-5 !3"%& 28. +'8& /)*" 3.- 5+% "7 5.""+- !2#!'
,3"%5 .

.10.1 924 !. (#&-

,!"#5* *")# feval !#$%&"' *+ /"+.25 6&-"42.5 ,1")#0*


.(32'& .m ;("%( 5#$%&"' 5234"5) function handle .1
.(32'& .m ;("%( 5#$%&"' 5234"5) !7"28- .2
.1##+24 .**" inline 6%#(". .3

!"#!
,*+-* .5*+ 1##24"0( !.$-&5 !7"28- *) !,$(-5 eval 1+( 5#$%&"' !-##% -

.clear 53"%'5 !. !,$(-

8
Matlab-* ."(-

f(x)=ecos( #x)/(x+1)
3
ecos(#x) /(x+1)
x min

2.5

y
1.5

0.5
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
x

$%+')+ ,)-%(%- $.)'( /0&- fminbnd $%&'()* :10.1 !"#

9
Matlab-* ."(-

5%()&/ 2& &"$/ $34'/ %)2, $+1/ )'2,*/! )'%#*('+ )$34! 10.2.2

#)5 .-4"35 .1#&!+- *+ +.2- 234"- #!*( 2'0- 6"*%* !"*4"0- !"#5* !"(##85 !"#$%&"' /&+#
"7 !"2+'. .46"62+* 1#'24 *+ "5+*) 2'0- *(%* 5*")#5 plot !#$%&"' .#5 "7) 5#$%&"' *+ 56"+'
.feval !#$%&"' !",$-.( 5#$%&"' *) *, !*,"'5 !#**) 5#$%&"' *+ 52345( !#&"#8
,5+8-5
,/5*+ 1#0'.5 !. ."$-* 1#$"2 "&. /5* !"#6-!- !"#$%&"' #!+ !"&"!&

.f (x) " A sin(,x % -)


/
0f (x) " ax % bx % cx % dx % e
4 3 2

x, A, ,, - :6*% *+ 1#26-2' 4 1&+# 0"&#05 !#$%&"'*


x, a, b, c, d, e :6*% *+ 1#26-2' 6 1&+# *"&5 1"&#*"'5 !#$%&"'*
,.(5 /'".( !"33"%- "*. !"#$%&"'

,1"+2& 0"&#05 !#$%&"' 2"(, feval-( +-!+5* #3)

,5.(5 5($55 !,$(!-"


arg1 ! x arg 3 ! w
arg 2 ! A arg 4 ! phi
.1#6&-"42. #&+ 3", :#2$ 1"&#*"'5 !#$%&"' !. (+8* #3) :.
,1"+2& 1"&#*"'5 !#$%&"' 2"(, feval-( +-!+5* #3) 2-"*)

,5.(5 5($55 !,$(!-"


arg1 ! x arg 3 ! b arg 5 ! d
arg 2 ! a arg 4 ! c arg 6 ! e

feval-( +-!+5* #3) .5%"#3( 1#6&-"42. 4 1#(##8 0"&#05 !#$%&"' 1, feval-( +-!+5* #3) 2-"*)
!#$%&"' ("!)* .*. 52#2( *) /#. "7 5+#4( /)* .%"#3( 1#6&-"42. 6 1#(##8 1"&#*"'5 !#$%&"' 1,
.!#**) 5#$%&"' .#5 feval !#$%&"'( +"-#+5 !26- #) 2")7& :. .52%- *)* !*(+ feval

,"7 5#,( /"2!'* !"+#4 #!+ !"-##%


varargin !#$%&"' .1
structure :2,-( +"-#+ .2

.varagin-( !+-!+- 5&&#. /)*" C-( 5("!) "7 5#$%&"' 5+,-*4


.Function Handle **") .*5

10
Matlab-* ."(-

varargin ,&-%*(.

,."5 varargin !#$%&"'( +"-#+5 /'".

3, 6*%5 *+ 1#6&-"42.5 *) .varargin ."5 /"28.5 6&-"42.5" 1#6&-"42. 2'0-- ()2"- 6*%5
varargin *+ 1"%#-5- 6*% 6&-"42. *) .1#*#42 6*% #6&-"42. 15 varargin *+ 1"%#-( 6&-"42.*
.varargin 1+( 1#.! :2,- :"!( ($"- 5.*5"
,.-4"3
,.(5 /'".( !234"-5 5#$%&"' 5&"!&

,.(5 /'".( *"&5 5#$%&"'* 5.#2%

,1#)2,5 *,( varargin 1+( 1#.! :2,- !2$##-" 2 :2, x 5&!+-( 5(#$-

.varargin 1+( 1#.!5 :2,-( "($"5 #&+5 6&-"42.5- *85 1#6&-"42.5 *) 2-"*)
:2,-( 1#+-!+- 3$#) :. .6*% #6&-"42. *+ "5+*) 2'0- !*(%-5 5#$%&"' 2#345* /!#& 575 /'".(
?5#$%&"'5 :"!( varargin 1#.!5
:"!* 1(#$5*" varargin-- 1$*8* /!#& 7. varargin 1#.!5 :2,-( "#5#+ ,3#-5 #&(- #4"0 1#,"3# 1.
.1#-#.!- 1#&!+-

,5#$%&"'5 !234"- .-4"3*

11
Matlab-* ."(-

,2-"*) .52"+ 2"6%" :"!* 1#2*%0 52#--5

x 1 &1 2 3 4 5'

!. 6"*%#+ 2"6%" ."5 x #) 2#345* 5#5 /!#& /)*" 1#2*%0 "($"# varargin-( #) ,"3# 5#5 *"&5 52%-(
1#,"3# 1&#. varargin-( "($"#+ ,3#-5 #&(- #4"0 2+.) 52"% 5- :. .varargin #.!- 1#2*%05
?+.2-
,1#.!5 :2,- :"!- 1#&"+ 1#4"0- 1#&!+-5 !. ;*8* 5)#2$5 feval !#$%&"' ."5 6*"( #)5 52%-5
.1")#05 !-4"3( 143"#+ #') {:} #3"8#5 2"62'".5 ."5 "7 5#,(* /"2!'5 .5*+ 1##24"05 :"!*"

!(+8-5 5.(5 5#$%&"'5 !. /8(& feval, varargin, function handles !"#$%&"' *+ 1")#0 !-4"3)
,.m #$(% 5+"*+ "(!)& 1+* .0"&#0 !#$%&"' 2"(, 1#43&" !#**) 5#$%&"' *+ 1#0'.5 !.
.script ;("% .1
.0"&#05 !#$%&"' .2
.find_zeros_varargin !#$%&"' .3

.1

*+ 6*%( 1#6&-"42.5
:2,-( "($"#+ 5#$%&"'5
varargin 1#.!5

.2

12
Matlab-* ."(-

.3

13
Matlab-* ."(-

.*(%!- 10.2 924

0.8

0.6

0.4

0.2

0
y
-0.2

-0.4

-0.6

-0.8

-1
0 2 4 6 8 10 12 14 16 18 20
x

2# #3
0 8 x 8 20 ,)2$3 f (x ) " sin 4 x % 5 1)(%1/ $%&'()* :10.2 !"#
64 37

,!".(5 !".$"!5 !"4$"- Command Window-(

,2-"*) .5#$%&"' *+ 1##24"05 :"!( 2(#. 2(#. 1#.!5 :2,- !($5* *"%+ varargin{:} %#265

"
#

,("#!
.varargout 1+( 6*'5 #&!+- 2"(, 5-#*+- 5#$%&"' !-##% -
.varargout -" ,varargin !"#$%&"'( !"+-!+- !"#623&60 Matlab !"#$%&"' -

14
Matlab-* ."(-

structure /"#0' +(0&+

6*%( cell array :"!( 1#&"+ 1#4"0- 1#&!+- !($5 .#5 varargin !#$%&"'( +"-#+5 !",-+-
.structure :2,-( 5($5 .#5 !#(#6&26*. 5+#4 .5#$%&"'5 :"!( +"-#+* 1#.!5 :2,- :"!- 1$"*#8"

!",$-.( ,$"(# 1!#2"4*.5+ :) varargin !#$%&"'( "+-!+5+ .m #$(% !+"*+ !. (!)+& 57 %*8(
,(!)"+-5 3"%5 6"2#' /*5* .structure :2,-( +"-#+
,15 1#(!)"+-5 .m #$(% 5+"*+
.script ;("% .1
.0"&#05 !#$%&"' .2
.find_zeros !#$%&"' .3

.1

.2

15
Matlab-* ."(-

.3

16
Matlab-* ."(-

,15 1#$(%( 1##"&#+5


script-5 3"% .1
1+( :2,- 234"5 .1#-#.!- 1#)2,( structure-5 :2,- *"8!#. find_zeros 5#$%&"'* 5.#2% #&'*
,5#$%&"' *) *+ 1#26-2'5 !. *#)-5 params

0"&#05 !#$%&"' .2
,*"&5 structure-5 :2,- :"!- 61#&!+-5 ;"*#8

find_zeros !#$%&"' .3
.+.2- ,"3# 6*%5 *+ 1#6&-"42.5 2'0-" 28.- feval-( *#42 +"-#+

.varargin !#$%&"' !",$-.( "-) !".$"!" 924 "!". *(%!-

,(&-%*(. 1+ ,(&-%*(. %". 2(3&4

,#**) 3"% !",$-.(+ :) (!)#5* 5)#2$ !"#$%&"' *+ 5#$%&"'


". function handle) + feval !",$-.( 5#$%&"'5 1+ 0", !"28. !"#$%&"' !",$"(- .1
.(inline 6%##(". ". !7"28-
#!*( 1#4"0- 6*%( 1#6&-"42. *+ +.2- ,"3# #!*( 2'0- !"*,( !"28. !"#$%&"' !",$"(- .2
.structure :2,- ". varargin !#$%&"' !",$-.( +.2- 1#,"3#

2#+# /'".( structure-5- 1#&!+-5 #)2, !. (#$5* /!#&" #82)5 "&&#. structure-5 :2,- :"!- 1#&!+-5 ;"*#8 (*+ 6
.53+5 1+" 53"%& /"#$ 1,

17
Matlab-* ."(-

*##(+"(, *#&!)- 10.3

5* ".2%+ !"#$%&"'( .*" 5#$%&"' 5!". :"!( %2 1#234"- 2-"*) ,1##*%"* 15 5#$%&"' :"!( 1#&!+-
*)( ")2, !. !"&+* /!#&" 5#$%&"' *)( 234"- #*("*4 5&!+- !.7 !-",* .Command Window-( ".
.("-$, 5&!+-5 !. .*. 5&!+-5 *+ %!", %2 5&+- "&#. #*("*4 5&!+- *+ :2, #"&#+ 2-"*)) 1"%-
.global !#$%&"' !",$-.( !,$"(- 1##*("*4 1#&!+- !2345

5&!+-) ,Command Window-( ". script ;("%( )"3( ,"2#345* :#2$ #*("*4 5&!+-( +"-#+ #&'*
2")7* #3) !"*"34 !"#!".( #*("*4 5&!+- /##$* ;*-"- ,#82)5 57 /#.+ !"2-* .:2, "( (#$5*" #*("*4
.#*("*4 5&!+- /). "57 #)

/#." !"&+* "&&"$2( /#.+ 1#,"(% 1#*34 !,#(% ,*+-* .1##*("*4 1#&!+-* 1#+"-#+ 2'0- 1&+#
1#&!+-( +-!+5* 14 /!#& .(2".5 !"2#5-) #*%#0#' ,"(% "-)) 5#$%&"' *)* 12#345* "&&"$2(
,.-4"3* .5#$%&"'* 1#.2"%+ 1#-,'5 2'0- !. ,*+-* ,1#2'"05 1#&"- 2"!( 1##*("*4
,1"+2& script ;("%(

,5#$%&"' 2#34&"

."2%* :#2$ 5#$%&"'( #) (* 1#+& .1-( *34 NUM_CALL 5&"-5 *"&5 5#$%&"'* .2%&+ 1,' *)(
."( 1#+-!+-+ #&'* #*("*45 5&!+-*
do_nothing !#$%&"' 6*%( 0#&)5* 1#)#2$ "&##5 57 52%-( #*("*4 5&!+-( 1#+-!+- "&##5 .* 1.
,2-"*) .6*'( ".#$"5*" 5&"-5 !.

,!"(#0 #!+- 1##*("*4 1#&!+-( +"-#+- 5("6 !"8' "7 :23


.5#$%&"'* 5.#2%5 /'". !. !"&+* :#2$ 5-+##* #3) .1
!"28. !"#$%&"'+ 5#$%&"' .#5 !".#2%5 2'0- !. 2"'0* 1#$"2 "&. 5*+ 5#$%&"'5 1. .2
.5* !".2"%5 !"#$%&"'5 *) *+ 6*%5" 6*'5 !. 14 !"&+* :#2$ 7. ,5* !".2"%

!"#!
./"2)#75- .* :. workspace-5- %2 "!". %8"- #*("*4 5&!+- 1, clear !#$%&"'( 6"+' +"-#+ -
,1"+2& NUM_CALL"-) #*("*4 5&!+- %"8-* #3)

18
Matlab-* ."(-

!"#$%&"' !! 10.4

!#7)2-5 5#$%&"'5 .#5 5&"+.25 5#$%&"'5 .!8. 5#$%&"'- 2!"# *#)5* *")# 5#$%&"' 4"0- .m ;("%
5#$%&"'5 #28. !234"-5 5#$%&"' *) .5#$%&"'5 1+( 1#.2"% 2+.) !,$"(-+ 5#$%&"'5 .#5"
5#$%&"' 5!". *+ !"28. !"#$%&"' !!*" !#7)2-5 5#$%&"'* %2 5+#4&5 5#$%&"' !! .#5 !#7)2-5
28.* !"234"- /5 3", *) 5&+- "&#. !"#$%&"' !!5 230 .5*+- 52345 !2"+ 5#$%&"' !! *)* .!#7)2-
.!#7)2-5 5#$%&"'5
,.-4"3

newstats .#5 !#7)2-5 5#$%&"'5 .2"6%" *+ /.#3-5" ,$"--5 !. !(+8- newstats 5#$%&"'5
.mean, median 1+( 5*+ !"#$%&"' !! #!+ "234"5"
!"#$%&"' :) 28. ,1+ "!". !"*,( !"#$%&"' !! 28. 5*#8! +'8- Matlab ,5#$%&"'* 5.#2% !,(
5#$%&"' !! "&2345 2+.) *"&5 .-4"3( /)* .search path-( !"#623&60 !"#$%&"' 9"0(*" 7!"#62'
"!". !*,( !#623&605 5#$%&"'( 1"%-( 5#$%&"'5 !!( +-!+! newstats !#7)2-5 5#$%&"'5 mean
.1+

.private functions .+"&5 28. help-( "+'8 !"#62' !"#$%&"' #(4* 2(05*7

19
Matlab-* ."(-

toolbox !.#$# 10.5

#+.25 /"*8( .!"#"$25 !"#$%&"'5 *) !. 5*#)-5 5#%#! 2"$#* 5*#8! :#2$ toolbox 2"$#* #3)
*) 1#4$"- 57 /"*8( .10.3 924* 5-"3( ,#'"# Set Path /"*8" Set Path... *, "$8* File 6#2'!(
"28( 1)*+ 5+385 toolbox-5 !##%#! !. 9#0"5* #3) .!"#$%&"' 28. +'8- Matlab /5(+ !"#%#!5
.5#"$25 5##%#!5 !. "28(" Add Folder… !##$'".(

,%-toolbox-/ $".#/ 4)+2 :10.3 !"#

!#$%&"'( +-!+5* #3) *+-* .(#!& *)- toolbox-( !"#$%&"'* 5+#4 !2+'.- toolbox !2#$#
toolbox-( 1#-+ )"3( .mean !#$%&"' !234"- 5( 5#%#!* !+4* :2"$ /#. !#623&605 mean
5#%#! *)* /!". %#!,5* .*+ #3) !"&"+ !"#%#!( 1#.$-&5 1#6%#"2' 2'0-* !"#+"-#+5 !"#$%&"'
.+38-

20
7 !"#$!
:!"#$!! %"&'(
2 ...............................................................................(Debugging) !"#$%& '"($!" !#$)* .11
8 .............................................................................................. Matlab-+ ,$-")$+ ."/$& 12
8 ............................................................................................................... Profiler 12.1
10 .............................................................................................. !"*0(!* !"$/.% !"1"-/ .13
10 ................................................................................................... ,$2.3" 4$)*$5# 13.1
13 ....................................................................... GUI (Graphical User Interface) 13.2
14 .......................................................................................... '"164 10"% !-$+( 13.2.1
15 ............................................................................................... ,$+$7. !/3"4 13.2.2
15 ........................................................................................... ,$2($$+"# ."&$$ 13.2.3
16 ........................................................................... ,$2($$+"# 1& !"5"7! !-$+( 13.2.4
19 .................................................................................................. GUI !"57! 13.2.5
Matlab-1 #"+*

(Debugging) )'"%*& +',%)' )"%#- .11


,!"#$%& 1& ,$%"3 $5& ,$*$$(
.'"7" 452( !"# ,"(*+ 41"0% !"# ,- ,& !+$!7 ,.%"3 !6$7& 1&*1 - syntax !"#$%& .1
'!$5 .4).44 '*8 !# 9"361 $07 457"!4 17 !).4 #11 +$!7 !"#$%& ("0+1 $"). !"+. ,$!$-1
.m-4 :+"( ,&" pcode !+$!7 !"-)*#+ !#8 !"&-1
.,$$("6 #1 ,$7.- ,$1+(!* ,$+"&$6* 4#)"!7 1&*1 .(runtime) 4).4 !"#$%& .2

!"#$%& '4 runtime !"#$%& !#8 !*"-1 .'(!1" #")*1 (!$36$) 1(& !"#$%& '4 syntax !"#$%&
-+ ,$$1("14 ,$5!&*4 17" !(3/"* 4).44 runtime !"#$%& !"1+(!* .&#7 ,;3"5+ ..!"$ !"(*(*6
-4 !$$)/"#+ &*!&41 '!$5 '71 .4$-+4 !+$3 !# 6!51 0#* 4&( 97" ,$(6*5 workspace
.,$$1("14 ,$5!&*1 !"&$%5 $07 9"! 4."& 4."& 0"(4 !).4 !# -)+1 $07 Debugging

(./+ 4%)"4& ,$3/#4 !#$)* !$)(5"/ 1& 4$"%& 43.% %$)5 Debugging-4 9$14! !# &$6*41 $07
.10
,script-4 :+"(

2
Matlab-1 #"+*

,4$)(5"/4

3
Matlab-1 #"+*

,4#+4 4#)"!4 !# 4+$5* script-4 :+"( !).4 9# '$(! 4#.5 0"(4 '$- !$#.*1

.syntax !#$%& !-0"4 41+(!4

!"#$
!".$&$ -$%41 '!$5 .Command Window-+ ,"0# -+)+ 4#$%& !#0"4 4-$/"* ,4#$%& !.)"5 ,#
!-0"4+ 1'"!6! "( ,- '*"3*4 4#$%&4 ,"($* 1- .+7-4 '*3 !*&4 $"- '"754 :+"(+ 4#$%&4 !."&1
..+7-4 1& $1#*&4 ."!/74 1- 4)$61" 4#$%&4

411"6!4 4+& 4."&1 -$%5" .+7-4 1& $1#*&4 ."!/74 1- :615" 4-0"44 ,"($* 1- '*34 !# ,$&5
.find_zero_wrong #1" find_zeros_wrong #$4 4$)(5"/4 ,&& 4!$$4 48 4.(*+ !"-24 .4#$%&4
:615 4).4 !# .")-1 $07 ..$+3 '*8+ ,$$3! #1 !$57"!4" script-4 :+"( !# :$.5 !"-24 '"($! .6#1
.Command Window '"16+ Ctrl+Break "# Ctrl+C 1-
48$#+ "1$/# "# ,4$-+4 $4* !"#.1 0+1+ 0"(+ !"17!34* 0#* 4&( ,,0"(4 syntax-4 %#+ !*"-1
.4$-+4 :+"(
,$&5 .(3/$! 4$)(5"/4 !).4 '4+& (breakpoints) 4.$)- !"0"(5 !-$+( #"4 %"+$0+ '"&#.4 +1&4
!*$$( 45$*$*" ,4."&4 ./3* !# 4%$)* .!"$+ !$1#*&4 .!"0"*- $!& !"*$$( 0"(1 1#*&* $7 +1
,."% 1"54 ,$""(4 1- .+7-4 1& $1#*&4 ."!/74 1- 4)$61 .- ,$""( ,$*$$( 4$!"."&* (16+& 40"*-
!"($. !"."& '4 4.$)- !0"(5 -"+(1 .&/# $# '4+ !"."&4 .24.$)- !0"(5 '$$)*4 ,"0# 1"%$- !-/"41
.!".-4 !"1$7*4 !"."& "#

,5 4."&+ 4.$)- !0"(5 +$)5

,5 4."&+ .)-$! 4).44 !-7" script-4 :+"( !# +"& :$.5

.Debug 1& +)*+ #"4 Matlab $7 '$$)1 $07 K>> '*$34 -$/"* !-7 workspace-4 '"16+

!# -)+1 $07 .'$$0- 4!"# 4-)$+ #1 9# !$67"54 4."&+ !#)*5 4).44 !-7 $7 '$$)* #1*4 (".$4 :64
. Step ."!/7 1- :615 4#+4 4."&1 ."+-1" !5*"3*4 4."&4
!-7 Workspace-+ .6 4."&1 #1*4 (".$4 :64 !# !*0(*" 5 4."& !# !-)+* Step 1- 4)$61
.x_range 45!&*4 -$/"*
+$)5 .4."& 4."& 1"54 !"."&4 17 1- ."+-1 .!"$* '71" ,$5!&* !.0%4 !-)"+* (. 10 4."&1 0-
!0"(51 0- !"."&4 17 !# -)+* 48 ."!/7 . Continue ."!/7 1- :615" 10 4."&+ 4.$)- !0"(5
.4.$)- !0"(5 0"- '$#" 4.(*+ 4).44 17 ,"$3 0- "# 4#+4 4.$)-4
.,$5$(! ,4& #0""1" ".0%"4& ,$5!&*4 17 1& ,7.- !# !"5&1 ;#" '"6+1 '!$5 !-7 $7 +1 ,$&5
.+7-4 !884 $"- "# Array Editor '"16 !"-)*#+ ,Command Window-+ ,$5!&*4 !# '"6+1 '!$5
$+%1 -0$* ,$5$$)*4 data tips ,$#.(54 !"5"16 ")")$ 97" Editor/Debugger '"16+ ,$5!&*4 1-
.45!&*4

.25.25$#+ hyperlink-1 4*"0 "84 ,"&$.4 !.") 1


.4.$)-4 !0"(5 !# 40$."* ,"0#4 1"%$-4 1- 4)$61 2

4
Matlab-1 #"+*

,#*%"01

!$)(5"/ 9"!+ !#)*5 4$-+4& ,$$"7$34 +". '71" ,$$"/) $!1+ ,$.+0 "/)5 #1 4!- 0-
'71" 1"54 4$)(5"/4 '7"! !# '"6+1 "51 .&/#! #1 Step ."!/7 1- 4)$61 .find_zeros_wrong
.#1*4 (".$4 :64 1& 4."&+ !$*$5/ 4$)(5"/ 9"!1 3575 48 ."!/7 . Step in ."!/7+ &*!&5
Step in ."!/7 1- 4)$61 $5/1 .-%.7 !#)*5 4).44 4$)(5"/ 48$# 9"!+ '$$)* Stack '"16 $7 +1 ,$&5
,%$)4 stack '"16

'"16" find_zeros_wrong !$)(5"/ 1& 345"&#.4 4."&1 ."+-5 Step in ."!/7 1- 4)$61 .6#1
,($.1 9/4 :64 find_zeros_script_wrong :+"( 1& '"16+ $7 +1 ,$&5 .!#8 '$$)* stack

.!5*"3*4 4."&+ 41 "#.(& !$*$5/4 4$)(5"/4 9"!+ !#)*5 4).44 !-7& #"4 48 '"*$3 1& !"-*&*4

,$$1("1 ,$5!&* ,4 "1# ,$5!&* .,$5!&* ./3* "/3"5 $7 +1 ,$&5 .40 4."&1 0- !"."& ./3* -)+5
.4$)(5"/1 #.(& script-4 :+"(+ ,$.0%"* ,5$#" find_zeros !$)(5"/ 1&
,$5!&*4 $5& '7#" stack '"16+ &*!&41 '!$5 script-4 :+"(+ ,$.0%"*4 ,$7.-4 !# '"6+1 $07
func, params, ,$5!&*4 !&"1& $7 +1 ,$&5 .script-4 :+"(+ ,$.0%"* #1 dx, tol ,$&064
,$5!&* ,4 1"54 ,$5!&*4 find_zeros_wrong 4$)(5"/+ 9# ,$)+(4 $5&+ ,$.0%"* x_range
"5&$ #1 4$)(5"/4 9"!+ ,$$"5$& '71" script-4 :+"(+ ,$5!&*4 1& ,$(!-4 (. ,4 .*"17 .,$$1("1
.script-4 :+"(+ ,7.- !#
+$)5 '*8+ 9"361 $07 .'$-1 !"#.54 !"$-+ !"1-* '5$# '"&#.4 3/#4 !#$)*1 0- !"0"(/4 1- .+-*
9.- 1- .#&5 x 9.- !-7 $7 +1 ,$&5 .(40 4."&+ 4.$)-4 !0"(5 !# 0$."5) 53 4."&+ 4.$)- !0"(5
45!&*4 !6$!/ $"- 4+ '$6+41 '!$5 .0#* 452( #$4 9# !"*0(!4 !*$$( ,4&-*1 .!"*0(!4 '$# '71" 2
.longG %")$ !.$6+" Array Editor '"16+ x
'2( #"4 !-7 $7 0.01 $."(*4 "7.-1 ".$8641 9$.) dx ,"0$(4 9.- ,)*")& .6#1& #$4 %#+1 4+$34
!# .$0%41 #$4 4$-+4 !# ."!/1 42"&/ 9.0 .4$).2$# 17+ ,0(!* "5$#" 2-*7 x 9.- '71" 10!6 -*
.3/# #)*5 .&#7 dx-+ "+$)41" dx_original-7 dx 1& $."(*4 9.-4
. Exit Debug Mode ."!/7 1- 4)$61 $"- Debug Mode-* !#)1 9$.) ,$$"5$&4 !# -)+1 $07
. Clear all breakpoints ."!/7 1- :615 4.$)-4 !"0"(5 17 !# 0$."41 $07
,,4 ,$$"5$&4 $5&
!

.!".-4 "# !"($. !"."& 11"7 #1 45"&#.4 4."&4 3

5
Matlab-1 #"+*

.&06* script-4 :+"( !# :$.5& $5/1 4$)(5"/+ "5-)$+& ,$$"5$&4 !# ."*&1 "5$1- $7 +1 ,$&5
x !# ,0(* ,!$."%1#4 !-7" 2 #"4 '"&#.4 3/#4& #)*5 4).4 .6#1" 54 4."&+ 4.$)- !0"(5 ;$3"5
Step out ."!/7 1- :615" 4.$)-4 !0"(5 !# 0$."5 4$)(5"/4 !).4 !# ,$$31 $07 .$").4 '/"#+
!).4 !# ,$$31 $07 .Step in ."!/7 9.0 4$1# "53575& !$*$5/4 4$)(5"/4 !).4 ,$$31 #"4 "0$(/!&
.!"*$#!*4 !"#)"!4" 10.2 ;.% 1+(!$ 8#" Continue ."!/7 1- :615 41"7 4$)(5"/4

.$"#.7 1-"/ ,!$."%1#4 $7 #0""1 $07 21( $5!&* 1& ,$5"& ,$7.- ./3* ("0+1 $"). 0$*!
4$)(5"/1 4#$.(4 !-+ ,# 4.($ 4* .457"!+ 06# %#+ 0"- .!"5 '$0- 9# '$(! 4#.5 174
, tol-" dx 1& 106*4 !.$.+ $7.- !# ,% 3$575 script-4 :+"(+ find_zeros_wrong

,4#$%&4 !1+(!*

4.)"5 -"0* .".+ #1 9# script-4 :+"( 1& 10 4."&1 '*34 !# #$+* 4#$%&4 ,"($* 1- 4)$61
#11 4#$%&4 !-)+!* 4+& 4."&1 0- 457"!4 !# :$.41 #$4 !$&"*$& 0#* 4$)/"# '71 .4#$%&4
!11"6!*& $5/1 -%. ,$5!&*4 $7.- 17 !#" 4#$%&4 4-)+!4 4/$# ("$0+ !"#.1 17"5 97 .4-")$+
.4#$%&4
,#+4 '/"#+ Stop if Error !".&/# 1- :615 Breakpoints 2$./!+

,4."&1 "5!"# 41$+"* !-7 457"!4 !).4

4$4$ ,$5!&* 5 1& 21(& "557$!& !".*1 21( !#$%& !-0"41 "5!"# 4#$+4 switch !0"(/ .*"17
, switch-4 !0"(/1 !/3"5 4$)/"# !/3"4 $"- #"4 4$-+4 '".!/ .'$(! 21(

6
Matlab-1 #"+*

.45$(! 4.")+ 0(/!* 0"(4 !-7

7
Matlab-1 #"+*

Matlab-. /%0'#%. $'1%& 12


,Matlab 0"( 1& ,$-")$+4 !# ./&1 ,$7.0 ./3* !"*$$(
4&( 4&-*1 .'!$54 177 while-" for !"#1"1 ,"(*+ !"$)$.2* !"$)(5"/+ &"*$& .1
;$1641 '!$5 ,$+. ,$.(*+ !#8 !*"-1" !"$)$.2* !"1"-/+ while !"#1"1 ;$1641
.!"$)$.2* !"1"-/+ for !"#1"1
"55$# 45!&* .&#7& #$4 971 4+$34 .&#.* -"0$ ,10"%& ,$5!&* 16!#1 ;$0- .2
'".7$84 !#)(4 $"5$&" "10"% $"5$&+ ,$+#&* 8+8+1 9$.) Matlab 8# 16!"#*
!$)(5"/+ ,$#! 9.-* ."+- ,zeros !$)(5"/+ &*!&5 $.*"5 9.-* ."+- ."1$+&+
.struct 4$)(5"/+ structure 9.-* ."+-" cell
!"-)*#+ ,$)"65 #1 ,$5!&* !($6*" ,$1"0% ,$$5*8 ,$5!&* !.$)$* !"-5*$4 .3
.clear !$)(5"/

,4.-4
,448 #"4 ,$#+4 ,$.(*4 $5& +"&$61 ,$&".04 ,$+#&*4" '".7$84 !"*7
.1

.2

Profiler 12.1

!#$)* #$4 Profiler-4 !.2* .45*8 !# 41+* 457"!4 '7$4 !0$0*1 42$& #$4 Profiling-4 !2$&
!"18"% !"$)(5"/ 48$# 4#.* Profiler-4 ..!"$ ,$.$4* ,$-")$+ .&/#1 $07 !"57"! 1& ("+(+4 $.#"")
."1# !"$)(5"/ ;"(-1 ,$7.0 #")*1 !"351 1"7$ &*!&*4" 4).44 '*8 !#
!"#."( '4" '7!$ 41$-$ 4.")+ !"+"!7 '4$5/ 1- '4& !"57"! '71" !"+7& ./3* &$ 7"0+ 0"(1
.,$+#&* !"8+8+*& 47"*5 .!"$ 4+7&+ !"$)(5"/1
!).4 1& !"#)"!4 ;.% '141 ,4&6*4 '-*1 .doc profile "+!7 484 $&"*$&4 $174 1- .!"$ 0"*11 $07
.,$3/# !#$)*1 457"!4
,0"(4

.12.1 ;.%+ '"164 !# +$5*

8
Matlab-1 #"+*

profiler $%&' :12.1 !"#

9
Matlab-1 #"+*

)'-2,)- )'%1$* )'3'01 .13


!"#$%& '"()"*+ 13.1

97 ,&1 .,$7$14! 1& 4($*5$0 &$6*41 ,$6$1)* ,5$# ,$$0**-!1!" ,$$0**-"0 ,$/.% ,$+. ,$.(*+
.4(16 4.")+ $5&4 .6# 06# ,$/.%4 !).4 $"- ,$/.%* ,$2.3 .)$$1 '!$5
,Matlab-+ ,$2.3 !.$)$1 ,$7.0 $!& !"*$$(
.&#7 41$-$ "8 42$& .on-line ,$(6*5" ,$22."&* ;.%+ ,$2($$+"# :Erase Mode !$$)/"# .1
.'2( #"4 ,$*$$./4 '$+ ,$$"5$&4
,$2.3 !.)"$ "8 42$& .off-line 2.37 06$ ,!.$*&" ,$/.% ./3* !.$)$ :movie !$)(5"/ .2
.4+ 0(*!5 '71" ,$(16 .!"$

9"!1 getframe !$)(5"/ !"-)*#+ "!"# ".*$& ,;.% ".)$ :.!"$+ 2"&/ movie !$)(5"/ 1& '"$-.4
.movie !$)(5"/ !"-)*#+ 2.34 !# "%$)4" ,structure %"3* 2.3 45!&*
2.3 !"5+1 $07 (,"!7 -+)+ !"5*"3* !"/3"!4) ,$3/# !#$)* ,!$."%1#1 !"/3"! ./3* -)+5 #*%"01
,,!$."%1#4 !"*0(!4 !# &$6**4
,script-4 :+"(

,4$)(5"/4

10
Matlab-1 #"+*

11
Matlab-1 #"+*

while !#1"1 1& 4$).2$# 17 914*+ .2.34 1& ,$*$$./4 17 !# 1$7*4 structure 9.-* #"4 M
.6#1 .getframe !$)(5"/ !"-)*#+ M-+ +)"* ;.%4 ,4#1"14 1& 4$).2$# 17 ,"$3+ .;.% 22."&*
Matlab-4 2.3 !# .$** script-4 :+"( .script-4 :+"(1 M !# 4.$86* #$4 ,4$)(5"/4 !).4 ,"$3
,0"(4 ;3"5+ .Matlab-1 :"6* 4$/)1 '!$54 avi 2*."/1 M

.4!6# ,-/ M 2.34 !# :$.*

0.8

0.6

0.4

0.2

0
y
-0.2

-0.4

-0.6

-0.8

-1
0 2 4 6 8 10 12 14 16 18 20
x

(")* &+ $%"',* -.."/* :13.1 !"#

.;).+ %)"$ 2.34& ,$*-/4 ./3* !# -+"( $5&4 25*"%.#4 4

12
Matlab-1 #"+*

GUI (Graphical User Interface) 13.2

buildgui.pdf !# "#.( 4($*-* .!"$ !".7$41 .$/.% (&** !.$)$ !"$.&/# !# 4.)(+ (. .("3 (./
.48 (./ +!75 "$/ 1-&

!"-)*#+ -)"+* $/.% (&** '"57! .,$-toolbox-4 !$+.*+ ,$$/.% ,$(&** Matlab-+ ,$*$$(
.13.2 ;.%+ .#"!*4 5$/.%4 (&** '"57! '"16 !# !6!"/4 guide !$)(5"/

(Layout Editor) ./"# 0+11 $%234 $%&' :13.2 !"#

,,$+1& 4&"1&1 (16!* GUI !.$)$ 9$14!


.4#.$ $/.%4 (&**4 0)$7 &#.* '57!1 :1*"* -GUI-4 '"57! .1
.Layout Editor-4 !"-)*#+ $/.%4 (&**4 1& $5")$64 42-*4 !.$)$ -GUI ;.% !.$)$ .2
1& 0"(/!4 !# '57!1 '!$5 "7.0& .m :+"( .)"$* GUI-4 ;.% !.$*& !-+ -GUI-4 '"57! .3
.(,$/.% ,,$2$./! ,,$."!/7) 2($$+"# 17

,#*%"0 !"-)*#+ GUI !.$)$ 1& ,$$3$3+4 !"5".(-4 !# &$6*5 48 (./+


,$/.% !.$)$1 !"$)(5"/4 !"-)*#+ peaks, membrane, sinc !"$0**-!1!4 !"$)(5"/4 2"2.&
.$").4 4($/.%4 (&** 1& $!*73 ,$&.! #"4 13.3 ;.% .surf, mesh, contour

.Show names in component palette !".&/#+ Preferences-+ ."6+1 File 2$./!+ 9$.) 48 '"16 1+(1 $075

13
Matlab-1 #"+*

.%5"* 0+11* &+ .413) (%("+ :13.3 !"#

'!$5 Component Palette-4* .13.2 ;.%+ '"164 !# !6!"/ Command Window-+ guide !).4
.$/.%4 (&**4 '"16 !# 4""4$ #"4& "Layout Area"-7 13.2 ;.%+ '*"3*4 62&+ ,$2($$+"# +$)41

+'34! 32'* )0%., 13.2.1

,'"164 10"% !-$+(1 !"$.&/# $!& !"*$$(


.Layout Area-4 1& 45"!6! !$5*$4 45$/+ ."6&4 -"+$.4 1- 4.$.%" 4)$61 -!$50$ .1
.13.4 ;.%+ '"164 !# !6!"/ Property Inspector ."!/7 1- 4)$61 -Property Inspector .2

Property Inspector $%&' :13.4 !"#

14
Matlab-1 #"+*

'"164 +6". !# !"5&1 '!$5 .2($$+"# 17 1& !"5"7!4 !# !"5&1 '!$5 Property Inspector '"16+
!"$").4 !"0$6$4 !# -"+(1 $07 .position !5"7!+ height-" width !"5"7! !! $"5$& $"- "*)-
.Units !5"7!+ ,$.6"+

1& !"5"7! -"+(1 '!$5 (Handle Graphics 1& ,$2($$+"# 7.2 (./+ .78"4& $/7) $7 +1 ,$&5
$/.% (&** #"4 Property Inspector '"16 ,4&-*1 .get-" set !"$)(5"/ !"-)*#+ 4($/.% 2($$+"#
!"-)*#+ "1# !"5"7! 45&*" get !$)(5"/ !"-)*#+ 4($/.% 2($$+"# 1& !"5"7!4 17 !# %$)*4
.set !$)(5"/

/%.%5$ )16'! 13.2.2

Layout -+ $").4 ,"($*1 "!.$.%" Component Palette -4* +$7. !.$6+ $"- !-)"+* ,$+$7. !/3"4
,$2#23 23(2 ,(push buttons) 4)$61 $."!/7 4&"1& Layout Area -+ +$)5 "51& #*%"0+ .Area
.!6# ,$.$) !7.-*" ,(popup menu) 6!/5 2$./!
.13.5 ;.%+ !%)"* 4#)"!4

.'3%2 GUI :13.5 !"#

/%7,%%.'" $'&%% 13.2.3

'"16 6!"/4 Align Objects ."!/7 1- 4)$61" ,$2($$+"#4 !)"+( !.$6+ $"- -)"+* ,$2($$+"# ."&$$
6"".*4 !# -"+(1 '!$5 .$(/"#" $75# ."&$$+ 2"1&1 '!$5 48 '"16+ .13.6 ;.%+ "*7 Align Objects
.(."&$$ #11 "# 87.* ,1#*& ,'$*$1) ."&$$4 %"3 !# ,%" 2($$+"# 17 '$+ $75#4" $(/"#4
.1#*&1 4$4$ $+6".4 ."&$$4" ,$13($/ 20-* +7."* $75#4 6"".*4 $7 "5-+( 13.6 ;.%+ ,#*%"01

15
Matlab-1 #"+*

$75# ."&$$ %"3

./3* $75# 6""$. %"3


,$13($/
$75# 6"".*+

$(/"# ."&$$ %"3


$(/"# 6""$. %"3
./3*
,$13($/
$(/"# 6"".*+

Align Objects $%&' :13.6 !"#

/%7,%%.'" 3& )'('5) )0%., 13.2.4

$07 ,1&*1 .Property Inspector-+ ,$.6"+" .+7-4 1& $5*$4 ."!/74 1- ,$)6"1 ,2($$+"#+ ,$.6"+
Property '"16 6!/$" 1"54 !"1"-/4 !# -)+5 ,.!"$+ '"$1-4 Push Button-4 1& !"5"7!4 !# !"5&1
.13.7 ;.%+ %)"*& $/7 Inspector

$%.&6* Push Button-* &+ Property Inspector $%&' :13.7 !"#

16
Matlab-1 #"+*

-1 ,&4 !# 45&5 String 45"7!+ .Surf-1 ."!/74 ,& !# !"5&1 ,$)". "5# 13.3 ;.%+ 4*734 $/1
23(24 !#" ,$.!"54 Push Button $."!/7 $5& !# 45&5 6'/"# "!"#+ .Push Button ,"(*+ Surf
.13.3 ;.%+ 4*731 ,#!4+ $2234
;.%+ :6+ '*"3*) String !5"7! 0$1 ')614 1- :615 (popup menu) 6!/5 2$./!4 !# -"+(1 $07
.!0./5 4."&+ '$")$ 6!/54 2$./!+ 25*1# 17 .-$/"$ String '"16" (13.8

$%.&6* Push Button-* &+ Property Inspector $%&' :13.8 !"#

0"(/!4 !# '57!5 "+& ,#+4 +1&4 !#.(1 ."!"# !5$$)*4 (Tag) !$""! !*$$( GUI-+ 2($$+"# 171
.$"4$81 !"1( !$""! -"+(1 $"). ,2($$+"# 17 1&
-4" pushbutton1-7 $2*"2"# '/"#+ !-+(5 Surf 4)$614 ."!/7 2($$+"# 1& !$""!4 ,1&*1
GUI '"16+ ,$2($$+"#1 #."(& .m :+"( .)"$* ,;.%4 !.$*& .6#1 .<automatic> #"4 Callback
.Tag !5"7!+ !8".6*4 !"-)*#+ -+(5 4*&& Callback 4$)(5"/ !! !"-)*#+
.13.1 41+2 $/1 !"$""!4 !# 45&5 #*%"0+

4&06 !$""! 106* !.$.+ !$""! 2($$+"#


surf_ pushbutton pushbutton1 Surf (Push Button)
mesh_ pushbutton pushbutton2 Mesh (Push Button)
contour_ pushbutton pushbutton3 Contour (Push Button)
text_select_data text1 Select Data (Static Text)
data_popup popupmenu1 Popup menu

13.1 *&7(

-1 name !5"7! !# 45&5" ;.%4 17 !# '*35 .GUI-4 1& ,&4 #$4 !"5&1 $").& !/3"5 45"7!
.simple_gui

:+"( ,% .)"5 45"&#.4 4.$*&+ .Activate Figure ."!/7 1- :615 41"-/+ GUI-4 !# !"#.1 $07
.2($$+"# 17 1& 0"(/!4 !# -+"(4 .m

.4!5"&& 45".6#4 45"7!4* '"*$34 !# 0$."41 &$ .*&$ $"5$&4& $07 $7 +1 ,$&56

17
Matlab-1 #"+*

1"6!#
1"6!#
!6$!/"
GUI-4

Callback-4 !"$)(5"/-!! !"*&


! !"$""!4 9*3 1- "-+(5

!!
!"$)(5"/
!"-+"(4
0"(/! !#
,$2($$+"#4
GUI-4 1&

18
Matlab-1 #"+*

GUI )'(5) 13.2.5

;.%4 !.$*& .6#1 .&*!&*4 $"- 1-/"* ,$#!*4 2($$+"#4 .&#7 !"-)"+* Callback !"$)(5"/ !!
.'!"# !57!1 &$" !"($. "1# !"$)(5"/ !!
,!".2* $!& 48 9.-*1 .handles ,&+ structure 9.-* #"4 GUI-+ +"&6 25*1#
2($$+"# 1& handle-4 1&*1 .GUI-+ ,$2($$+"#4 17 1& ,$-handle-4 17 !.$*& .1
.handles.data_popup #"4 data_popup
!! -")$+ !-+ '$*8 4$4$ 48 -0$*& 97 4$)(5"/ !!+ -0$* ."*&1 '!$5 .$1+"1% -0$* !.$*& .2
,0"(4 1&*1 .!".6# !"$)(5"/

43.%4 !# !.*"& 4&-*1 4$5&4 0"(4 !."&" X 9.- 1-+ handles 9.-*+ &06 40& .)"$
,2"&/ ,"&.5 .6# Callback-+ X-1 !&%1 $07 .handles 1& 4&064

,popup-4 '"16 !# !-7 '57!5

.)$$5 48 9.-1 ,#!4+ ,'71 .popup-4 '"164 4+ .6+ &*!&*4& !".&/#4 !# 1$7* 'Value' 9.-4
.handles-+ ."*&5" +$)5 ,,$#!* data

,,$."!/74 $5)61 !# !-7 '57!5


,$-Callback-4 '71 .popup '"16 $"- -+(54 -0$*4* 45"& %"3* ;.% .)$$1 ."*# Push button 17
.,$#!*4 ;.%4 !# ,$22.&*" handles-* -0$*4 !# ,$6("1 ,41&

,0"(4

19
Matlab-1 #"+*

.Command Line-4* "!"# ")$.4 .41"-/1 '7"* $/.%4 (&**4 !-7

.;.%4 &#.+ ,$2$./!+ 2"1&1 ,% '!$5


.13.9 ;.%+ -$/"*& $/7 New Menu-+ ".6+" Tools 2$./!+ Menu Editor 1- ")61

Menu Editor $%&' :13.9 !"#

'"$) $"- .)""$!& Callback-4 !$)(5"/1 ,& "5! .File-7 Label '"$) $"- 2$./!4 ,& !# ".6+ !-7
.Tag-4

20
Matlab-1 #"+*

File (."/4 4".5. -Menu Editor $%&' :13.10 !"#

"-+$( '/"# "!"#+ .New Menu Item 1- ")61" File-+ ".6+ File 2$./! 1& 2$./! !! .")$1 $07
.close 2$./! !! 1& Tag-4" Label-4 !#

!$2*"2"# 4&-*1 .Close-" File ,$2$./!4 $5& 1& ,$-Callback-4 !# '57!1 (. .!"5 4.$*& .6#1
close 2$./! !! 1& Callback-4 ."!"# '57!1 9.") 48 ,"&$$+ '$#" 4)$61 !-+ 6!/$ File 2$./!
figure1 #"4 4($/.%4 '"16 1& Tag-4" .6#* .GUI-4 '"16 !# ."%31 $07 delete !$)(5"/+ &*!&*
,#"4 0"(4 8#

21
Matlab-* 12-'! .03!
!""2. *%."

FACULTY OF ENGINEERING SCIENCES !"#$!! %&#'( !)(*+,!


DEP. OF BIOMEDICAL ENGINEERING .%/*,0*%1 !"#$!( !+(-'!

Matlab !"#$
*'+% – 1 '$% &'()!

:0*&%2! %/2*$
/*1' ./
.*%)'.%0/ .*(*&, .1
5%$.2' .4
syntax-* !1%.3 6*$4" .#
build-in .*%7+$*, .!
!#*1&! .1%1"* !$3*.( !"%$3 .*

:,$'" )-. )%'/


:)$0)$%/1 MATLAB (2 .%1! 0./1 (1+( 6.%$ .$3#*&'* .)0*,' 5%0," .'%20

www.mathworks.com
(!!! *7'/( 8('*') Matlab-1 !1%.3 6*$4"
http://www.mathworks.com/matlabcentral/fileexchange/download.do?obje
ctId=2529&fn=MatlabStyle1p5&fe=.pdf&cid=563176

Citrix .02 90# &7*1' – !$3*.! .$+.! /(( /+-*0' 12-'' Matlab -( !2%4
:90# !"%$3 ,!)%"01%$*/1
http://citrix.bgu.ac.il/login.asp

-1 -
D:\Tal\Matlab Intro course\Matlab - New\Class_1.DOC
*'+%

?MATLAB 01%
.5%%"#$!* 5%%$3) 5%1*2%- &*7%1( .#&*%'! .%)'.' !$3*. !$%! MATLAB
54 6/3'* ,.%0/%$%( !014(/1 .*%&1 0*.,( %#3 ,5%&12! .*$2 :*"1 ,!$3*.! !1.3$ 0*+'1
5%(3 :6*43 .*,"*$ .*10 .*%7,*/ !$3*.! .((*3 5*%3 .MATrix-LABoratory - !'2 .*&'2'
.02,/' !$3*.! .#*&* ,.*;*0-' ,5%'*$%(*, ,5%$*.$ #*1%& ,.*%(/%7$0,%# .*/**2'1 (*,%)(
5%#'' .(. ,5%#''-*# 5%,04 .0%7% ;!!*14 .%(/*;%** !'01 .*/7*. .47!* 5%$*.$ #*1%&
..*%7'%$/*
!$3*.( :%"*!( 6.%$ .#*/' .(14*' 5%%(*1'%" 5%1*2%- &*7%1( .*02,/!* ,%0'*$ !$3*.! %,*/
,.*.*/ #*1%& ,!0+1 6*43 5%$*2 5%/2*$1 .*#*+,! 07*/ (2 !1-0! 5%**!'! TOOL-BOXES
.#*&* .*%+(- .*%(/%7$0,%# .*/**2'
)'0*, ,2'.2'! 0*1& .Macintosh-(* PC -( ,Unix-( :!$3*.! (2 .*/"04 2*(2 .*'%%+
.!!; - .*/"04! 2*(21 .*#*+,! .1%.3
(%-.. !; 6*(-1 1*.3$2 !0*2 (3 .MATLAB -! (2 .*#*+,! 6*(- -.,$ !$3*.( !"%$3! 5&
*0'2% !; 9"'1 0%#4$2 5%$.2'!* .*(*&,! (3 .(.%)'*)*/ 6'"' MATLAB -!) >> 6'%"1
(2 !)%21 #1*& MATLAB -! .(WORKSPACE -!) MATLAB-! (2 !#*1&! 1-0'1
./ &71( MATLAB -( 5*04. RETURN 2+' (& !7%-( 63( ,(INTERPRETER) "20,'"
(2 !3*0/ !0#%" &71( 5%$%%$*&' 02/3 .!$*0-/! !0*21 *1.3$2 .*#*+,! :70 */ !#*+,!
0'*2! 90*& (31 2'.2!( 6.%$ .%$*7%- (editor) 90*&1 !.*/ 1*.3( 8('*' (.%$3*.) .*#*+,
.%'%$, 90*& .((*3! ,PC ."04 !$%! 5%#1*& *$/ !1 MATLAB ."04 .ASCII )'0*,1
0'2%!( .*3%07 .*%$3*.! PC 12-'1 5%#1*& 02/3 .*.m .'*%" 5& 0*'2( 2% .*%$3*.! ./
1 54 .0-/ 2*,%- (*("' MATLAB -( 0%#4!( */ MATLAB !$3*.! ./7'$ !1 !%0,"1
./ MATLAB ( 0%#4!( 2% ,6*12-! (2 .%2/0! !%0,"1 ./7'$ /( .%$3*.! 5/ ,UNIX
.(DOS !(&,!! .30&'1 *'3 dir-* cd .*#*+,1 2'.2!( 6.%$) 2*,%-! 1%.$
.(m .'*%"! /(() .*#*+,! 6*(-1 !'2 .1%.3 %"& .&7*1' MATLAB-1 .%$3*. .70!
.demo .%$3*.! .70! %#%-(& .*/0( 6.%$ MATLAB-! .*(*3%( .*/'4*#

362-1-2023 Matlab !"#$


-2-
!'0$0$+ !'02%!% !'&'.3
!'0$0$+ !'02%!% !'&'.3 :1 1&+2

Operation Symbol Example


addition, a + b + 5+3
subtraction, a - b - 23 - 12
multiplication, a ! b * 3.14 * 0.85
division, a " b / or \ 56 / 8 = 8 \ 56
power, a b ^ 5^2

.\ %(/'2 +*(%-* / %$'% +*(%- :5%(%&, +*(%- %$'%" %$2


./ .*$2( 6.%$ .0*"%- */ 0*1%- ,+*(%- */ (,3 ,.*+;- :/1! 0#"1 6%'%( (/'2' &7*1% 1*2%-!
.5%$*2/0 *12*-% 0.*%1 5%%'%$,! 5%%04*"! .5%%04*"1 2*'%2 %"& 0#"!
:/'4*#( 5%-**01 12-.' *$%/ MATLAB -!

» 2*4+2 * 5

ans =

18

.#(+! .ans (#-'! .0%01 !$.2'1 2'.2% MATLAB -! !/7*.! 0*1& !$.2' 0#4*! /( 5/
.*; !#1*& 02/. !#*1&! 1-0'1 ans

40"!5% 4. 1#'+.
:5%$.2' .0#4! %#%-(& !'*# 1*2%- &71( 6.%$
» a=4,b=5;

a =

» c=2*a+2*b

c =

18

2*'%2 .1.3$ /( b (2 *30& ./; .'*&( ,9"'! (& a (2 *30& 1.3$ !$*2/0! !0*2! &*7%1 %0-/
.0'2$ !/7*.! !",#! /(( 54 ,!0+' (31) 9"'! (& !/7*.! (2 !",#! &$*' ; .'*%"1
.(6*03%;1

362-1-2023 Matlab !"#$


-3-
.5/.!1 !$.2% c 5/! +*#1$* a (2 *30& ./ !$2$

» a=10;

» c

c =

18

.1*2%-! .(*&, ./ 2#-' &71( 9%07 c (2 *30& ./ 63#&( %#3

» c=2*a+2*b

c =

30

0,"' 2%+$ 5/ . # $ 5%7-1 2*'%2 %"& .*#*+,! 6*(-1 !1.3$ 0132 !0*2 0;-2( 6.%$
02/ ,!$*0-/! !#*+,! ./ .%#%%' (1+$ $ -* !1.3$ 0132 !#*+, (2 .*$*2/0 .*%.*/
.#(+*!2 :*0%71 !(%-.'

40)3$% &5 1(61 2%)'3


.*0," 4 /%! 5%2'' 5%0,"' 0*1& (#-'! .0%01 ..%$*02& !#*+$ /(( *1.3% 5%'(2 5%0,"'
..%$*02&! !#*+$! %0-/
» a=215/6

.!47!! .0*7 .&%1+( .*2'2' .*/1! .*#*+,!

1(611 !)'6! !.0+7 :2 1&+2

MATLAB Command Comment Example: 215 / 6


format short default display 35.8333
format long 16 digits 35.83333333333334
format short e 5 digits + exponent 3.5833e+001
format long e 16 digits + exponent 3.583333333333334e+001
format hex hexadecimal 4041eaaaaaaaaaab
format bank 2 decimal digits 35.83
format + positive negative or zero +
format rat rational approximation 215 / 6
.5.47! 6,*/ ./ +0 /(/ 5%$.2'! 90& ./ !$2' *$%/ 5%*"' )'0*,1 2*'%2 *

362-1-2023 Matlab !"#$


-4-
40"!5%
-! ..*/ .*%!( 1%%- 6*2/0! *.! 02/3 ,5%**. 19 #& .-/ !('' 130*% !$.2' (2 52
..*$)+ .*%.*/1 1.3%!( .*1%%- .*#*+, ..*$)+* .*(*#4 .*%.*/ 6%1 6%-1' MATLAB
:Matlab .,21 ".*0*'2 5%(%'" 5%**!'! ,5%/1! 5%$.2'( .$.%$ .#-*%' .*&'2'

!8).% 0"!5% :3 1&+2


Special variables Value
ans The default variable name used for results
pi The ratio of circumference of a circle to its diameter
eps The smallest number such that when added to one
creates a number grater than one on the computer
flops Count of floating point operations
inf Which stands for infinity, e.g., 1/0
NaN Which stands for Not-a-Number, e.g., 0/0
i or j i =j= %1

nargin Number of function input arguments used


nargout Number of function output arguments used

.12-'! .0%7&( /(* inf !/7*.( 5*04. 0-1 !+*(- , :*"$%/ = inf
.12-'( 03*'! 0.*%1 6)+! 0,"'! = eps
."+%0" ,0,"' 5%%+ /( = NaN

:.*#*+,1 2*'%2 %"& (1+$ WORKSPACE -1 5%0#4*'! 5%$.2'! (& &#%'

» who

Your variables are:

a b c

:.)0*,' 0.*% !0*71 */


» whos

Name Size Bytes Class

a 1 by 1 8 double array

b 1 by 1 8 double array

c 1 by 1 8 double array

Grand total is 3 elements using 24 bytes

362-1-2023 Matlab !"#$


-5-
:WORKSPACE -! ' 5%$.2' .+%-'( .*2'2' .*/1! .*#*+,!

» clear all % …or: clear, or…

» clear a b % clear only defined variables

!').1' 7'$03 #'70"


.!/7*.! .47! ./ &$*' ; -1 2*'%2 ..*#*+, 6%1 !#0,!( 5%2'2' "," -* ";" 5%$'%"!
.% -( 6%'%' 1.3%%2 !' (3' 5(&.% MATLAB -! ,.*0&! .1%.3( 2'2' "%" 6'%"!
:!/1! !0*21 !3%2'' !#*+,!2 MATLAB -( 0'*/ ". . ." 6'%"!

» 4*...

ans =

20

.!$.2' (2 52 &*)+( 6.%$ /(

40+8')% 40)3$%
5! MATLAB -1 5%$.2' %((3 6,*/1 ,!$.2'! 4*" ./ &*1+( 90*7 6%/ MATLAB -1
.%2'' 0(+" .*%!( (*3% %)0, !0+' .5%13*0' 5%01%/ .*(%3'! .*7%0)'
. j */ i .*%.*/1 5%%."% 13*0' 0,"' (2 !'*#'! 90&!

» c1=1+2i

c1 =

1.0000 + 2.0000i

» c2=1+sin(pi/2)*i

c2 =

1.0000 + 1.0000i

:.%;)0+ */ .%0/(*, !0*71 .%02,/ 5%13*0' 5%0,"' .47!


M&) = M ! e j ) ' a ( bi

M = a 2 ( b2 ) ' tan %1 (b / a )
a ' Mcos) b ' Msin)
362-1-2023 Matlab !"#$
-6-
:.*/1! .*#*+,1 2*'%2 %"& 01&'! &7*1% MATLAB -1

» M=abs(c1)

M =

2.2361

» teta=angle(c1)

teta =

1.1071

» a=real(c1)

a =

» b=imag(c1)

b =

.5%$/%#01 .*47*%' MATLAB -1 .*%**; *


.)(-*' 90& 1*2%-( 54 .2'2' abs !#*+,! **

362-1-2023 Matlab !"#$


-7-
!'0)2%'"'(0)2' !'02%!% !'067"'3
!'0)2%'"'(0)2' !'02%!% !'067"'3 :4 1&+2

Mathematical Functions
abs(x) Absolute value or magnitude of complex number
acos(x) Inverse cosine
acosh(x) Inverse hyperbolic cosine
angle(x) Four quadrant angle of complex
asin(x) Inverse sine
asinh(x) Inverse hyperbolic sine
atan(x) Inverse tangent
atan2(x,y) Four quadrant inverse tangent
atanh(x) Inverse hyperbolic tangent
ceil(x) Round toward plus infinity
conj(x) Complex conjugate
cos(x) Cosine
cosh(x) Hyperbolic cosine
exp(x) Exponential: e
x

fix(x) round toward zero


floor(x) round toward minus infinity
gcd(x,y) Greatest common divisor of integers x and y
imag(x) Complex imaginary part
lcm(x,y) Least common multiple of integers x and y
log(x) Natural logarithm
log10(x) Common logarithm
real(x) Complex real part
rem(x,y) Remainder after division. rem(x,y) gives the remainder of x / y
round(x) Round toward nearest integer
sign(x) Return sign of argument e.g., sign(1.2)=1, sign(-23.4)=-1, sign(0)=0
sin(x) Sine
sinh(x) Hyperbolic sine
sqrt(x) Square root
tan(x) Tangent
tanh(x) Hyperbolic tangent

362-1-2023 Matlab !"#$


-8-
:.*/'4*#
» y=sqrt(3^2+4^2)

y =

» y=rem(23,4) 23/4=5 (3)

y =

» x=-2.6, y1=round(x), y2=fix(x), y3=floor(x), y4=ceil(x)

x =

-2.6000

y1 =

-3

y2 =

-2

y3 =

-3

y4 =

-2

. x / y +*(%- %0-/ .%0/2 - rem(x,y)


.0.*%1 1*0+! 5(2! 0,"'! %,(3 (4&' - round(x)
.",/ %,(3 (4&' - fix(x)
.:*"$%/ "*$%' %,(3 (4&' - floor(x)
.:*"$%/ "*(, %,(3 (4&' - ceil(x)

406+7 4. 1#'+.
-! (2 .*#*+,! 6*(-1 #*1&( "-*$" /( !3*0/ .*(*&, .0#" &71( 5%$%%$*&' 02/3 :m-file
.MATLAB
.*.m .'*%" 5& 81*+3 !.*/ 0*'2(* .%$3*. ./ 1*.3( 8('*' !; !0+'1

362-1-2023 Matlab !"#$


-9-
406+7 &'10" !'#'73 :5 1&+2

File Management Functions

cd Show present working directory or folder


cd path !#*1& .%%0," %*$%2
chdir Same as cd
chdir path Same as cd path
delete test delete the M-file test.m
dir .%-3*$ !%0,"1 5%71+ .'%20
ls Same as dir
matlabroot Return directory path to MATLAB executable program
path 5%71+! 2*,%- 1%.$ (2 %*$%2 */ !47!
pwd same as cd
type test Display the M-file test.m in the Command window
what Return a listing of all M-file and MAT-files in the current directory or folder
which test Display the directory path to test.m

!47! ,9"'! (& .*#*+,! ./ 4%7' *$%/ /*! m-file 9*.' .*#*+, &71' MATLAB -! 02/3
:.*#*+,1 5%2'.2' 5/ .%02,/ ./;3
» echo on % activate command echoing

:(*)%1(
» echo off % cancel command echoing

5%!5% 75%%
5%!5% 75%% !'#'73 :6 1&+2

M-File Functions
disp(ans) Display result without identifying variable name
echo Control the Command window echoing of scrip file
commands
input Prompt user for input
keyboard Give control to keyboard temporarily (Type return to quit)
pause pause until user presses any keyboard key
pause (n) pause for n seconds
waitforbuttonpress Wait for key/mouse button-press over figure

362-1-2023 Matlab !"#$


- 10 -
.*'2 /(( !$.2' (2 90& */ .;*0-' .",#! - disp
..#(+'!' )(+( !$.'!* .;*0-' .",#! - input

» x=input('Please input x : ');

Please input x : 3

» disp(x)

!0"8'! !)7+
.5%0)'0, %*$%2 */ debugging 90*7( .%$3*.! (2 .%$'; !0%7& .*02,/' .*/1! .*#*+,!

(2 .%$'; !+",! 02,/' keyboard 17' .5%2+'! -*(( .%$'; !0+1 - keyboard
.(*)%1( .2'2' return !#*+, . WORKSPACE -! 90# .*#*+, .(&,!* .%$3*.

.(*)%1( 5*04% 2+' (3 ,.%$3*.1 .%$'; !+",! - pause


..*%$2 n ,.%$'; !+",! - pause(n)

.013&! (& !7%-( %"& (*)%1 ,.%$'; !+",! - waitforbuttonpress

.!(*&, &*7%1 &7'/1 MATLAB -! ./ !+%",' ctrl-c (& !7%-( !

362-1-2023 Matlab !"#$


- 11 -
.k_board /0+$! m-file 1*.3$ .keyboard :.*#*+,1 2*'%2 5%4#$

A=5; !%"&'!
B=5;

keyboard

C=A+B

» k_board .k_board !%"&'!( !)*+( ,)+


K» B=10;

K» return

C =

15

6'%"! .10 -( 5 -' B (2 *30& ./ .*$2( !02,/* .%$3*.! &*7%1 ./ !07& keyboard !#*+,!
.keyboard 17'1 5%/7'$ 02/3 &%,*' K»

362-1-2023 Matlab !"#$


- 12 -
!'#'73 9'&/ &'10" !'#'73
Command Window Control Commands
clc Clear the Command window
diary Save Command window text to file
home Move cursor to upper left corner
more Control the paged output in the Command Window

help - 1)-.
.!#*+,! 52* help .2+! %"& !#*+, (& 01"! (1+( 6.%$

» help sqrt

SQRT Square root.

SQRT(X) is the square root of the elements of X. Complex

results are produced if X is not positive.

See also SQRTM.

.help topics - 5%/2*$ .'%20 .47!( 5*04. !'7&(23 help !#*+,!

./2*$ *.*/1 .*0*2+! .*#*+,! .47! - help topic

.02+! %,( .*#*+, .'%20 - lookfor keyword

» lookfor complex

CONJComplex conjugate.

IMAGComplex imaginary part.

REALComplex real part.

. . . . .

. . . . .

.'*3* 013& ,5%)%0,. – %.*#%#% 2'.2' +2''1 ,.$**+' !0;& 6*(- .-%., – helpwin

.MATLAB .*(*3% (2 !'4#! .%$3*. .70! – demo

362-1-2023 Matlab !"#$


- 13 -
Matlab-+ 12-(" .03"
""#2. +&.#

FACULTY OF ENGINEERING SCIENCES "#$%"" &'$() "*)+,!"


DEP. OF BIOMEDICAL ENGINEERING .&/+!0+&1 "#$%") ",)-("

Matlab !"#$
!'*+*),% !'*)',-' !'&'./ – 2 '$% &'()!

!'+*),%

,130+( +/ &2(( – &0!#( 5+.% )3 .MATLAB-1 .&#&#1" 4&%+.%" 5+60/ .$&-& "%&" "7&0*(
&*0! "0,( +%&" 0+*,+ .&$((-+$ 80'( &$&-)' .67+&( "7&0*( ."7&0*( &$&-)' "2'() 67+&(
..&$(&(-$- "7&0*( )2
)' 0+9-) &)1( ,4&01&/ )2 ")+$6 "7+1, )' .+&*(.( .+)+'! '+7&1 02!/( .+7&0*(1 2+(&2
.01&/ )3 0+1' ")+'!"
.+0+2 5&1 01'( .[ . . . ] 4&'1+0( 4&&06+#1 2+(&2 &"' "2'. MATLAB-1 "7&0*( .0$6"
.","-1 +/ -++01 2(.2") 5.&% ,"0+21 4&01&/ 5&1 "$0!" 80+7) .";"-1 5(+#&
5.&% /)2 5++&3( . 0 " X " ! :4&30'" 0+1' y = sin(x) &+*&1" ./ 12-) 4&7+0 0%('#& !

: 0.1! )2 4&'+1, 4&-++0(1 "&7,%+!" ./ 4&(6+$ ,4+-.1 "$+,% )3 0+1' 12-)

» x=[0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1]*pi

x =

Columns 1 through 7

0 0.3142 0.6283 0.9425 1.2566 1.5708 1.8850

Columns 8 through 11

2.1991 2.5133 2.8274 3.1416

» y=sin(x)

y =

Columns 1 through 7

0 0.3090 0.5878 0.8090 0.9511 1.0000 0.9511

Columns 8 through 11

0.8090 0.5878 0.3090 0.0000

-1 -
D:\Tal\Matlab Intro course\Matlab - New\Class_2.DOC
:4&&(.&06+) +/ 4&0/&%&) 4&&$((-$- 4&30'( .0&7&) .+2(2( .+/1" .+$+,!"
x = [2 2 # pi sqrt(2) 2-3j] 1*"'2 1*)3*4 &*5%0 0)'2 )',-' !)*+*
x = first : last ,first 6).3 1*&*7!% '*)3*4 )24 ,0)'2 )',-' !)*+*
6).3 '4 last 6).3 1*%**!$%' ,1 &2 !'.*$/3 1*%#-!%
.0,%&% )!'*3 '*&4 3')-0
x = first : increment : last ,first 6).3 1*&*7!% '*)3*4 )24 ,0)'2 )',-' !)*+*
'4 last 6).3 1*%**!$%' ,increment &2 !'.*$/3 1*%#-!%
.0,%&% )!'*3 '*&4 3')-0 6).3
x = linspace ( first ,last ,n ) 6).3 1*&*7!% '*)3*4 )24 ,1*)3*4 n &.3 0)'2 )',-' !)*+*
.last 6).3 1*%**!$%' ,first
x = logspace ( first ,last ,n ) 6).3 1*&*7!% '*)3*4 )24 ,1*)3*4 n &.3 0)'2 )',-' !)*+*
.*%!*)('& 7''*) &.3 ,10last 6).3 1*%**!$%' 10first

:83 46 .+%1) 02!/ .($+," /(6+$1 '&!+"2 x 0+*,++" ./ 0%('#& !

» x=[0:0.1:1]*pi; % or …

» x=[linspace(0,1,11)]*pi;

0+*),% *)3*4& 0*"/


.0$6" 5(91 +.+/ +%0$6"2 &!3 ,"7&0*(" 42 5+&7 &"' "2'. "7&0*( 8+.1 4&+#( 01&/) "&%!
– &%2" 01&/"+ "0+2" 0!#( ./ 5&&7( 5+2/0" 0!#(" .01&/ +.+/ )2 #,$%&/"+ ,"7&0*("
.":" 5(&#" &$&-)' '71.. ,"0+21 +/ 0+*1 ,4&!&70 4&01&/ 0!#() "&&%! ."$+('" 0!#( ./
."$+(' / "0+2" &01&/ )3) .#-&&.( "&&%!" ,+&$&7( 4&0!#( /)) ":" 5(&#" ./ 1+.3% 4/
:53) 4$+, +%0$6"2 ,0+*,++1 2(.2% 4/ 0%('#& !

» x(3)

ans =

0.6283

» x(1:5)

ans =

0 0.3142 0.6283 0.9425 1.2566

» x(3:-1:1)

ans =

0.6283 0.3142 0

362-1-2023 Matlab !"#$


-2-
!')74 !'+*),%% 0+*),% !35)0
..+(&&, .+7&0*(-..( ")+$6 "7&0*( 1&30") 5.&%
0%('#& !

» a=1:5,b=1:2:9

a =

1 2 3 4 5

b =

1 3 5 7 9

» c=[a b]

c =

1 2 3 4 5 1 3 5 7
9

:";" 5(&#" &"' ,5&+7 0132 &!3 , .+0+2" +$0!+& &$(( -+$ 80'( 4&0&$6( 02/3
» A=[1 2 3;4 5 6]

A =

1 2 3

4 5 6

0#'%. )',-'' !)#(0


» a=[1; 2 ;3]

a =

:(transpose +.+'(2(2) " '" 5(&#1 2+(&2 &"' +/

» a=[1 2 3 4].'

a =

1
362-1-2023 Matlab !"#$
-3-
2

4&$+(7) +3!"& 4&13+0( 4&0!#( ,206" &%!) "$+,% /)) transpose 5(&#1 2(.2% 4/
.4&#,)!(+,"
0%('#& !
» c=a+a*i

c =

1.0000 + 1.0000i

2.0000 + 2.0000i

3.0000 + 3.0000i

4.0000 + 4.0000i

» c'

ans =

1.0000 -1.0000i 2.0000 -2.0000i 3.0000 -3.0000I

4.0000 -4.0000i

)/ 4&#-&&.( /) 02/3 .4&30'( 5&1 .+)+'! 5&&!/( MATLAB-1 "." 5(&#" &))3 5!+/1
.&%+12-" ")+'!" ,(.&0/%&) "016)/( +%) .03+(" .+#-&&."" 80$1) .+7&0*( )/3 4&30'("
.4&(/+. 4&01&/ .+6+9 )' .'7+1(
0%('#& !
» a=[1 2];b=[3 4];

» c=a.*b

c =

3 8

&$(( 1&&-(+ .&0/&%&) "016)/1 +(3 .+7&0*( .)!3" 67&&( "$+,% /)) )!3 5(&#1 2+(&2
:4&(/+. .+7&0*(
» c=a*b'
c =
11

362-1-2023 Matlab !"#$


-4-
0+*),%& )&-$ 8*3 !'&'./
&01/( $-/ )3 )' ""9 ")+'! '7+1. "7&0*(+ 0),# 5&1 .&%+12- ")+'! 4&'71( 02/3
:"7&0*("
» a=[1 2;3 4]

a =

1 2

3 4

» a+2

ans =

3 4

5 6

» 2*a-1

ans =

1 3

5 7

!'+*),% 8*3 !'&'./


: '71) 02!/ .+7&0*( 5&1 .4&"9 4&$(( .+7&0*() 02/3 ,0+ 8/ .&02!/ .+7&0*( 5&1 ")+'!
")+'!" , "7&0*(" 42 &0-/ "$+,% 5(#% 4/2 +%&/0 013 .",9-+ ,+)&- )!3 0+#&- 0+1&-
.""9 #,$%&/ &)'1 4&01&/ 5&1 '7+1.
0%('#& !

» a=[1 2;3 4];


» b=[1 1;2 2];
» a+b
ans =
2 3
5 6
» a.*b
ans =
1 2
6 8
» a./b % ( = b.\a )

362-1-2023 Matlab !"#$


-5-
ans =
1.0000 2.0000
1.5000 2.0000
» a.^b
ans =
1 2
9 16
» a.^(-1)
ans =
1.0000 0.5000
0.3333 0.2500

:.+7&0*( 5&1 .+)+'! .(3#( "/1" ")1*"


a = [a1 a2 . . . an] , b = [b1 b2 . . . bn]
c = <a scalar>
a + c = [a1+c a2+c . . . an+c] 0+*),%' )&-$ )'3*7

a # c = [a1 # c a2 # c . . . an # c] 0+*),%3 )&-$ !&/50


a + b = [a1+b1 a2+b2 . . . an+bn] !'+*),% )'3*7
a . # b = [a1 # b1 a2 # b2 . . . an # bn] !'+*),% *)3*4 !&/50
a . / b = [a1/b1 a2/b2 . . . an/bn] *"%* -'&*7
a . \ b = [a1\b1 a2\b2 . . . an\bn] *&4%2 -'&*7
a . ^c = [a1^c a2^c . . . an^c] 0-973 !'&'./
c . ^a = [c^a1 c^a2 . . . c^an]
a . ^b = [a1^b1 a2^b2 . . . an^bn]

362-1-2023 Matlab !"#$


-6-
6).% 6'!3 )3*4 !/&70 '4 02*(
» a=[1 2;3 4]
a =
1 2
3 4
» a(2,2)=5
a =
1 2
3 5
» a(3,3)=6
a =
1 2 0
3 5 0
0 0 6

01&/ 4,() +%20$23 ,"9 "0,(1 53) 80'(" 6+# +/ )$+6 &16) .+0"7" 20+$ /) MATLAB
.4&#!/ +1 +!#+%+ )$6+" 80'(" 2*2 80'(1 (3,3) #,$%&/ )'1

!'+*),%-!!' !'+*),%
:.0-/ "7&0*( 8+.( "7&0*( .0$6"
» a=[1 2 3;4 5 6;7 8 9]

a =

1 2 3

4 5 6

7 8 9

» b=a(1:2,2:3)

b =

2 3

5 6

362-1-2023 Matlab !"#$


-7-
:.+0-/ .+7&0*(1 2+(&2 &$&-)' "7&0*( .130"
» a=[1 2;4 5]

a =

1 2

4 5

» b=[3 6]'

b =

» c=[a b;7 8 9]

c =

1 2 3

4 5 6

7 8 9

:",&0 "7&0*(1 2+(&2 - "$+(' +/ "0+2 .,&-(


» c(:,2)=[]

c =

1 3

4 6

7 9

:4&(&&," 4&#,$%&/" )3 ./ 67&( #,$%&/ 4+,(1 (:) 5(&#1 2+(&2


» c(:)’

ans =

1 4 7 3 6 9

:"7&0*( .0&7&) 0+*,+ )+!32


» c

c =

362-1-2023 Matlab !"#$


-8-
1 3

4 6

7 9

» d=c(:,[2 2 2 2])

d =

3 3 3 3

6 6 6 6

9 9 9 9

)31 ,"++2 "$+(' #,$%&/ &)'1 4&01&/" )3) #-&&.( ,":" - 4&)+6'" 4&&06+#1 5+2/0" 01&/"
01&/" ,)"%" "(6+$1 ."$+('" #,$%&/ ./ 0&$6( &%2" 01&/" ."$+(' 0+*,+ "2'() – .+0+2"
.0+*,++1 01&/ )3 0+1' "$+(' 0+*,+ .&&%1 8&)". '71.& 83&!) .0+*,+ /+" &%2"

./ 0!+# MATLAB-" .$-/ #,$%&/1 2+(&2 &"' &$((-+$ 80'() 8&&2" 01&/ )/ .+%!) 5.&%
."*() ")'(( ,.+$+('" 0$# &!) ,"7&0*(" &01&/
» a=[1 2 3 4;5 6 7 8]

a =

1 2 3 4

5 6 7 8

» a(5)

ans =

(false) 0,2 67&&( #!/ .4&&6+) 4&30'( 4&/0,% $-/+ #!/ 4&01&/" ./ 4&)&3(" 4&30'(
.(true) .(/ 67&( $-/+

0%('#& !

» x=-3:3

x =

-3 -2 -1 0 1 2 3

» abs(x)>1

362-1-2023 Matlab !"#$


-9-
ans =

1 1 0 0 0 1 1

» y=x(abs(x)>1)

y =

-3 -2 2 3

4&01&/) /&" .+#-&&."" ,&%/&)+1 &/%. &$&-)' .67+&( "7&0*(1 4&01&/) "&%!" 02/3
.&/%." ./ 4&(&&,("

4&#,$%&/1 2+(&2" ./ .(3#( "/1" ")1*"


Addresses a subarray within A defined by the index vector of desired A(r,c)
rows in r and index vector of desired columns in c
Addresses a subarray within A defined by the index vector of desired A(r,:)
rows in r and all columns
Addresses a subarray within A defined by all rows and the index A(:,c)
vector of desired columns in c
Addresses all element A as a column vector taken column by column A(:)
Addresses a subarray within A defined by single index vector of A(i)
desired elements in i, as if A was the columns vector, A(:)
Addresses a subarray within A defined by the logical array x. x mast A(x)
contain only the values 0 and 1, and mast be the same size as A

362-1-2023 Matlab !"#$


- 10 -
2'/*7 !'#'-/

Return indices of the x where its elements are nonzero i=find(x)


Return row and column indices of the array x where its elements are [r,c]=find(x)
nonzero

)+$6 *)-+(" 430'2 4&0!#(" )3( .130+(" "7&0*( "%1% .($+," /(6+$1 +(3 0%('#& !
:2+!&- .$+,!1 2(.2% . 3 $' (-3) :4+-.1 1 -(

» x=-3:3

x =

-3 -2 -1 0 1 2 3

» k=find(abs(x)>1)

k =

1 2 6 7

» y=x(k)

y =

-3 -2 2 3

362-1-2023 Matlab !"#$


- 11 -
!'/$'" 0+*),% !'#'-/
.A "7&0*(" )2 &2/0" 5+#3)/"( "$+(' 0+*,++ "%+1 -diag(A)
.&2/0" 5+#3)/" ./ 1&30& V 0+*,++" ,.&%+#3)/ "7&0*( "%+1 - diag(v)

:"$&-& .7&0*( .+%1) &$3 diag "$+,!1 2+(&2 "2'% 0%('#& !

» v=[1 1];

» I=diag(v)

I =

1 0

0 1

."*() +/ ")'() "7&0*( 1+1&# - flipud(A)


.")/(2 +/ "%&(& "7&0*( 1+1&# - fliplr(A)
.(4&01/ m*n )&3") .1&&- A) A "7&0*(" )2 4&$((" &+%&2 - reshape(A,m,n)
.")/(2 +/ "%&(& .+)'( 90 )2 .+)+!31 "7&0*( 1+1&# - rot90(A,k)

0%('#& !

» a=[1 2 3;4 5 6]

a =

1 2 3

4 5 6

» b=rot90(a)

b =

3 6

2 5

1 4

362-1-2023 Matlab !"#$


- 12 -
MatlabŸ›³¡¨ž´²¥ž
ž«³´›£›š

FACULTY OF ENGINEERING SCIENCES ž«ªžž£¬¨¦ž¢¦Ÿ±®ž


DEP. OF BIOMEDICAL ENGINEERING  ´£šŸ®²Ÿ£›ž«ªž¦ž±¦¡¨ž

Matlab´ª«
  «¨šª«
 ´£²šª£¦ž²›œ¦š
´Ÿ£¬›ž¨´¡š´£²š£ª£¦ž²›œ¦šŸ´Ÿ°£²¢¨£›Ÿ³£¡¢³®¦£¥MATLABž›´¥ª²Ÿ±¨›
ž£¬›§£²´Ÿ®°£¥žš²ª´£²š£ª£¦´ŸšŸŸ³¨´¥²¬¨¦³©Ÿ²´®š£ž´£²š£ª£¦ž²›œ¦š›´Ÿ°Ÿ®ªž
MATLAB›Ÿ 

´£²š£ª£¦´ŸšŸŸ³¨´¥²¬¨©Ÿ²´®
´ŸšŸŸ³¨´¥²¬¨žªŸ´ª

­ x1  2 ˜ x2  3˜ x3 366
°
®4 ˜ x1  5˜ x2  6 ˜ x3 804
° 7 ˜ x1  8˜ x2 351
¯
´£ªŸ£°£²¢¨žž´²Ÿ°›Ÿ

ª1 2 3º ª x1 º ª366º
« 4 5 6» x « x » «804»
« » « 2» « »
«¬7 8 0»¼ «¬ x 3 »¼ «¬351»¼

A˜x = b
ž²›œ¦š¨ §£²£¥¨³ £®¥ ´Ÿ°£²¢¨ ´¦®¥¨ ´œ°££¨ ž  ž²±¨› ¦®¥ž ´¦Ÿ¬®
 ©¨£«›MATLAB›´ª¨Ÿ«¨ž§£¥²¬¨´¦®¥¨¦Ÿœ£ª› ´£²š£ª£¦

§š £¡£ ©Ÿ²´® §££±  x = A -1 ˜ b  £Ÿ¢£›ž £¬ ©Ÿ´ª ´ŸšŸŸ³¨ž ´¥²¬¨¦ ©Ÿ²´®ž
ž¥£®žž°£²¢¨š£žAž°£²¢¨ž

 ´£²¦Ÿœª£«š¦ž°£²¢¨ ž¥£®žAž°£²¢¨ž§šžMATLABž´² ¬›±Ÿ›ª






 
D:\Tal\MATLAB COURSES\Matlab - B 05\Class_3__new.DOC
Až°£²¢¨¦³ž¢ªª£¨²¢det(A)%

ž¥£®žž°£²¢¨š£ž«®š¨žªŸ³ž¢ªª£¨²¢´¦¬›´£¬Ÿ›£²ž°£²¢¨

» A=[1 2 3;4 5 6;7 8 0]


A =
1 2 3
4 5 6
7 8 0
» det(A)
ans =
27

 Až°£²¢¨ž´œ²–rank(A)%
ž¥£®žž°£²¢¨š£žžš¦¨žœ²´¦¬›´£¬Ÿ›£²ž°£²¢¨
» rank(A)
ans =
3

§£¥²£´³›MATLAB››³¡¦²³®š´ŸšŸŸ³¨ž´¥²¬¨¦©Ÿ²´®ž´š
A¦´£¥®Ÿžžž°£²¢¨ž´ššŸ°¨¦š£žžªŸ³š²ž¤²ž
Až°£²¢¨¦´£¥®Ÿžžž°£²¢¨ž–inv(A)%

» A=[1 2 3;4 5 6;7 8 0];


» b=[366;804;351];

» x=inv(A)*b
x =
25.0000
22.0000
99.0000



-- Matlabʺʰʣʱ
 
›ž´Ÿ¬¨³¨Ÿ´£²š£ª£¦ž²›œ¦š›´¨££±žª£šŸ ¥ž¦Ÿ¬®´Ÿ°£²¢¨´±Ÿ¦¡´¦¦Ÿ¥ž£ª³ž¤²ž
£¥®Ÿž›¦®¥š£žMATLAB
´£ª¨£ž±Ÿ¦¡¦´£¦š¨³ž±Ÿ¦¡©£›´Ÿ°£²¢¨´±Ÿ¦¡›§££±ž¦›ž¦›¦§£³¦³£&
´£¦š¨³ž±Ÿ¦¡A\b=inv(A)*b
ʤʤʦʤʰʩʠʤʠʶʥʺʤ
´£ª¨£ž±Ÿ¦¡b/A=b*inv(A) 

ž¨œŸ¦(

» x=A\b
x =
25.0000
22.0000
99.0000

©¥¦Ÿ´Ÿ£¢¨´¨´Ÿ¦Ÿ¬®´Ÿ¡®¦¬««Ÿ›¨šŸž£ª³ž©Ÿ²´®žšŸžMATLAB›¯¦¨Ÿ¨ž©Ÿ²´®ž
²´Ÿ£²£ž¨

-- Matlabʺʰʣʱ
 
´ŸšŸŸ³¨ž²®«¨¨¦Ÿœ§£¨¦¬ª²®«¨§¬´ŸšŸŸ³¨´¥²¬¨©Ÿ²´®
ž±Ÿ¦¡žž ž²±¨›´ŸªŸ²´®­Ÿ«ª£š§£¨££±§£¨¦¬ªž²®«¨¨©¢±´ŸšŸŸ³¨ž²®«¨²³š¥
§£«®š¦³£¦¨£«±¨²®«¨Ÿ›³©Ÿ²´®ž´š©´£´A\b
ž¥Ÿ®žŸŸš«®ž°£²¢¨›Ÿ³£¡–pinv(A)%

š¦ž°£²¢¨¦³¤£®žŸŸš«®´›³¡¨Ÿ žŸ±®pinv(A)žŸ±®›³Ÿ¨£³š£ž´²¡š´Ÿ²³®š
´£¬Ÿ›£²

{ At ˜ A˜ At
1
£ª¨£
RM
 A A˜ ARM In

{ At ˜ A ˜ At
1
£¦š¨³ A
LM
ALM ˜ A=Im
žª¢±žž¨²Ÿªž¦¬›©Ÿ²´®žšŸž x=pinv(A)*bž²Ÿ°ž¨ MATLAB ›©Ÿ²´®
²´Ÿ£›

ª x1 º
 x 2 x12  x 2 2 x = « » £¨¨Ÿ²Ÿ¢±ŸŸš¨œŸ¦¦Ÿœ ž¨²Ÿª
¬x 2 ¼
» A=[ 1 4 7 2 ; 2 5 8 5 ; 3 6 0 8 ];
» b=[ 366 ; 804 ; 351 ];
» x=A\b
x =
0
-165.9000
99.0000
168.3000
» xn=pinv(A)*b
xn =
30.8182
-168.9818
99.0000
159.0545






-- Matlabʺʰʣʱ
 
V²Ÿ¢±Ÿ¦³ž¨²Ÿªž›Ÿ³£¡–norm(V)%
´ŸªŸ²´®ž£ª³¦³ž¨²Ÿªž´šžŸŸ³ª
» norm(x)
ans =
256.2200
» norm(xn)
ans =
254.1731

-- Matlabʺʰʣʱ
 
§££¨°¬§£²Ÿ¢±ŸŸ§££¨°¬§£¥²¬
 ž°£²¢¨ž´œ²š£žn²³š¥ On…O2O1§££¨°¬§£¥²¬§£¨££±A´£¬Ÿ›£²ž°£²¢¨¦¥¦
§£¨££±¨²³šž¨š´ž›vn…v2v1§££¨°¬§£²Ÿ¢±ŸŸ

 A ˜ v = O ˜ v
š›ž©®Ÿš›¬°›´£Až°£²¢¨ž©Ÿ«¥¦

 / V-1 ˜ $ ˜ V
ž°£²¢¨š£ž / A¦³§££¨°¬ž§£²Ÿ¢±ŸŸž©žž£´ŸŸ¨¬²³š´£¦Ÿ¨ž°£²¢¨š£žV²³š¥
A¦³§£¨°¬ž§£¥²¬ž§ž©Ÿ«¥¦š›§£²›£šžž›´£ªŸ«¥¦š
§££¨°¬§£¥²¬Ÿ§££¨°¬§£²Ÿ¢±Ÿ›Ÿ³£¡–[V,D]=eig(A)%

´£¦Ÿ¨ž°£²¢¨ v n

1 §£¦¨²Ÿª¨§££¨°¬§£²Ÿ¢±ŸŸ´°£²¢¨V
A¦³§££¨°¬ž§£¥²¬ž§žž£²›£š³´£ªŸ«¥¦š´°£²¢¨D
ž¨œŸ¦(

» A=[1 2 3 ; 0 4 5 ; 0 0 6]
A =
1 2 3
0 4 5
0 0 6

» [V,D]=eig(A)
V =
1.0000 0.5547 0.5108
0 0.8321 0.7982
0 0 0.3193
D =
1 0 0
0 4 0
0 0 6

» V*D*inv(V)
ans =
1.0000 2.0000 3.0000
0 4.0000 5.0000
0 0 6.0000

-- Matlabʺʰʣʱ
 
´Ÿ°£²¢¨¦³´Ÿ£°±ªŸ®
¥ž°£²¢¨¦³ž£°±ªŸ®´²œŸ¨´£²š£ª£¦ž²›œ¦š›

 f (A) = V ˜ f (/) ˜ V
-1

 A¦³§££¨°¬ž§£²Ÿ¢±ŸŸž§žž£´ŸŸ¨¬³ž°£²¢¨ ´£¦Ÿ¨ž°£²¢¨V


 A¦³§££¨°¬ž§£¥²¬ž§ž©Ÿ«¥¦šž£²›š A¦³©Ÿ«¥¦ /
©Ÿ«¥¦šž£²›š¨¡š¦¥¦¬´£²¦±«´¬°Ÿ›¨ž¦Ÿ¬®ž´£ªŸ«¥¦šž°£²¢¨¦³ž£°±ªŸ® f (/)
 ©Ÿ«¥¦šž£²›š¦¬¤²¬¨´¦Ÿ¬®¦ž¨Ÿ
Ÿ ¤²›´Ÿ¦¬Ÿ®žž°£²¢¨´Ÿ£°±ªŸ®²Ÿ›¬m´¨Ÿ£«›³¨´³¨MATLABž
ž°£²¢¨¦³£¬Ÿ›£²³²Ÿ³–sqrtm(A)%
ž°£²¢¨¦³£¬›¢§´£²œŸ¦–logm(A)%
ž°£²¢¨¦³¢ªªŸ®«±š–expm(A)%
ž°£²¢¨¦³fun´£¦¦¥ž£°±ªŸ®›Ÿ³£¡–funm(A,’fun’)%


§£¥²£´³› eA ž°£²¢¨¦³¢ªªŸ®«±š›³¡¦©´£ªž¨œŸ¦(

» A=[ 1 2 ; 3 4 ];
» expm(A) % Matrix exponential
ans =
51.9690 7 4.7366
112.1048 164.0738

» [V,D]=eig(A);
» V*diag(exp(diag(D)))/V % using Array exponential
ans =
51.9690 74.7366
112.1048 164.0738

-- Matlabʺʰʣʱ
 
ž´Ÿš´¦¬®ž©£›Ÿ¦ªžž²œžž£®¦ž°£²¢¨¦³ž£°±ªŸ®©£›¦›žž´š³£¡¨ªž¨œŸ¦(
ž°£²¢¨ž£²›£š¦¬ž£°±ªŸ®
» A=[0 pi/3 ; pi/6 pi/2 ];

» sinA=funm(A,’sin’) % function of a matrix.


sinA =
-0.1200 0.6048
0.3024 0.7873

» cosA=funm(A,’cos’) % function of a matrix


cosA =
0.7873 -0.6048
-0.3024 -0.1200

» sinA^2+cosA^2 % = Identity matrix


ans =
1.0000 0
0.0000 1.0000

» SINa=sin(A) % function of a array


SINa =
0 0.8660
0.5000 1.0000

» COSa=cos(A) % function of a array


COSa =
1.0000 0.5000
0.8660 0.0000

» SINa.^2+COSa.^2 % = matrix of ones


ans =
1 1
1 1


-- Matlabʺʰʣʱ
 
´Ÿ°£²¢¨¦¬´Ÿ£°±ªŸ®ž¦›¢

balance(A) Scale to improve Eigen-values accuracy


cdf2rdf(A) Complex diagonal form to real block diagram form
chol(A) Cholesyky factorization
cond(A) Matrix condition number
condest(A) 1-norm matrix condition number estimate
d=eig(A), Eigenvalues and iegenvectors
[v,d]=eig(A)
det(A) Determinant
expm(A) Matrix exponential
expm1(A) M-file implementation of expm
expm2(A) Matrix exponential using Taylor series
expm3(A) Matrix exponential using eigenvalues and iegenvectors
funm(A, ’fun’ ) Computer general matrix function 
hess(A) Hessenberg form
inv(A) Matrix inverse
long(A) Matrix logarithm
lscov(A) Least squires with konwn covariance 
lu(A) Factors form Gaussian elimination
nnls(A) Nonnegative least squares
norm(A) Matrix and norm vector norms
norm(A,1) 1-norm,
norm(A,2) 2-norm (Euclidean),
norm(A,inf) Infinity
norm(A,p) P-norm (vectors only),
norm(A, ’fro’ ) F-norm
null(A) Null space
orth(A) Orthogonalization 
pinv(A) Pseudo inverse
poly(A) Characteristic polynomial
polyvalm(A) Evaluate matrix polynomial
qr(A) Orthogonal-triangular decomposition
qrdelete(Q,R,j) Delete column from qr factorization

-- Matlabʺʰʣʱ
 
qrinsert(Q,R,j,x) Insert column in qr factorization
qz(A,B) Generalized Eigen-values
rank(A) Number of linearly independent rows or columns
rcond(A) Reciprocal condition estimator
rref(A) Reduced row echelon form
rsf2csf(U,T) Real schur form to complex schur form
schur(A) Schur decomposition
sqrtm(A) Matrix square root
svd(A) Singular value decomposition
trace(A) Sum of diagonal elements



-- Matlabʺʰʣʱ
 
´Ÿ¡Ÿ£¨´Ÿ°£²¢¨
«®š§£¨¨´¦¬›ž°£²¢¨¥MATLAB›´²œŸ¨ž±£²ž°£²¢¨

ž¨œŸ¦(

» a=[1 2 3;4 5 6];


» b=find(a > 10) 
b =
[]

WORKSPACE›§££±Ÿžª´³¨šŸž ž ž²±¨›b ž°£²¢¨ž¦³§³ž



§£«®š´°£²¢¨´²œž–zeros( )%
§£¡š´°£²¢¨´²œž–ones( )%
ž£¡£´°£²¢¨–eyes( )%
¦©£›§£²›£š§¬´£š²±šž°£²¢¨–rand( )%

ž¨œŸ¦(

» zeros(3)
ans =
0 0 0
0 0 0
0 0 0
» ones(2,4)
ans =
1 1 1 1
1 1 1 1

-- Matlabʺʰʣʱ
 
» ones(3)*pi
ans =
3.1416 3.1416 3.1416
3.1416 3.1416 3.1416
3.1416 3.1416 3.1416
» eye(3)
ans =
1 0 0
0 1 0
0 0 1

» rand(3,1)
ans =
0.2190
0.0470
0.6789

ž°£²¢¨´²£°£¦´£¬Ÿ›£²ž°£²¢¨²Ÿ°£¦ªž´Ÿ£°±ªŸ®ž¦³§££²œŸ«›´¡šž²®«›³Ÿ¨£³&
(m,n)›Ÿ´¥ª´ŸŸ¨¬nŸ´Ÿ²Ÿ³m´¦¬›
Až°£²¢¨ž£¨¨´šž²£ ¡¨žž£°±ªŸ®–size(A)%
´²¡šž°£²¢¨¦§£ž §£¨¨´¦¬›ž°£²¢¨²Ÿ°£¦£¥sizež£°±ªŸ®›³¨´³ž¦©´£ª

ž¨œŸ¦(

a =

1 2 3

4 5 6

» ones(size(a))

ans =

1 1 1

1 1 1


´Ÿ¡Ÿ£¨´Ÿ°£²¢¨›´Ÿ²Ÿ³±žMATLAB´Ÿ£°±ªŸ®´š´¨¥«¨ž¦›¢

-- Matlabʺʰʣʱ
 
´Ÿ¡Ÿ£¨´Ÿ°£²¢¨›´Ÿ²Ÿ³±ž´Ÿ£°±ªŸ®ž¦›¢

[] The empty matrix


compan Companion matrix
eye Identity matrix
gallery Several small test matrix
hadamrd Hadamard matrix
hankel Hankel matrix
hild Hilbert matrix
invhild Inverse Hilbert matrix
magic Magic square
ones Matrix containing all ones
pascal Pascal triangle matrix
rand Uniformly distributed random matrix with element between 0 to 1
randn Normally distributed random matrix with element having zero mean
and unit variance
rosser Symmetric eigenvalue test matrix
toeplitz Toeplitz matrix
vander Vandermonde matrix
wilkinson Wilkinson eigenvalue test matrix
zeros Matrix containing all zero elements


-- Matlabʺʰʣʱ
 
§££¨¨›²§£¥²¬¨
¦¡ž›¦›§££¨¨ŸŸ§££¨¨¡§£¥²¬¨›³Ÿ¨£³Ÿ²³®šMATLAB¦³´Ÿ¨Ÿ±´Ÿš«²œ
¨¦Ÿœ§£¨¨
«¨£¦¬›§£¥²¬¨ §££¨¨›²§£¥²¬¨›³¨´³ž¦©´£ªMATLAB - 5´«²œ¨
 
²Ÿ«š´Ÿ°£²¢¨§¬Ÿ›¬¦´Ÿ¬Ÿ£¨ž´£²š£ª£¦ž²›œ¦š´ŸŸ±®›§£¨¨›²§£¥²¬¨›³Ÿ¨£³
§£¨¨Ÿ§£¥²¬¨´´¦£¨¨›²ž¤²¬¨ž´š±²®¦²³®šž ž²±¨›
£²›š¨¡š¦¥©£›Ÿ²¦±«ž©£›žž ž¦Ÿ¬®¬°Ÿ›´£¨¨›²¤²¬¨Ÿ²¦±«©£›ž¦Ÿ¬®¦¬®Ÿ´§š
¤²¬¨ž
§£¥²³Ÿ¦³›´£²³®š£¨¨›²¤²¬¨´²£°£
§£«±ª£š›³Ÿ¨£³£¬ x

 §£¡Ÿ£¨§£¥²¬¨ MATLAB´ŸŸ±®›³Ÿ¨£³ x

 £¨¨›²¤²¬¨´›¥²ž catžŸ±®›³Ÿ¨£³ x

§£¨¨Ÿ§£¥²¬¨ž³Ÿ¦³¨´Ÿ›¥³³Ÿ¦³¦¬›£¨¨´¦´¤²¬¨´²£°£ž¨œŸ¦(

» a=[1 0;0 1];


» b=[2 2;2 2];
» c=[0 3;3 0];

» d=cat(3,a,b,c)
d(:,:,1) =
1 0
0 3
0 1 3 0
d(:,:,2) = 2 2
2 2 2 2
2 2 1 0
d(:,:,3) = 0 1
d (2,2,1)
0 3
3 0
» size(d)
ans =
2 2 3
» A=[1 0 4;3 5 6;9 8 7];
» A(:,:,2)=[1 0 4;3 5 6;9 8 7]
A(:,:,1) =
1 0 4
3 5 6
-- Matlabʺʰʣʱ
 
9 8 7
A(:,:,2) =
1 0 4
3 5 6
9 8 7
» max(A)
ans(:,:,1) =
9 8 7
ans(:,:,2) =
9 8 7
» size(A)
ans =
3 3 2
» ndim(A)
ans =
3
» whos
Name Size Bytes Class
A 3x3x2 144 double array


¦Ÿœ¢¦¡Ÿ¨ž§¥²¬³§£²®«¨ž¦¥¨´›¥²Ÿ¨žž°£²¢¨žª›ª´¨Ÿ±žš¨œŸ›Ÿ¨¥ ž¨œŸ¦ (
³Ÿ®£¡´Ÿ±®›³¨´³ª 3¬(-3)§Ÿ¡´›¨

» x=-3:3
x =
-3 -2 -1 0 1 2 3
» k=find(abs(x)>1)
k =
1 2 6 7
» y=x(k)
y =
-3 -2 2 3

-- Matlabʺʰʣʱ
 
´Ÿ®«Ÿªž°£²¢¨´ŸŸ±®
Až°£²¢¨ž¦³£³š²ž©Ÿ«¥¦šž¨žŸ¨¬²Ÿ¢±ŸŸžªŸ›diag(A)
£³š²ž©Ÿ«¥¦šž´š›£¥²£V²Ÿ¢±ŸŸž´£ªŸ«¥¦šž°£²¢¨žªŸ› diag(v)

ž£¡£´°£²¢¨´Ÿª›¦£¥diagžŸ±®›³Ÿ¨£³ž³¬ªž¨œŸ¦(

» v=[1 1];
» I=diag(v)
I =
1 0
0 1

ž¢¨¦Ÿšž¦¬¨¦ž°£²¢¨›Ÿ›£« flipud(A)
ž¦š¨³Ÿšžª£¨£ž°£²¢¨›Ÿ›£« fliplr(A)
 §£²›šm*n¦£¥ž¦´›££¡A Až°£²¢¨ž¦³§£¨¨ž£Ÿª£³reshape(A,m,n)
ž¦š¨³Ÿšžª£¨£´Ÿ¦¬¨¦³´Ÿ¦Ÿ®¥›ž°£²¢¨›Ÿ›£« rot90(A,k)


ž¨œŸ¦(

» a=[1 2 3;4 5 6]
a =
1 2 3
4 5 6
» b=rot90(a)
b =
3 6
2 5
1 4

-- Matlabʺʰʣʱ
 
MatlabŸ›³¡¨ž´²¥ž
ž«³´›£›š

FACULTY OF ENGINEERING SCIENCES ž«ªžž£¬¨¦ž¢¦Ÿ±®ž



DEP. OF BIOMEDICAL ENGINEERING  ´£šŸ®²Ÿ£›ž«ªž¦ž±¦¡¨ž

Matlab´ª«
  «¨¦Ÿœ²´
 ´Ÿª¥´

§£°›±
MATLAB£°›±¦³§£œŸ«£ª³§£¨££±M-FILES¦¬««Ÿ›¨MATLAB›´Ÿª¥´
 script ´£ª¥Ÿ´£°›± 

 function ž£°±ªŸ®£°›± 

§ž§£œŸ«ž£ª³©£›§££²±£¬ž§£¦›žž
¢¦®¥§£¢ª¨Ÿœ²š§£²£ ¡¨Ÿ¢¦±¥§£¢ª¨Ÿœ²š§£¦›±¨ž£°±ªŸ®£°›± x

§£ª´³¨ Ÿ¦£šŸ MATLAB ¦³ žŸ›¬ž ›¡²¨› §£¨££±ž §£ª´³¨ž ¦¬ §£¦¬Ÿ® M-FILES £°›± x
§£²œŸ¨ š¦ ²³š §££¨Ÿ±¨ §£ª´³¨ §ª£ž  ž¨°¬ ž£°±ªŸ®› §£²œŸ¨ž  ž£°±ªŸ® £°›±› §££¨£ª®
ž£°±ªŸ®¦¯Ÿ¡¨


¯›Ÿ±¦³£¦¦¥žª›¨
function [out1,out2,…,outm] = func_name(in1,in2,…,inn)
% FUNC_NAME Simple function that does nothing
% FUNC_NAME(IN1,IN2,..,) blha, blha, blha …
% … explanation of function …
… program body …

§£¢ª¨Ÿœ²šž §£¬›Ÿ²¨ §££²œŸ«› ž£²¡š function ´£¦£¨ž ´š ¦Ÿ¦¥´ ž£°±ªŸ® ¦³ žªŸ³š² ž²Ÿ³
§£¬›Ÿ²¨ž §££²œŸ«› ¤²Ÿ° ©£š ›¦› ¡š¢ª¨Ÿœ²š¦¦Ÿ¥ ¢¦®žŸ ž£¨› ž£°±ªŸ®ž¨ ¢¦®¥ §£² ¡Ÿ¨ž
©¨£«²¡š¦²››Ÿ´¥¦š¦Ÿš§£±£²§£¬›Ÿ²¨§££²œŸ«²£š³ž¦©´£ªž£°±ªŸ®ž¨¢¦®¦¦¥©£šŸž£¨›
Ÿ ž²Ÿ³¢¦±¥ž£°±ªŸ®ž´¦›±¨³§£¢ª¨Ÿœ²šž§£¦Ÿœ¬§££²œŸ«›Ÿ£²¡šŸž£°±ªŸ®ž§³¬£®Ÿ£©Ÿ£ŸŸ³ž
›¦›´Ÿ£°±ªŸ®›¬£®Ÿ´
lookforž£°±ªŸ®›³Ÿ¨£³ž³¬ª²³š¥Ÿ ž²Ÿ³œ£°£MATLAB H1 help 1´²Ÿ³´š²±ªž£ª³ž²Ÿ³
§£œŸ«ž£ª³¨§£°›±›¬£®Ÿž¦ž¦Ÿ¥£Ÿ ž²Ÿ³ž¨¦³ž°£¡¨¦ help ž² ¬´³±›´¨²³š¥Ÿš
help  Ÿ ž ´¨££Ÿ«¨ž žŸ±®ž ¦¬ ž² ¬ ³±›´´³¥ H1 ´²Ÿ³ §¬ ¡£ Ÿœ°Ÿ£ ´Ÿš›ž ´Ÿ²¬žž ´Ÿ²Ÿ³
 function_name
ž£°±ªŸ®´£ª¥Ÿ´ž­Ÿœ¬£®Ÿ£´Ÿ²¬žž´Ÿ²Ÿ³²¡š¦

 
D:\Tal\MATLAB COURSES\Matlab - B 05\Class_4_new.DOC

§££¦›Ÿ¦œ¥§£ª´³¨´²œž–global%
²£œž¦£Ÿ°²§££¨Ÿ±¨š¦Ÿ§££¦›Ÿ¦œ§£ª´³¨¥§£²¥Ÿ¨Ÿ£ž£ globalžŸ±®ž²¡š¦Ÿ¬£®Ÿ£²³š§£ª´³¨
´Ÿ¦Ÿœ´Ÿ£´Ÿš›³¨´³ž¦³£–´£ª¥Ÿ´ž´Ÿ²£ž›©¬¨¦Ÿ´£ª¥Ÿ´ž´¦£¡´›§££¦›Ÿ¦œž§£ª´³¨ž´š

a§±¨ž²³š¥©Ÿ´ª²®«¨¦³´£¦¦¥ž± ¡Ÿž£ª³ž± ¡´š´›³¡¨žž£°±ªŸ®›Ÿ´¥ª  ž¨œŸ(


 pow.m £¦›Ÿ¦œžª´³¨¥²œŸ¨Ÿ¬Ÿ›±
function P=pow(x, n)
% pow calculate second order power and general order power of a given
number
% pow(x,y) returns coefficients (P(1) and P(2))
% of the power of x : P(1) = x ^2 , P(2) = x ^ n

% A is a global variable
global a;
P(1) = x ^ 2;
P(2) = x ^ n;

´£ª¥Ÿ´ž´°²ž
» help pow

pow calculate second order power and general order power of a given
number
pow(x,y) returns coefficients (P(1) and P(2))
of the power of x : P(1) = x ^2 , P(2) = x ^ n

»pow(2,3)

ans =

4 8

-- Matlabʺʰʣʱ
 
§£ª´³¨£«Ÿ®£¢
MATLAB›§££«£«›¬£¨£«Ÿ®£¢ž³£³§£¨££±
§£´›–MATLAB›§£²®«¨¦³£¢²ª¢«žœŸ°££ž–double x

§£´›££¦¬œ°Ÿ£¨Ÿ´¦¥´Ÿ Ÿ²¡¨Ÿ§£ŸŸ´¦³£¢²ª¢«œŸ°££–char x

§£«®š©ž£²›£š›Ÿ²²³š´Ÿ°£²¢¨©Ÿ«¡š¦ž²Ÿ°–sparse x

§£¥²¬¨¦¬§££¢¨´£²š§£›Ÿ³£¡¬°›¦©´£ªš¦MATLAB¦³´£¢±®¨Ÿ±©Ÿ«¡š´²Ÿ° –storage x
ž «Ÿ®£¢¨

«Ÿ®£¢¦ž¨Ÿ£œŸ¦žª›¨›§£²Ÿ³±ž§£ªŸ³§£«Ÿ®£¢¨§£ª´³¨²®«¨œš¨ž©Ÿ«¡šžª›¨ – cell x
´Ÿ£¦£¬´Ÿª¥´´Ÿ®³›struct

cell¦ž¨Ÿ–struct x

£¨£¨›²¤²¬¨££¦¬œ°Ÿ£¨Ÿ¦š§£«Ÿ®£¢¨¡š¦¥

§£²Ÿ¢²®Ÿš
§£²Ÿ¢²®Ÿš¦³§£œŸ«ž³Ÿ¦³§£¨££±

Ÿ¥ŸA   ´Ÿ°£²¢¨Ÿ§£¥²¬¨¦¬´Ÿ£¢¨´¨´Ÿ¦Ÿ¬®¬Ÿ°£›¦–§££¢¨´£²š§£²Ÿ¢²®Ÿš x

£œŸ¦²Ÿ¢²®Ÿš´¦¬®ž a Ÿ ! !  FALSEŸšTRUE§£²£ ¡¨–§££«¡£§£²Ÿ¢²®Ÿš x


§£¨£¨¦¬›¤²¬¨ž²£ ¡¨Ÿ¤²¬¨ž£²›£š¨¡š¦¥¦¬£œŸ¦ž²Ÿ¢²®Ÿšž´šž¦£¬®¨§£¥²¬¨£ª³¦¬
£œŸ¦¤²¬¨š²±ªž ¤²¬¨´£œŸ¦žž¦Ÿ¬®ž´š°Ÿ´¦§š´ž›Ÿ¦£¥¨ž§£ž 

§££œŸ¦§£²Ÿ¢²®Ÿš x

 ²Ÿš´  ²Ÿ¢²®Ÿšž
 £œŸ¦AND  
 £œŸ¦OR  _
 £œŸ¦NOT  a
 £œŸ¦EXCLUSIVE OR  xor
´ŸŸ¨¬ž¦¬¦¬Ÿ®²Ÿ¢²®Ÿšžž°£²¢¨›«®š¨§£ªŸ³²Ÿ¢±Ÿž£²›£š¦¥§š²£ ¡¨ all
 «®š¨žªŸ³ž²Ÿ¢±Ÿ›²›£š§££±§š²£ ¡¨ any






-- Matlabʺʰʣʱ
 
  ž¨œŸ(

»A=[1 2 3;4 5 6;7 8 9];


»B=[3 3 2;4 7 6;0 8 1];
»A==B
ans=
0 0 0
1 0 1
0 1 0
»u=[1 0 6 0 9 2];
»v=[4 0 0 1 1 0];
»u & v
ans=
1 0 0 0 1 0
»u | v
ans=
1 0 1 1 1 1
»~u
ans=
0 1 0 1 0 0
»all(B)
ans=
0 1 1
»all(u)
ans=
0
»any(v)
ans=
1




-- Matlabʺʰʣʱ
 
ž¨£² ´Ÿ²±›
´£ª¥Ÿ´›´ŸŸ±®ž§² ›Ÿ´£ª¦´Ÿ³¨³¨ž´ŸŸ±®©ž control flow ž¨£² ´Ÿ²±›
for´š¦Ÿ¦
£¦¦¥žª›¨
for index = start : increment : end
…statements…
end
ž¬£«®ž¦³¦¡¨ž´²£²›¤²¬increment¦³´Ÿ¬£«®›end¤²¬¦start¤²¬ž¨§±´£indexžª´³¨ž
šŸž
  ž¨œŸ(
A¤²¬¨ž¦³´ŸŸ¨¬ž£¬°Ÿ¨¨´š´›³¡¨ mloop.m žš›ž´£ª¥Ÿ´ž

A=[13 5 10 7;5 6 7 8;15 10 13 12];


M=zeros(1,4);
i=0;
for x=A
i=i+1;
M(1,i)=sum(x)/length(x);
end
´£ª¥Ÿ´ž´°²ž
» mloop
» A
A =
13 5 10 7
5 6 7 8
15 10 13 12
» M
M =
11 7 10 9

¦³´Ÿ²Ÿ³žŸ±¨ž¦š¨³¢¬¨ £šª´Ÿšžš¦Ÿ¦©Ÿœ¥ ²Ÿœ«žª›¨¦´Ÿ¥££³ž´Ÿ²Ÿ³¦£¡´ž¦œŸžª&
Ÿ¥Ÿ´›¦¦Ÿ¥ž£³š²žª›¨²¨Ÿ¦¥ ´£ª¥Ÿ´ž¦³£¥²²£žžžª›¨ž´šž³£œ¨Ÿ ž²Ÿ°´£³š²ž´£ª¥Ÿ´ž
 §£ª›¨´´
´Ÿ¦Ÿ¬®£¬¦³¨¦žš¦Ÿ¦š¦¦žž žš°Ÿ´¦›±¦©´£ª²³š¥žš¦Ÿ¦›³Ÿ¨£³¨¬ª¨£ž¦¯¦¨Ÿ¨&
´Ÿ£°£²¢¨Ÿš´Ÿ£²Ÿ¢±ŸŸ

-- Matlabʺʰʣʱ
 
´Ÿ®«Ÿª§£¥²£´³›¬°›¦(MATLAB› ©´£ª´¨Ÿ±žš¨œŸž´š

  ž¨œŸ(

» [m,n]=size(A); % Method number 1

» M=sum(A)/m

M =

11 7 10 9

» M=mean(A) % Method number 2

M =

11 7 10 9


©¥¦Ÿ§£¥²¬¨¦¬´Ÿ¦Ÿ¬®¬°›¦MATLABž¦³´¦Ÿ¥£ž´š´Ÿ¦°ª¨©ž³§Ÿ³¨´Ÿ°¦¨Ÿ¨ž¦š´Ÿ²Ÿ°&
žš¦Ÿ¦ž¦³´£«¡£¤Ÿ²šžž°£²ž©¨ ´š´Ÿ¥«Ÿ¡

´°£²£¥¤Ÿ´§£ª›ªž§£¥²¬¨¦³³š²¨ž²œžš£ž©¨ ›©Ÿ¥«£¡¤²Ÿ°¦´°¦¨Ÿ¨ž´®«Ÿªž¦Ÿ¬®&
 £¦¨£«±¨ £®Ÿ«ž§¦Ÿœ›§£«®š£¥²¬¨¥žš¦Ÿ¦ž


-- Matlabʺʰʣʱ
 
 ´£ª´Ÿ¨žš¦Ÿ¦ while ´š¦Ÿ¦
£¦¦¥žª›¨
while expression
…statements…
end
  ´¨š£Ÿ¢£›¦žŸŸ³expressionŸ¬¦¥´¬°›¨žš¦Ÿ¦ž

¦²¥Ÿ¨ž²´Ÿ£›©¢±ž²®«¨ž eps¤²¬ž›Ÿ³£¡¦ eps1.m š›ž´£ª¥Ÿ´ž´š›Ÿ´¥ª  ž¨œŸ(


 MATLAB
num=0;
eps=1;
while (1+eps)>1
eps=eps/2;
num=num+1;
end

´£ª¥Ÿ´ž´°²ž
» eps1
num =
53
» eps=2*eps
eps =
2.2204e-016


› §¨Ÿ¥«³¤¥²®«¨¦­£«Ÿž¦²³®š³²´Ÿ£›©¢±ž²®«¨ž´š´£›£¢²¢£šž²Ÿ°›´³®¡¨´£ª¥Ÿ´ž
¨¦Ÿœž£ž£ MATLAB

-- Matlabʺʰʣʱ
 
if, else, elseif£šª´£¢®³¨
£¦¦¥žª›¨
if expression1
…statements… 
elseif expression2
…statements… 
else
…statements… 
end
§£š›ž§£²±¨žŸª¥´£ž ž²±¨›
žªŸ³š²ž´ŸŸ±®ž´²«¬°Ÿ›´§££±´¨£Ÿ¢£›§š
´ŸŸ±®ž´²«¬°Ÿ›´§££±´¨£Ÿ¢£›Ÿž£¨›£Ÿ¢£›¦³ž±£›¬°›´´§££±´¨š¦£Ÿ¢£›§š
ž££ª³ž
else¦´¥££³ž´ŸŸ±®ž´²«¬°Ÿ›´§££±´¨Ÿª£š§££Ÿ¢£›ž¨¡š­š§š
ž›Ÿ¡Ÿª£šelseifŸšŸelse›³Ÿ¨£³želseifœŸ«¨§£Ÿ¢£›²®«¨›³¨´³ž¦©´£ª

 Ÿš>0.1 - 0.9 >0 - 0.1 §Ÿ¡´›š°¨ª£š²±š²®«¨§šž±Ÿ›ª  ž¨œŸ(


 arand.m >0.9 - 1@
a=rand(1)

if a < 0.1

disp('range = [0-0.1)')

elseif (a < 0.9)

disp('range = [0.1-0.9)')

else

disp('range = [0.9-1]')

end

´£ª¥Ÿ´ž¢¦®
» arand

a =

0.8913

range = [0.1-0.9)

-- Matlabʺʰʣʱ
 
 eps2.m eps¤²¬ž›Ÿ³£¡¦for´š¦Ÿ¦¤Ÿ´›£šª´¢®³¨›³Ÿ¨£³  ž¨œŸ(

eps2=1;

for num=1:1000;

eps=eps/2;

if (1+eps)<=1

eps=eps*2;

break

end % {if}

end % {for}

´£ª¥Ÿ´ž¢¦®
» eps2

eps =

2.2204e-016


break
´š ±£«®ž¦ ©´£ª š¦ MATLAB › ´£ª¥Ÿ´ž ¤³¨žŸ žš¦Ÿ¦ž ´²£°¬ š£ž break žŸ±®ž ´Ÿ¬¨³¨
žª´³¨ ¦›±¨ ²Ÿ ¡¨ §Ÿ£«›³ £ª®¨ žš¦Ÿ¦ž žª´³¨ ¦³ Ÿ¥²¬ £Ÿª£³ ££¦¬ ¬°¨š› žš¦Ÿ¦ž ¬Ÿ°£›
¤²¬¨›š›žžŸ¨¬ž²Ÿ¢±ŸŸ´šžš¦Ÿ¦ž
while ´š¦Ÿ¦¦¤Ÿ´›§œ³Ÿ¨£³¦ž¨£š´¨breakžŸ±®ž
›¦›´£¨£ª®žžš¦Ÿ¦ž¨š°Ÿ£break´ŸªªŸ±¨´Ÿš¦Ÿ¦›


-- Matlabʺʰʣʱ
 
switch-case-otherwise
£¦¦¥žª›¨
switch expression (scalar or string)
case value1
…statements…
case value2
…statements…

otherwise
…statements…

end
£Ÿ¢£›Ÿšžª´³¨¦³Ÿ¥²¬¦§š´ž›´ŸªŸ³´Ÿ¦Ÿ¬®¬°›¦§£ª££ªŸ¬¨§ž›§£²±¨¦´³¨³¨switch´Ÿ±®
´ Ÿ²¡¨Ÿš²¦±«œŸ«¨
 swit1.m   ž¨œŸ(

go=1;

while go

qu=input('input 1,2,3,4,5 or another number to quit');

switch qu

case {2,4}

disp(' 2 or 4')

case {1,2,3}

disp(' 1 or 2 or 3')

case 5

disp(' 5')

otherwise

go=0;

disp(' end');

end

end

-- Matlabʺʰʣʱ
 
¯›Ÿ±ž´°²ž
» swit1

1,2,3,4,5 or another number to quit to quit 1

1 or 2 or 3

1,2,3,4,5 or another number to quit to quit 2

2 or 4

1,2,3,4,5 or another number to quit to quit 3

1 or 2 or 3

1,2,3,4,5 or another number to quit to quit 4

2 or 4

1,2,3,4,5 or another number to quit to quit 5

1,2,3,4,5 or another number to quit to quit 6

end


¬§£š´¨žcasež¨´£ª¥Ÿ´žž¥£³¨¨switchž´Ÿ±®²¡š¦©ž›C©Ÿœ¥´Ÿ®³›switch ¨žªŸ³›&
žš¦Ÿ¦ž¨´š°Ÿ£´£ª¥Ÿ´ž§£š´¨žcasež²¡š¦MATLAB›žš¦Ÿ¦ž­Ÿ«Ÿšbreak´Ÿ±®¦


-- Matlabʺʰʣʱ
 
´Ÿ£¨£ª®´Ÿ£°±ªŸ®
¯›Ÿ±ž ³š²› ¬£®Ÿ´ ´£³š²ž ž£°±ªŸ®ž ¡š ¯›Ÿ±› ´Ÿ£°±ªŸ® ²®«¨ ²£œž¦ ©´£ª MATLAB ¯›Ÿ±›
¯›Ÿ± Ÿ´Ÿš› ´Ÿ²œŸ¨ž ´Ÿ£°±ªŸ® ¯›Ÿ±ž ¤³¨ž› žª¬®Ÿ´ ´Ÿ²¡šž ´Ÿ£°±ªŸ®ž ¯›Ÿ±ž §³ ´š š³£´Ÿ
§£ª´³¨¥ Ÿ²œŸž §£ª´³¨ž §š š¦š  ´Ÿ²¡š ´Ÿ£°±ªŸ® ¦³ §££¨£ª® §£ª´³¨¦ ´Ÿª®¦ ´Ÿ¦Ÿ¥£ ©ª£š
 §££¦›Ÿ¦œ
š›ž²«›Ÿ ž£°±ªŸ®MATLAB³®¡¨š£ž³¦¥ž£°±ªŸ®¦ž£ª®›
´£¨£ª®ž£°±ªŸ® x

 ¤³¨ž›²›«Ÿ£ ´£¢²®ž£°±ªŸ® x

´£¦¦¥ž£°±ªŸ® x

´£¢²®ž£°±ªŸ®
›šž ´°£¡¨› ´Ÿ£ª¥Ÿ´¨ ±² ©ž£¦š ´³œ¦ ©´£ª private §³› ´¡Ÿ£¨ ž°£¡¨ ´¡´ ´Ÿª¥Ÿ³ž ´Ÿ£°±ªŸ®
³¡¨ ²£œž¦ ©´£ª ›šž ´°£¡¨¦ ¯Ÿ¡¨ ´Ÿ³£œª ©ª£š ´Ÿ£¢²® ´Ÿ£°±ªŸ®³ ©ŸŸ£¥¨  parent directory
´Ÿ²¡š´Ÿ°£¡¨¨´Ÿ£°±ªŸ®¦žž §³´Ÿ¦¬›´Ÿ£°±ªŸ®



´Ÿ Ÿ²¡¨´²¨ž
´ Ÿ²¡¨›¬£®Ÿ¨ž£Ÿ¢£›´°²ž–eval(‘string’)%
´ Ÿ²¡¨¥ž¬£®Ÿ¨žž£°±ªŸ®´°²ž–feval(‘function’)%

  ž¨œŸ(

» t=’i+j’;
» i=2;
» j=3;
» eval(t)
ans=
5
»f=’sin’;
»k=pi/2;
» feval(f,k)
ans=
1

²Ÿ£ž¦´ª´£ªŸevalž£°±ªŸ®ž¨²´Ÿ£ž¦£¬£š£ž©¥³fevalž£°±ªŸ®›©´£ª³¥³¨´³ž¦­£¬&

-- Matlabʺʰʣʱ
 
ž£° £¨£¢®Ÿš
¯£š´´Ÿš¦Ÿ¦¦³ž£° £²Ÿ¢±Ÿ§£²Ÿ¢±ŸŸ´Ÿ°£²¢¨¦¬´Ÿ¦Ÿ¬®¬Ÿ°£›¦žªª¥Ÿ´³´£ª¥Ÿ´žª£žMATLAB
´£ª¥Ÿ´ž¬Ÿ°£›´š
 sin1.m 10*pi¬¨«Ÿª£«£¥²¬¦£¥¨ž²Ÿ¢±Ÿ´²£°£  ž¨œŸ(

tic
i=0;
for t=0:0.01:10
i=i+1;
y(i)=sin(t);
end
toc

 sin2.m ž£° £²Ÿ¢±Ÿ²¡š¦Ÿ ´£ª¥Ÿ´

tic

t=0:0.01:10;

y=sin(t);

toc


žªŸ³š²ž´£ª¥Ÿ´ž¨ž²£ž¨ž£ª³ž´£ª¥Ÿ´ž£¥žš²´´£ª¥Ÿ´ž£´³¦³ž°²ž


-- Matlabʺʰʣʱ
 
debugger-žžª›¨¼¡®«ª

-- Matlabʺʰʣʱ
 
Matlab-* 12-'! .03!
!""2. 1%1/

FACULTY OF ENGINEERING SCIENCES !"#$!! %&#'( !)(*+,!


DEP. OF BIOMEDICAL ENGINEERING .%/*,0*%1 !"#$!( !+(-'!

Matlab !"#$
5 '$% &'()!
!,#%% -'# *+,-)(

.,#%,%-'# .'+/ 0'0)1 – plot(x,y) !


8%01%/ %#%-(& .56*%' 4051 !#*+$ (3 02/3 ,.x 0*)+* #5$3 y 0*)+* (2 405 .0%%6' *7 !#*+,
y-* x 8%0*)+*1 .*0#5*'! .*#*+$! .(!**2 #'%'1 .*%!( 8%1%%- 8%0*)+*!) 8%0*)+*1 8%'%/.'
n *(1+.% m 90*/1 0*)+* *$%! x-* n ! m (#*51 90&' *$%! y *1 !0+'1 .8%02% 8%**+1 .*01*-'
.8%$*2 8%&161 .*'*+&
: 0 # 2 * " 8*-.1 sin(x) (2 405 )*)02 (5.1) *%('# "

» x=linspace(0,2*pi,30);

» y=sin(x);

» plot(x,y);

:(1+.'! )(,!

.8%02% 8%**+1 .*01*-'! ,(y-* x 8%0*)+*! (#*5) .*#*+$ 30 %,( 8*+& (1+.!
#5$3 y 0'*(3 , i #5$3 y(i) %30& (2 405 (1+.% plot(y) /'5*#( ,#-/ 8%$*.$ 0*)+* +0 :%*6% 8/
.(y 0*)+*! (#*5=n , i=1,2,....,n) y (2 8%"+#$%/!

-1 -
D:\Tal\Matlab Intro course\Matlab - B 05\Class_5.DOC
:s .7*0-' .,"*! %#%-(& *+! &16* 5*" ./ &*1+( .02,/' plot !#*+,!

» plot(x,y,s)

:.*/1! .*%*02,/! 9*.' :*$5"* &16 0*-1( :.%$


/34
2'"($
g green - solid
b blue . point
y yellow * star
m magenta : dotted
c cyan -. dashdot
r red -- dashed
w white o circle
k black + plus
x x-mark

.#-/ 4051 .*'*+& 0,"' )*)02( 85 2'2( !(*3% plot !#*+,!

(5.2) *%('# "

» plot(x1,y1,’r’,x2,y2,’b’,x3,y3,’y’)

.1*!61 x3 #5$3 y3 * (*-31 x2 #5$3 y2 ,8*#/1 x1 #5$3 y1 :.*'*+& 2*(2 ))02. ("$! !#*+,!

362-1-2323 Matlab !"#$


-2-
.,1)! 3'4,/
x ),4 !)!'5 !)#(* – xlabel(‘string’) !

y ),4 !)!'5 !)#(* – ylabel(‘string’) !

)35/ !'/4%63 '%'+,%' 6)+% !)#(* – legend(‘string1’,’string2’,…) !


.,1)! !)!'5 !)#(* – title(‘string’) !

(5.3) *%('# "

» x=linspace(0,2*pi,30);

» y=sin(x);

» z=cos(x);

» v=cos(x).^2;

» plot(x,y,'g:',x,z,'r-.',x,v,'co');

» grid on;

» ylabel('dependent variables y,z and v');

» xlabel('independent variable x');

» legend('sin(x)','cos(x)','cos(x)^2');

» title('sine and cosine curves');

:(1+.%2 )(,!

sine and cosine curves


1

0.8

0.6
dependent variables y,z and v

0.4

0.2

-0.2

-0.4

-0.6
sin(x)
cos(x)
-0.8
cos(x)2

-1
0 1 2 3 4 5 6 7
independent variable x

362-1-2323 Matlab !"#$


-3-
(on/off) !'0",#)'+* !1) &1 *)$*/*-$'* – grid !
7)(* .'8! !/,3+ - axis([Xmin Xmax Ymin Ymax]) !
.%*(. %.(1!* %*(.! - 8%$.2'! (2 8*'%$%'!* 8*'%"+'! %30& %, (& 0#5*' 405! 8*-. (#-' .0%013
.Ymin,Ymax -* Xmin ,Xmax %,( )(,1 8*-.! ./ .$3#&' axis !#*+,!
(on/off) .,,+ ,-)( 2'&83 7)( &/ 7)( !$-#* &'0,3/*&/-* – hold !
hold !#*+,1 2*'%2 .(figure) %,05! :*(-!' 8#*+! )(,! ./ !+$' 2#- )(, )*)02 (#-' .0%013
.(#-'! .0%01 16'( !0%7-' hold off .405 (& 405 .",#! 02,/'* (#-'! .0%01 16' ./ ()1' on

SUBPLOTS
.(subfigures) .*$*(--.. n ! m ( :*(-! ./ +(-( :.%$ #-/ %,05 :*(-1 8%$*2 8%,05 0,"' .56!(
.,-)( )-$% !(4*& ,-)(* 2'&8* !+'&8 – subplot(m,n,i) !
:%'%( (/'2' 8%0,"$ .*$*(-! .. 02/3) i '"' :*(- ..1 "%,#. *7 !#*+, %0-/ &%,*.2 plot .#*+,
.(!)'( !(&'('*
(5.4) *%('# "

» x=linspace(0,2*pi,30);

» y=sin(x);z=cos(x);

» a=2*sin(x).*cos(x);

» b=sin(x)./(cos(x)+eps);

» subplot(2,2,1);

» plot(x,y),axis([0 2*pi -1 1]),title('sin(x)')

» subplot(2,2,2);

» plot(x,z),axis([0 2*pi -1 1]),title('cos(x)')

» subplot(2,2,3);

» plot(x,a),axis([0 2*pi -1 1]),title('2sin(x)cos(x)')

» subplot(2,2,4);

» plot(x,b),axis([0 2*pi -20 20]),title('sin(x)/cos(x)')

:/1! )(, -! (1+.%

362-1-2323 Matlab !"#$


-4-
(figures) !'"'&8 )-$%3 *+,-)(
:*(-! (/ .*$,( :.%$ *.*&6'/1 ,'"' :*(- (3( .8%#0,$ .*$*(-1 8%,05 '"' ))02( :.$ Matlab -1
.*%15( +0 .*%)$1(0 !$%%!.2 .*(*&, &61(*
2#- %,05 :*(- .-%., – figure(h) !

(5.4) *%('# "

» figure(2)% creates new figure number 2

» close(2)% close figure number 2

» figure(1)

» figure(3)

» close all% close all figures

.8%%+ 013 /*!* !#%'1 x 0,"' :*(- .56!( .'0*5 figure(x) !#*+,!
.%-3*$! %,05! :*(-! ./ 0*5". :*(-! 0,"' :*%6 /(( close !#*+,!
.(,85'"* 2'&8* – &#8% !),)3) n )-$% 2'&8 ,'+," clf(n) !

*"0+* '6 *&#(*


.,#%,%-'# 7)( &1 *"0+* '6 *&#(* – zoom !
.zoom on .#*+,1 2*'%2 %"& %$#% :,*/1 405! ./ :%)+!( */ (%#5!( :.%$
.(#5*'! !$*'.! 730' '+$) !%*60! !#*+$1 (013&! .*&6'/1) :'"! ./ 8+'( 2% enter .2+! %0-/
.*.*/ :%)+. %$'%! (& !6%-(* 405! ./ (%#5. 013&! (2 %(/'2! 0*.,3! (& !6%-( .(.$)+*' */
.013&! (2 !0%05* !6%-( %"& %$1(' 8*-. &*1+( 85 02,/
.zoom off 2%+!( 2% zoom on (*)%1(

362-1-2323 Matlab !"#$


-5-
!',-)( 0&+ !'#'+-
)35/*% ,-)( 0&+ – [x,y]=ginput(n) !
, n '"' :*%6 /(( .y-* x 8%$.2'1 *0'2% 013&! .*&6'/1 405! (& *$'*"%2 .*#*+$ n (2 .*)$%#0*+!
.ENTER 2+'! (& !6%-( #& .*#*+$ :'"( 02,/% 12-'!
.;-($2 2+'! (2 ASCII -! 0,"' ./ .*)$%#0*+( 4"*$1 0*'2. [x,y,button]=ginput(n) !#*+,!

0'0)1
'+ 0'0)1 – line(x,y) !
.( plot -1 *'3) y(i+1) ,x(i+1) .*#*+$( y(i),x(i) .*#*+$!' 8%**+ )*)02

(5.5) *%('# "

» x=[4 6 8 9]

x =

4 6 8 9

» y=[6 7 10 11]

y =

6 7 10 11

» line(x,y)

:(1+.'! )(,!

362-1-2323 Matlab !"#$


-6-
2'(,&'- 0'0)1 – fill([x,y,c]) !
.c &161 x,y %"& 0#5*'! 8*-.! ./ .&1*6 !#*+,!

:y-* x 8%0#5*'! 8%0*)+*! (& !#*+,! ./ (%&,$ (5.6) *%('# "

» fill(x,y,'g')

:(1+.'! )(,!

362-1-2323 Matlab !"#$


-7-
.,"'1 .,-)(
,)&'- 7)( 0'0)1 – polar(theta,rho,s) !
.s :*$5"1 theta .%*7! #5$3 rho "*%#0! (2 %0(*, 405

(5.7) *%('# "

» r=linspace(1,30,20);

» teta=linspace(0,2*pi,20);

» polar(teta,r,'b')

:(1+.'! )(,!

.,'$% .'8!3 *,4+"'- )',4 – fplot(‘fun_name’,[xmin xmax]) !

(5.8) *%('# "

» fplot('sin(1 ./ x)', [0.05 0.1])

:(1+.'! )(,!

362-1-2323 Matlab !"#$


-8-
.Y )'0+''3 .,)3,6* !'(&-!* !6 )6!%* 7)( - hist(Y,N) !
,0*)+** /*! N 02/3 .Y -1 %('%"+'!* %('%$%'! 8%01%/! :%1 8%-**0'! '"' ./ :%%6' 0(+" N

-' 8%(*#5* Ni $ Ni $1 -' 8%$)+ Y -1 8%01%/ !'3 .$%%6'! !#*'& N(i) 01%/ (3 0*1& .(1+.'
2

. Ni $ Ni %1
2

.!'#'%/ N &1 !'4'3+ M-5 M-by-N *4,)0% &1 !'#'%/* 0'0)1 – bar(X,Y,'linetype') !

!#*+$1 8+*'. y(n) !#*'&! x 0*)+** /(( .%+,*/! 0%6! %15 (& .*#*'&! 8*+%' ./ 56%%' x 0*)+**
. %+,*/! 0%6! %15 (& n

.#,#3 7)( 0'0)1 – stem(X,Y,'linetype') !

.!'()#% 7)( 0'0)1 – stairs(X,Y) !

(5.9) *%('# "

» x=linspace(-2,2,20);
» y=-x.^2+5;
» yy=[1 1 2 3 2 5 6 3 4 5 6 7 ];
» nn=[1 2 3 5];
» z=x.^3;

» subplot(2,2,1);
» bar(x,y,'g')
» axis([-2 2 0 5])
» title('bar graph');

» subplot(2,2,2)
» stairs(x,y);
» title('stairs graph');

» subplot(2,2,3)
» hist(yy,nn);
» title('histogram');

» subplot(2,2,4)
» stem(x,z);
» title('stem');

362-1-2323 Matlab !"#$


-9-
:(1+.'! )(,!

362-1-2323 Matlab !"#$


- 10 -
!'6,(1 7)( – errorbar(X,Y,L,U,SYMBOL) !
8%0*)+**1 .*$*.$! .*/%52! :*'%"* X 0*)+** #5$3 Y 0*)+** %"& 0#5*'! 8*+&! :*'%" ./ ((*3! 405
.(!(&' %,(3 !/%52) U -* (!)' %,(3 !/%52) L
'"'( 8/.!1 8%,05 '"' *(1+.% .*6%0)' 0*1& ,!!7 #'' %(&1 .*%!( 8%1%%- X Y L U 8%0*)+**!
.!#*'&! %0*)+**
.(plot -1 *'3) 8*+&! (2 :'%" *//* &16 .7*0-' /*! SYMBOL -!
.errorbar(Y,L) !#*+,! %#%-(& (1+.% [y-L,y+L] 8*-.1 !/%52 .*#*'&* n #5$3 y(n) (2 405

(5.10) *%('# "

» y=[1:2:40];

» x=rand(size(y))*100;

» errorbar(y,y.^2,x,2.*x,'k');

:(1+.'! )(,!

2-4% 7)( 0'0)1 – compass(X,Y,'S') !

(2 */ Xn !i $ Yn !j 8%/.'! 0*)+**! (23 /*! ;- (3 (2 *$**%3* *(#*5 ,.%2/0!' 8%/6*%2 8%6- 405
.8%13*0' 8%0,"' *%01/2 Z #-/ 0*)+**1 4%(-!( 02,/ Y -* X ./ . X n $ iYn 13*0'! 0,"'!
feather 7)( 0'0)1 – feather(X,Y,'S') !

#-/ 8%**2 8%-**01 8%'+*''* %+,*/! 0%6!' 8%/6*% 8%6-! :/32 /(/ compass *'3 8%6%- 405
.%$2!'

362-1-2323 Matlab !"#$


- 11 -
(5.11) *%('# "

» z=eig(randn(20,20)) % create vector with 20 random complex numbers

» subplot(2,1,1)

» compass(z,'r')'

» title('compass');

» subplot(2,1,2)

» feather(z,'g');

» title('feather');

:(1+.'! )(,!

362-1-2323 Matlab !"#$


- 12 -
Matlab-) !"#$% &'(%
%","& !*!+

FACULTY OF ENGINEERING SCIENCES %,-.%% */-$0 %10)23%


DEP. OF BIOMEDICAL ENGINEERING &*+)3')*! %,-.%0 %20#$%

Matlab !"#$
(!'*$'!' +,%-) 5 '$% &'()!
Graphics Handle
%0+ &)-)23 .%%)!5 %$'! &)*3'5 &)-)23 &)+'2. ,%$)-() axis, mesh, plot : 4)5( &)*3'5 &)-)23
.handle graphics -&)+'2.) %()$. %$'! &)*3'5 &).)(& &)/!)2% ,&).*1)' 0/ &),,)!$
8/ 0-#$ &'*'! *('/ &)0!2$% ,&).)(& &('/$ &$**2 (%$)-() 7'5 ,'*6 ,4)0#) *3'5 12**!)+ 0(0
.)0+ &).)(& &+ &)."0 )+ /)!20 "$&"$0 '"3+$ MATLAB .12**!)+% &+ &'6)*% %-)23% &0/3%
8*'-),$ 8*12**!)+% .MATLAB -! 8*$**2% 8**3'5% 8*12**!)+% &+ %5*6$ %+!% %$'5+*-%
.&*(''*% %')6!
Root
(Computer Screen)

Figure Figure

Uimenu Uicontrol Axes Axes

Image Line Patch Text Surface

%$'! )*0/$ 8*+6$.% 8*12**!)+% &+ 85 (8*$**2 +0 8% 8+) ')6*& *3'5 12**!)+ &'6)*% %-)23 0(
&'6)* (line 5),$ 12**!)+ +)%") x -5.( y 8)2/ &11'"$% ,plot(x,y) %-)23% ,+$5)-0 .&*(''*%%

*-*-0/ %%)9$ MATLAB -! '-5)$% 12**!)+ 0( .figure 5),$ 12**!)+) axes 5),$ 12**!)+ 85

)0 8*$)- 8*12**!)+ -)/ 8*$**2 8+ 85 8*),$ 12**!)+ 0+ &).30 4&*." :( ,‘handle’ %.)($% '3,$
.:,$% 0/
.:,$% 0" 4)*0/% 20#! ,0-#$ &'*'!( ,56)$ (figure) *3'5% 4)0#% 0" )'3,$ ,+$5)-0
:'/ /)!20 4&*. %.)(& 0(0 ./)!2 &).)(&% '3,$ .)*3)+ &+ &)/!)2% &).)(& *3'5 12**!)+ 0(0 ,')$+(
.%$)-() 5), ,8)2*$ ,/!6 :4% &)*.**3)+ &).)(& .8*('/ 0" %6)!2 :)&$ -#+
-+$5)-0 ,%0)-5 &)+( &.$),$ %0*$ 0( 0" %.)"+'% &)+%) 8**"'5 :)&! !&(. %.)(&% 8"
4**60 4&*. .&)0)-5 &)*&)+! "$&"%0 !**# ).*+ +)% %.)(& 0" 8" 4**6$ &.(&$% '"+( .‘LineStyle’

*'"3+ ‘Position’ %.)(&% 4)*6 +$5)-0 .%-*#* %')6! '-5)& %.)(&%" :( ,&).)"+'% &)*&)+% &+ 2'

.'pos'-)+ ‘position' *"/ 85


-1 -
D:\Tal\MATLAB COURSES\Matlab - B 05\class_5_extra.doc
:8*('- *&"! 8*),$ 12**!)+ 0" %.)(& :'/ /)!20 4&*.
.)&'*6* 4$9! (1

.set %-)23% &)/6$+! ,'6). 12**!)+%" '#+0 (2

value +)4& H 01..2'3 &, Property !"'/! .'"., – set(H,’Property’,value) !

.&).)(& '3,$ &)."0 ,&#+ %-)23! 4&*. ,)9 %-)23!


*3'5 12**!)+ &.)(& &/*!2 (11.1) -%('# "

» H1=figure('Color','b') % method 1: open blue figure


H1 =
1
» set(H1,'Color','r') % method 2: change
figure to red

» % change two properties in one command:


» set(H1,'Color',[0 0.5 0.5],'NumberTitle','off','Name','My Figure')

-#+ 56**$ (-#+0 ,3+ 4*!) '3,$ 0( ,[8)-+ 2)'* 0)#(] R.B.G 1$')3 *30 /!6 '-5)% %.)'#+% %-)23!
:8% 8**.)6*2% 8*('/% .-),* */!6 &")0"$
.4!0 /!6= [1 1 1] , ')#" /!6 = [0 0 0]
:line 5),$ 12**!)+ &'-5% *"/ 05/$! &)'-),$% &)-)2. 1)1'" (11.2) -%('# "

» x=linspace(-1,1,32);
» y=sqrt(1-x.^2);
» H2=line('Color',[0.5 0.5 1],'LineStyle','none',...
'Marker','o','MarkerSize',6,'Xdata',[x x],'Ydata',[y -y])

H2 =
3.0002

:&*.()&% 103
1

0.8

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1

.(H2=3.0002) line 12**!)+0 handle %'*9#$ &*.()&%

362-1-2323 Matlab !"#$


-2-
.8*0+*6.1)3 8*('/) &).)(& 0" %$*"' &,3-% 85 &'"3+$ set %-)23%
>> set(H2)
Color
EraseMode: [ {normal} | background | xor | none ]
LineStyle: [ {-} | -- | : | -. | none ]
LineWidth
Marker: [ + | o | * | . | x | square | diamond | v | ^ | > | < |
pentagram | hexagram | {none} ]
MarkerSize
MarkerEdgeColor: [ none | {auto} ] -or- a ColorSpec.
MarkerFaceColor: [ {none} | auto ] -or- a ColorSpec.
XData
YData
ZData

ButtonDownFcn
Children
Clipping: [ {on} | off ]
CreateFcn
DeleteFcn
BusyAction: [ {queue} | cancel ]
HandleVisibility: [ {on} | callback | off ]
Interruptible: [ {on} | off ]
Parent
Selected: [ on | off ]
SelectionHighlight: [ {on} | off ]
Tag
UserData
Visible: [ {on} | off ]

.{} 8*0,0),$ 8**'5),! &).$),$ 0-#$% &)'*'!


&).)(& 0" 8*('/ "2!0 '"3+ .get %-)23% &)/6$+! 0!20 4&. &).)(&0 )$+&)%" 8*('/% &$*"' &+
.%+!% +$5)-! )$( &).)(&% 0( &$*"' &+ )+ ,&)*3*63,
>> get(H2)
Color = [0.5 0.5 1]
EraseMode = normal
LineStyle = none
LineWidth = [0.5]
Marker = o
MarkerSize = [6]
MarkerEdgeColor = auto
MarkerFaceColor = none

362-1-2323 Matlab !"#$


-3-
XData = [ (1 by 64) double array]
YData = [ (1 by 64) double array]
ZData = []

ButtonDownFcn =
Children = []
Clipping = on
CreateFcn =
DeleteFcn =
BusyAction = queue
HandleVisibility = on
Interruptible = on
Parent = [2.00049]
Selected = off
SelectionHighlight = on
Tag =
Type = line
UserData = []
Visible = on

.)9 %-)23 *-*-0/ *).*"0 &).&*.% &).)(&% &+ 2') :+ &00)( set %-)23% *"/ %0!2&%" %$*"'%
.(line -5),$ 12**!)+) H2 12**!)+! &)')"2% &).)(&% 0( &+ &00)( %**."% %$*"'%

current object
.'!(/% *-* 0/ '#!." 4)'#+% 12**!)+% 0+ ,#**&$ %9 5")$
:&)+!% &)-)23% &)/6$+! 85 current object -% 0+ &).30 4&*.
.5/'"- figure-&, handle !&21 – gcf !

.5/'"- figure-2 6.).7- &, handle !&21 – gca !

.5/'"- 01..2'3- &, handle !&21 – gco !

:0)#(0 *#().% figure-% /!6 *).*" (11.3) -%('# "

» figure(1)
» gcf
ans =
1
» set(gcf,'Color','b')

&'!)#! +)6$0 4&*. (‘property’) 8*12**!)+% &).)(& 0( ')!/ /-*$


.‘Using Matlab Graphics’

362-1-2323 Matlab !"#$


-4-
!'.7%."3' 6.0)$
.MATLAB -! %*6$*.+ /)6*! &)'"3+$ &)1*" *&"

drawnow - -#'1*2 ,'%.,' !'"'/! .'"., ."4 -.7%."3


:0/ &,,)!$ )9 %1*"
.surface )+ image , text , patch , line :5),$ 8**3'5 8*12**!)+ &'-5% !

).(-)/* '"+( "-#$ 11')"*) 12**!)+% 2#$* %9 !6$! ,‘xor’ !6$0 ‘EraseMode’ %.)(&% &/*!2 !
.).)"* +0 /2'! 8*+6$.% 8*'#+ 8*12**!)+ .)*&).)(&

&).)(&! '!)-$ 00( :'-! .handle graphics &)-)23! ")$*" *"/ %"/. 8*12**!)+% &).)(& *).*" !
.(‘xdata’,’ydata’,’zdata’) 8)2*$

0/ 8*56)$% 8*12**!)+% 0( &+ 4(-/0 !"#$% &+ &60+$ )9 %-)23 ,drawnow %-)23! ")$*" !
.:,$%

:anim1.m &*.()&% – (drawnow -! ")$*") 05 &/).& (11.4) -%('# "

clf;
%clear figure
x=linspace(0,8*pi,100); %define vector x
axis([0 8*pi,-2 2]); %define axes domain
gal=line('color',[0 0 1],'erase','xor','xdata',[x],'ydata',[sin(x)]);
for i=1:100
set(gal,'xdata',[x],'ydata',[sin(x+i/10)]);
drawnow;
end

:unisq.m &*.()&% – (x,y) &)1.*-')2% 0+ &*"+'%$ /)!*' 0" %/).& (11.5) -%('# "
.%*62.)3% 0" 102 *.&"$( "$&"$% *"/ 8*.&*. x,y 8*'1$'3% .%*62.)3( %')$" &*.()&%
function anisq(x,y)
clf;
xpoints=linspace(0,x,200);
ypoints=interp1([0 x],[0 y],xpoints);
set(gca,'box','on','xlim',[-10 110],'ylim',[-10 110]);
hold on
sq=line('color','b','linestyle','-','linewidth',1,...
'erase','xor','xdata',[-5 5 5 -5 -5],...
'ydata',[-5 -5 5 5 -5]);
title('to start animation press any key'),pause
for i=1:200
set(sq,'xdata',xpoints(i)+[-5 5 5 -5 -5],...

362-1-2323 Matlab !"#$


-5-
'ydata',ypoints(i)+[-5 -5 5 5 -5]);
drawnow;
end
hold off

.7'5% 0(0 !*!,$ 8*'*6 &('/$ – ‘box’ off

movie - !'#'1*2 ,'%., ."4 -.7%."3


5*6%0 4&*. %*%*" :( ,%6*'1$ :)&! 3-D )+ 2-D 5),$ &).)$& 0" %'-*, ')$"0 &'"3+$ MATLAB
.%*6$*.+ ')6*0) 76'! 4&)+
:&)-)23 3 -! ")$*" 0/ &,,)!$ )9 :'-! 8*1', &(*'/
M = moviein(n)
.&).)$& n - &'*$" &'"3+$% %0)-5 %6*'1$ '6)*
M(:,j) = getframe
.M %6*'1$! j - '3,$ %.)$&( (current figure) *#().% *3'5% :,$% &+ '$)"
movie(M,n,fps)
fps %.&"$% *"/ /!2. %.'2%% !62 .%*."% *'#+ &#+ M - %6*'1$! &)')$"% &).)$&% &+ 4*'2$
.8*$/3 n - 4'2)& &).)$&% &'-, .(%*."! &).)$& '3,$)

::( (movies &)-)23! ")$*" *"/) %*6$*.+ &*.()& %+'*& *00( 43)+!

M = moviein(n); !+"+*! n ,+$-./ M %&'()* (&+'


for j=1:n
plot_command
M %&'()*0 !+"+*! (*+1
M(:,j) = getframe
end
movie(M,n,fps) M %&'()*0 !+(+*1% !+"+*!% !. ,'(2*

'3,$! 2') :+ *)0& &'5,$% &6*'1$ 0-)5 .(&'5,$% &6*'1$) M %6*'1$! %-)$/( &'$". %.)$& 0(
,&#+ 8/3 &).)$&% &)56)$ 0-#$ &'*'!( .&).)$&% &)!(')$! *)0& ).*+ +)% ,&)')$"% &).)$&% 0-)5)
'"3+ .%0#&%0 7),%$ )+ 7),0 %0#&%$ 8*$/3 '3,$ %'-,% &+ 5*6%0 n '1$'3! ")$*" *"/ 4&.
!"#$% &)'*%$ *30 /!2* *"/$% %.'2%% !62 80)+ fps '1$'3% *"/ %.'2%% !62 &+ %/)!20
.8*.*$9% )*!+"$)

362-1-2323 Matlab !"#$


-6-
:4% %*6$*.+ %/)6*!0 )9 %1*" &).)',#
.%*6$*.+% /)6*! *.30 &).)$&% 0( &+ 5*6%0) ')6*0 "* !
(&'5,$% &6*'1$ &'*$" :')60 ")'- 4)'(*9! !' 8)2$) 8*!' !"#$ *!+"$ %(*'6$ %1*"% !

:&).)'&*
%6*'1$% &+ 4*'2%0 4&*. %9 :*0%& /6)!" *'#+ .&#+ 8/3 /6!0 "* &).)$&% &.*/1) !)"*# &+ !
.-!0! movie %-)23! ")$*" *"/ &/ 0(!

.8&)+ ')6*0 *+-( 8*!' 8*!)"*# 8*")'-" %0+( )+ &)!(')$ &).)$& 0" %*6$*.+ &(*'/ !

:anim2.m &*.()&% – (movie -! ")$*") 05 &/).& (11.6) -%('# "

clf;
x=linspace(0,8*pi,100);
y=sin(x);
plot(x,y,'b')
axis([0 8*pi -2 2])
M=moviein(10);
for j=1:10
y=(sin(x+j/10));
plot(x,y,'b')
axis([0 8*pi -2 2])
M(:,j)=getframe;
end
movie(M,2,5)

:anim3.m – (3-D) ;*32 0" %*6$*.+ (11.7) -%('# "

teta=0:pi/50:10*pi;
x=cos(teta);y=sin(teta);z=teta;
plot3(x,y,z)
lim=axis;
%keep constant axes
M=moviein(12);
for j=1:12
z=(1+0.5*sin((j-1)*2*pi/11))*teta;
plot3(x,y,z)
axis(lim)
M(:,j)=getframe;
end
title('to start animation press enter'),pause
movie(M,6,6)

362-1-2323 Matlab !"#$


-7-
Matlab-* 12-'! .03!
!""2. 1%1/

FACULTY OF ENGINEERING SCIENCES !"#$!! %&#'( !)(*+,!


DEP. OF BIOMEDICAL ENGINEERING .%/*,0*%1 !"#$!( !+(-'!

Matlab !"#$
6 '$% &'()!
!,#%%-!&! *+,-)(

!,#%% !&! *+,-)(


,#%,%-!&! ./)%. 0'+1 )',2 – plot3(x,y,z,’c’) !
-1 *'3 .%#''-.(. 1-0'1 .*'*+&* 5%02% 0*%6( ,plot !#*+,( .%#'' .(.! !(%1+'! /%! *4 !#*+,
8071 i - !#*+$ ,/'7*#() .1-0'1 .*#*+$ n 5%76%%'! ,n (#*71 5%0*)+** 5$! x,y,z ,.%#''-*#! plot
*//* &16! ./ &*1+( :.%$ .5%02% 5%**+1 .*#*+$! *01*-% 9"'! (& 76*%2 )(,1 .(x(i),y(i),z(i) - /%!
.c .4*0-' .,"*! %"& 5*+&! (2 :'%"!

:1-0'1 !(0%," )*)0%2 (6.1) *%('# "


» t=0:pi/50:10*pi;
» x=sin(t);
» y=cos(t);
» z=t;
» plot3(x,y,z,'r');
:(1+.'! )(,!

-1 -
D:\Tal\Matlab Intro course\Matlab - B 05\Class_6.DOC
z=f(x,y) *)'2*% !',2+"'- 0,(2,,%* 0,/34% 3'3)4 - mesh(x,y,z,c) !
(1+.' 9"'! (& 76*'! )(,! .x-y 0*2%'1 %$1(' 5*-. (&' z !)$%#0*/*+3 matlab -1 0#7*' -)2'
.5%02% 5%**+1 .*$32 .*#*+$ 0*1%- %"&
.n (#*71 0*)+** - y-* m (#*71 0*)+** - x ,n (& m (#*71 90&' -z
:.%$ . z(i, j) = f !x(i), y(j)" - 02+! %,( ,z -!1*71 .*#*+$ (2 .203 ))0*2' -)2'! mesh !#*+,1
02+! !%!% !4 !0+'1 ,z (2 (#*71 5%30&' 5! y -* x 02/3 57 !#*+,1 2'.2!(
. z(i, j) = f !x(i, j), y(i, j) "
&16 0'*(3 .!#*+$ (3( &16 5%/.'! c 8"*$ 90&' .0#7! %"& %&%10 #''3 2'2( (*3% -)2'! &16
. c(i, j) = g!x(i), y(j), z(i, j) " !'*+%' (2 !%6+$*, !%!% !#*+$!
.z !1*7! (2 !%6+$*,3 &16 &%,*% c 90&' /(( mesh .#*+,1
.%)%($/ !%6+$*, *0/.%2 93 x,y,z 5%30&'! ./ .*$1( .02,/' [xi,yi]=meshgrid(x,y) !#*+,!
.mesh !#*+,! %"& )*)02( *'%/.%* z = f(x, y)
x !0*2 %0*)+** m (%3'! xi 90&'! ./ !#*+,! !$*1 m (#*71 y-* n (#*71 x :)(+! %0*)+** 9'" (&
. y !#*'& %0*)+** n (%3'! yi 90&'! ./*
, 5%30&' (2 .*(*&,* .*%6+$*, .*&6'/1 z ./ .*$1( :.%$ n (& m (#*71 yi, xi 5%30&'! .%%$1 0-/(
. - z(i, j) = f !xi(i, j), yi(i, j) " 2 93

sin )' x 2 # y 2 &$


:z * ( % !%6+$*,! ./ 0/.'! -)2' )*)0%2 (6.2) *%('# "
2 2
x #y
» x=-8:0.5:8;
» y=x';
» [xi,yi]=meshgrid(x,y);
» r=sqrt(xi.^2+yi.^2)+eps;
» z=sin(r)./r;
» mesh(x,y,z,r)
:(1+.'! )(,!

362-1-2323 Matlab !"#$


-2-
.r !$.2'! %"& 76*%'! (x,y)=(0,0) !#*+$!' %+,*/! +-0'( %"-% 8071 &16!
.20! .*612' !$%%!. !4 !0+'1 9/ ,mesh !#*+,! *'3 -)2' .0%%6' surf(x,y,z,c) !#*+,!
:(1+.% .'#*+! /'7*#( 5%!4 (x,y,z,r) 5%$*.$ 0*1& ..*/('

» surf(x,y,z,r)

(3 !1 .*612' .203 ,.%,07 !0*61 5%0,"' .6%0)' 2%-'!( .02,/' pcolor(x,y,z) !#*+,!
!2&'( /*! (1+.'! -)2'! .01%/! 90&1 %*(. .612'! &16 .!6%0)'1 01%/ .76%%' .612'
5%"+#$%/! %, (& !6%0)'! (2 !$*'. (1+.. x,y 5%0*)+**! :*%6 /(( .!(&'(' )1'1 surf(x,y,z)
.,"*! %"& &16 .(+" (1+( :.%$ .(!)'( (/'2 #61 .%2/0! 02/3) !6%0)'1 01%/ (3( 5%'%/.'!
.colorbar !#*+,!

(6.3) *%('# "


» [xi,yi]=meshgrid(-1:0.05:1);
» zi=sqrt(xi.^2+yi.^2);
» pcolor(zi) ;
» colorbar;
:)(,! (1+.%

362-1-2323 Matlab !"#$


-3-
9'" (& .%#''-*# !$*'. .06*% !#*+,! .magesc(z,[cmin cmax]) /%! !'*# !(*&, .&61'! !#*+,
50*1&2 5%0,"'! 5*-. /*! [cmin cmax] -* 90&'! /*! z 02/3 .&16 5%76%%'! 5%0,"' 90&'
.&16 %(#1! *(1+.%
:1 -( 0 :%1 5%%/0+/ 5%0,"' .6%0)' (2 .%,07 !76! (6.4) *%('# "
» imagesc(rand(30,30),[0 1]);
» colorbar

362-1-2323 Matlab !"#$


-4-
(*,-)('-'3) *.'( ,'4 0,''+ !'12%5. /34% )',2 - contour(x,y,z,n) !
0(+" n ,mesh 1 *'3 x,y,z .(%#''-.(. -)2' .0/.'!) .%#''-*# !1*7 %**+ .,' .))02' !#*+,!
.5%!17! ./ (%3'! 0*)+**3 n .0#7! %"& 5%'%*"' 5%!17 :*%6 ;(/( :.%$ .!1*7! %*+ 0,"' ./ :%%6'2
.contour3(x,y,z,n) !#*+,1 2*'%2 %"& (1+.. .%#'' .(. !1*7 %**+ .,'

(6.5) *%('# "


» x=-15:1:15; y=x';
» [xi,yi]=meshgrid(x,y);
» r=sqrt(xi.^2+yi.^2)+eps;
» z=sin(r)./r;
» figure(1)
» contour(x,y,z,8);
» xlabel('x'); ylabel('y');
» figure(2)
» contour3(x,y,z,8);
» xlabel('x'); ylabel('y'); zlabel('z');

362-1-2323 Matlab !"#$


-5-
15

1.5 10

1 5

0.5
z

y
0
-5
-0.5

10 -10
15
10
0 5
0
-10 -5
-10 -15
y -15 -15 -10 -5 0 5 10 15
x
x

figure 2 figure 1

362-1-2323 Matlab !"#$


-6-
:!/1! !0*61 clabel !#*+,! .,"*! %#%-(& !1*7! 90& :*%6 ./ 57 5%((*3! ,!1*7 %**+ (1+( :.%$
» figure(1);
» [c,h]=contour(x,y,z,8);
» b=clabel(c,h);

c !6%0)'! .h 0*)+** c !6%0)' 57 (1+.. x,y,z .*)$%#0*+! %,( !1*7! %**+ 0*%6( 8"*$1 ,!4 !0+'1
:%%6' !6%0)' .. (31 :*%(& %(/'2! 01%/! .!1*7 %**+ .*0/.'! .*6%0)' -..' !%*$1 .*0*2 2 .(&1
92'!1 ,!1*7! *+ ./ .*06*%! .*#*+$! 0,"' ./ :%%6' :*.-. %(/'2! 01%/! ,%,%6,"! *+! !1*7 ./
. x,y .*)$%#0*/+! .*/6'$ !%$2!* !$*2/0! !0*2!
%*$%2* !1*7! *+ %*!%4 90*6( ,(handle) !1*7 *+ (3( 0,"' 9%%2'2 n (#*71 !#*'& 0*)+** *$%! h
.('*3* 5*+%' ,!0*6 ,&16) *%.*$*3.
.$%%6'! .%**. 80*6. 5!' #-/ (3(* 5%'%/.'! !1*7! %**+ *0.*/% clabel(c,h) !#*+,! .*&6'/1
.!1*7! 90& ./
.)$*,! (#*7 %*$%2 /'7*#( ,.*$*3.! %*$%2 ./ 02,/' b 0*)+**( )"+)! .*%**. 9*%2
» z=peaks(30); % produces an 30-by-30 matrix
» [c,h]=contour(z,10);
» b=clabel(c,h);
» set(b,'fontsize',7); % change the font size to 7

362-1-2323 Matlab !"#$


-7-
)'#6 )',2 - [x,y,z]=sphere(n) !
(#*71 0*#3 0%%6. surf(x,y,z) !#*+,!2 93 (n+1)*(n+1) (#*71 x,y,z .*6%0)' !$*1 sphere !%6+$*,!
.90*6( 5/.!1 x,y,z ./ .*$2( 2% *"*%#0 .(#7!* 0*#3! .44!( ..%2/01 *430'2 !#%-%
.sphere(n) .*&6'/1 %02,/ !#%-% 0*#3 (2 %#%%' 0*%6

(6.6) *%('# "


» sphere(20)
» [x,y,z]=sphere(20);
» x=x.*1.5+1;y=y.*1.5+4;z=z.*1.5+1
» axis('equal'), hold, surf(x,y,z);

!',&,&( !'3",#)'5'+. .'.,$ ,-'( )',2


R * z (2 5%0*)+** !(%-. !$1$ z=[a,b] 5*-.1 R=f(z) !%6+$*,! (2 z -! 0%6 1%1" 1*1%"! 8*7 0*%6(
.5*-.! %, (&
02/3 1*1%"! 8*7 0*/%.( .*%4)0+! .*6%0)'! ./ (1+$ [x,y,z’]=cylinder(R,n) !%6+$*,! .*&6'/1
.[0,1] 5*-.1 z’
(#*7 .( z’=(z-a)/(b-a) ) z=z’*(b-a)+a) !%6'0*,"$0) &61$ %0*+'! 5*-.1 1*1%"! 8*7 0*%6(
. (length(R),n+1) !%!% x,y,z .*6%0)'!

(6.7) *%('# "


a=3;b=10;
z=3:0.1:10;
r=(0.5+sin(z).^2).*z.^2;

362-1-2323 Matlab !"#$


-8-
[x,y,zl]=cylinder(r,20);
zm=zl*(b-a)+a;
surf(x,y,zm)
title(' r=0.5+z^2*sin(z)^2 (2 1*1%"! 8*7 ')
»rotbd

,#%,% !&! 3&-. (view point) 3.%* 7'',6 ,'",4

z
y

Elevation(theta)
x

Azimuth(phi)

:5%30# %.21 %02,/ )1'! :**%3 %*$%2


/*! %(/'2! 01%/! ,5%01%/ %$2 (&1 0*)+** /*! )(+! 02/3 ,view([phi,theta]) !#*+,1 2*'%2 .1
.*%**4!) xy-! 0*2%'( .%"-% (elevation) !!17!! .%*4 %$'%!* z 0%6 1%1" (Azimuth) 1*1%"! .%*4
.(.*(&'1
:**%32 93 .%2/0!' /6*%! %#'' .(. 0*)+** /*! )(+! 02/3 ,view([x,y,z]) !#*+,1 2*'%2 .2
.*$**%3( +0 /(/ 0*)+**! (#*7( .*&'2' :%/ ,.%2/0! :**%3( 0*)+**! !6+' /*! )1'!
:proj.m .%$3*.! ,(1*1%"! 8*7) 5#*+! #*'&1 0*%6! 0*1& (6.8) *%('# "

subplot(2,2,1)
mesh(x,y,zm);view([0 1 0]);title('front');
subplot(2,2,2)
mesh(x,y,zm);view([1 1 -1]);title('down isometric ');

362-1-2323 Matlab !"#$


-9-
subplot(2,2,3)
mesh(x,y,zm);view([0 0 1]);title('top');
subplot(2,2,4)
mesh(x,y,zm);view([1 1 1]);title('up isometric ');
»proj

:%#'%' .(. 0*%61 )1'! :**%3 (2 (#-'! .0%01 %30&


o
Azimuth(phi)=-37.5
Elevation(theta)=30o
.rotate3d on !#*+,! %#%-(& !2&% (matlab5 1) 013&! .*&6'/1 %$#% :,*/1 )1'! :**%3 %*$%2
.16'( 16'' 01*& rotate3d .!4 16'' /6*% rotate3d off
.%*4 ,!(2 1*1%"! .%*4 ,!'(6'! 5*+%'1 (*,%) .04&1 )1'! .*$*3. ./ .*$2( :.%$ Matlab- 5 1
!#*+,! .*&6'/1 !$&61.. .*0#7!! .!$*'.! 430' .#*+$* !-%.,!
.set(gca,’property’,N)
.%-3*$! axes ( !0#7!! ./ .3%%2' gca !#*+,!

:!'.'4/ !'"'6!
. x,y,z !#*+$1 !'(6'! ./ 5+'. set(gca,’cameraposition’,[x,y,z]) !#*+,!
!&%,*. !#*+$!2 93 ,x,y,z !#*+$( !'(6'! ./ :**3. set(gca,’cameratarget’,[x,y,z]) !#*+,!
.)1'! :**%3 ./ .*#*+$! 2 .*0%#7' !'(6'! 5*+%' .#*+$ 5& #-%1 .!$*'.! 430'1
93 ,)1'! :**%3 0*)+** 1%1" !'(6'! ./ 11*". set(gca,’cameraupvector’,[nx,ny,nz]) !#*+,!
. !(&' &%16% ,)1'! :**%3 0*)+**( 9$*/'! 0*2%'! %17 (& [nx,ny,nz] 0*)+**! (2 *()%!2

362-1-2323 Matlab !"#$


- 10 -
(33 .!'(6'! (2 (.*(&'1) deg !-%.,! .%**4 ./ 0%#7. set(gca,’cameraviewangle’,deg) !#*+,!
.9,!!* :)+ )+%%1*/!2 93 (#7 %*"%3! -)2 !(#7 .%*4!2

:camer.m .%$3*.! (6.9) *%('# "


[x,y,z]=sphere(20);
theta=45;
theta=pi*theta/180;
cp=[2 2 2];
ct=[0 0 0];
n=ct-cp;
b=cross([0 0 1],n);
zt=cross(n,b);
vup=zt.*cos(theta)+b.*sin(theta);%create cameravectorup with 45 degree rotation

vup=2*[vup]/norm(vup);
surf(x,y,z);%draws the sphere ;
axis('equal');
set(gca,'cameraposition',cp);
set(gca,'cameratarget',ct);
set(gca,'cameraupvector',vup);
set(gca,'cameraviewangle',50)
hold on;
plot3([0 vup(1)],[0 vup(2)],[0 vup(3)],'b');
»camer

362-1-2323 Matlab !"#$


- 11 -
Matlab-) !"#$% &'(%
%","& !*!+

FACULTY OF ENGINEERING SCIENCES %,-.%% */-$0 %10)23%


DEP. OF BIOMEDICAL ENGINEERING &*+)3')*! %,-.%0 %20#$%

Matlab !"#$
8 '$% &'()!
Cell arrays *+,! +-).%
.MATLAB -! 5**2% %.&"$ 0" 6), 0( 0*(%0 0)(*% ,+& +)% )! 1.$0+ 0( '"+ 4'/$ +)% 5*+& 4'/$
.!()'$ '3,$ 8,). +&) &9)'#$ '#+ +& ,%7*'1$ 0*(%0 0)(* 5*+& 4'/$ 4)&! -#+ +& ,+$6)-0
:5*+& 4'/$ 0" *'"3+ %.!$ ,%$6)-0
cell(1,1) cell(1,2) cell(1,3)
[3 4 2 [‘Anne smith’ [0.25+3j 8 - 16j
9 7 6 ‘ 9 / 12 / 94‘] 34 + 5j 7 + 92j]
8 5 1]
cell(2,1) cell(2,2) cell(2,3)
[3 4 [‘text’ [4 2

[1.43 2.98 5.76] 7 6] 1 5]


[4 2 7] [0.02+8j]

: 5*('- *&"! &*'"3+ 5*+& 4'/$ &**.!


.{} 5*0,0),$ 5**'6),) %$"% *13"$! ")$*" .1
:%0/$0 67)$% 4'/$% &**.! (12.1) /%('# !

» A(1,1)={[3 4 2;9 7 6;8 5 1]};


» A(1,2)={char('Anne smith','9/12/94')};
» A(1,3)={[0.25 8;34 7]+i*[3 -16;5 92]};
» A(2,1)={[1.43 2.98 5.67]};
» A(2,2)={[3 4;7 6]};
» A(2,3)={{['text'],[4 2 ;1 5];[4 2 7],[0.02+8i]}};
» A
A =
[3x3 double] [2x10 char ] [2x2 double]
[1x3 double] [2x2 double] {2x2 cell }

-1 -
D:\Tal\MATLAB COURSES\Matlab - B 05\class_8.doc
:($ '#+0) A=cell(m,n) – %-)23% &)/7$+! n -0/ m -0-)6! A 5*+&% 4'/$ 0" &$-2)$ %**.! .2
- (i,j) +&! ')12)) 5)2*$ ,%$6)-0 .5*0,0),$ 5**'6),! 5$)2*$ :)*7 *-*-0/ 5*+&! 5*('/ &$"%
.A{i,j}=[1 2 3 ]
. &*#'(% %.*+ 4+ &*.()&% 0" %7*'% :$9 &+ &'72$ &$-2)$% %**.!% "
.celldisp(A) - %-)23! ")$*" *"/ &*'"3+ A- 5*+& 4'/$ 0" )&0)(& &67%
(12.2) /%('# !
» celldisp(A)
A{1,1} =
3 4 2
9 7 6
8 5 1
A{2,1} =
1.4300 2.9800 5.6700
A{1,2} =
Anne smith
9/12/94
A{2,2} =
3 4
7 6
A{1,3} =
0.2500+ 3.0000i 8.0000-16.0000i
34.0000+ 5.0000i 7.0000+92.0000i
A{2,3}{1,1} =
text
A{2,3}{2,1} =
4 2 7
A{2,3}{1,2} =
4 2
1 5
A{2,3}{2,2} =
0.0200+ 8.0000i

362-1-2323 Matlab !"#$


-2-
.cellplot(A) %-)23! ")$*" *"/ &*'"3+ A -5*+&% 4'/$ 0" &*3'6 %67%
» cellplot(A)

362-1-2323 Matlab !"#$


-3-
*+$0#"+,1 2'%+2
.5*0,0),$ 5**'6),! )$)2*$ :)*7 *"/ /7)!& 5*),$ +&0 %+*'2
(12.3) /%('# !

» name=A{1,2}
name =
Anne smith
9/12/94

5**'6),! +&% 5)2*$ :)*7 *"/ /7!&& ,5*),$ +& 4)&! +7$.% ,4'/$ 4)&$ 5*),$ 4'/0 %+*'2
.5*0*6' 5*'6),! ,4'/$! 4'/% 5)2*$ ,4"$%! 5.*$*$) .5*0,0),$
(12.4) /%('# !
» A{1,1}(2,2)
ans =
7
» A{2,3}{1,2}(2,2)
ans =
5

+& 0" 5)2*$ 67** %')"! '!*+ 0(" 4( ,5*,2-.*+ 0" %')" :)*7 4')70 5**&-)2.! "$&"%0 :&*.
.(5%*.*! 5*2*,3 5/ 5*,2-.*+% &+ %0*($% %')"0 0)2" '!-%)
(12.5) /%('# !
» b{1,1}=[1 2 3];
» b{1,2}=[2 9 4];
» b{2,1}=[5 6 7];
» b{2,2}=[8 0 2];
» b
b =
[1x3 double] [1x3 double]
[1x3 double] [1x3 double]
» b{1:2,2}
ans =
2 9 4
ans =
8 0 2
» cross(b{1:2,2})
ans =
18 28 -72

362-1-2323 Matlab !"#$


-4-
Strings !'3')4%

-% 4'/ &)/7$+! matlab -! 67)*$ character 0( .(characters) &)*&)+ 4'/$ %.*% (string) &9)'#$

.(char array) &)*&)+ 4'/$ ).*% 5**"'6! /*3)$% *)1*! .)0" ASCII

(12.6) /%('# !
» name='thomas r. lee'
name =
thomas r. lee
» whos
Name Size Bytes Class
name 1x13 26 char array
Grand total is 13 elements using 26 bytes

.double %*72.)3% &"$"$ &9)'#$! &)+ 0( 0" ASCII-% *('/ &0!20

(12.7) /%('# !
» as=double(name)
as =
Columns 1 through 12
116 104 111 109 97 115 32 114 46 32 108 101
Column 13
101

.char %-)23% &"$"$ ASCII-% *('/ 4$, 0/ &9)'#$ &0!20

(12.8) /%('# !
» name=char(as)
name =
thomas r. lee

.5*))& '3,$ )&)+! &)')" 0!20 *-( 5*#))' 8*,)%0 "* &)')" %$($ *).!% ,&)9)'#$ 4'/$ &**.! 4')70

4')+0 &)9)'#$% 0( &+ %!*#'$ '"+ char %-)23% &)/7$+! '&)* %'*%$ %')7! &+9 &)"/0 :&*.

.5*#))' &3,)% &)/7$+! '&)*! %()'+% &9)'#$%

362-1-2323 Matlab !"#$


-5-
(12.9) /%('#
» nm=char(' name ','Thomas R.lee','senior developer')
nm =
name
Thomas R.lee
senior developer

. &9)'#$% 8),! 5*'&)*$% 5*#))'% &+ %1*$"$ deblank %-)23%

(12.10) /%('# !
» nmk=deblank(nm(1,:))
nmk =
name
» size(nmk)
ans =
1 5

'3,$$ 0)-6 4'/$ %.!. )!" :3)+ )&)+! /7!&$ , (5*)& 4'/$) &#+ &9)'#$0 &)9)'#$ '3,$ 0" 8)'*7

.5*('/$

(12.11) /%('# !
» fun1='sin(x)';
» fun2='cos(x)';
» fun3=['(',fun1,'+',fun2,')/2'];
» hold on;
» fplot(fun1,[0 2*pi],'r');
» fplot(fun2,[0 2*pi],'g');
» fplot(fun3,[0 2*pi],'m');

362-1-2323 Matlab !"#$


-6-
*." 4)&! -#+ string &+*7$
.%*."% 4)&! &#+ &9)'#$ 5)2*$ &+7)$ findstr %-)23%

(12.12) /%('# !
» s = 'How much wood would a woodchuck chuck?';
findstr(s,'a') returns 21
findstr('a',s) returns 21
findstr(s,'wood') returns [10 23]
findstr(s,'Wood') returns []
findstr(s,' ') returns [4 9 14 20 22 32]

.&)9)'#$ *&" :*! %))"$ strcmp %-)23%

(12.13) /%('# !
>> strcmp('fd', 'fda')

ans =

362-1-2323 Matlab !"#$


-7-
(!)'-3!) 5%3 !#'06
:$9% &67% .CPU TIME - !"#$% 0" -!/$% :$9! ")$*" 0/ &),,)!$ MATLAB -! :$9% &-)23
:&)')7 '3,$! &*'"3+
[ /"2 2#'4 2#'41-*'+ !'.2 !'0# !'+"2 ] - )'70'' .1
(12.14) /%('# !

» T=fix(clock)
T =
1998 1
25 19
32 16

.(&*.)'"/ %-)2. +00) 5*$0" 5*'3,$ &0!20 &"$"$ fix %-)23%


&+ '$)" MATLAB %! 4'-% *%)9 ,(5*'7).% &'*3, &0*#&) $6, !"2 3,% '6&42 *+%+/ )6$% .2
.:$9 *!)"*# /)7*!0 )&.*#!$ %#).% 4'-%) :$9%
(12.15) /%('# !

» t=now
t =
7.2978e+005

!3')4%- 5%3/ !(8/ .3


:dd-mmm-yyyy 1$')3! 4*'+&% &+ %6*7$ date %-)23% (12.16) /%('# !

» date
ans =
25-Jan-1998

362-1-2323 Matlab !"#$


-8-
&)+7)& 6*7%0 5*7)' '"+( .-!0! ')12)) )+ '3,$ *"/ 5*67)*$% 5*.$9! &*'"3+ :$9 *!)"*# /)7*!
*30 &9)'#$0 ,'3,$ *"/ 67)*$% :$9 &(3)% %-)23% - datestr %-)23! "$&"%0 *)7' %+*'2 %')7! :$9
.%+!% %0!1! 5*67)$% 5*1$')3% -#+
Example
DATEFORM DATEFORM string
number
0 'dd-mmm-yyyy 01-Mar-1995
HH:MM:SS' 15:45:17
1 'dd-mmm-yyyy' 01-Mar-1995
2 'mm/dd/yy' 03/01/95
3 'mmm' Mar
4 'm' M
5 'mm' 3
6 'mm/dd' 03/01
7 'dd' 1
8 'ddd' Wed
9 'd' W
10 'yyyy' 1995
11 'yy' 95
12 'mmmyy' Mar95
13 'HH:MM:SS' 15:45:17
14 'HH:MM:SS PM' 3:45:17 PM
15 'HH:MM' 15:45
16 'HH:MM PM' 3:45 PM
17 'QQ-YY' Q1-96
18 'QQ' Q1

362-1-2323 Matlab !"#$


-9-
(12.17) /%('# !
» t=now
t =
7.2978e+005

» datestr(t)
ans =
25-Jan-1998 19:56:04
» datestr(t,14)
ans =
7:56:04 PM

::*)7" 5)*% &+ 00)(% -#+ "-)# - :)(*'+& &67% (12.18) /%('# !
» calendar('1/26/98')
Jan 1998
S M Tu W Th F S
0 0 0 0 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
0 0 0 0 0 0 0

0 0 0 0 0 0 0

(5*!)"*# %/)7*! '"3+$) %7*'1$! "-)#% :),#+ (12.19) /%('# !


» c=calendar('1/26/98')
c =
0 0 0 0 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
0 0 0 0 0 0 0 0

362-1-2323 Matlab !"#$


- 10 -
)8. 5'.2 !'+80"'6
5'.2/ !&.6/ - tic #
.5'.2/ !&.6/ 3,% )1.2 5%3/ !,+)0 - toc #
:MATLAB -% *"/ 8'6 1)1'*" :$9 !)"*# (12.20) /%('# !
» tic; plot(rand(5)) ;toc
elapsed_time =
2.5300
» tic; plot(rand(5)) ;toc
elapsed_time =
0.5500

")'-% :$9% :(0) ,%.)($ &3"! !"#$% :)'(*9! %')$" %*72.)3% plot %-)23! :)"+'% ")$*"% *'#+
.%!'%! :12 %*."% 5/3! 8'6 1)1'"0

+6)( 5%3-)+8-1 2'%+2


'!$7-) 94 - '!$!). :5*(*'+&% :*! %'!# 0" 5**"-)# &)'*($ *')9#$ '+&$% 8'6 (12.21) /%('# !
:95 –
» s=[11 13 12 14 16 15 16 17 18 13 19 17 16 19]';
» y=[1994 1994 1995*ones(1,12)]';
» m=[11 12 (1:12)]';
» bar(datenum(y,m,1),s)
» datetick('x','mmmyy')
» ylabel('$ Million')
» title('Monthly Sales')

M o n th ly S a le s
20

18

16

14

12
$ Million

10

0
O c t9 4 Ja n 9 5 Ap r95 Ju l9 5 O c t9 5 Ja n 9 6

362-1-2323 Matlab !"#$


- 11 -

Potrebbero piacerti anche