You are on page 1of 224

MATLAB

2007

MATLAB

Copyright 2007, . , .

MATLAB


,
. ..
,
,


. ,
,

. ,

.
,
. ,

,
,
.
,
,
,
. ,

MATLAB

.
,
.

MATLAB, Mathematica.
MATLAB.

MATLAB.
,
,
- ,
MATLAB.
,
.
,
. .
,
.

MATLAB,
,
.

,
.
. , ,
,
.

, .

MATLAB,

, ,
.
,
MATLAB ,
,

.
,
, Fourier.
, 2007

MATLAB

MATLAB ............................................................... 3
............................................................................................ 3
....................................................................................... 7
1........................................................................................... 13
................................................................................ 13
1.1 ................................................................................... 13
1.2 ......................................................................... 15
1.2.1 ............................................................ 15
1.2.2 - .................................. 18
1.2.3 ............................................................... 19
1.3 .................................... 20
1.3.1 - ................ 21
1.4 ........................................................... 23
1.4.1 .......................................................... 26
1.5 .............................................................. 27
1.6 ....................................................................... 28
1.7 - ................................................................. 29
1.7.1 echo ......................................................................... 31
1.7.2 ....................................................... 31
1.8 ................................................ 32
1.9 ................................................................... 33
2........................................................................................... 36
......................................................................................................... 36
2.1 .......................................................................... 36

MATLAB
2.1.1 ............................................37
2.1.2 ........................................................38
2.2 ..........................................39
2.2.1 a_initial:a_step:a_final.............................................40
2.2.2 linspace(a1, a2, a3) ..................................................40
2.2.3 zeros(n, m) zeros(n)..........................................41
2.2.4 ones(n, m) ones(n)............................................41
2.2.5 eye(n) .......................................................................42
2.2.6 diag(a) ......................................................................42
2.2.7 magic(n) ...................................................................43
2.2.8 rand(n, m) rand(n) ............................................43
2.2.9 randn(n, m) randn(n) ........................................44
2.3 ......................................................................45
2.3.1 ...............................45
2.3.2 - ..................47
2.3.3 ...................48
2.3.4 ...........................48
2.4 .................................................................50
2.5 .......................................................................52
2.5.1 ...........................................58
2.6 ...................................................................59
3 ...........................................................................................64
...........................................................................................64
3.1 .......................................................64
3.2 ........................................................65
4 ...........................................................................................68
.....................................................................................68
4.1 : if.............................................................68
4.2 - .........................................71
4.2.1 For............................................................................71
4.2.2 While........................................................................75
4.3 ...........................................78

4.3.1 continue ................................................................... 79


4.3.2 break ........................................................................ 80
4.4 ....................................................................... 81
4.4.1 ....................................................... 82
4.4.2 .......................................................... 83
4.4.3 ....................................................... 84
5........................................................................................... 86
.............................................................................................. 86
5.1 ....................................................................... 86
5.1.1 .......................................... 87
5.1.2 .................................................... 91
5.2 .................................................................. 94
5.2.1 ......................................... 95
5.2.2 - ........................................................ 95
5.2.3 ............................................... 96
5.2.4 o................................................................. 97
5.3 ................................................................................. 98
5.3.1 ......................... 98
5.3.2 ...................................... 100
5.3.3 ............... 102
6......................................................................................... 105
............................................................................. 105
6.1 - plot....................... 105
6.1.1 .................................. 107
6.1.2 scatter .................................................................... 116
6.1.3 stem ....................................................................... 117
6.2 ............................................................... 118
6.2.1 hold on................................................................... 120
6.3 .................................................................. 122
6.4 ezplot................. 123
6.5 ................................... 126
6.6 .......................................................................... 127

10

MATLAB

6.7 ..........................129
6.8 ................................................................132
6.8.1 polar ...................132
6.8.2 ezpolar..............133
6.9 - .............134
6.9.1 ...................134
6.9.2 .................135
7 .........................................................................................138
MATLAB.......................................................................138
7.1 ..............................................138
7.1.1 (Command Window).....................138
7.1.2 (Workspace) ..........................139
7.1.3 (Current directory) .......139
7.1.4 (Command History)..........140
7.2 ...................141
7.3 (workspace) ............................142
7.4 ASCII............................144
7.5 input pause ...........................................................145
7.6 ................................................................147
7.7 ........................................148
8 .........................................................................................150
..................................................................................150
8.1 ...........................................150
8.1.1 ..........................................................151
8.2 ..........................................153
8.3 mesh ............................153
8.3.1 meshc .....................................................................155
8.4 surf ..............................156
8.4.1 surfc .......................................................................158
8.4.2 surfl ........................................................................159
8.5 ...........................................................160
8.5.1 .....................................160

11

8.5.2 .................................. 161


8.6 .............................................. 162
8.6.1 ................................................ 163
8.6.2 ................................ 164
8.7 .................... 165
8.8 ........................................... 166
8.8.1 ezplot3 ................................................................... 166
8.8.2 ezmesh ................................................................... 167
8.8.3 ezsurf ..................................................................... 168
8.8.4 ezcontour ............................................................... 169
9......................................................................................... 172
- ................................................................ 172
9.1 ..................... 173
9.2 ................................................................... 175
9.3 ................................................ 185
9.4 ........................................ 188
10....................................................................................... 191
MATLAB ...................................................................... 191
10.1 ............................................................................ 191
10.1.1 .............................................. 191
10.1.2 .................................................... 192
10.1.3 ..................................................... 193
10.1.4 ............................................................ 193
10.1.5 .................................... 195
10.1.6 .................. 195
10.1.7 .......................... 196
10.2 .............................................. 198
10.3 ............................................ 200
10.3.1 ............................................................... 200
10.3.2 ................................................................. 201
10.4 ...................................................... 202
10.4.1 .................. 202

12

MATLAB

10.4.2 .....................203
10.5 (Interpolation) .........................204
10.5.1 ................................208
10.5.2 ...............209
10.6 Fourier .....................................211
10.6.1 ...............................213
10.6.2 .........................................................................214
10.7 ..........................216
...........................................................................................217
................................................................................................219
........................................................................................219

13

1.1
,
,
.
,

.
,
,
,
.

,
,
. ,

. , .

MATLAB, Mathematica.
MATLAB
Mathematica

14

. ,
, ,

, ,
.

MATLAB,
.
WINDOWS.
MATLAB
.
MATLAB. MATLAB

.
(Command Window) MATLAB.
,
(Command Window) MATLAB. ,
, ,
,
.
(Workspace) MATLAB.

(Workspace).
,
, .
:
, , File
Exit ,
Ctrl Q .

1.2

15

1.2
MATLAB, ()
,
, :

+
*
/\
^
=

==
<
<=
>
>=
~=


,
. (,
, ==).
,
.
1.2.1
,
, >> ,
.
,
,
1.2,
Enter , ..
>> 2+7 Enter

16

ans =
9

(answer)
.

>> 8-4
ans =
4
>> 3*62
ans =
186
>> 24/7
ans =
3.4286
>> 2^3.1
ans =
8.5742
>> 231/27.5
ans =
8.4000
>> (2+3.7/6.5)/(3^3.6+1/3)
ans =
0.0489

MATLAB \ ,
,
,

1.2

17

>> (3^3.6+1/3)\(2+3.7/6.5)
ans =
0.0489

,
/ , \ ,
, 9.

MATLAB
, .
,
, .
,
, ,
,
,
.

MATLAB
. , 1 ,
i , j , ..
>> (2.8+5.9*i)/(37+71.2i)
ans =
0.0813 + 0.0029i
>> (2.8+5.9*j)/(37+71.2j)
ans =
0.0813 + 0.0029i
>> (2+5*i)^5/(3+11*i)^3
ans =
-1.3757 + 2.7283i

18

