You are on page 1of 211

.

MATLAB/SIMULINK

2008/2009

2 MATLAB/SIMULINK

:
MATLAB/SIMULINK -
/ :
, , o , , , .
: - ,
, ,
.
/ /
,
.


/
.
m.fileovi- SIMULINK
work
0 Matlab Tutorial.
. -

MATLAB/SIMULINK 3


MATLAB/SIMULINK

, . , ,
.
:
: kkk_pgp_xx_ime na problemot.m ( )
kkk
- () -
p
-
gp
- g - , p
xx
- ( )
ime na problemot - .
,
.
:
MATLAB (MATLAB TUTORIAL MT)
mt. 1, 2, 3, ...
, 3: 3.1.,3.2. 3.3, ... .,
3.1.01, 3.1.02.,..., 3.2.01., 3.2.02, ...
3.3.01., 3.3.02. ... ., MATLAB
pgp_xx = p31_01, p31_02, p32_01, p32_02, p33_01, p33_02 .
3.1.01. 3 3.1.
mt_p31_01_polinomi.m.
, , ()
5 2 20 (
5.2.20) (m ) ts_p52_20.m.

:
() () MATLAB/SIMULINK ts_pp_xx_xx.m, xx_xx
.

.

4 MATLAB/SIMULINK


work
().

. work
, .
, .1. MATLAB6p5 work1
( .2.)

.1.

.2.

cd. .
work1 .
cd, , :
>> cd,
C:\MATLAB6p5\work1 ( MATLAB)
cd .. , :
>> cd ..
>>
( MATLAB)
>> cd,
( )
C:\MATLAB6p5
( MATLAB)
cd (work1) . MATLAB6p5 . work1, :
C:\MATLAB6p5
( )
>> cd ('work1')
( work1)
>>
( MATLAB)
>> cd,
( )
C:\MATLAB6p5\work1 ( MATLAB)
work1 NIR proekt, :
C:\MATLAB6p5\work1 ( )
>> cd ('NIR proekt')
( NIR proekt)
>>
( MATLAB)
>> cd,
( )
C:\MATLAB6p5\work1\NIR proekt ( MATLAB)

. NIR proekt ( help, , , .).

MATLAB/SIMULINK 5

1.

..........................................................................

1.1. ....................................................................................................

1.2. .....................................................

10

1.3. ...........................................................................................

14

1.4. ............................................................................

15

1.5. ...............................................................................................
1.5.1. (:).........................................
1.5.2. ................................
1.5.3. ..........................................................................
1.5.4. ........................................................................
1.5.5. (*) ..........................................
1.5.6. (.*) ...............................................................
1.5.7. () (./) ................................................
1.5.8. (.^)

18
20
20
21
21
22
25
27
28

1.6. .................................................
1.6.1. ..............................................................
1.6.2. ..........................................
1.6.3. .....................................................
1.6.4. .................................................................
1.6.5 .....................................................................
1.6.6. ................................................................
1.6.7. .........................................................
1.6.8. .....................................
1.6.9. ..................................................
1.6.10. ............................................
1.6.11. .............................................................
1.6.12. ........
1.6.13. .............................................................
1.6.14. ...................................................................

30
31
32
32
33
34
34
35
37
38
39
40
40
42
43

1.7. .....................................................
1.7.1. ...........................................
1.7.2. .........................................................................
1.7.3. ...................................................................
1.7.4. (zoom) ..................................................................
1.7.5. (subplot)
1.7.6. ...............................

45
48
50
51
52
53
55

6 MATLAB/SIMULINK

2.

3.

61

2.1. .........

61

2.2. () ......

65

2.3. ......

66

2.4. While ........

69

2.5. if ..... then .... else .... end ...

71

2.6. ( m-)
2.6.1 - .........
2.6.2 - .............
2.6.3 ...............

72
72
75
78

2.7. ................
2.7.1. ........
2.7.2. sum .......................
2.7.3. max min ................
2.7.4. ....................
2.7.5. find ........
2.7.6. find ........

81
81
82
83
84
85
87

2.8. .......................
2.8.1. MATLAB .
2.8.2. .....................
2.8.3. ...
2.8.4. ..........
2.8.5. () ...........

88
88
88
90
90
90

2.9. () .........
2.9.1. () ......
2.9.2. () ...............
2.9.3. input ........................

92
92
93
94

......

95

3.1.
3.1.1. ..
3.1.2.
3.1.3. ()
()
3.1.4.

95
96
96

3.2.
3.2.1. .
3.2.2. .
3.2.3. ..

99
99
101
104

3.3. ( ) ..
3.3.1. .
3.3.2.

106
106

97
97

MATLAB/SIMULINK 7

4.

5.

109

3.4. .
3.4.1. .
3.4.2.
3.4.3. .

110
110
112
113

3.5. ...
3.5.1.
3.5.2. ( - )

115
119
121

3.6.
3.6.1.
3.6.2.

127
127
132

3.7. .
( !)
3.7.1. .
3.7.2. ........................................................................
3.7.3. - ....................................................................
3.7.4. ..............................
3.7.5. .........................

136

SIMULINK

137

4.1. SIMULINK? ..........................................................................

137

4.2. SIMULINK ............................................................

137

4.3. ........................................................................

139

4.4. ........................................
4.4.1 ....................................................................................
4.4.2 .........................................................................................
4.4.3 ...................................................................
4.4.4 .................................................

140
140
142
142
143

4.5. ...................................................................

145

4.6. ..........................................................................
4.6.1 .............................................................
4.6.2 ..................................................
4.6.3 ........................................
4.6.4 ................................................................................
4.6.5 ...............................................

149
149
152
153
158
158


( )

161

5.1.

161

5.2.
5.3.

8 MATLAB/SIMULINK

6.


(CONTROL SYSTEM TOOLBOX)
6.1. ...........................................................................................
6.1.1 ....................................................................
6.1.2 .............................
6.1.3 () ()
....................................................................................
6.1.4 ...................................................

169
169
169
171
171
172

6.2.
LTI .................................................
6.2.1 laplace .
6.2.2 tf
6.2.3 s z ....................................................................
6.2.4 ss .
6.2.5 pole zero

177
177
178
179
180
181

6.3. ..........................................................
6.3.1 initial
6.3.2 impulse .
6.3.3 step
6.3.4 lsim

182
182
184
188
189

6.4. LTI ......................


6.4.1 bode .
6.4.1 margin .
6.4.1 nichols .
6.4.1 nyquist .

194
194
197
199
199

6.5. (Root Locus)

201

6.6. Matlab Command List ..

203

7.

( )

206

8.

(T )

207

A: help

207

MATLAB/SIMULINK 9

1.
1.1.
MATLAB -
. ,


. :




,
,

MATLAB
.

. MATLAB-


FORTRAN C.
Imeto MATLAB MATrix LABaratory LINPACK EISPACK
.
MATLAB-
. ,
, . , MATLAB - () , .
MATLAB-
toolboxes (),
. MATLAB
(m-) MATLAB
.
:
, , , , ,
.

10 MATLAB/SIMULINK

MATLAB-
FORTRAN
C++.

.

C++ FORTRAN
MATLAB - .
1.2. MATLAB
MATLAB
MATLAB-. ,
m-.
MATLAB- WINDOWS
MATLAB . , MATLAB
>>
>> MATLAB promt
, .
help,
. quit
MATLAB.
MATLAB-
MATLAB -, .
:
+ (Addition),
- (Subtraction),
* (Multiplication),
/
(Division),
^ (Power),
( ).
>>.
:
-
>> 5^2
Enter MATLAB-
ans =
25

MATLAB/SIMULINK 11

:
>> 3.50*11/7+(25+6^3)
, Enter MATLAB :
ans =
246.5000
.
ans
. :
>> x = 3.50*11/7+(25+6^3)
MATLAB-
x=
246.5000
x. ,
3.50*11/7+(25+6^3), x
( ),
,
. x
.
:
>> x
x=
5.7500
x.
,
- ( ;)
>> x = 2 + 3/4*5; y = 2^2, z = 3^1.5;
y=
4
, x, y z,
. x z ;
y
;
. x z :
>> x
x=
5.7500
>> z
z=
5.1962

12 MATLAB/SIMULINK

MATLAB
MATLAB () .

1.

2.

3.

1.234, -10.76
3.21 - 4.3i ( i )

4.

Inf

5.

NaN

, 0/0

1362, -217897

"e" :
-1.3412e+03 = -1.3412x103 = -1341.2
-1.3412e-01 = -1.3412x10-1 = -0.13412
MATLAB (double
precision), 15- .
MATLAB "format".

: 45.7503820449325179

1.

>> format short

45.7504 (4 )

2.

>> format short e

4.5750e+001

3.

>> format long

45.75038204493252 (15 )

4.

>> format long e

4.575038204493252e+001

5.

>> format bank

45.75 (2 )


, 15
.
, format
:
>> format short
>> pi
ans =
3.1416
> format short e
>> pi
ans =
3.1416e+000

MATLAB/SIMULINK 13

>> format long


>> pi
ans =
3.14159265358979
>> format long e
>> pi
ans =
3.141592653589793e+000
>> format bank
>> pi
ans =
3.14

MATLAB-, format.

>> format compact


.
.
, MATLAB format
:
>> 34*3
ans =
102
format compact :
>> format compact
>> 34*3
ans =
102
( ).

14 MATLAB/SIMULINK

1.3. ()

. .
ProCredit, Tri2Plus, Y4, z21x6
:
Pro-Credit=1, Tri2%Plus=2, Y@4=3, z21#x6=4
.

.
: MATLAB- .
eps = 2.2204-16 = 2-54 1+eps
1 ,
pi = 3.14159 = .
MATLB- i j
( 1 ), .
:
>> i,j, i=3
ans =
0 + 1.0000i
ans =
0 + 1.0000i
i=
3
,
i
3.
:
1) -2^3+9
2) 2/3*3
6) 3*(3*4-2*5^2-3)

3) 3*2/3

4) 3*4-5^2*2-3

5) (2/3^2*5)(3-4^3)^2

MATLAB/SIMULINK 15

1.4.
MATLAB
,
.
MATLAB:
Elementaru Math Functions -
Trigonometric Functions
sin
Sine ()
sinh
Hiperbolic sine ( )
asin
Inverse sine ( - arcsin)
asinh
Inverse hyperbolic sine ( )
cos
Cosine ()
cosh
Hyperbolic cosine ( )
acos
Inverse cosine ( )
acosh
Inverse hyperbolic cosine ( )
tan
Tangent ()
tanh
Hyperbolic tangent ( )
atan
Inverse tangent ( )
atan2
Four quadrant inverse tangent ( )
atanh
Inverse hyperbolic tangent ( )
coth
Hyperbolic cotangent ( )
Exponential Functions
exp
Exponential ( exp(x)= x )

log
Natural logarithm ( )
log10
Common (base 10) logarithm ( 10)
log2
Base 2 logarithm and dissect floating point number (. 2)
pow2
Base 2 power and scale floating point number
sqrt
Square root ( )
Complex Function
abs
Absolute value and complex magnitude ( )
angle
Phase angle ( )
conj
Complex conjugate ( )
imag
Imaginary part of complex number ( ..)
real
Real part of complex number ( )
Rounding and remainder
fix
Round towards zero ( )
floor
Round towards minus infinity (. )
ceil
Round towards plus infinity (. )
round
Round towards nearest integer (. )
mod
Modulus (signed remainder after division) ( rem)
rem
Remainder after division ( )
sign
Signum (signum sign(-3.26)=-1)

16 MATLAB/SIMULINK

MATLAB
>>help funkcija

.
>> x = 5*cos(pi/6), y = 5*sin(pi/6)
x=
4.3301
y=
2.5000
sin cos pi/6,
.
>> acos(x/5), asin(y/5)
ans =
0.5236
ans =
0.5236
>> pi/6
ans =
0.5236
cos sin
pi/6. x y
MATLAB-
.
.
>> x = 9;format;
>> sqrt(x), exp(x), log(sqrt(x)),log10(x^2+6)
ans =
3
ans =
8.1031e+003
ans =
1.0986
ans =
1.9395
>> format long e, exp(log(9)), log(exp(9)
ans =
9.000000000000002e+000
ans =
9
>> format short

MATLAB/SIMULINK 17

exp(x) exp( x) e log(x) . 9.


format long e,
15 15-
. format short,
.
x

:
1. 3x , x 2 .
3

3. 7 x

x2
, x6.
2
3
4. 4 x , x 2 .
2
6. x 4 x , x 3 .
2.

, x4.
1

1 x
, x2.
x 1
2
3
7. 3x 4 x , x 2 .
3
9. 5( x ) , x 3 .
5.

11. 500 x

13. x

60

1
, x4.
( x )3
10.000
12.
, x 1,075 .
x120
10.

, x 1,01 .

e 0.2696*0.2 sin(2 0.2)


0.01 3 ln 18

14. x 3, y 5, z

15.

8. 6 x (6 x ) , x 10 .

e 0.2696 x sin(2 y )
x y ln(x y )

x 5.5, y 7.0, z ( x y ) 2

e 0.5 x cos(2 y )
x 2 y 2 ln(x 2 y 2 )

16. :

z1 3 4 j

i z2 7 10 j

: z z1 z2 , u z , w z1z z2u, a | z |, b | w |

18 MATLAB/SIMULINK

1.5.
, , :
- -. -
(blank).
() ,
.
[ ] .
>> v = [1 3, sqrt(5)]
v=
1.0000 3.0000 2.2361

>> length(v)
ans =
3
x ,
, ,

.
.
>> v2 = [ 3+ 4 5]
v2 =
7
5
>> v3 = [ 3 +4 5]
v3 =
3 4 5
v2 + MATLAB-
3 4 .
v3, +,
3 .
,
. v v3
, .
>> v + v3
ans =
4.0000 7.0000 7.2361
>> v4 = 3*v
v4 =
3.0000 9.0000 6.7082

MATLAB/SIMULINK 19

>> v5 = 2*v - 3*v


v5 =
-7.0000 -6.0000 -10.5279
:
>> w = [1 2 3], z = [8 9]
>> cd = [ 2*z, -w]
>> sort(cd)
w=
1 2 3
z=
8 9
cd =
16 18 -1 -2 -3
ans =
-3 -2 -1 16 18
cd w z. sort
cd ,
MATLAB-.

.
>> w(2) = -2, w(3)
w=
1 -2 3
ans =
3
. :
>> w = [ 2 4 9 16 25 ]
w=
2 4 9 16 25
>> sqrt(w)
ans =
1.4142 2.0000 3.0000 4.0000 5.0000
sqrt ( )
w.
MATLAB-
,
. .

20 MATLAB/SIMULINK

1.5.1. (:)
( : ),
.
>> 1:4
>> 3:7
>> 1:-1
ans =
1 2 3 4
ans =
3 4 5 6 7
ans =
Empty matrix: 1-by-0
1
4. .
3 1
7.
-1 1.
m : k : n
m k n ( n).
>> 0.32 : 0.1 : 0.6
>> -1.4 : -0.3 : -2
ans =
0.3200 0.4200 0.5200
ans =
-1.4000 -1.7000 -2.0000
1.5.2.
>> r5 = [1:2:6, -1:-2:-7]
r5 =
1 3 5 -1 -3 -5 -7
:
>> r5(3:6)
ans =
5 -1 -3 -5
:
>> r5( 1:2:7 )
ans =
1 5 -3 -7
: r5( 6:-2:1), ?

MATLAB/SIMULINK 21

1.5.3.
.
- (;) .
>> c = [1; 3; sqrt(5)]
c=
1.0000
3.0000
2.2361
>> c2 = [3
4
5]
c2 =
3
4
5
>> c3 = 2*c - 3*c
c3 =
-7.0000
-6.0000
-10.5279
( ).

1.5.4.
- -
. ( ' ).
>> w = [ 1 -2 3 ], c = [ 1; 3; sqrt(5)]
w=
1 -2 3
c=
1.0000
3.0000
2.2361
>> w', c
ans =
1
-2
3
ans =
1.0000 3.0000 2.2361

22 MATLAB/SIMULINK

>> t = w + 2*c'
t=
3.0000 4.0000 7.4721
>> T = 5*w' - 2*c
T=
3.0000
-16.0000
10.5279
>> T,t
T=
3.0000
-16.0000
10.5279
t=
3.0000 4.0000 7.4721
T t ,
MATLAB-

.
x , x
x.
>> x = [ 1+3i, 2-2i]
x=
1.0000 + 3.0000i 2.0000 2.0000i
>> x
ans =
1.0
3.0000i
.0000 + 2.0000i

1.5.5. (*)
(), . ,
,
. u v n - -

v [v1 , v2 ,...vn ]T

u [u1, u2 ,...un ]T

n

uT v [u1, u2 ,...un ] [v1, v2 ,...vn ]T ui vi u1v1 u2v2 ... unvn


1

MATLAB/SIMULINK 23

10
u 11
12

20
v 21 .
22

uT v 10 20 (11) (21) 12 (22) 167


MATLAB-
, :
>> u = [10; -11; 12 ], u, v = [20; -21; -22]
>> prod = u*v
u=
10
-11
12
u =
10 -11 12
v=
20
-21
-22
prod =
167
MATLAB- - . :
>> w = [2, 1, 3]
w=
2 1 3
u w,
MatLab- .
>> u*w
??? Error using ==> *
Inner matrix dimensions must agree.

u*w'
ans =
45
, - w .

24 MATLAB/SIMULINK

u .

ui

uT u

n . u [10, 11, 12]T


MATLAB- :
>> dol = sqrt(u*u)
dol =
19.1050

>> dol = norm(u)


dol =
19.1050

. MATLAB
.
:
- x y e :

cos

xT y
x y

x [1, 2,3]T

y [3, 2,1]T

.
MATLAB:
>> x=[1, 2, 3]';
>> y=[3, 2, 1]';
>> costheta=x'*y/(norm(x)*norm(y));
>> theta=acos(costheta);
>> thetad=theta*180/pi;
>> costheta, theta, thetad
( )
costheta =
0.7143
theta =
0.7752
thetad =
44.4153

( =theta)
( )
( )

MATLAB/SIMULINK 25

1.5.6. (.*)

.
MATLAB-. u v
( ),
( ) :

u v [u1v1, u2v2 ,....,unvn ]


.
.
- (.*).
>> u=[10; -11; 12];
>> v=[20; -21; -22];
>> u.*v, u'.*v'
ans =
( u.*v)
200
231
-264
ans =
( u.*v)
200 231 -264
: y x sin(x) za x=0.0:0.25:1.0.
-,
x .
>>x = 0.0:0:25:1.0; x = [0.0:0:25:1.0]; ( )
x=
0 0.2500 0.5000 0.7500 1.0000
>>
>> x = [0.0:0:25:1.0];
( )
x=
0
0.2500
0.5000
0.7500
1.0000
y
x spx= sin(x) :

26 MATLAB/SIMULINK

>> spx=sin(pi*x);
ans =
0
0.7071
1.0000
0.7071
0.0000


=x

spx= sin(x)

0.000
0.2500
0.5000
0.7500
1.0000

0.0000
0.7071
1.0000
0.7071
0.0000

= y x sin(x)
0.0000
0.1768
0.5000
0.5303
0.0000

MATLAB- :
>> y = x.*sin(pi*x)
y=
0
0.1768
0.5000
0.5303
0.0000


:
- x pi = 3.1495.
sin . -
r sin(pi*x)
x y,
.
, MATLAB :
>> x=[0.0:0.25:1.0];
>> spx=sin(pi*x);
>> y=x.*spx;
>> [x, spx, y]
ans =
x
0
0.2500
0.5000
0.7500
1.0000

% )
% sin(x) - )
% y
%

