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

..

.. , ..

MATLAB

:
21.04.01 (131000)

, 2016


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

ISBN
c
.. , .. , 2016


, -
-
.. .
, -
-
, ,
, -
. , -
.
- , -
, , ,
. , -
-

.
. , -
,
MATLAB.
.
MATLAB, C# , -
, MS Excel? ,
.
Petrel ? -
, -
, -
, -
.


,
.

for i=1:10 -
(7);
~ (12);
~x, X
x, X (17);
i = m, n i m
n (??).

4
1.
MATLAB

1. MATLAB
MATLAB, -
The MathWorks, MATrix
LABoratory. MATLAB , -
. -
MATLAB . -
, -
,
(C++, Pascal .). MATLAB -
, ,
. ,
MATLAB, MS Excel, -
DLL , -
.

1.1. MATLAB. M-
,
4 (. 1.1).
,
Desktop Desktop Layout Default. -
MATLAB, .
, , Command
Window ( ), , -
Current Folder ( ), -
Workspace ( )
Command History (
). -
. ,
. , -
,
.

5
1. MATLAB

. 1.1. MATLAB

-
>>. , 2+2 Enter:
>> 2+2
ans = 4
ans , -
, - -
.
-
, , ,
, , -
.
.
MATLAB -
Editor,
-
(Notepad, WordPad ). *.m,
MATLAB M-le .

6
1. MATLAB

Editor
2 ( -
- ) File.
MATLAB,
, Dock Editor (
).
- :
clear all (Workspace),
;
clc (Command Window );
close all
( ).
MATLAB -
, , ^ ,
.
. 1.1 .
-
.

1.2. .
-
, Hello world! MATLAB
. -
,
.

. -
M-, :
clc; clear all; close all

U = 3*15 %
3 = U/2 %
C3 = 2^3 %
d = C3 - c3 %
sineOfPi = sin(3.14) %
MATLAB
.

7
1. MATLAB

% , , ..
, -
. ,
,
.
, -
MATLAB .
() () -
, C3 c3
, sin(3.14)
, sineOfPi ,
.

, . -
.
,
( , MATLAB)
. -
,
, (-
x, x, ~x ..), (
). . -
,
,
, ..

, .
,
:

, very_long_variable;
,
camelCase (), veryLongVariable.

-
,
. -
, . ,
:

8
1. MATLAB

clear all %
3 = U/2 % - U
U = 3*15
, -
( F5
Run).
- ,
, . -
, - .
,
,
, :
Change Directory , , -
;
Add to Path -
.
,
.
Workspace 5 -
(. 1.2).

. 1.2. MATLAB

9
1. MATLAB

. -
, -
;.
S = sin(3.14)
S =
0.0016

, 2- -
. MATLAB pi
15- . sin() :
S = sin(pi)
S =
1.2246e-016
.. S 16- -
. MATLAB (
) -
, .. , S = 1.2246 1016.

1.3.
-
. MATLAB . -
round(), -
. ,
. ,
:
n = 3;
x = round(pi*10^n)/10^n
.. 1000, ,
1000.
, -
. 1.1.

10
1. MATLAB

1.1. MATLAB


abs(x)
sqrt(x)
exp(x)
log(x)
log10(x)
( ) sin(x)
( ) cos(x)
( ) tan(x)
( ) cot(x)
asin(x)
acos(x)
atan(x)
round(x)
ceil(x)
floor(x)

1.4.
1. x y. , -
, .. x y, -
y x.
2 ax2 sin(x)
2. y = p x = 6, a = 3.
ea + ln(1 + x)
3. q = 3 e6.3 -

. 
4. w = 175 tg -
4
.
5. x. -
, .. , .
6. z. -
.

11
2.

2.
MATLAB , -
. -
11.
MATLAB , . -
, , , -
.
,
.
, ,
, . , -
>>,
.

2.1.
, , -
().
. , , -
, . -
.
, ~x.
() , .
, ,
, x1 , x2 xi .
,
.
-:
T~ = [23, 24, 22, 25, 26, 24, 25],
T1 = 23, T2 = 24, T3 = 22, T4 = 25, T5 = 26, T6 = 24, T7 = 25,
7.
MATLAB , -
,
. -
, ,
, .
MATLAB :
>> T = [23,24,22,25,26,24,25]

12
1. MATLAB

