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

. . , . .

MATLAB

2001

32.97:53
53.072
. ., . .
MATLAB: . / . -. , 2001. 173 .

.
, MATLAB
.
19972000 , N274.

..
.

c


, 2001



20 . ( Windows-95). ,
. , , , , . ,
,
.
, [1], , , : .., .., ..,
.. . , , 1986;
.., .. : . , , 1992.

( ), MATLAB MathWorks, . [2-6],
. , Windows 1 , , , , , . ,

.
m-.
. , ( , , ), - ( , , ), ().
1

MATLAB UNIX.

, , :
.

, MPP(Modeling of Physics Phenomena),
. , , .
, ( , ).
.

1.
1.1.

( ) 2 .
.
MATLAB ( , ).
, , , . , ,
. ,
,
, ,
. . , .
1.2.
. 1.3, 1.4 MATLAB. ,
-
2

, , ..

(, MathCad)
. .
.
, , .
1.3.

MATLAB

MATLAB (MATrix LABoratory) MathWorks. Windows 3.1 ( 4


4.2) Windows-95(98) ( 5.0).
,
, . MATLAB, ,

>> y=sin(0.125)
ENTER ,
y=
0.1247
>>

.
.
(. E), . ( ) ,
.
.
, 2- 3 . . , ,
. E.

1.4.

MATLAB

MATLAB, ,
,
, (. 1),
- >>.
MATLAB

. 1.
File , New , Open M-file - -
- , . , / m-.
m- , ,
.m, . , , , , , , (
File /) .
(. . 1)
. ,
- (Copy), (Open),
(Print) .. Path Browser,
, Workspace Browser,
.
help, ,
6

Enter3, , . help
<_> .
, help eig eig - .
MATLAB
demo.
, , MATLAB, .
, . , . , , , . , ,
, .
, , save <_>.
<_>.mat. ,
load <_> . clear ,
. clear , ,
.
quit
File/exit.

2.
2.1.

,
:


x = a1 cos(1t)
y = a2 cos(2t)

, , , , ,
Enter .
7

1/2 - , . ,
.
MATLAB,
. diary <_>.
,
- . MATLAB ,
%, , ..
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% , %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
>> a1=1.2; %
>> a2=1.0; %
>> w1=1.5; %
>> w2=1.0;
% whos
% .
% ,
% , clear.
>> whos
Name Size Elements Bytes Density Complex
a1
1 by 1 1
8
Full No
a2
1 by 1 1
8
Full No
w1
1 by 1 1
8
Full No
w2
1 by 1 1
8
Full No
Grand total is 4 elements using 32 bytes
% , whos,
% .
% ,
% 1x1.
% t
8

>> t=0:0.1:3.2;
% x, y
>> x=a1*cos(w1*t);
>> y=a2*cos(w2*t);
% y(x)
>> plot(x,y);
, plot(x,y)
. (x(i), y(i)), (x(i+1),
y(i+1)) .
.
, .
.
t. , : ( . , . 2.2) MATLAB.
, ,
1. , x=4:15
x=[4 5 6 7 8 9 10 11 12 13 14 15]. ,
( ) . 1, , ,
: t=0:0.1:3.2, .
( x
y) MATLAB: , , (). ( - cos)
( )
.

MATLAB .
, . 2.
, ,
, . .
, , . , ,
Del, BkSp, .
9

.
>> t=0:0.1:10;
>> x=a1*cos(w1*t);
>> y=a2*cos(w2*t);
>> plot(x,y);
, . 2.



. 
,




.



 





, . 2.
, .

 

1.
( ),
1 /2 a1
a2 .
-.
.m, MATLAB. ( diary).

10

%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%
%
a1=1.2; a2=1.0; w2=1.0;
% t
t=0:0.1:15; x=a1*cos(w2*t);
% w1
w1=1.25:0.25:2.0;
%
for k=1:4,
y=a2*cos(w1(k)*t);
% -
s=[w1/w2= num2str(w1(k))];
% ()
% subplot ,
%
subplot(2,2,k); plot(x,y); title(s);
end;
- . s=[w1/w2= num2str(w1(k))]; s1=w1/w2 -,
, .. . s2=num2str(w1(k)) num2str, w1(k)
. , , w1(1)=2.34, s2=num2str(w1(1))
s2=2.34.
s1 s2 s=[s1 s2].
(. , . 2), , , ,
.
s title , .
for...end
subplot(m,n,k), mxn , k
11

w1/w2=1.25

w1/w2=1.5

0.5

0.5

0.5

0.5

1
2

1
2

w1/w2=1.75
1

0.5

0.5

0.5

0.5

w1/w2=2

1
2

1
2

. 3. 4 .
. . 3. ,
.
2. 1 2 , .. cos(1t)
cos(1t + 1) cos(2t)
cos(2t + 2).
3. plot
comet (.
, . 8.1) ,
t .
2.2.

y(t) = a1 cos 1 t + a2 cos 2t.

12

, .
Beats y(t) t t0 tm . :
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%
%
% AXIS %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
a1=1.0;
%
a2=1.0;
%
w1=1.0;
%
w2=1.2;
%
t0=0;
%
tm=20;
%
N=600;
% /
T=tm-t0;
%
dt=T/N;
%
t=t0:dt:tm;
%
y=a1*cos(w1*t)+a2*cos(w2*t); %
plot(t,y);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

. ,
t 0 20. (, t 1 2),
axis([xmin xmax ymin ymax]),
xmin xmax - x, ymin ymax - y.
4 .
1.
.
?
4

, t y, ,
.
13

2.
?
3. 1  2 . , ,
(.. t) 10-20 .
2.3.

( , MATLAB)
, - ,
, . ( ) ,
(. 4)5 .
,
( Interface_Window)
.
(Drive_Beats Run_Problem) .
Drive_Beats, .
(, ..).
, , Run_Problem.
, ,
(
Run_Problem). ,
( ), , .
: a1 , a2 , 1 2, t1
t2 .
5

MATLAB , GUI Graphics User Interface ( ).


(. 9).
.
14

. 4.
.
Drive_Beats
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BEATS
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;%
%
a1=1; a2=1; w1=1; w2=1.2; t1=0; t2=20; N=600;
% N
global N;
%
VALUE(1)=a1;
VALUE(2)=a2;
VALUE(3)=w1;
VALUE(4)=w2;
VALUE(5)=t1;
VALUE(6)=t2;
%
15

%
NAME(1,:)= a1;
NAME(2,:)= a2;
NAME(3,:)= w1;
NAME(4,:)= w2;
NAME(5,:)= t1;
NAME(6,:)= t2;
%
%
Interface_Window(VALUE, NAME, Run_Beats)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

:
1. ( VALUE NAME)
;
2. ( ) VALUE NAME ;
3. - NAME ( )
, 8 ;
4. (m-), , , .
Run_Beats
Drive_Beats Interface_Window
. ,
( Drive_Beats),
UserData. ()
.
() ,
. UserData.
Run Run_Beats. UserData. , Run_Beats,
16

, Interface_Window.
Run_Beats Interface_Window , 6, ,
.
Exit , .
Run_Beats- , ,
RUN. - , . m- ( )
Interface_Window .
Run_Beats.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% , RUN %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function r=Run_Beats()
% , ,
% GLOBAL
%
global N;
%
%
h0=gcf;
% ,
% UserData
info=get(h0,Userdata);
%
a1=info.VALUE(1);
w2=info.VALUE(2);
w1=info.VALUE(3);
w2=info.VALUE(4);
t1=info.VALUE(5);
t2=info.VALUE(6);
%
6

, Interface_Window ,
.
17

%
tm=t2-t1;
%
dt=tm/N;
%
%
t=t1:dt:t2;
%
y=a1*cos(w1*t)+a2*cos(w2*t); %
%
cla;
%
axis([t1 t2 -2 2]); %
hl=line(t,y);
%
set(hl,color,b); %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
, Interface_Window UserData
VALUE info.VALUE, Run_Beats info=get(gcf,Userdata).

( )
Edit_Value.m Exit_Problem.m.
4. .
y(t)
y(t) = a1 cos(t) + a2 cos(2t + 2) + a3 cos(3t + 3)
a2 , a3 2, 3.
2.4.
, ,
.. ,
, .

y(x, t) = a cos(kx t)
a, k .
= 2/k, T = 2/. (
, kx t = 0) v = x/t = /k.

y(x, t) = a1 cos(k1x 1t) + a2 cos(k2x 2t)
18

(1)

x.
a2 = a1
y(x, t) = a(x, t) cos(kx t),

a(x, t) = 2a1 cos(dk x d t),


1
1
1
1
k = (k1 + k2 ), = (1 + 2 ), dk = (k1 k2 ), d = (1 2).
2
2
2
2
|a(x, t)| ;
u = d/dk, .
Wavepak ,
.. . ,
MATLAB. MATLAB
(. 8) [1, 2, 3, 5].
2.4.1.

, , , ( , ,
..) ( . , . 8.4)
- .
,
. ,
.
: (figure), (axes), (line). ,
. , .
, 7 ,
, , , .. . ,
, . , , . , line x y . / - ,
, handle.
7

19

set(handle, PropertyName, PropertyValue, ...). handle, PropertyName ()


. , , ( - g) line c h set(h, color, g).
- get, .
, -. , - , . (
MATLAB C++, .)
2.4.2.

MATLAB
. EraseMode ( ) , . ( WAVEPAK).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% WAVEPAK %
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t=0:0.1:100; %
x=0:0.3:30; %
k=1.3; w=0.9; n=length(t);
% t(1)
% x
y=cos(k*x-w*t(1))+cos(x-t(1));
h=line(x,y); %
% h
% () h
% color
set(h,color,g);
axis([0 30 -3 3]);%
axis manual;
20

%
% EraseMode xor.
% MATLAB,
% ( ,
% , ),
% , .
set(h,EraseMode,xor);
pause; % .
%
% .
% .
% pause(0),
%
for i=2:n;
% t(i)
y=cos(k*x-w*t(i))+cos(x-t(i));
% ,
% ()
% h XData (
% x- )
% y-.
% .
set(h,XData, x,YData,y);
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
. , , . () ,

().
5. , , . 2.3. , WAVEPAK ,
, ( Drive_Beats),
21

. 5. ,
Run_Beats.
,
WAVEPAK.

3.

. ( , , .) ,
, .
m,
l (. 5). , .
, , F = Av, F (t) = F cos t,
. ( ,
, , ).

x.

d2 x
dx
ml 2 = mg sin x Al
+ F cos x cos t.
dt
dt
22

(1)




, , 2 l/g.

t t = l/g ;
, , . (1)
d2 x
dx
=

sin
x

a
+ f cos x cos ,
(2)
d 2
d




l
l
F
A