sin(x)

0
0.7071
1.0000
0.7071
0.0000

y x sin(x)
0
0.1768
0.5000
0.5303
0.0000

x sin(x)

MATLAB/SIMULINK 27

1.5.7. ( ) ( ./ )
.
MATLAB- ( ./ )
.
(),
.
>> a = 1:5, b = 6:10, a./b
a=
1 2 3 4 5
b=
6 7 8 9 10
ans =
0.1667 0.2857 0.3750 0.4444 0.5000
>> a./a
ans =
1 1

>> c = -2:2, a./c


c=
-2 -1 0 1

Warning: Divide by zero.


ans =
-0.5000 -2.0000
Inf 4.0000 2.5000
, c .
a c, ,
MATLAB- Inf, .
>> a.*b - 24 , ans./c
ans =
-18 -10 0 12 26
Warning: Divide by zero.
ans =
9 10 NaN 12 13
0/0, MATLAB- NaN, .
: :

lim
x 0

sin(x)
x

28 MATLAB/SIMULINK

sin(x)/x
x 0. .
>> x = [0.1; 0.01; 0.001; 0.0001]
x=
0.1000
0.0100
0.0010
0.0001

>> sin(pi*x)./x
ans =
3.0902
3.1411
3.1416
3.1416
. ,
ans.
, .
>> format long
>> ans - pi
ans =
-0.05142270984032
-0.00051674577696
-0.00000516771023
-0.00000005167713
, ,
8- .
( ./ ), () .
>> 1./x
ans =
10
100
1000
10000
1.5.8. ( .^ )
, u.*u.
( .^ ).
>> u = [10 11 12];

MATLAB/SIMULINK 29

>> u.^2
ans =
100 121 144
>> u.*u
ans =
100 121 144
>> u.^4
ans =
10000

14641

20736

>> v = [ 20; -21; -22]; w = [2, 1, 3];


>> v.^2
ans =
400
441
484
>> format, u.*w.^(-2)
ans =
2.5000 11.0000 1.3333
( .^ ), .

30 MATLAB/SIMULINK

1.6.

. . - ( ; )
. MATLAB .
( )
.
.
, 33

6 1 2
A 1 8 3 .
2 4 9
MATLAB,
.

>> A = [6 1 2; -1 8 3; 2 4 9]
:
>> =
6 1 2
-1 8 3
2 4 9
- ( ; )
() :
>> A = [ 6 1 2
-1 8 3
2 4 9]
:
>> =
6 1 2
-1 8 3
2 4 9

MATLAB/SIMULINK 31


( : ).
:
>> r3 = A(3,:)
:
>> r3 =
2 4 9
, (:,2) .

>> c2 = A(:,2)

>> c2 =
1
8
4
1.6.1.
() size.
, B x:
>> B=[-1 2 5;9 0 5]
>> x=[ 1;2;3];

>> size(B), size(x)
ans =
2 3
(B 2X3 2 3
ans =
3 1
(x e 3X1 3 1 )
>> size(ans)
ans =
1 2
B 2x3 x, 3x1, size
. ,
ans - size(ans) 1 2.
size
.

32 MATLAB/SIMULINK

>> [r,c]=size(B'), S = size(B')


r=
()
3
c=
()
2
S=
3
2
1.6.2.
. :
>> A=[5 11 0 4; -7 3 7 6; 0 5 9 2];
>> B=[3 0 8 5;
>> A+B
ans =
8 11 8
-3 4 10
12 5 12
>> A-B
ans =
2 11
-11 2
-12 5

4 1 3 -4; 12 0 3 4];

9
2
6

-8 -1
4 10
6 -2

1.6.3.
- . ,
: ..
.
>> D=[1:5; 6:10; 11:2:20];
>> D, D'
D=
1
2 3 4
5
6
7 8 9 10
11 13 15 17 19
ans =
1 6 11
2 7 13
3 8 15
4 9 17
5 10 19
>> size(D), size(D')

MATLAB/SIMULINK 33

ans =
3 5
ans =
5 3
1.6.4.
MATLAB
.
ones(m,n) mxn :
>> P=ones(2,3)
P=
1 1 1
1 1 1
zeros(m,n) mxn :
>> Z=zeros(2,3), zeros(size(P')
Z=
0 0 0
0 0 0
ans =
0 0
0 0
0 0

.

(nxn). ..
.
.
>> S = [ 2, -1, 0; -1, 2, -1; 0, -1, 2]
S=
2 -1 0
-1 2 -1
0 -1 2
>> St = S'
St =
2 -1 0
-1 2 -1
0 -1 2

34 MATLAB/SIMULINK

>> S-St
ans =
0 0
0 0
0 0

0
0
0

1.6.5.
nxn,
. MatLab-
eye(n).
>> I = eye(3), x = [8; -4; 1], I*x
I=
1 0 0
0 1 0
0 0 1
x=
8
-4
1
ans =
8
-4
1

.
1.6.6.

.
MATLAB- .
.
>> D = [-3 0 0; 0 4 0; 0 0 2]
D=
-3 0 0
0 4 0
0 0 2

(. 100x100). diag.
d
diag(d) .

MATLAB/SIMULINK 35

>> d = [-3 4 2], D=diag(d)


d=
-3 4 2
D=
-3 0 0
0 4 0
0 0 2
A , diag(A), .
>> F=[0 1 8 7; 3 -2 -4 2; 4 2 1 1]
F=
0 1 8 7
3 -2 -4 2
4 2 1 1
>> diag(F)
ans =
0
-2
1
.
1.6.7.
:
>> C = [ 0 1; 3 -2; 4 2]; x = [8; -4; 1];
>> G = [C x]
G=
0 1 8
3 -2 -4
4 2 1
>> A = [ 5 7 9; 1 -3 -7], B, H = [A;B]
A=
5 7 9
1 -3 -7
B=
-1 2 5
9 0 5
H=
5 7 9
1 -3 -7
-1 2 5
9 0 5

36 MATLAB/SIMULINK

C x
G, H
A B, A B
.
>> J= [1:4; 5:8; 9:12; 20 0 5 4]
J=
1 2 3 4
5 6 7 8
9 10 11 12
20 0 5 4
>> K = [diag(1:4)
K=
1 0 0 0
0 2 0 0
0 0 3 0
0 0 0 4
1 5 9 20
2 6 10 0
3 7 11 5
4 8 12 4

J; J' zeros(4,4)]
1 2 3
5 6 7
9 10 11
20 0 5
0 0 0
0 0 0
0 0 0
0 0 0

4
8
12
4
0
0
0
0

spy(K) -
K, - .
>> spy(K), grid
0
1
2
3
4
5
6
7
8
9

4
5
nz = 34

:
y=4sin(3x) u=3sin(4x) x =
0.0.1,0.2,....0.5

MATLAB/SIMULINK 37

>> x=0:0.1:0.5;
>> y=4*sin(3*x); u=3*sin(4*x);
>> [x',y',u']
ans =
0
0
0
0.1000 1.1821 1.1683
0.2000 2.2586 2.1521
0.3000 3.1333 2.7961
0.4000 3.7282 2.9987
0.5000 3.9900 2.7279
.
>> x=(0:0.1:0.5)';
>> [x 4*sin(3*x) 3*sin(4*x) ]
ans =
0
0
0
0.1000 1.1821 1.1683
0.2000 2.2586 2.1521
0.3000 3.1333 2.7961
0.4000 3.7282 2.9987
0.5000 3.9900 2.7279

1.6.8.
.. ,
.
. i- j-
Aij Matlab A(i,j). :
>> J = [1:4; 5:8;9:12; 20 0 5 4]
J=
1 2 3 4
5 6 7 8
9 10 11 12
20 0 5 4
>> J(1,1)
ans =
1
>> J(2,3)
ans =
7
>> J(4,5)
??? Index exceeds matrix dimensions.

38 MATLAB/SIMULINK

>> J(4,1) = J(1,1) + 6


J=
1 2 3 4
5 6 7 8
9 10 11 12
7 0 5 4
>> J(1,1) = J(1,1)-3*J(1,2)
J=
-5 2 3 4
5 6 7 8
9 10 11 12
7 0 5 4

.
, .
>> J(:,3)
ans =
3
7
11
5

% tretata kolona se izdvojuva

% kolonite 2 i 3 se izdvoeni

>> J(:,2:3)
ans =
2 3
6 7
10 11
0 5
>> J(4,:)
ans =
7 0

>> J(2:3,2:3)
ans =
6
7
10 11

1.6.9. ( .* )
.

().
>> A = [ 6 7 9; 1 -3 -7]

MATLAB/SIMULINK 39

A=
6 7
1 -3

9
-7

>> B = [ -1 2 5; 9 0 5]
B=
-1 2 5
9 0 5
>> A.*B
ans =
-6 14 45
9 0 -35
>> A.*B'
??? Error using ==> .*
Matrix dimensions must agree.

B' A.
1.6.10.
..

Ax
xA .
, Ax x A
x.
>> x = [8; -4; 1]
x=
8
-4
1
>> A
A=
6 7 9
1 -3 -7
>> A*x
ans =
29
13
>> x*A
??? Error using ==> *
Inner matrix dimensions must agree.
, Ax xA,
.

40 MATLAB/SIMULINK

1.6.11.
A mxn B
nxp, .. A*B, A
B, C
mxp.
>> A = [5 7 9; 1 -3 -7]
A=
5 7 9
1 -3 -7
>>B = [ 0, 1; 3, -2; 4, 2]
B=
0 1
3 -2
4 2
>> C = A*B
C=
57 9
-37 -7
>> D = B*
D=
1 -3 -7
13 27 41
22 22 22

, .. AB BA.
1.6.12. -
MATLAB :
A\ B A-1B
A/B AB-1
, m-file-. m-file-
2. .
1.6.1.
, Ax = B, x :

4 2 10 x1 10
2 10 12 x 32

4 6 16 x3 16

MATLAB/SIMULINK 41

:
% Primer 1.6.1
% file: mt_p16_01_linalgebra.m
%
A = [ 4 -2 -10; 2 10 -12; -4 -6 16];
B = [-10; 32; -16];
X = A\B
:
X=
2.0000
4.0000
1.0000
MATLAB
-.
(MATLAB Users Guide).
1.6.2.
inv A
1.6.1., x.
:
% Primer 1.6.2
% File: mt_p16_02_linalgebra.m
%
A = [ 4 -2 -10; 2 10 -12; -4 -6 16];
B = [-10; 32; -16];
C = inv(A)
X = C*B
:
C=
2.2000 2.3000 3.1000
0.4000 0.6000 0.7000
0.7000 0.8000 1.1000
X=
2
4
1
1.6.3.
lu
1.6.2. , A = LU.
1 1

x X U L B.

42 MATLAB/SIMULINK

(m-file):
% Primer 1.6.3
% File: mt_p16_03_linalgebra.m
%
A = [4 -2 -10; 2 10 -12; -4 -6 16];
B = [-10; 32; -16];
[L,U] = lu(A)
X = inv(U)*inv(L)*B
:
L=
1.0000
0
0
0.5000 1.0000
0
-1.0000 -0.7273 1.0000
U=
4.0000 -2.0000 -10.0000
0 11.0000 -7.0000
0
0 0.9091
X=
2
4
1

1.6.13. (Eigenvalues)
n x n ,

Ax x
.
eig(A)
.

MATLAB
[X,D]=eig(A)
D , X
:

AX XD .

MATLAB/SIMULINK 43

. 1.6.4.
:

1 1
0

A 6 11 6
6 11 5
M-file ( ):
% Primer 1.6.4
% File: mt_p16_04_eigenvalues.m
%
A = [0 1 -1; -6 -11 6; -6 -11 5];
[X,D] = eig(A)
:
X = ( )
0.7071 -0.2182 -0.0921
0.0000 -0.4364 -0.5523
0.7071 -0.8729 -0.8285
D = ( )
-1.0000
0
0
0 -2.0000
0
0
0 -3.0000
1.6.14.
:
1.6.5.

x y z t 4
2x y t 2
3x y z t 2
x 2 y 3 z t 3
. Ax = B,

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

3 1 z 1 z 2


1 2 3 1 t 3
1
x A B .

44 MATLAB/SIMULINK

.
:
% Primer 1.6.5
% File: mt_p16_05_linalgebra.m
%
A = [ 1 1 1 1; 2 -1 0 1; 3 2 -1 -1; 1 -2 -3 1];
B = [4; 2; 2; -3];
X = A\B
: x [ x y z t ]T :
X=
0.8437 (x)
0.8750 (y)
1.0937 (z)
1.1875 (t)
:
1.6.1.

2x 3y z t 1
x y z t 1
3 x y z 2t 0
x z t 2
1.6.2.

x 2y z t 3
xz t
2y z t
x 4 y 2z t 1
1.6.3.

x 2y w 0
3x y 4 z 2w 3
2 x 3 y z 5w 1
x 2 z 2w 1

MATLAB/SIMULINK 45

1.7.
2D, 3D, , , , , ,
, .
2-D : plot, loglog, semilogx, semilogy, polar, i
bar.
grid , title(tekst),
xlabel(tekst), ylabel(tekst) text(tekst)
.
(.,+,*,o,x)
(r,b,g,w). :
plot(t, y1,r , t, y2, +b)
, + .
.

axis([ x min. x max. y min. y max.])


. :
axis([-10 40 -60 60])
x -10 +40, y - 60 60.
axis .
.
.
>> title(' Graph of y = sin(3pi x)')
>> xlabel('x axis')
>> ylabel('y-axis')
, ( ' ' )
. - .
. a
.
:
>> legend('kriva 1', 'kriva 2')
.
. grid .
grid grid off.

46 MATLAB/SIMULINK

plot .
:
plot(x,y) y x.
plot(y) y
If y , plot(y) plot(real(y),imag(y)).
plot, .
,
:
plot(x,y,s) s
:
b
g
r
c
m
y
k

blue -
green -
red
cyan -
magenta -
yellow -
black

.
o
x
+
*
s
d
v
^
<
>
p
h

point ()
circle ()
x-mark ( )
plus ()
star ()
square ()
diamond ()
triangle (down)
( )
triangle (up)
( )
triangle (left)
( )
triangle (right)
( )
pentagram ()
hexagram ()

:
-.
--

solid ( )
dotted ( )
dashdot (-)
dashed ( .)

, plot(x,y,'c+:')
() ; plot(x,y,'bd')
, .
plot(x1,y1,s1,x2,y2,s2,x3,y3,s3,...) (x,y,s), x and y , s .
, plot(x,y,'y-',x,y,'go') ,
.
plot, ,
default. Default
. ,
.

MATLAB/SIMULINK 47

plot (semilogx, semilogy, loglog, polar


bar, ) ,
help naredba ( help loglog).
plot3 3-D . plot3() plot().
plot3(x,y,z), x, y z ,
3-D x, y and z.
plot3(x,y,z), X, Y and Z ,
X, Y Z.
, plot
plot3(x,y,z,s) s 1, 2 or 3
plot .
plot3(x1,y1,z1,s1,x2,y2,z2,s2,x3,y3,z3,s3,...)
(x,y,z,s) , x , y z s
.
plot3: ():
>> t=0:pi/50:10*pi;
>> plot3(sin(t),cos(t),t), grid;
.1.7.1. .

.1.7.1.

.

48 MATLAB/SIMULINK

1.7.1.
MATLAB-

.

y sin(3x)

0 x 1 . x
0 1
y. (x,y), x-y
.
N+1 h .
>>N = 10; h= 1/N; x = 0:h:1;
x=0, 2h, 3h,...,1-h,1.
y :
>> y=sin(3*pi*x);
:
>>plot(x,y)
1

0.8

0.8

0.6

0.6

0.4

0.4

0.2

0.2

-0.2

-0.2

-0.4

-0.4

-0.6

-0.6

-0.8

-0.8

-1

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

-1

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

.1.7.2: ) y sin(3x) h = 0.1,


)

y sin(3x) h = 0.01

. 1 ),
N . N 100,
.
>> N = 100; h=1/N; x = 0:h:1;
>> y = sin(3*pi*x); plot(x,y)
.1.).

MATLAB/SIMULINK 49

:
1.7.1.
X-Y :
t

10

11

12

.5

11

14

15.5

16

16

16

16

, . M-File:
% Primer 1.7.1
% File: mt_p17_01_lingraf.m
%
t = [0 1 2 3 4 5 6 7 8 9 10 11 12];
y = [0 0.5 1 2 4 7 11 14 15.5 16 16 16 16];
plot(t,y), grid

. 1.7.3. ( 1.7.1)

.1.7.4. ( 1.7.2)

1.7.2.
y

sin x
4 x 4 .
x

( - title), (xlabel, ylabel) (grid).


% Primer 1.7.2. Sinusna funkcija
% File:mt_p17_02_sin_funkcija.m
%
x = -4*pi: 0.05: 4*pi;
y = sin(x)./x;
plot(x,y)
title('Primer 1.7.2 Sinusna funkcija');
xlabel('x [rad]');
ylabel('y=sinx/x');
grid

50 MATLAB/SIMULINK

1.7.2.
,
>> plot(x,y,'w-',x,cos(2*pi*x),'g--')

>> legend('Sin kriva','Cos kriva')

. MATLAB-
(.1.7.5).
1.7.3.
% Primer 1.7.3. dve krivi na eden graf
% File:mt_p17_03_dve_krivi.m
x=0:0.01:1;
plot(x,sin(3*pi*x),'b-',x,cos(3*pi*x),'g--')
legend('Sin kriva','Cos kriva')
title(' Multi-plot')
xlabel('x axis'), ylabel('y axis')
grid
Multi-plot
1
Sin kriva
Cos kriva

0.8
0.6
0.4

y axis

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

0.1

0.2

. 1.7.5:

0.3

0.4

0.5
x axis

0.6

0.7

0.8

0.9

y sin(3x) y cos(3x) h = 0.01

HOLD
plot,
.
. ,
hold.
>> plot(x,y), hold
>> plot(x,y,'gx'), hold off
hold , hold off ,
. clg.

MATLAB/SIMULINK 51

1.7.3.
, ..
x y.
>> clf, N=100; h=1/100; x=0:h:1;
>> y=sin(3*pi*x); plot(x,y), grid, axis([-0.5 1.5 -1.2 1.2])

.1.7.6:
axis .
x-
y-. .
.1.7.6.
:
>> x = -1:0.1:1;
>> plot(x,sin(pi*x),'g-')
>> hold on
>> plot(x,cos(pi*x),'r-')
1.7.4. (zoom)
()
.
>>zoom
(pointer) ,

.
.
.
,

.

52 MATLAB/SIMULINK

1.7.5. (SUBPLOT)
subplot(m,n,p), or subplot(mnp),
m--n ( ), p
. ,
, m=2, n=2, p= 1,2,3,4:
subplot(2,2,1), plot(x,y,s),
subplot(2,2,2), plot(x,y,s),
subplot(2,2,3), plot(x,y,s),
subplot(2,2,4), plot(x,y,s),
(window), (mxn),
.
1.7.4. subplot.
% Primer 1.7.4. Subplot
% File:mt_p17_04_subplot.m
x=0:0.01:1;
subplot(221), plot(x,sin(3*pi*x)),grid
xlabel('x'), ylabel('sin 3 pi x')
subplot(222), plot(x,cos(3*pi*x)),grid
xlabel('x'), ylabel('cos 3 pi x')
subplot(223), plot(x,sin(6*pi*x)),grid
xlabel('x'), ylabel('sin 6 pi x')
subplot(224), plot(x,cos(6*pi*x)),grid
xlabel('x'), ylabel('cos 6 pi x')

.1.7.7.
subplot (22p) (subplot) 2x2, a o
subplot (221) (1) , subplot (222),
. .

MATLAB/SIMULINK 53

1.7.5.
()
t 0 3 0.0005.
:
1) v 120 sin t i 100 sin(t
.
2) p v i
.

) t .