:
(1) ,
MATLAB
, .
(2) ,
, ..
>> (2+3.7/6.5)/(3^3.6+1/3
??? (2+3.7/6.5)/(3^3.6+1/3
|
Error: Expression or statement
unbalanced (, {, or [.

is

incorrect--possibly

(3)
(Command History) ,

, .
,
.
(4) MATLAB 7
, , ,
.
1.2.2 -
.
,
( ),
( ).
.
:

1.2

19

>> 7>3
ans =
1
>> 7<=4
ans =
0
>> 2^3==8
ans =
1
>> 3^2~=9
ans =
0

1.2.3


; ,
,
, , ..
>> (2.6+1/3.3)*(37+i*82.6)/(23/47+7.3*i/73.9);


, ..
.
: , , ,
, >>, .

20

1.3
MATLAB ,
,
. :
(1) ,  3.14159... .
pi,
. ,
pi ,
..
pi^2
ans =
9.8696
pi*2.5^2
ans =
19.6350

(2) ,
inf.
1/inf
ans =
0
1/0
Warning: Divide by zero.
ans =
Inf

(3) NaN (Not a Number), ,


.. 0/0, /,
0/0
Warning: Divide by zero.

1.3

21

ans =
NaN
inf/inf
ans =
NaN

,
MATLAB
. ,
, .
,
(1) realmax
MATLAB ,
realmax
ans =
1.7977e+308

e+308 (exponent) ,
e + 308 10308 .

(2) realmin
MATLAB ,
realmin
ans =
2.2251e-308

1.3.1 -
MATLAB, ,
.
,
, , , =,
name = value

22

, name,
,
. , .
value,
( ). , .
3.
, ,
,
. , ,

.

x=2.1, y=7/3
x =
2.1000
y =
2.3333
z=x*y
z =
4.9000
x*y*sin(z)
ans =
-4.8140

x y,
z,
.

1.4

23

1.4
, MATLAB
,
. MATLAB,
,
, , ..
function(argument)

function,
argument.
MATLAB,
:
sqrt(x)

log(x)

(
x)

exp(x)
cos(x), sin(x), tan(x)
cosd(x), sind(x), tand(x)
acos(x), asin(x), atan(x)
acosd(x), asind(x), atand(x)
cosh(x), sinh(x), tanh(x)
cosh(x), asinh(x), atanh(x)
real(z), imag(z)
angle(z)
abs(z)
conj(z)

, e x

( )

( )

( )

( )


, z

()

24

max([x, y, ])
min([x, y, ])
mod(x, y)
rand, randn
eps(x)


[x,
y, ]
[x,
y, ]

x y.
.

x.


sqrt(2)
ans =
1.4142
sin(3.1)
ans =
0.0416
acos(0.71)
ans =
0.7813
tan(pi/4)
ans =
1.0000
exp(1)
ans =
2.7183
mod(23,5)
ans =
3

1.4

25

rand
rand ,
[0, 1],
. ,
, ..
rand
ans =
0.9501
rand
ans =
0.2311
rand
ans =
0.6068

randn
randn ,

(0, 1),
.
, ..
randn
ans =
-1.6656
randn
ans =
0.1253
randn
ans =
0.2877

26

eps(x)
eps(x)
x , ..
eps(1)
ans =
2.2204e-016
eps(realmax)
ans =
1.9958e+292
eps(realmin)
ans =
4.9407e-324
eps(0)
ans =
4.9407e-324

1.4.1
)
MATLAB
. .
d (degree),
.
sin(pi/7)
ans =
0.4339
cos(2*pi/7)
ans =
0.6235
tand(45)

1.5

27

ans =
1.0000
sind(135)
ans =
0.7071

)
, MATLAB, 7,
,
( ),
MATLAB , .. Log(12.5)
log(12.5),
,
Log(2.2)
??? Undefined function or
arguments of type 'double'.

method

'Log'

for

input

log(2.2)
ans =
0.7885

1.5

(workspace),

.

.
,
clear
clear variable
clear global

workspace

global

28

clear function
clear all



,
.

1.6
,
.
,
.
,
.
1)
,
,
, ..
(2+sqrt(11+3/7))...
/(3+sin(21/85))
ans =
1.6584
(2+sqrt(11+3/7))/(3+sin(21/85))
ans =
1.6584

2)

, .
, , ; .

1.7 -

29

o ,
; , ..
2/3,4^2,1/6
ans =
0.6667
ans =
16
ans =
0.1667
2/3;4^2;1/6;

1.7 -

,
MATLAB. ,
MATLAB
, .
, ,
,
.
,
,
.
(script files),
.m, -
.
-

(
). MATLAB,

30

. ,

, ,

,

MATLAB.
-
ASCII (editor), .. MATLAB,
FileNewM-file.
, ,
MATLAB.

MATLAB, ,
.

- ,
example1.m,
, .

,
velocity=50;
travelingtime=60;
distance=velocity*travelingtime;
distance

,
,
,
>> example1
distance=

1.7 -

31

3000

1.7.1 echo
, -

. echo on,
-,
, .. ,
>> echo on
>> example1
velocity=50;
travelingtime=60;
distance=velocity*travelingtime;
disp('distance= '),disp(distance)
distance=
3000
>> echo off

echo off,
.
1.7.2
- (cells)
%%,

.
, (editor) MATLAB,
.
,
.

1.7, ,

32

%%
echo on
velocity=50;
travelingtime=60;
%%
distance=velocity*travelingtime;
disp('distance= '),disp(distance)
echo off
%%

1.8
,

, .
,
.

2,

a=sqrt(2)
a =
1.4142

[1] ceil(a), a
.
ceil(a)
ans =
2

[2] floor(a), a
.

1.9

33

floor(a)
ans =
1

[3] fix(a), a
.
fix(a)
ans =
1

[4] round(a), .
round(a)
ans =
1

[5] chop(a, n), a n


.
chop(a,3)
ans =
1.4100

1.9
:
,

help name

name
, ..
help log

34

LOG
Natural logarithm.
LOG(X) is the natural logarithm of the elements of X.
Complex results are produced if X is not positive.

MATLAB
. ,
F1.
,
.
: ,

. .
%
( )
,
,
% This is an M-file, with the name example1.m,
% for the calculation of the traveled distance.
%
velocity=50;
travelingtime=60;
distance=velocity*travelingtime;
disp('distance= '),disp(distance)

,
,
%
.
%{ ,
, %}
, , ..
,

1.9

35

%{
This is an M-file, with the name example1.m,
for the calculation of the traveled distance.
%}
velocity=50;
travelingtime=60;
distance=velocity*travelingtime;
disp('distance= '),disp(distance)

Ctrlt C.
:
x,
disp(x)

a =
3.2400
disp(a)
3.2400

:
clc

36

() MATLAB,

, ,
.
,
.
(spreadsheet),

(.. Excel), .
,

, ,
9
.

2.1
()
,

. ,

2.1

37

,
.

,
.
.
MATLAB
, [ ] , .
.
; ,
, ..
a1=[2, 7, 71, 8, 9]
a1 =
2
7
71

a2=[3 8; 4 5]
a2 =
3
8
4
5
a2=[3 8
4 5]
a2 =
3
4

8
5

a1 a2,
.
2.1.1
, ,
, ..
. MATLAB
, .. a(i, j) (i, j)

38

a.
, .. a1(i).
, .. (i, j)
a a(i, j).
,
, ,
a(i, j, ...) =

a1, a2
,
a1(3)=11
a1 =
2

11

a2(2, 1)=13
a2 =
3
8
13
5

2.1.2
.
m n ,
m n (). , m 1 m , 1 n , - n .

.

[1] Size: ,
m n
. .. a1, a2, ,
size(a1)

2.2

39

ans =
1
size(a2)
ans =
2

[2] Length:
, m n,
a1 a2, ,
length(a1)
ans =
5
length(a2)
ans =
2

[3] Numel: ,
a1 a2, ,
numel(a1)
ans =
5
numel(a2)
ans =
4

2.2

, .
. ,

40


.
2.2.1 a_initial:a_step:a_final
,
,
a = a_initial:a_step:a_final

,
a_initial, a_step,
a_final. ,
.

d1=1:2:10
d1 =
1
d11=1:9
d11 =
1


,
, ..
d2=[1:1:5; 2:2:10; 1:.5:3]
d2 =
1.0000
2.0000
3.0000
2.0000
4.0000
6.0000
1.0000
1.5000
2.0000

4.0000
8.0000
2.5000

2.2.2 linspace(a1, a2, a3)



a = linspace(a1, a2, a3)

5.0000
10.0000
3.0000

2.2

41


a3 , a1 a2, ..
linspace(1,4,5)
ans =
1.0000
1.7500

2.5000

3.2500

4.0000

2.2.3 zeros(n, m) zeros(n)



zeros(m, n)

m n
. zeros(n, n)
zeros(n).

zeros(2,3)
ans =
0
0
0
0

0
0

zeros(3)
ans =
0
0
0

0
0
0

0
0
0

2.2.4 ones(n, m) ones(n)



ones(m, n)

m n
. ones(n, n)
ones(n).

42

ones(2,3)
ans =
1
1

1
1

1
1

ones(3)
ans =
1
1
1

1
1
1

1
1
1

2.2.5 eye(n)

eye(n)

n n , ..
eye(3)
ans =
1
0
0

0
1
0

0
0
1

2.2.6 diag(a)

diag(a)

a n ,
n n ,
, a.
diag ,
- , ..

2.2
c=diag([1,2,3,4])
ans =
1
0
0
0
2
0
0
0
3
0
0
0

43

0
0
0
4

diag(c)
ans =
1
2
3
4

2.2.7 magic(n)

magic(n)

n n ,
,
, , .

magic(4)
ans =
16
5
9
4

2
11
7
14

3
10
6
15

13
8
12
1

2.2.8 rand(n, m) rand(n)


rand, ,

rand(m, n)

44

m n ,
[0, 1].
rand(n, n) rand(n).

rand(2,3)
ans =
0.9501
0.2311

0.6068
0.4860

0.8913
0.7621

rand(3)
ans =
0.4057
0.9355
0.9169

0.4103
0.8936
0.0579

0.3529
0.8132
0.0099

2.2.9 randn(n, m) randn(n)


randn, ,

randn(m, n)

m n
(0, 1).
randn(n, n) randn(n).

randn(2,3)
ans =
-0.4326
-1.6656

0.1253
0.2877

-1.1465
1.1909

randn(3)
ans =
1.1892

0.1746

-0.5883

2.3
-0.0376
0.3273

-0.1867
0.7258

45
2.1832
-0.1364

2.3
2.1
,
(), ()
, . ,

.

.
2.3.1

,

, ..
a1=12:20;
a1([1,3,7])
ans =
12
14

18

,
.
,
( )
, ,
..
a=magic(5)

46
a =
17
23
4
10
11

24
5
6
12
18

1
7
13
19
25

8
14
20
21
2

15
16
22
3
9

a([1,2,4], 2)
ans =
24
5
12
a([1, 3], [2, 4])
ans =
24
8
6
20

,
a, ..
a(1:3,2)
ans =
1
32
9
a(2,2:end)
ans =
32
7

1:3 2:end, ,
a. i:end
i
.
,
: ,

2.3

47

,
, ..
a(2, :)
ans =
3

32

21

23

25

2.3.2 -
,
: , -
, ,
a2 =[1
4

3
2]

a2(:)
ans =
1
4
3
2

,
,

, , ..
a2(3)
ans =
3
a2(end)
ans =
2

48

2.3.3
MATLAB
,
,
.
, -
, ,
- .


,
b=1:10;
b(b>5)
ans =
6

10

a=magic(4);
a(a>8)
ans =
16
9
11
14
10
15
13
12

2.3.4
2.1,
.
,
.
,

2.3

49

, .

.
,
, , ,
.


a=magic(4)
a =
16
2
5
11
9
7
4
14

3
10
6
15

13
8
12
1


a(2:3,1:2)
ans =
5
11
9
7


a1=[11 7; 8 5]
a1 =
11
7
8
5


a(2:3,1:2)=a1
a =

50
16
11
8
4

2
7
5
14

3
10
6
15

13
8
12
1


,
, [] .

a

a(3,:)=[]
a =
16
2
11
7
4
14

3
10
15

13
8
1


a(:,2)=[]
a =
16
3
11
10
4
15

13
8
1

2.4
H S, , A, B, C,
D, , MATLAB ,
, A, B,
.
,

2.4

51

, .
:
1)


S =[A, B, ]

, , ...
, ..
A1=[1 3;8 4];
A2=[3 7 2;9 4 6];
S=[A1 A2]
S =
1
8

3
4

3
9

7
4

2
6

2)


S =[A; B; ]

, , ...
, ..
B1=[1 7 9; 5 8 4];
B2=[3 8 6; 6 9 4; 4 9 6];
S=[B1; B2]
S =
1
7
5
8

9
4

52
3
6
4

8
9
9

6
4
6

3)
,
S =[A1 2 ...; B1 2 ...; ]



,

, ..
C1=[1
C2=[3
C3=[7
C4=[8

4; 7
8 7;
9 6;
6; 6

8];
8 7 3];
9 5 7; 8 5 4];
1; 7 5];

S=[C1 C2; C3 C4]


S =
1
4
3
7
8
8
7
9
6
9
5
7
8
5
4

8
7
8
6
7

7
3
6
1
5

2.5

,
. MATLAB
.
3x3
A B,

2.5

53

A=[7,7,6; 1,9,4; 5,4,1];


B=[11,6,5; 7,5,9; 8,6,2];

1. . ()
()
. MATLAB

+ - ,
,
A+c A-c

[1] ,
,
A+2
ans =
9
3
7

9
11
6

8
6
3

4
-2
2

4
6
1

3
1
-2

A-3
ans =


,
,
A+2==2+A
ans =
1
1

1
1

1
1

54
1

2. . () () .
MATLAB
* , / \ ,
,
c*A , A/c c\A

[2] ,
,
3*A
ans =
21
3
15
A/3
ans =
2.3333
0.3333
1.6667

21
27
12

18
12
3

2.3333
3.0000
1.3333

2.0000
1.3333
0.3333

1.2, MATLAB
\ ,
, ,
3\A
ans =
2.3333
0.3333
1.6667

2.3333
3.0000
1.3333

2.0000
1.3333
0.3333

2.5

55

, A/c c\A
, .
3. .
() .
()
() .
MATLAB
+ - ,
, ,

A+B, -

[3] , ,
,
A+B
ans =
18
8
13

13
14
10

11
13
3

A-B
ans =
-4
-6
-3

1
4
-2

1
-5
-1


,
,
A+B==B+A
ans =
1

56
1
1

1
1

1
1

4. () . ()
. ()
() .
, ,

A.*B ./ .\
: () ,
,
, . ,
. ,
,
9.2.

[4] , ,
,
A.*B
ans =
77
7
40

42
45
24

30
36
2

B.*A
ans =
77
7
40

42
45
24

30
36
2

A./B
ans =
0.6364

1.1667

1.2000

2.5

57

0.1429
0.6250

1.8000
0.6667

0.4444
0.5000

B.\A
ans =
0.6364
0.1429
0.6250

1.1667
1.8000
0.6667

1.2000
0.4444
0.5000

,
A.* .*A ,
A./ .\A.
5. . ,
A.^n

