Professional Documents
Culture Documents
/
>> d = 15;
>> disp (d);
15
:(2)
>> a = 'ali';
>> disp (a);
ali
:(3)
1
MATLAB
/
:msgbox -
>> msgbox ('ok', 'result')
[1\
/,
('*+ ?@ A,@ ) S HW M,
]
Y?U
(ok) N
?1 (result) /,"
R4-O ? M, 6 E+ N@ 96O
P%
-Q4
:fprintf ! -
:(1)
>> x=15000;
>> y=12;
>> fprintf('Numer = %e',x)
Numer = 1.500000e+04
>> fprintf('Numer = %e',y)
Numer = 1.200000e+01
:"
2
MATLAB
/
3
"
#"!$ ^>>>>>>4-
3
"
J4
F @+K
1 K]
3
"
M!%
P@- J4
. (True/False)T] 17 _\ M84 ?
`Ia
,E7
,\5
K]
3!"
1
'-G>>>>> -b '7 / @+K
3
1 K]
-"
LE L
"% ? M!%
.
-H "%
+H @+K
3
1 K]
-"
LE 2- /,641 ; ^False -G\
-H "41 True ,
.False 9E = %&' "
1 True 9E = $ "
',5% K] 3,G\ =
K]
,G\
@, =64 ; ^4"
3,G\
= c c ,@ K]
3,G\
-H "%1
.4"
-"
= c KI $ I ?
3,G\
?C @, K4-W 0G
"
Relational Operators : &' ()*+* :#$
:?
18
? E+
1 "XU
@+K
3+O 9; @+K
3!" =d %
0'5, *+, /
= -.7 <
'14 17 -.7 <=
= -H;7 >
'14 17 -H;7 >=
(= = *@ ?6
) 1
+O ==
1
# +O ~=
= @+K
17 ^J85
0G@
= ,G>>>>>\ = @+K
@+K
3!" #$ >>>>>I =64 :(1)
/,6%1 ^-G
"
L ,G>>>>>>\
= ->>>>>>\ 9; @+K
5
fN J %1 -G 1 ,G>>>>>>\
:?
F
SH4 ; @+K g% ?
,G\
J8< 0G 8%
,G\
>> a = 1; b = 5;
>> x = a > b
x=
0
>> A = 1: 9, B = 9 - A :
A= = -H;7 ? ?
A = ->>"
@E17 K
1 2 3 4 5 6 7 8 9 ? 8%
,G\
? +G& -Q%1 ^4
B= -Q4 ^A i 4 ->>>>>"
LC,
8 7 6 5 4 3 2 1 0 .A > 4 1 JC-
>> tf = A > 4
tf =
0 0 0 0 1 1 1 1 1
>> tf = (A == B) '1>>>>>>>% ?
A ->>>>>> 84 J% K
tf = 0 0 0 0 0 0 0 0 0 .B ,G\
? -"
3
MATLAB
/
:67 "
"
"%1 =4-. @+K (==) #,K4 l< ^c G
$ c `>>>>O /"% (==) 1 (=) =%+>>>>O / k<&
"
2- >>>I (=) #$ >>>% ^=41>>> @,64 J
B c-G>>>1 =41>>> @; B <1
.-.
?) c+G>>>1 (Y = -H;7 X
< ?) 3<1 ->>> K] 4<7 ,G>>>\
,
:(1) F
.(Y '1% 17 = -.7 X
<
>> X= [10 17 22 0 7 3 2];
>> Y= 7;
>> y = (X > Y)
y=
1 1 1 0 0 0 0
?) c+G>>>1 (Y = -H;7 X
< ?) ->>>"
0G@ ->>> 4<7 ,G>>>\
,
:(2) F
.(Y '1% 17 = -.7 X
<
>> z = X.* (X>Y)
z=
10 17 22 0 0 0 0
"
Logical Operators: ('- *.+) '- ()*+ :%
K]
3!"
?
18
-Q41 ^@+K
-"% ?G@ 17 P
K4-W K]
3!"
-,%
:M!%
.
? ,E,
0'5, 8+9;, /
,
(1) AND &
(17) OR |
(?G@) NOT ~
:(1)
>> a = 1;
>> b = 5;
>> x = a ~= b
x=
1
:(2)
4
MATLAB
/
tf = .3<,
1 +G& LC, H I
1 1 1 1 0 0 0 0 0
>> tf = (A > 2) & (A < 6) /,64 <1 "
+H"
fN "% l<
tf = .6 = 9C1 2 = -H;7 A = -\"
0 0 1 1 1 0 0 0 0
"
:*+ '2
$ :1%
^9"
KH>>>IZ r
,K
= ,8
c >>> -H"% C 84q M!%
P@- #,K4
3B 3!"
JK%1 ^@
KH>>>>>IZ 3B 3!"
9HC
"
KH>>>>>IZ 3B 3!"
>>>>>5%1
"4 ?
9"
KH>>I7 ,C ?
18
s->>U41 .=
+>>
= 41>>
KH>>IZ
.P-H
+< =5>?
/
,
Z ( ) t,CZ
(.^ ^^) ,K
^(')+1
(~) ?G
+O
(./ ^/) K
^(.* ^* ) M-d
(-) s-]
1 ^(+) L8
(:) =%"
= ]K
9"
-H;7 ^(>) = -H;7 ^(<=)'14 17 -.71 ^(<) = -.7
(~=) 1
# ^(==) 1
^(>=) '14 17 =
AND (&)?K]
L8
@Z OR (|) ?K]
9"
5
MATLAB
/
"
:IF-ELSE-END 45 :+3&
V"4& N NG41 .?W->>O +H = P%@ 2-
c >>I -17 = ,8 M>><
2 5@ C
:?
4 ;1 if-else-end .\
#$ I -H M!%
.
?
:IF-END -1
if expression = >>>"C,
(commands) -1Z N>>>G >>>>>>I1
(commands) -H"
>>>C g>>>@>>>; B end 1 if =%+>>>H"
end .true /,6% (expression)
:(1)
>> x = 10;
>> if x == 10
disp ('ok')
end;
:8 F if-else-end @A, B<A>C DE *, EF G$ :IF-ELSE-END -2
if expression < ? -1Z =
1Z ,8
NG >>I l<
(commands evaluated if True) >> ^true >>>K
expression -H"
y>>>
else -H"
y>>>
B >>>@>>>F
>>>,8
N>>>G%
(commands evaluated if False) .false K
expression
end
:8,>, /OP, if-else-end %<>, HI>?C DJ K
L;M N*F G$ -3
if expression1
(commands evaluated if expression1 is true)
elseif expression2
(commands evaluated if expression2 is true)
elseif expression3
(commands evaluated if expression3 is true)
elseif expression4
(commands evaluated if expression4 is true)
.
.
else
(commands evaluated if no other expression is true)
End
:(2)
>> x = 10;
>> if x == 10
msgbox ('ok', 'result');
6
MATLAB
/
:(3)
>> if x == 10
msgbox ('ok', 'result');
else
msgbox ('no', 'result');
end;
:(4)
>> x = 11;
>> if x == 1
disp ('1');
elseif x == 2
disp ('2');
else
disp ('3');
end;
%Q
3
:
1
P%@ 84 P@- ; :(5)
7
MATLAB
/
else
z=sqrt(x)+sqrt(y)
w=log(x)-3*log(y)
end
"
: SWITCH-CASE-OTHERWISE 45 :
*
9>>
= ^ R>>I,
?; + & +-6 #$ >>I
c -17 NG%
E, 4
:
"
.\
?
switch-case .\
#$ I
switch expression .4*+
I 17 c-G c 7 expression /,64 /7 84
case test-expression1 >>K>>>>>>>>
>>.>>>>>>\
? ,E,
expression -H"
/+>>K4
(commands1) .
1Z case +>>H ? ,E,
test-expression1-H"
>>
case test-expression2 (commands1) -1Z NG% J >>>>>>I ^/-H"
z1>>>>>>>% B1
(commands2) J
B >>7 .end +>>H"
< >>>>" >>"C,
3>>
"
?]$%1
otherwise l>>>< ^?@>>>F
W->>>>>>>U
-H $>>>>>>> ^1Z W->>>>>>>U
pK5 4
(commands3) test- 3+H"
L p>>>>>>>
F
? expression /+K>>>>>>I
end z1>>>>>>>% B1 .@F
case +H ? ,E,
exoression2
< 3+H"
K 9%1 (commands2) NG% J I ^/-H"
-H"
L 1>>>>>>>
case +>>H '7 pK5% J
B .end +>>H
?
% ?
(commands3) -1Z NG >>>>>I ^expression
.otherwise +H"
-1Z 3,8 z<7 NG% J >>>>I S@T switch-case .>>>> = f@+17 'N
s->>>>U
= k<&
:
FZ y
1 switch-case .\
@,6
:(1)
x = 1;
switch x
case {1, 2, 3, 4, 5}
disp ('1..5');
case {9, 10}
disp ('9..10');
otherwise
disp ('this is impossible');
end;
1..5
:(2)
clc;
clear;
8
MATLAB
/
n = 3;
switch n
case {0}
m = n + 3;
case {2}
m = 'ali';
case {3}
m = magic (n);
otherwise
disp ('error');
end;
disp (m);
2-
8 1 6
3 5 7
4 9 2
:(3)
x = 2.7;
units = 'm';
switch units
case {'inch', 'in'}
y = x * 2.54;
case {'meter', 'm'}
y = x / 100;
case {'feed', 'ft'}
y = x * 2.54 / 12;
case {'millimeter', 'mm'}
y = x * 10;
case {'centimeter', 'cm'}
y = x;
otherwise
disp (['Unknown Units:' units]);
end;
2-
y = 0.027
9
MATLAB
/
&6 7& !
S T8M$S%O>,$SE$,S/7SU
S
5V
S!,S@,S%C5!
{,]$1{3-
{={="{"
{-1Z{={ ,8{N G%{ q {for{3K
<{#,K%STforS : ! XW
S }?
4{;{for|{K
5
{"
{.\
{]"%1{^"
for i = x1: x3: x2 {?%+H{={"C,
{(commands)-1Z{NG%{"4{l<
(commands) {x2{>>X>>
{>>K
{
{ x1 >>X>> &{>>K
{={ end1{ for
end; { }?
{F
{?{;{x3{+K{4*1
TZWYS
{ <{,E,
{+H"
{C{M<{84{5{
{1{={n
{C{9;{9E{=STK$,S[HMS%?&!SUO$
{ <{N61{n = 2{@F
{+1
{?{/,6%1{^n = 1{
1Z{+1
{?{n{C{/,6%{^
{end{+H
{ n = 5{
{9\%
>> for n=1:5
x(n)=sin(n/10)
end
x=
0.0998
x=
0.0998 0.1987
x=
0.0998 0.1987 0.2955
x=
0.0998 0.1987 0.2955 0.3894
x=
0.0998 0.1987 0.2955 0.3894 0.4794
{ {T
{
{ 4{+-6
{.\{&{/{,{for|{+-6
{.\{&1{' &{&{={[-G
N61{z-Z{H41{C{gHF4{-{9;{K<! {3+1
T]>
SSTZ\YS
>> x=1:5;
>> y=sin(x)
y=
0.8415 0.9093 0.1411 -0.7568 -0.9589
S TZxYS,_SZforYS%O>,S@A_SSTZ^YS
1
MATLAB
/
end
y=
0.8415
y=
0.8415 0.9093
y=
0.8415 0.9093 0.1411
y=
0.8415 0.9093 0.1411 -0.7568
y=
0.8415 0.9093 0.1411 -0.7568 -0.9589
a+CSZforYS%O>,S@A_SSTZ`YS
2
MATLAB
/
TZfYS
>> for i = 1: 10 >> for i = 0:2:10 >> for i = 10: -2: 1
{{{{{{disp (i); {{{{{{{{{{disp (i); disp (i);
end; end; end;
{ 2- 2- { 2-
1 0 10
2 2 8
3 4 4
. 6 2
10 8
10
TFactorial 10 (10!)S7STZgYS
F=1;
for k=1:10;
F=F*k;
end
disp(F)
2-
S }?
{F
{?{;{^
{for{3K
<{{YU@{=64{TZhYS
3
MATLAB
/
17 20 25 32 41
26 29 34 41 50
S T%j,S$7S
<kSTZiYS
A= 5 10 15 A= 0 5 10 A= 5 5 5
20 25 30 15 20 25 5 5 5
35 40 45 30 35 40 5 5 5
50 55 60 45 50 55 5 5 5
>> s=0 {{s=0 s=0
for r=1:4 for r=1:4 for r=1:4
for c=1:3 for c=1:3 for c=1:3
s=s+5; A(r,c)=s; d=s+5;
A(r,c)=s; s=s+5; A(r,c)=d;
end end end
end end end
disp(A) disp(A) disp(A)
A= A= A=
5 10 15 0 5 10 5 5 5
20 25 30 15 20 25 5 5 5
35 40 45 30 35 40 5 5 5
50 55 60 45 50 55 5 5 5
A = 4 8 12 A= 4 8 12 16 20 A= 3 5 7
10 14 18 18 22 26 30 34 9 11 13
16 20 24 32 36 40 44 48 B= 4 6 8
22 26 30 46 50 54 58 62 10 12 14
s=0 s=0; s=3;
for r=1:4 for r=1:4 for i=1:2;
for c=1:3 for c=1:5 for j=1:3;