t .

3) :

f a Fm sin t , f b Fm sin(t

2
4
), f c Fm sin(t ) , Fm 3.0
3
3

t . .

4) fR 3Fm .
.

% Primer 1.7.5. Slika so povece grafovi


% File: mt_p17.05_slika_so_4_grafa.m
%
% CRTANJE CETIRI GRAFA NA EDNA SLIKA
% Primerot ima za cel samo da go demonstrira nacinot na crtanje
% cetiri grafici (dijagrami) na edna slika
%
clg
%
wt = 0: 0.05: 3*pi;
% Agol fi=wt=w*t [rad]
v=120*sin(wt);
% Sinusoidalen napon [V]
i=100*sin(wt-pi/4);
% Sinusoidalna struja [A]
p=v.*i;
% Momentna snaga (mok') [W]
%
Fm=3.0;
fa = Fm*sin(wt);
% Tri fazi: fa, fb, fc
fb = Fm*sin(wt-2*pi/3);
fc = Fm*sin(wt-4*pi/3);
%
% Graf "gore - levo" na slikata:
subplot(221),plot(wt,v, wt,i),grid % Napon (v) i struja (i) vo f-cija od wt
title('Napon i struja')
xlabel('wt - agol [rad]');
%

54 MATLAB/SIMULINK

% Graf "gore - desno" na slikata:


subplot(222),plot(wt,p), grid % Momentna snaga (mok') vo f-cija od wt
title('Snaga (mok)')
xlabel('wt [rad]');
ylabel('Snaga (Wati) [W]');
%
% Graf "dolu - levo" na slikata:
subplot(223),plot(wt,fa,wt,fb,wt,fc),grid % Crtanje na trite fazi
title('Tri fazi');
xlabel('wt [rad]');
ylabel('Amplitudi');
%
% Graf "dolu - desno) na slikata (Graf vo polarni koordinati)
fR= 3/2*Fm*(wt+1)./(wt+1);
% Vrtenje - rotacija
subplot(224),polar(wt,fR), grid % Crtanje graf vo polarni koordinati
title('Polaren graf')
%
% x = 3*sin(wt)
% y = 3*cos(wt)
% fR = sqrt(x.^2+y.^2)
% subplot(224),polar(wt,fR), grid % Crtanje graf vo polarni koordinati
% title('Polaren graf')

Sl. 1.7.8.

MATLAB/SIMULINK 55

1.7.6.
f(x,y)
x y, .
z=f(x,y),
(x,y,z). , x y,
- Karteziev () .
x y
(x,y) x y,
(. 2 < x < 4 1 < y < 3).
. (x,y)

.
MatLab- meshgrid
x y .
>> x = 2:0.5:4; y=1:0.5:3;
>> [X,Y] = meshgrid(x,y);
>> X
>> Y
X=
2.0000 2.5000 3.0000
2.0000 2.5000 3.0000
2.0000 2.5000 3.0000
2.0000 2.5000 3.0000
2.0000 2.5000 3.0000
Y=
1.0000 1.0000 1.0000
1.5000 1.5000 1.5000
2.0000 2.0000 2.0000
2.5000 2.5000 2.5000
3.0000 3.0000 3.0000

3.5000
3.5000
3.5000
3.5000
3.5000

4.0000
4.0000
4.0000
4.0000
4.0000

1.0000
1.5000
2.0000
2.5000
3.0000

1.0000
1.5000
2.0000
2.5000
3.0000

X Y,
(X(i,j), Y(i,j)) .. X Y
(x,y) f(x,y) . MATLAB-
X Y f.
1.7.6:

f ( x, y) ( x 3) 2 ( y 2) 2 , 2 < x < 4 1 < y < 3.


% Primer 1.7.6. Graf na funkcija
% File mt_p17_06_Sedlo.m
%
[X,Y] = meshgrid(2:0.2:4, 1:0.2:3);

56 MATLAB/SIMULINK

Z = (X-3).^2- (Y-2).^2;
mesh(X,Y,Z)
title(' Sedlo '), xlabel('x'),ylabel('y')
Saddle

0.5

-0.5

-1
3
2.5

4
3.5

1.5
y

2.5
1

.1.7.9.
MatLab-
mesh(X,Y,Z) .
2 ( x 2 y 2 )

1.7.7: a) f xye
-2 < x < 2, -2<y<2.
b)
.
% Primer 1.7.7. ) Graf na funkcija 3D
% File mt_p17_07__graf_na_funkcija_3D.m
[X,Y] = meshgrid(-2:0.1:2, -2:0.1:2);
f = -X.*Y.*exp(-2*(X.^2+Y.^2));
mesh(X,Y,f)
title('Funkcija f=-x*y*exp(-2*(x^2+y^2))'), xlabel('x'),ylabel('y')

. 1.7.10.

MATLAB/SIMULINK 57


m-file.
% Primer 1.7.7.b) Ekstremi na funkcija
% File mt_p17_07_b_graf_na_funkcija_3D.m
%
[X,Y] = meshgrid(-2:0.1:2, -2:0.1:2);
f = -X.*Y.*exp(-2*(X.^2+Y.^2));
fmax = max(max(f))
kmax = find(f==fmax)
Pos = [X(kmax), Y(kmax)]
contour(X,Y,f)
xlabel('x'), ylabel('y'), grid, hold on
plot(X(kmax),Y(kmax),'*')
text(X(kmax),Y(kmax),'Maximum')
fmax =
0.0920
kmax =
641
1041
Pos =
-0.5000 0.5000
0.5000 -0.5000
2
1.5
1

0.5

Maximum

0
-0.5

Maximum

-1
-1.5
-2
-2

-1.5

-1

-0.5

0
x

.1.7.11.

0.5

1.5

58 MATLAB/SIMULINK

1.7.8.
Bessel-

Jo x2 y2 .
% Primer 1.7.8. Graf na Bessel-ovata funkcija J0(sqrt(x^2+y^2))
% File mt_p17_08_Meshgrid.m
%
clg
[x,y]=meshgrid(-12:.5:12, -12:.5:12);
% meshgrid ja transformira specificiranata domena
% vo matrici x i y za ocenuvanje na z
%
r=sqrt(x.^2+y.^2);
z= bessel(0,r);
m=[-45 60];
%
% 3-D mesh plot
mesh(z,m),grid

Sl. 1.7.12. ( 1.7.8)


1.7.9
z
-10 10 0.5

sin r
, r x 2 y 2 , x y
r

% 1.7.9. Graf na funkcijata z=sinr/r, r=sqrt(x^2+y^2)


% M-file: mt_p17_09_Meshgrid2.m:
%

MATLAB/SIMULINK 59

clg
[x,y]=meshgrid(-10:.5:10);
% meshgrid ja transfora specificiranata domena
% vo matrici x i y za ocenuvanje na z
%
r=sqrt(x.^2+y.^2)+eps;
z= sin(r)./r;
%
% 3-D mesh plot
mesh(x,y,z)

a) [x,y]=meshgrid(-10:.5:10);

b) [x,y]=meshgrid(-20:.5:20);
. 1.7.13. (a 1.7.9)

60 MATLAB/SIMULINK

MATLAB/SIMULINK 61

2.
2.1.
,
. ,
.
, .
.
. ,
.
MATLAB- . .
2.1.1: a sin(nx) 1 < x
< 1, n = 1,2,3,...8.
8
. :
% Primer 2.1.01.for - end ciklus
% File: mt_p21_01_for.m
%
x = -1:0.05:1;
for n = 1:8
subplot(4,2,n), plot(x,sin(n*pi*x))
end
1

-1
-1
1

-1
-1
1

-1
-1
1

-1
-1
1

-0.5

-0.5

0.5

0.5

0
-1
-1
1

-0.5

0.5

-0.5

0.5

-0.5

0.5

-0.5

0.5

-1
-1
1

-1
-1

-1
-1

-0.5

-0.5

0.5

0.5

.2.1.1. sin(nx)
1 < x < 1, n = 1,2,3,...8.

62 MATLAB/SIMULINK

for end n
1 2,3 8. subplot 4x2
n- , n-
.

% Primer 2.1.02.for - end ciklus
% File: mt_p21_02_for.m
%
x=-1: 0.05: 1;
for n=1:2:8
subplot(4,2,n),plot(x,sin(n*pi*x))
subplot(4,2,n+1), plot(x,cos(n*pi*x))
end
sin(nx) i cos(nx), za n =

-1
-1
1

-0.5

0.5

0
-1
-1
1

-0.5

0.5

0.5

-1
-1
1

-0.5

0.5

-0.5

0.5

-0.5

0.5

-0.5

0.5

0
-1
-1

-0.5

0
-1
-1
1

-1
-1
1

1,3,5,7

-1
-1
1
0

-0.5

0.5

-1
-1

.2.1.2.
( n),

.
for .

MATLAB/SIMULINK 63

>> for brojac = [23 11 19 5.4 6]


..........................
>> end
5 brojac
.
2.1.03: 0 1,
. f1 = 0,
f2 = 1

f n f n1 f n2 za n = 3,4,5.....
f n / f n 1

( 5 1) / 2 1.618
>>
>>
>>
>>
>>
>>
>>
>>

F(1) = 0; F(2) = 1;
for i=3:20
F(i) = F(i-1)+F(i-2);
end
plot(2:19,F(3:20)./F(2:19),'o')
hold on
plot(2:19,F(3:20)./F(2:19),'-')
plot([0 20],((sqrt(5)+1)/2)*[1,1])

2
1.9
1.8
1.7
1.6
1.5
1.4
1.3
1.2
1.1
1

10

. 2.1.3.

12

14

16

18

20

64 MATLAB/SIMULINK

2.1.4: .

1
1
1
1
2 2 ..... 2
2
2
3
4
20
1
1
1
1
1
S 21 1 2 2 2 ..... 2 2
2
3
4
20
21
S 20 1

...

S100

1
1
1
1
1
1
1 2 2 2 ..... 2 2 ......
2
3
4
20
21
100 2