,f=
, = 
(3)
a=
m g
mg
g
.
:
x( ) ( x(0), dx/d (0)) m, l, g, A, F, , a, f, . , . ,
.
, (2) ,
, ,
, (1)
. -308 (realmin) 308 (realmax),
. , ,
, .
(1) (2) .
,
. , 
, , g = 1 (.. l/g).
l = m = g = 1 (1), (2),
t, A, F . , a, f, , (2).
(3), l, m, g
. , mg,
F = mgf .
, f ,
. (3).
23


. .
m U = /R
,
. ( = m M, , m
, M ; m , = |qQ|,
q , |Q| 8 .)
d2 R
R
m 2 = 3.
dt
R
R0 (
, , R0 = 108 ,
R = 108 ).
 0
t0 = R03 m/ ( t0
,
). r
t = t0 , R = R0 r,
9
d2 r
r
=

.
d 2
r3
, c = 1.
, v/c. , . , m = 511
.

4.
.
.
, .
8

,
, , .
9
=1, U.
24

.
x = a cos(0t + 0), 0 , a , 0 . (
x. ,
. 3, .. m l , 0 = 1.)

x + x = 0,
x, .

,
. .
4.1.


(x, p), p = x .
(x, p) , p 10 , ,
x = x(t), p = p(t),
.
, , x(0) p(0).
, . , ,
. .
,
, x < . ,
(, p) (, p) , , x =
x = ,
x < ,
< p <
10

,
ml2 x,
m = l = 1.

25

. , ,
, , .
,
,
.
.
, ,
, . (,
, .)
,
(3.1),(3.2).

x + sin x = 0.

(1)

( ),
.

x = p,
p = sin x.

(2)

: t,
t
x(t + t) = x(t) + x(t)t

p(t + t) = p(t) + p(t)t,

x p (2). , ` x(t) p(t). ` ,


, .

1
1
3
t t, t + t, t + t,
2
2
2

t,

t + t,

t + 2t,
26

5
t + t, ...,
2
t + 3t, ...

( B).
.
(1) ,
t.
(.. , x(t) p(t) ).
(,
, ). , (,
). ,
t. , ,
.
MATLAB,
.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
%
%
x1=2.2;
%
p1=0.0;
%
dt=0.025;
%
axis([-pi pi -pi pi]);
%
hl=line(x1,p1);
%
%
% EraseMode,none -
% LineStyle,: -
%
% Color,r
-
set(hl,EraseMode,none,LineStyle,:,Color,r);
grid on;
%
pause;
% ,
%
%
while 1
%
27

%
x2=x1+p1*dt;
p2=p1-sin(x2)*dt;
%
if x2> pi
x2=x2-2*pi;
end;
if x2< -pi
x2=x2+2*pi;
end;
%
set(hl,XData,x2,YData,p2);
%
x1=x2;p1=p2;
end;
( t dt, pi=.) ,
x=x-p*dt/2, ,
x p
. ,
, , x p
.
.
. ,
, ( pause). , . , Ctr+C.

11 .
1. dt, , (,
, ). ,
11

, , , () m-, . . ,
.
28

,
.
.
(. . 2.3), , .
, , .
, (/ )12 .
2. , . E
x, (E, x) (p, x) ( )
, - .
( subplot,
. . 2.1). .
1.
.
................
subplot(2, 1, 1);
axis([-pi pi -pi pi]);
hl=line(x,p);
...............
subplot(2, 1, 2);
axis([-pi pi 0.8 1.2]);
he=line(x,E);

% 1-
%
% 1-
% 2-
%
% 2-

2. , , , .
12

, x p
,
.

29

.............................
set(hl,XData,x,YData,p);
set(he,XData,x,YData,E);
............................
3. ( )
, x(t). EraseMode - xor,
background none. , , ( set)
EraseMode background .
( ) x(t),
.
4. .
(x0, p0)
p = 0 T /2, , p.
(x0) .
5. , .
, ( ).

4.2.
( ), , (1).
x = sin x 2 x + f cos x sin t.
4.2.1.

(3)

,
, , . ,
30

. -
, , .
, .. , ,
, ,
( ). ,
.
6. . f , , , .
,
.
x(t) ,
t13 .
4.2.2.

(1) ( ),
. . () . (x  1)
F (t) =
f cos t 14 .
sin x x, x ( ):
x + 2x + x = f cos t.

x = Aeit + A eit,
13

(4)

,
, .
14
cos(x) ,
, , x  1 cos(x) 1.
31

F = (f /2)(eit + eit).
,
A=

f
2(1 2 + 2i)

(5)

x = a cos(t + ),

a = 2 |A| = f / (1 2 ) + 42 2 ,
2 1
.
= arcctg
2
() sin x, -
x :
x 2x + x x3/6 = f cos t.
, (4) x3 /6,
x3/6 = (A3e3it + 3A2Aeit + 3AA2eit + A3e3it)/6,
, e3it, (4) . , cos 3t, ,
.
, , ,
, eit ,
(5) (1 2 + 2i + |A|2/2)A = . 6.
f /2,
2

1.8
1.6
1.4

1.2
1

0.8
0.6
0.4
0.2

0
0.6

0.7

0.8

[(1 2 + |A|2 /2)2 + 42 2]|A|2 = f 2/4.

0.9

1.1

(6)

2, a()
MATLAB ( 6). , , [7].
32

a() f .
(3) , , ( ,
, ).
, ,
.
, f cos xsin t.
x0() (, ,
).
, t + .
, , wt,
: wt=wt+dwt, dwt=w*dt .
dw= :
dwt=dwt+dw*dt15 .
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% , lam --
% !
clear;
f=0.26;
lam=0.095;

%
%
% !
a=0.05:0.001:1.0; aa=a.^2;
d=4*lam^4-4*lam^2*(1-0.5.*aa)+f^2./(4*aa);
k=find(d>=0);
aa=aa(k);
15

t

(t)dt.
0

33

a=a(k);
d=d(k);
w1=sqrt(1-2*lam^2-0.5.*aa-sqrt(d));
w2=sqrt(1-2*lam^2-0.5.*aa+sqrt(d));
h=plot(w1,2*a, w2, 2*a);
axis([0.6 1.0 0 2])

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7.
, "". "",
(6).
4.2.3.

, , , . ( ) ,
. ,
, . !
, "".
.
, x p,
x(t) p(t) .
.
, , .
.

34

5.
r(t) U (r)
U
= F(r)
(1)
r
r(0) = r0 , v(0) = v0 . (1) , . - U (r)
.
mr =

5.1.

, , . , (. [7]). M = m[rv], ,
M. ,
( )
. , , rmin r rmax ,
.
U0 = /r, ,  :
a = /2 | E | b = M/ 2m | E |.

16 .
(1) ( ) , , ""t,

r = vt,
1
v =
F(r(t))t.
m

(2)

U r 2 , , , .
16

35

, .
, (2)
1
F(r(t + t)),
(3)
m
.. t + t ( . B).
,

. alpha /m, dt= t, v=(vx,vy),
F/m = a =(ax,ay).
v =

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
%
%
Alpha=1; dt=0.025;
x1=2.5; y1=0;
vx1=0; vy1=0.25;
Emax=-1e18;
Emin=1e18;
r=sqrt(x1^2+y1^2);
%
E1=(vx1^2+vy1^2)/4 ...
- Alpha/r;
% ( ?)
hl=line(x1,y1);
%
a=Alpha/(2*abs(E1));
%
b=r*vy1/sqrt(2*abs(E1));
%
axis([-2*a 2*a -1.2*b 1.2*b]); %
set(hl,EraseMode,none,Color,g);
ha=gca;
%
set(ha,XTick,[-a 0 a],YTick,[-b 0 b]);
grid on;
%
pause
%
while 1
%
36

x2=x1+vx1*dt;
%
y2=y1+vy1*dt;
%
r=sqrt(x2^2+y2^2);
%
A = Alpha/r^2;
%
ax=-A*x2/r;
%
ay=-A*y2/r;
%
vx2=vx1+ax*dt;
%
vy2=vy1+ay*dt;
%
E2=((vx2+vx1)^2+(vy2+vy1)^2)/8 ...
-Alpha/r;
%
if E2> Emax
%
Emax=E2;
%
end;
if E2< Emin
Emin=E2;
end;
%
set(hl,XData,[x1 x2],YData,[y1 y2]);
%
x1=x2;
y1=y2;
vx1=vx2;
vy1=vy2;
end;
% while 1
.
. ,
Ctr+C. ,
MATLAB ,
. Ctr+C, (,
), hold on( ), .
17 .
,
. 17

, , , (, . . 2.3)
m-, .
. ,
.
37