T =
23 24 22 25 26 24 25
. , -
:
>> T_weekend_mean = (T(6)+T(7))/2
T_weekend_mean =
24.5000
-
:
sum(T) ;
length(T) ( ) .
:
>> T_week_mean = sum(T)/length(T)
T_week_mean =
24.1429
,
. end:
>> T_Sunday1 = T(end)
T_Sunday1 =
25
>> T_Sunday2 = T(length(T))
T_Sunday2 =
25
, , .
-
(), .
:, -
, () -
. -. , ,
1 10:
>> odd = 1:2:10
odd =
1 3 5 7 9

13
2.

, 10
, odd.
:
>> inc = 0:0.2:1
inc =
0 0.2000 0.4000 0.6000 0.8000 1.0000
>> dec = 10:-1:1
dec =
10 9 8 7 6 5 4 3 2 1
1, :
>> x = 1:10
x =
1 2 3 4 5 6 7 8 9 10
, -
. , ,
, :
>> T_3_days = T([1 2 5])
T_3_days =
23 24 26;
-
(:), , -
, . , -
:
>> x_even = x(2:2:8)
x_even =
2 4 6 8
>> x_inverse = x(end-2:-1:1)
x_inverse =
8 7 6 5 4 3 2 1
,
. ,
x
:

14
1. MATLAB

>> x(1:2:end) = -x(1:2:end)


x =
-1 2 -3 4 -5 6 -7 8 -9 10
>> x(2:2:end) = 0
x =
-1 0 -3 0 -5 0 -7 0 -9 0
( )
, [ ].
, x , -
:
>> x([2, 4, 5]) = [ ]
x =
-1 -3 0 -7 0 -9 0

2.2.
,
-,
-. ,
,
( ) , MATLAB. :
>> odd
ans =
1
3
5
7
9


: , , -
. MATLAB
:
() .. -
( ) ;
+ - - -
;

15
2.

(.*), (./)
(.^); ..
, .
, 2 x:
>> x = x+2
x =
3 4 5 6 7 8 9 10 11 12
dec x:
>> s = dec+x
s =
13 13 13 13 13 13 13 13 13 13
, -
.*. -
-
, :
>> p = x.*dec
p =
30 36 40 42 42 40 36 30 22 12
,
-
,
- - -
( -
).

x dec -
:
>> x*dec
ans =
330
,

:

16
1. MATLAB

>> dec2 = dec.^2


dec2 =
100 81 64 49 36 25 16 9 4 1
MATLAB
. ,
dec2
sqrt:
>> sq = sqrt(dec2)
sq =
10 9 8 7 6 5 4 3 2 1

2.3.
,
m n .
- -
. -
, X.
() , . -
, ,
. ,
, , Xij .
MATLAB , -
,
.
:
>> A = [1 2 3 4; 5 6 7 8; 9 10 11 12]
A =
1 2 3 4
5 6 7 8
9 10 11 12
( )
. , ,
3 2:
>> A(3,2)
ans =
10

17
2.

(. . 2.1),

.
, -

. A,
, :
>> A(:, 1)
ans =
1
5
9
>> A(2, :)
ans =
5 6 7 8
,
, ,
;. ,
A :
>> y = [1; 2; 3];
>> B = [A y]
B =
1 2 3 4 1
5 6 7 8 2
9 10 11 12 3

2.4.
- -
, A+c, , A*c. -
A B , -
A+B. (
), *
(
). , - x -
A x.
>> x = [2 4 6 8]

18
1. MATLAB

x =
2
4
6
8
>> y = A*x
y =
60
140
240
, , -
, , . ,
A B , -
C=A.*B C, -

A B.
-
det(A).
,
:
zeros(m,n) m n ,
;
ones(m,n) m n ,
.
:
>> A = ones(2,3)
A =
1 1 1
1 1 1
>> B = zeros(4,1)
B =
0
0
0
0
, -
,

19
2.

, -
1.
, -
, .
-
. -
,
.
size() .
(
, ), ,
:
>> d = size(A)
d =
2 3
>> [m, n] = size(B)
m =
4
n =
1

2.5.
M S Excel
(, data.xls) -

N = xlsread(data.xls);
N ,
.
, ,
N = xlsread(data.m, 2, A3:D16).
,
xlswrite().
.
M S Excel.

20
1. MATLAB

MATLAB xls-. -
, mat- (-
MATLAB), -
load() save().

2.6.
MATLAB.

1. ~x = [1, 2, , 97].
2
(x1 x3 x4 )x2 .
2. ~x. ~y, -
, .
3. .
, , .
4. ~x
2, 5, 8, ..., 20. :

~ y , yi = x2i , i -
;
~x -
~y .

5. n- A B -
A1 , A2 , ..., Ans B1 , B2 , ..., Bn .
n
=
P 2
(Ai Bi ) .
i=1
.
6. , -
xi yi
zi = , xi , yi ~x = [1, 2, 3, 4]
xi + yi
y = [8, 6, 4, 2].
~
7. x n.
n n, x.
8.

