Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
"#$ %$&"%%
'&!$
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
.&%1%1! .&+&20
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'
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%#+ )%$+/+
.'/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 . . .
.!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# !"#! !,+
(/ #,27 !23+ %"! 2%,3% .#*++-%* :2$+ #*#-($ )6&%#+ !,+ .!,+ #*#-($ !/$+( 6&%#+
.)#*++-*0 !,+
($ 2%90( #('+ ,.#2'#5 (/ !(%*6 !&%'3 ($ )%#,+)+ )%(%$7 $%&#' 2/75+ )%,+' /%+#/
.2'#5 (- 2%'$ !(%$7!
8+%1# )%2%/ 8#' 2'$+ .[ . . . ] .#$'%2+ .##26%1' /%+#/ #"$ !/$) MATLAB-' !,+ )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
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- ,!,+! ./ 8%#& #"$ !/$) !,+ :%)' .#%1+ 2'#5( !#"7
)5 – #"/! 2'#5!% !2%/! 271+ )5 8##&+ 8%/52! 271+! .2'#5 %)%5 (/ 13*"#5!% ,!,+!
'%)-" .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
» 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
.*%+&( %-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*') )%,+ (5-
..#+5%) .#2'#5 )%6%9 ($ )$&%'+
,$4&/+ !
» a=[1 2];b=[3 4];
» c=a.*b
c =
3 8
#*++ '##0+% )#25#"#( !2'6(5' %+- )%,+ )(7-! 6&##+ !*%3" 5(( (7- 8+#1' /%+#/
:.#+5%) )%,+
» c=a*b'
c =
11
8
,)%35$+ 3+*1 6%! .&+&20
:!,+! #2'5+ *05 (- ($ !!9 !(%$7 $&%') !,+% 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
9
:)%,+ 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*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!
» 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
11
)5 27%1 MATLAB-! .*05 13*"#5' /%+#/ #"$ #*++-%* :2$+( :##/! 2'#5 (5 )%"7( 2/75
.!,+( !($++ ,)%*%+$! 2*1 #7( ,!,+! #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'#5( !#"7! 2/5-
.#5")! )5
/%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%+! !,+ !"'" )+*%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
,$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
,$4&/+ !
:!*#0# ),+ )%"'( #*- 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 )%,+ .$ *%'$( )%*$%#+! )#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%*
» 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
(%!7&3$ (%301$
)%,+ .! 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
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
.&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)
)%&%7"! )%#$'!+ )05 .)#25#"#( !2'6(5% )%,+ #'%/#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%&'%
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- ,)%,+ )(7-+ )6&##+ !9 !23+' (7-! )(%$7
.(" .* " 8+#1' MATLAB -' )"+%1+! .#-2$+
5#! A !,+! .5 *#0# 8%2)7 .##3 . x = A ( b #%,#'! #"$ 8%)" )%5%%/+! )-2$+( 8%2)7!
-1
.!-#7! !,+
.()#2(%6"#1 5( !,+) !-#7! A !,+! .5! MATLAB -! )29$' 3%*'"
19
A ,)%35$ +" ,5--%$35/ - det(A)
.!-#7! !,+ 5#! 175+ !"%/ !,""#+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
» x=inv(A)*b
x =
25.0000
22.0000
99.0000
!)%$+/+% )#25#"#( !2'6(5' )+##3 !"#5 %9- !(%$7 ,)%,+ )3%(0 )((%- !#"/! :2*! .2
.#-7%!' (7- 5#! MATLAB -'
:)#"+# !3%(0( )#(5+/ !3%(0 8#' )%,+ )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$
» 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 !,+( .#!9 .#*++ )($' !,+ 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 !�+' )%2*6%+ )%#1#1'! )%#&3"%7! .)%,+ (/ )%*%+$' %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 !,+! )%*%+$ (/ %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
» avg_t=mean(avg_temps)
avg_t =
24.2473
» [min_temps,I]=min(temps)
min_temps =
5 16 25
I =
2 20 25
24
: temps (/ #('%(6! .%+#13+/.%+#"#+! '%/#0( )%#2/75 .#-2* #)/ ,$4&/
:2$+ .#",3 %5 .#(%*6! .#2'#5 :2$+ :%)+ )%7"( 5%! min/max -! )%*%37 (/ 41%" /%+#/
.)%,+ #)/+ 2)%#' .#",3/.#(%*6 .#+#5)+ .#2'#5+ !,+ )%"'( 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
.!*#05 !2%&' .#6(%7+% , 1 -( 0 8#' .#271+ .! %#2'#5/ :- ,m 3 n (*%6' #5235 :2$+ !#!# R
$&%++ .$ ()#(+2%" )%6(7)!) )#1%56 .#6(%7+ %#2'#5/ :- M 3 N (*%6' #5235 :2$+ !#!# Rn
.1 83) )##,1% 0
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%,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'-$! ($ !�( #"$ (%,#' ,)#"+9 !317! - waitforbuttonpress
27
.!(%$7 $%&#' $&+5' MATLAB -! )5 !3#17+ ctrl-c ($ !�(
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%+
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%/ .!+(/ !�+( (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(
#%&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! )�+' )%#"-%)+ 32 8!#(5 )/6( 8)#" .private ./' )*0%#+ !�+ )0) )%"-%/! )%#&3"%7
/*0+ 2#*6!( 8)#" ,'5! )�+( ;%0+ )%/#6" 8"#5 )%#,27 )%#&3"%7/ 8%%#-+ .(parent directory)
.)%205 )%�++ )%#&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 ,)%,+ 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$+ ($ )%#,+)+ )%(%$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! !,+' .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'! )#"-%)!
.!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
,.#-2$+ ($ )%(%$7 $&'( MATLAB -! (/ )(%-#! )5 )%(&"+ 8!/ .%/+ .&)+$&$ ,+' .&3&)
.!5(%(! (/ )#10# :%25! !! 8+9 )5 )%-1%0 8-(%
) #*- :%) .#"'"! ,.#-2$+ (/ /52+ !2*6! 5#! 8+9' 8%-1#0 :2%&( )&(+%+! )71%" !(%$7
.(#(+#13+) #7%1! .(*%6' .#175 #-2$+- !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 -')
!+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’)
,$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%% )%,+ ($ )%(%$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
tic
t=0:0.01:10;
y=sin(t);
toc
39
.%/$$ -&/ ,*%034
.#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)#"
.'%!&' x3 *6"- y3 % (%0-' x2 *6"- y2 ,.%*5' x1 *6"- y1 :)%+%3$ /%(/ ,,2/) (""! !*%37!
(%"3. !&)%2
x 3%) .3.&7 .3/4, – xlabel(‘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
#)('!% #%()! - .#")/+! (/ .%+#"#+!% .%+#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)')
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)
.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
#"+#! ($ !�(% 426! )5 (#*6) 2'-$! (/ #(5+/! 2%)7-! ($ !�( .()",3%+ %5 )(*6%+!
.2'-$! (/ !2#26% !�( #"$ #"'(+ .%0) $%'3( .6 2/75 .%)%5 8#,3)
.zoom off /#3!( /# zoom on (%,#'(
'1+ 8%#& 5(( .y-% x .#")/+' %2+/# 2'-$! )%$&+5' 426! ($ %"+%1#/ )%*%3" n (/ )%,"#*2%3!
.ENTER /3+! ($ !�( *$ )%*%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!
» fill(x,y,'g')
:('3)+! ,(7!
45
(%034 %4&1
%3+&0 ;34 5&53" – polar(theta,rho,s)
:('3)+! ,(7!
46
(%&1$ (&#.! ,%)*-&0 3&%) – fplot(‘fun_name’,[xmin amax])
,$4&/
:('3)+! ,(7!
,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') #
,$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%'$ ,!!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!
%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 %+- .#� 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!
: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+ ),+ /#0+!( )2/75+ pcolor(x,y,z) !*%37!
!/$+( 5%! ('3)+! 0,/+! .2'#5! :2$' #%() )&'/+! $'& .!,+' 2'#5 )6&##+ )&'/+
#7 ($ !,+! (/ !"%+) ('3)) x,y .#2%,3%%! 8%#& 5(( .!($+(+ ,'+' surf(x,y,z)
.(!,+( (5+/ *&' )#/52! 2/5-) !,+' 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+ ),+ (/ )#726 !6&! ,$4&/
» imagesc(rand(30,30),[0 1]);
» colorbar
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 !,+ .6 ('3)) x,y,z )%,"#*2%3! #7( !'%6! #%%3 2%#&( 41%"' ,!9 !23+'
(-' 8%#($ #(5+/! 2'#5! .!'%6 #%%3 )%25)+! )%,+ -))+ !#%"' )%2%/ 2 )($' c !,+!
)%2&%#! )%*%3"! 271+ )5 8##&+ 8%)0) #(5+/! 2'#5! ,#7#&71! %3! !'%6 )5 8##&+ !,+ ))
. 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 )%,+ !"%' 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! )%,+! )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 )%,+!
,$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)
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%
» 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
» polyder([1 2 3 4])
ans =
3 4 3
62
a ,/&*-! (&-%+&0, 932 !&"%# – Y=polyval(P,a)
.%"#(%7! :2$ !#!# !' 2'#5 (-/ :- ,!!9 (*%6' Y !,+ ('3)) n 3 m (*%6' !,+ 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!
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
» [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+!+
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" '%/#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)
:)#"-%)! )&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
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
:)#"-%)! )&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 =
-0.1148 -0.6231
(%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&/
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
:-1 < x < 2 .%0)' humps !#&3"%7( )0)+ 0,/! '%/#0 ,$4&/
.&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
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
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)')
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, %!+"
(/ 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") ('
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)]
,%)*-&0, 67&.
Function vdot=rc(t,v)
vdot * % a ! v % b
v" * % av % b
79
:.! ode45 .%)*-&0 +" (%5+*,
z (1) * x z (2) * `x
(&7%1
80
Cell arrays (%'. %732$
-' .##3! !")/+ (/ 6%1 (- (#-!( (%-#! ,5) 5%! %' ,"+(5 (- 2/5 :2$+ 5%! .#5) :2$+
41%" 5)% )9%20+ 205 5) ,!,+ (#-!( (%-# .#5) :2$+ :%)' *05 5) ,5+6%*( .MATLAB
.'-%2+ 271+
:.#5) :2$+ (/ #2/75 !"'+ ,!+6%*(
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 )#"-%)! (/ !! 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
$%& $!'
Nir Adar
Email: underwar@hotmail.com
Home Page: http://underwar.livedns.co.il
-1-
underwar@hotmail.com !"# !$%
(!'!!') *+,- .1
4 .............................................................................................................................................. #*,- .2
14 ......................................................................................................................................... 14$3!0 .5
-2-
underwar@hotmail.com !"# !$%
16 .......................................................................................................................................+*$.4%*3 .7
-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 ,"'"'#
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*.#%
-4-
underwar@hotmail.com !"# !$%
.2'-3$ ,#! -# 2%*$ "# $#+/%' command window-$ 2'-3 "# $45% '( $+:45'1
.6*'5 '" -.% 8-7"% Matlab !,#. ,$5",% 8, -.- 8+''" 31 &7 8+:4'%
"'-'&9 "'+"'# 2+/ $-+&/% Matlab !,#. ,2'"3" '4' "'+"'# ,8+!1*%% /.!'% "'+$- -'.+ $5",% 8,
."'5;4-
-5-
underwar@hotmail.com !"# !$%
-,3!$7. -$%4/ .3
:"'-'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!*# +#$.- !
-6-
underwar@hotmail.com !"# !$%
)!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# !
)# ,!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'&-
-7-
underwar@hotmail.com !"# !$%
-,3!$7. 1) -,!31,5!'. .4
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%$$)
-8-
underwar@hotmail.com !"# !$%
Transpose .4.2.1
:#%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
.$-, $;55+%!;&$ "# !+(3" det(A) $&'41$ +(# ,"+7'/+! $:+!;% A +$"
-9-
underwar@hotmail.com !"# !$%
:$-, "+.1'$$ $:+!;%$ "# #':%- 8+.!& 3 25,+ . n ' n "+7'/+! $:+!;% A +$"
inv(A) .1
A^(-1) .2
eye(n)/A .3
"# $!+(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
.!/;:%$ '+!/+# 8'.* #" -./, )!'# '"'#/ !';4'' "# $!+(3%' !';4'' "-/4% $+:45'1$
.!/;:%$ "'&'%7$ 8'.* ":+!;% "# $!+(3%' $:+!;% "-/4% #+$ 2+1'-+3-
-10-
underwar@hotmail.com !"# !$%
"# $!+(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
.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
-11-
underwar@hotmail.com !"# !$%
'"'# "# 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+#"
-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
-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
:$#:'"$
-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
:!+/3" .$- 2"+5$ +#5"$ 8++4"% &'7 -. ,8+%71 !1*% &'4 7;4 -7 !'(3- "!,1#% while "#-'-
while expression
statements
end
:'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
.$+:45'1$ 8, $%,. '%,, m 0/'4/ $"'# !'%,- '5+-7 $+:45'1 8+!:'+ '535# !,#.
:#%9'&
-16-
Matlab-! "#$%
&"%
'!( )*#+
!"#$"%&'& (&$'
,'.,# &#)%+, ,2+)% +#$)2 &*/("%, 3)-+0,%# 3)40+#4-! &%+5&% & &(/ "), Matlab &(/
.&#".#& 6#&)0! &+2#)%, ,$)$-$# +#5 &#*#/$ 4#2)% .%"$ &0##'% &)L
,3, &#*6" &#(/# Matlab 8)$ 3))71*%, 3)!+$,,
"#, Matlab 3/, *#5% ,,/2%! .&#.)*4%# 3)*#45# 3, 3))-)-$, 3)0&/%, Matlab-$ .1
.Matrix Laboratory
./%&/%,% $* & 9-#6, (built-in) �$#% &#).50#( !/ $* *(-% .2
toolbox &#)*(-$ 3)*#%/, 3))().(- 3))-+0, 3)%#6&! &#+2#)%, &#).50#( !/ $6*0 *'"% .3
.,%#+% &#").% #!)("# ,5)4-)44- ,,*5$ ,�#%& +#$)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,
2
Matlab-! "#$%
!"#""#$ %&'(
2........................................................................................................................................ ,).$)4#%# "#$%
6.......................................................................................................................... MATLAB !"#"$ !%&' .1
6........................................................................................................................ 3/#%)/# �#!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-! "#$%
4
Matlab-! "#$%
5
Matlab-! "#$%
,+#$2, $6*%
*$-, 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.#$% 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
6
Matlab-! "#$%
;$#5 &'#.&
8%#-%, ,*72,
!1
).$5
,*72,
3)!)1%,
&!)% &"
/#()6,
,*72, ;$#5
8%#-%,
7
Matlab-! "#$%
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$
.#!/
$))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!
,8,% /#!/ 5* ').0 9" &#$* &#).(#" &#%))5 .format &).50#( .%"$ ,'#.&, &" �/! 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
,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 &#", .%"$ 3)0)#.% 3)-5!(%#5 3)*(-%
.Matlab-$ 3))5#6
-! ))/ "!/ &#).50#( 3' ,').%, lookfor &).50#( &%#2! ,3)-toolbox-$ )(#%, &#).50#( 5* ,').% #7 &#*/("1
.3)-toolbox
8
Matlab-! "#$%
.realmax -% !#+', 9*2$ #" -("$ *(-% &5#!6% !$5&% Inf 9*2,
I nf 0
. #" ,5#!6,% !$5&% NaN 9*2,
Inf 0
!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%
3)%6#&% ,.)*4%, )*$)" .,1#&$ 3)1*2 &$.,# ,.)*4% &*).) "), Matlab-$ *&#)$ &)-)-$, ,!#2(,
.&#*#/, 8)$ +)*(% ";" 8%)-# ,.)*4%, !/ ,*#/, )*$)" 8)$ +)*(% "," 8%)- ,$#*% 3))*'#- 9#&$
,"$, 8(#"$ A ,.)*4%, &" *)+'0 !/%!
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
,"%'#+!
10
Matlab-! "#$%
:)-#0 ,+#5(, :#-$ 81!# 3)1*2, &$., &!#2( *6"! ,.)*4%, 81#& &" &#"*! ,.*0 "! 3)$* 3)*5%$
,,+#5(, *%#!1 .";"
.,.)*4%, 81#& &" ,').% ,00)" 9" A ,.)*4% ,&#" &" &*.#)
.$#/)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"
&#*#/, *(-% ,)*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-! "#$%
.&#+6#)% &#.)*4% &#*.#), &#).50#( *(-% &#%))5 &#.)*4% !/ &)0+), ,*+',! :-#0$
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#( .%"$ &)0#-1!" ,.)*4% *.)! 8&)0
.3"&,$ &)0#-1!" ,.)*4% *)76%# *#45# *#&$ )/"*, 8#-1!", &"
,!"0, &#+#5(, "%'#+!
B &)2#$)*, ,.)*4%,% 3))0#-1!", 3)*$)", &" ").#,! *%#!1 ,,1#(,, ,!#2(, &" 2.$! 8&)0
,"$, 8(#"$ ,).50#( ,&#" .%"$
&.)*4% &" *.))! )+1 .,+)6), &.)*4% "#, &)0#-1!" ,.)*4% !/ )/#%)/ )4*( ,*5%
,"$, 8(#"$ eye ,).50#($ 3)/%&/% I[n , n] ,+)6),
12
Matlab-! "#$%
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-! "#$%
,,),) &#+#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-! "#$%
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)"
,.)*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%
�/! 8&)0 :-#0$ .,%)"&%, &)0#/!, !2 ,.)6! )"2 Array Editor-$ ,0&/%! ,0&/% 8)$ *#$2! 8&)0
.)0+) 8(#"$ ,.)*4%, )*$)" )1*2 &"
.3)-5+0)"$ /#%)/# ,$., &!#2( .%"$ "), ,.)*4%$ 3)*$)" �/! &(-#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-! "#$%
16
Matlab-! "#$%
('-"+./ 32 ,"-3'7"#/ .3
+&#"/%'- )"316,"( 3.1
8)).% 8#/"*, *$)", .3)-5+0)"# &#!#'2 3))*'#- .%"$ ,.)*4% 9#&$ 3)*$)",% 5!6! &/'! 8&)0
.3,+#%2 *(-% 8)).% )0/, *$)",# ,*#/ *(-%
,3, ,.)*4%$ 3)-5+0)",
,"%'#+!
# 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,:) %/% .,)0/, ,+#%2,# &#*#/, !1 "), A(:,2) *%#!1 .3)*$)", !1 "), ":" !/ %/%,
)#4)$! #" A(1,1:4) )#4)$! !#5/ A(1,:) )#4)$, *%#!1 .&#+#%2, !1# ,0#/"*, ,*#/, "),
.4A(1,1:end)
.3))#.*, 3)*$)"$ ,$., &!#2( .%"$ ,.)*4%, 9#&$ 3)1*2 �/! 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 &$.,
.[] )* $#*% 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-! "#$%
&).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#(, !/
,&#.)*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)" !/ )*/(" /#%)/
.%"$ 3)*6" 3)40%!" *#6$! 8&)0 .-("% 3)0#/, 3)*$)" &").%! 5* &!$'#% ,00)" find &).50#(
,find-, &).50#( !/ 3))*'#-$ )#.* )"0& &$)&1
3,)*$)"/ 91 3)*#45# /+6% &*+-%, sort &).50#( "), 3)-5+0)" &!$5! &(-#0 &)/#%)/ ,).50#(
./+6% *#+)-, 2#.)$! /%/%, -5+0)", &"# !#+'! +2 845,% #),)
,"%'#+!
,' 8%)-, .%"$ &2.#$% (,.)*4% &#+#%2# &#*#/ 8)$ :#!)6) 6transpose &!#2(
# 2 3$
C - %% 1 8 &&
%( 5 4 &)
"*","-","+" 3)*#4*(#", .%"$ &#.)*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&)
#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
ans - "5
#"
. det(A) / 0 -/ )"0&$ 7
21
Matlab-! "#$%
.3)1*2%! &#.)*4%, !/ %/%, &" �/% ,/2%! ,+#50, *#4*(#" .%"$ .#$%, &#!#2(
.*$)" !#% *$)" ,!#2( -3)1*2% )0/ 8)$ ,!#2( "), &#.)*4% )&/ 8)$ ,+#50 &!#2( *%#!1
,&#)!!1 &#.)*4% )&/ *)+'0 ,/6%, 82%!
!"#$%
.&)0#$/6, ,!#2(, *#4*(#" )0(! 35#%% ,+#50, *#4*(#" -
.,0#/ "), ,+#50, *#4*(#" "!! 5#!)6,# !(1, %/% &#)2#$)* &#.)*4% *#$2 -
*#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*(#" .%"$
,"%'#+
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
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-! "#$%
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(#"$
#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, #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
# 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 &)
,&#"%'#+ )&/ 8!,! .&#.)*4%# 3)*#45# &!(1% .%"$ 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-! "#$%
,*#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(#"
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
#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 &)
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)
, 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-! "#$%
.3)+6" &.)*4% #" *#45#1 X &2)$5 )"2 &#.)*4% !(1/! /%% 8&)0
29
Matlab-! "#$%
# 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 &)
&)2#$)* ,.)*4% "), A ,.)*4%, 7" 0 m ' n 1 ,,7 3)%!20, *(-%# &#"##/%, *(-% #$/ ,*5%$
,"$, 8(#"$ inv &).50#( .%"$ !"0, &#"##/%, &1*2% &" 10*#&(! 8&)0
,"$, 8(#"$ \ *#4*(#", .%"$ "), !"0, &#"##/%, &1*2% &" *#&(! ,()+2 9*+
30
Matlab-! "#$%
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#/( #"
('9'+5/ 3.9.1
.4-54 &'.,! #*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
.#*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$ #), #*6%, )&/ !"0, ,*5%$
&).50#( 32 #7 ,)2$ !2 *$'&,! 8&)0 .,")'/ &2+#, !$50 3)0#/ 3)1*#" &#!2$ #*6% -)01,!
.str2mat
,&$)&1 "%'#+!
&#".#& &'.,$ !/%!) 3))*(-% 3)0&/% )1*2 &'.,# 2#$5 4-54 !/ $#!)/$ 9*#. /) 3)$* 3)*5%$
,+$!$ #*6% ').,! &#!#1) disp #%1 4-54 &'., &#).50#(# *6"% .(:*' !/ &*$ #" ,+)+% !/
.#*6%! 3)*(-%, 3)1*2, &" *)%,! num2str &).50#($ 3)/%&/%
,"%'#+
.Command Window-$ &"7 ').,! 3).#* #0"# %"- 9 "#, 45))$#" 9*#" )1 !$5&,
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*(! .,'#.&$ *&#)
input &).50#( .%"$ 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
!(%2'
.4-54$ ,*#/ &+*! )+1 \n :)-#,! 8&)0 -
#7 ,).50#( .pause &).50#( "), /%&/%,% &#*)/) &#"*#, &!$5%, &(-#0 ,).50#( -
.#,/!1 *#&(1 !2 ;6!) /%&/%,/ +2 ,.*,, &" ,,/%
33
Matlab-! "#$%
$0"/ "#)/ .4
,3)"$, 2+)%, )0$% )'#- 3)%))5 Matlab-$ .#*6%# &#.)*4% #%1 2+)% )0$% *(-%! #0(/60 ,&2 +2
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, #*6%# &#.)*4% 3)"*50
.3)(-#0 2+)% )0$% !/
!/ :-#"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 &" �)).%, (8$#%1 !+#' #&#"$)
9*2% &##,% *+6$ ,*#4*(%4 !/ &#)+%% &!& &#+)+% "%'#+! ,)+%% $* 2+)% *#&$ 3' 3))+%%-$*
.)+%% &!&
zeros, ones, rand, #%1 &#)-)-$ ,.)*4% &*).) &#).50#( .%"$ 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 !/ !+#',
34
Matlab-! "#$%
,&)(*' ,/6%,
)-)-$ 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)).)/ )*. 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#(
,!/%!
3%.2 3)+%%, &" �/! #" permute &).50#( .%"$ 9*2% !/ 3)+%%, *+- &" �/! 8&)0
.reshape &).50#( &%"$
-, &!#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 )
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#( .%"$
,"%'#+!
36
Matlab-! "#$%
37
Matlab-! "#$%
string 7
,0/) "! 3)*6", 3)1*2%, )#0)/ 81!# 3)*6" 3)1*2% !/ 3)5 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
,"%'#+!
.3)40%#'*" +#2 &(-#, )"2 3))+%%-$* 3)"& )1*2% *#.)! 8&)0 #7 ,).50#( .%"$
,"%'#+!
39
Matlab-! "#$%
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
.billing 135.00
120 39 78
.test 98 154 12
,"$, 8(#"$ 9*2%, &" $)6*,! 9)*. 6#5! +#2 *#.)! )+1
40
Matlab-! "#$%
,3#/*0 *1, 2+ !+#'$ patient 9*2% #&#" &" *#.)! )+1 "%'#+!
.(&#+/, !1! 3)1*2 !)1%,) structure 9*2%$ *$)" !1! &/'! 8&)0
,"%'#+!
41
Matlab-! "#$%
.)#.*, ,+/, 3/# ,,+#50 ,9*2%, 3/ &" $#*% 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 !/ �#*&), +6"
.3))+%%
,3)'#- ,/#!/! !6&% ,5)(*', &#).50#(
,).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#( .%"$ 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-! "#$%
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#*)
,3,)0)$ 3)##5 *#$)6 "!! &#+#50 !/ 3)(*' 44*/! 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-! "#$%
1
0 1 2 3 4 5 6 7 8
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
,17"$, 8(#"$ &"7 2.$! 8&)0 3)*)., &#!#$'% 5!6 5* �/! #00#.*$ 3"
,3)*)., &#!#$' &" #,/!1 *#45#$ $).,! ,!)6&
,81+#2%, *#45#, &" axis-! *)76,!# &#)#.*, ()+*#"#5, &" *#45#$ �/!
.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-, ()+*#"#5 &" ,##,% )0/, *#45#,# x-, ()+*#"#5 &" ,##,% +%.$ 8#/"*, *#45#,
,+#5, "%'#+!
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
.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$.,#
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, "%'#+!
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% *#). "), &6" "%'#+ .,+)6"# &)*"0)! ,*#.$ 3#'+ x-*). +'01 5* "! 3)(*' 44*/! 3' 8&)0
,"), &#)74*5 ()+*#"#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
!/ &#).*#(#*(, &" &2$#5 axis square &).50#( .3)*).! 3)!)$5%, 3)##5 &/* &*.#) grid &).50#(
&).50#( .%"$ 3)*)., &1*2% &'#.& &" +)*#,! 3' 8&)0 .!+#' #&#"$ #"*) 3)*)., )0// 91 :*',
.19axis off
.axis equal-# axis tight #%1 &#)/#%)/ &#)*/(" *(-% +#2 *#(% axis &).50#( !/ help-$ 19
48
Matlab-! "#$%
-1
-2
-3
-4 -2 0 2 4 6
&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, "%'#+!
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
50
Matlab-! "#$%
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*/
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-! "#$%
'%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
,"%'#+!
,&#).50#( /#!/ �#&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##
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&&
,+#5,
53
Matlab-! "#$%
0
0 1 2 3 4 5 6
,0#/ &#+#50 *(-% ,,0#/ 3#6&$ 3)*). &1*2%$ ,).50#( !1 #044*/ !"0, :*'$ )1 $! 3)/0
.3)0#/ &#*#.# 3)2$.#
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 .%"$ 9(,!# off-! on
,+#5, "%'#+!
54
Matlab-! "#$%
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
55
Matlab-! "#$%
*/("% Matlab .&#".#&, &*,$,! 3)0#%)- :)-#,! 3' !$#5% 3)(*' .%"$ &#".#& &'., &2$
,3)"$, 3)0#%)-, &(-#,
:*'! &* -
y-# x 20)*).! &#* -
"*5% &$)& -
:*'$ 3#5% !1$ 4-54 -
:*'$ 3#5% !1$ 3).6# 3)##5 -
2.#%%# 3#%)0)% ,3#%)-5% #%1 &)-)-$ ,5)4-)44- -
. "3 E x E 3 3#6&$
,+#5,
.3)0#%)- &(-#& "!! !"0, &#).50#(, )&/ &" *"&% ,7 :*' .5.11 :*' &" $)0%
0
-3 -2 -1 0 1 2 3
56
Matlab-! "#$%
$&1 .3)!%-# )&6& $&1 ,)!)2 $&1 ,&#)0##) &#)&#" *#.)! 8&)0 4-54 &%/,# &#* &(-#,! &#+#5($
&)0##), &#", 3/ &$)&1# \ .%"$ &#)0##) &#)&#" ._ .%"$ )&6& $&1 ,^ .%"$ *.#)% )!)2
&#)&#" &$)&1 #%1 "), 21&#!#+' &#)0##) &#)&#" &*).) .\alpha &$)&1 )"2 &*.#)% G "%'#+! .&)!'0"$
&*.#)% H "%'#+! .,!#+' &)!'0" &#" &#),! 9)*. &)0##), &#", 3/ !/ ,0#/"*, &#",/ 5* ,-
.\ .%"$ *#.)! 3))*/(", 3)!%-, !1 &" &%1-% 5.4 ,!$4 .\Gamma &$)&1 )"2
3))*'#- .%"$ &"7 &#/2! 8&)0 .400#(-5" !/ )#4)$ #%1 ,+6) 3)!%- *(-% +'"! )#.* &#$* 3)&)2!
2
.e^{\alphax_1^2} $#&10 eGx1 )#4)$, "%'#+! .{} &#!-!#-%
,"%'#+! .xlabel &).50#( !/ 4!5$ &7#*6% &$., )"2 &2.#$% x *).! &* &(-#,
,"%'#+! .ylabel &).50#( !/ 4!5$ &7#*6% &$., )"2 &2.#$% y *).! &* &(-#,
&7#*6%# 4-54, &$)& ()+*#"#5 &$., )"2 &2.#$% :*'$ (x, y) ()+*#"#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##), &#)&#", �)#.% "! ,!$4$ 21
. VerticalAlignment-# HorizontalAlignment &#).50#( .%"$ &#*6" &#)*/(" 22
57
Matlab-! "#$%
58
Matlab-! "#$%
!/ 4!5$ ,:*'$ &#).50#(, &#%/ &" &#'.)%, ,#*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)*)&-% (/ :*'$ 3)*6" &#%#5%$ ,$).,!
,3)"$, 3)1*2, &" !$5%, ,0&/% +#2 4!5$ :)-#,! #" (Unlock Axes Position .%"$) )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
(/ ,%1 ,*)&-% ,$)&, #$/ 3#5)%$ ,:*', &#!#$' 9#&$ "*5%, &$)& 3#5)% 0
,0#)!2 &)0%), ,0)($ ,+*(0$ :*', &#!#$'! ;#6% "*5%, &$)& 3#5)% -1
,+#5, "%'#+!
.*$+ *)&-& "! "*5%, &$)&/ 91 8#)!2 )71*%, 5!6$ "*5%, &$)& &" $).%
3()-#,! &#!5$ 8&)0 #.%"$# &#).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 .:*'!
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
,,0)%) 8##)1$ &#),! 3)!#1) #!" 3).6 9" .\ *#4*(#", .%"$ text &).50#($ 3).6 *.))! 8&)0
.(7.1 5*( #"*) )0+) 8(#"$ 2.#$% 3,/!1 3).6# 3)##5 4#4*/ .,4%!# ,!2%! ,,!"%/
.,0/% #0)" 8&$)&1 *+-# plot &).50#( *6"! $&1),! &#$))6 3)0#%)-, &(-#, &#+#5( !1
5
sin(F!x)+4I
y
4
0
-3 -2 -1 0 1 2 3
x
.)0+) 8(#"$ #0#/ "*5%, &$)&$ f (x) ' sin( Fx) . 4 ,).50#(, !/ 3#%)-1%# 3#%)0)% )1*2! 4-54, 23
60
Matlab-! "#$%
9*+, .)#0)/! �&)0, �#1& !/ $* *(-% 45))$#" !1!# ,3))(*' 3)45))$#" *(-%% $1*#% :*' !1
,).50#( !/ 4!5$ :)-#,! "), &#+#5( .%"$ ,5)(*' !/ 3)45))$#" !/ �#1& �/! ,4#/(,
.#7 ,0#1& !/ /+6, 9*2# ,0#1&, 3/ !/ &7#*6% :3)(-#0 3)0&/% )0/ )(*' 45))$#" &*.#),
,+#5, "%'#+!
2
7
@x
6
y
sin( F!x)+4 I
4
0
-3 -2 -1 0 1 2 3
x
!(%2'
.7.2 5*($ 4*#(%/ )(1 set-# get &#).50#($ /#%)/ "), *&#) &1$#-%, 9*+, -
,*72, ;$#5$ #00#$&, 3,)�#1&# ,5)(*' !/ 3)45))$#" !1 !/ ,%)/* &#"*! )+1 -
.(html 4%*#($) Handle Graphics Online Documentation
61
Matlab-! "#$%
&" "!%! )#.* !"0, "%'#+$ !/%! .fill &).50#( .%"$ &#).50#( )&/ 8)$ :*'$ *#7" "!%! 8&)0
.3#+" 2$.$ "2 E x E 2 *#7"$ ,!#$*(,# -#0)-, 8)$ *#7",
,+#5,
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
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-! "#$%
)*/(" /#%)/ .:*' #&#" !2 3)0#/ y )*). )0/ &#!2$ &#).50#( 4#4*/ &*/("% plotyy &).50#(
)&!$ ,0&/% #&#" !/ ,).50#(1 (�#/ &#+)6)) 3)0#/ 3))!5-)( 3)!+' )0/ 4#4*/ "#, ,7 '#-% :*'!
.)#!&
,+#5, "%'#+!
40 1
20 0
0 -1
0 2 4 6 8 10 12 14 16 18 20
x
#" 8#0'- &" �/! #" ,+6" !1! ,0#/ ylabel 8)).! )+1 .�#/ y 2 !/# y1 !/ ,!5-, )1 $! 3)/0
.plotyy !/ help-$# set &).50#($ *72),! /) 3)##5, 2$.
:*', ,!#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 !/ ()+*#"#5, &" ,$).% :*'$ &%)#-%
.Enter !2 3).6#! 3))-! )+1 .x,y 3)*#45#! ()+*#"#5
63
Matlab-! "#$%
,subplot &).50#( .%"$ ,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* )(/%
Figure No.1
"
"
"
"
"
"
64
Matlab-! "#$%
0 0
-5 0 5 1 2 3 4
0 0 -48.2
-1 -1 -48.6
0 100 200 0 5 10 210 211 212
65
Matlab-! "#$%
#&#" )!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$50 z *). !/ &)##&,# z *). !)$/$ ,40)+*"#5 +#2 :)-#,! 9)*.
,"%'#+
,+#5, .,)0/, +)! &6" ,!*)(-# ,+)%*)( 44*/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
67
Matlab-! "#$%
!/ )!!1 3#/)* .surf -# mesh &#).50#( 8, 3))+%% &!& 3)64/% &*).)! &#)-)-$, &#).50#(,
,#!" &#).50#(
-#
&).50#( .%"$ x,y ()+*#"#5 )*#45#% (#%, ()+*#"#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,
68
Matlab-! "#$%
15
10
z 5
0
4
0
3
-2 2
1
0
y -1
-4 -2
-3
x
15
10
z 5
0
4
0
3
-2 2
1
0
y -1
-4 -2
-3
x
'%2'
!1 ,#!" 3))+%% &!& 3)(*' &*).) &#).50#( .%"$ *&#) 5!6 64/% 44*/! #00#.*$ 3" -
.,%)'+ &#+#50 *&#) &(-#, .%"$ ,).#!#7*, &" 8)45,! "#, &#/2! #0)!2/
69
Matlab-! "#$%
,*/"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) "! +"% 8(#"$ *$)" *$)" Z &.)*4% $#/)6 &/6%, 8!,!
&" �/! 8&)0 Matlab-$ .view &).50#( ,0#/"*, 32($ ,'.#, 6.3-# 6.2 :*' &*).)! +#5$
.rotate3d &).50#( .%"$ #" (5.15 :*' #"*) *#&(1, !2 ,.)6! )"2 :*', !/ ,).40)*#",
view &).50#($ 3)/%&/% #7 ,)(. &)#7$ '.#) :*', 32( !1/ 3).#*# ,)#.* ,)(. &)#7 3)".#%/ 2'*$
,"$, 8(#"$
.6.4 :*' ,"* .,,$',# 4#%)7" .%"$ ,).40*#", &" 3)*)+'%
,+#5, 2#.)$
70
Matlab-! "#$%
!/ 4!5$ #!" 3)1*2 &$., )"2 &2.#$% :*', !/ ,).40*#", &2)$5 .!"0, 3)0&/%$ #!" 3)1*2 $).%
,26view &).50#(
-# surfc &#).50#($ 8()!60 mesh-# surf &#).50#( *#$2 :*'! &6&% *"&% )##5 3' !$5! #00#.*$ 3"
.(*"&% )##5 ,%/%/ contour ,!)%, &" &'.)% c &#",) 3"&,$ meshc
,+#5, "%'#+!
15
10
z 5
0
4
0
3
-2 2
1
0
y -1
-4 -2
-3
x
.az=-37.5 el=30 3, )+%% &!& :*' !/ !+6%, &*)*$ )1*2# az=0 el=90 3, )+%% #+ :*' !/ !+6%, &*)*$ )1*2 26
71
Matlab-! "#$%
15
10
z 5
0
4
0
3
-2 2
1
0
y -1
-4 -2
-3
x
camlight &).50#( .%"$ )#.* 8##)1$ 27,*#"& :)-#,! 3' 8&)0 )-)( 45)$#" !/ ,/#6& &*).) 82%!
,!"0, &#*#/, &(-#, ,"%'#+! .lighting &).50#( 32 ,*#"&, '#- &" *#6$!#
.surfl &).50#( .%"$ 3' ,!!.,# ,*#"& )!2$ 3)(*' *#.)! 8&)0 27
72
Matlab-! "#$%
2#$5! 3' 8&)0 .,).50#(, !/ )-6), ,$#'! 3"&,$ 3)0&/% ,).50#(, !/ 3)2$., !+6% &*)*$ *#&$
,,).50#(, .%"$ ,$#'$ &#!& "!! +)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))-)-$,
,&#)*/(",%
73
Matlab-! "#$%
,waterfall-# meshz 3/$ ,surf-# mesh &#).50#(! &#).")*# &##,%, &#).50#( )&/ +#2 &#%))5
.3)0#/ 42% 3)(*' &#*.)%,
,+#5,
74
Matlab-! "#$%
15
10
z 5
0
4
0
3
-2 2
1
0
y -1
-4 -2
-3
x
15
10
z 5
0
4
0
3
-2 2
1
0
y -1
-4 -2
-3
x
75
Matlab-! "#$%
,+#5, "%'#+!
9*#. 8)" 3" .*"&%, )##5 !/ 3)1*2, &" ').% !"0, 8(#"$ clabel &).50#($ /#%)/,
.!)'*, 8(#"$ ,).50#($ 3)/%&/% 7" #!" 3)1*2$
,+#5, "%'#+!
.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, "%'#+!
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
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
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
78
Matlab-! "#$%
,"%'#+!
. g(x, y) ' x . y . 4 ,!#$*( +#2 6.3-# 6.2 5*(% "%'#+, 64/%! :)-#0
2 2
,+#5,
79
Matlab-! "#$%
.,5!6$ &*&-#% ,0#&6&, ,).50#(, 91% ,".#&1# )0/, &" +6" 3)1 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%*( .%"$ :*'$ 3)64/%, !/ &#()5/,
.&4!6#% &#()5/ 0-# ,&4!6#% &#%)4" '.))% 1 */"1 1-!
,"%'#+!
.+$!$ 8#)!2, 64/%, &#()5/ &" ,0/0
,+#5,
alpha "-0"3 .( )&3*6/ )"*5*& %*2* !"# ,( .*2-/0 "3+0 -&-"/ :6.15 !"#
80
Matlab-! "#$%
!"7+1) ,."32 .7
3))(*' 3)45))$#" !/ �#1&, .+#2# 4-54 ,3)##5 ,3)*). !/ 3)45))$#" 3)!)1% Matlab-$ 3)(*'
.Handle Graphics .%"$ #" )0+) 8(#"$ )#0)/! �&)0
2)(#%/ )(1 (toolbar) ,+#$2, !'*- 9*+ #" Insert 4)*(& 9*+ ,2)$5! �&)0 5.3 5*($ &#+#5(,
.7.1 :*'$
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&#(
�#1& &1)*2
*+&3*- *3"# 4&,2 :7.1 !"#
.Edit 4)*(& 9*+ )#0)/! �&)0 3)*)., 45))$#"# :*', 45))$#" !/ �#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$ .�#1&, &1)*2 8#!6 &" &6&#( Properties !2 ,.)6! ,45))$#", �#1& !1! &/'! )+1
45))$#" !/ �#1& &1)*2 8#!6 '.#% 7.2 :*'$ .+#2# 8#0'- ,#5 )$#2 ,2$. #%1 3)$* 3)*4%*( �/!
.#5
81
Matlab-! "#$%
!1$ 4#4)/
45))$#", '#- :*', !/ 3)45))$#",
!/ )0%), *#&(1, !2 ,.)6! ,,1)*2, ;6 .%"$ #0#%)- )"2 :*', &" ,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 .%"$ :*', !#(1/ ,*$-#,/ )(1
.)0/, :*'$ )#.*,
82
Matlab-! "#$%
-# 3)(*' *.#)# 2+)% ').% Matlab 3&*72$ 3))-)-$, 4#4*/, )$)1* 3, Handle Graphics )45))$#"
.45))$#", !/ �#1&, &" �/! 8&)0 #1*+/ handle */#5% ,5)(*' 45))$#" !1! .3)-GUI
,7.4 :*'$ *"#&%1 "), Handle Graphics 45))$#" !/ ,)1**),,
Root
Figure
)+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 :*'$
83
Matlab-! "#$%
84
Matlab-! "#$%
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#(, .%"$ :*' 45))$#" &*).)
handle = function('propertyname',propertyvalue,...)
,+#5, "%'#+!
7.7 !"#
,+#5, �/*(
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 $50 &#+)6), .,5)(*', 8#!6 )+%% &" $#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, &" $#5 Ytick-# Xtick �#1&
.x-y-z *#/)%$ 64/%, $#5 Zdatat ,Ydata ,XData �#1&
!1 4#*)( &#*/("% 3' 8, :-#0$ .,5)(*' 45))$#" !/ �#1& &#*)76%# $#5 get-# set &#).50#(
.2945))$#", �#1& !/ 3))*/(", 3)1*2,
,,5)(*' 45))$#" &0#1& !/ 9*2 &2)$5! )-)-$, 3#/)*,
,0#1&$ $.#)/ 9*2, &"7 &%#2! .#*6% +)%& 3, PropertyName �#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#/)*,
,!$50#
Handle Graphics Online Documentation ,*72, ;$#5$ &%))5 3,)�#1&# ,5)(*' !/ 3)45))$#", !1 !/ ,%)/*29
.(html 4%*#($)
86
Matlab-! "#$%
7.8 !"#
,+#5,
7.9 !"#
87
Matlab-! "#$%
Editor/Debugger %'35 .8
,7 8#!6$ .Matlab-$ &, !/ 2)*1%, $#*, 2.#$% #1*+ 8#!6, "#, Editor/Debugger 8#!6
.Matlab-$ +#5, ).$5 3,/ .m ).$5 3)$
)*6" &6" .#$% &#+#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#( .%"$ #!" &#).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 �#!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 !/ &*$ * 8%)- -
.(C-$ &#!(%#5%, &#).50#( *(-%% ;#6) Matlab !/ &#)4*+04- &#).50#( 81#& &" ,').% type &).50#(30
88
Matlab-! "#$%
2$.$ �%#-% &#*2, .#)*6"! )#.*, 4-54, &$)&1# % 3)7#6", 8%)- .%"$ &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)*(&$
6&(% &#!% .Matlab !/ &#*+',, )(! "!" 8,$ /%&/,! ,*5% !1$ *#-"/ 6&(% &#!% *(-% &#%))5
,6&(%, &#!% 8!,! .!#61 2$.$ �%#-%
8&)0 #.%"$/ Find & Replace 8#!6 &" &6&#( Ctrl+F !2 ,.)6! #" 8%)-, !2 ,.)6!
,3)##& :)!6,!# "#.%!
89
Matlab-! "#$%
&)).(#" &*)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,
#6*, &#)().(- &#*#/ 8)$ *#$2! #00#.*$ 3,$ 3)1#*" .m ).$5 *#$2 *5)2$ &)/#%)/ #7 ,).(#"
.,)0/,% &6"
'%2'
.3).$5 &*)'- &2$ &#*%/0 80)" &#)0%)- -
,!#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
.8%-, ".%0 8,$/ 3))*'#-, 8)$ ".%0, +#5, !1 8#%)- "), #7 ,).(#" %/% 31
90
Matlab-! "#$%
/#%)/ ,+#5(
3))'#! 3)"0& 9%- !2 &#+#5( ;$5% &2.$% else-# elseif &#+#5( 32 +6) &#+#5(
,0&/% !/ 9*2 -"2 �#/ &#+#5( ;$5% &2.$% otherwise-# case &#+#5( 32 +6) switch ,)0&,
2#.)$, 8%7$  ,!'&% 3" &#+#5(, $#&)0 &" ,0/% try…catch &#+#5(
)'#! )"0& -"2 ,3)%2( !/ !$'#% )&!$ *(-% &#+#5( ;$5% &2.$% ,"!#!
2#$5 3)%2( *(-% &#+#5( ;$5% &2.$% for
,"!#!$ &#*�, &#+#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#.)$ *+-$ !#/ !"0, &#+#5(, !1
32
&%" &#),! !#1) )'#! )"0& .)'#! )"0& "#, +#5, 2#.)$ )"0& while-# if &#+#5($ .,+#5(, 3#)- &"
"#, )'#!, )"0&, 3" 5* +#5 .$% #!" &#+#5( )&/ *%#!1 .(0 )*%#0 9*2) *5/ #" (1 )*%#0 9*2)
.&%"
.9.2 ,!$4$ 3)4*#(%, 3))-6) #" 3))'#! 3)*#4*(#"% )'#! )"0& �$! 8&)0
9.2 $,'-
isempty ,).50#(, !/%! .is* 8#0'-,% 3)$.% +#$, &#).50#( .%"$ )'#! )"0& �$! 3' 8&)0
.,5)* ,.)*4% "), ,.)*4% 3" &5+#$
,&#"%'#+
,)"0&,
.0 9*2 $)0%
,)"0&,
.1 9*2 $)0%
91
Matlab-! "#$%
92
Matlab-! "#$%
if +6&10 9.1
*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%,
,!/%!
else &+#5(
(else if &+#5( #") if-, &+#5( !/ )'#!, )"0&, 3" .#$% ,! &#*#/5, &#+#5(, .)'#! )"0& ,! 8)"
.false ,),
,"%'#+
93
Matlab-! "#$%
else if &+#5(
-, &+#5( !/ )'#!, )"0&, 7" false ,), &%+#5, (else if &+#5( #") if-, &+#5( !/ )'#!, )"0&, 3"
..#$% ,! &#*#/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'
94
Matlab-! "#$%
.(&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(, .#$% 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#(
,"%'#+!
95
Matlab-! "#$%
&#+#5($ ,")'/ ,*.#0 "! 3" .,")'/ &*.#0 */" +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( .#$% 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-! "#$%
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-! "#$%
,&)!!1, ,&*#. .3)%2( /"*% 2#$5 *(-% &#+#5( ;$5% .$% 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 ,"%'#+!
3#/)*, %/% .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-! "#$%
.-5+0)" 8%-% i */"1 &#.)*4%# 3)*#45# 9#&$ 3)1*2 3)$).% &#$* 3)&2! for-# while &#"!#!$
,&#"!#! .%"$ ,.)*4%$ 3)*$)" $).,! &#)*5)2 3)1*+ /#!/ &#%))5
99
Matlab-! "#$%
.3)1*2, 3)$.#% ,)!" ,.)*4%, !+#' ,),) ,% /"*% 2#+) "!/ )0%# &#!#5/ !"0, 3)1*+, )&/
&" !6&"! 8&)0 81! .x )*#5%, *#45#, 9*#"! ,,7 &#),! 9)*. x_abs *#45#, 9*#" )1 *#*$ #7 "%'#+$
,"$, 8(#"$ #01+2!# 3)-(" *#45#1 x_abs
,$)-, .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)"$ &#*�, &#+#5(, *"/ !2 &'!+% while #" for &"!#! 9#&$ continue &+#5(
,"!#! 9#&$ &".%0, ,"!#! 9#&% &2.#$% continue &+#5( 3" .,"$, ,).*4)",% ,1)/%% ,.*,,#
.+$!$ &)%)0(, ,"!#!, 9#&$ 5* &#*�, &#+#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,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 &)
&#*#/, !2 '!+! 3).#* #0"/ +#2$ (j) &#+#%2, !2 ,.*/ &"!#! 9#&$ &%5#%% 2#.)%, &"##/% &21
,&#+#%2, &"!#!% &".! )+1 break &+#5($ /%&/0 91 3/! .&#)0#.)5,
102
Matlab-! "#$%
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#( .%"$
.,.*,, &" ,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" +#, 6#!/! )+1 warning &).50#($ /%&/,! 8&)0 81 #%1 -
.+#5, 2#.)$ &" 5)-(,! "! 9" Window
103
Matlab-! "#$%
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#(
,"%'#+
104
Matlab-! "#$%
"
.,).50#(! 3)"*#5 3.%"$/ 3)0&/%, &#%/! 3),7 &#),! 3)$))6 30)" ,).50#($ 3)0&/%, &#%/
,&$)&1 !/%!
,3#/)*, ,/2%!
105
Matlab-! "#$%
nargin @ 4
,3)*5% )0/$ *5)2$ ,$#/6 #7 ,).50#(
,")'/ **#' )+% !#+' 3)0&/% *(-% &$., .4!5, �)5& &5)+$ "#, ,).50#( !1 !/ $#/6 5!6 .1
.*$-,#
,4!5$ 3)0&/% 5 &$., "%'#+!
!2 4#!/! #00#.*$ 3" .3)*+'#% "! 3)0&/% !2 ,")'/ +#, *#*') )+% 845 3)0&/% *(-% &$.,
*(-% &" ,*)76%, ,).50#(! 3)5#57 #0" #0%24! &#*#*$ *&#) ,")'/ +#, *)+',!# ,")'/, +#,
.,).50#( !/ 4!5$ 3)40%#'*",
.3)0&/% !/ ,0#/ *(-% 32 ,.*,! �&)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&/% ( 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%,
nargout @ 3
.3)*&#)% 3)$#/)6$ 9#-6! 3).#* */"1 *5)2$ ,$#/6 #7 ,).50#(
?3)0&/% ( /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" ,"%'#+!
,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)
,"%'#+!
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 *(-%, &"
,3#/*0 .4-! 2 8)$ "#, ,!/ 4!5$ 8)5&, 3)0&/%, *(-% )1 2$50
,,")'/, &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! 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 .%"$/ 91 $&1),! ,1)*. &#).50#( !/ ,).50#(
.,).50#(, 3/ -"2 &#*6" &#).50#( .#$% .1
4!5$ 3)40%#'*" !/ /"*% 2#+) )&!$ *(-% &#!2$ &#*6" &#).50#( .#$% .2
./"*% 3)2#+) )&!$ 3)'#-%
.@ 8%)- .%"$ 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 ,"##/%! �#*&( &".#% -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
help-$ *#(%, &#$)-% 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
3#%)0)%, &+#50 &" "#.%! )+1 .!"0, 3#6&$ #7, ,).50#(, !/ 3#%)0)% &+#50 "#.%! "), #0)&*4%#
,,"$, ,"##/%, &" *#&(!# -("! &*7'0, &" &##/,! 9)*. )4)!0" 8(#"$
fminbnd &).50#( .%"$ )*%#0 8(#"$ 3#%)0)%, &+#50 *6" /(60 81!# +"% ,/5 #7 ,"##/% &*)&(
,+#5, .
'%2'
,!/%! .,!/ 3))*'#-$ &".%0, &7#*6% !1 &2.$%, eval 3/$ ,).50#( &%))5 -
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
111
Matlab-! "#$%
)1, "%'#+, .3)0&/% !/ /"*% *+'#% )&!$ *(-% 4#!5! &#!'#-% &#),! &#$))6, &#).50#( 80/)
#7 &#*/(" .394#4*/! 3)(*' !/ #,/!1 *(-% !$5! ,!#1), plot &).50#( "), #71 ,).50#( !/ ,4#/(
.feval &).50#( .%"$ ,).50#( !1 !2 &!2#(, &)!!1 ,).50#( !/ ,*+',$ &)0#)6
,,/6%,
,&#"$, (&#)4%&%,) &#).50#(, )&/ !/ 3)-(", &" "#.%! 3).#* #0"
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
112
Matlab-! "#$%
varargin !#)31(0
+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
,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#&!
113
Matlab-! "#$%
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-! "#$%
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
,*%#!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-! "#$%
4!5$ cell array 9#&$ 3)0#/ 3)'#-% 3)0&/% &$., "), varargin &).50#($ /#%)/, %/%
.structure 9*2%$ ,$., "), &)$)40*4!" ,/)' .,).50#(, 9#&$ /#%)/! 3)"&, 9*2% 9#&% 3.#!)6#
.%"$ 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-! "#$%
-#0)-, &).50#( .2
,!"0, structure-, 9*2% 9#&% 413)0&/%, ;#!)6
find_zeros &).50#( .3
./"*% 2#+) 4!5, !/ 3)40%#'*", *(-%# *6"% feval-$ !)'* /#%)/
*)/) 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-! "#$%
,! #"*5/ &#).50#($ "!# ,).50#( ,&#" 9#&$ 5* 3)*+'#% *%#!1 ,3))!5#! 3, ,).50#( 9#&$ 3)0&/%
!1$ #1*2 &" �/! 8&)0# ,).50#( !1$ *+'#% )!$#!' ,0&/% &"7 &%#2! .Command Window-$ #"
.(#%.2 ,0&/%, &" "!" ,0&/%, !/ 5 5* ,0/% #0)" )!$#!' ,0&/% !/ 9*2 )#0)/ *%#!1) 3#5%
.global &).50#( .%"$ &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)"# �/! #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#%, &"
'%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-! "#$%
)/"*, 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… &)).(#"$
&).50#($ /%&/,! )+1 !/%! .$)&0 !1% toolbox-$ &#).50#(! ,/)' &*/("% toolbox &*).)
toolbox-$ 3)%/ 1"+$ .mean &).50#( &*+'#% ,$ ,)5)&! &/'! 9*#. 8)" &)4*+04-, mean
,)5)& !1! 8&#" 5)&2,! "!/ )+1 �#/ &#)5)&$ 3)".%0, 3)45)#*( *(-%! &#)/#%)/, &#).50#(
./+6%
122
Matlab-! "#$%
&#")'/ 8, runtime &#")'/ &"7 &%#2! .85&!# "#.%! (&)-6)) !5/ &#")'/ 8, syntax &#")'/
-$ 3))!5#!, 3)0&/%, !1# &5-(#% ,.*,, runtime &#")'/ &#!$5&% */"1 ,:-#0$ .*&#) %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 &)"*%!
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%$ , .,")'/,
;6!0 ,.*, &" *#.2! )+1 .*)$- 8%7$ 3))-& "! &)01#&,# script-, ;$#5 &" ;)*0 , 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%, &#*#/ #" )*
.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 &" �/! :"# 8#6$! 8&)0 &21 )1 $! 3)/0
*$12, &77, )"2 #" Array Editor 8#!6 .%"$ ,Command Window-$ 3)0&/%, &" 8#6$! 8&)0
)$'! 2+)% 3)0)).%, data tips 3)"*50, �#!6 #.#.) 91# Editor/Debugger 8#!6$ 3)0&/%, !2
.,0&/%,
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#%)- !/ %/%,
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/
"
"
"
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 *� 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)*(&$
,),) 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-! "#$%
128
Matlab-! "#$%
,,*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 #& !/ 5#$5$, )*"##.
.#!" &#).50#( :#52! 3)1*+ "#.%! &#-0! !#1) /%&/%,# ,.*,, 8%7 &"
&#"*#5 8,# 81&) ,!)2) ,*#.$ &#$#&1 8,)0( !2 8,/ #& 81!# &#$1/ *(-% /) 1"+$ +#5!
.3)$"/% $7$%/ ,1#%0 *&#) ,$1/$ &#).50#(!
&.*, !/ &#".#&, :*' 8!,! ,,/6%, 82%! .doc profile #$&1 ,7, )/#%)/, )!1, !2 *&#) +#%!! )+1
.3)-(" &").%! ,01#&,
,+#5,
'%2'
&#).50#(, +%.# ,cputime ,).50#(, .%"$ ,).50#( 6*1,$ "! "#,/ +#5 &.*, 8%7 +#+%! 3' 8&)0
.toc-# tic
129
Matlab-! "#$%
130
Matlab-! "#$%
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#( .%"$ #&#" #*%)/ ,:*' #*.) :*&#)$ 4#/( movie &).50#( !/ 8#)2*,
.movie &).50#( .%"$ 4*-, &" #').,# ,structure '#-% 4*- ,0&/%
4*- �$! )+1 (3#&1 2$.$ �%#-% &#(-#&,) 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#( .%"$ M-$ $.#% :*', ,,"!#!, !/ ,).*4)" !1 3#)-$ .:*' 44*#/%
Matlab-, 4*- &" *)%% script-, ;$#5 .script-, ;$#5! M &" ,*)76% "), ,,).50#(, &.*, 3#)-
,+#5, :-#0$ .Matlab-! ;#6% ,))(.! 8&)0, avi 4%*#(! M
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
133
Matlab-! "#$%
&" #"*5 ,5)%2% *&#) &#*1),! .)(*' 5/%% &*).) &#)*/(" &" ,*.5$ 5* *5#- ,7 5*(
.,7 5*( $&10 #)( !2/ buildgui.pdf
.%"$ 2.#$% )(*' 5/%% 8#01& .3)-toolbox-, &)$*%$ 3))(*' 3)5/%% Matlab-$ 3)%))5
.13.2 :*'$ *"#&%, 48)(*', 5/%% 8#01& 8#!6 &" &6&#(, guide &).50#(
.Show names in component palette &#*/("$ Preferences-$ *#6$! File 4)*(&$ 9)*. ,7 8#!6 !$5! )+148
134
Matlab-! "#$%
8&)0 Component Palette-,% .13.2 :*'$ 8#!6, &" &6&#( Command Window-$ guide &.*,
.)(*', 5/%%, 8#!6 &" ,##,) "#,/ "Layout Area"-1 13.2 :*'$ 8%#-%, 64/$ 3)45))$#" $).,!
135
Matlab-! "#$%
8#!6, $6#* &" �/! 8&)0 .45))$#" !1 !/ �#1&, &" �/! 8&)0 Property Inspector 8#!6$
&#)#.*, &#+)6), &" 2#$5! )+1 .position &0#1&$ height-# width �#1& && )#0)/ )"2 #%.2
.Units &0#1&$ 3)*6#$
!/ �#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#( .%"$ ,5)(*' 45))$#"
.%"$ #!" �#1& ,0/%# get &).50#( .%"$ ,5)(*' 45))$#" !/ �#1&, !1 &" ').%,
.set &).50#(
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 :*'$ &'.#% ,".#&,
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-! "#$%
)+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-, !/ �#1&, &" �/!
.13.7 :*'$ '.#%/ )(1 Inspector
137
Matlab-! "#$%
-! 3/, &" ,0/0 String ,0#1&$ .Surf-! *#&(1, 3/ &" �/! 3).#* #0" 13.3 :*'$ ,%1-, )(!
4-54, &"# 3)*�, 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
+#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$50 ,%// Callback ,).50#( && .%"$
.13.1 ,!$4 )(! &#)##&, &" ,0/0 "%'#+$
13.1 $,'-
-! name &0#1& &" ,0/0# :*', !1 &" 8%-0 .GUI-, !/ 3/, "), �/! )#.*/ &(-#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-,
&&
&#).50#(
$#5,
+#5(& &"
3)45))$#",
GUI-, !/
139
Matlab-! "#$%
:*', &*)%/ *6"! ./%&/%, )"2 !2(#% 3)"&%, 45))$#", */"1 .#$% Callback &#).50#( &&
.8&#" &01&! /)# )* #!" &#).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,
*.))0 ,7 9*2! 3"&,$ ,81! .popup-, 8#!6$ *6$ /%&/%,/ &#*/(", &" !)1% 'Value' 9*2,
.handles-$ *#%/0# $).0 ,3)"&% data
,+#5,
140
Matlab-! "#$%
8#). )"2 *.##)&/ Callback-, &).50#(! 3/ #0& .File-1 Label 8#). )"2 4)*(&, 3/ &" #*6$ &21
.Tag-,
141
Matlab-! "#$%
#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* *� ,*)%/ *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-! "#$%
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/
146
שבוע ראשון
בסיס מטלב
’)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(1,4)=5 הרחבה
=a 1 2 0 5
3 4 0 0
>>a=-3:3
=a -3 -2 -1 0 1 2 3
abs(a)>1
ð 1 1 0 0 0 1 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
מחרוזות
– 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)
שבוע שני
גרפים דו מימדיים
;)>>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צייר גרף ותמשיך לא מחכה לקומפילציה ניתן כך לראות עדכוני גרף -
דוגמאות
· ניוטון ראפסון
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 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
גזירה נומרית
פתרון מישדיף
– [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])
שבוע חמישי
;)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
)>>a=diff(data
=a
-0.7190
0.3757
-0.1209
0.4053
-0.1292
;)>>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
;)>>xi=linspace(0,4,30
;)>>yi=linspace(0,6,40
;)>>[xxi,yyi]=meshgrid(xi,yi
אינטרפולציה של כמה נקודות >>zzi=interp(x,y,z,xxi,yyi,’cubic’); -
פולינומים
x4-12x3+0x2+25x+116
ייצוג פולינום – ]>>p=[1 -12 0 25 116
;)x=linspace(-3,3,40
;]p=[1 3 5 5 3 1
ࢌሺ࢞ሻ ൌ െ
ሺ࢞ െ Ǥ ሻ Ǥ ሺ࢞ െ Ǥ ૢሻ Ǥ
)function y=humps(x
;y=1./((x-.3).^2+.01+1./((x-.9).^2+.04)-6
-----------------------------
מרחקים קטנים יותר מדוייק ;x=-1:0.2:2
;)y=humps(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])
!
"#
#*%)"*
-(.$# -* )%(&
+*
*#)
)"*--(.&
1
#&
*$"&'5,,3,4,12)"*#$'*#-(.
5,,,3,4,1,6,7,8,9,2.( )
$*-(.
.( )
%+"$*-(#$&.&
#(*
')-($#' )
&"&"(
:+#$"&'#
:+#$ $''&#.(&
*
%+) $#'$)'"&-&$#
$.*'&
"&"#$.&
;<=>?
@AB,
*+$.@AB%($##&
#"*;<=>?.
#%($##&
$##
#
C"&
?@#%(#+.*$
# #
:+#$"% -*#0$#
#C" #%(&(" '&$)$)$,#
C" #%(&(" *&$ -*
#&
*#
#*%
DD1E3
%"%#$
',#%(&("&*
DD1E3
8
"%#
:+#
',#%(&("&*
$
$'
!"#$%&!
#
'
'()*
!*# $%&!
$!*
##
''+!*#$!*
#
,
-
26./
2
-6
/3
1
50!--605 /436# 31
' 0-
.6
7 6
0268*
657
.2
'()!9**:*++#;
<
* =
-0 /2
7 6
9>?@
>*@
'266 = A-1
2 /3
'B%),!#
C(9!
5'#
A-/
/35D.-.236
'
= ./ 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-5H00500
0503
.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:
%,"*
**
#$ !"
*+,
%
!
&"
'(
)
'("
"
!
*+#,
"!
&"
20
(
(
"
&"
1
-(!
*'(",
!
&"
20
130+40 #
2+
567
44
*20,4
'
!
!(
'(
#'
'
#
'(!
(
-(!
"
'
"(
#
!
(
9:;7*86, 8)6'(
<*86,
.'"!
:56=*86,
#
'
(
5*,
(
5*#++,
!
(
5*##+,
!!
(
#
#
'
#
'('
1>
(
#
+!?
!
5
0
(+
('04 .
'"0
A'%
"
!
&@'
3
A5(
A%0/3
$
%
!
(?
"
@'
A
!(
%
"
('
B
!"
#$####%
&'(#)*+(%+%
!"
#,#,$#,#,%
-#*$.+%
!"
.#//
01!2$)+%
!"
####)####%
!
!
"!
#
$! %
"!
$! %$
"%!
45
334
43/3
6
*3
44
4>
?4
0!334
=<11(
;!
9:783
65
94 0!
A
4=6
4=0!633:@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
443
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
5D
*
+
(
,
*-
,
*$
,
$-+.(
*&/
<*
*
*
4F
5
$
#
012#/#&
/34#
&&&
G
HH
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
'$%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!.)!(7$08,!(') 6a
!65zeros!(&#A1)!*#,$*!$@?>!B!X!B !.)!;$%1<!"7$08,!(') 6b
!$@?> !E !;(!(.*!D#%-.<(!$0)$<!.-* !C !X !C !.)!"$'#%-.<!(7$08,!(') 6c
65eye!(&#A1)!*#,$*
&A A#
!(&$+$!"7$08,!<$( !A !0*<- !! $ ! !F (<)(!(0#7(, !C ! (7$08,!(') 6d
%B A "
!6 .!#"#<)!;$%1<!"7$08,!BG#!3!X!3!.)
!4H !D$) !;$,.* !;$$<0A<!;$01%,!;( !($0)< !.-* !9!X !9 !.) !(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!.)!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 !.)!;$<"!J0?,!(')!5=
!6"$'-#"(!)"#-!;*!F54I4>!0)$<
!!!01%,,!(01%!.$-,!#-#")!<"!.-* !M !J0#<)!(0#*!0#8A# !F54I3> !0)$<
!6"$'-#"(!)"#-!.*!"#(K(
!6E!X!E!.)!(&$+$!"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=
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=
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=
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 !(Q)!*#,$*!$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 !(')!#D-0,* !F !0@#8) !.26,!.*!;02!0$$< !ezplot !(Q)!*#,$*!$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$!"#',!($'*(#
!B5908,!;%#(G!"+9!@#@0*!"Q)!*#,$*!$76 B9
B5"$,"$02#.!$<+!(.8%)!2<(G!hold on!I!@#@0* !"#Q!$"*)!*#,$*!$76 B)
B5subplot!(Q)!*#,$*!$76G !"&01'!C$0$<!"-06,)!6$1#"!($<8'#1!.-*!=- B2
!"'!"9!$102!:1#9)!9<, !ginput !(Q)!*#,$*(# !zoom !6#< $)!$76 B&
B5('(!$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
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
:*+",&
!(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
!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 ('!.-.!#">2(.!&>#!0#&-(!">#'"
!t !0#:7#)!J#*60(!0)$6( !A"#0>( !5 ('!.-)!">8#,,(!"$>20(!"#0$(,(!"6
U U
!
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 =
ans =
6.9692 16.1137
:*+",&
"./+/,*!")!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<#!.*!#"#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)!.(!"7!%$1& "#!(.$.&!(&$+$!"@$0C,#!nXn !.)!(&$+$
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
:*+",&
:-.,&%,
:-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
:*+",&
%.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 =
:*+",&
:-."#!$%&/'$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
90 1 90 2
120 60 120 60
180 0 180 0
!
!
!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
:*+",&
.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
180 0 180 0
:*+",&
..&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/"	!8.!&/":*09!&7!9//19!-&/":*09!)1!'01!J"*/
:*+",&
:!"#!$%&'()'!$)*#+$!'()'
!&-+.,!/(0*
1 2 3 4 5 6 7 8
:%&"#!$%&,&-.!$*-$!'-
.!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
Columns 7 through 8
2154 2149
min_bet =
Columns 1 through 6
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
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
Columns 8 through 11
-0.5000 0 0 0
integ = ("#$%&
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
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
:*+",&
3()/34, 0 & x & 1 : *&+),(!"#$%&'!(-.(-/01%"2!()2(,.+ y % sin !3 # x " $ 4 # x :!"#$%&'!(!%&)%
4
1.465644569112382
sys2 =
1.463330831271255
sys3 =
1.463330827658138
2!"#$%!&'()
U
:*+",&
( n # cos!( n "
.n (",&"+&(*-.(/'76(1-$(!%).68()-,$6!(!"#$%&'(,&)8 %1 : !2&&.6!(!%&)%
(n ' 5
value =
2.6544 4.6331
value=funfun(3)
value =
value=funfun(7)
value =
:*+",&
: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 *,
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<&% !!!!!!!!!
!"#$%&'()*+,(!"&-"!.(/&%$
.'"$*! 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
$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
$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
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%)
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=
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=
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*
!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-(!@<&
!
!
!
!<&% !!!!!!!!
!"#$%&'()*+,(!"&-"!.(/&%$
.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
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&
!
!
!<&% !!!!!!!!!
!
!
!
!
!
!
!
!
!
!
!
!
!
!"#$%&'()*+,(!"&-"!.(/&%$
(""$*!(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=
c= maxi =
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 !"#$%
(%&'$)MATLAB !"#!$
()*+!
/)01 02*3
!,-,.+ !*,&
Matlab !"#$%
Ҝ҇҅҇ҙҖґ҇Ҏ҉
6+*. *,)/ •
Preferences •
Matlab !"#$%
(GLWRUґ҇Ҏ҉
Matlab !"#$%
ҌқҐ҆(GLWRUґ҇Ҏ҉
Editor •
0& &,+)4
(3.(
*+3'
$*,&
Matlab !"#$%
Path Browser ґ҇Ҏ҉
Set Path... <- File -' pathtool !"-02 •
!-+)-30)+" !-51*- !-')* !)12'%
ҜҖҋғ҇҆
ҏҋҚ҅Ҏ҇Җ
Ҏ҇ҖҍҙҋҎҙ
җ҃҇ҙҜ҉ҋҜҖҎ
Matlab !"#$%
:RUNVSDFHґ҇Ҏ҉
"(&$- .'
!"#!
#$%&
'("'%#
)*+,
Matlab !"#$%
/))*(#'05 /)6,&)4
» 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 !"#$%
Matlab !"#$%
/))*(#'05 /)6,&)40 !,5'2,-
% - indicates comments
%-- Matlab works in radians
Matlab !"#$%
% - 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 !"#$%
Matlab !"#$%
( %&'$) /)*+3' !2,7! 0& /)('*,+
Matlab !"#$%
Matlab !"#$%
Matlab !"#$%
Matlab !"#$%
A=[1,2,3;5,6,7]
Matlab !"#$%
!,7)*(' !*-2$0 !,)7.#,+
!,)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)
2 $-,'" 3 $*,&
)3*
+ ,
A(:, 2) & + ,
10
A(3,:) & / 9 6 7 12 0
+6,
++ ,,
- 15 .
Matlab !"#$%
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 !"#$%
Matlab !"#$%
!,)('!)*5 !,5'2,- - !,7)*(' !))70,+)#'
Matlab !"#$%
Matlab !"#$%
!,)('!)*5 !,5'2,- - !,7)*(' !))70,+)#'
Matlab !"#$%
M FILES
!"#$!% &'( 1
!"#$%&"'
!"#$!% &'( 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()*+) !"#$%&"'
.) ,#&!)* :-.-,#)
-" h) pyt !#$%&"'
"/0)& 0. (angle
1"/2#3-
!"#$!% &'( 4
2
(2()*+) !"#$%&"'
!"#$!% &'( 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
!"#$!% &'( 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;
!"#$!% &'( 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 !#$%&"'
2.0946 15
10
-5
-10
0 0.5 1 1.5 2 2.5 3
!"#$!% &'( 9
>> x=exroot(2)
x=
2.0946
!"#$!% &'( 10
5
DEBUGGER - !"0#4) #"'#&
!"#$!% &'( 11
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
!"#$!% &'( 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
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
!"#$!% &'( 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
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
• » 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
!"#$!% &'( 25
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#-%
!"#"$% #&'(&
)"*+&)% ,&)- !. %#/.%0 1)"*
av=[0 1 0 10];
axis(av)
!"#$!% &'( 27
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
15
(()*+) ,#.#4/!
)!1*"#+) 0( '!!1 .4 •
f(E) = (2*pi/(pi*kT)^1.5)*sqrt(E)*exp(-E/k)
!"#$!% &'( 31
(()*+) ,#.#4/!
!"#$!% &'( 32
16
&##'"$% &#($)#*+ ,-(."
Matlab /(#(!(
4 '!" #$!#%
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
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.
3
5. “ break “
The break statement lets you exit early from a for-loop or while-loop.
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
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
2
Convolution
Deconvolution
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
4
Polynomial Curve Fitting, Cont.
10
5
Partial Fraction Expansion
11
12
6
!"#""$% &$ '(%") *"(!+
!'"&#',)+'-
ODE
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
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
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);
5
Example 3:
Solution 3:
plot(t, y), axis([-.05 .55 -.05 1] ), xlabel('t'), ylabel('y(t)')
title('Solution to the stiff ODE')
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
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&
2
Matlab-! "#$%
&6#$5& $.,%
,$+& 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
3
Matlab-! "#$%
;$#3 *4#(*
7%#+%& &,95&
!-
'($3
&,95&
0'!'-%&
*!'% *"
1#/'.&
&,95& ;$#3
7%#+%&
4
Matlab-! "#$%
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
$''. 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)!
,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
&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#.
5
Matlab-! "#$%
.realmax -% !#64& 8,5$ #" +/"$ ,/+% *3#!.% !$3*% Inf 8,5&
I nf 0
. #" &3#!.&% !$3*% NaN 8,5&
Inf 0
!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% "'& &(',)%
0'%.#*% &(',)%& ',$'" .&-#*$ 0'-,5 *$(&# &(',)% *,'(' "'& Matlab-$ ,*#'$ *'+'+$& &!#5/&
.*#,#1& 7'$ 6',/% ";" 7%'+# &(',)%& !1 &,#1& ',$'" 7'$ 6',/% "," 7%'+ ,*#5$#,% 0'',4#+ 8#*$
,"$& 7/#"$ A &(',)%& *" ,'642 !1%!
.&2*1%& 4#+ *" Matlab-$ 1",% ,'64&! 8,#( 7'" '- $! 0'12
.&,#1$ ,$'" !- 7'$ .##, *%1& "'& &(',)% !1 &,#1& ',$'" 7'$ 6',/&! */+#2 8,6
,&6#3/& ,%#!-
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!
7
Matlab-! "#$%
:'+#2 &6#3/& :#+$ 7-!# 0'-,5& *$(& *!#5/ ,."! &(',)%& 7-#* *" *#",! &(,2 "! 0'$, 0',3%$
,&6#3/& ,%#!- .";"
.&(',)%& 7-#* *" &4'(% &22'" 8" A &(',)% &*#" *" *,(#'
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) &(',)%& '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!
.*#6.#'% *#(',)% *#,(#'& *#'(32#/ ,/+% *#%''3 *#(',)% !1 *'26'& &,64&! :+#2$
8
Matlab-! "#$%
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*'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'.'&
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-! "#$%
,&'&' *#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-! "#$%
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'"
&(',)%& !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-! "#$%
12
Matlab-! "#$%
7''(% 7#1",& ,$'"& .0'+362'"# *#!#45 0'',4#+ *#5(%"$ &(',)% 8#*$ 0',$'"&% 3!.! *14! 7*'2
.2&6#%5 ,/+% 7''(% '21& ,$'"&# &,#1 ,/+%
,0& &(',)%$ 0'+362'"&
,"%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 &)
*'(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-! "#$%
,*#(',)%! "%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
15
Matlab-! "#$%
*#5(%"$ 0',." 0')2%!" ,#.$! 7*'2 .+/"% 0'2#1& 0',$'" *"'(%! 3, *!$4#% &22'" find *'(32#/
,find-& *'(32#/ !1 0'',4#+$ '#(, '"2* *$'*-
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!
,' 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 &)
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
ans - "5
#"
. det(A) / 0 -1 '"2*$ 6
17
Matlab-! "#$%
.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%!
*#,5&
.*'2#$1.& &!#5/& ,#),/#" '2/! 03#%% &6#32& ,#),/#" -
.&2#1 "'& &6#32& ,#),/#" "!! 3#!'.&# !/-& *#5%1% *#'5#$', *#(',)% ,#$5 -
,#),/#" 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&
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
'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
' 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 *,
,)$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&
p # 24
.sum )'*+,$" )!$0" 9"$1! %%7 )$*'-.& !0 prod )'*+,$" )!$0" 9"$1
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)',
'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 *,
'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&
, 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&
6
Matlab-! 1$2&
' 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 *,
)'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)',
,12% 9"$12 \ -$.-"$1% )$0*&12 1'% !",% )$1$$3&% )#-0& )1 -$)"! %"'60 5-6
7
Matlab-! 1$2&
/',)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
+'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!
)$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-1 )"($% '"0 )7$-8&! )$-&$&% 3)$-"(% -"(&2 .$!3! /4 9)',
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 -)$'
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
!"#$%
..(+.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
/''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 ;($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%
11
Matlab-! 1$2&
,)'"-4 %38&%
)$!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&!
/&*0 /'6&&% )1 )$,3! $1 permute )'*+,$" )$0*&12 5-0& !3 /'6&&% -6( )1 )$,3! 9)',
.reshape )'*+,$" )$0)&12
-% )!$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 ,
+'-.
!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!
13
Matlab-! 1$2&
14
Matlab-! 1$2&
,;-4% )1 )-*''&
string 7
%,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&
,1&4$6!
./'.,&$4-1 6$0 )"($% '"0 /''6&&-2- /'1) '#-0& -$*'! 9)', $7 %'*+,$" )$0*&12
,1&4$6!
16
Matlab-! 1$2&
/'!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
.billing 135.00
120 39 78
.test 98 154 12
17
Matlab-! 1$2&
.()$63% !#! /'#-0 !'#&%) structure 5-0&2 -2'1 !#! )34! 9)',
,1&4$6!
18
Matlab-! 1$2&
.'$*-% %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-% '()*+,' -#).(/#*
').(/#* -.+' .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-! "#$%
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#(% (#+)
,3')/)$ 3)##( +#$)6 "!! -#&#(/ !1 3)*+, 00+1! 3)5+& )-1 -#%))(
.(+*' ;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
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
,3"$' 8*#"$ -"9 7.$! 8-)/ 3)+).' -#!#$,% (!6 (+ -#/1! #//#.+$ 3"
,3)+).' -#!#$, -" #'1!5 +#0(#$ $).'! '!)6-
,85%' +#0(#' -" axis-! +)96'!# -#)#.+' -#0/)&+#"#(' -" +#0(#$ -#/1!
.z +). -" 3#6-! )&5 z min , z max 3)/-1% )/1  -*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(#'
,&#(' "%,#&!
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
.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$.'#
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)$
,&#(' "%,#&!
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
.!,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
. r "#' #4#)&+# (a, b) '&#(/$ "#' !,7%' 95+% . 0 ' & ' 2( +1"5
,&#('
!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%  -#0+#*% axis -).(/#* !1 help-$ 5
7
Matlab-! "#$%
-1
-2
-3
-4 -2 0 2 4 6
-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
"
,&#(' "%,#&!
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
9
Matlab-! "#$%
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
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-! "#$%
%#$%
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#!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(##
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 !!,$
,&#('
12
Matlab-! "#$%
0
0 1 2 3 4 5 6
'/#1 -#&#(/ +*4% ,'/#1 3#6-$ 3)+). -5+7%$ ').(/#* !5 #/00+1 !"/' 2+,$ )5 $! 3)1/
.3)/#1 -#+#.# 3)7$.#
-%+#, 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$ $.%'
,&#(' "%,#&!
13
Matlab-! "#$%
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
14
Matlab-! "#$%
+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
.3)/#%)4 -*4#- "!! !"/' -#).(/#*' )-1 -" +"-% '9 2+, .5.11 2+, -" $)/%
0
-3 -2 -1 0 1 2 3
15
Matlab-! "#$%
$-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%
.xlabel -).(/#* !1 0!($ -9#+6% -$.' )"7 -7.#$% x +).! -+-#5 -*4#'
,"%,#&!
.ylabel -).(/#* !1 0!($ -9#+6% -$.' )"7 -7.#$% y +).! -+-#5 -*4#'
,"%,#&!
-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-! "#$%
17
Matlab-! "#$%
!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%  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
,&#(' "%,#&!
.+$& +)-4- "! "+(%' -$)-1 ;5 8#)!7 )95+%' (!6$ "+(%' -$)- -" $).%
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+,!
18
Matlab-! "#$%
!7 !"/' 7&)%' -" 00+1! )&5 .# 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
,'/)%) 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
.'/1% #/)" 8-$)-5 +&4# plot -).(/#* +6"! $-5)'! -#$))6 3)/#%)4' -*4#' -#&#(* !5
5
sin((4x)+45
y
4
0
-3 -2 -1 0 1 2 3
x
.)/&) 8*#"$ #/#1 "+(%' -$)-$ f (x) # sin( (x) % 4 ').(/#*' !1 3#%)45%# 3#%)/)% )5+7! 04(0' 9
19
Matlab-! "#$%
)#/)1! -#/-)/ 3))*+, 3)0()$#" !1 -#/#5-' .# 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
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
.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!
.# 8#/,4 ,#( )$#7 ,7$. #%5 3)$+ 3)+0%+*
20
Matlab-! "#$%
!5$ 0#0)1
0())$#"' ,#4 2+,' !1 3)0())$#"'
!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+,$ )#.+'
21
Matlab-! "#$%
;+&' .)#/)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()$#"
,&#(' "%,#&!
2
7
$x
6
y
sin( (4x)+4 5
4
0
-3 -2 -1 0 1 2 3
x
!"#$%
.set-# get -#&#(*$ 1#%)1 ")' +-#) -5$#4%' ;+&' -
'+97' :$#($ #//#$-' 3')-#/#5-# '()*+,' )0()$#" !5 !1 '%)1+ -#"+! )&5 -
.(html 0%+#*$) Handle Graphics Online Documentation
22
Matlab-! "#$%
-" "!%! )#.+ !"/' "%,#&$ !1%! .fill -).(/#* -#7.%"$ -#).(/#* )-1 8)$ 2+,$ +#9" "!%! 8-)/
.3#&" 7$.$ 12 ' x ' 2 +#9"$ '!#$+*'# 4#/)4' 8)$ +#9"'
,&#('
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
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-! "#$%
)+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+,!
.)#!-
,&#(' "%,#&!
40 1
20 0
0 -1
0 2 4 6 8 10 12 14 16 18 20
x
#" 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$.
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"'
&%.  '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 -).(/#* -#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
#
#
#
#
#
25
Matlab-! "#$%
0 0
-5 0 5 1 2 3 4
0 0 -48.2
-1 -1 -48.6
0 100 200 0 5 10 210 211 212
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#+) #%"$ .'+,- &,'5' 7.2.1
22 ............................................................ )*'+,- 0*''$#" !1 #7& &3'$*# &!$* 7.2.2
Matlab-! "#$%
#&#" '!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#) .!(#- #&#"% ',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#+ #%"$ &3$*6 z ,'5 !1 &'##&)# z ,'5 !'$1$ )06'(,"#* (#3 2'4#)! 9',5
,"%-#(
,(#*) .)'61) ('! &/" )!,'+4# )('%,'+ 00,16
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
3
Matlab-! "#$%
!1 '!!7 .#1', .surf -# mesh &#'5*6#+ :) .''(%% &!& .'/01% &,'5'! &#'4'4$) &#'5*6#+)
,#!" &#'5*6#+
-#
&'5*6#+ #%"$ x,y '(,#"#* ',#0*#% &#*+#%) '(,#"#*) ',0% :) X,Y ,1"7
.X,Y ',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
,(#*)
4
Matlab-! "#$%
15
10
z 5
0
4
0
3
-2 2
1
0
y -1
-4 -2
-3
x
15
10
z 5
0
4
0
3
-2 2
1
0
y -1
-4 -2
-3
x
!"#!
!7 ,#!" .''(%% &!& .'+,- &,'5' &#'5*6#+ #%"$ ,&#' *!/ /01% 00,1! #66#5,$ ." -
.)%'-( &#(#*6 ,&#' &+4#) #%"$ )'5#!#8,) &" :'0*)! "#) 
! #6'!31
5
Matlab-! "#$%
,,1"7
[n 31] ,#0*# - x
[m 31] ,#0*# - y
,"$) :+#"$ [m 3 n] !(#-$ X,Y ))8 !(#-$ ',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);
,',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 #%"$ ,$'"
,!'3' "! ("% :+#"$ ,$'" ,$'" Z &5',0% $#1'/ &1/%) :!)!
&" =! :&'6 Matlab-$ .view &'5*6#+ )6#1",) .3+$ )-5#) 6.3-# 6.2 2,- &,'5'! (#*$
.rotate3d &'5*6#+ #%"$ #" (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" #%"$ )'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
-# surfc &#'5*6#+$ :+'!/6 mesh-# surf &#'5*6#+ ,#$3 2,-! &/&% ,"&% '##* .- !$*! #66#5,$ ."
.(,"&% '##* )%1%1 contour )!'%) &" &-5'% c &#")) ."&)$ meshc
,(#*) "%-#(!
15
10
z 5
0
4
0
3
-2 2
1
0
y -1
-4 -2
-3
x
.az=-37.5 el=30 .) '(%% &!& 2,- !1 !(/%) &,',$ '7,3# az=0 el=90 .) '(%% #( 2,- !1 !(/%) &,',$ '7,3 3
7
Matlab-! "#$%
15
10
z 5
0
4
0
3
-2 2
1
0
y -1
-4 -2
-3
x
camlight &'5*6#+ #%"$ '#5, :##'7$ 4),#"& 2'4#)! .- :&'6 '4'+ 0*'$#" !1 )1#/& &,'5' :3%!
,!"6) &#,#1) &+4#) ,"%-#(! .lighting &'5*6#+ .3 ),#"&) -#4 &" ,#/$!#
8
Matlab-! "#$%
3#$*! .- :&'6 .)'5*6#+) !1 '4/') )$#-! ."&)$ .'6&1% )'5*6#+) !1 .'3$5) !(/% &,',$ ,#&$
,)'5*6#+) #%"$ )$#-$ &#!& "!! ('/" )')' 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+")%
9
Matlab-! "#$%
,waterfall-# meshz .1$ ,surf-# mesh &#'5*6#+! &#'5"',# &##)%) &#'5*6#+ '&1 (#3 &#%''*
..'6#1 03% .'+,- &#,5'%)
,(#*)
10
Matlab-! "#$%
15
10
z 5
0
4
0
3
-2 2
1
0
y -1
-4 -2
-3
x
15
10
z 5
0
4
0
3
-2 2
1
0
y -1
-4 -2
-3
x
11
Matlab-! "#$%
,(#*) "%-#(!
9,#5 :'" ." .,"&%) '##* !1 .'7,3) &" -'5% !"6) :+#"$ clabel &'5*6#+$ 1#%'1)
.!'-,) :+#"$ )'5*6#+$ .'1%&1% 8" #!" .'7,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 .'+,-) .'"!%
,(#*) "%-#(!
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
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
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
14
Matlab-! "#$%
,"%-#(!
. g(x, y) # x ' y ' 4 )!#$,+ (#3 6.3-# 6.2 *,+% "%-#() /01%! 2'4#6
2 2
,(#*)
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%,+ #%"$ 2,-$ .'/01%) !1 &#+'*1)
.&0!/#% &#+'*1 0-# ,&0!/#% &#%'0" -5''% 1 ,1"7 1-!
,"%-#(!
.($!$ :#'!3) /01%) &#+'*1 &" )616
,(#*)
alpha "2-"* 51 $)*%', $"%&%) 0%8% !"# .1 5%82,- "*6- 2)2", :6.15 !"#
16
Matlab-! "#$%
0%.$/1 !2%,& .7
.''+,- .'0*''$#" !1 #7&) .(#3# 04*0 ,.'##* ,.','5 !1 .'0*''$#" .'!'7% Matlab-$ .'+,-
.Handle Graphics #%"$ #" '6(' :+#"$ '#6'1! &'6
3'+#%1 '+7 (toolbar) )(#$3) !-,4 9,( #" Insert 0',+& 9,( )3'$*! &'6 5.3 *,+$ &#(#*+)
.7.1 2,-$
0*''$#"
:%#4%
8'8)! :&'6
04*0 !7
)5'/! '"3 !3 )5'/!
)3#$* ,#&+7)
)7'1%# !1 '6%')
!3 ,$73)
0*''$#"
:%#4%
&/&#+
0',+&
&#',1+"
:#!/ /&#+
#7& &7',3
%6)*%2 %*"# 9).8 :7.1 !"#
.Edit 0',+& 9,( '#6'1! &'6 .','5) 0*''$#"# 2,-) 0*''$#" !1 #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 :#!/$ .#7&) &7',3 :#!/ &" &/&#+ Properties !3 )5'/! ,0*''$#") #7& !7! &1-! '(7
0*''$#" !1 #7& &7',3 :#!/ -5#% 7.2 2,-$ .(#3# :#6-4 ,#* '$#3 ,3$5 #%7 .'$, .',0%,+ =!
.#*
17
Matlab-! "#$%
!7$ 0#0'1
0*''$#") -#4 2,-) !1 .'0*''$#")
!1 '6%') ,#&+7) !3 )5'/! ,)7',3) ;/ #%"$ #6#%'4 '"3 2,-) &" )6$% &" &'6(' =! :&'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 .',($ (%#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 #%"$ 2,-) !#+71 ,,$4#)1 '+7
.'61) 2,-$ '#5,)
18
Matlab-! "#$%
-GUI ,5#'# 3('% -'5% Matlab .&,83$ .''4'4$) 0#0,1) '$'7, .) Handle Graphics '0*''$#"
.0*''$#") !1 #7&) &" =! :&'6 #7,(1 handle ,1#*% )87 0*''$#" !7! ..'
,7.4 2,-$ ,"#&%7 "') Handle Graphics 0*''$#" !1 )'7,,'))
Root
Figure
'(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,-$
19
Matlab-! "#$%
,#1'% !3% )$#-7 )5',0% !1 06%!" !7 0#0,1 '"3 )5',0% !1 '(%%-&!& -#5'' Surface
.x-y
%&1% title, xlabel, ylabel #%7 &#'!3 &#'5*6#+ ..'##& #,/% Text
.#!" .'0*''$#"$
20
Matlab-! "#$%
0*''$#" ,5''! '(7 !1%! ..1 #&#" &!3$ 0*''$#") &,'5'! )'5*6#+ &%''* )*'+,- 0*''$#" -#4 !7!
%&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#+) #%"$ 2,- 0*''$#" &,'5'
handle = function('propertyname',propertyvalue,...)
,(#*) "%-#(!
7.7 !"#
,(#*) =,+
:#!/ '(%%# .#*'% &" &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 $*6 &#('/') .)*'+,-) :#!/ '(%% &" $#* 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) &" $#* Ytick-# Xtick #7&
.x-y-z ,#1'%$ /01%) $#* Zdatat ,Ydata ,XData #7&
!7 0#,'+ &#,1+"% .- :) 24#6$ .)*'+,- 0*''$#" !1 #7& &#,'8/%# $#* get-# set &#'5*6#+
.6#7& !1 .'',1+") .'7,3)
,)*'+,- 0*''$#" &6#7& !1 9,3 &3'$*! '4'4$) .#1',)
)6#7&$ $5#'1 9,3) &"8 &%#3! .#,/% ('%& .) PropertyName #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',)
,!$*6#
Handle Graphics Online Documentation ),83) ;$#*$ &%''* .)'#7&# )*'+,- !1 .'0*''$#") !7 !1 )%'1,6
.(html 0%,#+$)
22
Matlab-! "#$%
7.8 !"#
,(#*)
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
+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-! "#$%
*$-$ (#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,).($
'(.% (#!% .Matlab !3 (#)59++ ,.! "!" &+$ 3%(3+! +)2% !7$ )#1"3 '(.% (#!% ).1% (#%,,2
,'(.%+ (#!% &!+! .!#'7 *$-$ (#0%#1%
&(,0 #(#*-%"$3 Find & Replace &#!' (" ('(#. Ctrl+F !* +-,'! #" &%,1+ !* +-,'!
,/,##( 8,!'+!# "#-%!
3
Matlab-! "#$%
(,,-.#" (),'$ #" 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 -
+!#*. )#-,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
.&%1+ "-%0 &+$3 /,,)9#1+ &,$ "-%0+ 5#2+ !7 &#%,1 ",+ #4 +,-.#" (#*%3% 2
4
Matlab-! "#$%
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
9.2 +&./
isempty +,-20#.+ !3%! .is* [+% /,$-% (#25#$+ (#5#2. (#*-%"$ ,9#! ,"0( (#0$! /9 &(,0
.+2,) +-,)6% ",+ +-,)6% /" (25#$
,(#"%9#5
,,"0(+
.0 ;)* $,0%
,,"0(+
.1 ;)* $,0%
5
Matlab-! "#$%
6
Matlab-! "#$%
if !"2"0) 9.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%!
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%!
%#$%
8
Matlab-! "#$%
.((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!
9
Matlab-! "#$%
(#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-! "#$%
/,%*. !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+ ")+ )$,"! 5* )#62#+ ,)$," (" /71% "$+ 5#2+
21.,( "! /!#*! +"!#!+ #$3 $-% &7(, ()'" ,#+3,(% /,,2(, +),-*+ ,"0( ,7 $! /,3! $#3'
.Ctrl+Break #" Ctrl+C !* +-,'! ,"* (*-#$% +%#,1 ,0.! +-)+ (),-* .+%-*$
11
Matlab-! "#$%
,(,!!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,)+ (#*%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-! "#$%
.1250," &%1% i )3"7 (#-,)6%# /,)#62# ;#($ /,7)* /,$,-% (#$) /,(*! for-# while (#"!#!$
,(#"!#! (#*-%"$ +-,)6%$ /,)$," $,-+! (#,)2,* /,7)5 3#!3 (#%,,2
13
Matlab-! "#$%
./,7)*+ /,$-#% +,!" +-,)6%+ !5#9 +,+, +% 3")% *#5, "!3 (#',0%# (#!#23 !"0+ /,7)5+ ,(3
(" !'("! &(,0 &7! .x ,)#2%+ )#62#+ ;)#"! ++4 (#,+! ;,)- x_abs )#62#+ ;)#" ,7 )#)$ #4 "%9#5$
,"$+ &.#"$ #075*!# /,1." )#62#7 x_abs
+$,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
)$," (" (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+ !* 9!5! /,-#) #0"3 5#*$ (j) (#5#%*+ !* +-)3 ("!#! ;#($ (%2#%% *#-,%+ ("##3% (*7
,(#5#%*+ ("!#!% ("-! ,57 break (5#2.$ 3%(30 ;7 /3! .(#,0#-,2+
16
Matlab-! "#$%
/" 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-* ."(-
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
2
Matlab-* ."(-
.5#$%&"'* 1#.2"% 1!",$-.(+ 1#&!+-5 !"-+* 1#57 !"#5* 1#(##8 1&#. 5#$%&"'( 1#&!+-5 !"-+
,!(#!) *+-*
,1"+#25 5+,-*
3
Matlab-* ."(-
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
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")#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*
,& 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 !.
,1"+2& .4-* 2 /#( ."5 5*+ 6*%( /#%!5 1#&!+-5 2'0- #) ,(%&
.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-* ."(-
("!)* 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"�)
,#**) 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#
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%##(". .2
help-( !"62"'-5 !"(#0- :. function handle-( +-!+5* 1"%-( !7"28- 2"!( 5#$%&"'5 1+ !. ("!)* 14 /!#& 3
.function handle-( +-!+5* 9#3,
7
Matlab-* ."(-
"-) 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
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#*&. /'".(
fminbnd !#$%&"' !",$-.( #2-"& /'".( 1"-#&#-5 !3"%& 28. +'8& /)*" 3.- 5+% "7 5.""+- !2#!'
,3"%5 .
!"#!
,*+-* .5*+ 1##24"0( !.$-&5 !7"28- *) !,$(-5 eval 1+( 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
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-!- !"#$%&"' #!+ !"&"!&
feval-( +-!+5* #3) .5%"#3( 1#6&-"42. 4 1#(##8 0" !#$%&"' 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
10
Matlab-* ."(-
varargin ,&-%*(.
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&"!&
,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#&!+-
11
Matlab-* ."(-
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"� !#$%&"' 2"(, 1#43&" !#**) 5#$%&"' *+ 1#0'.5 !.
.script ;("% .1
.0" !#$%&"' .2
.find_zeros_varargin !#$%&"' .3
.1
*+ 6*%( 1#6&-"42.5
:2,-( "($"#+ 5#$%&"'5
varargin 1#.!5
.2
12
Matlab-* ."(-
.3
13
Matlab-* ."(-
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
,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-* ."(-
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" !#$%&"' .2
.find_zeros !#$%&"' .3
.1
.2
15
Matlab-* ."(-
.3
16
Matlab-* ."(-
0" !#$%&"' .2
,*"&5 structure-5 :2,- :"!- 61#&!+-5 ;"*#8
find_zeros !#$%&"' .3
.+.2- ,"3# 6*%5 *+ 1#6&-"42.5 2'0-" 28.- feval-( *#42 +"-#+
2#+# /'".( structure-5- 1#&!+-5 #)2, !. (#$5* /!#&" #82)5 "&&#. structure-5 :2,- :"!- 1#&!+-5 ;"*#8 (*+ 6
.53+5 1+" 53"%& /"#$ 1,
17
Matlab-* ."(-
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 !.
!"#!
./"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-* ."(-
#+.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… !##$'".(
!#$%&"'( +-!+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 #"+*
!"#$%& '4 runtime !"#$%& !#8 !*"-1 .'(!1" #")*1 (!$36$) 1(& !"#$%& '4 syntax !"#$%&
-+ ,$$1("14 ,$5!&*4 17" !(3/"* 4).44 runtime !"#$%& !"1+(!* . ,;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
!"#$
!".$&$ -$%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 !"."& "#
.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
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 . 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&
!
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$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 #"+*
7
Matlab-1 #"+*
,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
8
Matlab-1 #"+*
9
Matlab-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 $!& !"*$$(
. 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
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
12
Matlab-1 #"+*
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"/
.Show names in component palette !".&/#+ Preferences-+ ."6+1 File 2$./!+ 9$.) 48 '"16 1+(1 $075
13
Matlab-1 #"+*
'!$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
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"/
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
'"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 #"+*
$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
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
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+
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
!!
!"$)(5"/
!"-+"(4
0"(/! !#
,$2($$+"#4
GUI-4 1&
18
Matlab-1 #"+*
;.%4 !.$*& .6#1 .&*!&*4 $"- 1-/"* ,$#!*4 2($$+"#4 . !"-)"+* 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
.)$$5 48 9.-1 ,#!4+ ,'71 .popup-4 '"164 4+ .6+ &*!&*4& !".&/#4 !# 1$7* 'Value' 9.-4
.handles-+ ."*&5" +$)5 ,,$#!* data
,0"(4
19
Matlab-1 #"+*
'"$) $"- .)""$!& Callback-4 !$)(5"/1 ,& "5! .File-7 Label '"$) $"- 2$./!4 ,& !# ".6+ !-7
.Tag-4
20
Matlab-1 #"+*
"-+$( '/"# "!"#+ .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. *%."
Matlab !"#$
*'+% – 1 '$% &'()!
:0*&%2! %/2*$
/*1' ./
.*%)'.%0/ .*(*&, .1
5%$.2' .4
syntax-* !1%.3 6*$4" .#
build-in .*%7+$*, .!
!#*1&! .1%1"* !$3*.( !"%$3 .*
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*#
» 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
» a=10;
» c
c =
18
» 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 !(%-.'
.12-'! .0%7&( /(* inf !/7*.( 5*04. 0-1 !+*(- , :*"$%/ = inf
.12-'( 03*'! 0.*%1 6)+! 0,"'! = eps
."+%0" ,0,"' 5%%+ /( = NaN
» who
a b c
a 1 by 1 8 double array
b 1 by 1 8 double array
c 1 by 1 8 double array
» 4*...
ans =
20
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
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 =
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
y =
y =
x =
-2.6000
y1 =
-3
y2 =
-2
y3 =
-3
y4 =
-2
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
!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
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*.
A=5; !%"&'!
B=5;
keyboard
C=A+B
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»
help - 1)-.
.!#*+,! 52* help .2+! %"& !#*+, (& 01"! (1+( 6.%$
» help sqrt
» lookfor complex
CONJComplex conjugate.
. . . . .
. . . . .
.'*3* 013& ,5%)%0,. – %.*#%#% 2'.2' +2''1 ,.$**+' !0;& 6*(- .-%., – helpwin
Matlab !"#$
!'*+*),% !'*)',-' !'&'./ – 2 '$% &'()!
!'+*),%
,130+( +/ &2(( – &0!#( 5+.% )3 .MATLAB-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%('#& !
» 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
Columns 8 through 11
» y=sin(x)
y =
Columns 1 through 7
Columns 8 through 11
-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
» x=[0:0.1:1]*pi; % or …
» x=[linspace(0,1,11)]*pi;
» x(3)
ans =
0.6283
» x(1:5)
ans =
» x(3:-1:1)
ans =
0.6283 0.3142 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
a =
» a=[1 2 3 4].'
a =
1
362-1-2023 Matlab !"#$
-3-
2
4&$+(7) +3!"& 4&13+0( 4&0!#( ,206" &%!) "$+,% /)) transpose 5( 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 =
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( 2+(&2
:4&(/+. .+7&0*(
» c=a*b'
c =
11
a =
1 2
3 4
» a+2
ans =
3 4
5 6
» 2*a-1
ans =
1 3
5 7
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
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
c =
1 3
4 6
7 9
ans =
1 4 7 3 6 9
c =
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
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&(&&,("
)+$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
» v=[1 1];
» I=diag(v)
I =
1 0
0 1
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
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»¼
Ax = b
²¦¨ §£²£¥¨³ £®¥ ´°£²¢¨ ´¦®¥¨ ´°££¨ ²±¨ ¦®¥ ´¦¬®
©¨£«MATLAB´ª¨«¨§£¥²¬¨´¦®¥¨¦£ª´£²£ª£¦
§ £¡£ ©²´® §££± x = A -1 b £¢£ £¬ ©´ª ´³¨ ´¥²¬¨¦ ©²´®
¥£®°£²¢¨£A°£²¢¨
D:\Tal\MATLAB COURSES\Matlab - B 05\Class_3__new.DOC
A°£²¢¨¦³¢ªª£¨²¢det(A)%
¥£®°£²¢¨£«®¨ª³¢ªª£¨²¢´¦¬´£¬£²°£²¢¨
» 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=[ 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 ];
-- Matlabʺʰʣʱ
´°£²¢¨¦¬´£°±ª®¦¢
-- 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´²¨±£²°£²¢¨
¨¦(
¨¦(
» 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ʺʰʣʱ
´¡£¨´°£²¢¨´²³±´£°±ª®¦¢
-- Matlabʺʰʣʱ
§££¨¨²§£¥²¬¨
¦¡¦§££¨¨§££¨¨¡§£¥²¬¨³¨£³²³®MATLAB¦³´¨±´«²
¨¦§£¨¨
«¨£¦¬§£¥²¬¨§££¨¨²§£¥²¬¨³¨´³¦©´£ªMATLAB - 5´«²¨
²«´°£²¢¨§¬¬¦´¬£¨´£²£ª£¦²¦´±®§£¨¨²§£¥²¬¨³¨£³
§£¨¨§£¥²¬¨´´¦£¨¨²¤²¬¨´±²®¦²³® ²±¨
£²¨¡¦¥©£²¦±«©£ ¦¬®¬°´£¨¨²¤²¬¨²¦±«©£¦¬®¦¬®´§
¤²¬¨
§£¥²³¦³´£²³®£¨¨²¤²¬¨´²£°£
§£«±ª£³¨£³£¬ x
£¨¨²¤²¬¨´¥²cat±®³¨£³ x
§£¨¨§£¥²¬¨³¦³¨´¥³³¦³¦¬£¨¨´¦´¤²¬¨´²£°£¨¦(
» 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¦£¥¦´££¡AA°£²¢¨¦³§£¨¨£ª£³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³¡¨´²¥
«³´£
§£°±
MATLAB£°±¦³§£«£ª³§£¨££±M-FILES¦¬««¨MATLAB´ª¥´
script´£ª¥´£°±
§§£«£ª³©£§££²±£¬§£¦
¢¦®¥§£¢ª¨²§£²£ ¡¨¢¦±¥§£¢ª¨²§£¦±¨£°±ª®£°± 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£°±ª®³¨£³³¬ª²³¥ ²³£°£MATLABH1help 1´²³´²±ª£ª³²³
§£«£ª³¨§£°±¬£®¦¦¥£ ²³¨¦³°£¡¨¦help² ¬´³±´¨²³¥
help ´¨££«¨ ±® ¦¬ ² ¬ ³±´´³¥ H1 ´²³ §¬ ¡£ °£ ´ ´²¬ ´²³
function_name
£°±ª®´£ª¥´¬£®£´²¬´²³²¡¦
D:\Tal\MATLAB COURSES\Matlab - B 05\Class_4_new.DOC
§££¦¦¥§£ª´³¨´²–global%
²£¦£°²§££¨±¨¦§££¦¦§£ª´³¨¥§£²¥¨££ global±®²¡¦¬£®£²³§£ª´³¨
´¦´£´³¨´³¦³£–´£ª¥´´²£©¬¨¦´£ª¥´´¦£¡´§££¦¦§£ª´³¨´
% 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
§££¦§£²¢²® x
²´ ²¢²®
£¦AND
£¦OR _
£¦NOT a
£¦EXCLUSIVE OR xor
´¨¬¦¬¦¬®²¢²®°£²¢¨«®¨§£ª³²¢±£²£¦¥§²£ ¡¨ all
«®¨ª³²¢±²£§££±§²£ ¡¨ any
-- Matlabʺʰʣʱ
¨(
-- Matlabʺʰʣʱ
¨£² ´²±
´£ª¥´´±®§² ´£ª¦´³¨³¨´±®©control flow¨£² ´²±
for´¦¦
£¦¦¥ª¨
for index = start : increment : end
…statements…
end
¬£«®¦³¦¡¨´²£²¤²¬increment¦³´¬£«®end¤²¬¦start¤²¬¨§±´£indexª´³¨
¨(
A¤²¬¨¦³´¨¬£¬°¨¨´´³¡¨mloop.m´£ª¥´
-- Matlabʺʰʣʱ
´®«ª§£¥²£´³¬°¦(MATLAB©´£ª´¨±¨´
¨(
» M=sum(A)/m
M =
11 7 10 9
M =
11 7 10 9
©¥¦§£¥²¬¨¦¬´¦¬®¬°¦MATLAB¦³´¦¥£´´¦°ª¨©³§³¨´°¦¨¨¦´²°&
¦¦¦³´£«¡£¤²°£²©¨ ´´¥«¡
´°£²£¥¤´§£ªª§£¥²¬¨¦³³²¨²£©¨ ©¥«£¡¤²°¦´°¦¨¨´®«ª¦¬®&
£¦¨£«±¨£®«§¦§£«®£¥²¬¨¥¦¦
-- Matlabʺʰʣʱ
´£ª´¨¦¦
while ´¦¦
£¦¦¥ª¨
while expression
…statements…
end
´¨£¢£¦³expression¬¦¥´¬°¨¦¦
´£ª¥´´°²
» 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¦´¥££³´±®´²«¬°´§££±´¨ª£§££¢£¨¡§
¡ª£elseifelse³¨£³elseif«¨§£¢£²®«¨³¨´³¦©´£ª
if a < 0.1
disp('range = [0-0.1)')
disp('range = [0.1-0.9)')
else
disp('range = [0.9-1]')
end
´£ª¥´¢¦®
» arand
a =
0.8913
range = [0.1-0.9)
-- Matlabʺʰʣʱ
eps2.meps¤²¬³£¡¦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
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 or 2 or 3
2 or 4
1 or 2 or 3
2 or 4
end
¬§£´¨case¨´£ª¥´¥£³¨¨switch´±®²¡¦©C©¥´®³switch ¨ª³&
¦¦¨´°£´£ª¥´§£´¨case²¡¦MATLAB¦¦«break´±®¦
-- Matlabʺʰʣʱ
´£¨£ª®´£°±ª®
¯± ³² ¬£®´ ´£³² £°±ª® ¡ ¯± ´£°±ª® ²®«¨ ²£¦ ©´£ª MATLAB ¯±
¯± ´ ´²¨ ´£°±ª® ¯± ¤³¨ ª¬®´ ´²¡ ´£°±ª® ¯± §³ ´ ³£´
§£ª´³¨¥ ² §£ª´³¨ § ¦ ´²¡ ´£°±ª® ¦³ §££¨£ª® §£ª´³¨¦ ´ª®¦ ´¦¥£ ©ª£
§££¦¦
²« £°±ª®MATLAB³®¡¨£³¦¥£°±ª®¦£ª®
´£¨£ª®£°±ª® 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.m10*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/
Matlab !"#$
5 '$% &'()!
!,#%% -'# *+,-)(
» 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)
» 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. ("$! !#*+,!
» 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;
» legend('sin(x)','cos(x)','cos(x)^2');
:(1+.%2 )(,!
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
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);
» subplot(2,2,2);
» subplot(2,2,3);
» subplot(2,2,4);
» figure(1)
» figure(3)
.8%%+ 013 /*!* !#%'1 x 0,"' :*(- .56!( .'0*5 figure(x) !#*+,!
.%-3*$! %,05! :*(-! ./ 0*5". :*(-! 0,"' :*%6 /(( close !#*+,!
.(,85'"* 2'&8* – % !),)3) n )-$% 2'&8 ,'+," clf(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
» x=[4 6 8 9]
x =
4 6 8 9
» y=[6 7 10 11]
y =
6 7 10 11
» line(x,y)
:(1+.'! )(,!
» fill(x,y,'g')
:(1+.'! )(,!
» r=linspace(1,30,20);
» teta=linspace(0,2*pi,20);
» polar(teta,r,'b')
:(1+.'! )(,!
:(1+.'! )(,!
-' 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
» 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');
» y=[1:2:40];
» x=rand(size(y))*100;
» errorbar(y,y.^2,x,2.*x,'k');
:(1+.'! )(,!
(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!'
» subplot(2,1,1)
» compass(z,'r')'
» title('compass');
» subplot(2,1,2)
» feather(z,'g');
» title('feather');
:(1+.'! )(,!
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
%$'! )*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'
-#+ 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
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 ]
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 !
» figure(1)
» gcf
ans =
1
» set(gcf,'Color','b')
).(-)/* '"+( "-#$ 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! ")$*" !
.:,$%
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],...
::( (movies &)-)23! ")$*" *"/) %*6$*.+ &*.()& %+'*& *00( 43)+!
'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% )*!+"$)
:&).)'&*
%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$*.+ &(*'/ !
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)
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)
Matlab !"#$
6 '$% &'()!
!,#%%-!&! *+,-)(
-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
» 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 !#*+,!
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
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
z
y
Elevation(theta)
x
Azimuth(phi)
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 ');
:!'.'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
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
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 -
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
» 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
-% 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
(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
(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 :&*.
(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');
(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]
(12.13) /%('# !
>> strcmp('fd', 'fda')
ans =
» T=fix(clock)
T =
1998 1
25 19
32 16
» t=now
t =
7.2978e+005
» date
ans =
25-Jan-1998
» 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
")'-% :$9% :(0) ,%.)($ &3"! !"#$% :)'(*9! %')$" %*72.)3% plot %-)23! :)"+'% ")$*"% *'#+
.%!'%! :12 %*."% 5/3! 8'6 1)1'"0
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