n-
A. n
.
[5] , ,
,
A.^2
ans =
49
1
25
A.^(-1)
ans =
0.1429
1.0000
0.2000

49
81
16

36
16
1

0.1429
0.1111
0.2500

0.1667
0.2500
1.0000

58
1./A==A.^(-1)
ans =
1
1
1
1
1
1

1
1
1

6. .
,

A.

,
.
, 9.2,
,
.
.
[6] , ,
,
A.'
ans =
7
7
6

1
9
4

5
4
1

B.'
ans =
11
6
5

7
5
9

8
6
2

2.5.1
MATLAB
,

2.6

59

. ,
,
.

, ,
.

[1] ,
/4.
sin([0:pi/4:pi])
ans =
0
0.7071

1.0000

0.7071

0.0000

[2] 3 4 , 10*rand(3, 4),


,
a=10*rand(3,4)
a =
3.1269
6.8312
0.1286
0.9284
3.8397
0.3534
log(a)
ans =
1.1400
-2.0508
1.3454

1.9215
-0.0743
-1.0402

6.1240
6.0854
0.1576

0.1635
1.9007
5.8692

1.8122
1.8059
-1.8477

-1.8106
0.6422
1.7697

2.6
,
.
,

60

a=11:20;
A=[7,7,6; 1,9,4; 5,4,1];

1. find. find
,
find(_)


. ,
. ,
,
, .
,
(i, j)
. (i, j) ,
.

a , ..
16 ,
find(a>16)
ans =
7
8

10

, 6
,
find(A>6)
ans =
1
4
5

2.6

61

[i j]=find(A>6)
i =
1
1
2
j =
1
2
2

2. min.
,
min(a)

a , min(a)
,
-.
min(A)
ans =
1

min(A(:))
ans =
1

3. max.

max(a)

a , max(a)
,
-.
max(A)

62
ans =
7

max(A(:))
ans =
9

4. sum.

sum(a)

a , sum(a)
,
-.
sum(A)
ans =
13

20

11

sum(A(:))
ans =
44

5. prod.

prod(a)

a , prod(a)
,
-.
prod(A)
ans =
35

252

prod(A(:))
ans =

24

2.6

63

211680

6. sort.
,
sort(a)

a , sort(a)
,
-.

, ,
sort(a, mode)

mode,
,
'ascend' ( )
'descend' .

sort(A,'ascend')
ans =
1
4
1
5
7
4
7
9
6
sort(A,'descend')
ans =
7
9
6
5
7
4
1
4
1

64

, MATLAB . MATLAB
(), .

.

3.1
MATLAB,
,
.
, ..
s1='Introduction to strings'
s1 =
Introduction to strings
s2='Paragraph one'
s2 =
Paragraph one

3.2

65

, s1
s2 .

3.2
MATLAB, ,

.
,
, .
,
3.1.
1. , length, ..
length(s1)
ans =
16

2. ,
, ..
s1(1)
ans =
C
s1(1:9)
ans =
Character
s1(11:16)
ans =
string

66

3. , ,
, ..

a='numperr'
a =
numperr
a(7)=[]
a =
numper
a(4)='b'
a =
number

4.
ASCII,
, ..
s2=s1*1
s2 =
Columns 1 through 15
67
104
97
114
32
115
116
114
105
Column 16
103

97
110

99

116

101

114

5. ASCII,
, char, ..
char(s2)
ans =
Character string

3.2

67

6. , ,
, , , ..
s1(11:16)'
ans =
s
t
r
i
n
g

7. , , ..
u w [u,w],
s3=['part', ' ', 'one']
s3 =
part one

68


. MATLAB
.
, .. FORTRAN, PASCAL, C
...
.

,
. if,
for which.
.

4.1 : if
,
, , .
, ,
, .
if.
if

4.1 : if

69

if _
_1
else
_2
end

,
, .
, _1,
_2, .
,
if
if _
_1
end

[1] .. a
b, ,
a 33 b>3, 21 ,
if b>3
a=33;
else
a=21;
end

[2] , if,
, x,
if x > 6
a = 2;
b = 3;
else

70
a = 5;
b = 6;
end
disp(a), disp(b)



. if
if __1
_1
elseif _ _2
_2

elseif __-1
_-1
else
_
end

[4] , x y,
y<0, 0 y 1 , y-1 y>1.

,
if y<0
x=-y;
elseif y<=1
x=0;
else

4.2 -

71

x=y-1;
end

4.2 -

.
,
,
. MATLAB
for while.
4.2.1 For
:
for =

end

,
.
,
.. 1:10. . , , .
1

1-10.
,
x=[];
for i=1:10
x=[x,i^2];
end

72

:
x. ,

, i^2.
2
1-100.
n=100;
s=0.;
for i=1:n
s=s+i;
end

: s
.
, ,
,
i.
3
,
,
.
,
,
10*rand(1,100).
,
n=100;
a=10*rand(1,n);
amax=a(1);imax=1;
for i=2:n
if a(i)>amax
amax=a(i);

4.2 -

73

imax=i;
end
end

:
.
.
,
, .
,
, .
4

y
:
x=2.0+rand
100 ,
(standard deviation).

,
.

1 n
x = i=1 xi ,
n

1
n
( xi x ) 2 ,

i =1
n 1
n .
:
s = s2 =

n=100;
x=2+rand(1,n);
% evaluation of the mean value

74

xsum=0.;
for i=1:n
xsum=xsum+x(i);
end
xmean=xsum/n
%
%evaluation of the stantard deviation
x2sum=0.;
for i=1:n
x2sum=x2sum+(x(i)-xmean)^2;
end
sdx=sqrt(x2sum/(n-1))

for
for,
,
.
5
3 4 ,
a(i j ) = (i + j ) / 2 .

,
for i=1:3
for j=1:4
c(i,j)=(i+j)/2.;
end
end

6

magic(10).
,
( ),
,

4.2 -

75

n=10;
a=magic(n);
asum=0.;
for n1=1:n
for n2=1:n
asum=asum+a(n1,n2);
end
end

4.2.2 While
, ,
,
,
,
.
while, ,
__
while __

___
end

for, while,
, .
,
,
. ,
, ,
.

76

1
while,
,
.
for,
,

10*rand(1,100)
,
,
n=100;
a=10*rand(1,n);
amax=a(1);imax=1;
ni=1;
while ni<n+0.5
if a(ni)>amax
amax=a(ni);
imax=ni;
end
ni=ni+1;
end
disp('maximum number= '),disp(amax)
disp('position= '),disp(imax)

ni < n + 0.5 , , ni n
ni = n , .
ni == n .
2
, y :
y=2.0+0.2*rand

4.2 -

77

while,
(standard deviation) 10000
. :
n=10000;
y=2+0.2*rand(1,n);
% evaluation of the mean value
ysum=0.;
i1=1;
while i1<n+0.5
ysum=ysum+y(i1);
i1=i1+1;
end
ymean=ysum/n
%evaluation of the stantard deviation
y2sum=0.;
i2=1;
while i2<n+0.5
y2sum=y2sum+(y(i2)-ymean)^2;
i2=i2+1;
end
sdy=sqrt(y2sum/(n-1))


,
,
.
3

migic(10).
,
n=10;
a=magic(n);

78

asum=0.;
n1=1;
while n1<n+0.5
n2=1;
while n2<n+0.5
asum=asum+a(n1,n2);
n2=n2+1;
end
n1=n1+1;
end
disp('list element summation= '),disp(asum)

for
while,
n=10;
a=magic(n);
asum=0.;
for n1=1:n
n2=1;
while n2<n+0.5
asum=asum+a(n1,n2);
n2=n2+1;
end
end
disp('list element summation= '),disp(asum)

4.3
, 4.2,
for while,
.
, ,
.
continue break.

4.3

79

4.3.1 continue

, ,
.
continue , ,
, ,
.
, for, ,
for =_

.
.
continue
.
end


continue.
continue
while.


floor(20*rand(1,100)+1)


[1, 20]. .
.
n=100;
a=floor(20*rand(1,n)+1);
asum=0.;
for ni=1:100

80
if mod(a(ni),2)==1
continue
end
asum=asum+a(ni);
end
disp('sum= '),disp(asum);

continue
.
4.3.2 break
,
for while.
break. ,
for, ,
for =_

.
.
break
.
end


break.
while.


floor(20*rand(1,100)+1)

,
[1, 20].

4.4

81


.
.
n=100;
a=floor(20*rand(1,n)+1)
asum=0.;index=0;
for ni=1:100
if mod(a(ni),2)==index
index=index+1;
if index>1
break
end
disp(a(ni))
elseif index==1
disp(a(ni))
end
end

, index,
index=0.
mod(a(ni), 2)==index,
index=1.
, ,
, mod(a(ni), 2)==index.

4.4
1.2 ,
. ,
.
,
.
, , . ,

82

, ( ), (
).
4.4.1

. ,
& ,
&& . ,
,
.
.

(test1)&(test2)&(test3)...

(test1)&&(test2)&&(test3)...

.
[1] magic(6)
2 3, .
af=magic(6);
s=0;
for i=1:6
for j=1:6
if mod(af(i,j),2)==0&&mod(af(i,j),3)==0
disp(af(i,j));s=s+af(i,j);
end
end
end
disp(s)

4.4

83

,
2 3,
s.
4.4.2

, |
|| , ,
(test1)|(test2)|(test3)...

(test1)||(test2)||(test3)...

. ,
.
[2] magic(6) 2
3, .
af=magic(6);
s=0;
for i=1:6
for j=1:6
if mod(af(i,j),2)==0||mod(af(i,j),3)==0
disp(af(i,j));s=s+af(i,j);
end
end
end
disp(s)

,
2 3,
s.

84

4.4.3
( 1 0
) , ~ ,
,
~(test1)

[3] magic(6)
2 3, .
af=magic(6);
s=0;
for i=1:6
for j=1:6
if (~mod(af(i,j),2)==0)&&(~mod(af(i,j),3)==0)
disp(af(i,j));s=s+af(i,j);
end
end
end
disp(s)

,
2 3,
s.

4.4

85

86


MATLAB.
,
() .
.

5.1
1.7 -,
, .
-
,
.
,

.


,
,

5.1

87

. ,
-.
-
ASCII (editor), .. MATLAB.
-
- ,
-, .
, ,
.
, .m .

, .. MATLAB,
, .
5.1.1
,
, .

,
.
1)

,
. ,
, , .
,
.

88

2)
.

,
.
,
.


, .
,
,
function [w1 w2 ]=function_name(x1, x2, )

x1, x2, ,
w1, w2, .
,
.
,

,

, . ,

.

[z1 z2 ]=function_name(x1, x2, )

z1, z2, .

5.1

89

1
- x

cos( x + 2) /(1 + ( x + 2) 2 ) .

function y=ff(x)
x=x+2;
disp(x)
y=cos(x)./(1+x.^2);

ff.m.
, x
, ff,
. , y.
y

,
,
>> x =1
>> disp(x)
1
>> z=ff(x)
x =
3
z =
-0.0990
>> disp(x)
1
>> disp(y)
??? Undefined function or variable 'y'.

x , ,
, , ,

90

.
y
z, y,
disp(y).
2

.
, .
,
findmaximum, findmaximum.m.
function [amax, index]=findmaximum(a)
n=length(a);
amax=a(1);
index=1;
for i=2:n
if a(i)>amax
amax=a(i);
index=i;
end
end

,
a, [amax, index],
,
findmaximum(a).

,
>> a=rand(100,1);
>> findmaximum(a)
ans =
0.9883
>> z=findmaximum(a)
z =

5.1

91