(. . 2.4.2),
, . ,
( ) ( set(ha,XTick,[-a 0 a],YTick,[-b 0 b])
grid on.
1. t . t , .

x. ,
. subplot .

.
2. U =
/r2.
(
r = rmin ).
, , . , ,
, .

.
. , , ,
.
5.2.
, U U0. U r = |r|,
U = U0 + U ,
38

, rmin r rmax . , , U ,
.
,
, U .
, U . , , ,
U = fr, , .
U (, U0 = /r + /r2) 18 . ,
!
,
M E < 0 .
, . ,
= [rf].
( ): M
M , . (
M) ( ).
U0(r) , " ", M
, .
3.
U = fr. , , .
. ,
.
18

, U , U0 /r 2.

39

, f, .
, .
, , (X,Y), (Y,Z) (X,Z),
. ( .)
ZData ( .
, . 8.3).
,
, . ( ,
.)
4. U = fr , . , , U0 = /r + /r2.
5.3.

. , , .. .


, , . , , .
.
5.
U = /r.
(..
, , ).
: U12 = /|r1 r2|.
"" U (r) ,
.
40

6.
.
. . 1905 - 1906 .

.
6.1.
, ,
.
N ( y) x x.
.
. ,
. ,

x = 0.
(1)
, x
. . 19 . ,
( , ). x , .
y. x(k)
k .
x(k + 1) = x(k) + x.

(2)

( ),
x(k + 1) = x(k),
.. x(k) , ,
x(0) = 0. x
x(k) =

1
N

19

N

j=1

xj (k)

(3)

, - , , 1/2.
41

( xj - j- )20.
, k- ,
x2(k).
, (2) :
x2 (k + 1) = x2 (k) + 2x(k)x + (x)2.

(4)

x(k) x
x(k)x = x(k)x = 0.
(x)2 = a2 . (4)
x2 (k + 1) = x2(k) + a2 ,
.. a2 . ,
x2 (k) = ka2 .

(5)

N
1 
x2j
(6)
N j=1
.

f (x), ; dW = f (x)dx
, j- k- x xj x + dx.
k

x2  =

1
x2
f (x) =
exp
.
2ka2
2ka2

(7)

x .
(. 7).
(5).
l , a2 (5) a2 = la2 , k k = k/l.
.. (5)
x2(k) = la2 k/l = a2 k,
.
20

N k.
42

. 7. ( )
6.2.
. vT ,
,
mvT2 kT,

(8)

m , k , T
. v  vT ,
mv = F,
F = v 21 . , ,
, , v v/ :
mv/ v,

m/.

(9)

, v vT , , ,
, v .
21

R
= 6R.
43


. , (9) , .
, . ( ,
2 , /2, ,
, , 2, 1/2 ..
.)
,
a vT .

(10)

, x, x, (,
).
t  k t/ . t (5):
t
r2(t) ka2 (vT )2 vT2 t.
(11)


r2 (t) = 6D t,

(12)

D 22 . (8), (9), (11)


k T
.
(13)
D

- , , r(t).
(12), (13), , ,
.
, . , (
), .
22

x (12) x2 (t) = 2D t.


44

6.3. ,
.
x = dh (2.0 rand 1.0), rand
, , dh , . rand(m,n)
m x n , 0
1, ( ) , .
, x
dh < x < dh:

dw
=
dx

0 |x| > dh,


|x| < dh.

1
2dh

(14)

dw
dh2
(x)  =
dx =
.
(x)
dx
3

(15)


, ,
,
. .
.
1. , x.
diffus.m, MPP, .
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n =500;
%
dh =.02;
%
% -
45

y =1:n;
y=y; x =zeros(size(y));
h=plot(x,y,k.);
%
axis([-2 2 0 n+1 ]);
%
%
set(h,EraseMode,background,MarkerSize,3);
pause
%
i=0;
%
while 1
%
i=i+1;
x=x+dh*(2*rand(n,1)-1);
% x-
%
%
set(h,XData,x,YData,y,Color,k)
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

2. x. , ,
(. 2.1) , , (. 8) subplot.
, (
hist) , , , .

hist [n,x]=hist(y,m),
, , stairs, line,
set, . 2.4.2 . , , , . 8.2.
,
MPP Hist_my .
(7).
line, fplot (,
46

. 8). ,
.

S = (xmax xmin)N/L,
xmin, xmax - x, N - , L - , x.
(7), w =
Sf (x) , : (xmin x xmax, 0
w wmax).
,
[ xmin, xmax]?
3. x x2
.
, (. C),
. , x x2 .
, (5).
.
4. , .
x, y
,

2
R = x + y 2 .
R.
, . , ( ,
).
. , (7).
(7), R.

47

R ( ). , hist,
.

6.4.
, , .
,
,
, :
n(z) exp(mgz/kT ).

(16)

n(z) - z .

.
, , `:
t  . , v
23
mg v,

(17)

, .
b t
b vt g t.

(18)

t : , ,
. (16)
mgb  k T , .. gb  vT2 . t (18), (11) ( t t),
b2  a2 ,
.. .
23

48

, , , .
b, , .
, -, ,
. , , , , . (, .)

n(x) = C exp (x/h).

(19)

h b a , (16) (19)
k T
vT2 t a2
h

.
mg
g t
b

(20)

5. . x,
.
(20). , h
,

x =

xn(x)dx


0

n(x)dx

 x/h

xdx

 x/h
e
dx
0

= h.

(19) ,
hist. ( ,
, , , ;
.) hist
49

,
.

7.
. 6 . , .
t  , , .. , . 6.
7.1.
t  . t . ,
, L t
`, L 1. t p = mv p + p,
:
p = f t,

f = v,

(1)

p .
,
:
1
3
mv 2  = k T.
(2)
2
2
, , ,
. (p)2 ,
, .
. t
vv

1
vt + p
m
m

50

(3)

v 2 :
v 2  v 2 (1

t 2
1
) + (p)2 ,

(4)

(p)2 = 2mv 2 t.

(5)

2
( ( t
)  1 .) ,

(p )2 = 6kT t.


f =
t:


2  =
f

p
t ,

(6)



 6k T


.
(7)
t
(6) t , .
t:
L t, , p, L t.
(3) : , ,
12 v 2 .
,
t  , ..
t  m
, ,

r = vt.

(8)

, . t 
, t . t,
t  , ,
, .
, , , ,
t (
0).
51

. (3) m 2 x,
vv

1
vt 2xt + p .
m
m

(9)

1. . .
, L, t?
2. x(t), v(t) .
.
3. N 200 ,
v 2 (t) r2 (t). .
7.2.
t1 t2 , = t2 t1 , | | :
vx(t1 )vx(t2) = vx (t1 )vx(t2 ) = 0.

(10)

( ,
.) | | ;
24
() = vx(t)vx(t + ).

(11)

, (, ), , t t + .
, , () - ; ,
(11) t t .
x(t), y(t) (x(t)
x(t))(y(t) y(t)). (11) . (11)
, x, vx  = 0.
24

52

x(t)x(t + ), x(t)vx(t + ) .. ,


t, . ,
, t
.
4. .

, .
5. x(t)x(t + ), x(t)v(t + ), v(t)v(t + ).
 1; 1;  1.

8.

, ,

( ).
-, . , .
, , , ( ).
, , ,
. , , ..
8.1.

8.1.1.

t .
,
, , .
53

. 8.
( , ). ,
, .
t , t,
t , t
:
ri (t) = ri + vi t.
t < t, t ,
(, i- j-) .
,
. (
t t t ): ,
t .. .
, , (. . 8) x + vxt = L R, L , R .
vx vx.
(r +
 2
vt ) = 4R2 , r = ri rj , v = vi vj .
54


vi vi v, vj vj + v,
v = n(nv), n = r/(2R) 25
( , ). , ,
Y, g, y + vy t gt2 /2 = L R.
8.1.2.

Balls

Balls.

Balls2 MATLAB 2 .
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% balls2
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[r,v]=balls2(rin,vin,dt);
global rball lx ly ;
%
a=rball; a2=4*a*a; eps=1E-10;
r=rin; v=vin;
%
ldl=[a a; a a];
%
% , ,
% -
lur=[lx-a ly-a ; lx-a ly-a]; %
t=dt;
% - "balls"
while (t> 0)
%
vm=(v.*v < eps);
v1=v+vm;
tdl=(ldl-r)./v1;
% l d
% )
25

,
vi vi

mj
mi
v, vj vj +
v.
mi + mj
mi + mj

55

tdl=tdl+1E10*((tdl< =0)+vm);
tur=(lur-r)./v1;
% r u
tur=tur+1E10*((tur< =0)+vm);
%
[t1,j1]=min(tdl(:));
[t2,j2]=min(tur(:));
if(t1< t2)
t0=t1; j0=j1;
else
t0=t2; j0=j2;
end;
%
r0=r(:,1)-r(:,2); v0=v(:,1)-v(:,2);
rr=r0*r0; vv=v0*v0;
rv=r0*v0; d=rv*rv-(rr-a2)*vv;
if (d> 0) & (rv< 0) & (vv> 1E-10)
tb=-(sqrt(d)+rv)/vv;
else
tb=inf;
end;
% if
%
if(t< t0)&(t< tb)
r=r+v.*t;
%
elseif(t0< tb)
r=r+v.*t0;
%
v(j0)=-v(j0);
else
t0=tb;
r=r+v.*t0;
r0=r(:,1)-r(:,2);
v0=v(:,1)-v(:,2);
dv=r0*v0/a2*r0;
ddv=[-dv,dv];
%
v=v+ddv;
end;
% if
56

%
r=r+v*eps;
t=t-t0-eps;
end;
% while
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
, C, , Matlab. MATLAB
. .

[xf,yf,vxf,vyf]= Balls(n,x,y,vx,vy,dt,R,Lx,Ly,m)
n ri = [x(i), y(i)]) vi = [vx(i), vy(i)]), (i = 1, 2, ..., n)
dt
rf i = [xf (i), yf (i)] vf i = [vxf (i), vyf (i)]). Lx Ly , R,
m . 26
,
(. . 8).
n nmax = 25. R, Lx, Ly, m
. R = 20, Lx = Ly = 256,
.
R x(i) Lx R, R y(i) Ly R ,
, 2R. 27
Balls .
, ,
GAS.m.
26

, Balls ,
. , , Balls
.
27
, .. ,
.

57

8.2.
, ( ). , , , ,
( ).
5-10 ( ), ,
. ,
- .
8.2.1.

, ,
(. 9) 0 108, ,
.
. ,
l ,
l  a.


OO l0. AA

1/2 ,OO l0 .
.
, , ,
, .

. 9.
58

1 ( , 1.5 2 
). AA /a, 1 (l/a)0  0
. k k (l/a)k 0.
l/a 10, 8-10 , k 1 . ,

.
100 ,
100
.
, .
. -
, ,
- .
, ( 28 )
.
, . 29
8.2.2. ?

,
, , .
,
, . . ,
: vi vi.
.
28

: , , .
29
, ,
. . .. ,
.. .

59

. ,
.

( o . : set(hh, EraseMode,none)),
.
1. tm
: vi vi , .
tm.
8.3.

, ri (t)
, ,
. (vx, vy )
, .
, .
f0(v) ,
N0 . ,
v f (v) = f0 (v)/N0,

 

f (v)dvxdvy = 1.

, dvxdvy
v,
dw = f (v)dvxdvy .

(1)

, ,30


mN 1

f (v) =
1
2 E
E

N 2

(2)

= mv 2 /2 ; E N .
30

: (.. ,,1996), 5.1, 3- .


60

. 10.
 E 1 /E exp(/E),

(N 2)
mN 1
exp
.
f=
2 E
E
N  1, : k T = E/N , k
. ( (3/2)kT = E/N ).
(1) :

m(vx2 + vy2 )
m

.
f (vx, vy ) =
exp
2k T
2k T
(vx, vy ) (. 10) v
N , (1/N )(N/v).
(1) dvxdvy 2vdv.
. = mv 2 /2 d = mv dv, 2v dv (2/m)d.
(1/N )(N/) (vx, vy ) .
vx
(vx, vy ) vx (. 11). 61


dw
=
f (vx, vy )dvy .
dvx






m
mvx2
vy = x 1
2E
2E





N 3

 2m (N 1)
dw
mvx2


=
1
dvx
E

2E

31
I(N ) =

 1
0

(1 x2) dx =

I(N 2),

(2N )!!
,
(2N + 1)!!

N !! = N (N 2)(N 4)...
2. (vx, vy ).
(vx,vy). dt Balls , ,
.
3. ()
vx , v,
.
( (2)).

, ( m4 (v1 v2)2 ).
31

I(N) -

I(N) =

2N
I(N 1),
2N + 1

(31)
N  1 I(N) , , x  1. 1 x2
exp (x2 ),
. I(N)


