Вы находитесь на странице: 1из 19

5

MATLAB







&&&& ,
MATLAB , &
.
2
MATLAB, (+), (), (*), (/)
(^). 4 MATLAB &
, (.*, ./, .\ .^). ,
MATLAB &&&& , &
, &
, , . &
.

, + .
.+ . MATLAB .

, &
, &&&& .
, .

, MATLAB
. ( )
, .

MATLAB.



MATLAB &
. . 5.1 MATLAB &
.
5.1. MATLAB

plus

uplus

minus

uminus
mtimes

.*

times

mpower

.^

power

/
\


( )

mrdivide
mldivide

./

rdivide

.\

ldivide


( &
). , X Y .
>> X=[0 1 2; 5 3 6]
X =
0
1
2
5
3
6
>> Y=[9 8 4; 3 1 1]
Y =
9
8
4
3
1
1
plus .
>> plus(X,Y)
ans =
9
9
6
8
4
7
times &
.
>> times(X,Y)
ans =
0
8
8
15
3
6

5. MATLAB

93

X Y,
mtimes. , &
.
>> mtimes(X,Y')
ans =
16
3
93
24
power , &
.
>> power(X,Y)
ans =
0
1
16
125
3
6
, &
uminus.
>> uminus(Y)
ans =
-9
-8
-4
-3
-1
-1
rdivide X &
Y.
>> rdivide(X,Y)
ans =
0
0.1250
0.5000
1.6667
3.0000
6.0000
/ \ ( mrdivide mldivide) MATLAB &
. 6,
.

1, MATLAB
(double) :
(single), .
,
.
,
. MATLAB 9, .


, &
, . &
. &
. 5.2.
5.2.

==

eq

~=

ne

<

lt

94

II. MATLAB

. 5.2

>

gt

<=

le

>=

ge

. MATLAB
(1), &&&& (0). , &
MATLAB, &
.
.
>> x=1; y=2; z=3;
>> ((x+y)==z)+(y<z)+(x<=y)
ans =
3
, :
eq((x+y),z)+lt(y,z)+le(x,y).

: (x+y)==z, y<z x<=y. x=1, y=2


z=3 , ..
1. ans ,
, .. 3 (1+1+1).
, , &
.
, &
. , .
>> x+y==z+y<z+x<=y
ans =
1
,
, &
.
>> A=[1 0 3; -2 5 -6];
>> B=[8 -9 1; 7 2 2];
>> A>B
ans =
0
1
1
0
1
0
, , MATLAB
. &
,
>> X=3; Y=[2 4 7; 3 8 1]; X<Y

>> X=3*ones(2,3); Y=[2 4 7; 3 8 1]; X<Y


:
ans =
0
1
1
0
1
0

5. MATLAB

95

,
.

. <, >, <= >=
, == ~=
, .


&
.
MATLAB . 5.3.
5.3.

&

and

or

not

xor

(.. ), &
&&&& ().

,
, .
, &
, &&&& , . 1,
, 0, .
1, . 1
, , ,
0. , 1, &
, 0 .
. 5.4, &
0 1.
5.4.

x&y
and(x,y)

x|y
or(x,y)

~x
not(x)

xor(x,y)

.
>> A=[2 5; 0 3];
>> B=[7 1; -4 0];
>> A&B
ans =
1
1

96

II. MATLAB

0
0
>> A|B
ans =
1
1
1
1
>> xor(A,B)
ans =
0
0
1
1
>> ~A
ans =
0
0
1
0
. 5.3, MATLAB
:
any 1, ;
, , &
;
all 1, ; &
, , &
.
.
>> C=[1 0 3; 7 0 0; 2 0 10]
C =
1
0
3
7
0
0
2
0
10
>> any(C)
ans =
1
0
1
>> all(C)
ans =
1
0
0


, , &
.
.
MATLAB &
.
1. ().
2. (.'), ('), &
(^), (.^).
3. (+), (), (~).
4. (.*, ./, .\, *, /, \).
5. (+ ).
6. (<, <=, >, >=, ==, ~=).
7. (&).
8. (|).

5. MATLAB

97


2 ,
MATLAB. , ,
, ,
.
&
, . , &
, &
.
MATLAB &
.



MATLAB, (. 5.5). &
, &&&& . ,
, &&&& . &
. , ,
, d ( degree &&&& ),
, , .
5.5. MATLAB

sin(x), sind(x)

cos(x), cosd(x)

tan(x), tand(x)

cot(x), cotd(x)

sec(x), secd(x)

csc(x), cscd(x)

asin(x), asind(x)

( [1,1]
/2 /2,
)

acos(x), acosd(x)

( [1,1]
0 ),
)