0.9883
>> [z1 z2]=findmaximum(a)
z1 =
0.9883
z2 =
100

100
a,
findmaximum .
,
,
, .

.
5.1.2
, - ,

-, .

-.
,
.
return error.
1) return
, ,
.
,
return.
return.

92

1

.
,
.
,
firstnumber, firstnumber.m.
function [a10, index]=firstnumber(a)
n=length(a);
a10=-1;
index=1;
for i=1:n
if a(i)>a10
a10=a(i);
index=i;
end
if a(i)>10
return
end
end
disp('number>10 not found, returns the maximum nember')

return,
,
10.
,
a=15*rand(12,1);
[z1 z2]=firstnumber(a)
z1 =
11.7499
z2 =
5

5.1

93

2) error

,
, .. .

,
,
,
.
error.
error.
2
1,
.
firstnumber,
a, .
,
firstnumbernew, firstnumbernew.m.
function [a10, index]=firstnumbernew(a)
n=length(a);
for j=1:n
if a(j)<0
error('negative input number')
end
end
a10=-1;
index=1;
for i=1:n
if a(i)>a10
a10=a(i);
index=i;

94
end
if a(i)>10
return
end

end
disp('number>10 not found, returs the maximum nember')

,
a, ,
, ,
>> a=15*rand(12,1);
>> a(10)=-1;
>> [z1 z2]=firstnumbernew(a)
??? Error using ==> firstnumbernew
negative input number

5.2
MATLAB ,
,
.


, ..
,
.
,
, ,
.

,
.

MATLAB, -,

5.2

95

,
.
.
5.2.1
MATLAB
, .

driver_name=@function_name

driver_name , function_name
.
,
, ,
.

, fsin, sin,
,
fsin=@sin;
fsin([0: 0.25 :1])
ans =
0
0.2474

0.4794

0.6816

0.8415

5.2.2 -
,
driver_name=@-function_name

, ,
, .

96

ffindmaximum -
findmaximum.m, 5.1.1
,
,
>> ffindmaximum=@findmaximum
ffindmaximum =
@findmaximum

,
>> a=rand(50,1);
>> [z1 z2]=ffindmaximum(a)
z1 =
0.9805
z2 =
44

5.2.3
,
.
,
, . ,
driver_name=@(x, y, ) expression(x, y, );

,
@, ,
,
. ,
@, .

5.2

97

1/( x 2 + 1) , x 2 + y 2 ,
,
>> f1=@(x) 1./(x.^2+1);
>> f1(1)
ans =
0.5000
>> f1([1:0.2:2])
ans =
0.5000
0.4098
0.2000

0.3378

0.2809

0.2358

>> f2=@(x,y) x^2+y^2;


>> f2(1, 1)
ans =
2
>> f2(1.2,0.8)
ans =
2.0800

5.2.4 o
,
, .
,
,
.
,
@(x, y, ) expression(x, y, )

98
>> quad(@(x) x.^2./(1+x.^2), 0, 1)
ans =
0.2146

quad

x 2 /(1 + x 2 ) dx .

5.3.1.

5.3
,
.

,
.
5.3.1
b

f ( x)dx
a



.
,
1

x (1 x) dx ,

(
),

5.3

99

0.4
0.3
0.2
0.1

0.2

0.4

0.6

0.8

,
n , n+1
, [ x1 , x2 ,..., xn+1 ] , ,
, ,
b

f ( x)dx =

1 n+1
( f ( xk ) + f ( xk 1 ))( xk xk 1 ) ,
2 k =2

- , n ,
, trapez.m .
function s=trapez(f,a,b,n)
s=0;
x=linspace(a,b,n+1);
for i=2:n
s=s+(f(x(i))+f(x(i-1)))*(x(i)-x(i-1))/2;
end

100

, ()
1

x (1 x) dx 1000 ,

,
ff=@(x) (1-x)*sqrt(x);

- trapez
,
trapez(ff,0,1,1000)
ans =
0.2667

,
4/16=0.266666... ,
()

sin( x) dx ,

,
trapez(@sin,0,pi,10000)
ans =
2.0000

5.3.2

.

(standard deviation).
,

.
1 n
x = i=1 xi ,
n

5.3

101


1
n
( xi x ) 2 ,

i =1
n 1
n . MATLAB

, , ,
.

statistics,
statistics.m,
s = s2 =

function [xmean sdx]=statistics(x)


n=length(x);
% evaluation of the mean value
xsum=0.;
for i=1:n
xsum=xsum+x(i);
end
xmean=xsum/n;
%
%evaluation of the standard deviation
x2sum=0.;
for i=1:n
x2sum=x2sum+(x(i)-xmean)^2;
end
sdx=sqrt(x2sum/(n-1));


,
.

,

,
y :

102

y=2.0+0.5*rand
100.000 y
,
a=2+0.5*rand(100000,1);
[amean sdx]=statistics(a)
amean =
2.2503
sdx =
0.1448


,
rand .

5.3.3

. MATLAB
. ,

A=rand(100, 3);

rand
,
.
1. : mean.

mean(a)

5.3

103

a , mean(a)
,
-.
mean(A)
ans =
0.5150

0.4961

0.5093

mean(A(:))
ans =
0.5068

2. : std.

std(a)

a , mean(a)
,
-.
std(A)
ans =
0.2633

0.2988

0.2995

std(A(:))
ans =
0.0207

3. : var.

var(a)

104

a , mean(a)
,
-.
var(A)
ans =
0.0693
var(A(:))
ans =
1.3554e-004

0.0893

0.0897

105


,
. MATLAB
,
, .

, ,
8.

6.1 - plot

MATLAB plot.
, ,
plot(x, y)

x y x y , .
, ..
plot( y)

106

y
.
y.

200 x,
[0, 4],
y=cos(x). x
y , ,
,
x=linspace(0, 4*pi, 200);
y=cos(x);
plot(x,y)

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

plot y
plot(y)

12

14

6.1 - plot

107

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

20

40

60

80

100

120

140

160

180

200

6.1.1
,
, .. ,
, ...
, .
,

Help .
1. .
,
, .
,
axis([xmin xmax ymin ymax])


, .
2. .
,

108
xlabel('xlabel_name')
ylabel('ylabel_name')

3. . ,
,
title('figure title')

4. .
,
grid on

5. .

box on


box off


,
,
y = cos(t ) ,
t=linspace(0, 4*pi, 200);
y=cos(t);
plot(t,y)
axis([0 4*pi -1 1])
xlabel('time t')
ylabel('displacement y')
title('Figure 1')
grid on

6.1 - plot

109

Figure 1
1
0.8
0.6

displacement y

0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

6
time t

10

12

6. .
,
,
plot.
plot ,
plot(x, y, )


, ,
,
: ,
: ,
: .

, ,
,

110

b
g
r
c
m

y
k
w

Blue
.
Green
o
Red
x
Cyan
+
Magenta
*

Yellow
Black
White

s
d
v
^
<
>
p
h

Point
Circle
Cross
Plus sign
Asterisk

:
-.
-[]

Solid
Dotted
Dash-dot
Dashed

Square
Diamond
Triangle (down)
Triangle (up)
Triangle (left)
Triangle (right)
Pentagon
Hexagon


, .

cos( x) ,

,
x=linspace(0, 4*pi, 50);
y=cos(x);
plot(x,y,'bx-')
axis([0 4*pi -1 1])

6.1 - plot

111

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

12

7. .
,
plot.
:

Color:
[R G B], R,
G, B
(R), (G), (B) ,
,

plot(x,y,'Color',[1,0,0])

112

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

12

14

LineWidth:
,
,

plot(x,y,'Color',[1,0,0],'LineWidth',4)
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

12

14

6.1 - plot

113

MarkerSize:

,
,

plot(x,y,'bx-','MarkerSize',10)

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

12

14

8. , set.
,
set,
plot. ,
plot,
, ,
MATLAB set
,
, ..:

Color .
Color

114

, , [R G B], R, G,
B
(R), (G), (B) ,
,
x=linspace(0, 4*pi, 50);
y=cos(x);
fig=plot(x,y);
set(fig,'Color',[1,0,0])

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

12

14

LineWidth .
, ,
,
,

fig=plot(x,y);
set(fig,'Color',[1,0,0],'LineWidth',4)

6.1 - plot

115

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

12

14

MarkerSize . ,
,
,

fig=plot(x,y,'bx-');
set(fig,'MarkerSize',10)

116

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

12

14

6.1.2 scatter
,
,

scatter(x, y, )

scatter .

y = cos( x) ,

scatter, ,
x=0:0.2:4*pi;
y=sin(x);
fig1=scatter(x,y,'r+')
axis([0 4*pi -1.1 1.1])

6.1 - plot

117

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0

10

12

6.1.3 stem
stem ,
,
.
,
stem(x, y)

x y x y , .
, ..
stem( y)

y
.

118

sin(x)
,
stem,
a=sin(0:0.1:2*pi);
stem(a)

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

20

30

40

50

60

70

6.2
, , plot,
,
.
,
plot(x1, y1, , x2, y2, , )

xi, yi i
,
.

6.2

119

x y1=cos(x),
y2=sin(x), ,
x=linspace(0, 4*pi, 50);
y1=cos(x);
y2=sin(x);
plot(x,y1,'bx-',x,y2,'r*:')
axis([0 4*pi -1 1])

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

12

x-
, y-
w,

plot(x,w)


w x.

y=cos(x)
y=sin(x), w,

120

x=linspace(0, 3*pi, 200);


y1=cos(x);
y2=sin(x);
w=[y1;y2];
plot(x,w)

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

6.2.1 hold on
, ,
. , ,
plot ,
, .

, plot.


hold on

6.2

121


, .

hold off

hold on.

, y=cos(x), y=sin(x),
hold on
x=linspace(0, 3*pi, 200);
y1=cos(x);
y2=sin(x);
plot(x,y1,'b-')
axis([0 3*pi -1 1])
hold on
plot(x,y2,'r:')
hold off

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

122

6.3
MATLAB
,
subplot(n, m, i)

n m
n * m . i
, subplot(n, m, i),
, plot
.
-.

2 2 ,
y = sin( x) ,

y = sin( x) 2 , y = 4 /(4 + x 2 ) , y = e x sin( x) .


, ,
x1=linspace(0,2*pi,50);
y1=sin(x1);
y2=sin(x1).^2;
x2=linspace(0,10,50)
y3=4./(4+x2.^2);
y4=exp(-x2).*sin(x2);
subplot(2,2,1)
plot(x1,y1)
axis([0 2*pi -1.1 1.1])
title('Figure 1: sin(x)')
subplot(2,2,2)
plot(x1,y2)
axis([0 2*pi 0 1.1])
title('Figure 2: sin(x)^2')
subplot(2,2,3)
plot(x2,y3)

6.4 ezplot

123

axis([0 5 0 1.1])
title('Figure 3: 4/(4+x^2)')
subplot(2,2,4)
plot(x2,y4)
axis([0 6 -0.1 0.5])
title('Figure 4: exp(-x) sin(x)')
Figure 2: sin(x)2

Figure 1: sin(x)
1

0.5
0

0.5

-0.5
-1
0

Figure 3: 4/(4+x 2)

Figure 4: exp(-x) sin(x)

0.4
0.2

0.5

0
0

6.4 ezplot
, MATLAB
,
ezplot. 5.2,
ezplot
,
, ,
ezplot( function_driver, [ xmin, xmax] )

124


, x [xmin, xmax].
,
[-2, 2].

y=sin(x),
fsin, ,
fsin=@sin;
ezplot(fsin, [0, 5*pi])

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