0

exp (Nx2 )dx =


62

/N/2.

. 11. vx
4.
( ).

,
.
8.4.

( ) , , , , , , .
,
, .
? , , , , .
? , (
) .
Balls ,
63

, , , . , , .
5 ,
U ,
U . .
() ?

, .
. , -
.
, , , ,
. , .
, .
6 ,

.

9.
,
( -).
, .
, , , (
, , ).
64

, ,
(A) , (O).
. ,
. .
9.1.
RA RO . A , O.
, < R, R = RA + RO .
O A , = R2 . 32 .
( ) ,
A O .. , , A
1 2, , .. , ,
A
d
= f ()
d
, d = f ()d
+ d ( d).
[7, 18], .

.
. - nO
O 1 3. ( dx ) S nO S dx O, dS = nO Sdx.
, dW = dS/S = nO dx ,
O dx. ,
. ,
, - O
. dW  1.
32


1016 2 , 1026 2 , 10 - 12 .
65

9.2.
. .
.
A NA0 . dx
dNA = NA dW . , , NA (x),

dNA (x) = NA (x)nO dx.


(1)
, NA (0) =
NA0,
NA (x) = NA0 exp(bx),
(2)
b = nO . 1/b ,
( e =2.7 ).
.
, -. dx, x (
). dx ,
0 1 ( rand MATLAB)
dW ; rand< dW , ,
. Na
x, Na(x).
, Xmax Na . ,
- ,
, 0 1. 1
, 0 - .
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
Na=250; k=(1:Na);
no=100; Section=0.05;
dx=0.01;
x=zeros(size(k));
%
sc=ones(size(k));
% -
66

dW=no*Section*dx;
% dx
hl=line(x,k);
% Na
set(hl,Marker,o,MarkerSize,3);
axis([0 1 0 Na+1]);
%
pause;
%
% ,
% x dx ,
% Xmax
while (any(sc)> 0)
ra=rand(size(k));
%
%
%
k1=find(ra-dW< 0);
%
%
sc(k1)=0;
%
x=x+sc*dx;
% dx
set(hl,XData,x);
%
end;
% while
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Beam1 .
, (2) ,
, .
.
Nf , , ( ). p = Nf /Na .
1. (2) Na (x) ,
().
, . .
. , , A , B, ,
67

AB , A A .
, .
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% A %
% A -> B
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SecA - A
% SecAB - A B
% scA - A
% scB - B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
%
Na=25;
% A
L=5; k=(1:Na); no=100;dx=0.01;
SecA=0.1; SecAB=0.15;
x=zeros(size(k));
%
scA=ones(size(k));
% - A
scB=zeros(size(k));
% - B
dWa=no*dx*SecA;
% A dX
dWab=no*dx*SecAB;
% A -> B dX
.......................................
%
% A L
while (any(scA)> 0 & all(x)< L)
ra=rand(size(k));
ka=find(ra-dWa< 0);
% A
scA(ka)=0;
% A
kb=find(dWa< ra & ra< dWa+dWab)
scB(kb)=scA(kb);
% A B
scA(kb)=0;
% A
x=x+scA*dx+scB*dx;
pause(1);
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

68

, A B, (1), (2).
( x ,
).
2. B B
A.
9.3.
A
, .
(O) m, (A) M,
V , E (m  M).
, , , A
.
, .
, .
, d/d = F (E, ),
E , .
, ,
dX . , ,
. , D.
..........................................
ra=rand(size(k));
ka=find(ra-Na*Sect*dX< 0);%
for ii=ka
%
% ,
% F(E,eps)
while 1
% , break
eps(ii)=epsmax*rand;
%
if Fmax*rand < F(E(ii),eps(ii))
break;
end;
% if
69

end;
% while
E(ii)=E(ii)-eps(ii);
%
end;
% for
..........................................
epsmax=max , Fmax
=Fmax , , F (E, ).
: F (E, )
0 < < max (max = 4mME/(m+
M)2 4(m/M)E) (. [7, 18, 2]). while - end
.
F (E, )
0. . , -
min . Fmax = F (E, min).
:
, ..
. ,
, < min ,
minXmax/dX  E/l, .
min ,
:
F (E, ) min < < max , 0 < F < Fmax.
3. , Xmax.
:
- ;
- , U =
/r. ,
[7, 19]:
d
22 M
=
d
mE2

< max = 4
70

m
E.
M

, , ,
.
9.4.
, , A
. , ,
, .
V , ..
2
p mV mV2 . 2
N M2V / M/m . 1 p/MV m/M.
,
, (Vy , V
z ) . N 1 N m/M  1.

. y = cos Vy /V z =
sin Vz /V .
, 33
d
d = f () .
, 1 ,
X.
, 34 :
.................
phi = 2*pi*rand;
.................
33

,
,
(. [7, 17]). ,
( , , ).
34
A B
,
. .
71

1y = 1 cos , 1z = 1 sin y , z :
y y + 1y , z z + 1z .
(Vy , Vz ).

, .
4. , Xmax,
( ).
(E, ) , -
, .
5. ,
x.

dx.

10.
. MATLAB (Signal Processing
Toolbox), . 35 .
, , MATLAB.
10.1.

, , , /
,
,
. , , ,
35

. ..
[11] .
72

, .
, ,
, - .
1. - 0 50 0,2. 36:
1) f (t) = A 0 t T ;
2) f (t) = A/ t 0 t T ;
3) f (t) = A Sin(2t/ ) 0 t T ;

0 t
4) f (t) =
0 t T ;
A t 
5) ;
6) .
, .
,
( ) .
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
%
%
a1=1.5;
%
a2=0.15;
%
tau=10.0;
%
t=0:0.2:50;
%
y1=a1*sin(2*pi*t/tau); %
y2=a2/tau*t;
%
plot(t,y1,t,y2);
%
axis([0 50 -2 2])
%
36


sin, mod sign
73

grid on
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
,
MATLAB, .. .
.
MATLAB . 2 , . 2.
2. , , (:), , ,
1, ..
3. , , (10 ).
4. , , , (
).
5. diff(x), -,
, . sum(x), (
) . (figure) ,
, .
( title).
10.2.

U r
...
E(t) (),
R
C
E (t)
R
C. :
. 12. RC-

74

dQ

dt = i

E(t) =

Ur + Uc

Ur = I R

Uc = Q
C
Ur Uc , .
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RC- %
%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
%
%
C=0.1;
%
R=1;
%
tau=R*C;
%
t0=0; Q0=0;

% t, I Q
t(1)=t0; I(1)=0; Q(1)=Q0;
dt=0.02; t=0:dt:4;
E=signale(t,3);
ss(1)=0.0
for k=2:length(t)
Q(k)=Q(k-1)+I(k-1)*dt;
Uc(k)=Q(k)/C;
Ur(k)=E(k)-Uc(k);
I(k)=Ur(k)/R;
ss(k)=ss(k-1)+E(k)*dt;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6. , RC
. E, , , Uc, Ur.
, Uc , Ur .
75

. 2.3 , .
7. , RC-, L (RL-), 5. , ,

=iR
Ul = E Ur

di
Ul
dt = L
10.3.

u,
:
 u

T
I = I0 e 1 ,
(3)
I0 - ,
T - .
T
kT
=
.
q
11600
T , 4
7 
,
.
- . 13.

. T=300 K, T (300K) = 0, 025 .
I0, , . I0,
, .
, , |u|  T
I0 ,
T =

76

, , . I0 = 1, .
, . 13,
.
I = R1 (U0 u)
I = I0 (eu/T 1) .
8. I(u), (3).
, . I0 = 1, R = 1.
9. , ,
I = R1 (U0 u).
, , x = f (x).
, . x0 , x1, . . xi xi+1. ,
. .
, . , , -
MATLAB, fzero, .
f (x) = 0, ,
x0,
:
f (x) = f (x0) + f (x0)(x x0) + ...
, :
x = x0 f (x0)/f (x0).
:
xi+1 = xi f (xi)/f (xi).
77

, . , , ,
. , .
10. RC RL , (I0 =
1) (R = 1) ()
. 10
50 . .
, , .
.
.
, R2
C, (. 14).
R 1
D
U 0 S in (M t)
C
R 2
,

. 14.
. -
R2 . ,
, :

u + iR1 +

ir R2 = Uc

i = ir + ic

dU

ic = C c

dt

Uc = U0Sin(t)
,

i = F (u)
F (u) - ( 3).
, , :
1. Uc = 0
2. u + F (u)R1 = U0 sin(t) Uc
78

3. i = F (u)
4. ir =

Uc
R2

5. ic = i ir
6. Uc = Uc + Cic dt
Uc t + dt
2. ,
, . ,
,
(R1C, R2 C).
11. , , .
, 50 , 10 . ,
. .
. .

79


A.
,
.

1.
(. [9, 6.3]).
2. CO2 .
(. [7, 24, 1]).

3. , , ( ), :
) , ;
) , .

4. ,
3, 3.
E
, , - FE = e [EvB].
c

, , , .
,
h2
h3
x(t + h) = x(t) + hx(t)
+ x(t) + x(t) + O(h4 ),
2
6
x(t)

= v(t), x
(t) = f (t) = F (t)/m, x (t)
f (t)fh(th) .
80

5. ( ).

6. , X, U (x, r).
,
:
) U =

Ay 2

r2 +B 2

) U =

Ay 2

(r2 +B 2 )2

7. , , . (
),
. , Ev ,

Er Ev t/v 2.
, , 9.
8. X

U (r) = / r2 + a2 .
. ,
.
9.
x = x0 + aekz0 cos kx0 cos t, z = z0 + aekz0 sin kx0 cos t.
. ,  1/, .

10. ( ) U = k2 (x2+
y 2 +z 2 ).
r = x2 + y 2 + z 2 .
81

B.

B.1.
, . , . x v

dx
dv
= v,
= a(x),
dt
dt
a(x) = F (x)/m, F (x) , m .
x(t), v(t) ,
x(0) v(0).

: dt,

dx
x(t + dt) x(t) + dt = x(t) + v(t)dt,
dt
dv
v(t + dt) v(t) + dt = v(t) + a(t)dt.
dt
, x v
, .
, , x,

dx
1 d2 x
1
2
dt +
a(t)(dt)2. ()
(dt)
=
x(t)
+
v(t)dt
+
2
dt
2 dt
2
, (dt)2; t , t/dt,
t dt. ( t)
10 , 10 dt.
, .
() dt. , (), , , , v(t + dt/2). ,
x(t + dt) x(t) +

82

, (t + dt/2, t +
dt/2 + dt), .. t + dt, ,
x(t + dt).
,
t,

t + dt,

t + 2dt,

t + 3dt, ... ,


t + dt/2,

t + 3dt/2,