atan(x), atand(x)

(
/2 /2)

acot(x), acotd(x)

asec(x), asecd(x)

acsc(x), acscd(x)

sinh(x)

cosh(x)

tanh(x)

coth(x)

sech(x)

csch(x)

98

II. MATLAB

. 5.5

asinh(x)

acosh(x)

atanh(x)

acoth(x)

asech(x)

acsch(x)

.
>> z=[0.5 1 2.5]
z =
0.5000
1.0000
2.5000
>> cos(z)
ans =
0.8776
0.5403
-0.8011
>> sec(z)
ans =
1.1395
1.8508
-1.2482
>> atan(z)
ans =
0.4636
0.7854
1.1903
>> sinh(z)
ans =
0.5211
1.1752
6.0502
>> asech(z)
ans =
1.3170
0
0 + 1.1593i


MATLAB (. 5.6).
5.6.

exp(x)

log(x)

log10(x)

log2(x)

pow2(x)

2 x

sqrt(x)

nextpow2(x)

, 2, ,
; x , ,
nextpow2(length(x))

expm1(x)

exp(x)-1 x

log1p(x)

log(1+x) x

nthroot(x, n)

n x
( x n , n )

5. MATLAB

99

, . 5.6.
>> a=[1 4.5 3 8]
a =
1.0000
4.5000
3.0000
8.0000
>> exp(a)
ans =
1.0e+003 *
0.0027
0.0900
0.0201
2.9810
>> log10(a)
ans =
0
0.6532
0.4771
0.9031
>> pow2(a)
ans =
2.0000
22.6274
8.0000 256.0000
>> nextpow2(a)
ans =
2


MATLAB ,
. 5.7.
5.7.

abs(x)

angle(x)

x ( , )

conj(x)

, - x

imag(x)

real(x)

complex(A,B)

A+Bi
(A B )

, .
>> z=[5+7i, 2i, 9-4i];
>> abs(z)
ans =
8.6023
2.0000
9.8489
>> angle(z)
ans =
0.9505
1.5708
-0.4182
>> conj(z)
ans =
5.0000 - 7.0000i
0 - 2.0000i
9.0000 + 4.0000i
>> real(z)
ans =
5
0
9
>> imag(z)
ans =
7
2
-4

100

II. MATLAB


. 5.8 .
5.8.

fix(x)

floor(x)

ceil(x)

round(x)

mod(x)

rem(x)

sign(x)

.
>> b=[1.95 8.17 -4.2];
>> fix(b)
ans =
1
8
-4
>> floor(b)
ans =
1
8
-5
>> ceil(b)
ans =
2
9
-4
>> round(b)
ans =
2
8
-4
>> mod(b,2)
ans =
1.9500
0.1700
1.8000
>> rem(b,3)
ans =
1.9500
2.1700
-1.2000
>> sign(b)
ans =
1
1
-1


MATLAB ,
. , , ,
.
MATLAB :
''; ( &
&&&& , 1& 0000 ); &
[ ].
&
. MATLAB . &
. , 73 1871
17 2003 , 37 745,75 , &
18 17 2003 .

5. MATLAB

101

,
:
datenum ;
datestr ;
datevec ,
, .
>> datenum('17-Oct-2005')
ans =
732602
>> datestr(732602.75)
ans =
17-Oct-2005 18:00:00
>> datevec(732602.75)
ans =
2005
10
17
18
0
0
datestr ,
( 1, '').
help datestr,
.

clock, , [
].
>> a=clock
a =
1.0e+003 *
2.0050
0.0040
0.0040
0.0140
0
0.0353
, clock &
fix.
>> a=fix(clock)
a =
2005
4
4
14
0
57
a , &
( , 2005), &&&& (), &&&& (4), &
, , (14:00:57).
now, , , ..
, , &&&& .
>> format long;
>> now
ans =
7.324065858320834e+005
, date.
>> date
ans =
04-Apr-2005
calendar &
.
>> calendar
Apr 2005
S
M
Tu
W
Th
F
S
0
0
0
0
0
1
2

102

II. MATLAB

3
10
17
24
0

4
11
18
25
0

5
12
19
26
0

6
13
20
27
0

7
14
21
28
0

8
15
22
29
0

9
16
23
30
0

& ,
calendar ( , &&&& &
). , 2005 .
>> calendar(2005,3)
Mar 2005
S
M
Tu
W
Th
F
S
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
MATLAB tic toc, &
.
:
tic, , toc
&&&& , &
, ( ).
tic , toc . &
,
, .
tic toc , &
.
, ,
cputime, etime, weekday eomday, , MATLAB.