81 . S20
sum(1./(1:20).^2). ( sum -).
MatLab .
>> S= zeros(100,1);
>> S(20) = sum(1./(1:20).^2);
>> for n =21:100
>> S(n) = S(n-1) +1/n^2;
>> end
>> clf; plot([20:100],S(20:100),'.',[20
100],[1,1]*pi^2/6,'-')
>> axis([20 100 1.5 1.7])
>> [(98:100)' S(98:100)]
ans =
98.0000
1.6348
99.0000
1.6349
100.0
1.6350
1.7
1.68
1.66
1.64
1.62
1.6
1.58
1.56
1.54
1.52
1.5
20

30

40

50

60

70

.2.1.5.

80

90

100

%
%
%
%
%

1
2
3
4
5

%
%
%

6
7
8

MATLAB/SIMULINK 65


. - S 100
0. S20,
sum. ,
for , ,
1/n^2 i
Sn.
. S98, S99 S100.

2.2. (T)
MATLAB- .

MATLAB- (true) 1, (false) 0.
true = 1, false = 0
x
, :
x 2 ?
x 2?
x 2?
x 2?
x 2?
x 2?

x == 2
x ~= 2
x>2
x<2
x >= 2
x <= 2

1 0
.
>> x = pi
x=
3.1416
>> x ~= 3, x ~= pi
ans =
1
ans =
0
x , .
>> x = [ -2
x =
-2.0000
-1.0000
>> x == 0

pi

5; -1

3.1416
0

1 ]

5.0000
1.0000

66 MATLAB/SIMULINK

ans =
0
0

0
1

0
0

x .
x,
0 (2,2), 1
.. 1.

2.3.
-
.
&
|
~
xor
any
all

(AND)
(OR)
(NOT)
XOR

A
1
1
0
0

A
1
1
0
0

B
1
0
1
0

A&B
1
0
0
0

A
1
1
0
0

B
1
0
1
0

A|B
1
1
1
0

A
1
0

~A
0
1

B
1
0
1
0

xor(A,B)
0
1
1
0

MATLAB/SIMULINK 67

MATLAB- .
>> a=[1 1 0 0], b=[1 0 1 0]
>> a&b,a|b, xor(a,b)
a =
1
1
0
0
b =
1
0
1
0
ans =
1
0
0
0
ans =
1
1
1
0
ans =
0
1
1
0
any(x), all(x)
ans =
1
ans =
1

x , any all
-
x. any(x) -
, x
. all(x), -
, x
.

, .
>> x > 3 & x < 4
ans =
0 1 0
0 0 0
, .
>> y = x>3, z = x<4, y&z
y =
0
1
1
0
0
0
z =
1
1
0
1
1
1
ans =

68 MATLAB/SIMULINK

0
0

1
0

0
0


.
x>3 y
x 1,
y . x< 4,
. y i z,
AND (&), ,
.
ans.
>> x = [-2
x =
-2.0000
-5.0000

pi

5; -5

3.1416
-3.0000

-3

-1]

5.0000
-1.0000

>> x > 3 | x == -3 | x <= -5


ans =
0
1
1
1
1
0

() .
>> x, L = x >= 0
x =
-2.0000
3.1416
-5.0000
-3.0000
L =
0
1
1
0
0
0
>> pos = x.*L
pos =
0
3.1416
0
0

5.0000
-1.0000

5.0000
0

pos 0 ..
.
>> x = 0:0.05:6; y = sin(pi*x); Y= (y >=0).*y;
>> plot(x,y,':',x,Y,'-')

MATLAB/SIMULINK 69

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

.2.1.6.

2.4. While

, for ,

. while .... end
.
2.4.1: n

S 12 2 2 ..... n 2
100.
>>
>>
>>
>>
>>
>>

S =1; n = 1;
while S+ (n+1)^2 < 100
n = n+1;
S = S + n^2;
end
[n,S]

ans =
6

91

while end
S+ (n+1)^2 < 100 ().

70 MATLAB/SIMULINK

: 100, 10
. n=2 S=5.
2.4.2.: x = cos(x).
.
x = /4,

n=2,3,4 ....
xn cos( xn1 )
| xn xn-1 | , xn
.
1:
>> x = zeros(1,20) ; x(1) = pi/4;
>> n = 1; d = 1;
>> while abs(d) > 0.001
>> n = n+1; x(n) = cos(x(n-1));
>> d = x(n)-x(n-1);
>> end
>> n,x
n=
14
x=
Columns 1 through 7
0.7854 0.7071 0.7602 0.7247 0.7487 0.7326 0.7435
Columns 8 through 14
0.7361 0.7411 0.7377 0.7400 0.7385 0.7395 0.7388
Columns 15 through 20
0
0
0
0
0
0
. x
.

. 20
14- . d<=0.001,
, .
.
, .
2.
>> xold = pi/4; n = 1; d = 1;
>> while abs(d) > 0.001 & n < 20
>> n = n+1; xnew = cos(xold);

MATLAB/SIMULINK 71

>> d = xnew-xold;
>> xold = xnew;
>> end
>> [n, xnew, abs(d)]
ans =
14.0000 0.7388 0.0007
, .
0.001 n < 20.
.
while :
while

end

2.5. if ..... then .... else .... end


,
,
.

. MATLAB
.
if ..... then .... else .... end

>>
>>
>>
>>

e :

a = pi^exp(1); c = exp(pi);
if a >= c
b = a^2 - c^2
end

b, a =
:

<c=

e .

>> if a >= c
>> b = sqrt(a^2 - c^2)
>> else
>> b = 0
>> end
b =
0
b a<c.

72 MATLAB/SIMULINK

if.... :
if _1

_1
elseif _2

_2
.
.
.
else _

_
end

2.6. ( m-)
, MATLAB .
.
, .
, MATLAB ,

. . ,
, , .
.
MATLAB,
MATLAB .
(
), MATLAB
(), m-, m, MATLAB ,
. - .
MATLAB m-: () m-
m-.

2.6.1. () m-
m-
m-.

MATLAB/SIMULINK 73

2.6.1: m- a
.
a,b c
:

A s(s a)(s b)(s b)

abc
2

MATLAB :
>> a=10;b=15;c=20;
>> s=(a+b+c)/2;
>> A=sqrt(s*(s-a)*(s-b)*(s-c))
A =
72.6184

, ,
.
.
m-,
.
m- mt_p26_01_area.m
work.
mt_p26_01_area.m
s=(a+b+c)/2;
A=sqrt(s*(s-a)*(s-b)*(s-c))

.
>> a=10;b=15;c=20;
>> mt_p26_1_area
A =
72.6184
>> a=3;b=4;c=5;
>> mt_p26_1_area
A =
6

74 MATLAB/SIMULINK

MATLAB
m-. , mt_p26_1_area.m
mt_p26_1_area.m
. .
,
. m-
.
a, b c
. ,
a ,
;.
A (),
;.
, m-,

.
, .
.
2.6.02 - file
File mt_p26_02_crta.m
plot(x,y,'w-')
legend(' matematicka kriva')
title(' Crtez na fukcija ')
xlabel('x-oska'), ylabel('y-oska')
grid

>> x=-pi:pi/20:pi;
>> y=sin(x);
>> mt_p26_02_crta
Crtez na fukcija
1
matematicka kriva
0.8
0.6
0.4

y-oska

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

-3

-2

-1

0
x-oska

Sl. 2.6.1.

MATLAB/SIMULINK 75


>> x=-1:2/100:1;
>> y=x.^2;
>> mt_p26_02_crta
Crtez na fukcija
1
matematicka kriva
0.9
0.8
0.7

y-oska

0.6
0.5
0.4
0.3
0.2
0.1
0
-1

-0.8

-0.6

-0.4

-0.2

0
x-oska

0.2

0.4

0.6

0.8

x y
,
m-.
2.6.2. m-
m- (,
.


.

. ,
mt_p26_01_area.m, .
function [A] = povr(a,b,c)
% presmetuva povrsina na triagolnik cii
% strani imaat dolzina a, b i c.
% Vlez:
%
a, b, c : Dolzini na strani
% Izlez
%
A: Povrsina na triagolnikot

76 MATLAB/SIMULINK

% Koristenje:
%
Area = povr(2,3,4)
%-----------------------------------------------s = (a+b+c)/2;
A=sqrt(s*(s-a)*(s-b)*(s-c));
%----------- kraj na funkcijata povr ----------
m-,
povr. povr.m
E:\mat-vezb, .
>>
>>
>>
>>

povr(3,4,5)
Area = povr(3,4,5)
x=10;y=15;z=20;
A=povr(x,y,z)

ans =
6
Area =
6
A =
72.6184

.
3,4 5

ans.
Area. x, y z
povr
x, y z A.
A = povr(x,y,z)

, j
.

. povr.m
s. s,
.
>> s
??? Undefined function or variable 's'.
s i.
s (
),

MATLAB/SIMULINK 77

.
.
function [A,s] = povr(a,b,c)
:
1. .
>> povr(10,15,20)
ans =
72.6184
( )
ans.
2.
>> Area = povr(10,15,20)
Area =
72.6184
.
3.
>> [Area, hlen] = povr(10,15,20)
Area =
72.6184
hlen =
22.5000
,
.

1.


MATLAB.
povr () povr.m.

2.


function [ ] =
__ [ ]
:
function [A,s] = povr(a,b,c)

78 MATLAB/SIMULINK

3.

..
. %, MATLAB
%
.

4.

.

,
.


, MATLAB- .

>> help povr
presmetuva povrsina na triagolnik cii
strani imaat dolzina a, b i c.
Vlez:
a, b, c : Dolzini na strani
Izlez
A: Povrsina na triagolnikot
Koristenje:
Area = povr(2,3,4)
-----------------------------------------------
.

2.6.3.

,

f1 0, f 2 1, f n f n1 f n2

n=3,4,5.....

n i fn.
1: Fajl E:\mat-prog\Fib1.m ( !)
function f = Fib1(n)
% premetuva n-tiot broj vo
%nizata na Fibonaci
F = zeros(1,n+1);
F(2) = 1;
for i = 3:n+1

MATLAB/SIMULINK 79

F(i) = F(i-1) + F(i-2);


end
f = F(n);
%--------------- kraj-------------- 2: Fajl E:\mat-prog\Fib2.m

n .
.
function f = Fib2(n)
% premetuva n-tiot broj vo
%nizata na Fibonaci
if n== 1
f=0;
elseif n==2
f=1;
else
f1 = 0; f2 = 1;
for i = 2:n-1
f = f1 + f2;
f1 = f2; f2 = f;
end
end
%--------------- kraj-------------- 3: Fajl E:\mat-prog\Fib3.m
MatLab-
.. .
function f = Fib3(n)
% premetuva n-tiot broj vo
% nizata na Fibonaci
if n== 1
f=0;
elseif n==2
f=1;
else
f = Fib3(n-1) + Fib3(n-2)
end
%--------------- kraj-------------- 4: Fajl

E:\mat-prog\Fib4.m

80 MATLAB/SIMULINK

.. .

function f = Fib4(n)
% premetuva n-tiot broj vo
% nizata na Fibonaci
A = [0 1;1 1];
y = A^n*[1;0];
f = y(1);
%--------------- kraj-------------- .
>>
>>
>>
>>

x1
x2
x3
x4

=
=
=
=

Fib1(10)
Fib2(10)
Fib3(10)
Fib4(10)

x1 =
34
x2 =
34
x3 =
34
x4 =
34

.
tic toc, .
>>
>>
>>
>>

tic,x1
tic,x2
tic,x3
tic,x4

=
=
=
=

Fib1(20),toc
Fib2(20),toc
Fib3(20),toc
Fib4(20),toc

x1 =
4181
elapsed_time = 0.1700 sec
x2 =
4181
elapsed_time = 0 sec
x3 =
4181
elapsed_time = 31.8000 sec
x4 =
4181
elapsed_time = 0 sec

MATLAB/SIMULINK 81

2.7.
2.7.1

.
>> x=[ -3.14 0
x =
-3.1400
0
ans =
-3
0

3.14

6.45

10

>> fix(x)
ans =
-3

>> floor(x)
ans =
-4
0

>> ceil(x)
ans =
-3
0

10

3.1400

>> sign(x), rem(x,3)


ans =
-1
0
1
ans =
-0.1400
0

9.75], round(x)
6.4500

0.1400

9.7500

0.4500

0.7500

round ..
0.5 ,
.
fix
.
floor
.
ceil
.
sign .
rem(x,k) x k .

82 MATLAB/SIMULINK

2.7.2 sum
sum
,
.
>>A = [1:3; 4:6; 7:9]
A=
1 2 3
4 5 6
7
8
9
>> s = sum(A), ss=sum(sum(A))
s=
12 15 18
ss =
45
>> x = pi/4*(1:3)'
>> A=[sin(x), sin(2*x), sin(3*x)]/sqrt(2)
x=
0.7854
1.5708
2.3562
A=
0.5000 0.7071 0.5000
0.7071 0.0000 -0.7071
0.5000
-0.7071
0.5000
>> s1 = sum(A.^2), s2 = sum(sum(A.^2))
s1 =
1.0000 1.0000 1.0000
s2 =
3.0000
A 1,
A 3.
>> A*A'
ans =
1.0000
0
0
0 1.0000 0.0000
0

0.0000

1.0000

AA' A'A .
>> S = eye(3)

MATLAB/SIMULINK 83

S =
1
0
0

0
1
0

0
0
1

>> A*A' - S
ans =
1.0e-015 *
-0.2220
0
0
-0.2220
0
0.0555

0
0.0555
-0.2220

>> A'*A - S
ans =
1.0e-015 *
-0.2220
0
0
-0.2220
0
0.0555

0
0.0555
-0.2220

2.7.3 max min


sum.
x max(x) x,
min(x) .
>> x = [1.3 -2.4
x=
1.3000 -2.4000
ans =
2.3000
ans =
2.4000

2.3],

max(x),

max(abs(x))

0 2.3000

>> [m,j] = max(x)


m=
2.3000
j=
4
,

. .

84 MATLAB/SIMULINK

, max (A), -
( ) .
A, max(max(A)).
>> max(max(A)), min(min(A))
ans =
0.7071
ans =
-0.7071

2.7.4
rand(n,m)
mxn, 0 1. rand
0 1.
>> y = rand, Y=rand(2,3)
y=
0.9501
Y=
0.2311 0.4860 0.7621
0.6068
0.8913

0.4565

.
>> y = rand, Y=rand(2,3)
y=
0.0185
Y=
0.8214 0.6154 0.9218
0.4447
0.7919
0.7382
: m- n
.
( )

1 6. rand 0 1, 6,
0 6 .

floor(1 + 6*rand)
file: E:\mat-prog\kocka.m
function [d] = kocka(n)
% simulira n frlawa na par kocki
% Vlez:
n, broj na frlawa

MATLAB/SIMULINK 85

% Izlez: nx2 matrica, kade sekoj red se odnesuva na edno frlanje


%
a kolonite gi oznacuvaat prvata i vtorata kocka.
%
% Upotreba: T = kocka(3)
d = floor(1 + 6*rand(n,2));
%------------------- kraj------------------------------------->> kocka(3)
ans =
2
6
3
3
6
6

2, 3 6 6, 3 6.


3.5,
. .
>> sum(kocka(100))/100
ans =
3.5600
3.3100

2.7.5 find
find ()
x, .
>> x = -1:0.05:1;
>> y = sin(3*pi*x).*exp(-x.^2); plot(x,y,':')
>> k = find(y > 0.2)
k=
Columns 1 through 12
9 10 11 12 13 22 23 24 25 26 27 36
Columns 13 through 15
37 38 39

86 MATLAB/SIMULINK

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

-0.8

-0.6

-0.4

-0.2

0.2

0.4

0.6

0.8

.2.7.1
>> hold on, plot(x(k),y(k),'o')
>> km = find(x>0.5 & y < 0)
km =
32 33 34

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

-0.8

-0.6

-0.4

-0.2

0.2

.2.7.2.

0.4

0.6

0.8

MATLAB/SIMULINK 87

2.7.6. find
find .
>> A = [ -2
A=
-2 3 4
0 5 -1
6

4;

4
6
8

-1

6;

1]

>> k=find( A== 0)


k=
2
9
A 2 9.
find
() A -
.

1
2
3

4
5
6

7
8
9

10
11
12

>> n = find(A <= 0)


n=
1
2
8
9
>> A(n)
ans =
-2
0
-1
0
n
0 A(n) .

88 MATLAB/SIMULINK

2.8.
MatLab-
. .




2.8.1 MATLAB
MatLabo-
-. m-
MATLAB .
:

f ( x)

1
1

6
2
( x 0.3) 0.01 ( x 0.9) 2 0.04

.
m- humps(x).
file e:\mat-vezbi\humps.m
function y=humps(x)
y = 1./((x-0.3).^2 + 0.01) + 1./((x-0.9).^2 + 0.04) 6;
2.8.2
fplot
x .
100

80

60

40

20

-20
-5

-4

-3

-2

-1

. 2.8.1

MATLAB/SIMULINK 89

.2.8.1. :
>> fplot('humps',[-5
>> grid on

5])

.
>> fplot(' 2*sin(x+3)', [-1 1])

2
1.5
1
0.5
0
-0.5
-1
-1.5
-2
-1

-0.8

-0.6

-0.4

-0.2

0.2

0.4

0.6

0.8

.2.8.2.

>> fplot('[2*sin(x+3), humps(x)]', [-5 5])
100

80

60

40

20

-20
-5

-4

-3

-2

-1

.2.8.3.

90 MATLAB/SIMULINK

2.8.3
x,
fmin.
>> x = fmin('humps',0.3,1)
x=
0.6370
0.3 1
.
2.8.4
fzero
.

.
>> a = fzero('humps', -0.2)
a=
-0.1316
a 0.2. fzero
.
. .
fzero
:
>> a = fzero('humps', -0.3, 0.0)
a=
-0.1316
.

2.8.5 ( )


. MatLab-

quad
Simpsonova
quad8 Wutn-Kouts
humps 0 1
.
>> q = quad('humps',0,1)
q =

MATLAB/SIMULINK 91

29.8583
: .

x(t ) sin(2t )
za t [0,3 ] .

y(t ) cos(t )

z (t ) t

:
>> t=0:0.1:3*pi;
>> plot3(sin(2*t),cos(t),t)

10
8
6
4
2
0
1
0.5

1
0.5

-0.5

-0.5
-1

-1

.2.8.4.
:
3

4 cos 2 (2t ) sin 2 (t ) 1dt

-,
hcurve.m.
fajl E:\mat-vezbi\hcurve.m ()
function f = hcurve(t)
f = sqrt(4*cos(2*t).^2 + sin(t).^2 +1);
quad.
>> len = quad('hcurve',0,3*pi)
len =
17.2220

>> fplot('[2*sin(x+3), humps(x)]', [-5 5])

92 MATLAB/SIMULINK

2.9. ()

:
, .
.. .
- .
, .
2.9.1 ( )


- .

.
.
50x50,
2500 .

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

m-
.
m-
MatLab- .
matdat.m .
fajl E:\mat-vezbi\matdat.m ()
A = [ 0.2113 0.8098 0.4832 ; 0.0824 0.987 0.6512;
0.7599 0.4524 0.2749; 0.0087 0.8075 0.888];
,
>> matdat
>> A
A=
0.2113 0.8098 0.4832
0.0824 0.9870 0.6512
0.7599 0.4524 0.2749
0.0087
0.8075

0.8880

MATLAB/SIMULINK 93

,
.

ASCII ().

.
() .
ASCII .
load
.
fajl E:\mat-vrzbi\b.dat
0.2113 0.8098 0.4832
0.0824 0.9870 0.6512
0.7599 0.4524 0.2749
0.0087 0.8075 0.8880
>> load b.dat
>> b
b =
0.2113
0.0824
0.7599
0.0087

0.8098
0.9870
0.4524
0.8075

0.4832
0.6512
0.2749
0.8880

fopen, fread
MatLab- ( ) .

.

MatLab-
MatLabo-.

2.9.2 ()
,
.

,
diary diary .
.
;
diary .
.

94 MATLAB/SIMULINK

ASCII save -ascii


. ,
A = rand(4,3);
save temp.dat A -ascii
type temp.dat
9.5012929e-001 8.9129897e-001 8.2140716e-001
2.3113851e-001 7.6209683e-001 4.4470336e-001
6.0684258e-001 4.5646767e-001 6.1543235e-001
4.8598247e-001 1.8503643e-002 7.9193704e-001

,
.

2.9.3 input
input
. ,
n = input(' poraka : ')
n
.
n = input(' Vnesi broj ')
Vnesi broj: 23
n =
23
Input - . ,
v = input(' Ime i Prezime : ','s')
Ime i Prezime : Petre Petrevski
v =
Petre Petrevski

MATLAB/SIMULINK 95

3.

, , ,
.
. - . ,

.

.
3.1.
. 1

p ( x) x 3 2 x 5
.
MATLAB-, -

.
>> p = [ 1 0 -2 -5]
p =
1
0
-2

-5


.
:
conv
dekonv
polyval
roots
poly
polyfit

Primerot e zemen od upatstvoto za MATLAB

96 MATLAB/SIMULINK

3.1.1.
roots ..
.
, roots
:
>> r = roots(p)
r=
2.0946
-1.0473 + 1.1359i
-1.0473 - 1.1359i
, -.
roots poly.
.
>> p2 = poly(r)
p2 =
1.0000 -0.0000 -2.0000 -5.0000
roots poly (
) . poly

roots
2
>> A = [ -5.0 -52.06; 0.092 -0.433];
>> p2 = poly(A)
>> r = roots(p2)
p2 =
1.0000 5.4330 6.9545
r=
-3.3683
-2.0647
3.1.2
polyval
. p(x)
x = 5 :

primerot e zemen od knigata "MATLAB i dodatni moduli Control system toolbox i Simulink " od
Latinka ^alasan i Menka Petkovska, Mikro kwiga, Beograd

MATLAB/SIMULINK 97

>> polyval(p,5)
ans =
110
:
>> x = [ 0.1 0.5 1 2 3 4 5];
>> v = polyval(p,x)
v=
-5.1990 -5.8750 -6.0000 -1.0000 16.0000 51.0000 110.0000

3.1.3 () ( )
. conv deconv.
:

a(s) s 2 2s 3 i b(s) 4s 2 5s 6
>> a = [ 1 2 3]; b = [ 4 5 6];
>> c = conv(a,b)
c=
4 13 28 27 18
- c
a b.
>> [q,r] = deconv(c,a)
q=
4
r=
0

- r .

q.
3.1.4
polyfit
.
.

98 MATLAB/SIMULINK

p = polyfit(x,y,n)
x y
, n .
>> x = [ 1 2 3 4 5]; y = [ 5.5
>> p = polyfit(x,y,3)
p =
-0.1917

31.5821

43.1

-60.3262

128

290.7

498.4];

35.3400


polyfit.
>> x2 = 1:.5:5;
>> y2 = polyval(p,x2);
>> plot(x,y,'o',x2,y2)
>> grid on

500
450
400
350
300
250
200
150
100
50
0
1

1.5

2.5

3.5

4.5

.3.1.1.
3.1.2.
>> x = [0.1 0.3 0.4 0.44 0.5 0.7 0.8];
>> y = [ 1 1.5 1.6 1.4 1.1 1.1 1.5];
>> c = polyfit(x,y,4)
c =
13.2447

1.3735

>> x1 = 0.1:0.01:0.8;

-20.3555

10.3114

0.1641

MATLAB/SIMULINK 99

>> y1 = polyval(c,x1);
>> plot(x1,y1,x,y,'*')

1.6

1.5

1.4

1.3

1.2

1.1

0.9
0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

3.2.
, polyfit,
( )
.


.


3.
3.2.1

,
4.
-.

3
4

Vidi del 1.3 vo predavawata za vtoriot semestar


Isto

100 MATLAB/SIMULINK

function [A]= myvander(x)


%----------% funkcija vander presmetuva Vandermundovata matrica
% Vlez:
x vektor-kolona na nezavisnata promenliva
%
vo koja se dadeni podatocite
% Izlez: matrica A
%----------n = length(x);
for i=1:n
j= n-i;
A(:,i)=x.^(j);
end
MatLab:
>>
>>
>>
>>

x = [ 0 1 3 ]';
fx = [ -5 1 25]';
A = myvander(x)
coef = A\fx

A =
0
1
9
coef =
2
4
-5

0
1
3

1
1
1

coef = A\fx
. coef = A-1*fx.
:

f ( x) 2 x 2 4 x 5

.
>> x1 = 0:0.2:3.4;
>> y1 = polyval(coef,x1);
>> plot(x1,y1,x,fx,'*')

MATLAB/SIMULINK 101

35
30
25
20
15
10
5
0
-5
0

0.5

1.5

2.5

3.5

.3.2.1
MatLab-
,
vander(x). ,
>> A = vander(x)
A =
0
1
9

0
1
3

1
1
1

myvander.

myvander
- .
-
,
.
3.2.2

L0(x), L1(x),...., Ln(x),

x, 15.
5

Vidi del 1.4 vo predavawata za vtoriot semestar

102 MATLAB/SIMULINK

MatLab-
-.
- .
, ,
.
function [C,L] = lagrange(X,Y)
%-----------------------------------------------------------%LAGRANGE Konstrukcija na polinom so metoda na Lagranzova
%
interpolacija
% Nacin na povikuvawe
% [C] = lagrange(X,Y)
% [C,L] = lagrange(X,Y)
% Inputs
% X vector na vrednostite na nezavisnata promenliva
% Y vector na zavisnata promenliva
% Izlez
% C lista na koeficienti za Lagrange polinom
% L matrica koja gi sodrzi poedinecnite Lagrange polnomi
%
%----------------------------------------------------------n1 = length(X);% dolzina na vektorot X, broj na elementi
n = n1-1;
L = zeros(n1,n1);% se generira matrica so n1xn1 nuli
for k=1:n+1, % formirane na koeficientite na Lagrange plinom
V = 1;
% so koristene na poly(r1)za kreirawe na polinom
for j=1:n+1, % so poznati koreni, i conv(P2,P1)
if k ~= j, % za mnozenje na polinomi.
V = conv(V,poly(X(j)))/(X(k)-X(j));
end
end
L(k,:) = V;
end
C = Y*L;
m- lagrange,
m- myvander .
>> x = [ 0 1 3];
>> y = [ -5 1 25];
>> [coef,L] = lagrange(x,y)
coef =
2.0000
L =

4.0000

-5.0000

MATLAB/SIMULINK 103

0.3333
-0.5000
0.1667

-1.3333
1.5000
-0.1667

1.0000
0
0


.
,
.
3
.
L,
: L0(x), L1(x)
L2(x) , :
L0(0) = 1, L0(1) = 0, L0(3) = 0
L1(0) = 0, L1(1) = 1, L1(3) = 0
L2()) = 0, L2(1) = 0, L2(3) = 1
L .
L(x) = -5*L0(x) + 1*L1(x) + 25*L2(x)
L .
>> x = 0:0.1:3;
>> L0 = L(1,:); L1=L(2,:);L2=L(3,:);
>> y0 = polyval(L0,x);
>> y1 = polyval(L1,x);
>> y2 = polyval(L2,x);
>> plot(x,y0,x,y1,x,y2)
>> grid on
1.2
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
0

0.5

1.5

.3.2.2

2.5

104 MATLAB/SIMULINK

( - coef)

.
:
>> x1 = [ 0.5 1.5 2];
>> y1 = polyval(coef,x1)
y1 =
-2.5000

5.5000

11.0000

3.2.3. -

.
6.
function [C,D] = newtint(X,Y)
%----------------------------------------------------------%NEWTINT metoda na Newtonova interpolacija
% Nacin na povikuvanje
% [C] = newtint(X,Y)
% [C,D] = newtint(X,Y)
% Vlez:
% X vector na nezavisnata promenliva
% Y vector na zavisnata promenliva
% Izlez
% C koeficientite na Newton-oviot polinom
% D tabela na konecni razliki
%
%-----------------------------------------------------------n = length(X);
D = zeros(n,n);
D(:,1) = Y';
for j=2:n,
for k=j:n,
D(k,j) = (D(k,j-1)-D(k-1,j-1))/(X(k)-X(k-j+1));
end
end
C = D(n,n);
for k=(n-1):-1:1,
C = conv(C,poly(X(k)));
m = length(C);
C(m) = C(m) + D(k,k);
end

Vidi del 1.5 vo predavawata za vtoriot semestar

MATLAB/SIMULINK 105

- C ,
D 0-, 1, 2, .... - ,
. f (x)
k

(- ) k !.
MatLab-
.
17:
>> x = [0 1 2]; y=[ -5 1 11];
>> [C,D] = newtint(x,y)
C =
2

-5

-5
1
11

0
6
10

0
0
2

D =

28:
>> x = [ 0 1 2 3 4]; fx=[ -5 1 9 25 55];
>> [C,D]=newtint(x,fx)
C =
0

-2

-5

-5
1
9
25
55

0
6
8
16
30

0
0
1
4
7

0
0
0
1
1

0
0
0
0
0

D =

1.5
>> a = 1.5;
>> vrd = polyval(C,a)
vrd =
4.3750

7
8

Primerot e od predavawata za vtoriot semestar


Isto

106 MATLAB/SIMULINK

3.3. ( )

. ,
( )
,
.

.

.

3.3.1.

, .
, , x-.
9.

f ( x 0 h) f ( x 0 h )
f
lim
x 0 x
h 0
2*h

f ( x0 ) lim


x0.
-,
f, .
function [H,D,E,n] = difflim(f,x,toler)
%----------------------------------------------------------%DIFFLIM Numericka aproksimacija na f'(x).
%
Metodata se bazira na presmetuvanje na limes.
% Primer na povikuvnje:
% [H,D,E,n] = difflim('f',x,toler)
% Vlez:
% f name of the function
% x differentiation point
% Izlez:
% H vector na golemina na cekori
% D vector na priblizna vrednost na izvodot
% E vector na greska vo sekoj cekor
% n elementot D(n) "najdobra aproksimacija na f'(x)"
%
9

Vidi del 2.1 vo predavawata za vtoriot semestar

MATLAB/SIMULINK 107

% (Diferenciranje koristejki limes).


%--------------------------------------------------------h = 1;
max1 = 15;
H(1) = h;
D(1) = (feval(f,x+h) - feval(f,x-h))/(2*h);
E(1) = 0;
for n = 1:2
h = h/10;
H(n+1) = h;
D(n+1) = (feval(f,x+h) - feval(f,x-h))/(2*h);
E(n+1) = abs(D(n+1) - D(n));
end
n = 2;
while ((E(n)>toler)) & n<max1
h = h/10;
H(n+1) = h;
D(n+1) = (feval(f,x+h) - feval(f,x-h))/(2*h);
E(n+1) = abs(D(n+1) - D(n));
n = n+1;
end
n = length(D)-1;
% kraj na funkcijata............

%1
%2
%3
%4
%5
%6
%7
%8
%9
%10
%11
%12
%13
%14
%15
%16
%17
%18
%19
%20

m- difflim, :
f, x , toler,
.
- .
cos(x) m- .
file:f.m
function z = f(x)
z = cos(x);
.
- ( ) .
, ., difflim
. H
.
D E
. Brojot n D
(
).

-

108 MATLAB/SIMULINK

.
.
,
.

.
- difflim
. :
1-:
2:
3:
4:

1.
15.

H.
(**)
,
D. f
feval('f',x).

f 1.8
-0.2
>> x = 0.8;
>> h = 1;
>> f1 = feval('f',x+h)
>> f2 = feval('f',x-h)
>> izv = (f1-f2)/(2*h)
f1 =
-0.2272
f2 =
0.9801
izv =
-0.6036
'f',
feval .
5: .
6: for
.
.
13: while

D(n) E(n). while ..

.

MATLAB/SIMULINK 109

20: n, D
.
m- MatLab .
>>
>>
>>
>>

x0 = 0.8;
toler = 1e-7;
[H,D,E,n] = difflim('f',x0,toler);
n,H(n),D(n),E(n)

n =
4
ans =
1.0000e-004
ans =
-0.7174
ans =
1.1836e-007

x=0.8 D(4)=-0.7174,
H(4)=0.0001
E(4)=0.000000118.

3.3.2.


() .

.
10.
, -
x0 x () y
().
newtint(x,y) m-
. 2.3
-, .. . polyder
( help) polyval
MatLab .
m-.
function vr = newtdiff(x,y,x0)
[C,D] = newtint(x,y);
10

Vidi 2.3 od predavawata vo vtoriot semestar

110 MATLAB/SIMULINK

derc = polyder(C);
vr = polyval(derc,x0);
newtdiff
3.1.
:
>> x = 0:0.2:1.2;
>> y = cos(x);
>> x0 = 0.8;
>> izvod = newtdiff(x,y,x0)
izvod =
-0.7174
11
>>
>>
>>
>>

x=1:0.05:1.3;
y = sqrt(x);
x0 = 1.0;
izvod = newtdiff(x,y,x0)
izvod =
0.5000

3.4.
, quad
quad8 .


. 12
.

3.4.1

. m-
.

11
12

Od predavawata na strana 24
Vidi 3.2 od predavawata vo vtoriot semestar

MATLAB/SIMULINK 111

function v = trapez(x,y)
n = length(x);
m = length(y);
if m ~= n
display(' x i y nemaat ednakov broj na podatoci ')
break
end
v = 0;
for k=1:n-1
v=v+(x(k+1)-x(k))*(y(k+1)+y(k))/2.0;
end
x y,
y x(1) x(n).
13
>> x = 0.4:0.1:1.0;
>> y = exp(x)./x;
>> integral = trapez(x,y)
integral =
1.7950
14
>> x = 0:pi/12:pi/2;
>> y = sin(x);
>> integral = trapez(x,y)
integral =
0.9943
sin(x)
[0,pi/2], 1.
.

.
>>
>>
>>
>>

13
14

x = 0:pi/500:pi/2;
y = sin(x);
format compact
integral = trapez(x,y)

od predavawata na st. 29
od predavaqata na st. 30

112 MATLAB/SIMULINK

integral =
0.99999671012970
1,
5- 500.

.

3.4.2

15. -
.
function v = simpson(h,y)
%------------------------------% vlezni parametri
% h cekor na integracija ( interval)
% y() vrednosti na funkcijata t.e podatoci vo brojcana forma
%------------------------------n = length(y);
% se ispituva dali ima neparen broj na podatoci
% ako ne da se namali za eden
m = n;
if rem(n,2) == 0
m = n-1;
end
m,n
v = 0.0;
for k=2:m-1
if rem(k,2)== 0
v = v+4.0*y(k);
else
v = v+2.0*y(k);
end
end
v = h*(v + y(1)+y(m))/3;
% vo slucaj na paren broj na tocki dodaj ja
% poslednata povrsina koristejki trapezno pravilo
if rem(n,2) == 0
v = v+h*(y(n)+y(n-1))/2;
end

15

Vidi 3.2.3 od predavawata vo vtoriot semestar

MATLAB/SIMULINK 113

>>
>>
>>
>>

h = pi/500;
x = 0:h:pi/2;
y = sin(x);
integral = simps1(h,y)

integral =
1.0000

3.4.3.

.

.
,
16.
.

.
m- gauss5,
5 .
function sum = gauss5(f,a,b)
%------------------------------------------------------------------% Vlez
% f
ime na funcijata
% a
left endpoint of [a,b]
% b
right endpoint of [a,b]
% A
table of abscissas
% W
table of weights
% Return
% sum rezultat na integracija
%----------------------------------------------------------A = [-0.90617985 -0.53846931 0.0 0.53846931 0.90617985];
W = [0.23692689 0.47862867 0.56888889 0.47862867 0.23692689];
n = length(A);
sum = 0;
fct = (b-a)/2;
for k = 1:n
z = A(k);
w = W(k);
x = (b*(z+1)+a*(1-z))/2;
16

Vidi 3.3 od predavawata vo vtoriot semestar

114 MATLAB/SIMULINK

fz = fct*feval(f,x);
sum = sum+w*fz;
end
.

>> a = 0;
>> b = pi/2;
>> integral = gauss5('f',a,b)
integral =
1.0000
f m-.
function z = f(x)
z = sin(x);

>> a = 0.4;
>> b = 1.0;
>> integral = gauss5('f',a,b)
integral =
1.7904
f so:
function z = f(x)
z = exp(x)/x;

>> a = 1;
>> b = 2;
>> integral = gauss5('ff',a,b)
integral =
0.6931
ff m-:

function z = ff(x)
z = 1./x;

MATLAB/SIMULINK 115


quad quad8.
>> a = 1;
>> b = 2;
>> err = 1.e-5;
>> format long;
>> integral = quad('ff',a,b,err)
integral =
0.69314722007998
>>
>>
>>
>>

a = 1;
b = 2;
err = 1.e-5;
integral = quad8('ff',a,b,err)

integral =
0.69314718056011
3.5.

. ,
,
.
.

.
.
MATLAB, ,

. MATLABOT
.
.
3.5.1
2x1 + x2 = 4
x1 - x2 = -1

Ax b

t.e

MATLAB :

2 1 x1 4
1 1 x 1

116 MATLAB/SIMULINK

>>
>>
>>
>>
>>

A=[2 1; 1 -1];
b = [4 -1]';
x = A\b
c = A*x
err = b-c

x=
1
2
c=
4
-1
err =
0
0
\
- b A
- x.
e:

x A1b

A-1 A.


.
>> Ainv = inv(A)
>> x = Ainv*b
Ainv =
0.3333 0.3333
0.3333 -0.6667
x=
1.0000
2.0000

MatLab-
,

. ..
.
A (3,:) = [ 1 1]
b(3) = 4
x = A\b
c = A*x

MATLAB/SIMULINK 117

razlika = b-c
err = norm(razlika)
A=
2 1
1 -1
1 1
b=
4
-1
4
x=
1.0714
2.2857
c=
4.4286
-1.2143
3.3571
razlika =
-0.4286
0.2143
0.6429
err =
0.8018
x e
razlika. :
x1 =
1.09
2.24
x :
>> x1 = [1.09 2.24]'
>> c1 = A*x1
>> raz1 = b - c1
>> err1 = norm(raz1)
>> err1 - err
x1 =
1.0900
2.2400
c1 =
4.4200
-1.1500
3.3300
raz1 =
-0.4200
0.1500

118 MATLAB/SIMULINK

0.6700
err1 =
0.8049
ans =
0.0031

.
3.5.2:
>> A = [ 10 -7 0; -3 2 6; 5 -1 5]
>> b = [7 4 6]'
>> x = A\b
>> c = A*x
>> err = b-c
A=
10 -7 0
-3 2 6
5 -1 5
b=
7
4
6
x=
0
-1
1
c=
7
4
6
err =
0
0
0
3.5.3:
>> A = [ 1.0 0.8 0.64;1.0 0.9 0.81; 1.0 1.1 1.21]
>> b = [erf(0.8) erf(0.9) erf(1.1)]'
>> x = A\b
>> c = A*x
>> err = b-c
A=
1.0000 0.8000 0.6400
1.0000 0.9000 0.8100

MATLAB/SIMULINK 119

1.0000 1.1000 1.2100


b=
0.7421
0.7969
0.8802
x=
-0.0122
1.2937
-0.4386
c=
0.7421
0.7969
0.8802
err =
1.0e-015 *
0.1110
0
0
erf(x) .
help erf. err 10-15,
.

3.5.1
-- ,

.
(30x30).
, ,
. ,
.. .

.
gaussj Gaus-Jordan .
,

MATLAB-. (8 )

for .

120 MATLAB/SIMULINK

function X = gaussj(A,B)
%-------------------------------------------------------------%GAUSSJ Gauss-Jordan metoda za resavawe na linearni ravenki
% Povikuvanje
% X = gaussj(A,B)
% Vlez
% A coeficienti na matricata
% B vector na desnata strana
% Izlez
% X vector na resenie
%---------------------------------------------------------------[n n] = size(A);
A = [A';B']';
X = zeros(n,1);
for p = 1:n,
for k = [1:p-1,p+1:n],
if A(p,p)==0, break, end
mult = A(k,p)/A(p,p);
A(k,:) = A(k,:) - mult*A(p,:);
end
end
X = A(:,n+1)./diag(A);
3.5.4:
>> A = [ 1.0 0.8 0.64;1.0 0.9 0.81; 1.0 1.1 1.21]
>> b = [erf(0.8) erf(0.9) erf(1.1)]'
>> x = gaussj(A,b)
>> c = A*x
>> err = b-c
A=
1.0000 0.8000 0.6400
1.0000 0.9000 0.8100
1.0000 1.1000 1.2100
b=
0.7421
0.7969
0.8802
x=
-0.0122
1.2937
-0.4386
c=
0.7421
0.7969
0.8802
err =
1.0e-015 *

%1
%2
%3
%4
%5
%6
%7
%8
%9
%10
%11

MATLAB/SIMULINK 121

0.1110
0.1110
0.2220

>> A = [ 10 -7 0; -3 2 6; 5 -1 5]
>> b = [7 4 6]'
>> x = gaussj(A,b)
>> c = A*x
>> err = b-c
A=
10 -7 0
-3 2 6
5 -1 5
b=
7
4
6
x=
0
-1.0000
1.0000
c=
7.0000
4.0000
6.0000
err =
1.0e-013 *
-0.3109
0.1066
-0.0355

3.5.2 (Gaus - Zajdel)


,
.


.
gseid MatLab- a GausZejdel- .

122 MATLAB/SIMULINK

function [P,dP,Z] = gseid(A,B,P,delta,max1)


%------------------------------------------------------------% GSEID Gauss-Seidel iteraciona metoda
% Se povikuva
% [X,dX] = gseid(A,B,P,delta,max1)
% [X,dX,Z] = gseid(A,B,P,delta,max1)
%
% Vlez
% A
mtricata na sistemot
% B
vectorot na desnata strana
% P
poceten vector na iteracija
% delta tolerancija na greska
% max1 maximalen broj na iteratcii
%
% Izlez
% X
vector resenie
% dX greska vo sekoja iteracija
% Z
matrica na resenija vo sekoja iteracija
%-------------------------------------------------------------Z = P';
n = length(B);
Pold = P;
for k=1:max1,
for r = 1:n,
Sum1 = B(r) - A(r,[1:r-1,r+1:n])*P([1:r-1,r+1:n]);
P(r) = Sum1/A(r,r);
end
dP = abs(Pold-P);
err = norm(dP);
relerr = err/(norm(P)+eps);
Pold = P;
Z = [Z;P'];
if (err<delta)|(relerr<delta), break, end
end
3.5.5:
>> A = [ 10 1 1; 2 10 1; 2 2 10]
>> b = [12 13 14]'
>> x0 = [ 1.07 0.4 2.3]'
>> toler = 1.e-8
>> maxiter = 20
>> [x,DX,Z] = gseid(A,b,x0,toler,maxiter)
>> c = A*x
>> err = b-c

MATLAB/SIMULINK 123

A=
10 1 1
2 10 1
2 2 10
b=
12
13
14
x0 =
1.0700
0.4000
2.3000
toler =
1.0000e-008
maxiter =
20
x=
1.0000
1.0000
1.0000
DX =
1.0e-008 *
0.2106
0.0150
0.0451
Z=
1.0700 0.4000
0.9300 0.8840
1.0079 0.9947
1.0006 0.9999
1.0000 1.0000
1.0000 1.0000
1.0000 1.0000
1.0000 1.0000
1.0000 1.0000
c=
12.0000
13.0000
14.0000
err =
1.0e-009 *
0.3013
0.4511
0

2.3000
1.0372
0.9995
0.9999
1.0000
1.0000
1.0000
1.0000
1.0000

124 MATLAB/SIMULINK

3.5.6:
>> A = [ 8 1 1; 1 5 -1; 1 -1 5]
>> b = [26 7 7]'
>> x0 = [ 1.07 0.4 2.3]'
>> toler = 1.e-6
>> maxiter = 20
>> [x,DX,Z] = gseid(A,b,x0,toler,maxiter)
>> c = A*x
>> err = b-c
A=
8 1 1
1 5 -1
1 -1 5
b=
26
7
7
x0 =
1.0700
0.4000
2.3000
toler =
1.0000e-006
maxiter =
20
x=
3.0000
1.0000
1.0000
DX =
1.0e-005 *
0.1308
0.0940
0.0450
Z=
1.0700 0.4000
2.9125 1.2775
2.9562 1.0234
2.9954 1.0036
2.9993 1.0005
2.9999 1.0001
3.0000 1.0000
3.0000 1.0000
3.0000 1.0000
c=
26.0000
7.0000
7.0000

2.3000
1.0730
1.0134
1.0016
1.0002
1.0000
1.0000
1.0000
1.0000

MATLAB/SIMULINK 125

err =
1.0e-005 *
0.1389
-0.0450
0
x0 =
[1.07 0.4 2.3]' .
x0 .
>> A = [ 8 1 1; 1 5 -1; 1 -1 5]
>> b = [26 7 7]'
>> x0 = [ 107 -24 32]'
>> toler = 1.e-6
>> maxiter = 20
>> [x,DX,Z] = gseid(A,b,x0,toler,maxiter)
>> c = A*x
>> err = b-c
A=
8 1 1
1 5 -1
1 -1 5
b=
26
7
7
x0 =
107
-24
32
toler =
1.0000e-006
maxiter =
20
x=
3.0000
1.0000
1.0000
DX =
1.0e-006 *
0.4888
0.3509
0.1679
Z=
107.0000 -24.0000 32.0000
2.2500 7.3500 2.4200
2.0288 1.4782 1.2899
2.9040 1.0772 1.0346

126 MATLAB/SIMULINK

2.9860 1.0097
2.9982 1.0013
2.9998 1.0002
3.0000 1.0000
3.0000 1.0000
3.0000 1.0000
3.0000 1.0000
c=
26.0000
7.0000
7.0000
err =
1.0e-006 *
0.5188
-0.1679
0

1.0047
1.0006
1.0001
1.0000
1.0000
1.0000
1.0000


( ),
.
.
48.
49
.
>> A = [ 65 17 -13 7
23 56 11 -19
-5 3 47 20
16 12 17 18]
>> b = [84 36 18 25]'
>> x0 = [ 1.07 0.4 2.3 0.5]'
>> toler = 1.e-8
>> maxiter = 100
>> [x,DX,Z] = gseid(A,b,x0,toler,maxiter);
>> [iter,m]=size(Z);
>> x,iter
>> c = A*x
>> err = b-c
A=
65 17 -13 7
23 56 11 -19
-5 3 47 20
16 12 17 18
b=
84
36
18

MATLAB/SIMULINK 127

25
x0 =
1.0700
0.4000
2.3000
0.5000
toler =
1.0000e-008
maxiter =
100
x =
1.6181
-0.3788
0.8241
-0.5752
iter =
33
c =
84.0000
36.0000
18.0000
25.0000
err =
1.0e-006 *
0.3173
-0.2172
0.1472
0.0000

3.6.

. ,
(), . 17,
.
.
,
( least square error LSQ ).

Vo angliskata literatura ovoj vid na aproksimacija se narekuva "fitting". Vo na{iot jazik


soodveten prevod ne postoi zatoa ~esto mo`e da se sretne samiot zbor fiting. Najblisku do
prevodot e "aproksimacija" iako vo angliskata literatura zborot aproksimacija (aproximation)
e poseben zbor. Vo statistikata postapkata se narekuva regresija a krivata regresiona kriva.
17

128 MATLAB/SIMULINK

3.6.1

. LSQ
.
- m-
polyfit. m-
.
y ( y1 , y 2 ,...., y n ) x ( x1 , x2 ,..., xn ) .

p(s) ak x k ak 1 x k 1 .... a1 x a0
ak, ak-1,...,a1,a0
.

S j 1 ( p( x j ) y j ) 2
n

y1
y
ak
2
a
y3
k 1

'
'
A A . A .

.
a1

a 0
y n 1
y
n
A :

MATLAB/SIMULINK 129

x1k
k
x2
x3k .

A .
.
k
x n 1
xk
n

x1k 1
x 2k 1
x3k 1
.
.
k 1
x n 1
x nk 1

. x1
. x2
. x3
.
.
.
.
. x n 1
. xn

1
1

.
.

1
1

A (n,(k+1)) n (k+1)
, A' A.
M- polylsq MATLAB .
function [C,Err]= polylsq(x,y,k)
%------------------------------% Polinomna regresija
% Vlez:
% y : vektor na podatoci
% x : vektor na tocki koi soodvestvuvaat na podatocite
% k : redot na polinomot
% Izlez:
% C : vektor na koeficienti
% Err : Vkupna kvadratna greska
%--------------------------------% proveri dali x i y se vektor-koloni ako ne transformiraj gi vo vektor-koloni
[nr,nc] = size(y);
if nr == 1
y=y';
n=nc;
end
[mr,mc] = size(x);
if mr == 1
x = x';
m = mc;
end
n = length(y);
m = length(x);
if m ~= n
display(' x i y nemaat ist broj na podatoci'), break
end
A = zeros(n,k+1);
for j = 1:k+1
A(:,j)= x.^(k+1-j);

%1
%2
%3

130 MATLAB/SIMULINK

end
S = A'*A;
b = A'*y;
C = S\b;
Err = norm((y-A*C));

%4
%5
%6
%7
%8

for ( 2-4) A.
5 A'A 6- ,
, A'.
\
.
.
3.6.1:
>> x =[0 1 2 3 4 5 6];
>> y =[ 2 3 5 5 8 9 10];
>> [C,Err]=polylsq(x,y,1)
C =
1.3929
1.8214
Err =
1.2956

(Err = 1.2956 ) :

p( x) 1.3929 x 1.8214
(. =2)
m- .
>> x =[0 1 2 3 4 5 6];
>> y =[ 2 3 5 5 8 9 10];
>> [C1,Err1]=polylsq(x,y,2)
C1 =
0.0119
1.3214
1.8810
Err1 =
1.2910
m- polylsq 2.
. .
>> x =[0 1 2 3 4 5 6];

MATLAB/SIMULINK 131

>> y =[ 2 3 5 5 8 9 10];
>> [C3,Err3]=polylsq(x,y,3)
C3 =
-0.0278
0.2619
0.7659
2.0476
Err3 =
1.2247
Err3 = 1.2247

.
? ,
,
.


.
.
.
.
>>
>>
>>
>>

x1 = 0.0:0.2:6.2;
y1 = polyval(C,x1);
y3 = polyval(C3,x1);
plot(x,y,'o',x1,y1,'-',x1,y3,'--')

11
10
9
8
7
6
5
4
3
2
1
0

.3.6.1.

132 MATLAB/SIMULINK

, ,
.
()
.
polyfit.
>> [C1,Q1] = polyfit(x,y,1)
>> [C2,Q2] = polyfit(x,y,2)
>> [C3,Q3] = polyfit(x,y,3)
C1 =
1.3929 1.8214
Q1 =
R: [2x2 double]
df: 5
normr: 1.2956
C2 =
0.0119 1.3214 1.8810
Q2 =
R: [3x3 double]
df: 4
normr: 1.2910
C3 =
-0.0278 0.2619 0.7659 2.0476
Q3 =
R: [4x4 double]
df: 3
normr: 1.2247
,
normr.
3.6.2

- .
. .

f ( x) ak qk ( x) ak 1qk 1 ( x) .... a1q1 ( x)


qk(x)
.

. A .

MATLAB/SIMULINK 133

q k 1 ( x1 )
q k ( x1 )
q (x )
q k 1 ( x 2 )
k 2
q k ( x3 )
q k 1 ( x3 )

A
.
.

.
.

q k ( x n 1 ) q k 1 ( x n 1 )
q (x )
q k 1 ( x n )
k n

. q 2 ( x1 )
q1 ( x1 )
. q2 ( x2 )
q1 ( x 2 )
. q 2 ( x3 )
q1 ( x3 )

.
.
.
.
.
.

. q 2 ( x n 1 ) q1 ( x n 1 )
. q2 ( xn )
q1 ( x n )


, ,
, .
, , ,
ak,
qk.
(non-linear least sqyare NLSQ).
.
.

f ( x) a1q1 ( x) kade q1 ( x) e bx
a1 b
q1.

.

ln( f ( x)) ln a1 bx
:

y c bx

y ln( f ( x)) a c ln( a1 )

,
. m- polylsq.
.
>> x = 0:0.1:3;
>> a = 5.5;
>> b=-1.2;
>> y = a*exp(b*x);
>> n=length(y);
>> z = rand(1,n)-0.2;
>> y1 = y+z;
>> plot(x,y1,'o')

134 MATLAB/SIMULINK

0
0

0.5

1.5

2.5

.
y
-0.2 0.2. y =
a*exp(b*x).
>>
>>
>>
>>
>>
>>
>>

ylg = log(y1);
[C,Err] = polylsq(x,ylg,1)
alg = C(2)
breg = C(1)
areg = exp(alg)
yteor = areg*exp(breg*x);
plot(x,y1,'o',x,yteor,'-')
C=
-0.8863
1.5706
Err =
2.8748
alg =
1.5706
breg =
-0.8863
areg =
4.8095

MATLAB/SIMULINK 135

0
0

0.5

1.5

2.5

a
4.8095 b e -0.8863. 5.5 -1.2.


, .

136 MATLAB/SIMULINK

3.7.

MATLAB/SIMULINK 137

4. SIMULINK
4.1. SIMULINK
SIMULINK MATLAB . SIMULINK,
. : , .,
. Simulink
MATLAB .
SIMULINK MATLAB
, .
SIMULINK Unix, Macintosh Windows;
MATLAB .
SIMULINK , ,
, . SIMULINK MATLAB
Control System toolbox .
4.2. SIMULINK
SIMULINK MATLAB (MATLAB
Command Windows) >> simulink
Enter ( .4.2.1)

.4.2.1. SIMULINK
, SIMULINK
.

(Simulink Library Browser) .4.2.3.

Simulink (.4.2.2).

138 MATLAB/SIMULINK

.4.2.2. Simulink

.4.2.3. Simulink

MATLAB/SIMULINK 139

4.3.
Simulink, , ,
. ,
(.4.2.2)
(. work) MATLAB.
File
MATLAB. ,
mt_p42_01_Primer.mdl.
1) (.4.2.1)
>> :
>> mt_p42_01_Primer
, Enter ( 4.3.1).

.
2) File
File, Open
mt_p42_01_Primer. mdl (..4.3.2 .4.3.2.).
3) Ctrl+O
MATLAB
Ctrl+O.
- mt_p42_01_Primer.mdl

.4.3.1. Simulink mt_p42_01_Primer


140 MATLAB/SIMULINK

.4.3.2. Simulink
(.4.3.1)

,
.

4.4.
Simulink: (blocks)
(lines). , , ,
.
.
4.4.1.
.4.4.1.

6.5.
.

4.2., .4.4.1.).
Simulink. e ( ) Continuous

(.4.4.1. ) .
Simulink .


.

MATLAB/SIMULINK 141

)
)
.4.4.1. ( 6.5)
Simulink :
(Continuous)
,
(Diskontinuities)
,
(Discrete)
( , .),
(Look-Up Tables) (
),
(Math Operations)
,
(Signal Routing),
(Sinks) ,
(Sources) ,
(User Defined Functions)
.

.
. .
.4.4.2.
( = , , ).

142 MATLAB/SIMULINK

. 4.4.2.
.
4.4.2.
.

.
,
.4.4.3.

. 4.4.3.
:
.
.
.
(SISO Single-Input, Single Output Systems),
.
(MIMO Multi-Input, MultiOutput Systems),
.

.
.
4.4.3.
mt_p42_02_Primer (Sl.4.4.4) : Step,
Transfer function Scope. Step (source block)
u=S(t) () .

MATLAB/SIMULINK 143


.
y=y(t)
Scope (sink block). Scope
.

.4.4.4.
Simulink,
.
.4.4.4.
4.4.4.
.
, Transfer Fcn .4.4.4.
-.
-
.
,
. , s^2+2s+1,
Denominator : [1 2 1] Close, posle
.4.4.6.

.4.4.5. - Transfer Fcn

144 MATLAB/SIMULINK

.4.4.6. Transfer Fcn


"Step"
-.

.4.4.7. "Step"
Default - "step"
t=1 sec, 0 1. (
, t=1). .
- .
"Scope" .
.4.4.8.

MATLAB/SIMULINK 145

.4.4.8. "Scope"
, "Scope"
.. "Scope"
.

4.5.
,
: mt_p42_03_Primer.mdl.
Simulink
. .4.4.9.

.4.4.9.

146 MATLAB/SIMULINK

,
Scope.
Simulation.
.4.4.10.

.4.4.10.
Start
. .4.4.11.

.4.4.11.
( ) .

.
.
.4.4.12.

MATLAB/SIMULINK 147

.4.4.12.

Ctrl-T. .
.4.4.12. t =
1. "step".

. "Transfer Fcn"
: [1 20 400]
,
Ctrl-T, ,
()
.4.4.13 ).

)
.4.4.13. ) , ) )


.
, .
.

148 MATLAB/SIMULINK

Simulink 10
- .

. Simulation
Simulation Parameters . -
.4.4.14.

.4.4.14. Dialog-box Simulation Parameters ) , )


,
.
(Simulation time: Start time Stop time).
Start time 0.0 0.8 (
t=1.0). Stop time
10.0 2.0 .
- .
()
,
.4.4.15.

.4.4.15. .4.4.14 )

MATLAB/SIMULINK 149

4.6.

Simulink (Simulink's Block
Libraries).
. 4.6.1.

.4.6.1.

(block libraries).
( )
.
.
.
4.6.1.
:
1) Simulink ( 4.2) ( New File
Ctrl-N, )).
4.6.2. )
.

150 MATLAB/SIMULINK

)
.4.6.2. ) ,
)

.4.6.2. ) Simulink (
) :
Continuous ( ), Discontinuities, Discrete, Look-Up Tables,
Math Operations ( ), , Signal Routing, Sinks (),
Sources (), User-Defined Functions.
. .4.6.2. )
Continuous
(Derivative, Integrator, State Space, Transfer FCN, Transport Delay, Variable Transport
Delay, Zero-Pole).
.
2) Sources
(.4.6.3.).
Step,
.
Step
.
(.4.6.3.).
.
3) Math Operations (
), (Sum)
(Gain). O .4.6.4.)

MATLAB/SIMULINK 151

)
)
.4.6.3. ) Sources (), )

.4.6.4. ) Math Operations


( ) , )
Continuous ( )
4) Continuous ( )
(
.4.4.1). .4.6.4.).

. .
5) Sinks ()
( Scope).
() .
.4.6.5.

152 MATLAB/SIMULINK

.4.6.5

.
(
)
.
. Transfer Function
1.
1.
/ .
4.6.2.

.
- Sum.
List of signs +-. .
- Gain. 2.5
-.
- Transfer Fcn. [1 2],
[1 0]. -.
- Transfer Fcn1. [1],
[1 2 4]. -.
,
,
.4.6.6.

MATLAB/SIMULINK 153

.4.6.6.
-

.

.
. Transfer Fcn
PI Regulator.
Transfer Fcn1 Objekt.
.

.4.6.7.

.4.6.7.
4.6.3.
.
.
-

Step.

154 MATLAB/SIMULINK

.

. ,
(
) , , ,
Step. .4.6.8.

.4.6.8.
- .

.
, .
. 4.6.9.

.4.6.9.
-

MATLAB/SIMULINK 155

.

. ,
,
.
Objekt Scope, Ctrl
, (
) .
.4.6.10.

.4.6.10.
-


. -
.
.4.6.11.

.4.6.11.

156 MATLAB/SIMULINK

,
.4.6.11.
,
.
, ,
.
,
.
.

.

.
()

( )
. .4.6.12.

.4.6.12.
-

, .
,
.

.
.
. .4.6.13
: r , e , u
, y .

MATLAB/SIMULINK 157

.4.6.13.
-

File Save As
.
mt_p46_02_primer.mdl.
(
.4.6.14).

.4.6.14.
mt_p46_02_primer.mdl
.4.6.14
.

158 MATLAB/SIMULINK

4.6.4.
,
. Simulation Start
. Scope .
()
.4.6.15.

.4.6.15.
4.6.5. MATLAB
, , ,
Simulink .
. ,

K.
:
>> K=2.5
a Simulink Gain.
, Gain
- , Gain .

MATLAB/SIMULINK 159

.4.6.16.

. Gain
(.4.6.17).

.4.6.17. ()

Scope. .
.4.6.15 .4.6.18.

160 MATLAB/SIMULINK

.4.6.18. =2.5



. \
:
>> =5
.
, .
4.6.19.

.4.6.19. =5

,
.

MATLAB/SIMULINK 161

5.

, ,
, .

.
.
.
5.1.
5.1.1. ,
. 5.1.1.
m - masa
k - konstanta na pru`inata
c - koeficient na viskozno pridu{uvawe

Dijagram na slobodno telo

kx

F (t )
cx

F (t )

Trieweto na trkalata so
podlogata se zanemaruva

x
. 5.1.1. ,
. 5.1.1
x :

d 2x
dx
c kx F (t )
2
dt
dt

(5.1.1.)

d 2 x c dx k
1

x F (t )
2
m dt m
m
dt

(5.1.2)

162 MATLAB/SIMULINK

F(t)

- ;
- , ,

kx
dx
c
cx - , c ,
dt
(5.1.2) :

c
k
1
x x F (t )
m
m
m

(5.1.3)

.5.1.1
x
dx / dt x . :

x1 x
x2 x

(5.1.4)

(5.1.3)
, :

x1 x2
x 2

(5.1.5)

1
[ F (t ) cx 2 kx1 ]
m

(t=0) , ,
F(t)=25N . :

m 1[kg], k 25[ N / m], c 5[

N
]
m/ s



.

-
, -
, (ode23
), .
fc_mehsist.m:
%
function dx = fc_mehsyst(t,x); % Gi presmetuva izvodite na varijablite na sostojba
%
% Parametri na sistemot
F = 25;
% Nadvoresna sila - Step vlez [N]
m =1;
% Masa [kg]
c = 5;
% Koeficient an prigusniot element [N/m/s]
k = 25;
% Koeficient na pruzinata [N/m]
%
% Presmetka na desnite strani na diferencijalnite ravenki
dx = [x(2) ; 1/m*( F - c*x(2) - k*x(1))];

MATLAB/SIMULINK 163

: mt_p51_01_fc_mehsist.m
% Glavna programa: Mehanicki sistem-Masa pruzina-prigusuvac
% Ime na fajlot: mt_p51_01_fc_mehsist.m
% Funkcii: fc_mehsist.m
%
t0 = 0;
% Pocetno vreme
tf = 3;
% Konecno vreme
x0 = [0, 0];
% Pocetni uslovi
tol = 0.001;
% Tocnost
trace = 0;
% Ako ne e nula, sekoj cekor se pecati
%
% Integracija na diferencijalnite ravenki:
[t,x] = ode23('fc_mehsist',t0,tf,x0,tol,trace);
%
% Crtanje na rezultatite:
subplot(211),plot(t,x)
title('Vremenski odziv na meh.translatoren sistem')
xlabel('t [s]')
text(2,1.2,'Pomestuvanje d')
text(2,.2,'Brzina v')
%
d = x(:,1);
% Pomestuvanje
v = x(:,2);
% Brzina
%
subplot(212), plot(d, v)
title('Brzinata V vo funkcija na pomestuvanjeto d')
xlabel('Pomestuvanje d')
ylabel('Brzina V')
%

. 5.1.2.

164 MATLAB/SIMULINK

5.1.2.
. 5.1.3.
G mg[kg] L
[m].
C [

[rad ]

N
].
m/ s

( ) VT x L
( x
.5.1.3) :

FT G sin CL

FN

mx mL FT

G sin

Cx

x VT L

+x

G cos

G mg
.5.1.3.
:

mL FT

mL CL G sin 0

MATLAB/SIMULINK 165

.
: x1 ( ) x2 ( ), :

x1 x2
x 2

C
G
x2
sin x1
m
mL

MATLAB. M-file fc_nisalo.m (), M-file mt_p51_02_fc_nisalo.m.


.
M-File:
%
function dx = fc_nisalo(t,x); % Presmetuvanje na izvodite
% na varijablite na sostojba
% Parametri na nisaloto
%
G = 2;
% Tezina na nisaloto[N]
L = 0.6;
% Dolzina na stapot [m]
C = 0.02;
% Koeficient na prigusuvanje [N/m/s]
g0 = 9.81;
% Zabrzuvanje na zemjinata teza [m/s^2]
m = G/g0;
% Masa na nisaloto [kg]
%
% Presmetka na desnite strani na dif. ravenki
%
dx = [x(2); (-C/m)*x(2)-G/(m*L)*sin(x(1))]; % dx = d/dt(x)
%
% GLAVNA PROGRAMA
% SUL_PrV2_2_fc_nisalo.m
%
clg
%
t0 = 0;
% pocetno vreme
tf = 5;
% krajno vreme
x0 = [1, 0];
% pocetni uslovi
tol = 0.0001;
% tocnost
trace = 0;
% ako ne e nula sekoj cekor se pecati
%
% Integracija na dif. ravenki
[t,x] = ode23('fc_nisalo',t0,tf,x0,tol,trace);
%
% Crtanje na varijablite:
%
subplot(211),plot(t,x), grid

166 MATLAB/SIMULINK

title('Vremenski odgovor na nisalo so krut stap')


xlabel('t [s]')
text(3.2,3.1,'Brzina [m/s]')
text(3.2,1.2,'Agol [rad]')
%
teta = x(:,1);
% Agolna pozicija
dteta = x(:,2);
% Agolna brzina
%
subplot(212),plot(teta, dteta), grid
title('Graf na nisaloto vo faznata ramnina')
xlabel('Agolna pozicija [rad]')
ylabel('Agolna brzina [rad/s]')
%

. 5.1.4. . 5.1.5.
tf.
.
.

. 5.1.4. . 5.1.3. (tf=5 s)

MATLAB/SIMULINK 167

. 5.1.5. . 5.1.3. (tf=20 s)

168 MATLAB/SIMULINK

MATLAB/SIMULINK 169

6. (CONTROL SYSTEM TOOLBOX)


6.1.
.

p( s ) s 3 2s 5
.
MATLAB-, -

.
>> p = [ 1 0 -2 -5]
p =
1
0
-2

-5


.
:

1.
2.
3.
4.
5.
6.
7.

conv
deconv
poly
roots
polyval
polyder
polifit

8.
9.

polivalm
residue










()

6.1.1.
roots ..
.
, roots
:
>> p = [ 1 0 -2 -5]
>> r = roots(p)

170 MATLAB/SIMULINK

r =
2.0946
-1.0473 + 1.1359i
-1.0473 - 1.1359i

6.1.1.: Najdi gi korenite na polinomot:

s 6 9s 5 31.25s 4 61.25s 3 67.75s 2 14.75s 15


Matlab re{enie:
>> p2=[1 9 31.25 61.25 67.75 14.75 15];
>> r = roots(p2)
r=
-4.0000
-3.0000
-1.0000 + 2.0000i
-1.0000 - 2.0000i
0.0000 + 0.5000i
0.0000 - 0.5000i
, -. roots poly.
.
>> r =[2.0946, -1.0473 + 1.1359i, -1.0473 - 1.1359i];
>> p1 = poly(r)
p1 =
1.0000

-0.0000

-2.0000

-5.0000

6.1.2.
>> r2 = [ -4.0, -3.0, -1.0 + 2.0i, -1.0 - 2.0i, 0.0 + 0.5i, 0.0 - 0.5000i];
>> p2 = poly(r2)
p2 =
1.0000 9.0000 31.2500 61.2500 67.7500 14.7500 15.0000
roots poly (
) . poly

roots
[9]

MATLAB/SIMULINK 171

>> A = [ -5.0 -52.06; 0.092


>> p3 = poly(A)
>> r3 = roots(p3)
p3 =
1.0000
r3 =
-3.3683
-2.0647

5.4330

-0.433];

6.9545

6.1.2
polyval
. o

p( x) x 3 2 x 5 x = 5 :
>> p = [1 0 -2 -5];
>> polyval(p,5)
ans =
110
:
>> x = [ 0.1 0.5 1 2 3 4 5];
>> v = polyval(p,x)
v =
-5.1990
-5.8750
51.0000 110.0000

-6.0000

-1.0000

16.0000

x.
6.1.3 () ( )
. conv deconv.
:

a(s) s 2 2s 3 i b(s) 4s 2 5s 6
>> a = [ 1 2 3]; b = [ 4 5 6];
>> c = conv(a,b)
c =
4

13

28

27

18

172 MATLAB/SIMULINK

- c
a b.
>> [q,r] = deconv(c,a)
q =
4

r =
0

- r .

q.

6.1.4.
()
:

H s

Bs bm s m bm1s m1 ... b1s b0

.
As an s n an1s n1 ... a1s a0

(1)

m n ,

H (s) (proper) ,
m n H (s) (improper)

.
p1, p2 , ..., pn

A(s) :
As an s p1 s p2 ...s pn ,

(2)

H (s) :
H s

Bs
Bs
n
,

As
a s p1 s p2 ...s pn

(3)

H (s)

residue
.

MATLAB/SIMULINK 173


p1 , p2 , ... , pn ,

H s

H (s)

Kk
Kn
K1
K2

...
...
,
s p1 s p2
s pk
s pn

K k ()
residue:
[k,p]=residue(b,a)
: k [ K1, K 2 , ..., K n ]
p [ p1, p2 , ..., pn ] .
6.1.3:

H ( s)

s2
s 4s 2 3s
3

>> b = [1 2];
>> a = [1 4 3 0];
>> [k,p] = residue(b,a)
k=
-0.1667
-0.5000
0.6667
p=
-3
-1
0

1
2
1
0.1667 0.5000 0.6667
H ( s)

6 2 3
s (3) s (1)
s0
s 3 s 1 s

H ( s)
>> b = [1 -2 1];
>> a = [1 3 4 2];
>> [k,p = residue(b,a)
k=
-1.5000 + 2.0000i
-1.5000 - 2.0000i
4.0000

s 2 2s 1
s 3 3s 2 4s 2

(4)

174 MATLAB/SIMULINK

p=
-1.0000 + 1.0000i
-1.0000 - 1.0000i
-1.0000

H (s)

1.5 j 2 1.5 j 2
4

s 1 j
s 1 j
s 1


m n H (s)

H ( s)

B( s)
A( s)

H s

Kp
K p 1
Kn
K1
K2

...
...
.....
,
2
p
s p1 ( s p1 )
s p p 1
s pn
( s pk )

6.1.4:

H ( s)

5s 1
s 3s 2
3

>> b = [5 -1];
>> a = [1 0 -3 -2];
>> [k,p] = residue(b,a)
k=
1.0000
-1.0000
2.0000
p=
2.0000
-1.0000
-1.0000

H ( s)

1
1
2

s 2 s 1 ( s 1) 2

(5)

MATLAB/SIMULINK 175

H ( s)

B( s)
A( s)

B(s)

A(s), (m n).
, H (s) :

H ( s ) Q( s )

R( s )
A( s)

Q(s ) m n R(s)
n . R(s) / A(s)
.

e residue :
[k,p,q]=residue(b,a). k [ K1, K 2 , ..., K n ] ,
() p [ p1, p2 , ..., pn ]
q [q1 , q2 , ... , qnm ] .
6.1.5.:

H (s)

s 3 2s 4
s 2 4s 2

>> b = [1 0 2 -4];
>> a = [1 4 -2];
>> [k,p,q] = residue(b,a)
k=
20.6145
-0.6145
p=
-4.4495
0.4495
q=
1 -4

H ( s) s 4

20.6145
0.6145

s 4.4495 s 0.4495

176 MATLAB/SIMULINK


k [ K1, K 2 , ..., K n ] ,

p [ p1, p2 , ..., pn ] q [q1, q2 , ..., qn m ]


[b,a] = residue(k,p,q)
H ( s)

B( s)
.
A( s)

6.1.6. :
>> k = [20.6145 -0.6145];
>> p = [ -4.4495 0.4495];
>> q = [ 1 -4];
>> [b,a] = residue(k,p,q)
b=
1.0 0.0 2.0 -4.0
a=
1.0 4.0 -2.0

H ( s)

s 3 2s 4
s 2 4s 2

MATLAB/SIMULINK 177

6.2.
LTI :
laplace, tf, ss , pole, zero, o: s z
6.2.1 laplace
laplace
.
.
6.2.1:
:

x e2t , x a sin(t ), x a sin(t ) b cos(t )


__________________
% Laplace_1.m
clear; pack; clc
syms x t
x = exp(-2*t);
X = laplace(x)
:
X=
1/(s+2)
(syms )
___________________
% Laplace_2.m
clear; pack; clc
syms x t a w
x = a*sin(w*t);
X = laplace(x)
X=
a*w/(s^2+w^2)
__________________
% Laplace_3.m
clear; pack; clc
syms x t a b w
x = a*sin(w*t)+b*cos(w*t);
X = laplace(x)
X=
a*w/(s^2+w^2)+b*s/(s^2+w^2)

178 MATLAB/SIMULINK

LTI (Linear Time Invariant) :


6.2.2 tf
:
:
G=tf(num,den)
: num
G(s) ;
den
G(s)
6.2.2.
G( s)

2s 3
s 4s 1
2

>> G=tf([2 3],[1 4 1])


:
Transfer function:
2s+3
------------s^2 + 4 s + 1
6.2.3.
G ( s )
:

5s
s 2
2

>> num=[5 0]; den=[1 0 2];


>> G=tf(num,den)
:
Transfer function:
5s
------s^2 + 2
:
:
G=tf(num,den,)
: num
G(z ) ;
den
G(z )

MATLAB/SIMULINK 179

6.2.4.
G ( z )
:

1
z 0.2 z 0.2
2

>> G=tf([1],[1 -0.2 0.2], 2)


:
Transfer function:
1
----------------z^2 - 0.2 z + 0.2
Sampling time: 2
6.2.5.
G ( z )
:

z2

z

>> num = [1 2]; den = [1 0]; Ts=1;


>> G = tf(num, den,Ts)
:
Transfer function:
z+2
----z
Sampling time: 1
6.2.3 s z

: s=tf(s); G=(bm*s^m++b1*s+b0)/(an*s^n++a1*s+a0);
6.2.6.
G( s)
>> s=tf('s');
>> G=(2*s+3)/(s^2+4*s+1)
:
Transfer function:
2s+3
------------s^2 + 4 s + 1

2s 3
s 4s 1
2

180 MATLAB/SIMULINK


: z=tf(s, Ts); G=(bm*z^m++b1*z+b0)/(an*z^n++a1*z+a0);
6.2.6.
G ( s)
:
>> z=tf('z',1);
>> G=(z+1)/(z^2-0.2*z+0.2)

z 1
z 0.2 z 0.2
2

:
Transfer function:
z+1
----------------z^2 - 0.2 z + 0.2
6.2.4 ss
ss state-space ( )
LTI .
:
sys = ss(A,B,C,D)
(a continuous-time state-space (ss) model) SYS A,B,C,D.
SYS ss . D=0
.
sys = ss ss .
:
:

G=ss(A,B,C,D)
A, B, C, D :

x Ax Bu
y Cx Du

6.2.7: :

x1 1 2 x1 0
x 0 1 x 1u
2
2
x
y 1 0 1
x2
Matlab :
>> A=[-1 2; 0 -1]; B=[0; 1]; C=[1 0]; D=[0];
>> G=ss(A,B,C,D)

MATLAB/SIMULINK 181

:
a=
x1 x2
x1 -1 2
x2 0 -1
b=
u1
x1 0
x2 1
c=
x1 x2
y1 1 0

6.2.5. pole zero


Slu`at za odreduvawe na polovite i nulite na sistemot.
:
p=pole(G); z=zero(G);
: G e LTI ( tf ss)
p z , .
6.2.8
:

G(s)

2s 3
s 4s 1
2

; : p1,2 2 3 ; : z1

:
>> s=tf('s');
>> G=(2*s+3)/(s^2+4*s+1);
>> p=pole(G)
>> z=zero(G)
:
p=
-3.7321
-0.2679
z=
-1.5000

3
1.5
2

182 MATLAB/SIMULINK

6.3. ODZIVI NA LINEAREN SSTEM


: initial, impulse, step, lsim
6.3.1. Funkcija initial
LTI .
:

x Ax , x(0) x 0
y Cx

:
initial(sys,x0) -
(state-space model) sys ( ss) x0 .
.
initial(sys,x0,tfinal) t=0 t=tfinal.
initial(sys,x0,) T .
0:dt:Tf.
initial(sys1,sys2,...,x0,T) LTI SYS1,SYS2,...
. T .
, ,
: initial(sys1,'r',sys2,'y--',sys3,'gx',x0).
[Y,T,X] = INITIAL(SYS,X0)
Y, T X.
6.3.1:
.6.3.1
:

x1 (t ) 0
x (t ) 1
2
L

1
C
R

L

x1 (t ) 0
x (t ) 1 u (t )
2 L

(1)

x (t )
y (t ) 1 0 1 ,
x2 (t )
: x1 (t ) U C (t ) -

x2 (t ) i(t ) - , :
y(t ) uc (t ) x1 (t ) )
(1) :

MATLAB/SIMULINK 183

x (t ) Ax(t ) Bu (t )
y (t ) Cx(t ) Du (t )

(2)

A , B , C D :

0
A
1

1
C 0 1000 ,

R
10 10
L

0 0
B 1 , C [1 0], D [0] .
10
L

(3)

, , U c (t ) ,
U c (0) 5V , x(0) x0 [5 0]T .
L

DC

U (t )

i (t )

U c (t )

.6.3.1. - RLC
: R 4, L 0.10 H , C 1000 F
m- mt_p631_elsist_1.m,
.6.3.2.
% Primer 6.3.1 Elektricno kolo
% File: mt_p631_elsist_1.m
%
A=[0 1000;-10 -40]; B=[0;10]; C=[1 0]; D=[0];
G=ss(A,B,C,D);
x0=[5 0];
initial(G,x0),grid

Sl.6.3.2. 6.3.1

184 MATLAB/SIMULINK

6.3.2:

G( s)

s 2s 1
: x(0) [1 3]T .
2

(1)

.
% Primer 6.3.2
% File: mt_p63_02_initial.m
%
num = [1];
den = [1 2 1];
[A,B,C,D] = tf2ss(num,den);
x0=[1 3];
initial(A,B,C,D,x0), grid

Sl.6.3.3. 2
6.3.2. Funkcija IMPULSE
LTI .
:
IMPULSE(SYS) LTI SYS ( tf, zpk,
SS). .
.
IMPULSE(SYS,TFINAL) t=0
t=TFINAL.
IMPULSE(SYS,T) T .

MATLAB/SIMULINK 185

IMPULSE(SYS1,SYS2,...,T) LTI
SYS1,SYS2,... , .
, :
impulse(sys1,'r',sys2,'y--',sys3,'gx').
, [Y,T] = IMPULSE(SYS)
Y T .
6.3.3:
% Primer 6.3.3
% File: mt_p63_03_initial.m
A=[-1 2;0 -1]; B=[0;1];
C=[1 0]; D=[0];
impulse(A,B,C,D), grid;

.6.3.4.
6.3.3.
6.3.4
% Primer 6.3.4
% File: mt_p63_04_initial.m
% Elektricno kolo - odziv na impuls
% File: elsist_2_impulse.m
A=[0 1000;-10 -40]; B=[0;10]; C=[1 0]; D=[0];
sys=ss(A,B,C,D);
impulse(sys), grid

.6.3.5. 6.3.4

186 MATLAB/SIMULINK

6.3.5.
e :
% Primer 6.3.5 MIMO sistem - odziv na impuls
% File: mt_p63_05_MIMO_sistem_1_impulse.m
A=[-3 1; 1 -3];
B=[1 2; 2 1];
C=[1 0; 0 1];
D=[0 0; 0 0];
sys=ss(A,B,C,D);
impulse(sys), grid

.6.3.6. 6.3.5
, t impulse

. , .
6.3.6:

(R.Nelson: Flight Srtability and Automatic
Control, Second Edition, page 369):

1.0 0.027
0.334
q 2.52 0.387 q 2.6 m

x1
x2 q .

MATLAB/SIMULINK 187

u m . .
.
m .
a) Koristena opcija impulse(sys),
% Primer 6.3.6
% Model na avion - odziv na impuls
% File: mt_p63_06_avion_odziv_impulse.m
A=[-0.334 1.0; -2.52 -0.387];
B=[-0.027; -2.6];
C=[1 0; 0 1];
D=[0; 0];
sys=ss(A,B,C,D);
impulse(sys), grid
.6.3.7 ).

a)

b)
.6.3.7. - 6.3.6

b) Koristena opcija impulse(sys,t),


% Primer 6.3.6b
% Model na avion - odziv na impuls
% Se koristi opcijata impulse(sys,t)
% File: mt_p63_06b_avion_odziv_impulse_sys_t.m
A=[-0.334 1.0; -2.52 -0.387];
B=[-0.027; -2.6];
C=[1 0; 0 1];
D=[0; 0];
t=0:0.01:10;
sys=ss(A,B,C,D);
impulse(sys,t), grid
.6.3.7 ).

188 MATLAB/SIMULINK

6.3.3. Funkcija STEP


step .
:

step(G)

: G e LTI ( tf ss).
6.3.7
:

G( s)

s 1
s s 1
2

step(G) .6.3.8
m-file:
% Primer 6.3.7
% Odziv na step
% File: mt_p63_07_step_1.m
s=tf('s');
G =(s+1)/(s^2+s+1);
step(G), grid

.6.3.8.

6.3.8. 6.3.6.
m file:
% Primer 6.3.6
% Model na avion - odziv na step
% File: mt_p63_08_step_2_avion.m
A=[-0.334 1.0; -2.52 -0.387];
B=[-0.027; -2.6];
C=[1 0; 0 1];
D=[0; 0];
sys=ss(A,B,C,D);

.6.3.9. a

MATLAB/SIMULINK 189

step :
[y,t] = step(G);
t y
, ;
.
step :
y=step(g,t);
t
,
y.
6.3.9
G ( s )

s 1
.
s s 1
2

y=step(G,t):
m-file:
% Primer6.3.9
% Odziv na step
% File: mt_p63_09_step_3.m
s=tf('s');
G =(s+1)/(s^2+s+1);
t=0:0.1:25;
y=step(G,t);
plot(t,y),grid

.6.3.10. step

6.3.4. Funkcija lsim


lsim LTI a
.
:
y=lsim(G,u,t);
: G e LTI
tf ss;
t

u
t .

190 MATLAB/SIMULINK

6.3.10.

G( s)

Y ( s)
s 1
2
U ( s) s s 1

(1)

.6.3.11.
.
u(t) y(t).
U(t)
1

20

40

60

80

100

-1

.6.3.11.
% Primer 6.3.10.
% Odziv na proizvolna pobuda
% File: mt_p63_10_lsim_1.m
%
s=tf('s');
G=(s+1)/(s^2+s+1);
t=0:0.1:100;
for i=1:length(t)
u(i)=0;
if (t(i)>=20)&(t(i)<40)
u(i)=1;
end
if (t(i)>=60)&(t(i)<80)
u(i)=-1;
end
end
y=lsim(G,u,t);
plot(t,u,'b'),grid; hold on;
plot(t,y,'r'); hold off;

.6.3.12. .6.3.11

.6.3.12
(1) , .

MATLAB/SIMULINK 191

6.3.11. ( )
% Primer 6.3.11.
% Odziv na proizvolna pobuda
% File: mt_p63_11_lsim_2.m
%
Ta=1;
G=tf)[1], [1 -0.2 0.2], Ts);
t=0:Ts:50;
for i=1:length(t)
u(i)=0;
if (t(i)>=10)&(t(i)<30)
u(i)=1;
end
if (t(i)>=25)&(t(i)<35)
u(i)=1;
end
end
y=lsim(G,u,t);
plot(t,u,'b-o'); hold on;
plot(t,y,'r-o'); hold off;
grid;

.6.3.13

6.3.13.
:

G( z )
.

1
z 0.2 z 0.2
2

lsim za LTI
o :
[Y,T,X] = LSIM(SYS,U,T,X0)
X, LENGTH(T)
.
A, B, C D,
SYS=SS(A,B,C,D),
:
[Y,T,X] = LSIM(A,B,C,D,U,T,X0).
plot .

192 MATLAB/SIMULINK

6.3.12

:

1.0 0.027
0.334
q 2.52 0.387 q 2.6 m

y1 1 0 0
y 0 1 q 0 m

2
x1
x2 q .
u m .
, 6.3.8 (.6.3.9), 0.1
.
m file:
% Primer 6.3.12
% Model na avion - odziv na proizvolen signal
% File: mt_63_12_lsim_2_avion.m
%
% Matrici na sistemot
A=[-0.334 1.0; -2.52 -0.387]; B=[-0.027; -2.6];
C=[1 0; 0 1]; D=[0; 0];
%
% Vrema za simulacija
t=0:0.1:100;
%
% pocetni uslovi
x0=[0 0];
%
% Generiranje na vlezniot signal
for i=1:length(t)
u(i)=0;
if (t(i)>=20)&(t(i)<40)
u(i)=0.1;
end
if (t(i)>=60)&(t(i)<80)
u(i)=-0.1;
end
end
% Simulacija
[y,x] = lsim(A,B,C,D,u,t,x0); % ili sys=ss(A,B,C,D); [y,x] = lsim(sys,u,t,x0)
% Zna;ewe na varijablite:
alfa = x(:,1); % napaden agol
q
= x(:,2); % agolna brzina na izvisuvanje

MATLAB/SIMULINK 193

deltam = u; % otklon na kormiloto za visina


%
% Crtanje
figure(1)
plot(t,alfa,t,deltam,'r --'),grid, xlabel('t [s]'), ylabel('alfa [rad], deltam [rad]'),...
title('Odziv na napadniot agol alfa na pobuda deltam=u(t)'),legend('alfa','deltam')
figure(2)
plot(t,q,t,deltam,'r --'),grid, xlabel('t [s]'), ylabel('q [rad/s], deltam [rad]'),...
title('Odziv na agolnata brzina q na pobuda deltam=u(t)'),legend('q','deltam')

.6.3.14. .6.3.11

.6.3.15.
.6.3.11.

194 MATLAB/SIMULINK

6.4 LTI
6.4.1. bode
:
:

bode(G)
G LTI tf
ss

6.4.1
:

G( s)

1000( s 1)
( s 0.1)(s 100)

(1)

bode(G) ja .6.4.1, m-:


% Primer 6.4.01
% Frekvenciski karaktristiki na LTI sistemi
% File: mt_p64_01_bode_1.m
%
% Prensna funkcija na sistemot
s=tf('s');
G=1000*(s+1)/((s+0.1)*(s+100));
bode(G), grid

.6.4.1
(1)

MATLAB/SIMULINK 195

bode :
[m,ph]=bode(G,w);
w
, m ph,
; () m
.
6.4.2
% Primer 6.4. 2
% Frekvenciski karaktristiki na LTI sistemi
% Se koristi bode funkcija vo forma: [m,ph]=bode(G,w)
% File: mt_p64_02_bode_2.m
%
% Prenosna funkcija na sistemot
s=tf('s');
G=10/(s^2+2*s+10);
w=0:0.1:20;
%
[mag,ph]=bode(G,w);
m(1,:)=mag(1,1,:);
p(1,:)=ph(1,1,:);
%
subplot(2,1,1); plot(w,m); grid;
xlabel('Frekvencija [rad/s]');
ylabel('Amplituda');
%
subplot(2,1,2); plot(w,p); grid;
xlabel('Frekvencija [rad/s]');
ylabel('Faza [stepeni]');

.6.4.2.
G=10/(s^2+2*s+10);

196 MATLAB/SIMULINK

6.4.3

bode(num,den).
% Primer 6.4.3
% Bode dijagram so komandata bode(num,den)
% File: mt_p64_03_bode_num_den.m
%
clc
clear all
clf
num=10;
den=[1 5 5 0];
bode(num,den);
grid;

.6.4.3
G=10/(s^3+5*s^2+5*s);
6.4.4

:
bode(num,den,w).
% Primer 6.4.4
% Bode dijagram so komandata bode(num,den,w)
% File: mt_p64_04_bode_num_den_w.m

MATLAB/SIMULINK 197

%
clc
clear all
clf
num=10;
den=[1 5 5 0];
w=logspace(-1, 4, 100);
bode(num,den,w);
grid;

.6.4.4
G=10/(s^3+5*s^2+5*s)
6.4.2. margin
margin o
(gain and phase margins) [vidi SAU: Z.Vukic, S.Milic].
(gain margin) Gm [dB]
, ..

Gm

1
G ( jcg )

G( jcg ) cg
180 deg . :
Gm_dB = 20*log10(Gm)

198 MATLAB/SIMULINK


cp .

Pm 180 (cp )

( Gm 0, Pm 0 ) , .
[Gm,Pm,Wcg,Wcp] = MARGIN(SYS) Gm, Pm,
: Wcg Wcp SISO SYS ,
.
6.4.5

, G=10/(s^3+5*s^2+5*s), .
.:
% Primer 6.4.5
% Odreduvanje na amplitudnoto i faznoto osiguruvanje
% File: mt_p64_05_margin_num_den.m
clc
clear all
clf
num=10;
den=[1 5 5 0];
margin(num,den);
grid;

6.4.5. 6.4.5

MATLAB/SIMULINK 199

6.4.3. nichols
nichols(num,den).
.
% Primer 6.4.6
% Odreduvanje na Nikolsovite dijagrami
% File: mt_p64_06_nichols_num_den.m
%
clc
clear all
clf
num=10;
den=[1 5 5 0];
nichols(num,den);
ngrid;
ngrid.

.6.4.6.

6.4.4. nyquist
nyquist .
nyquist(num,den) e 6.4.7.
nyquist(num,den) . .6.4.7
-.

200 MATLAB/SIMULINK

.6.4.7
,
.:
% Primer 6.4.8
% Nyquist-ov dijagram
% File: mt_p64_08_nyquist_num_den_w.m
%
clc
clear all
clf
num=10;
den=[1 5 5 0];
w=0:0.1:100;
[re,im,w]=nyquist(num,den,w);
plot(re,im);
grid

.6.4.8

.

MATLAB/SIMULINK 201

6.5 -
(ROOT LOCUS)
- (root locus)
.
.
rlocus(num,den) num den
.
.6.5.1.

R(s )

E (s )
K

G(s)

num
den

Y (s )

.6.5.1

G( s)

num
1
3
den s 5s 2 5s

(1)

6.5.1
% Primer 6.5.1
% Geometrisko mesto na
% korenite GMK (root locus)
% File: mt_p65_01_rlocus_1.m
%
clc
clear all
clf
num=1;
den=[1 5 5 0];
rlocus(num,den);

.6.5.2 .6.5.1
.6.5.2 0

.
,
6.5.2 .

202 MATLAB/SIMULINK

6.5.2
0 100.
% Primer 6.5.2
% Geometrisko mesto na
% korenite GMK (root locus)
% File: mt_p65_02_rlocus_2.m
%
clc
clear all
clf
K=0:1:100;
num=1;
den=[1 5 5 0];
rlocus(num,den,K);

.6.5.3 .6.5.1 =0:1:100

.6.5.3 .6.5.1 =0:1:100


6.5.3

% Primer 6.5.3
% Geometrisko mesto na
% korenite GMK (root locus)
% File: mt_p65_03_rlocus_3.m
%
clc
clear all
clf
k1=0:2:10;
k2=11:0.5:90;
k3=91:10:500;
K=[k1 k2 k3];
num=1;
den=[1 5 5 0];
rlocus(num,den,K);

.6.5.3 .6.5.1

MATLAB/SIMULINK 203

6.6. Matlab Commands List


The following list of commands can be very useful for future reference. Use "help" in
Matlab for more information on how to use the commands.
In these tutorials, we use commands both from Matlab and from the Control Systems
Toolbox, as well as some commands/functions which we wrote ourselves. For those
commands/functions which are not standard in Matlab, we give links to their
descriptions. For more information on writing Matlab functions, see the function page.
Note:Matlab commands from the control system toolbox are highlighted in red.
Non-standard Matlab commands are highlighted in green.
Command

Description

abs

Absolute value

acker

Compute the K matrix to place the poles of A-BK, see also place

axis

Set the scale of the current plot, see also plot, figure

bode

Draw the Bode plot, see also logspace, margin, nyquist1

c2dm

Continuous system to discrete system

clf

Clear figure (use clg in Matlab 3.5)

conv

Convolution (useful for multiplying polynomials), see also deconv

ctrb

The controllability matrix, see also obsv

deconv

Deconvolution and polynomial division, see also conv

det

Find the determinant of a matrix

dimpulse

Impulse response of discrete-time linear systems, see also dstep

dlqr

Linear-quadratic requlator design for discrete-time systems, see also


lqr

dlsim

Simulation of discrete-time linear systems, see also lsim

dstep

Step response of discrete-time linear systems, see also stairs

eig

Compute the eigenvalues of a matrix

eps

Matlab's numerical tolerance

feedback

Feedback connection of two systems.

figure

Create a new figure or redefine the current figure, see also subplot, axis

for

For, next loop

format

Number format (significant digits, exponents)

function

Creates function m-files

grid

Draw the grid lines on the current plot

gtext

Add a piece of text to the current plot, see also text

204 MATLAB/SIMULINK

help

HELP!

hold

Hold the current graph, see also figure

if

Conditionally execute statements

imag

Returns the imaginary part of a complex number, see also real

impulse

Impulse response of continuous-time linear systems, see also step,


lsim, dlsim

input

Prompt for user input

inv

Find the inverse of a matrix

jgrid

Generate grid lines of constant damping ratio (zeta) and settling


time (sigma), see also sgrid, sigrid, zgrid

legend

Graph legend

length

Length of a vector, see also size

linspace

Returns a linearly spaced vector

lnyquist1 Produce a Nyquist plot on a logarithmic scale, see also nyquist1


log

natural logarithm, also log10: common logarithm

loglog

Plot using log-log scale, also semilogx/semilogy

logspace

Returns a logarithmically spaced vector

lqr

Linear quadratic regulator design for continuous systems, see also


dlqr

lsim

Simulate a linear system, see also step, impulse, dlsim.

margin

Returns the gain margin, phase margin, and crossover frequencies, see
also bode

norm

Norm of a vector

nyquist1

Draw the Nyquist plot, see also lnyquist1. Note this command was
written to replace the Matlab standard command nyquist to get
more accurate Nyquist plots.

obsv

The observability matrix, see also ctrb

ones

Returns a vector or matrix of ones, see also zeros

place

Compute the K matrix to place the poles of A-BK, see also acker

plot

Draw a plot, see also figure, axis, subplot.

poly

Returns the characteristic polynomial

polyadd

Add two different polynomials

polyval

Polynomial evaluation

print

Print the current plot (to a printer or postscript file)

pzmap

Pole-zero map of linear systems

rank

Find the number of linearly independent rows or columns of a matrix

MATLAB/SIMULINK 205

real

Returns the real part of a complex number, see also imag

rlocfind

Find the value of k and the poles at the selected point

rlocus

Draw the root locus

roots

Find the roots of a polynomial

rscale

Find the scale factor for a full-state feedback system

set

Set(gca,'Xtick',xticks,'Ytick',yticks) to control the number and spacing of


tick marks on the axes

series

Series interconnection of Linear time-independent systems

sgrid

Generate grid lines of constant damping ratio (zeta) and natural


frequency (Wn), see also jgrid, sigrid, zgrid

sigrid

Generate grid lines of constant settling time (sigma), see also jgrid, sgrid,
zgrid

size

Gives the dimension of a vector or matrix, see also length

sqrt

Square root

ss

Create state-space models or convert LTI model to state space, see


also tf

ss2tf

State-space to transfer function representation, see also tf2ss

ss2zp

State-space to pole-zero representation, see also zp2ss

stairs

Stairstep plot for discreste response, see also dstep

step

Plot the step response, see also impulse, lsim, dlsim.

subplot

Divide the plot window up into pieces, see also plot, figure

text

Add a piece of text to the current plot, see also title, xlabel, ylabel, gtext

tf

Creation of transfer functions or conversion to transfer function, see


also ss

tf2ss

Transfer function to state-space representation, see also ss2tf

tf2zp

Transfer function to Pole-zero representation, see also zp2tf

title

Add a title to the current plot

wbw

Returns the bandwidth frequency given the damping ratio and the rise or
settling time.

xlabel/ylabel

Add a label to the horizontal/vertical axis of the current plot, see also
title, text, gtext

zeros

Returns a vector or matrix of zeros

zgrid

Generates grid lines of constant damping ratio (zeta) and natural


frequency (Wn), see also sgrid, jgrid, sigrid

zp2ss

Pole-zero to state-space representation, see also ss2zp

zp2tf

Pole-zero to transfer function representation, see also tf2zp

206 MATLAB/SIMULINK

7.
1.

The MathWorks, Inc. MATLAB, Using MATLAB, Natick, 1999.

2.

The MathWorks, Inc. MATLAB, Using MATLAB Graphics, Natick, 1999.

3.

Matlab na straneh podjetja MathWorks,


http://www.mathworks.com/products/matlab/

4.

M. Saje, D. Zupan, Kinematika in dinamika, http://www.km.fgg.unilj.si/predmeti/KiD/KD.htm

5.

MATLAB Educational Sites, http://www.eece.maine.edu/mm/matweb.html

6.

University of New Hampshire: MATLAB-tutorial,


http://spicerack.sr.unh.edu/mathadm/tutorial/software/matlab/

7.

University of Florida: MATLAB Tutorial, http://www.math.ufl.edu/help/matlabtutorial/

8.

Jordanovski, Lj. Osnovi na MATLAB so primeri, upatstvo, PMF Skopje

9.

L. Kalaan, M. Petkovska: MATLAB i dodatni moduli Controls Szstem Toolbox


and Simulink, Verzija 4.2. za Windows, Beograd,

10. Hadi Saadat: Computational Aids in Control Systems Using Matlab, McGraw-Hill,
Inc. New York, 1993.
11. Hadi Saadat: Power System Analysis, Second edition, McGraw-Hll Primis, New
York, 2002.
12. Delores M. Etter: Engineering Problem Solving with Matlab, Second Edition,
Prentice Hall, Inc, New Jersey, 1993.
13. Norman S. Nise: Control Systems Engineering, Third Edition, John Wiley and
Sons, Inc, New York, 2000.
14. Richard C. Dorf, Robert H. Bishop: Modern Control Systems, Ninth Edition,
Prentice Hall Inc. 2001.
15. Robert H. Bishop: Modern Control Systems Analisys and Design Using MATLAB,
Addison Weley Publishing Company, New York, 1993.
16. Robert H. Bishop: Modern Control Systems Analisys and Design Using MATLAB
and SIMULINK, Addison Weley Longman, Inc. Menlo Park, California, 1997.
17. SIMULINK Dynamic System Simulation for MATLAB Using SIMULINK
Version 3, The Math Works, Inc. 1999. (http://www.mathworks.com )
18. Control Tutorials for Matlab, Carnegie Melon, University of Michigan,
http://www.engin.umich.edu/group/ctm/
19. , .: ,
2006/2007, , , 2007..
20. Stoji, M.: Sistemi automatskog upravljanja, Elektronski fakultet, Ni, 2004.
21. Vuki Z., Kuljaa Lj.: Automatsko upravljanje - analiza linearnih sustava,
Kigen d.o.o. Zagreb. 2005.

MATLAB/SIMULINK 207

8.
A: HELP
MatLab-,
help,
.
help
HELP topics:
matlab\general
matlab\ops
matlab\lang
matlab\elmat
matlab\elfun
matlab\specfun
matlab\matfun
matlab\datafun
matlab\polyfun
matlab\funfun
matlab\sparfun
matlab\graph2d
matlab\graph3d
matlab\specgraph
matlab\graphics
matlab\uitools
matlab\strfun
matlab\iofun
matlab\timefun
matlab\datatypes
matlab\winfun
matlab\demos

- General purpose commands.


- Operators and special characters.
- Programming language constructs.
- Elementary matrices and matrix manipulation.
- Elementary math functions.
- Specialized math functions.
- Matrix functions - numerical linear algebra.
- Data analysis and Fourier transforms.
- Interpolation and polynomials.
- Function functions and ODE solvers.
- Sparse matrices.
- Two dimensional graphs.
- Three dimensional graphs.
- Specialized graphs.
- Handle Graphics.
- Graphical user interface tools.
- Character strings.
- File input/output.
- Time and dates.
- Data types and structures.
- Windows Operating System Interface Files DDE/ActiveX)
- Examples and demonstrations.

For more help on directory/topic, type "help topic".


help general
General purpose commands.
MATLAB Toolbox Version 5.2 18-Dec-1997
General information
help
- On-line help, display text at command line.
helpwin
- On-line help, separate window for navigation.
helpdesk
- Comprehensive hypertext documentation and roubleshooting.
demo
- Run demonstrations.
ver
- MATLAB, SIMULINK, and toolbox version information.

208 MATLAB/SIMULINK

whatsnew - Display Readme files.


Readme
- What's new in MATLAB 5.1
Managing the workspace.
who
- List current variables.
whos
- List current variables, long form.
clear
- Clear variables and functions from memory.
pack
- Consolidate workspace memory.
load
- Load workspace variables from disk.
save
- Save workspace variables to disk.
quit
- Quit MATLAB session.
Managing commands and functions.
what
- List MATLAB-specific files in directory.
type
- List M-file.
edit
- Edit M-file.
lookfor
- Search all M-files for keyword.
which
- Locate functions and files.
pcode
- Create pre-parsed pseudo-code file (P-file).
inmem
- List functions in memory.
mex
- Compile MEX-function.
Managing the search path
path
- Get/set search path.
addpath
- Add directory to search path.
rmpath
- Remove directory from search path.
editpath
- Modify search path.
Controlling the command window.
echo
- Echo commands in M-files.
more
- Control paged output in command window.
diary
- Save text of MATLAB session.
format
- Set output format.
Operating system commands
cd
- Change current working directory.
copyfile
- Copy a file.
pwd
- Show (print) current working directory.
dir
- List directory.
delete
- Delete file.
getenv
- Get environment variable.
mkdir
- Make directory.
!
- Execute operating system command (see PUNCT).
dos
- Execute DOS command and return result.
unix
- Execute UNIX command and return result.
vms
- Execute VMS DCL command and return result.
web
- Open Web browser on site or files.
computer
- Computer type.

MATLAB/SIMULINK 209

help elmat
Elementary matrices and matrix manipulation.
Elementary matrices.
zeros
- Zeros array.
ones
- Ones array.
eye
- Identity matrix.
repmat
- Replicate and tile array.
rand
- Uniformly distributed random numbers.
randn
- Normally distributed random numbers.
linspace
- Linearly spaced vector.
logspace
- Logarithmically spaced vector.
meshgrid
- X and Y arrays for 3-D plots.
:
- Regularly spaced vector and index into matrix.
Basic array information.
size
- Size of matrix.
length
- Length of vector.
ndims
- Number of dimensions.
disp
- Display matrix or text.
isempty
- True for empty matrix.
isequal
- True if arrays are identical.
isnumeric
- True for numeric arrays.
islogical
- True for logical array.
logical
- Convert numeric values to logical.
Matrix manipulation.
reshape
- Change size.
diag
- Diagonal matrices and diagonals of matrix.
tril
- Extract lower triangular part.
triu
- Extract upper triangular part.
fliplr
- Flip matrix in left/right direction.
flipud
- Flip matrix in up/down direction.
flipdim
- Flip matrix along specified dimension.
rot90
- Rotate matrix 90 degrees.
:
- Regularly spaced vector and index into matrix.
find
- Find indices of nonzero elements.
end
- Last index.
sub2ind
- Linear index from multiple subscripts.
ind2sub
- Multiple subscripts from linear index.
Special variables and constants.
ans
- Most recent answer.
eps
- Floating point relative accuracy.
realmax
- Largest positive floating point number.
realmin
- Smallest positive floating point number.

210 MATLAB/SIMULINK

pi
i, j
inf
NaN
isnan
isinf
isfinite
flops
why

- 3.1415926535897....
- Imaginary unit.
- Infinity.
- Not-a-Number.
- True for Not-a-Number.
- True for infinite elements.
- True for finite elements.
- Floating point operation count.
- Succinct answer.

Specialized matrices.
compan
- Companion matrix.
gallery
- Higham test matrices.
hadamard
- Hadamard matrix.
hankel
- Hankel matrix.
hilb
- Hilbert matrix.
invhilb
- Inverse Hilbert matrix.
magic
- Magic square.
pascal
- Pascal matrix.
rosser
- Classic symmetric eigenvalue test problem.
toeplitz
- Toeplitz matrix.
vander
- Vandermonde matrix.
wilkinson
- Wilkinson's eigenvalue test matrix.
help isnumeric
ISNUMERIC True for numeric arrays.
ISNUMERIC(A) returns 1 if A is a numeric array and 0 otherwise.
For example, sparse arrays, and double precision arrays are
numeric while strings, cell arrays, and structure arrays are not.
See also ISCELL, ISSTRUCT, ISOBJECT, ISSPARSE.
help find
FIND Find indices of nonzero elements.
I = FIND(X) returns the indices of the vector X that are
non-zero. For example, I = FIND(A>100), returns the indices
of A where A is greater than 100. See RELOP.
[I,J] = FIND(X) returns the row and column indices of
the nonzero entries in the matrix X. This is often used
with sparse matrices.
[I,J,V] = FIND(X) also returns a vector containing the
nonzero entries in X. Note that find(X) and find(X~=0)
will produce the same I and J, but the latter will produce
a V with all 1's.
See also SPARSE, IND2SUB.

MATLAB/SIMULINK 211

help .
a help
.

MatLab-.

You might also like