t + 5dt/2,

t + 7dt/2, ... .

( x)


dv
x(t + dt) x(t) + v(t + dt/2)dt x(t) + v(t) + dt/2 dt,
dt
(). (dt)3.
, .
, , ,
, .
B.2.
MATLAB
. "
". MATLAB , , , ().
,
. M-
.
, ,
, t,
y (t) = F(t, y(t))
83


y(t0 ) = y0.
,
,
, MATLAB.
B.3. M- ()
M- -- 37 .
y  (1 y 2 )y  + y = 0, > 0 .
y1 = y, y2 = y  ,
:
y1 = y2
y2 = (1 (y1)2 )y2 y1 .
M-, . ( =
1, y1 y2 y(1) y(2)) :
function dy = vdp1(t,y)
dy = [y(2); (1-y(1)^2)*y(2)-y(1)];
t,
y,
t y.
[0 20]
y1 (0) = 2, y2(0) = 0
[T, Y]= ode45(vdp1,[0 20],[2;0]);
- T,
, Y,
T. ,
37

-- ,
.
84

J_r_gb_mjgby<Zg^_jIhey^eyPX 


\
\


J_r_gb_<









<j_fy7











. 15. -- mu=1

................................
%
plot(T,Y(:,1),-,T,Y(:,2),--);
title( - -- mu=1);
xlabel( T);
ylabel( Y);
legend(y1,y2);
................................
, 15.
B.4.
. ,
ode45. MATLAB
ode45 -. y(tn )
y(tn1). , .
85

ode23 -, , (
) .
.
ode113 --.
, ode45, .
,
.
MATLAB
4 .
ode15s ,
. , ode113, . ,
ode45, ode15s.
ode23s . , , ode15s,
.
ode23t .

.
ode23tb -.
ode23s
.
B.5.

. :
[T,Y] = odeXX(F,tspan,y0),

odeXX - , ;
86

F - , ;
tspan - , . tspan
= [t0 tfinal] , t0
tfinal. tspan , odeXX
, tspan, , t0 > tfinal ;
y0 - ;
T - - ;
Y - . (
y(i)) .
, . odeXX
, .. :
[T,Y] = odeXX(F,tspan,y0,options,p1,p2,...).
options (. odeset Function). p1,p2,.... ,

F(t,y,flag,p1,p2,...).
.
B.6.
.

t y, - .
, F (t, y), -.
87

, F (t, y), (
flag)
.
--. , = 1. .
function [out1,out2,out3] = vdpode(t,y,flag,mu)
if nargin < 4 | isempty(mu) % 4- ,
mu = 1;
% mu=1
end
% 3-
if nargin < 3 | isempty(flag)
% dy/dt = F(t,y)
out1 = [y(2); mu*(1_y(1)^2)*y(2)_y(1)];
% 3 flag=init
elseif strcmp(flag,init)
% [tspan,y0,options].
% vdpode
out1 = [0; 20]; % tspan
out2 = [2; 0]; %
% ()
out3 = odeset(RelTol,1e_4);
end

88

C.
XY(. 16),
N (xi, yi). ,
yi .
,
y =
a0 + a1 x y(x), x , y
. 
F = i (a0 + a1 xi yi )2
. 16.

F/a0 = F/a1 = 0,

N a0 + a1 xi = yi ,



a0 xi + a1 x2i = xiyi .
a0 a1 , .
( xi yi ) -.
- od, x, , (,
)
od = ones(size(x));
s(1,1) = od*(od);
s(2,1) = od*(x);
s(1,2) = s(2,1);
s(2,2) = x*(x);
p = [od*(y); x*(y)];

sa = p,

a = s1p .
89

MATLAB,
a = s \ p;. - a
.

( - ) polyfit
polyval, , :
a = polyfit(x,y,1); %
y1 = polyval(a,x); %
(help polyfit) [2, 3, 6].

90

D.

rand MATLAB.
(0,1).
,
[,]:
=+(-)*rand.

, [,] f (X) (. 17).
D, . 17.
f (X)
[,].
, f (X). , .
X [A,B] f (X).
, , , , :
.......................
x=A+(B-A)*rand;
while (f(x) < H*rand)
x=A+(B-A)*rand;
end;
.......................
f (X) MATLAB.
X .
, f (X),
. f (X).
.
91

, , () . , , .
, , .
, dS , dS/4.
(, ) Z, dS = sin dd, 0 , 0 2. ()
.

.................
C=2.0*rand-1;
Fi=2.0*pi*rand;
.................
Fi , .
MATLAB ,
randn(m,n). m x n
.
.

92

E. MATLAB
MATLAB 20 . help . help
< > , . , ,
.

general
ops
lang
elmat
specmat
elfun
specfun
matfun
datafun
polyfun
sparfun
plotxy
plotxyz
graphics
color
sounds
strfun
iofun
demos
(Ctrl+P)
(Ctrl+N)

demo
help
lookfor









-









/









93


path
what
clear
length
load
save
size
who
whos
cd
delete
diary
dir
!
clc
echo
quit
startup

*
.*

.
kron
\
/
./


M-, MAT-, MEX-







, .





DOS



MATLAB
MATLAB
, MATLAB










94


>
<
>=
<=
==
=
&
|

xor
.
..
...
%
all
any
exist
find
finite

eval
feval
function
global
nargchk
break
end
error
for
if,else, elseif



(AND)
(OR)
(NOT)
(XOR)



( )


,
,
,



MATLAB




-





95


return
while
input
pause

eye
linspace
logspace
meshgrid
ones
rand
randn
zeros
:
ans
eps
flops
i,j
inf
nargin
nargout
pi
realmax
realmin
clock
cputime
date
etime
ti,to





( )




3d-









-
-



96


diag
fliplr
flipud
reshape
rot90
tril
triu

abs
acos
acosh
angle
asin
asinh
atan
atanh
conj
cos
cosh
exp
imag
log
log10
real
rem
round
sign
sin
sinh
sqrt
tan
tanh
bessel
besselh





90









- ()



97


betain
betaln
ellipj
ellipke
erf
erfinv
gamma
gammaln
gammainc

cond
det
norm
orth
rank
trace
inv
\ or /
eig
poly
expm
funm
logm
sqrtm

cumprod
cumsum
max
mean
min

-
-




-
-
-
-









, 4










98


prod
sort
std
sum
trapz
diff
gradient
corrcoef
cov
conv
deconv
filter
abs
angle
fft
fftshift
ifft
nextpow2

conv
deconv
poly
polyder
polyfit
polyval
polyvalm
roots
interp1


99


fmin
fmins
fzero
ode23
quad

comet
fill
line
loglog
plot
semilogx
semilogy
bar
errorbar
fplot
hist
polar
grid
gtext
text
title
xlabel
ylabel
comet3
plot3
contour
image
mesh




0
(
)


x-y






y








,


x
y

3-
3d


3d

100


surf
colormap

clf
close
figure
gcf
axes
axis
cla
gca
hold on/off
subplot
delete
drawnow
get
reset
set
print
setstr
strcmp
int2str
num2str
str2num
fclose
fopen
fread
fwrite
fgetl
fprintf

3d


















( )








/






101


fscanf

F.

MATLAB

MATLAB - , . , , .
, MATLAB , .
:
m- p-, MATLAB
(pcode)
m- dll- (mcc)

, dll- MATLAB.
F.1.

pcode

,
m-.
, p-
.
. :
pcode file1 (file2 ...) file1.m file1.p .., file1.m , pathdef.m, file1.p
pcode *.m m- p-
pcode file1 (file2 ...) -inplace p- ,
m-.
F.2. mcc
MATLAB ,
20-40%. , dll-
: ,
102

MATLAB . m- :
, dll- (Borland C/C++
5.x, Microsoft Visual C++ 4.2 5.0, Watcom C/C++ 10.6 11)
MATLAB Compiler ( MATLAB)
mcc m- , MATLAB ( , MATLAB ), .
F.3. mex
MATLAB.
( ), dll-,
MATLAB. dll-
, . (timestwo.c) :
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#include "mex.h"
/*
* timestwo.c - API-guide
*
* 2.
*
* MEX-file MATLAB.
* Copyright (c) 1984-1998 The MathWorks, Inc.
*/
/* $Revision: 1.5 $ */
void timestwo(double y[], double x[])
{
y[0] = 2.0*x[0];
}
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
103

double *x,*y;
int mrows,ncols;
/* Check for proper number of arguments. */
if(nrhs!=1)
{
mexErrMsgTxt("One input required.");
}
else
if(nlhs>1)
{
mexErrMsgTxt("Too many output arguments");
}
/* The input must be a noncomplex scalar double.*/
mrows = mxGetM(prhs[0]);
ncols = mxGetN(prhs[0]);
if( !mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) ||
!(mrows==1 && ncols==1) ) {
mexErrMsgTxt("Input must be a noncomplex scalar double.");
}
/* Create matrix for the return argument. */
plhs[0] = mxCreateDoubleMatrix(mrows,ncols, mxREAL);
/* Assign pointers to each input and output. */
x = mxGetPr(prhs[0]);
y = mxGetPr(plhs[0]);
/* Call the timestwo subroutine. */
timestwo(y,x);
}
timestwo, , mexFunction, x y. :
nlhs - dll-
nrhs -
*plhs[ ] -
*prhs[ ] - .
104