1 2 3 4 5

A= 7 9 1 0 1 .

8.3 0 7 1 1

21
2.

:
A A;
CC 2BC + BB , B
A, ,
; C A,
, ;

A.
9. :
3
P
Ai1 Ai2 ;
i=1
5
P
Aik Ajk , i, j = 1, 3,
k=1

A .

22
1. MATLAB

3.
3.1. plot()
M-
F5 Run, -
,
. , -
( clear all
clc).

plot(), :
x ;
y .
x,y . -
.
3.1. y = ax2 + bx+ c.
,
, ,
plot():

x = -2:0.1:2;
a = 3; b = 5; c = 2;
y = a*x.^2+b*x+c;
plot(x,y)
,
,
.
, , x y,
,
.
x
!

23
3.

25

20

15

10

5
2 1.5 1 0.5 0 0.5 1 1.5 2

. 3.1.

3.2.
plot. -
() , -
(. 3.1)
(. 3.2); , (. 3.3) .
, r:
.
-
. , , -
, , plot(x,y,LineWidth,2)
2 . -
.
, , -
.
( MarkerSize),
( MarkerEdgeColor) (
MarkerFaceColor).

24
1. MATLAB

3.1.

y
m
c
r
g
b
w
k

3.2.

-
--
:
-. -

3.3.

o
x
+
*
s
d
p

25
3.

, -
:
title ( ) ;
text (x,y, ) -
(x, y);
gtext () ,
;
xlabel( x) ylabel( y)
;
legend() -
.

( FontName), ( FontSize), -
( FontAngle italic),
( FontWeight). -
, -
. 3.2.
,
plot() , -
, -
-
hold on.
3.2.
, . -
, -
. -
grid on (. 3.2).

x = -2:0.1:2;
a = 3; b = 5; c = 2;
y = a*x.^2+b*x+c;

D = b^2-4*a*c; %
%
x12 = [(-b+sqrt(D))/(2*a), (-b-sqrt(D))/(2*a)];

plot(x, y, LineWidth, 2)
grid on; hold on

26
1. MATLAB

plot(x12, [0 0], d, MarkerEdgeColor, b,...


MarkerFaceColor, m,...
MarkerSize, 10)

title(Graph 2, FontSize, 16, FontName, Times)


xlabel(x, FontSize, 16, FontWeight, bold)
ylabel(Function, FontSize, 16, FontWeight, bold)

text (-1.1, 3, Solution,...


FontAngle, italic, FontSize,13)
legend (parabola, roots)

, -
figure .
,
.

Graph 2
25
parabola
roots
20

15
y

10

solution
0

5
2 1.5 1 0.5 0 0.5 1 1.5 2
x
. 3.2.

27
3.

3.3.

,
. - -
, -
. ,
, .. .
f igure,
axes. ,
set(h, property, value), h , property
, value . ,
.. , -
gca (GetCurrentAxes).
, Oy ,
, YDir. -
reverse set(gca,YDir,reverse).


- x1, y1 x2, y2
, ,
y1(i) < y2(i) i. .
fill(). -
, -
, .
x1, y1
Ox, -
x2, y2. , x2, y2
: x2(end:-1:1) y2(end:-1:1) (. 4 . 2.6). -

:
fill([x1, x2(end:-1:1)], [y1, y2(end:-1:1)], color)
color , -
, ( r -
), 0 1, -
, (RGB),
[0.5 0.25 0.1] .

28
1. MATLAB


F igure ()
Insert () . -
, Arrow ()
, -
. , -
P ropertyEditor ( ). -
, ,
, P ropertyEditor ( ).
,
F igure () F ile Save. -
(bmp, jpg,
png, tif, eps, pdf .).

3.4.
1. y = x sin(x) [2 , 2 ].
2. y = 1 + x2
y = 1 x2 .
. . -
.
3. .
!
4.
.

29
4. 3D

4. 3D
4.1.
, -

~r(s) = [x(s), y(s), z(s)],

x, y, z,
plot3(x, y, z). ,
, ,
, .
, LineWidth, MarkerSize . (. . 3.2).

4.2.
Z z(x, y)
(Xi , Yj ), .. Zij = z(Xi , Yj ). -
. plot3()
, , .. -
, ,
, Z.
ndgrid(), ,
:
[Xgrid, Ygrid] = ndgrid(X, Y);
( , -
): X (n ) Y (m ). 2 -
n m.
, X = [1 2 3], Y = [5 6], -
Xgrid Ygrid
Xgrid = Ygrid =
1 1 5 6
2 2 5 6
3 3 5 6
:
plot3(Xgrid, Ygrid, Z, .k, MarkerSize, 5);