10

15


, ,
ezplot(@function_name, [ xmin, xmax] )

function_name .

6.4 ezplot

125

cos(x)
,
ezplot(@cos)
cos

0.5

-0.5

-1
-6

-4

-2

0
x

,
, ,
ezplot(@(x) sin(x)/(1+x), [0,6*pi])

0.5

0.4

0.3

0.2

0.1

-0.1

-0.2

10

12

14

16

18

126

6.5
,
. .. y(t) ,
t, y(t).
t,
y,
ey. ,
{t i , yi } ,
eyi
,
errorbar(t, y, ey, )

[1] ,
y (t ) = sin(t / 2) ,
10% y.
,
, ,
t=0:0.2:12;
y=sin(t/2);
ey=0.1*y;
plot(t,y)
errorbar(t,y,ey)
axis([0 12 -1.2 1.2])

6.6

127

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0

10

12

6.6
, ,
, ()
,
. ,
,

.

.
,

.
MATLAB .
x
, y
,
hist (y, x )

128

y , ..
hist( y)

.
y,
.

=5000
,
.
.
(1, 0)

1
exp( x 2 / 2) ,
P ( x) =
2

h
1
exp( x 2 / 2) ,
x, n = N h
2
.
,
, ,
x=-2.9:0.2:2.9;
y=randn(5000,1);
hist(y,x)
hold on
ezplot(@(x) 5000*0.2*exp(-x^2/2)/sqrt(2*pi),[-3 3],'r')
hold off

6.7

129

450
400
350
300
250
200
150
100
50
0
-3

-2

-1

6.7

. ,
,
,
. , x
y , ,

semilogx,

semilogy,

loglog


x y , .

130

[1] ,
y,
f ( x) = exp( x) :
x=0.0:0.5:10;
y=exp(x);
semilogy(x,y)
5

10

10

10

10

10

10

10

[2] ,
x,
y = log( x) + 2 :
x=1:2:100;
y=log(x)+2;
semilogx(x,y)

6.7

131

7
6.5
6
5.5
5
4.5
4
3.5
3
2.5
2
0
10

10

10

[3] ,

y = x4 :
x=1:2:100;
y=x.^4;
loglog(x,y)

132

10

10

10

10

10

10

10

10

10
0
10

10

10

6.8
, ,
r=f(),
.
,
,
.
6.8.1 polar
,
, {i , ri } ,

. theta
rho ,
polar (theta, rho, )

r = sin 2 (2 ) ,

6.8

133

th=linspace(0, 2*pi, 150);


rth=sin(2*th).^2;
polar(th, rth)

90
120

1
0.8

60

0.6
150

30

0.4
0.2

180

210

330

240

300
270

6.8.2 ezpolar
ezpolar r=f(),
,
ezpolar( @() f(), [ min, max] )

r = sin 2 (2 ) .
ezpolar( @(t) (sin(2*t)).^2,[0,2*pi])

134

90
120

1
0.8

60

0.6
150

30

0.4
0.2

180

330

210

240

300
270
r = (sin(2 t))2

6.9 -
, y x y = f ( x) .
, y x
, .
.
6.9.1
,
x y , .. t, x = x(t ) y = y (t ) .

,
ezplot(@(t) x(t), @(t) y(t), [tmin, tmax])


x(t) y(t) ezplot.
t,
0<t<2.

6.9 -

135

[1] {cos(2t), sin(3t)},


t [0, 2], ,
ezplot(@(t) sin(2*t), @(t) cos(3*t), [0,2*pi])
x = sin(2 t), y = cos(3 t)
1
0.8
0.6
0.4

0.2
0
-0.2
-0.4
-0.6
-0.8
-1

-0.5

0
x

0.5

6.9.2
,
F(x, y)=0,
y. MATLAB
:
ezplot(@(x, y) F(x, y), [ xmin, xmax, ymin, ymax])


F(x, y) ezplot.
x y,
-2<x<2 -2<y<2.

136

x 2 y 2 = 4 ,
5 < x, y < 5 .
ezplot(@(x,y) x.^2-y.^2-4,[-5,5,-5,5])
x 2-y 2-4 = 0
4
3
2

1
0
-1
-2
-3
-4
-4

-3

-2

-1

0
x

,
ezplot(@(x,y) x.^2-y.^2-4)
x2-y 2-4 = 0
6

-2

-4

-6
-6

-4

-2

0
x

6.9 -

137

138

MATLAB


MATLAB.
Help .

7.1
MATLAB,
, ,
1. (Command Window)
2. (Workspace) ,
3. (Current Directory),
4. (Command History).

,
.
7.1.1 (Command Window)
, ,

7.1

139

, ,
.
7.1.2 (Workspace)

(Workspace).
,
,
.
7.1.3 (Current directory)
MATLAB
,
.
,
.
.
, ,
(current directory) work
MATLAB.
MATLAB
, ,

.
,

.

,
.
,
, Current Directory,
,

140

MATLAB

,
,
( Make New Folder).

7.1.4 (Command History)


,
(Command History)
. ,
,
. ,
.

7.2

141

7.2

16 . ,
,
. FORMAT
,
.
help format

FORMAT
.
.
FORMAT may be used to switch between different output display formats
of all float variables as follows:
FORMAT SHORT Scaled fixed point format with 5 digits.
FORMAT LONG Scaled fixed point format with 15 digits for double
and 7 digits for single.
FORMAT SHORT E Floating point format with 5 digits.
FORMAT LONG E Floating point format with 15 digits for double and
7 digits for single.
FORMAT SHORT G Best of fixed or floating point format with 5
digits.
FORMAT LONG G Best of fixed or floating point format with 15
digits for double and 7 digits for single.
FORMAT SHORT ENG Engineering format that has at least 5 digits
and a power that is a multiple of three
FORMAT LONG ENG Engineering format that has exactly 16
significant digits and a power that is a multiple of three.
FORMAT
SHORT.
FORMAT .
format new-format

142

MATLAB

FORMAT LONG
E,

2 ,

format long e
a=sqrt(2)
ans =
1.414213562373095e+000

FORMAT,
, .

7.3 (workspace)
7.1.2,
, ,
(Workspace).
,
. ,
,
,
. ,
,
, .
,
, ,
.
,
.
-
who, ..
who
Your variables are:
a
ans

7.3 (workspace)

143


clear, 1.5.

, ,
,
save file_name

,
, file_name.mat.

. ,
MATLAB.mat, ..
save
Saving to: MATLAB.mat


,
,
save file_name var1 var2

var1, var2,
.

,

load file_name

,
file_name.mat, .

MATLAB

144

,
.
, MATLAB.mat, ..
load
Loading from: MATLAB.mat


,
,
delete file_name

file_name .
, . ..
delete MATLAB.mat

MATLAB.mat ,
delete *.mat


.mat.

7.4 ASCII

, .mat.

. ,
ASCII

7.5 input pause

145

. ,
, , ASCII,
:
save(filename, a , b ,...-ascii)

a, b, filename.
a, b, .
, filename, ,
,
. ,
,
,
Directory\filename
Directory/filename
, \,
Windows, , /, Unix.
MATLAB .
[1] x
.
,
,
x=0:0.1:10;
save('test.dat','x','-ascii')
save('c:\test1.dat','x','-ascii')
save('c:/test2.dat','x','-ascii')

7.5 input pause


input pause ,
- -.
,

146

MATLAB

.
,
,
.
input pause,
.

1) input
- ,
-,
,
.
,

. ,
input, ,
,
. ,
=input('Enter the applied magnetic field B ');

,
, .
, ,
.
2) pause
-, pause.

, .
pause.
pause, MATLAB

7.6

147

.
.

7.6
/, ,

,
MATLAB.

, , (editor) .
- ,
-, ,
.
, .

,
,
. 7.4
7.5.

,
, .
,
, . ,
, ,
.

diary on

,
, diary,

MATLAB

148


, ,
.
, .

diary off


. diary
,
. diary
.
diary ASCII,
ASCII,
.
diary, ,
.
()
diary file_name

7.7
.
[1] tic, toc: tic, toc
, ,
.
tic,
, toc
tic, toc,

tic
operations

7.7

149

toc

[2] cputime: tic, toc, ,


,
. cputime
, ,

MATLAB
cputime.
,
t0=cputime, ,
totaltime=cputime-t0
,
t0=cputime
operations
totaltime=cputime-t0

150

6
, y = f ( x) ,
y x. z = f ( x, y ) ,
z , x y.
,
x y, z.
,
Help
MATLAB.

8.1
6.1
plot.
plot3,
plot3(x, y, z, )

,
x, y, z, .

8.1

151

, , ,
, ,
: ,
: ,
: .

H {sin(t ),cos(t ), t} , t

0<t<8, ,
t = 0:pi/50:8*pi;
plot3(sin(t),cos(t),t);

30
25
20
15
10
5
0
1
0.5

1
0.5

-0.5

-0.5
-1

-1

8.1.1
, ,

.
, .
,
plot3(x1, y1, z1, , x2, y2, z2, , )

152

xi, yi, zi i
,
. ,
,
hold on, ,
, , -
.

{sin(t ),cos(t ), t} ,

{sin(t + 3),cos(t + 3), t} , t 0<t<8,


t = 0:pi/50:8*pi;
plot3(sin(t),cos(t),t,'b-');
hold on
plot3(sin(t+3),cos(t+3),t,'r*');

30
25
20
15
10
5
0
1
0.5

1
0.5

-0.5

-0.5
-1

-1

8.2

153

8.2
z = f ( x, y ) ,
:
[1]

{xi }

{y j} ,

x y ,
xy.
, .. x
y .
[2] , [X,
Y] xy, z.
x y
x y , xy

[X,Y]=meshgrid(x,y);

[3] f ( x, y ) ,
[X, Y], , .. .

Z=f(X, Y)

[4] [, , ].

mesh surf.
.

8.3 mesh

,
mesh. mesh ,
[, , ],
.
mesh(X, Y, Z)

154
, ,
mesh(Z)


.
.

z = sin( x 2 + y 2 ) / x 2 + y 2 ,

[7.5,7.5] [7.5,7.5] , ,
x=-7.5:0.5:7.5;
y=x;
[X,Y]=meshgrid(x,y);
R=sqrt(X.^2+Y.^2)+eps;
Z=sin(R)./R;
mesh(X,Y,Z)
xlabel('x label'),ylabel('y label'),zlabel('z label')
title('mesh figure')
mesh figure

z label

0.5

-0.5
10
5

10
5

-5
y label

-5
-10

-10

x label

8.3 mesh

155

mesh Z
mesh(Z)

0.5

-0.5
40
30

40
30

20

20

10

10
0

mesh
.

meshc,

8.3.1 meshc

,
. xy
, (contour).
meshc, ,
xy,
meshc(X, Y, Z)

, , ..
meshc(Z)


.
.

156

H z = sin( x 2 + y 2 ) / x 2 + y 2 ,
[7.5,7.5] [7.5,7.5] , ,
,
x=-7.5:0.5:7.5;
y=x;
[X,Y]=meshgrid(x,y);
R=sqrt(X.^2+Y.^2)+eps;
Z=sin(R)./R;
meshc(X,Y,Z)

0.5

-0.5
10
5

10
5

-5

-5
-10

-10

8.4 surf
mesh, surf. surf
,
{, , },
,
,
surf(X, Y, Z)

8.4 surf

157

, , ..
surf(Z)


.
.

z = sin( x 2 + y 2 ) / x 2 + y 2 ,
[7.5,7.5] [7.5,7.5] , ,
x=-7.5:0.5:7.5;
y=x;
[X,Y]=meshgrid(x,y);
R=sqrt(X.^2+Y.^2)+eps;
Z=sin(R)./R;
surf(X,Y,Z)