MATLAB , &
.
,
dec2bin.
>> dec2bin(9)
ans =
1001
bin2dec ( ,
, ).
>> bin2dec('1001')
ans =
9
dec2hex.
>> dec2hex(216)
ans =
D8

5. MATLAB

103

hex2dec.
>> hex2dec('D8')
ans =
216
, &
. , ,
.
>> X=[12 35 8; 31 5 2];
>> dec2hex(X)
ans =
0C
1F
23
05
08
02
bitmax ( ) &
. , &
. IEEE& 2531.
>> bitmax
ans =
9.0072e+015
bitand
( ), bitor ( ) bitxor ( ). &
,
( , &
. 5.4). .
, bitmax.
x=55 y=25.
>> x=55; y=25;
>> bitand(x,y)
ans =
17
>> bitor(x,y)
ans =
63
>> bitxor(x,y)
ans =
46
, &
, dec2bin.
, MATLAB , &
(0 1). bitget
bitset. , 4& 358 (
101100110), .
>> bitget(358,4)
ans =
0
52.
4& 358, .

104

II. MATLAB

>> bitset(358,4,1)
ans =
366
366,

101101110.


, MATLAB &
, .
&
.
,
. ,
, .
,
MATLAB.
, (airy) & &
.

d 2w
zw = 0
dz 2
MATLAB :
w=airy(z) &&&& ;
w=airy(k,z) &&&& k: k=1
; k=2 ;
k=3 ;
[w,ierr]=airy(k,z) &&&&
(ierr=0 &&&& ;
ierr=1 &&&& ; ierr=2 &&&& ,
Inf; ierr=3 &&&& ; ierr=4 &&&&
, z ; ierr=5 &&&&
, NaN).
.
>> k=2; z=96;
>> [f,ierr]=airy(k,z)
f =
3.8786e+271
ierr =
0

z2

d2y
dy
+ z + ( z2 v2 ) y = 0
dx 2
dz

v. , .
MATLAB .
besselj(nu,z) . nu ,
, z .

5. MATLAB

105

z nu ,
. &&&& , &
&&&& , ,
, . &&&& &,
&&&& &, .
bessely(nu,z) .
besselh(nu,k,z) , ,
. k: k=1 &
, k=2 .
,
(. &
airy).
.
>> N=[0 1; 2 3]; Z=[2+3i, 6; 5-i, 27];
>> [f,ierr]=besselj(N,Z)
f =
-0.4695 - 4.3138i -0.2767
0.0304 + 0.3929i -0.1459
ierr =
0
0
0
0
ierr , , &
.
MATLAB . 5.9.
5.9. MATLAB

besseli

besselk

beta

betainc

betaln

gamma

gammainc

gammaln

ellipj

ellipke

expint

erf

erfc

erfcx

erfinv

factorial

106

II. MATLAB

. 5.9

gcd

lcm

legendre

rat


MATLAB
( )
, doc ,
.


MATLAB
(. 5.10). ,
MATLAB.
5.10.

()

[]

{}

..

...

( ),
( )


( MATLAB )

'

( ) ; ,
, ,
ASCII-

[,]

[;]

5. MATLAB

107

. 5.10 :
colon (:), paren ((), [], {}), punct (., .., ..., ,, ;, %, !, =), ctranspose ('), horzcat ([,])
vertcat ([;]). ,
MATLAB , help
doc, .


MATLAB, . &
, .
&&&& &
. , , &
, , &
MATLAB .
:
MATLAB &&&&
;
;
MATLAB (1), &&&&
(0);
( )
;
MATLAB : , &
.
MATLAB, &
.

.
1. .+ .:
) ;
) ;
) MATLAB .
2. :
) ;
) .
) .
3. :
) ;
) .

108

II. MATLAB

4. ,
MATLAB, :
) ;
) .
5. 1 :
) ;
) ;
) , .
6. :
) ( )
, ;
) ( )
, ;
) , &
.
7. clock:
) ;
) ;
) .
8. :
) datenum;
) datestr;
) datevec.
9. :
) bitget;
) bitset;
) setbit.
10. besselh :
) ;
) ;
) .

1. : (+),
(^), (|), (./),
(), (<), (~).
2. (a*b~=c)+(a>=c)+(a^c<=b), &
.
, a b &
.

5. MATLAB

109

3.
, . &
, .
4. , &
.
5. , 39
20. , &
.
6. nu=0, nu=1,
nu=2, nu=3 nu=4 0 20 0,01.

110

II. MATLAB