( mxGetPr mxGetPi - , mxGetString - , mxGetN mxGetM ,
mxCreateNumericArray,
mxCreateDoubleMatrix, mxCreateString - ,
, ..
\matlab5.2\help\techdoc\AP IREF \AP IREF T OC.HT ML
, timestwo MATLAB. mex:
mex -setup (
), MATLAB.
mex file file.c file.dll.
, help mex MATLAB.

105


[1] .., .. . : , 1998. 123 .
[2] .. Matlab 5 . .: -, 1998. 314 .
[3] .. MATLAB. . .: , 1998. 314 .
[4] .., .. MATLAB 5.X. , , . .: -, 2000. 332 .
[5] Marchand Patrick. Graphics and GUI with MATLAB. Second edition. CRC
Press LLC, New York; Washington, 1999. 445 p.
[6] Using MATLAB. Version 5.2. The MathWorks, Inc. Natick, MA, 1998.
[7] .., .. . .: , 1976.
[8] .. . : , 1996.
[9] .., .. .
.:, 1977.
[10] .., .. LaTeX. : . , 1999.
[11] .., ..
. : , 1995. 65 c.

106


MATLAB
MATLAB , - . - . MATLAB MATrix LABoratory ( ).
- MATLAB.
.
, . c , .
, . 1.4, help
, . help <_>
, . help <_> . , help eig
eig.
MATLAB intro demo. MATLAB Users Guide
( ). MATLAB Sun/Apollo/VAXstation/HP workstations, VAX, MicroVAX, Gould,
PC AT , 80386 80486 , Apple Macintosh, . , 5.0., 5.1
5.2. MATLAB MathWorks, Inc., Cochituate
Place, 24 Prime Park Way, Natick, MA 01760, (508)653-1415, Fax: (508)6532997,
Email: info@mathworks.com.

1.
1.1.

MATLAB

MATLAB
, matlab. Windows
3.1 Windows-95
. quit. Windows
3.1 ( 4.0 4.2) m- Notepade,
107

Windows-95 ( 5.0 ) 1. MATLAB ,


- . MATLAB MATLAB
DOS. , dir
, what
m-2 . cd , delete type
.
1.2.
L :
help;
lookfor;
l;
;
WEB- he MathWorks.
1.2.1.

hl

m- - help < m->. .


, help magic :
MAGIC Magic square.
MAGIC(N) is an N-by-N matrix constructed from
the integers 1 through N^2 with equal row, column,
and diagonal sums.
Produces valid magic squares for N = 1,3,4,5,...
1

5. Windows-95.
m- ,
MATLAB .m.
MATLAB . . 6 .
2

108

,
, . .
help , . , help elmat, , .
.
,
, -
, .
1.2.2. lookfor

m- ;
, ,
. , L m-
inverse, help inverse - inverse.m
not found (inverse.m ).
lookfor inverse ,
, ( )
L. lookfor -all
lookfor <> -all
- <> , ..
.
1.2.3.

Help

L :
Help Window
Help Tips
Help Desk(HTML)
Examples and Demos
109

About MATLAB
Subscribe (HTML)
Help Window ,
help. ,
,
help .
Help Tips , .. .
Help Desk
,
, CD-ROM HTML.
, -
Internet (, Internet Explorer Netscape Navigator). MATLAB c
. ,
.
Examples and Demos Matlab .
About Matlab Matlab,
Subscribe HTML (
) MathWorks.
. PDF dobe
rbat. , , , . .
1.3.

MATLAB .

Backspace Delete. . PC, Home, End Delete.
edit .
- .
110

, ,
. , m-,
MATLAB (. . 7 7.2). , (. . 7.4) , ,

.
>> a = rand(8); flops(0), inv(a); flops
sin(mx) sin(nx) [0, 2]
n m,
m=2;n=3;x=0:.01:2*pi;y=sin(m*x);z=sin(n*x); plot(x,y,x,z)
. .
1.4.

MATLAB , .
format short

4
( )
format long
14
format short e 4
format long e 15
. format compact ,
.
.
1.5.

diary. diary <_> ,


( ) <_>.
, diary.
111

diary off , diary on


..
, ,
m-.

2.
MATLAB - , . .
11 , . MATLAB .
;
;
m- (. . 6 7 );
.
2.1.
,
A = [1 2 3; 4 5 6; 7 8 9]

A = [1 2 3
4 5 6
7 8 9]
33 . .

, . (, 2.34e-9) . m-, (.

112

. 7). rand, magic hilb , . rand(n)


nxn, -
[0 1], rand(m,n) mxn. magic(n) nxn,
( ).
hilb(n) nxn, (m n , , ).
for(. . 4).

. , A(2,3) , 2- 3- A, x(3)
3- x. . . A , , A(k). ,
. ,
-, . 5
A(2,2), A(5). .
2.2. (:)
MATLAB, .
( )
. (
MATLAB) . ,
. 1:5 - [1 2 3 4 5].
. ,
x=0.2:0.2:1.2 x [0.2, 0.4, 0.6, 0.8, 1.0, 1.2], 5:1:1 [5 4 3 2 1]. , ,
. .

113

x = [0.0:0.1:2.0];
y = sin(x);
[x y]
, sin (.. ), x,
y. . ,
A(1:4,3) -, A.
. , A(:,3) A, A(1:4,:)
. . , A(:,[2 4])
, 2- 4- A. ,
, .
, A(:,[2 4 5]) = B(:,1:3) 2,4 5- A
B. ,
A .
. 2- 4- A
[1 2; 3 4] 22- A(:,[2,4])
= A(:,[2,4])*[1 2; 3 4]. , , . , ,
n, x = x(n:-1:1).
MATLAB,
, .
2.3.

eye
zeros
ones
diag
triu
tril
rand

, (m,n) mxn , zeros(n)


nxn; A , zeros(size(A))
114

, A. x , diag(x)
, x;
A , diag(A) ,
A. , diag(diag(A))?
. , A 33,
B=[A, zeros(3,2); zeros(2,3), eye(2)]
55. .

3.
3.1.

MATLAB ,
.. , , . MATLAB
=

, , , . ,
. = ,
ans (answer - ),
.
Enter. ,
, Enter. , ,
.
, , . . MATLAB
, . , solveUT
, solveut. who
.
clear <_>. clear
. eps
115

(epsilon) - 106 .
. , MATLAB, Ctrl+C
(Ctrl+Break PC).
3.2.
MATLAB:
+


\

/
, , ( 11).
, ,
,
.
. A , b - - -
, x = A\b A x = b ,
x = b/A x A = b . A - ,

A x = b. ,
, -
. b/A = (A \b) .
3.3.
,
- , .
, *,, \, / ,
116

. , [1,2,3,4].*[1,2,3,4]
[1,2,3,4].2 [1,4,9,16].
.
.
3.4.

MATLAB .
save
matlab.mat.
MATLAB, load
.

4. for, while, if, case


MATLAB , .
4.1.

for

, n,
x = [ ]; for i = 1:n,x=[x,i2], end

x = [ ]; for i = 1:n x = [x,i2] end


n,
x = []; for i = n:-1:1, x=[x,i2], end
, . . , (, x = [ ].)
for i = 1:m
for j = 1:n
H(i, j) = 1/(i+j-1);
end
end
H
mxn. , ,
117

, H .
4.2. while
while
while <>
<>
end
<> , <>
. , a
n, 2n < a:
n = 0;
while 2^n < a
n = n + 1;
end
n
4.3.

if

if :
if <>
<>
end
<> <> .
, .
if n < 0
parity = 0;
elseif rem(n,2) == 0
parity = 2;
else
parity = 1;
end
118

, elseif,
, .
4.4.

case

if,
switch ... case. :
switch <>
% <> -
case <1>

% , <>=<1>
case <2>

% , <>=<2>
...
otherwise

% , <>
%
end
4.5.

( )

MATLAB :
<

>

<=
>=
==

=

, = ,
== . (, , , )
:
119

&
|

,
1 0 , .
3 < 5, 3 > 5, 3 == 5, 3 == 3. ,
, 0 1, .
a=rand(5), b=triu(a), a == b. while if ,
.
, , A B
,
if A == B <> end
, A B , if any(any(A = B)) <> end , ,
if A == B else <> end. , if A = B, <>, end , ,
A B.
any all. any , (. 5.2). for 1:n. ,
for, .[6].
4.6. find
find , ,
.
k=find(x) k / x. x- , , . find(x) ,
120

0 1 ( ). find ,
. , for
for k=KK, KK - , . <>
, 3, :
for i=find(A>3)
<>
end;

5. MATLAB
MATLAB ,
(. . E ).
. (, , ..),
MATLAB.
5.1.

MATLAB , , .

sin asin
exp
abs round
cos acos log () sqrt floor
tan atan rem () sign ceil
5.2.
, (
), mxn (m >= 2),
, .. -,
. ( )
. , mean(A).
:
121

max sum median any


min prod mean all
sort std
, max(max(A)), max(A). .
5.3.

MATLAB . :
eig

chol

svd

inv

lu
LU-
qr
QR-
hess

schur

rref
expm

sqrtm

poly

det

size

norm

cond

rank

MATLAB . ,
y = eig(A), eig(A) -,
A, [U,D] = eig(A) U, A, D
. .

122

6. M-
MATLAB ,
. m-,
<>.m. MATLAB
, m-. m: -, , -.
6.1. -,
- MATLAB.
, , rotate.m, rotate,
, .
( )
.
; . , ,
data.m
A=[
1234
5678
];
data , . m- m-,
.
6.2. -
- MATLAB, , ,
, MATLAB.
, 4.0 (global). -.
function r = randint(m,n)
% RANDINT .
% randint(m,n) mxn
123

% 0 9.
a = floor(10*rand(m,n));
:
function r = randint(m,n,a,b)
% RANDINT .
% randint(m,n) mxn
% 0 9.
% rand(m,n,a,b)
% a b.
if nargin < 3, a = 0; b = 9; end
r = floor((b-a+1)*rand(m,n)) + a;
randint.m ( - ).
- , , . ,
, . , , z = randint(4,5) 4 5 m n,
z. - ,
MATLAB.
nargin ( ).

, a b .

.
.
function [mean, stdev] = stat(x)
% STAT
% x, stat(x)
% x.
% x, stat(x)
% -, , ,
%
% x.
[m n] = size(x);
124

if m == 1
m = n; % -
end
mean = sum(x)/m;
stdev = sqrt(sum(x.^2)/m - mean.^2);
stat.m, [xm,xd]=stat(x)
x xm, xd. ,
. , xm = stat(x) ( xm ) xm x. %
, . , -,
, , .. , help
stat. -. MATLAB,
. , , x.2, x, sum, (. 5.2), sqrt,
(. 5.1), sum(x)/m, - .
,
,
(. . 6.3).
function a = gcd(a,b)
% GCD .
% gcd(a,b)
% a b, .
a = round(abs(a)); b = round(abs(b));
if a == 0 & b == 0
error(gcd , )
else
while b ~= 0
r = rem(a,b);
a = b; b = r;
end
end
125

. , , , tol,
nargin ( ). nargout ( ). ,
, (1 -
0 - ). , while if ,
nonzero , 0 .
feval ,
.
function [b, steps] = bisect(fun, x, tol)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BISECT
% .
% bisect(fun,x) .
% fun - ,
% ;
% m-.
% x .
% , fun .
% , bisect(sin,3) pi.
% sin.
%
% .
% eps.
% ()
% ;
% [c f(c)].
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
if nargin < 3, tol = eps; end
trace = (nargout == 2);
if x ~= 0, dx = x/20; else, dx = 1/20; end
a = x - dx; fa = feval(fun,a);
b = x + dx; fb = feval(fun,b);
% .
while (fa > 0) == (fb > 0)
126

dx = 2.0*dx;
a = x - dx; fa = feval(fun,a);
if (fa > 0) ~= (fb > 0), break, end
b = x + dx; fb = feval(fun,b);
end
if trace, steps = [a fa; b fb]; end
%
%
while abs(b - a) > 2.0*tol*max(abs(b),1.0)
c = a + 0.5*(b - a); fc = feval(fun,c);
if trace, steps = [steps; [c fc]]; end
if (fb > 0) == (fc > 0)
b = c; fb = fc;
else
a = c; fa = fc;
end end
MATLAB , m-. m- ( MATLAB ) type
<_>. type eig, type vander, type rank.
6.3. , ,
MATLAB . , s = This is a test s.
disp. ,
disp(this message is hereby displayed)
.
error. , m-
error(Sorry, the matrix must be symmetric),
m- . m-
, input. iter = input( : ),
,
, .
Enter iter, .
127

7. m-
MATLAB m, MATLAB
. 5.0 ( Windows-95) / (. . 7.2), .
. 5.0
(. 7.3).
M-, , .
, ( DOS).
DOS ( cd),
File/Set Path,
.
7.1.
m- L m- . , foo L :
1) , foo ;
2) , foo ;
3) m- foo.m;
4) m- foo.m .
- , 3 , (private)
- .
m-,
.
7.1.1.