0.5

-0.5
10
5

10
5

-5

-5
-10

-10

158

surf, .
8.4.1 surfc
surfc, ,
xy.

z = sin( x 2 + y 2 ) / x 2 + y 2 ,
[7.5,7.5] [7.5,7.5] , ,
x=-7.5:0.5:7.5;
y=x;
[X,Y]=meshgrid(x,y);
R=sqrt(X.^2+Y.^2)+eps;
Z=sin(R)./R;
meshc(X,Y,Z)
xlabel('x label'),ylabel('y label'),zlabel('z label')
title('mesh figure')
surf with contours figure

z label

0.5

-0.5
10
5

10
5

-5
y label

-5
-10

-10

x label

8.4 surf

159

8.4.2 surfl
surfl, .

z = sin( x 2 + y 2 ) / x 2 + y 2 ,
[7.5,7.5] [7.5,7.5] , ,
x=-7.5:0.5:7.5;
y=x;
[X,Y]=meshgrid(x,y);
R=sqrt(X.^2+Y.^2)+eps;
Z=sin(R)./R;
meshc(X,Y,Z)
xlabel('x label'),ylabel('y label'),zlabel('z label')
title('mesh figure')
surf with lighting figure

z label

0.5

-0.5
10
5

10
5

-5
y label

-5
-10

-10

x label

160

8.5
,
,
.
meshc
surfc.
. .
8.5.1
xy ,
,
. MATLAB
:
contour(X,Y,Z,n)

, ,
, n
.

[1] z = sin( x 2 + y 2 ) / x 2 + y 2 ,
[8,8] [8,8] , ,
x=-8.0:0.5:8.0;
y=x;
[X,Y]=meshgrid(x,y);
R=sqrt(X.^2+Y.^2)+eps;
Z=sin(R)./R;
contour(X,Y,Z,25)
xlabel('x label'),ylabel('y label')
title('contour plot')

8.5

161
contour plot

8
6
4

y label

2
0
-2
-4
-6
-8
-8

-6

-4

-2

0
x label

8.5.2
,
, ,
.
:
contour3(X,Y,Z,n)

, ,
, n
.

[1] , [8,8] [8,8] ,

z = sin( x 2 + y 2 ) / x 2 + y 2 ,
,
x=-7.5:0.5:7.5;
y=x;
[X,Y]=meshgrid(x,y);

162

R=sqrt(X.^2+Y.^2)+eps;
Z=sin(R)./R;
contour3(X,Y,Z,25)
xlabel('x label'),ylabel('y label'),zlabel('z label')
title('contour3 plot')
contour3 plot

z label

0.5

-0.5
5
5

0
-5

-5

y label

x label

8.6

.
(i, j)
{xi , yi } (x, y),

f(x, y) .
, ,
.

8.6

163

8.6.1

,

xy,
.
dataf ,
pcolor(dataf)

rand
,
,
n=50;
dataf=rand(n);
pcolor(dataf);
axis square;

50
45
40
35
30
25
20
15
10
5
5

10

15

20

25

30

35

40

45

50

164

8.6.2
,

xy,
xy.
,
stem3(dataf)

dataf .

rand
,
,
n=6;
dataf=rand(n);
stem3(dataf);

1
0.8
0.6
0.4
0.2
0
6
4
2
0

8.7

165

8.7
MATLAB
.
,
help.
,
,
xy,
,
.
MATLAB, ,
meshgrid,
, .

quiver(X,Y,DX, DY)

, x y
, DX, DY x y
.

[1] {y, x},


[1,1] [1,1] , :
x=-1:0.1:1;
y=x;
[X,Y]=meshgrid(x,y);
DX=Y;
DY=X;
quiver(X,Y,DX,DY)

166

1.5

0.5

-0.5

-1

-1.5
-1.5

-1

-0.5

0.5

1.5

8.8
8.1-8.6
. 6.4, MATLAB
.

, 8.1-8.6. ,
,
.
8.8.1 ezplot3
ezplot3 plot3,

.
ezplot3( @(t) f1(t), @(t) f2(t), @(t) f3(t), [ tmin, tmax] )

8.8

167

{f1(t),
f2(t), f3(t)}, t min < t < t max .
t, [0, 2].

{sin(t ),cos(t ), t} , t

0<t<8, ,
ezplot3(@(t) sin(t),@(t) cos(t),@(t) t, [0,8*pi]);
x = sin(t), y = cos(t), z = t

30
25

20
15
10
5
0
1
0.5

1
0.5

-0.5
y

-0.5
-1

-1

8.8.2 ezmesh
ezmesh
z = f ( x, y ) , mesh.


ezmesh(@(x, y) f(x, y), [xmin, xmax, ymin, ymax])


[ x min < x < x max] [ y min < y < y max] .
x y,
[2 < x < 2 ] [2 < y < 2 ] .

168

ezmesh, ezmeshc,
meshc.

z = sin( x 2 + y 2 ) / x 2 + y 2 ,
[8,8] [8,8] , ,
ezmesh(@(x,y) sin(sqrt(x.^2+y.^2))./...
(sqrt(x.^2+y.^2)+eps))
sin(sqrt(x 2+y 2))/(sqrt(x 2+y 2)+eps)

0.5

-0.5
5
5
0

0
-5

-5
x

8.8.3 ezsurf
ezsurf
z = f ( x, y ) , surf.


ezsurf( @(x, y) f(x, y), [xmin, xmax, ymin, ymax])

8.8

169


[ x min < x < x max] [ y min < y < y max] .
x y,
[2 < x < 2 ] [2 < y < 2 ] .
ezsurf, ezsurfc,
surfc.

z = sin( x 2 + y 2 ) / x 2 + y 2 ,
[8,8] [8,8] , ,
ezsurf(@(x,y) sin(sqrt(x.^2+y.^2))./...
(sqrt(x.^2+y.^2)+eps))
sin(sqrt(x 2+y 2))/(sqrt(x2+y 2)+eps)

0.5

-0.5
5
5
0

0
-5

-5
x

8.8.4 ezcontour
ezcontour
xy, contour.

ezcontour( @(x, y) f(x, y), [xmin, xmax, ymin, ymax], )

170


[ x min < x < x max] [ y min < y < y max] . N
. x
y, [2 < x < 2 ] [2 < y < 2 ] .
ezcontour, ezcontour3,
contour3.

z = sin( x 2 + y 2 ) / x 2 + y 2 ,
[8,8] [8,8] , ,
ezcontour(@(x,y) sin(sqrt(x.^2+y.^2))./...
(sqrt(x.^2+y.^2)+eps),20)

sin(sqrt(x2+y2))/(sqrt(x 2+y 2)+eps)


6

-2

-4

-6
-6

-4

-2

0
x

8.8

171

172

, , ,
, .

.
. ,
,
. , 2,
, .
MATLAB
, ,
(transpose), (det), (inverse),
(trace), (eigenvalues) (eigenvectors)
, ...
, .

9.1

173

9.1
MATLAB m 1 - m
, 1 n - n .

.
,
MATLAB.
, ,
a=[5;2;4];
b=[8;2;7];

1. . a b,
dot(a, b)

a, b
dot(a,b)
ans =
72
dot(b,a)
ans =
72

, dot(a,b)=dot(b,a).
2. . a b, ,

cross(a, b)

a, b

174
cross(a,b)
ans =
6
-3
-6
cross(b,a)
ans =
-6
3
6

, cross(a,b)=cross(b,a).
3. . a,

norm(a)

a, b
norm(a)
ans =
6.7082
norm(b)
ans =
10.8167

, , ,
acos(dot(a, b)/(norm(a)*norm(b)))
ans =
0.1244

9.2

175

acosd(dot(a, b)/(norm(a)*norm(b)))
ans =
7.1250

9.2
MATLAB
, . , ,
A=[7, 7, 6; 1, 9, 4; 5, 4, 0];
B=[0 ,6, 0; 7, 5, 9; 8, 6, 2];

1. .
,
.
MATLAB * ,
, ,
c*A

A*c

,
2.5.
[1] ,
,
3*A
ans =
21
3
15

21
27
12

18
12
0

A*3
ans =
21

21

18

176
3
15

27
12

12
0


,
.
2. () . ()
, ,
()
. , ,
,
A+B -

,
2.5.
[2] , ,
,
A+B
ans =
7
8
13

13
14
10

6
13
2

13
14
10

6
13
2

B+A
ans =
7
8
13


,
.

9.2

177

3. .
, mxn nxk,
,
mxk.
, , ..
:
A*B

[3] , ,
,
A*B
ans =
97
95
28
B*A
ans =
6
99
72

113
75
50

75
89
36

54
130
118

24
62
72


,
.
4. .
2.5 , A. ,

. .
,

.
A

178

.
.
[4]
,
A.'
ans =
7
7
6

1
9
4

5
4
0

7
7
6

1
9
4

5
4
0

A'
ans =

1 i


i 1
sc=[1 i; i 1];
sc.'
ans =
1.0000
0 + 1.0000i

0 + 1.0000i
1.0000

sc'
ans =
1.0000
0 - 1.0000i

0 - 1.0000i
1.0000

[5]
transopse( A1 * A2 *...* An ) = transpose( An ) *...* transpose( A2 ) * transpose( A1 ) ,
,

9.2
(A*B)'
ans =
97
113
75

95
75
89

28
50
36

B'*A'
ans =
97
113
75

95
75
89

28
50
36

179

5. .
,
det(A)

[6] , ,
,
det(A)
ans =
-218
det(B)
ans =
348
det(A*B)
ans =
-75864
det(B*A)
ans =
-75864
det(A)*det(B)
ans =
-75864

180


,
.
6. .
,
.
:
inv(A)

[7] , ,
, [6],
,
inv(A)
ans =
0.0734
-0.0917
0.1881

-0.1101
0.1376
-0.0321

0.1193
0.1009
-0.2569

inv(B)
ans =
-0.1264
0.1667
0.0057

-0.0345
0
0.1379

0.1552
0
-0.1207

inv(A)

inv(A)*A
ans =
1.0000
0.0000
-0.0000

-0.0000
1.0000
0

-0.0000
0
1.0000

[8]

9.2

181

Inverse[ A1. A2 ... An ] = Inverse[ An ]...Inverse[ A2 ].Inverse[ A1 ] ,


,
inv(A*B)
ans =
0.0231
0.0122
-0.0349

0.0042
-0.0183
0.0222

-0.0584
0.0199
0.0456

inv(B)*inv(A)
ans =
0.0231
0.0042
0.0122
-0.0183
-0.0349
0.0222

-0.0584
0.0199
0.0456

,
det(Inv(A))=1/det[A]
,
det(inv(A))
ans =
-0.0046
1/det(A)
ans =
-0.0046

7. ..
, n
n

n = *A*A* ...*A
n
A n = (inv( A)) n

182

[9] , ,
,
A^3
ans =
1088
566
574

2106
1356
1096

1060
632
518

1088
566
574

2106
1356
1096

1060
632
518

A*A*A
ans =

A^(-3)
ans =
-0.0009
-0.0067
0.0153

-0.0068
0.0043
-0.0016

0.0103
0.0085
-0.0273

(inv(A))^3
ans =
-0.0009
-0.0067
0.0153

-0.0068
0.0043
-0.0016

0.0103
0.0085
-0.0273

8. expm(A). A,
expm(A)

/ n! .

n =0

.
,

9.2

183

, .
0
[10] exp m(

) ,
0