30
1. MATLAB

4.3.
MATLAB
: mesh() surf().
() , -
. -
x y (
ndgrid()), z -
. .. X, Y, Z mesh() surf()
. -
.
. 4.1 z = x2 y 2 , -
mesh(). ,
0.2.

[X, Y] = ndgrid(-2:0.2:2, -2:0.2:2);


Z = X.*2 - Y.*2;
mesh(X, Y, Z);
title (3D plot with function mesh);
xlabel(x); ylabel(y); zlabel(z);

3D plot with function mesh

0
z

4
2
1 2
0 1
0
1 1
y 2 2
x

. 4.1. , mesh()

surf() -
,
z(x, y) , .
. . 4.2 ,

31
4. 3D

surf(). -
, -
( colorbar).

3D plot with functions surf and colorbar


4

3
4
2
2
1

0
z

2
1

4 2
2
1 2
1 3
0
0
1 1 4
y 2 2 x

. 4.2. , surf ()

(0.1 0.2),
(. 4.3).

3D plot with dense grid

0
z

4
2
1 2
0 1
0
1 1
y 2 2
x

. 4.3. , surf (),

32
1. MATLAB

4.4.

z = f (x, y) , .. ,
. -
MATLAB contour()
contourf(). (. 4.4),
(. 4.5). -
, contour() contourf(),
ndgrid().
contour(X, Y, Z,10) contour(X, Y, Z,20)
2 2

1.5 1.5

1 1

0.5 0.5

0 0

0.5 0.5

1 1

1.5 1.5

2 2
2 1 0 1 2 2 1 0 1 2

. 4.4. , -
contour(),

contourf(X, Y, Z,10) contourf(X, Y, Z,20)


2 2

1.5 1.5

1 1

0.5 0.5

0 0

0.5 0.5

1 1

1.5 1.5

2 2
2 1 0 1 2 2 1 0 1 2

. 4.5. , -
contourf (),

33
4. 3D


z(x, y) (. 4.6):
[C,h] = contour(X, Y, Z, 10);
clabel(C,h);

contour(X, Y, Z,10) with labels


2
2.
1 545 3.2727 2
0. .09 5 818 64
36 09
2.545
5 1. 63 4
36 9 0.3 636 09
1.

1.5 9 0 3 09
09 1. .545 3.2727

4 1.8182 . 0 .
1 0 1.
09 818 5

45 2
0 1.0909

2.5 818
.36
2

5
1 36

1.
2

4 64
363
0.

27
0.3

64
0.5

63

3.27
63

1.0909
1.0909

0.3
64

0
82

1.81
1.81

0.5

2.5
455

82
0.

455
363
2.5

0.
3 64 36
4 64 36
6 36
1 36 0.3 1.0909
0. 4

1.
09

09
09

1.8182 1

09
.09
1.

1.5 09 0.
2.54 36
09 55 1
.81 36
.09 455 82 4
1 2.5 3.2727
2
2 1.5 1 0.5 0 0.5 1 1.5 2

. 4.6. -

clabel(C,h)
, . ,

clabel(C, h, FontSize,15, Color,r, LabelSpacing,72);


15 , -
72 , 1
(2.54 ). clabel(C, manual),
.

34
1. MATLAB

5.
5.1. f or
,
.
for:
for i = i1:h:i2
<>
end
i , ,
i1 , ; h
, ( ,
1); i2 , -
.
for
end. , , .
i , ..
, i1, h i2 , -
, 1, ..
i = i1:i2.
, ,
.
5.1. 1 10.
S 0.
S 1, 2, 3
.. , ,
S. i
.
S=0;
for i=1:10
S = S+i;
end
S
. S = S+i,
. ,

35
5.

= , -
== (. . 5.1), ,
.. , ,
, . :
S 0;
(i = 1) S + i = 0 + 1 = 1 -
S, S = 1;
(i = 2) S + i = 1 + 1 = 2 -
S, S = 2 ..
-
, -
.
, -
S.
(Editor) for end
.
.
-
. break.
if.
break ,
,
.

5.2. if

if <_()>
<>
end
if (), MATLAB
. ( -
), ,
end, , -
, end.

36
1. MATLAB

if -
:
if _
<1>
else
<2>
end

elseif. , -
, else -
if.
5.2. ,
, a, b, c.

,
if. , -
2 ; ,
; , -
-
. -
, error().
clear all
clc