, :
3

, ,
, .
128

path - ;
path (s) - s;
addpath /home/lib path(path, /home/lib) - ;
rmpath /home/lib - /home/lib .
, , pathdef.m,
ll;
MATLAB.
Path Browser (. ), .
7.1.2.

MATLAB m-
mat- .
, MATLAB, .
cd.
cd< >.
7.1.3.

Path Browser

, Windows-95
Path Browser (. 1). File/Set Path ,
.

File/Save Path,
4.
4

,
, local
.

129

. 1. Path Browser
7.2. /
/ m-,
. -
MATLAB edit. ,
edit poof poof.m,
File Preferences .
File/New
New File (. ). m- File/Open Open File.
/ , . 2
, , , ..
, .
:



130

. 2. /


Tools/Fonts , . ,
.
(Del, Bspace, Home ..).
Edit/GoTo Line .
. (..
) Tools/Run.
.
, , . , ,
. , .
131

( )
View/Workspace Browser.
View. Evaluate Selection,
,
Auto Indent Selection, MATLAB.
View/Options ,
.
7.3. m-
- ,
:
, m-
. MATLAB , m-;
, , ,
, .
, . , m- ,
MATLAB. ,
:
, , ;
m- keyboard,
m- m-.
K. return;
m- .
;
132

L.

,
. , m-, . , m m- .
, .
, .
, Editor/Debuger. edit <_>
File/Open. / File/New/M-file.
/.
. 2, ,
, . 3.
/ :

. 3. /
. , ,
, . . , (Step in) (Single Step), (Continue)
133

(Quit Debugging). , View/Workspace Browser ,


. , , ,
.
7.4. : flops etime
- (flops) .
MATLAB flops . flops(0) ( flops = 0!) . , flops(0), flops .
clock
(. help clock). t1 t2 etime(t2,t1)
, t1 t2. , , , Ax = b
: t = clock; x = A\ b; time = etime(clock,t).
flops
x = inv(A)*b;. . 4.0
tic toc.

8.

MATLAB , , , .
demo.
,
, . MATLAB ,
. , plot, title, axis, text, hist,
contour , .

134

8.1.

8.1.1.

plot

plot x-y ; x y
, plot(x,y)
y(x). -4 4

x = -4:.01:4; y = sin(x); plot(x,y)
. x
0.01, y - (,
- ).
. ( MATLAB
, ,
Alt+Tab ). 2
ex -1.5 1.5 : x =
- 1.5:.01:1.5; y = exp(-x.2); plot(x,y). ,
, , (. . 3.3 ). , , , . , , t=0:.001:2*pi;
x=cos(3*t); y=sin(2*t); plot(x,y).
plot . :
plot(y)
y - , y y.
y - , , .
plot(x,y)
x y - (
), y x.
x - , y - , y
x. y , x, ,
x. y ,
135

x, , x. y ,
x.
x - , y - ,
y x , .
x y - ,
, y x.
, ,
plot . ,
plot (. ).
plot . plot(x1,y1,x2,y2,...), ,
, .
line , plot.
(. . 8.4).
comet(x,y),
. , , (,
).
8.1.2.

grid . , ,
. . , title( )
. gtext() ,
.
Xlabel(X), Ylabel(Y). 136

- , (, , ..),
LaTeX ( LaTeX
, [10]). ,
, ,
.
, .
1.

x=0:.01:2*pi;
y1=sin(x);
y2=sin(2*x);
y3=sin(4*x);
plot(x,y1,x,y2,x,y3)
, , Y, ,
x=0:.01:2*pi;
Y=[sin(x), sin(2*x), sin(4*x)];
plot(x,Y)

2. hold
on, , . , . hold off
, plot
, .. .
, . ,
x=0:.01:2*pi;
y1=sin(x);
y2=sin(2*x);
y3=sin(4*x);
plot(x,y1,--,x,y2,:,x,y3,+)
, , ,
+ (. 4). x,y,s, x y - , s -
137

. 4. Sin(x), Sin(2x) Sin(3x)


, -
.
S
y
m
c
r
g
b
w
k

8.1.3.

S
.
o
x
+
*
s
d
v

<
>
p
h

:
x-
-.

()
()
()
()


, . ,
138


5 , . axis.
.

axis([xmin xmax ymin ymax]) x y .


Inf,
. ,
-Inf.
axis(square)

axis(equal)

x y
.

axis(normal)

axis(ij)

. x - , y - .

axis(xy)


y .

axis(tight)

xmin , ymin .

139


axis(off) , .
axis(on)
8.1.4.

, .. , , subplot(m n p) , , subplot(m,n,p).
m , , n -
, p -
. subplot ,
. plot
. ,

x= -1:.1:1;
y1=sin(x);
subplot(2, 1, 1), plot(x, y1);
y2=log(abs(y1));
subplot(2, 1, 2), plot(x,y2);
. sin(x), - log(abs(sin(x))).
, x=0 log(0).
,
1 x 0.1 0.1 x 1.
8.2.
,
, , ,
.
8.2.1.

, . MATLAB
140

( ), .
, - , bar_h, , bar(bar_h).
, , bar(bar_h,x).
, bar(x, y,
line_style_string), plot(. . 8.1.2). , , . . ,
(help bar).
8.2.2.

MATLAB , . , ( plot line)


stairs yi , i i + 1,
stairs(y), xi xi+1, stairs(x,y). xi
. , [xs,ys]=stairs(x,y).
plot, line - (., ,
. 8.6)
8.2.3.

. (.. ) ( :
)6, , ,
6

.. , . , 150 200 ,
10 , 5 , .
150 155 .. , ,
141

.
MATLAB hist, hist(y)
10 ,
ymax ymin. , hist(y) .
- , .
- , .
,
.
.
bar, , hist(y) . [n,x]=hist(y) [n,x]=hist(y,num_of_bins)
[n,x] = hist(y,bin_centers) . n
y , x. hist
( bar), , ,
( stairs).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% STAIRS %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
y=randn(100,1);
%
dx=0.5;
%
x=-2.5:dx:2.5;
%
[ny,xh]=hist(y,x);
%
xh=xh-dx/2;
% x
[xl,yl]=stairs(xh,ny); %
hl=line(xl,yl);
%
hl,
set.
8.2.4. ()

, , errorbar(x,y,e).
, .
142

, y(x), x,y ,
, e.
x,y,e , , .
8.2.5.

fplot y x
plot. ,
,
.
, f (x).
, f (x), MATLAB
/ . f (x) , x, , , x.
, y = sin(x)cos(2x) x 0 5, fplot (sin(x) .*cos(2x), [0
5*pi]). fplot () . - , (
plot), - . 2 103,
, ,
. ,
. (, ) [Xp,Yp]
= fplot(...)]. ,
Xp Yp .
8.3.


. , , .
.

143

8.3.1.


plot(x,y), plot3(x,y,z). . 5 t=0:0.1:50; x=0.5*t.*cos(t);
y=0.6*t.*sin(t); z=0.2*t; plot3(x,y,z);
plot3,
- . 5.
. 8.1.1 -
. plot - plot3 comet - comet3.
comet3 , comet (. , . 8.1).
() line
plot, .. line(x,y,z).

set, . 5.1. ZData
( XData YData), z-
. ,
.
8.3.2.

mesh. mesh(z) ,
z. Z-
X-Y .
mesh(eye(10)). z=f(x,y) , xx yy , . meshgrid
x, xx, yy, , , y,
yy, [x,y] = meshgrid(xx,yy);.
z,
f , x y, mesh. , ,
[-2,2] [-2,2] (
144

)
xx = -2:.1:2;
yy = xx;
[x,y] = meshgrid(xx,yy);
z = exp(-x.^2 - y.^2);
mesh(z)
, [x,y] = meshgrid(-2:.1:2, 2:.1:2);
(help plot3/mesh/surf),
[6] [5].
8.3.3.

. MATLAB
- , X-Y, , - .
-
contour(Z), Z - , , .. Zij = f (i, j). MATLAB ,
. ,
contour(Z,n_of_lines),
, contour(Z,values
_of _levels).
, , ,
, .

Z , Z(1,1)
. X Y . ,
Z.
:
contour(x,y,Z); contour(x,y,Z,n_of_lines);
contour(x,y,Z,values_of_levels);
145

Contour of z = sin(x).*cos(y+ /2)


3

2.5

1.5

0.5

0
0

. 6. z(x, y) = sin(x) cos(y + /2).


f (x) = sin(x)
cos(y + /2).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
sin(x)*cos(y+pi/2)
% [4]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% x y
[x,y]=meshgrid(linespace(0,2*pi,30),linespace(0,pi,30));
%
% .*
z=sin(x).*cos(y+pi/2);
%
% -1 1
% 0.1 0
contour(x,y,z,[-1:0.1:-0.1 0.1:0.1:1]);
xlabel(x);
146

ylabel(y);
title(Contour of z = sin(x)*cos(y+ pi/2));
. 6.
, , ,
plot. , ,
, . ,
clabel, ,
contour3 contour.
clabel . contour

c=contour(x,y,z,[-1:0.1:-0.1 0.1:0.1:1]);
clabel(c);
, -
. , :
c=contour(x,y,z,[-1:0.1:-0.1 0.1:0.1:1]);
clabel(c,[-1:.2:1]);
, clabel. , clabel
clabel(c,manual). ,
.
, , ,
. ,
Return. , ,
.., , 8.4. ,
, , .. , ..,
contourf , contour.
147

8.4.

( )

,
.
,
8.1- 8.3 .
8.4.1.

. - , .
. , , , , ..
, , (line), (axes) .
, MATLAB .
, 10
, MATLAB.
.

Figure()
Axes ()
Line()
Patch ()
Surface ()
Image ()
Light ()
Text ()