s2=[0 pi; -pi 0];


expm(s2)
ans =
-1.0000
0.0000
-0.0000
-1.0000

1 0
[11] exp m(i
) ,
0 1
s2=[1 0; 0 -1];
expm(-i*pi*s2)
ans =
-1.0000 - 0.0000i
0

0
-1.0000 + 0.0000i

[12] ,
,
expm(A)
ans =
1.0e+006 *
0.5392
0.3169
0.2788

1.1442
0.6729
0.5917

0.5494
0.3231
0.2841

. expm(A+B)
expm(A)*expm((B).
, *=*,
,
expm(A+B)=expm(A)*expm(B)

184

[13] ,
expm(A+B)
ans =
1.0e+012 *
1.9147
2.4912
1.8062

2.6758
3.4815
2.5242

1.6782
2.1835
1.5831

expm(A)*expm(B)
ans =
1.0e+012 *
1.7147
1.9593
1.0083
1.1522
0.8866
1.0131

1.4048
0.8261
0.7264

expm(A+A)
ans =
1.0e+012 *
0.8065
0.4742
0.4170

1.7120
1.0066
0.8852

0.8220
0.4833
0.4250

expm(A)*expm(A)
ans =
1.0e+012 *
0.8065
1.7120
0.4742
1.0066
0.4170
0.8852

0.8220
0.4833
0.4250

9. . (trace)
,

trace(A)

[14] , ,
,

9.3

185

trace(A)
ans =
16
trace(B)
ans =
7

.
trace(A*B)=trace(B*A), trace(A*B) trace(A)*trace(B)

[15] ,
trace(A*B)
ans =
208
trace(B*A)
ans =
208
trace(A)*trace(B)
ans =
112

9.3
nxn, x b
n,
A*x=b

ij a11 a12 a1 n yz ij x1 yz ij b1 yz
jj
zj z j z
jj a21 a22 a2 n zzz jjj x2 zzz jjj b2 zzz
jj
zj z nj z
jj

zzz jjj zzz jjj zzz
j
z
k a n 1 a n 2 a n n { k xn { k b n {

186

A , det(A)0,
x , A b,
,
x=inv(A)*b

inv(A) .

x=\b
:
1.
, A\b
,

( ).
2.

, .. ,
( )
.
,
.


, MATLAB
LINSOLVE
.

x= linsolve(A, b)


y*B=c

9.3

187

nxn, y c
n,

y=c/B

1 1 x 1
[1]
=
1 2 y 2
A1=[1 1; 1 -2];
b1=[1;2];
x=A1\b1
x =
1.3333
-0.3333


x=inv(A1)*b1
x =
1.3333
-0.3333

[2] 8.2, b
b=[6;3;1], A*x=b
b=[6;3;1];
A\b
ans =
0.2294
-0.0367
0.7752


inv(A)*b

188
ans =
0.2294
-0.0367
0.7752

[3] 8.2, c
c=[6,3,1], x*A=c

c=[6 3 1];
x=c/A
x =
0.3532

-0.2798

0.7615


x=c*inv(A)
x =
0.3532

-0.2798

0.7615

9.4
A,
n n , ,
A bi = i bi , i=1, 2, , n
{bi} {i} . {bi}
- n ,
. MATLAB
, .

E=eig(A)

9.4

189

-, ,


,
[V, D]=eig(A)

D,
, V
, :
D(i, i) i-, V(:, i) i-
A.

*V = V*D

1 i
[1] s1 =
,
i 1
>> s1=[1,i; -i, -1]
s1 =
1.0000
0 + 1.0000i
0 - 1.0000i -1.0000
[V,D]=eig(s1)
V =
0 + 0.3827i
0 + 0.9239i
-0.9239
0.3827
D =
-1.4142
0
0
1.4142

190

s1*V
V*D ,
s1*V
ans =
0 - 0.5412i
1.3066

0 + 1.3066i
0.5412

0 - 0.5412i
1.3066

0 + 1.3066i
0.5412

V*D
ans =

[2] 9.2
[V,D]=eig(A)
V =
-0.3474
0.7704
-0.2678
-0.5318
0.8987
0.3516
D =
-3.1249
0
0
4.9065
0
0

-0.7874
-0.4630
-0.4071
0
0
14.2183

, ,
,
D(2,2)
ans =
4.9065
V(:,2)
ans =
0.7704
-0.5318
0.3516

191

10

MATLAB

MATLAB .
, ,
.

10.1
MATLAB .
,
, ,
, .
10.1.1
,
. n-
,

an x n + an1 x n1 + ... + a1 x + a0 .
,
x, [an , an1 ,..., a1 , a0 ] , MATLAB
,

MATLAB

192

, ..
n+1 ,
p = [an , an1 ,..., a1 , a0 ] .
,
, ,

. ,
,
.


,
p1=[1 2 3 0 1]
p2=[1 0 4 2 1]

10.1.2

: ,

,
,
. ,
, p1 p2 , ,

p1 + p2
,
,
,
.
.. [1, 2,3, 4]

10.1

193

[0,0,1, 2,3, 4] ,

. ,
p1 = [an , an1 ,..., a1 , a0 ],
p2 = [bn , bn1 ,..., b1 , b0 ]
,
P = p1 + p2 = [an + bn , an1 + bn1 ,..., a1 + b1 , a0 + b0 ] .

10.1.1
p1+p2
ans =
2

10.1.3
, p1 p2 ,

,
, ,
conv( p1 , p2 )

10.1.1
conv(p1,p2)
ans =
1
2

10

18

10.1.4
,
,
roots(p)

p .

MATLAB

194

-,
-.

10.1.1
r1=roots(p1)
r1 =
-1.1102 + 1.3935i
-1.1102 - 1.3935i
0.1102 + 0.5504i
0.1102 - 0.5504i
r2=roots(p2)
r2 =
0.2643 + 1.9904i
0.2643 - 1.9904i
-0.2643 + 0.4221i
-0.2643 - 0.4221i

,
r -,

p(1)*poly(r)

p(1)
.


poly(r1)
ans =
1.0000
poly(r2)

2.0000

3.0000

0.0000

1.0000

10.1

195

ans =
1.0000

0.0000

4.0000

2.0000

1.0000

10.1.5
, p,
x=x1
.
polyval(p, x1)


, x,

polyval(p, x)

10.1.1
polyval(p1,sqrt(2))
ans =
16.6569
polyval(p2, [1 2 3 4])
ans =
8
37
124
329

10.1.6
, p, ,
polyder(p)

,
polyint(p, K)

K .

MATLAB

196

10.1.1
polyder(p1)
ans =
4
6
polyint(p2,2)
ans =
0.2000
2.0000

1.3333

1.0000

1.0000

10.1.7
,
,

,

.
(fitting).

, .
,
[ xi , yi ] , y

x,
x = [ x1 , x2 ,..., xN ] , y = [ y1 , y2 ,..., yN ] .
, ,
[cn , cn1 ,..., c0 ]
n- ,

Pn (cn , cn1 ,..., c0 , x) = cn x n + cn1 x n1 + ... + c1 x + c0


,

10.1

197

J (cn , cn1 ,..., c0 ) = i=1 ( P(c1 , ..., cn , xi ) yi ) .


N

MATLAB ,

p=polyfit(x, y, n)

x y , n
.
p - n+1 ,
, ,
p(1) x n + p(2) x n 1 + ... + p (n) x + p(n + 1)
,
, ..

, ,
,
polyval(p, x)

y = 4 x 2 + x 4 ,

4(rand 0.5) .

,
x=linspace(0,3,20);
y=-4*x.^2+x.^4;
y=y+4*(rand(1,20)-0.5);
plot(x,y)
pol=polyfit(x,y,4)
y1=polyval(pol,x);
plot(x,y,'+',x,y1,'r--')

MATLAB

198

50

40

30

20

10

-10

0.5

1.5

2.5

10.2

.
, ,
. MATLAB
,
,
fzero(@f, x0)

x0
. ,
,

x0, ,
.
,
.

10.2

199

[1] ,
(3 x) exp( x) 3 ,
>> fun=@(x) (3-x)*exp(x)-3;
>> fzero(fun, 3)
ans =
2.8214

[2] x 2 + 4 , x0=1
,
>> ff1=@(x) x.^2+4
ff1 =
@(x) x.^2+4
>> fzero(ff1, 1)
Exiting
fzero:
aborting
search
for
an
interval
containing a sign change
because NaN or Inf function value encountered during
search.
(Function value at -1.7162e+154 is Inf.)
Check function or try again with a different starting
value.
ans =
NaN

,
,
>> fzero(ff1, i)
ans =
0 + 2.0000i


.
,

MATLAB

200
fzero(@f, [x1, x2])

[x1, x2],
.
[3] x3 + 2 x + 3 ,
[0, 3],
>> ff2=@(x) -x.^3+2*x+3;
ff2 =
@(x) -x.^3+2*x+3
>> fzero(ff2, [0 3])
ans =
1.8933

: ,
fzero, ,
,

.

10.3
f(x)
,
.
MATLAB
.
.
10.3.1
f(x),
[x1, x2],

10.3

201

fminbnd(@f, x1, x2)

.
, ,
[X, FVAL]=fminbnd(@f, x1, x2)

,
.

[1]

x + 5six( x) ,

4 5,
[X, FV]=fminbnd(@(x) x+5*sin(x), 4, 5)
X =
4.5110
FV =
-0.3879

,
,
,
,
.
10.3.2
fminbnd
f ( x) ,

f ( x) .
[2] x + 5sin( x) ,
[6, 9],

MATLAB

202

[X, FV]=fminbnd(@(x) -(x+5*sin(x)), 6,9)


X =
8.0553
FV =
-12.9543

10.4
,
, ,

10.4.1
,
y, n
x,

1 n
I = i=2 ( yi + yi1 )( xi xi1 ) .
2
MATLAB
trapz(x, y)

,
trapz( y)

( xi xi 1 ) , x,
.

,
rand. y 1000
, rand.

10.4

1000

203

y (n)dn /1000 .

,
y=rand(1000,1);
trapz(y)/1000

ans =
0.5012

, , 0.5.
10.4.2

, f ( x) , x1 x2,

x2

x1

f ( x)dx ,

quad(@f, x1, x2)

, Simpson
.


f=@(x) sqrt(1+x.^2+x.^4)
f =
@(x) sqrt(1+x.^2+x.^4)
quad(f,0,1)
ans =
1.2197

1 + x 2 + x 4 dx .

MATLAB

204

10.5 (Interpolation)
,

.
, ,
.
,
10.1.7. .

,

, ,
.

( ).
,
y,
x,
{xi , yi } , , { yi } ,
, {xi } .
n+1 , {{xi , yi } , i=1,n+1}, n- ,
n

f ( x) = ck x k .
0

n+1 {ck , k = 0,1,..., n} n+1


, ( y i = f ( xi ) , i=1,n+1),
, ,

10.5 (Interpolation)
x 1n . . . x 1
n
x 2 ... x 2
.
.
.

.
.
.
n
x n +1 ... x n +1

1 cn

1 c n 1
. .

. .

1 c 0

y1

y2
= .

.
y

n +1

205


{ci } . , ..
Newton Lagrange,
, .
, , n,
n-1,
.
,
.
,
,
.
MATLAB :
, x y
x y ,

, xi.
, ,

yi=interp1(x, y, xi)

, ,
:
1.
,

MATLAB

206

,
.
2.

, ,
.


f ( x) = x + cos( x) , ,

,
x=0:0.05:4*pi;
y=x+cos(x);
zx=linspace(0,4*pi,21);
% zy= interpolating values of y at the positions of zx
zy=interp1(x,y,zx);
plot(x,y,zx,zy,'*')

14

12

10

0
0

10

12

14

MATLAB
, help,

10.5 (Interpolation)

207

'nearest' - ,
'linear' - ,
'spline' - spline (SPLINE),
'pchip' - .
MATLAB
,
yi=interp1(x, y, xi, method)


f ( x) = x + cos( x) ,

SPLINE, ,
x=0:0.05:4*pi;
y=x+cos(x);
zx=linspace(0,4*pi,21);
% zy= interpolating values of y at the positions of zx
zy=interp1(x,y,zx,'spline');
plot(x,y,zx,zy,'*')

14

12

10

0
0

10

12

14

MATLAB

208

10.5.1
MATLAB,
SPLINE PCHIP,

, , ,
pp=spline(x,y)

pp=pchip(x,y)

, ..
,
,
, xi, ,
yi=ppval(pp, xi)


,
f ( x) = x + cos( x) ,


,
x=0:0.05:4*pi;
y=x+cos(x);
zx=linspace(0,4*pi,21);
pp=spline(x,y);
zy=ppval(pp,zx);
plot(x,y,zx,zy,'*')

10.5 (Interpolation)

209

14

12

10

0
0

10

12

14

10.5.2
, ,

.
1)
,
,
ppd=fnder(pp)

,

, xi,
yid=ppval(ppd, xi)


f ( x) = x + cos( x) ,

MATLAB

210
x=0:0.05:4*pi;
y=x+cos(x);
zx=linspace(0,4*pi,100);
pp=spline(x,y);
ppd=fnder(pp)
zyd=ppval(ppd,zx);
plot(zx,zyd

2
1.8
1.6
1.4
1.2
1
0.8
0.6
0.4
0.2
0

10

12

14

1)

, ,

ppi=fnint(pp, K)


. ,
.


, xi,
yii=ppval(ppi, xi)

10.6 Fourier

211


f ( x) = 10c os( x) ,


,
x=0:0.05:4*pi;
y=10*cos(x);
zx=linspace(0,4*pi,100);
pp=spline(x,y);
ppd=fnint(pp)
zyd=ppval(ppd,zx);
plot(zx,zyd)

10
8
6
4
2
0
-2
-4
-6
-8
-10

10

12

14

10.6 Fourier
T,
f (t ) f (t ) = f (t + nT )
t n, Fourier.
f (t )
t, ,
,
f = [ f (1), f (2),..., f ( N )] .

MATLAB

212

f ( n + N ) = f ( n) ,
Fourier Fourier, ,
MATLAB, :
N

F (k ) = f (n) exp(i 2 (n 1) (k 1) / N )

k = 1, 2,..., N

n =1

Fourier ,
f ( n) =

1
N

F (k ) exp(i 2 (n 1) (k 1) / N ),

n = 1, 2,..., N .

k =1

F (k + N ) = F (k ) .

k = 2 (k 1) / N ,
k = 1, 2,..., N ,
,

Nq = 2 ,
Nq Nyquist.
, ,
,
,

.
f (n) ,
f (n) = f * (n) , f * (n) f (n) ,

10.6 Fourier

213

F (k )* = F ( k ) = F ( N k ) , | F (k ) |=| F ( k ) |=| F ( N k ) |
,
, /2, [0, ].
10.6.1
, MATLAB,
, ,
, f ,

f = [ f1 , f 2 ,..., f N ] ,
,
F = [ F1 , F2 ,..., FN ] , :
F=fft(f)

-
= (2 / N )( 1) .
,
,
, Fourier
ff=ifft(F)
. Fourier

, N = 2n , MATLAB
Fourier (Fast Fourier Transform)
. ,

.

MATLAB

214

10.6.2
, f (t ) = mod(t ,1) 0.5 , ,
ezplot(@(t) mod(t,1)-0.5, [-3, 3])
mod(x,1)-0.5
0.5
0.4
0.3
0.2
0.1
0
-0.1
-0.2
-0.3
-0.4
-0.5
-3

-2

-1

0
x

, Fourier
+
+
1
i
f (t ) =
sin(2 kt ) =
(exp(i 2 kt ) exp( i 2 kt )) .
k =1 k
k =1 2k
, t, t = n / N n, N ,
,
+
i
f ( n) =
(exp(i 2 nk / N ) exp(i 2 nk / N ))
k =1 2k
+

i
(exp(i 2 nk / N ) exp(i 2 n( N k ) / N ))
k =1 2k

Fourier, N = 26 . ,

10.6 Fourier

215

MATLAB, ,
,
, k = 1, 2,..., N ,
N

i
(exp(i 2 nk / N ) exp(i 2 n( N k ) / N )) ,
k =1 2k

f ( n) 

N=2^6;
x=linspace(0,1,N);
y=x-0.5;
fy=fft(y);
k=1:2:N-1;
fy1=1./(2*pi*k)-1./(2*pi*(N-k));
plot(imag(fy(2:end))/N)
xlabel('k')
ylabel('amplitude, imag(F(k))/N')
hold on
plot(k,fy1,'+r')
hold off

0.2
0.15

amplitude, imag(F(k)) / N

0.1
0.05
0
-0.05
-0.1
-0.15
-0.2

10

20

30

40
k

50

60

216

MATLAB


,

. ,
,
k = 1, 2,..., N , N + 1, N + 2,... ,
[0, 2],
2.

10.7
MATLAB
, ,
.
MATLAB ,

,
, ODE (Ordinary Differential Equations), BVP
(Boundary Value Problems), PDE (Partial Differential Equations), DDE
(Delay Differential Equations).

217

. , . ,
Mathematica. ,
(2004).
. , . , Mathematica .
(2005).
Matthew P. Coleman, An Introduction to Partial Differential
Equations with MATLAB (Chapman & Hall/Crc Applied Mathematics &
Nonlinear Science), Publisher: Chapman & Hall/CRC (2004).
Timothy A. Davis, Kermit Sigmon, MATLAB Primer, Seventh
Edition, Publisher: Chapman & Hall/CRC (2004).
Amos Gilat, MATLAB: An Introduction with Applications 2nd
Edition, Publisher: Wiley (2004).
Duane C. Hanselman, Bruce L. Littlefield, Mastering, MATLAB 7,
Publisher: Prentice Hall (2004) .
Thomas L. Harman, James B. Dabney, Norman John Richert,
Advanced Engineering Mathematics with MATLAB, Publisher: ThomsonEngineering (1999).
Desmond J. Higham (Author), Nicholas J. Higham, MATLAB Guide,
Publisher: SIAM, (2005).
Brian R. Hunt, Ronald L. Lipsman, John E. Osborn, Jonathan M.
Rosenberg, Differential Equations with Matlab, Publisher: John Wiley &
Sons (2005).
Wendy L. Martinez, Angel R. Martinez, Exploratory Data Analysis
with MATLAB, Publisher: Chapman & Hall/CRC (2004).
Stephen R. Otto, James P. Denier, An Introduction to Programming
and Numerical Methods in MATLAB, Publisher: Springer (2005).

218

Rudra Pratap, Getting Started with MATLAB 7: A Quick Introduction for Scientists and Engineers, Publisher: Oxford University Press (2005).

219

MATLAB (
), .

-, 15
", 15
%, 33
&, 81
&&, 81
*, 15
/, 15
@, 94
^, 15
|, 82
||, 82
~, 82
~=, 15
+, 15
<, 15
<=, 15
=, 15
==, 15
>, 15
>=, 15
a_initial:a_step:a_final, 39
abs, 23

acos, 23
acosd, 23
acosh, 23
angle, 23
asin, 23
asind, 23
asinh, 23
atan, 23
atand, 23
atanh, 23
box, 106
break, 79
ceil, 31
chop, 32
clear, 27
clear all, 27
clear function, 27
clear global, 27
clear variable, 27
command history, 131
command window, 129
conj, 23

212
continue, 78
contour, 151
contour3, 152

conv, 185
cos, 23
cosd, 23
cosh, 23
cputime, 139
cross, 164
current directory, 130
delete, 135
det, 170
diag, 41
diary, 138
disp, 34
dot, 164
echo, 30
eig, 179
end, 45, 68, 70, 74, 75
eps, 24, 25
error, 92
errorbar, 119
exp, 23
expm, 173
eye, 41
ezcontour, 160
ezmesh, 158
ezplot, 116, 126, 127
ezplot3, 157
ezpolar, 125
ezsurf, 159
Fast Fourier Transform, 205

fft, 205
find, 58
fitting, 188
fix, 32
floor, 32
fminbnd, 193
fnder, 201
fnint, 202
for, 70
format, 132
FORMAT, 132
fzero, 190, 192
help, 32
hist, 120
hold on, 113
if, 67
ifft, 205
imag, 23
inf, 20
input, 137
interp1, 197, 199
Interpolation, 196
inv, 171
length, 38
linsolve, 177
linspace, 39
load, 134
log, 23
loglog, 122
magic, 42
max, 23, 60
mean, 101


mesh, 144
meshc, 146
min, 23, 60
mod, 24
NaN, 20
norm, 165
numel, 38
ones, 40
pause, 137
pchip, 200
pcolor, 154
plot, 103, 111
plot3, 141, 142
polar, 124
poly, 186
polyder, 187
polyfit, 189
polyint, 187
polyval, 187, 189
ppval, 200, 201, 202
prod, 61
quad, 195
quiver, 156
rand, 24, 42
randn, 25, 43
real, 23
realmax, 21
realmin, 21
return, 90
roots, 185
round, 32
save, 134, 135

213
scatter, 109
script files, 29
semilogx, 122
semilogy, 122
sin, 23
sind, 23
sinh, 23
size, 37
sort, 61
spline, 200
sqrt, 23
std, 101
stem, 110
stem3, 155
subplot, 114
sum, 60
surf, 147
surfc, 149
surfl, 150
tan, 23
tand, 23
tanh, 23
tic, 139
toc, 139
trace, 175
trapz, 194
var, 102
while, 74
workspace, 130, 133
zeros, 40

214
() , 167
, 184
,
46
, 56
, 168
,
171
o, 96
, 135

, 118
, 194
, 15

, 97
,
131
, 15
, 14,
130
, 70
() , 54
, 185
, 151

, 144
,
141
,
153

,
103
,
116, 157
, 63
, 135
, 27
, 34

Fourier, 203
, 102

, 105

, 132
, 107
, 156
, 44
, 64
,
133
, 56
, 172

, 38
, 21
, 33
,
130
, 32
, 77
, 67

, 139
break, 79
continue, 78
expm, 173
while, 74
, 21
, 19
,
190
, 164
, 70
,
57
, 22
, 164
, 105
, 137

, 122
, 179
' , 15
, 120
, 175
-, 37
-, 37
, 18
, 80
, 82
, 81
, 82

215
,
208
,
176
- , 29
, 85
, 19
, 101
, 165
, 17
, 95
-, 94
, 93
, 170

, 26
, 35

, 187
, 14, 129
, 126
, 86
, 87
, 196
, 126
, 106
, 106
, 124

, 52
, 168
, 114

216
, 111, 142
, 183
, 164
, 51
, 166
, 18

, 188
,
31
, 52
, 17
, 185
,
99
, 168
, 183

, 101
eps(x), 25
rand, 24
randn, 25
, 49

, 28
, 28
, 28
Nyquist, 204
, 15
, 90
, 105
, 106

, 200
,
192
, 192
, 193

, 144, 147
, 36
, 101
,
187
, 73, 76
, 37

You might also like