a = 3; % x^2
b = 5; % x
c = 2; %
D = b^2-4*a*c; %

if D > 0 % 2
x(1) = (-b+sqrt(D))/(2*a)
x(2) = (-b-sqrt(D))/(2*a)
elseif D == 0
x = -b/(2*a) % 1
else
error( ) %
end

37
5.

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

(. 5.1) (. 5.2).
5.1.

==
~=
<
>
<=
>=

5.2.

~
||
&&

5.3. -

(, -
).
--. -
function.

38
1. MATLAB

,
myfunc. , myf unc.m.
myfunc, -
X1, X2, ..., Xn
Y1, Y2, ..., Ym . -
, .
myf unc.m .

function [Y1, Y2, ..., Ym] = myfunc(X1, X2, ..., Xn)
, -
.
5.3.
. solveSquareEq.m
:
%
function [x1, x2] = solveSquareEq(a, b, c)
D = b^2-4*a*c;
if D > 0
% 2
x1 = (-b+sqrt(D))/(2*a);
x2 = (-b-sqrt(D))/(2*a);
elseif D == 0
x1 = -b/(2*a); % 1
x2 = NaN;
else
error( ) %
end
, x2 -
N aN (N ot a N umber), ,
.
, -
clear all,
, . -
clc,
. -
, , , ..

.

39
5.


, -. , -
-
, .
, - ()
.
.
, x1 x2:
a = 4;
coeff2 = -2;
[x1, x2] = solveSquareEq(b, coeff2, 0.5+0.5);
-
, ,
. , -
-
. ,
, -
.

. ,
,
, -
.
%
function x = solveSquareEq2(a)
D = a(2)^2-4*a(1)*a(3);
if D > 0
% 2
x(1) = (-a(2)+sqrt(D))/(2*a(1));
x(2) = (-a(2)-sqrt(D))/(2*a(1));
elseif D == 0
x(1) = -a(2)/(2*a(1)); % 1
x(2) = NaN;
else
error( ) %
end
solveSquareEq2 :

40
1. MATLAB

p = [4, -2, 1];


x = solveSquareEq2(p)
, -
- , :
??? Input argument "a" is undefined
Error in ==> solveSquareEq2 at 2
, -
. -
-,
.


, ,
, . -
. ,
-
, -
.
, (-
, a = 2.5;) ( )
, .
,
, . -
, -
. -
,
.



, ,
.
.

41
5.

:
% -
function MyEquation()

p = [4, -2, 1];


x = solveSquareEq2(p)

%
function x = solveSquareEq2(a)
D = a(2)^2-4*a(1)*a(3);
if D > 0
% 2
x(1) = (-a(2)+sqrt(D))/(2*a(1));
x(2) = (-a(2)-sqrt(D))/(2*a(1));
elseif D == 0
x(1) = -a(2)/(2*a(1)); % 1
x(2) = NaN;
else
error( ) %
end
:
.
.


,
function.

@. , f (x) = 2 sin(x) x2 :
f = @(x) 2*sin(x) - x^2;
,
f , -
(. ), .
, , -
. .. x
,
.

42
1. MATLAB

-
. , ,
, -
( ),
.


, -
, ,
-
f (x).
,
f (x). .. f (x) -
. , -
. , -
function, @, -
.
:

%
function [res1, res2] = main()
%
f1 = @(x) 2*sin(x) - x^2;

res1 = my_algorithm(1, f1);


res2 = my_algorithm(1, @f2);

% f(x)
% x - , f -
function res = my_algorithm(x, f)
res = f(x) + 2.5;

%
function y = f2(x)
y = 2*cos(x) + x^2;

43
5.

5.4.
,
sum() factorial() -
for.

1. 1 + 3 + 5 + 7 + ... + 113.
2. n.
n! = 1 2 3 ... n.
3. n. , -

1 + 1 2 + 1 2 3 + 1 2 3 4 + ... + 1 2 ... n.
n = 4, n = 6 n = 17.
10
bk k2 ,
Q 
4.
k=1
~b = 1 , 1 , 1 , ..., 1 .
 
30 28 26 2

. ~b -
: .
5. ~x xm = 2m 1,
m = 1, 9.
6. Q Qij = i2 + j 2 ,
i, j = 1, 5.
7. a, b, c n. -
, n n,
b, c
a. .
8. . ,
.
9. . ,
, 5. ,
.
Qn
10. , (10 Xk ),
k=1
k6=2,k6=4
~ = [X1 , X2 , ..., Xn ]. 3- -
X
~ X
X, ~ = [1, 2, 1, 2, 0, 1, 2].

44