figure
axes
line(x,y) line(x,y,z)
patch(x,y,c) patch(x,y,z,c)
surface(X,Y,Z,C), surface(X,Y,Z)
surface(Z,C), surface(Z)
image(C) image(x,y,C)
light(Prop_name,Prop_value,...)
text(x,y,text_str)
text(x,y,z,text_str)
uicontrol

User Interface Control


( )
User Interface Menu
uimenu
()

x,y z, line, , 148

( - ). .
x,y z, patch, (). c .
( surf) X,Y Z , , . C .
( text_str)
x,y z. x,y x,y,z.
light - , patch / surf. ( ) .
MATLAB
, - . -.
-
. , (line) ,
(axes) (figure). , .
, plot(1:10), . ,

figure;
axes;
plot(1:10);
, close,
, .. . delete ().
. 7. root . MATLAB. (figure) ,
, , . ,
. ( figure)
149

. 7.
.
figure(num), num -
, .
(figure) . - uicontrol, uimenu
Uicontextmenu , , . (axes) .
, (.. , , ..). subplot,
.
8.4.2.

,
MATLAB, (graphics
object handles). , . . , .
(root) (figure) ,
.
, , ..
graph_handl=function_name.
, , , , fig_han, axes_han line_han

fig_han = figure;
150

axes_han = axes;
line_han = plot(exp(-([-3:3].^2)))
, , plot
( ),
-. , , -, , -, ,
, ,
. , .
h=f() . :
bar compass errorbar feather
fplot
hist
polar
rose
stairs quiver
sphere cylinder
, , . , gcf(get current figure),
(.. , ) gca(get current axes).
, , - . , .
, , .
gco(get current object).
, (.. )
figure(fig_handl) / axes(axes_handl).
, , (gcf, gca .), , .
get set.
/
findobj.
, Root
151

. H=findobj(hl),
hl - - ,
H .
h=findobj(_,_) ,
_ _. , .
8.5.

. get set

, , , . , , . , , . (Property_Name) , , .
, .

( ). ,
get set.

h, get(h).
, , . ,
, line, hl=line(1,1) hl=line,
get(hl) ,
. (, ). ,
, line() 7
7

152

Color = [0 0 0]
EraseMode = normal
LineStyle = LineWidth = [0.5]
Marker = none
MarkerSize = [6]
MarkerEdgeColor = auto
MarkerFaceColor = none
XData = [0 1]
YData = [0 1]
ZData = []
...

= [0 0 0]
= normal
= = [0.5]
= none
= [6]
= auto
= none
X = [0 1]
Y = [0 1]
Z = []
...

,
. ,
, set(hl), hl - , . ,
Color
EraseMode: [ {normal} | background | xor | none ]
LineStyle: [ {-} | -- | : | -. | none ]
LineWidth
Marker: [ + | o | * | . | x | square | diamond | v | ^ | >]
MarkerSize
MarkerEdgeColor: [ none | {auto} ] -or- a ColorSpec.
MarkerFaceColor: [ {none} | auto ] -or- a ColorSpec.
....
, , ,
.
- get x=get(hl,
Property_Name). , x Property_Name. , x=get(hl, Color), x= [0 0 0] RGB-. set set(hl, Prop_Name,
153

Prop_Value); , ( line
hl) set(hl,Color,red), , , set(hl,Color,[1 0 0]).
8.6. ()
MATLAB . -
.
. movie.
" ", .. . ,
comet comet3. -, . (comet , comet3 ), ,
. ,
( -
).
, , ( ) plot, line histo,
axes()
,
. , .
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
t=0:0.1:100;
x=0:0.3:30;
k=1.3; w=0.9;
n=length(t);
y=cos(k*x-w*t(1))+cos(x-t(1));
figure
154

plot(x,y);
for i=2:n;
y=cos(k*x-w*t(i))+cos(x-t(i));
plot(x,y);
drawnow
end;
, . , -
.

- , ( ).
, .
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% >> %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all
%
t=0:0.1:100;
x=0:0.3:30;
k=1.3; w=0.9;
n=length(t);
% t(1)=0
y=cos(k*x-w*t(1))+cos(x-t(1));
%
axlim = [min(x) max(x) min(y) max(y)];
figure
%
lh = line(x,y);
%
set(lh,color,r);
axis(axlim);
%
set(lh,erasemode,xor);
%
155

for i = 2:n
%
y=cos(k*x-w*t(i))+cos(x-t(i));
% -
set(lh,XData,x,YData,y);
drawnow;
end
, .
1. -, EraseMode ( ) xor. . ,
- none
, . background,
.
grid on (
). xor
, background
, .
2.
XData YData (, XData, YData ZData,
).
3. drawnow, .
pause getframe.
( )
. pause
, drawnow ,
.

156

9.
. , .
,
. ,
(
) . ,
, , .
, /
:
1. .
2. , / .
3. ,
, , .
4. ( ).
, ,
, - .., . . 8 , ,
.
, .
, ,
. uicontrol.

MATLAB , , .

157

. 8.

9.1.
MATLAB (GUI-GraphicsUserInterface) () . MATLAB guide.
, , , .9.
(Control Panel, )
(Figure, ). ,
.
, guide - . guide -
, . .

, .
,
subplot(3,1,k), k - . ,
158

. 9.
, /
. .
, - , . N, R, C.
RC- N, R C - ( . 10
).
N =
N, R, C,
C =
(,
R =
), Ur .
R u n
E x it

- RUN -
. 10.
EXIT -
.
.

(. 10).
159

(axes ),
N,R,C (text), / (edit) (push).
, , , ( ). , , ,
, . ,
. .
edit,
. , . , ,
.
text, .
-
, ,
Property editor,
.
Push.
, .
,
, (Alignment
Tools).
, . , -
, ,
Shift.
.
- (, ..)

, Windows.
160


( Windows)
.

,
, /
.
.
normalized.

Property editor.
( Ctrl ), Units
( ) .
Units,
- , . Units normalized. FontUnits
. .
( , )
String, (, ).

8, Title.
8

,
.
161

, ,
, .
String, ( , )
, , FontUnits, normalized.
Tag, .
/ , . ,
, Tag. , ,
(Axes1, ),
, .
m-.
File/Close control panel
, NAME.M NAME.MAT ( NAME ).
, , .
MATLAB NAME.M, guide(gcf)
.
,
NAME.M ,
/ . GUIDE ,
, ,
, . , .
9.2.
GUI
Windows - .
.
, , 162

, CallBack. ( ),
, ,
.
( )
Enter ( ).
GUIDE
( ) m- uicontrol NAME.M, GUIDE. ,
, RUN, - ( NAME.M) GUIDE. ( RUN) ,
CallBack
(, Func_Run), .
. uicontrol
RUN :
h1 = uicontrol(Parent,h0, ...
Units,normalized, ...
FontUnits,normalized, ...
Callback,Func_Run, ...
FontSize,0.45, ...
ListboxTop,0, ...
Position,[0.722 0.131 0.1 0.0542], ...
String,Run, ...
Tag,Run);
NAME.M uicontrol CallBack,Func_run, ....
9.2.1.

NAME.M

M-
, , .
1. ,

163

h1 = axes(Parent,h0, ...
FontUnits,normalized, ...
CameraUpVector,[0 1 0], ...
CameraUpVectorMode,manual, ...
Color,[1 1 1], ...
ColorOrder,mat7, ...
FontSize,0.0877, ...
Position,[0.061 0.048 0.63 0.24], ...
Tag,Axes3, ...
XColor,[0 0 0], ...
YColor,[0 0 0], ...
ZColor,[0 0 0]);
Tag, (
Axes3)
.
2.
, , ..
h2 = text(Parent,h1, ...
Color,[0 0 0], ...
HandleVisibility,off, ...
HorizontalAlignment,center, ...
Position,[-0.0648 0.487 9.160], ...
Rotation,90, ...
Tag,Axes2Text3, ...
VerticalAlignment,baseline);
set(get(h2,Parent),YLabel,h2);
.
String
, , , . ,
YLabel , , Y.

164

3. uicontrol,
, , .
, ,
h1 = uicontrol(Parent,h0, ...
Units,normalized, ...
FontUnits,normalized, ...
BackgroundColor,[1 1 1], ...
Callback,Func_Edit, ...
FontSize,0.260, ...
ListboxTop,0, ...
Position,[0.814 0.889 0.120 0.0583], ...
String,num2str(N), ...
Style,edit, ...
Tag,EditN);
, Callback
( Func_Edit) ,
, . Tag, EditN,
. Style,
edit, , . String , . num2str,
( N)
.
4. , , ,
h1 = uicontrol(Parent,h0, ...
Units,normalized, ...
FontUnits,normalized, ...
Callback,Func_Run, ...
FontSize,0.45, ...
ListboxTop,0, ...
Position,[0.722 0.13125 0.1 0.0542], ...
Style,pushbutton, ...
String,Run, ...
Tag,Run);
165

Callback,
( Func_Run) , . ,
, String,
Tag ( )
.
, , NAME.M
N=1;
R=1; %
C=1;
info.N=N;
info.R=R; % INFO.
info.C=C;
uicontrol, ( Style,edit,...), String num2str(N), ,
h1 info.sN,
info.sN = uicontrol(Parent,h0, ...
Units,normalized, ...
FontUnits,normalized, ...
BackgroundColor,[1 1 1], ...
Callback,Func_Edit, ...
FontSize,0.26, ...
ListboxTop,0, ...
Position,[0.814 0.89 0.12 0.058], ...
String,num2str(N), ...
Style,edit, ...
Tag,EditN);
NAME.M Set(h0,UserData,info);.
info - UserData.
9.2.2.

. 11. DRIVE
166

DRIVE

GRAPH

G?L
MOUSE?

>:

RUN

EDIT

EXIT

. 11.
NAME.M, , , ..
-
. ,
Enter, Func_Edit, info, UserData.
Run, Run.
9.2.3.

Run

( RUN)
R-C .
-
h0=gcf;
info=get(h0,UserData);
N=info.N;
R=info.R;
C=info.C;
167

....................
%
%
% -
hax1=finobj(Tag,Axes1); %
subplot(hax1);
%
cla;
%
hl1=line(t,E);
% hl1
set(hl1,color,r,...
% hl1 r
parent,hax1);
% hax1
title( E(t));
%
...........................
.
9.2.4.

Exit

-
function f=Func_Exit()
delete(gcf);

.
9.2.5.

Edit

.
h0=gcf;
% .
info=get(h0,UserData);
%
hedN=findobj(h0,Tag,EditN
% e
newN=sscanf(get(hedN,string),%e);
if ~isempty(newN)
info.N=newN;
end;
................
168


findobj -
( - Tag).
NAME.M global UserData.
, , info
UserData
set(h0,UserData,info); % info UserData.

169

300 .

60 84/16
.-.

- ;
; 630090, -90,
. , 2.