Professional Documents
Culture Documents
Matlab PDF
Matlab PDF
...........................................................................................
...........................................................11
........................... 11
MATLAB .......................................................
.......................................................13
....................... 13
. MATLAB .............................................................. 13
. MATLAB ......................................................................... 15
. MATLAB ....................................... 15
1) ...........................................................................................................................................15
2) ...........................................................................................................................................16
3) ...........................................................................................................................................17
. MATLAB ........................................... 19
1) M ...............................................................................................................................................19
2) MAT ..........................................................................................................................................20
3) MEX ..........................................................................................................................................21
. MATLAB ........................................... 21
1) .........................................................................................................................21
3) ...........................................................................................................................................25
4) .............................................................................................................................................26
5) .......................................................................................................................26
6) ..............................................................................................................................27
7) MATLAB ..............................................................................................................29
8) ...............................................................................................................................................32
9) MATLAB (Help)...........................................................................................................33
MATLAB .......................................................
.......................................................46
....................... 46
. ................................................................ 46
. ......................................... 48
. ......................................... 48
. ............................................. 52
. ............................................. 55
. ................................................................ 59
. .................................................... 59
2
. .................................................... 60
. ................................................................... 62
. .................................................... 62
. .................................................. 65
. .................................................. 66
. ............................................................................. 67
. .......................................... 67
. ................................................... 69
. ................................................................ 73
. ........................................................ 73
. .................................................... 76
. .................................................. 79
. ........................................................ 80
. ........................................... 81
MATLAB ................................
................................................................
...................................................................
...................................86
... 86
. ................................................................................ 86
. 2 plot................................... 87
. 2 .................................................. 90
. 2 fplot ......................... 92
. 3 .............................................................................. 94
. 3 ....................................... 94
. 3 ...................................................... 97
1) ................................................................................................................................97
2) ...........................................................................................................................................99
. ................................................................101
3
. ....................................... 101
1) .................................................................................................................101
2) ..................................................................................................................................102
3) ............................................................................................................................104
. ................................................ 105
. .................................................... 106
1) ................................................................................................................106
2) .........................................................................................................................................107
.MATLAB
.MATLAB ................................
................................................................
................................................................ 109
. .....................................................................109
. ....................................................... 109
1) for - end ...................................................................................................................................109
2) while-end ................................................................................................................................110
. ....................................................... 111
.
M MATLAB .........................................................112
. M ............................................... 113
. M ................................................... 114
. ...............................................................117
. ...............................................................118
. echo ...................................................... 118
. input, yesinput ........................................... 119
. pause ..................................................... 120
. ................................................................121
. .................................................... 121
. .................................................... 122
. save load ....................................... 123
4
.
.
SIMULINK ................................................................
............................................................................
............................................ 124
. SIMULINK ..................................................124
. ...........................................................................131
. Simulink .................................................. 131
. ..................................................... 132
1) , ...........................................................................................................132
2) ................................................................................................................................133
3) .........................................................................................................................133
4) ............................................................................................................................135
. ...............................................................................136
. (menu) ............. 136
1) ..........................................................................................................137
2) .........................................................................................137
. ....................................... 142
. S
................................................................145
. S
. ........................................... 146
. S
............................................ 147
. S
................................................... 149
1) S
.................................................................................................150
2) M S
.................................................................................................152
3) C (MEX ) S
4) S
. S
.............................................................................154
......................................................................................................................160
................................ 161
.
. ................................................................
.........................................................................
......................................... 164
. ................................................................164
5
. ................................................ 164
1) LTI ...................................................................................................................165
2) LTI ................................................................................................................172
3) .....................................................................................................................172
. ................................................ 175
. ..........................................................177
. ................................................ 177
1) ..................................................................................178
2) .............................................................................................179
3) ..................................................................................................................................182
. ................................................ 186
.......................................................................................................................................186
2) .........................................................................................................................................187
. .....................................................................192
. ................................................ 192
. ................................................... 198
. ................................................. 201
. .........................................................................203
. ................................................. 204
. ..................................................... 205
. LQG ...................................................... 210
.
. MATLAB ................................
.............................................................
............................................................. 217
......................................................................217
(General purpose commands) ............................. 217
1) (General Information)....................................................................................................217
2) (Managing the workspace).....................................................................................218
10
.
,
,
.
.
.
, ,
.
.
.
MATLAB
MATLAB .
MATLAB
.
11
MATLAB
.
12
MATLAB
.
. MATLAB
, (Visualization)
,
GUI(Graph User Interface)
13
MATLAB FORTRAN
,
LINPACK EISPACK . C
.
1
1 10 MATLAB
(OS) .
1
1.
76(1987)
80(1991)
82(1993)
82(1993)
82(1993)
83(1994)
84(1995)
86(1997)
MATLAB
1
11
5
5
86(1997) 5
87(1998) 1
88(1999) 1
89(2000)
MATLAB 3.0
3.5
3.5K
4.0
4.1
4.2
4.2C
5.0
& Simulink 2.0
MATLAB 5.1
& Simulink 2.1
MATLAB 5.2
& Simulink 2.2
MATLAB 5.3
& Simulink 3.0
MATLAB 6.0
DOS
DOS
Windows 3.0
Windows 3.x
Windows 95
Windows 95 &
NT
14
.
. MATLAB
. MATLAB
MATLAB .
1)
WIndows 95 MATLAB 1
1. MATLAB
MATLAB . 33
.
>>A = [1 2 3 ; 4 5 6 ; 7 8 9]
15
( )
(;) . MATLAB
.
A =
1 2 3
4 5 6
7 8 9
>>B = inv(A);
. MATLAB .
(;)
,
>>B = inv(A)
.
A B (Workspace)
.
MATLAB MATLAB (
) .
2)
MATLAB .
MATLAB
. MATLAB (Figure Window) .
Figure No:
.
16
.
.
figure
. File New Figure
.
3)
MATLAB
. MATLAB example
MATLAB .
1) example .
2) example .
3) (Folder) example.mex, example.dll
example.m . MEX
MATLAB .
4) MATLAB (path )
example.mex, example.dll example.m
.
MATLAB path
.
>>path
MATLAB PATH
C:\PROGRAM FILES\MATLAB\toolbox\matlab\general
C:\PROGRAM FILES\MATLAB\toolbox\matlab\ops
C:\PROGRAM FILES\MATLAB\toolbox\matlab\lang
17
C:\PROGRAM FILES\MATLAB\toolbox\matlab\elmat
C:\PROGRAM FILES\MATLAB\toolbox\matlab\elfun
C:\PROGRAM FILES\MATLAB\toolbox\matlab\specfun
C:\PROGRAM FILES\MATLAB\toolbox\matlab\matfun
C:\PROGRAM FILES\MATLAB\toolbox\matlab\datafun
C:\PROGRAM FILES\MATLAB\toolbox\matlab\polyfun
C:\PROGRAM FILES\MATLAB\toolbox\matlab\funfun
C:\PROGRAM FILES\MATLAB\toolbox\matlab\graphics
C:\PROGRAM FILES\MATLAB\toolbox\matlab\uitools
C:\PROGRAM FILES\MATLAB\toolbox\matlab\strfun
C:\PROGRAM FILES\MATLAB\toolbox\matlab\iofun
C:\PROGRAM FILES\MATLAB\toolbox\matlab\timefun
C:\PROGRAM FILES\MATLAB\toolbox\matlab\datatypes
C:\PROGRAM FILES\MATLAB\toolbox\matlab\winfun
C:\PROGRAM FILES\MATLAB\toolbox\matlab\demos
C:\PROGRAM FILES\MATLAB\toolbox\compiler
C:\PROGRAM FILES\MATLAB\toolbox\wavelet\wavelet
C:\PROGRAM FILES\MATLAB\toolbox\lmi\lmictrl
C:\PROGRAM FILES\MATLAB\toolbox\lmi\lmilab
C:\PROGRAM FILES\MATLAB\toolbox\fuzzy\fuzzy
C:\PROGRAM FILES\MATLAB\toolbox\mpc\mpccmds
C:\PROGRAM FILES\MATLAB\toolbox\ncd
C:\PROGRAM FILES\MATLAB\toolbox\nnet\nnet
C:\PROGRAM FILES\MATLAB\toolbox\nnet\nnutils
C:\PROGRAM FILES\MATLAB\toolbox\nnet\nnobsolete
C:\PROGRAM FILES\MATLAB\toolbox\mutools\commands
C:\PROGRAM FILES\MATLAB\toolbox\mutools\subs
C:\PROGRAM FILES\MATLAB\toolbox\signal
18
C:\PROGRAM FILES\MATLAB\toolbox\optim
C:\PROGRAM FILES\MATLAB\toolbox\robust
C:\PROGRAM FILES\MATLAB\toolbox\ident
C:\PROGRAM FILES\MATLAB\toolbox\control
C:\PROGRAM FILES\MATLAB\toolbox\rtw
C:\PROGRAM FILES\MATLAB\toolbox\Simulink\Simulink
C:\PROGRAM FILES\MATLAB\toolbox\Simulink\blocks
C:\PROGRAM FILES\MATLAB\toolbox\Simulink\simdemos
C:\PROGRAM FILES\MATLAB\toolbox\Simulink\dee
C:\PROGRAM FILES\MATLAB\toolbox\tour
C:\PROGRAM FILES\MATLAB\toolbox\local
path MATLAB
.
MATLAB matlabrc.m
( MATLAB .) .
.
. MATLAB
MATLAB
. .
1) M
M m ASCII
(MATLAB M ) .
MATLAB M . M (
19
M ) M ( ).
M MATLAB
MATLAB C FORTRAN
M MATLAB
MATLAB function
.
M MATLAB
.
M MATLAB
M
(global)
(local). M
.
M edit .
poof M
>> edit poof
poof.m .
File New Open
M .
2) MAT
MAT MATLAB 2 . MATLAB
. MATLAB
MATLAB
MAT . MAT
MATLAB .
MAT , ,
20
3) MEX
MEX MATLAB 2 . MEX
MATLAB . MATLAB BASIC
M
MEX . M
MEX .
. MATLAB
MATLAB 1
1 .
1)
MATLAB .
.
MATLAB
.
MATLAB
.
(;)
. [ ] .
>>A = [1, 2, 3; 4, 5, 6; 7, 8, 9]
Enter
.
>>A =
21
1 2 3
4 5 6
7 8 9
>>A = [1 2 3
4 5 6
7 8 9]
Enter .
Enter (
) MATLAB (>>) .
A MATLAB (WorkSpase)
. clear
MATLAB
(MATLAB ) .
>>a = [1, 2, 3]
>>b = [4, 5, 6]?
()
>>b = [4; 5; 6]
.
(, ) .
MATLAB (;) (,)
22
. .
(;) 3 :
.
.
.
(,) 3 :
.
.
.
1]
[ 1
(ans) . ans
.
>>1996/18
ans =
110.889
.
.
=, +, -, * .(
.)
.
, 31
. MATLAB
A a .
MATLAB ,
.
()
.
[ 1
2]
>>S = 1-1/2+1/3-1/4+1/5-1/61/7+1/8;
24
3)
MATLAB
(Permanent variables) . 1-2
.
1
2.
Eps
Pi
inf Inf
NaN
i, j
. PC 2 52 .
3.14159265358979
1
. ( )
0
0
(Not a Number) ,
,0
0
, i = 1 , j = 1
MATLAB clear
. clear
.
IEEE 0
. .
[ 1
3]
>>S = 1/0
Warning:Divide by zero
S =
inf
25
4)
10 .
.
3
-99
0.001
9.456
1.3e-3
4.5e33
10 308
10 308 .
.
* , / , ? , ^
.
.
[ 1
4]
>>x = 2*pi/3+2^3/5-0.3e-3
x =
3.6941
5)
MATLAB i j .
z = 3+4*i z = 3+4*j .
.
>>A = [1, 2; 3, 4]+i*[5, 6; 7, 8]
>>A = [1+5*i 2+6*i ; 3+7*i 4+8*i]
26
i
.
.
1 + 5i( .)
.
[ 1
5] ( )
1.1600-0.0900i
1.1600+0.0900i 1.3700
6)
MATLAB
.
( )
format
. format
27
.
()
.
( ) short g
5 .
.
>>x = [4/3 1.2345e-6]
.
>>format short
1.3333 0.0000
>>format short e
1.3333e+000 1.2345e-006
>>format long
1.33333333333333 0.00000123450000
>>format long e
1.33333333333333e+000 1.2345000000000e-006
>>format short g
1.3333
1.2345e-006
>>format long g
1.33333333333333
1.2345e-006
>>format bank
1.33 0.00
>>format hex
3ff555555555 3eb4b6231abfd271
>>format +
++
format 1
3 .
28
3.
Format
format short
format long
format short e
format long e
format short g
format
5
15
5
15
5
format long g 15
format rat
format hex
16
format +
+, -, , ,
format bank
() ,
format compact
. .
format loose
.
3.1416
3.14159265358979
3.1416e+00
3.14159265358979e+00
355/113
400921fb54442d18
+
3.14
7) MATLAB
(1) who whos
MATLAB
who whos .
.
who whos
( ,
) .
29
[ 1
6]
>>who
Your variables are:
A ans S
A, S, ans 3 .
[ 1
7]
>>whos
Name
Size
Bytes
class
3*3
72
double array
ans
1*1
16
double array(complex)
1*1
double array
.
>>load mydata.dat
mydata.dat MATLAB
mydata .
save
load save .
. MATLAB
temp.mat .
>>save temp
>>save temp X Y Z
save temp save temp X Y Z
X, Y, Z temp .
.
>>A = rand(2, 3);
>>save temp.dat A -ascii
temp.dat 2 3
0.2113
0.8096
0.4842
0.8217
0.8234
0.2354
.
(3) clear
.
>>clear
.
31
>>clear X Y Z
X, Y, Z .
>>clear functions
>>clear variables
.
>>clear mex
>>clear global
>>clear all
MEX .
.
, , MEX .
MATLAB 1
.
1
4.
clc
clf
clear
dir
cd
disp
type
echo
exit,
quit
MATLAB
.
.
.
.
.
() .
.
MATLAB .
8)
C MATLAB .
() .
>>disp(text string?)
% disp:
text string
>>S = Use MATLAB?
32
S =
Use MATLAB
.
. S 1*10 .
9) MATLAB (Help)
(Help)
(Help)
MATLAB (On
line) MATLAB
.
MATLAB
.
Help
(help window)
lookfor
MATLAB HelpDesk
help
help ( )
.
.
3 .
help
.
>> help
HELP topics:
matlab\sparfun
- Sparse matrices.
matlab\graph2d
matlab\graph3d
matlab\specgraph
- Specialized graphs.
matlab\graphics
- Handle Graphics.
matlab\uitools
matlab\iofun
- File input/output.
matlab\timefun
matlab\datatypes
matlab\winfun
matlab\demos
lmi\lmictrl
lmi\lmilab
dspblks\dspblks
DSP Blockset.
dspblks\dspmex
fuzzy\fuzzy
fuzzy\fuzdemos
toolbox\ncd
nnet\nnet
mutools\commands
mutools\subs
toolbox\signal
toolbox\robust
toolbox\ident
toolbox\control
Simulink\Simulink
- Simulink
Simulink\blocks
Simulink\simdemos
Simulink\dee
toolbox\local
- Preferences.
normest
rank
- Matrix rank.
det
- Determinant.
trace
null
- Null space.
orth
- Orthogonalization.
rref
subspace
Linear equations.
\ and /
inv
cond
condest
chol
cholinc
lu
luinc
qr
- Orthogonal-triangular decomposition.
nnls
- Non-negative least-squares.
pinv
- Pseudoinverse.
lscov
svd
gsvd
eigs
- A few eigenvalues.
svds
poly
- Characteristic polynomial.
polyeig
condeig
hess
- Hessenberg form.
qz
schur
Matrix functions.
expm
- Matrix exponential.
logm
- Matrix logarithm.
sqrtm
funm
Factorization utilities
qrdelete
qrinsert
rsf2csf
cdf2rdf
balance
planerot
cholupdate
qrupdate
help
.
>>help magic
magic .
MAGIC Magic square.
MAGIC(N) is an N-by-N matrix constructed from the integers
1 through N^2 with equal row, column, and diagonal sums.
Produces valid magic squares for N = 1, 3, 4, 5, ...
. MATLAB
.
MATLAB help Help Window
(Toolbar) ? .
37
>> helpwin
. help .
lookfor
lookfor (keyword)
.
.
>> lookfor inverse
.
INVHILB Inverse Hilbert matrix.
ACOS
Inverse cosine.
ACOSH
ACOT
Inverse cotangent.
ACOTH
ACSC
Inverse cosecant.
ACSCH
ASEC
Inverse secant.
ASECH
ASIN
Inverse sine.
ASINH
ATAN
Inverse tangent.
INV
Matrix inverse.
PINV
IFFT
IFFT2
IFFTN
Pseudoinverse.
Inverse discrete Fourier transform.
Two-dimensional inverse discrete Fourier transform.
N-dimensional inverse discrete Fourier transform.
38
% .)
lookfor xyz ?all
xyz .
Help Desk
MATLAB Help Desk
HTML(Hyper Text Markup Language) PDF(Portable Document
Format) .
Help Desk help Help Desk
>> helpdesk
.
MATLAB help Desk HTML
. help
.
doc
. eval
>> doc eval
.
PDF
Adobe Acrobat reader .
39
.
. MATLAB (Toolbox)
(Toolbox)
(Toolbox)
MATLAB (Toolbox) .
30
() 2 .
MATLAB , ,
, , MATLAB
.
MATLAB .
1) (Communication Toolbox)
, , 100
150 SIMULINK
C
, ,
,
, , LQG
40
3) (Financial Toolbox)
,
6) (Higher
41
10) (Mu
H 2 H
42
43
44
45
MATLAB
MATLAB . MATLAB
.
MATLAB ,
.
.
.
MATLAB , , ,
, , , ,
.
MATLAB
.
.
. A B (
MATLAB ) A(m*n) B(n*k)
A*B(m*k)
, A(m*n) B(m*n)
( a ij bij ).
2
1 .
4 , (.)
2
1.
46
A?
A.?
A+B
A.+B
A-B
A.-B
s+B
(MATLAB
)
(MATLAB
)
(A
B .)
B s .
s-B,B-s
A*B
s*B
A/B
B?A
inv(A)
A^n
A^p
A B (
X*A=B X )
A B (
A*X=B X )
A
A n
A
p^A
expm(A) A
logm(A) A
sqrtm(A)
A.*B
s.*B
A./B
B.?A
s./B,B.?
s
A.^n
A.^p
p.^A
exp(A)
log(A)
sqrt(A)
conj(A?)
(
)
(
)
,
B s .(
.)
A B
.[A(i,j)/B(I,j)]
s B .
A n
A
A
A
A
. , , , 3
.
, ,
.
47
.
.
.
C mn = Amk Bk n .
. Amn Bmn
.
C m n
=
"
!
"
a m1 bm1 ! a mn bmn
.
.
[ 2
1]
A = [11 12 13 14 15 ; 16 17 18 19 20];
B = [1 2 3 4 5 ; 6 7 8 9 10];
C = A*B?
D = A.*B
C=
205 530
280 730
D=
11
24
39
56
75
.
.
MATLAB
48
. MATLAB
B\A=(A?/B?)?
.
. MATLAB
.
inv(A)
A .
MATLAB A
Warning:Matrix is singlular to working precision .
Inf().
A (ill-condition) .
MATLAB inv .
.
Ax = b .
.
x = inv(A)*b
A b
x = A\b
.
.
[ 2
2]
.
49
rand(seed?,12);
A = rand(100)+1.e8; % (100x100) .
%
% A .
x = ones(100,1);
% x 1 100
b = A*x;
% A x b .
cond(A)
% A .
ans =
5.0482e+011
A 5 * 1011 .
.
tic
% .(Stopwatch Timer)
xi = inv(A)*b;
toc
% xi .
%
% .
eri = norm(x-xi)
% xi x
% 2
rei = norm(A*xi-b)/norm(b) % 2
elapse_time =
0.8700
eri =
1.0044e-004
rei =
1.2177e-006
50
.
tic
xd = A\b;
% xd .
toc
erd = norm(x-xd)
red = norm(A*xd-b)/norm(b)
elapsed_time =
0.3300
erd =
2.7502e-005
red =
5.7220e-016
2.5 .
MATLAB inv
.
.
.
a11 / b11 # a1n / b1n
.
A./B,B./A "
#
"
a m1 / bm1 # a mn / bmn
51
a11 / s # a1n / s
#
" .
A./s "
a m1 / s # a mn / s
s / b11 # s / b1n
s./B,B./s "
#
" .
s / bm1 # s / bmn
.
(1)
A^p
.
p .
1) p>0 A^p A p .
2) p<0 A^p A p
. .
3) p=0 A^0 A .
p .
AV = VD
d11p # 0
A p = V " # " V 1
0 # d nnp
. D .
.
A^p sqrtm(A)
(Schur) .
MATLAB . A^0.5
52
sqrtm(A) .
[ 2
3] .
>>A = [1 2 3 ; 4 5 6 ; 7 8 9];
>>Ap = A^0.3
Ap =
0.6962+0.6032i 0.4358+0.1636i 0.1755-0.2759i
0.6325+0.0666i 0.7309+0.0181i 0.8292-0.0305i
0.5688-0.4700i 1.0259-0.1275i 1.4830+0.2150i
.
>>[V, D] = eig(A);
>>AAp = V*diag([D(1, 1)^0.3, D(2, 2)^0.3, D(3, 3)^0.3])/V,
AAp =
0.6962+0.6032i 0.4358+0.1636i 0.1755-0.2759i
0.6325+0.0666i 0.7309+0.0181i 0.8292-0.0305i
0.5688-0.4700i 1.0259-0.1275i 1.4830+0.2150i
A.^p
(mxn) A p
a11p # a1pn
" # "
p
a mp1 # a mn
.
[ 2
4]
>>Aap = A.^0.3
Aap =
1.0000 1.2311 1.3904
53
.
(2)
p^A
p
p d11
pA =V "
0
" V 1
p d nn
#
#
#
. V, D AV = AD .
[ 2
5] .
>>pA = (0.3)^A
% A .
pA=
2.9342
0.4175 -1.0993
-0.0278
0.7495 -0.4731
-1.9898 -0.9184
1.1531
p.^A
(m*n)
p a11 #
#
"
a
m
1
p
#
p a1n
"
p amn
.
[ 2
6] .
>>paA = 0.3.^A
54
paA=
0.3000 0.0900 0.0270
0.0081 0.0024 0.0007
0.0002 0.0001 0.0000
.
MATLAB .
f() .
funm() .
:
f (a11 ) #
a11 # a1n
2.
sin
cos
tan
cot
sec
csc
asin
f (a1n )
" .
f (a mn )
2 2
3 .
f()
Acosh
Atanh
Acoth
Asech
Acsch
Fix
Ceil
()
acos
atan
atan2
floor
round
rem
55
acot
asec
acsc
sinh
cosh
tanh
coth
sech
csch
asinh
3.
sign
abs
angle
imag
real
conj
log10
log
exp
sqrt
f()
bessel
beta
gamma
rat
1,2
beta
erf
eerfinv
ellipke
ellipj
MATLAB 2
2
4.
cond(A)
4 .
svd(A)
det(A)
dot(A,B)
A (
)
A
trace(A)
expm(A)
A
e
eig(A)
expm1(A)
A
A
()
norm(A,1)
A 1-
expm2(A)
expm3(A)
e .
norm(A)
A 2-
(A )
e A .
56
norm(A,inf)
norm(A,?fro?
)
rank(A)
[ 2
A -
A
logm(A)
sqrtm(A)
ln(A)
A
1
2
funm(A,?FN? A
)
7] , , , .
>>A = magic(5);
>>s = svd(A)?
>>d = det(A), t = trace(A), rk = rank(A), c = cond(A)
>>n1 = norm(A, 1), n2 = norm(A), ninf = norm(A, inf), nf =
norm(A, ?fro?)
S =
65.0000 22.5471 21.6874 13.4036 11.9008
d =
5070000
t =
65
rk =
5
c =
5.4618
n1 =
65
n2 =
65.0000
ninf =
57
65
nf =
74.3303
.
.
exp(A)
log(A)
sqrt(A)
[ 2
expm(A)
logm(A)
sqrtm(A)
funm(A,?FN?)
8] sqrt sqrtm
Bs =
2.6458 3.1623
3.8730 4.6904
Bs =
1.5667 1.7408
2.6112 4.1779
BsBs =
19.2474 23.1990
28.4129 34.2474
BBs =
58
7.0000 10.0000
15.0000 22.0000
BSBS =
7.0000 10.0000
15.0000 22.0000
[ 2
.
.
.
.
<
<=
>
>=
==
:
1) a, b
a, b 1 .
a, b 0 .
2) A B A, B
59
.
A( B) 0 1
.
3) a B a B
B
0 1 .
4) , , .
[ 2
10]
A = [1 2 3; 4 5 6; 7 8 9];
x = 5; X = 5*ones(3);
Ax = x>=A
AX = X>=A
Ax =
1 1 1
1 1 0
0 0 0
AX =
1 1 1
1 1 0
0 0 0
.
.
3 .
&
,
60
:
1)
1 ;
0 .
2) a b
a&b : a, b 1, 0
.
a|b : a, b 1 .
a : a 1, 0 .
3) A, B A, B
.
4) a B a B
B .
5) , , .
[ 2
11] 3
AandB =
1 0 0
0 1 0
AorB =
1 1 1
1 1 1
notB =
0 1 1
1 0 1
61
[ 2
12]
a = 1&0+3, b = 3>4&1
a1 = (1&0)+3
b1 = 3>(4&1)
a =
1
b =
0
a1 =
3
b1 =
1
.
.
.
.
,
.
.
MATLAB .
: .
: .
1) x = N1:N2
62
13]
x1 = 0:10
x2 = 0:3:11
% 0 10 1 .
% 0 11 3 .
x3 = 11.5:-3:0
x1 =
0 1 2 3 4 5 6 7 8 9 10
x2 =
0 3 6 9
x3 =
11.5000 8.5000 5.5000 2.5000
.
linspace logspace .
.
y = linspace(x1,x2)
(1*100)
y(1) = x1,y(100) = x2 .
y = linspace(x1,x2,n)
(1*n)
y(1) = 1,y(n) = x2 .
63
.
.
.
.
y = logspace(a,b)
(1*50)
y(1) = 10 a , y(50) = 10 b .
y = logspace(a,b,n)
(1xn)
y(1) = 10 a , y(n) = 10 b .
[ 2
14] 3
y1 = 1:200:1000
y2 = linspace(1, 1000, 5)
y3 = logspace(0, 3, 5)
y1 =
1 201 401 601 801
y2 =
1.0e+003*
0.0010 0.2507 0.5005 0.7502 1.0000
y3 =
1.0e+003*
0.0010 0.0056 0.0316 0.1778 1.0000
64
.
MATLAB
.
eye ,
o0nes 1 , zeros
rand , randn
linspace , logspace
meshgrid 3 ;
(linspace logspace meshgrid 3 2 .)
, 1 , .
eye(n)
ones(n)
(n*n)
(n*n) .
.
(n*n)
(n*n) 1 .
.
zeros(n) (n*n)
(n*n) .
.
.
randn(n), randn(m,n), randn(size(A))(n*n),(m*n) A
.
65
.
MATLAB ,
.
compan
hankel
hilb
invhilb
kron
magic
diag
toeplitz
pascal
vander
Amn B pq
a11 B # a1n B
= "
#
"
a m1 B # a mn B ( m p )( nq )
.
K = kron(A, B)
[ 2
15] .
A = [1 2 3; 4 5 6], B = [2 4; 6 8]
C = kron(A, B)
A =
1 2 3
4 5 6
B =
2 4
6 8
C =
6 12
66
8 12 16 18 24
8 16 10 20 12 24
24 32 30 40 36 48
.
.
.
MATLAB .
p ( x) = a n x n + a n 1 x n 1 + # + a1 x + a 0
n+1 .
p = [a n
a n 1 # a1 a0 ]
.
p(x) = x 3 -2x-5
p = [1 0 -2 -5];
.
poly
.
p = poly(AR)
p . AR .
AR AR . AR
67
15]
>>A = [1 2 3; 4 5 6; 7 8 0];
>>p = poly(A)
p =
1 -6 -72 ?27
>>r = roots(p)
12.1229
-5.7345
-0.3884
>>p2 = poly(r)
p2 =
1 -6 -72 -27
A
A
. roots .
68
.
(1)
polyval .
p(s) = s 3 -2s-5 s = 5 .
>>polyval([1 0 -2 -5], 5)
ans =
110
. p(X)= X 3 -2X-5 p(X)= X 3 2X-5 . X X
p .
>>p = [1 0 -2 -5];
>>X = [2 4 5; -1 0 3; 7 1 5];
>>Y = polyvalm(p, X)
Y =
377 179 439
111
81 136
S p s 3 -2s+12 polyval(p, S)
S.^3-2*S+12*ones(size(s))
polyvalm
S*S*S-2*S+12*eye(size(s))
. polyval
polyvalm .
(2)
m a n b (convolution) c
c(k ) = a ( j )b(k + 1 j )
j =1
. c (m+n-1).
. MATLAB
c = conv(a, b)
.
[ 2
17] ( s 2 +2s+2)(s+4)(s+1) .
C =
1 7 16 18 8
C =
s^4+7s^3+16s^2+18s+8
70
(3)
(Deconvolution) . a
c q
r .
k
c( k ) r ( k ) = a ( j ) q ( k + 1 j )
j =1
c a q
r .
.
[q,r] = deconv(c, a)
(4)
r = roots(p)
PA = polyval(p, S)
p .
.
p , S .
PM = polyvalm(p, S)
.
p , S .
[r,p,k] = residue(b, a)
. b, a ,
r, p, k ,
, .
P = polyfit(x, y, n)
n x, y
.
b(s) na a(s)
.
71
rn
r1
r2
b( s )
=
+
+#+
+ k ( s)
a ( s ) S p1 S p2
S pn
p1 , p2 , # , pn (poles), r1 , r2 ,# rn (Residues), k(s)
(Direct term) . a(s) m p j
rj
S pj
r j 1
(S p j )2
+#+
r j + m1
( S pi ) m
.
18] ( x 3 6 x 2 72 x 27) .
[ 2
19]
0.4176+1.1130i
0.4176-1.1130i
-0.299i
k =
[]
k
.
.
.
.
MATLAB x
. x
( ) .
.
MATLAB
.
max(X)
X .
mean(X)
X .
median(X)
X .
min(X)
X .
std(X)
X .
prod(X)
X .
sum(X)
X .
73
S = cumsum(X) X .( .)
i
S X S ij = x kj .
k =1
P = cumprod(X) X . P X
i
Pij = x kj .
k =1
sort(X)
[ 2
X .
20]
randn(seed?, 0)
A = randn(100, 4);
AMAX = max(max(A))
AMED = median(A)
AMEAN = mean(A)
ASTD = std(A)
AMAX =
3.2069
AMED =
0.1489 0.0301 0.0443 -0.0189
AMEAN =
0.0727 0.0264 0.0351 0.0424
ASTD =
0.9536 1.0628 1.0860 0.9927
74
C = cov(X)
. cov{X } = M {( X x )T ( X x )}
x2
cov( x, y )
.
y2
cov( y, x)
C (i, j )
P = corrcoef(X) .
P(i, j ) =
C (i, i ) C ( j , j )
cov(x,y)
P = corrcoef(x,y) (2*2) .
[ 2
21] X, Y ,
rand(seed?, 1)
X = rand(10, 3); Y = rand(10, 3);
CX = cov(X), CY = cov(Y), Cxy = cov(X, Y)
PX = corrcoef(X), Pxy = corrcoef(X, Y).
CX =
0.0508
0.0159 0.0442
0.0006 0.0882
CY =
0.0870
0.0656
0.0318
0.0656
0.0874
0.0576
0.0318
0.0576
0.1013
Cxy =
0.0577 0.0078
0.0078 0.0877
PX =
1.0000
0.3875 0.6600
75
0.3875
1.0000 0.0104
0.6600
0.0104 1.0000
Pxy =
1.0000 0.1104
0.1104 1.0000
.
,
. MATLAB
(Signal Processing Toolbox) . MATLAB
.
abs
angle
conv
con2
deconv
fft
fft2
filter
filter
ifft
ifft2
fftshift
(FFT)
FS = fft(X)
FS = fft(X,n)
n
76
IFS = ifft(X)
IFS = ifft(X,n)
X . fft(X) iff(X)
X
. fft(X, n)
ifft(X, n) X n 0
.
[ 2
.
x1 = [1 1 1 1 1]; x2 = [2 1 3 1 1];
fx1 = fft(x1, 9), fx2 = (x2, 9)
x = conv(x1, x2)
y = ifft(fx1.*fx2)
ry = real(y)
fx1 =
columus 1 throgh 4
5.0000 0.5000-2.8356i 0.5000+0.1820i 0.5000-0.8660i
columus 5 throgh 8
0.5000+0.4195i 0.5000-0.4195i 0.5000+0.8660i 0.5000-0.1820i
column 9
0.5000+2.8356i
fx2 =
columns 1 through 4
8.0000 1.8473-4.8053i -0.3794-0.5021i 0.5000+0.8660i
columns 5 through 8
77
yn =
1
(b1 x n + b2 x n 1 + # + bnb x n nb +1 a 2 y n 1 # a na y n na +1 )
a1
Zf = [ y N na +1 , y N na + 2 , #, y N ] .
X = [ x1 , x 2 , # , x N ] .
Zi .
[ 2
23] ( 2
1)
%
noise = 0.2*rand(size(t)); % .
x = S+noise;
y = zeros(size(x));
A = [1 -0.9]; B = [0.05 0.06]; %
y = filter(B, A, x);
% 1
IIR
1.
.
.
MATLAB
. MATLAB
.
(Function Functions)
. ,
79
, .
.
S = quad(fname?, a, b, tol, trace)
.
S = quad8(fname?, a, b, tol, trace) Newto-cotes
.
fname ,
a b tol
. tol = 0.001 .
trace 1 0 .
quad8 quad .
[ 2
24] y (t ) = e
0.5 t
sin(t + )dt
6
S=
y (t )dt
esin.m (esin
) .
function y = esin(t)
y = exp(-0.5*t).*sin(t+pi/6);
function .(
4 .)
esin.m (Folder) .
.
S = quad(esin?, 0, 3*pi)
80
S =
0.9008
esin
.
Z = trapz(X, Y)
Sc = cumsum(Y)
[ 2
25] .
Z =
0.9008
Scf =
0.9016
.
(ODE) MATLAB
.
MATLAB 1
x = f ( x, t ) 1 .
81
y ( n ) = f (t , y, y ' , #, y ( n 1) )
x1 = y, x 2 = y ' ,# , x n = y ( n 1)
.
x1 = x 2
x 2 = x3
xn = f (t , x1 , x2 , # , xn )
y???-3y??-y?y = 0
y(0) = 0, y?(0) = 1, y(0) = -1
. x1 = y, x 2 = y ' , x3 = y ' '
x1 = x 2
x 2 = x3
x3 = 3x3 + x 2 x1
x1 (0) = 0, x 2 (0) = 1, x3 (0) = 1
.
x = f ( x, t ) 1 .
x , t, x
82
. M
.
function dx = F(t, x)
dx = [x(2); x(3); 3*x(3)+x(2)*x(1)];
ODE .
t x .
ODE .
MATLAB
.
ODE .
[T, X] = solver(F?, tspan, x0)
T
X
()
.
ODE
tspan
[t0, tfinal]
x0
solver
ode45, ode23, ode113, ode15s, ode23
.
[0 1] ode45
[T, X] = ode45(F?, [0 1], [0; 1; -1])
. X T
x1, x2, x3 .
[ 2
26]
83
ODE
y?? - (1 y ) 2 y?+y = 0
. >0 .
.
y = x1 , y = x2
x1 = x2
x 2 = (1 x1 ) 2 x2 x1
.
ODE
=1
.
function dx =
vdp1(t, x)
dx = [x(2); (1x(1)^2)*x(2)-x(1)];
vdp1 .m
.
y(0) = 2,
2.
ylabel(solution X?)
legend(X1?, ?X2?)
2
2 .
85
MATLAB
.
.
.
MATLAB
, , .
MATLAB 2 3
.
.
.
1 .
. MATLAB 2
.
MATLAB .
3-1 .
.
.
1.
plot
loglog
86
semilogx
semilogy
plotyy
x y
y x
. 2 plot
MATLAB 2 plot . plot
MATLAB (Build-in function) 2
plot .
.
plot 3 .
plot(x)
x x
.
1, 2, 3, .
x
. x
.
x x
.
[ 3
1] ( 3
1)
1.
X = [1, 3, 6, 3, 5, 9, 0, 2];
plot(X)
plot(X,Y)
X, Y X, Y ,
.
87
[ 3
2] ( 3
2)
X = 0:pi/100:2*pi;
Y = sin(X);
plot(X, Y);
X .
. , plot(X, sin(X))
.
2.
3.
plot .
. plot(X, Y) Y
X () Y
. Y .
[ 3
3] ( 3
3)
X = 0:pi/100:2*pi;
X = X?;
Y = [sin(X), cos(X), cos(X+0.5)];
plot(X, Y);
plot X Y
plot(X1, Y1, X2, Y2,) X-Y
88
.
plot ,
.
.
.
plot(X, S)
plot(X, Y, S)
plot(X1, Y1, S1, X2, Y2, S2,)
S (
) . 1
3 3
[ 3
4] ( 3
2 .
4)
t = 0:pi/12:2*pi;
y1 = sin(t); y2 = cos(t);
plot(t, y1, ?r-?, t, y2, ?b--?);
% ,
grid ;
X = [1.7*pi; 1.6*pi];
Y = [-0.3; 0.8];
S = [Sin(x)?;?Cos(x)?];
Text(X, Y, S);
2.
(RGB)
Y(110)
C(011)
R(100)
G(010)
o
+
*
89
B(001)
M(101)
W(111)
K(000)
--.
:
. 2
2
plot .
MATLAB plot 2
.
MATLAB 2
4.
3.
bar
compass
contour
errorba
r ezplot
feather
fill
fplot
gplot
hist
pcolor
polar
2
quiver
x
rose
stem
2
stairs
3 .
.
[ 3
5] bar(x, y) y ( 3
x = 0:pi/10:2*pi;
90
5)
y = sin(x);
bar(x, y);
bar(y) y x .
plot .
[ 3
6] errorbar(x, y, e) ( 3
5.
6.
x = 0:pi/12:2*pi;
y = sin(x);
e = rand(size(x))/5;
errorbar(x, y, e);
x, y, e .
.
[ 3
7] stem(x, y) ( 3
x = 0:0.2:4*pi;
y = exp(
0.3*x).*sin(x);
stem(x, y)
stem(y)
y x
plot
91
7)
6)
.
3
[ 3
7.
8] polar(t, y) ( 3
8)
t = 0:0.01:5*pi; y = t;
polar(t, y)
t, y t .
.
[ 3
9] stairs ( 3
9)
x = 0:pi/20:2*pi; y = sin(x);
stairs(x, y)
8.
9.
. 2 fplot
y = f(x) x
.
92
y = f(x) fplot .
.
.
fplot
.
fplot .
fplot(fname, lims, marker, tol)
[X, Y] = fplot(fname, lims, marker, tol)
.
fname
, MATLAB
.
lims
marker
, , plot .
tol
, 2e-3 .
X, Y
, X Y
. X Y .
fplot .
[ 3
10)
1337
(2) (
( 3
11)
t = (-0.4:1.8/n:1.4)?;
plot(t, cos(tan(pi*t)));
n
. .
10. fplot
11. plot
.
. 3
MATLAB 3 ,
.
. 3
MATLAB 3
.
mesh, meshc, meshz 3
94
plot3
2 plot 3
mesh
3 () mesh .
3 x
z = f(x, y)
x
z 3 .
mesh 2 .
mesh(Z)
Z 3 .
mesh(X, Y, Z)
X, Y X
Z
Y Z
. 3 x
y X, Y
Z . X, Y, Z
3 .
[ 3
11] Z 3 ( 3
Z = [1 1 1 1 1 1 1
1 2 2 2 2 2 1
1 2 2 3 2 2 1
1 2 2 2 2 2 1
1 1 1 1 1 1 1];
mesh(Z); colormap([1 0 0]);
95
2.
12)
xlabel(x?); ylabel(y?);
x-y-z X Z
y . colormap([1 0 0])
.
plot3 3 . plot
plot3 MATLAB 3
.
plot3 3 .
plot3(x, y, z)
x, y, z .
3 {(x(i), y(i), z(i)} .
[ 3
12] ( 3
13)
t = 0:pi/50:10*pi;
plot3(sin(t), cos(t), t);
grid on
3
[ 3
12 3 .
13]( 3
14)
t = 0:0.05:100;
x = t; y = sin(t); z = sin(2*t);
plot3(x, y, z, b:?)
96
13. 3
14. 3
plot3(X, Y, Z)
X, Y, Z . MATLAB X, Y,
Z . plot
MATLAB .
plot3(X, Y, Z, S)
plot , ,
. S , ,
.
.
>>plot3(X1, Y1, Z1, S1, X2, Y2, Z2, S2, )
(Xi, Yi, Zi) .
. 3
1)
2 z = f(x, y) 3 .
MATLAB z = f(x, y) D = [x0,
xm]*[y0, yn]
97
y meshgrid
. meshgrid .
>>[X, Y] = meshgrid(x, y)
>>[X, Y] = meshgrid(x)
meshgrid(x, x) . x [x0, xm]
y [y0, yn] . X Y
. X x, Y
y . X Y (X(i, j), Y(i, j)) D (i, j)
.
(X(1, 1), Y(1, 1)) (x0, y0) .
meshgrid .
. X Y MATLAB
.
z = sin( x 2 + y 2 ) / x 2 + y 2 [-8, 8]*[-8, 8]
.
[ 3
x =
14]
8:0.5:8; y = x;
Z = sin(R)./R;
mesh(Z)
R
eps .
2)
mesh
. 3
14
mesh(Z) 3
3 . 3
15
15 x
y Z
. mesh
15.
.
1) mesh(X, Y, Z, C) . X, Y, Z, C
, C .
(i, j), Y(i, j), Z(i, j)
C .
2) mesh(X, Y, Z)
1) C=Z .
3) mesh(x, y, Z, C) x y ,Z C . X
99
Z y Z
. (x(j), y(i), Z(i, j))
C .
4) mesh(x, y, Z)
5) mesh(Z, C)
3) C=Z .
Z C m*n mesh(x,y,Z,C)
. x, y x=1:n, y=1:m
.
) mesh(Z)
5) C=Z .
( 3
17).
16. Meshc
17. Meshz
100
(4 )
.
MATLAB surf .
mesh . surf mesh
mesh surf
.
[ 3
15] ( 3
18)
x = -1.5:0.3:1.5; y = -1:0.2:1;
[X, Y] = meshgrid(x, y);
Z = sqrt(4-X.^2/9-Y.^2/4);
surf(X, Y, Z)
meshc meshz
surfc, surfz
.
18.
.
.
MATLAB
.
1)
MATLAB
101
figure(n) n .
clf( clg) .
cla
.
Windows
>> close(n)
. n .
figure(n) .
2)
MATLAB
.
. MATLAB
.
MATLAB subplot .
>> subplot(m, n, p)
102
m, n, p . m*n
p . m n
,
p
.
. 4
.
[ 3
16] ( 3
19)
19.
t = 0:pi/20:2*pi;
[x, y] = meshgrid(t);
subplot(221); plot(sin(t), cos(t))
axis equal
subplot(222)
z = sin(x)+cos(y);
plot(t, z)
axis([0 2*pi 2])
103
subplot(223)
z = sin(x).*cos(y);
plot(t, z)
axis([0 2*pi -1 1]
subplot(224)
z = (sin(x).^2)-(cos(y).^2);
plot(t, z)
axis([0 2*pi -1 1])
axis
3)
hold . 3
.
hold on
.
hold off
hold
.
.
hold on MATLAB
.
[ 3
17]
y = ax+b
104
.( 3
20)
20.
% .
.
MATLAB
.
MATLAB
.
.
MATLAB .
axis .
axis([xmin, xmax, ymin, ymax])
2 x- y-
.
105
.
axis(auto?)
axis(ij?)
(ij) .
axis(xy?)
axis(off?)
axis(on?)
.
[S1, S2, S3] = axis(state?)
S1 auto , manual . S2 on
off . S3 xy
, ij .
.
.
MATLAB
. Windows
(
)
.
1)
MATLAB .
title(String?)
xlabel(string?)
x () .
y () .
2 ).
2)
MATLAB text gtext
. text gtext
.
text(x, y, z, ?String?)
(x, y, z) String?
.
gtext(String?)
text x, y, z 3
String . String
.
z 2 ( 3
4 ).
gtext
107
.
.
108
.MATLAB
.MATLAB
.
.
. BASIC C, PASCAL
MATLAB
.
.
.
.
. MATLAB for-end
while-end .
1) for - end
for-end .
>>for i = 1:n, x(i) = 0, end
x n .
n . n 1
MATLAB .
for-end .
109
2) while-end
.
110
.
>> n = 1;
>> while prod(1:n)<1.e100, n = n+1; end
>> n
n! 100 n . prod(1:n)
1:n .
while
end .
while
()
end
.
1*1 . 1*1
any all 1*1 .
v all(v) 1
.
.
( )
.
if-else-end .
.
if
1
else
2
end
111
if 1
1
elseif 2
2
else
3
end
.
.
M MATLAB
MATLAB .
MATLAB
.
MATLAB MATLAB .
DOS (Batch) .
MATLAB .
MATLAB (Text) M .
m (MATLAB
.) ASCII
.
M MATLAB M
(Toolbox) ,
, , , , , , , , ,
112
. M
,
. MATLAB C M
C .
MATLAB M M (Script FIle) M
(Function File) .
. M
M
MATLAB . MATLAB M M
. M MATLAB
MATLAB .
MATLAB (Workspace)
,
. clear
.
M MATLAB
.
, MATLAB M
.
M .
.
% An M-file to calculate Fibonacci numbers
f = [1 1]; i = 1;
while f(i)+f(i+1)<1000
f(i+2) = f(i)+f(i+1);
i = i+1;
113
end
plot(f)
fibno.m ( .
m MATLAB
.) MATLAB
.
(>>) fibno
. MATLAB 16
f i MATLAB
.
(%) MATLAB
.
M M (Folder)
MATLAB . M
MATLAB
MATLAB matlabrc.m (
MS-DOS
config.sys .) .
. M
M
MATLAB . M M
function ,
. M
. MATLAB
(Toolbox)
.
114
.
. M
M
.
M (text)
. M
MATLAB
MATLAB .
M .
.
.
function y = meanvalue(x)
% MEAN Average or mean value
% MEAN(x)
% MEAN(x)
[m, n] = size(x);
if m == 1
m = n;
end
y = sum(x)/m;
mean.m . MATLAB
( ) . 1
99
>> z = 1:99;
z
115
>> mean(z)
.
ans =
50
, ,
.
,
, . MATLAB
(Toolbox)
.
.
, .
. M
.
function [output1, , outputn] = function_name(input1, , inputm)
(%) .
(help) .
>>help function-name
.
.
M mean.m x
y . M
. MATLAB
nargin nargout
. MATLAB
M .
116
.
.
.
MATLAB M (local)
.
(Workspace) .
.
.
(global)
.
.
global .
>> global X Y Z
X, Y, Z .
.
.
gfibno.m .
function f = gfibno
% gfibro.m
% NDCBA .
global NDCBA
f = [1, 1];
117
i = 1;
while f(i)+f(i+1)<NDCBA
f(i+2) = f(i)+f(i+1);
i = i+1;
end
MATLAB .
>> global NDCBA
>>NDCBA = 1000;
>>gfibno
ans =
columns 1 through 12
1 1 2 3 5 8 13 21 34 55 89 144
columns 13 through 16
233 377 610 987
.
.
.
.
.
.
.
. echo
M
118
. echo
.
echo .
M echo
.
echo on
echo on
echo
echo
. echo
.
echo FileName on FileName
.
echo FileName off FileName .
echo FileName
FileName .
echo on all
. input, yesinput
input ,
. .
a = input(Please input a number:?)
.
Please input a number:
, ( ),
119
. Enter a .
a = input(Please input a string:? , ?s?)
Please input a string:
. ( )
a .
a = yesinput(Prompt:? , Default, Possible)
yesinput .
.
[ 4.1] yesinput
MATLAB .
>>a = yesinput(Order of the filter? , 10 , [0, 12]);
.
Order of the filter(10):
. Enter
10, 12 0
.
. pause
pause
. pause
. pause
.
pause
.
.
pause(n)
n .
120
.
.
MATLAB
.
.
.
[]
.
.
M
M .
MATLAB .
.
.
fopen, fread MATLAB I/O
.
MEX
.
MAT
C FORTRAN MAT-
. MAT MATLAB .
121
.
MATLAB .
diary
MATLAB diary
(, , )
.
.
diary
.
.
>>diary filename
filename .
>>diary off
.
save
save .(
.)
fopen, fwrite
.
MEX
.
122
. save load
MATLAB
. save MATLAB
load .
save .
save
MATLAB 2 matlab.mat
. 2
. . 10000
8 .
save dfile
dfile.mat . mat
.
save dfile x y
x y 2 dfile.mat .
.
save dfile.dat x y -ascii
x y 8 dfile.dat .
save dfile.dat x y -ascii -double
x y 16 dfile.dat .
load
.
load
matlab.mat
.
load dfile
load dfile.dat
dfile.mat .
dfile.dat .
123
.
.
SIMULINK
Simulink , , .
MATLAB Simulink (Visual)
Windows
. Simulink MATLAB,
Simulink C, FORTRAM, Simulink DSP, Simulink
,
.
Simulink Simulink MATLAB
. Simulink , ,
MATLAB .
MATLAB .
Simulink Real-Time Workshop Simulink C
. ,
.
Simulink
.
.
. SIMULINK
SIMULINK
.
MALAB
>> Simulink
5
1 Simulink untitled
.( 5 ?2)
124
1. Simulink
2. Simulink
Simulink 5
3 PID
.
.
125
3. PID
.
. Simulink
5
Sources
. Sources
(pointer)
5
4 sources
.
Step
. step
.
. 5-5
.
step
4. Sources
. step
.
126
5.
Simulink
. Step 5
, , .
6. Step
127
.
PID Linear , ,
5
8)
7. PID
8. PID
(
) Linear .
Sinks
scope .
PID
5
9, 5
10 .
128
9. PID
10.
(+)
(+ -) .
5
11 .
.
. Simulink
(>) .
.
(>)
(>)
.
.
5
12 .
. (Menubar)
Simulation Start
129
(Toolbar)
5
13 Scope
11.
12. PID
130
13. .
.
.
. Simulink
MATLAB () Simulink 2
. MATLAB Simulink (
1, 5
2 MATLAB
.
Simulink ()
. Sources Sinks
( ) Discrete,
Linear,
Nonlinear , ,
Connections
.
131
()
.
MATLAB
, , .
Simulink S-
, , M
.
1) ,
,
.
.
,
.
. (
, , ) .
4
. 4
.
Delete .
.
(cut) Edit cut .
Windows (Clipboard) .
132
Edit Paste
.
Edit Copy
. Edit
Paste .
.
Ctrl
.
2)
.
.
(+)
.
3)
.
.
.
.
()
.
133
.
. 5
14
14
.
. Transfer Fcn
.
Plant
.
14.
. (sum)
134
. 5
15 .
(+ -) . 5
14
. 3
. .
5
16
. Numerator Denominator
5
14
15.
4)
File Save ( Save )
. M
.
135
16.
Simulink type
, .
.
.
()
.
.
Simulink
.
.
. (menu)
(menu)
(menu)
Simulink
.
136
1)
.
.
. ( ,
) .
.
.
2)
Simulink Simulation Parameters
. 5
Simulink .
137
17
17.
3 .
(1) Solver
Solver
.
Start time
Stop time . 0.0
10.0 . . 10
10 . ,
, .
Simulink (ODE)
Simulink .
138
.
.
.
Simulink
.
ode45, ode23, ode113, ode15s, ode23s
discrete . ode45,
discrete .
Ode45 Dormand-Prince -(4, 5)
y (t n ) y (t n 1 ) .
ode45
.
Ode23 Bogacki Shampine -(2, 3)
.
Ode113 Adams-Bashforth-Moulton PECE
ode15s ode23s 2
Rosenbrock .
Discrete Simulink
.
ode5, ode4, ode3, ode2, ode1 discrete
.
Ode5 Dormand-Prime ode45 ode4 4 , ode3 Bogacki-Shampine ode23 , ode2
Euler Heun , ode1 Euler .
. auto
.
139
.
auto .
(Max Step Size)
. .
hmax =
t stop t start
50
.
.
.
Solver (Output options)
Refine Output Produce additional output,
Produce specified output only .
(2) Workspace I/O
MATLAB Simulink (Workspace)
.
Simulation Parameters Workspace I/O
.
( 5-18) Inport .
Load from workspace Input
. Save to workspace
.
140
19. Diagnostics
141
Save options .
Limit rows to last .
(3) Diagnostics
Simulation Parameters Diagnostics
.( 5
19)
,
.
.
M
. sim set-param .
Sim .
[t, x, y] = sim(model, timespan, option, ut, p1, , pn);
Simulink
.
model
Simulation parameters .
t, x, y , , .
model MEX-, M S-
.
Timespan [tStart, tFinal] .
tFinal 0 . [tStart OutputTimes
tFinal] t
.
Options
simset .
142
ut Inport p1, , pn
sys = sfun(t, x , u , flag , p1 , p2 , , pn)
s-.
>>[t, x, y] = sim(vdp?, 1000, simset(MaxRows?, 100,
?OutputVariables?, ?ty?, ?FinalStateName?, ?xFinal
?));
vdp 1000s 100 .
t y xFinal .
set_param , , .
get_param . set_param
.
set_param(sys? , ?Simulation Command ? , ? cmd?)
sys cmd start, stop, pause, continue, update
.
5
12 5
14
143
.
5
1 5-20
.
. Output
Connections
.
sim1.mdl
MATLAB
5-20.
.
[t, x, y] = sim(sim1?, 20)
plot(t, y)
5-21
. Output
plot
y
.
5-2
5-21.
.
5-22 .
(ToWorkspace) Variable Name y
. To WorkSpace1 t .
(clock) Sources .
144
5-22.
Space .
.
. S
Simulink Simulink
S
S
MATLAB C
145
. C S
MEX
mex MEX
MATLAB
(Link).
S
M , MEX (C FORTRAN
) 3 . S
.
. M
. MEX
.
.
S
3 ,
. S
.
.
SIMULINK SIMULINK
M () . S
()
.
MATLAB .
SIMULINK () .
. S
SIMULINK .
S
S
SIMULINK .
MATLAB
. MATLAB
.
M , .
.
.
146
SIMULINK S
M C MEX S
. S
,
SIMULINK .
M MEX
S
. S
. S
S
Simulink
, , ,
.
Simulink .
u y, x .(
.)
,
.
y = f 0 (t , x, u )
()
dxc
()
dt
= f a (t , x, u )
x d (k + 1) = f u (t , x, u )
()
xc
x=
x d
.
147
Simulink .
.
x .
Simulink
, , .
S- .
S- mdl .
M S- Simulink flag
. flag
S-
. flag M
. C MEX S- Simulink S-
.
S- .
sys = sfunction(t, x, u, flag)
[sys, x0, str, ts] = sfunction(t, x, u, flag)
sfunction t , x
, u flag S-
.
sys, x0, str, ts .
sys flag .
flag = 3 sys S- . x0 (
) str S- API
M S- .
ts 2 .
.
flag 5
1 .
148
flag
x(k+1)
dx/dt
S-
mdlInitializeSizes
mdlGetTimeOfNextVarHit
mdlOutputs
mdlUpdate
mdlDerivativeqs
mdlTerminate
flag
0
4
3
2
1
9
flag = 0
[sizes,x0] = sfunction([],[],[],0)
x0 .
sizes .
S
.
. S
flag = 0 .
( .)
(flag = 1 .) (flag = 3
.) . (
.) flag = 2 flag = 3
.
. S
3 (, M MEX )
S
149
1) S
23 .
23.
S
S
( M )
M .
150
MATLAB M
Simulink S
vdps.mdl MATLAB
flag .
5
3] flag=0 vdps .
[sizes,x0] = vdps([],[],[],0)
sizes =
2
0
0
0
0
0
x0 =
0.2500
0.2500
sizes 2
, .
x0 .
[ 5
4]flag = 1 .
flag = 1;
u = [];
t = 0;
x = [0.25; 0.25];
dx = vdps(t, x, u, flag)
151
dx =
0.2500
-0.0156
2) M S
M
S
.
Simulink M S- S-
. , , .
Simulink S-
mdlInitializeSizes simsizes .
sizes = simsizes;
5-2 sizes .
5-2
Sizes
Sizes.NumContStates
Sizes.NumDiscStates
Sizes.NumOutputs
Sizes.NumInputs
Sizes.DirFeedthrough
Sizes.NumSampleTime
s
Sizes simsizes
sys = simsizes(sizes);
Sizes Simulink sys
152
.
.
.
M
.
function [sys, x0, str, ts] = vdpm(t, x, u, flag)
%VDPM Example M-File S-function implementing
%
switch flag,
% Initialization %
case 0,
[sys, x0, str, ts] = mdlInitializeSizes;
% Derivatives %
case 1,
sys = mdlDerivatives(t,x,u);
% Update, Output, and Terminate %
case {2, 3, 9},
sys = [];
% do nothing
otherwise
error(['unhandled flag = ',num2str(flag)]);
end
%=============================================
% mdlInitializeSizes
% Return the sizes, initial conditions,
%
%=============================================%
function [sys, x0,str,ts] = mdlInitializeSizes
153
sizes = simsizes;
sizes.NumContStates = 2;
sizes.NumDiscStates
= 0;
sizes.NumOutputs
= 0;
sizes.NumInputs
= 0;
sizes.DirFeedthrough
= 1;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = [.25 .25];
str = [];
ts = [0 0]; % continuous sample time: [period, offset]
% end mdlInitializeSizes
%===========================================
% mdlDerivatives
%===========================================
function sys = mdlDerivatives(t,x,u)
sys(1) = x(1) .* (1 - x(2).^2) - x(2);
sys(2) = x(1);
% end mdlInitializeSizes
3) C (MEX )
) S
S- C MEX .
Simulink Simulink ODE ,
MEX .
154
, ,
.
MEX
. 5-3
Simulink . S? MEX
.
5-3.
S-
MdlInitializeSizes
MdlInitializeSampleTime
MdlInitializeCondition
MdlOutputs
MdlUpdate
MdlGetTimeOfNextVarHit
MdlDerivatives
mdlTerminate
C MEX S- M- S-
. M S- C MEX S-
mdlInitializeSampleTimes mdlInitializeCondition
. M S- C MEX S- S-
flag .
Simulink S- .
Simulink SimStruct S-
. SimStruct include
#include Simulink.h
.
C MEX S-
.
155
(pointer)
ssSetNumStates(S, numContStates)
ssSetNumDiskStates(S, numDiscStates)
ssSetNumOutputs(S, numOutputs)
ssSetNumInputs(S, numInputs)
ssSetDirectFeedThrough(S,
dirFeedThru)
ssSetNumSampleTimes(S,
numSampleTimes)
ssSetNumInputArgs(S, numInputArgs)
ssSetNumIWork(S, numIWork)
ssSetNumRWork(S, numRWork)
ssSetNumPWork(S, nuimPWork)
.
2 SimStruct
156
S . S- Sizes
.
Static void mdlInitializeSizes(Simstruct *S)
{ ssSetNumContStates(S, 1);
ssSetNumDiscStates(S, 0);
ssSetNumOutputs(S, 1);
ssSetNumInputs(S, 1);
ssSetDirectFeedThrough(S, 0);
ssSetNumSampleTimes(S, 1);
}
, ,
.(
.)
5-5 C MEX S-
/*
*
*
*/
ssSetNumDiscStates(S, 0);
if (!ssSetNumInputPorts(S, 0)) return;
if (!ssSetNumOutputPorts(S, 0)) return;
ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 0);
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 0);
ssSetNumModes(S, 0);
ssSetNumNonsampledZCs(S, 0);
ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);
}
/* Function: */
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0); }
#define MDL_INITIALIZE_CONDITIONS
/* Function: mdlInitializeConditions
*
/* Function: mdlOutputs
*
y = Cx + Du
*/
{ }
#define MDL_DERIVATIVES
/* Function: mdlDerivatives
*
xdot(x1) = x0
*/
real_T *dx
= ssGetdX(S);
real_T *x
= ssGetContStates(S);
Windows vdpmex.dll .
Window MATLAB .
[sys, x0] = vdpmex([], [], [], 0)
5
3 .
159
C MEX S-
mdlUpdate mdlDerivative .
MEX . C MEX S- M S .
4) S
3
S
.
.
MEX S
M
S
5-24.
.( 5-24)
[ts, xs] = sim(vdps?, 10);
[tm, xm] = sim(vdpm?, 10);
plot(ts, xs, ?o?, tm, xm, ?-?);
S
2.9100
S
. S
Simulink .
S
.
5-6 2 .( 5-25)
M .
function [sys,x0,str,ts] = timestwo(t,x,u,flag)
%TIMESTWO S-function whose output is two times its input.
%
y = 2 * u;
switch flag,
% Initialization %
case 0
[sys,x0,str,ts]=mdlInitializeSizes;
% Outputs %
case 3
sys=mdlOutputs(t,x,u);
% Unhandled flags %
case { 1, 2, 4, 9 }
sys=[];
% Unexpected flags (error handling)%
otherwise
161
= -1;
= -1;
sizes.DirFeedthrough = 1;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
str = [];
x0 = [];
ts = [-1 0];
% end mdlInitializeSizes
5-25.
% mdlOutputs
function sys = mdlOutputs(t,x,u)
sys = u * 2;
% end mdlOutputs
5-25 .
162
5-26. S-
.
5-26
.
163
.
.
.
,
. (CAD)
20 .
.
MATLAB . MATLAB
Control system Toolbox, System Identification Toolbox, Model Predictive
control Toolbox, Robust control Toolbox,
.
.
.
(Linear Time Invariant
LTI) .
.
164
dx
= Ax + Bu
dt
y = Cx + Du
. u x y
.
.
Y ( s ) = G ( s )U ( s )
G ( s ) = C ( sI A) 1 B + D
.
G (s) =
bm s m + bm 1 s m1 + # +b 1 s + b0
an s n + a n 1 s n1 + # + a1 s + a0
,
G (s) = k
( s z1 )( s z 2 ) # ( s z m )
( s p1 )( s p2 ) # ( s pn )
.
MATLAB 3 ,
, .
1) LTI
LTI ,
/(SISO) /(MIMO) .
165
3 LTI .
(TF),
G (s) =
s+2
s + s + 10
2
(ZPK),
2( z 0.5)
G( z ) =
z ( z + 0.1)
z2 + z +1
( z + 0.1)( z + 0.2)
(SS)
dx
= Ax + Bu
dt
y = Cx + Du
3 tf, zpk, ss
.
sys = tf(num,den)
sys = zpk(z,p,k)
sys = ss(A,B,C,D)
166
G (s) =
n( s )
d ( s)
n(s) d(s) .
s+2
s + s + 10
2
>>G = tf([1 2], [1 1 10])
. .
Transfer function:
s+2
------------s^2+s+10
>>num = [1 2];
>>den = [1 1 10];
>>G = tf(num, den);
>>G
.
MATLAB
s .
167
[1 3 5] s 2 + 3s + 5 . MATLAB
.
MIMO SISO . MIMO
. ,
SISO .
s 1
N (s) =
s + 2
s +1
D( s) = 2
s + 4 s + 5
s 1
s + 1
G (s) =
s + 2
s + 4 s + 5
. MIMO G(s) .
>>N = {[1 -1]; [1 2]}
% N(s)
% D(s)
>>G = tf(N, D)
Transfer function from input to output...
s-1
#1: ---s+1
s+2
#2: -------s^2+4s+5
SISO G(s) .
>>g11 = tf([1 -1], [1 1]
%(s-1)/(s+1)
168
%(s+2)/(s^2+4s+5)
( s z1 ) ! ( s z m )
( s p1 ) ! ( s p n )
k z1 ,! , z m p1 , # p n g(s)
.
.
g = zpk(z, p, k)
z p k .
g(s) =
2s
( s 2)( s 2 2 s + 2)
.
>>g = zpk(0, [1-i 1+i 2], -2)
Zero/pole/gain:
-2s
-----------------------(s-2)(s^2-2s+2)
MIMO . p*m MIMO
.
G = zpk(Z, P, K)
z p*m , p p*m , K p*m
169
.
>>Z = {[], -5; [1-i 1+i] []}
% []
G ( s ) = 2( s 2 2s + 2)
( s 1)( s 2)( s 3)
3( s + 5)
( s + 1) 2
SISO MIMO
.
.
dx
= Ax + Bu
dt
y = Cx + Du
ss .
sys = ss(A, B, C, D)
A, B, C, D .
dx
= Ax + Bu
dt
y = Cx
170
0 1
A=
5 2
0
B=
3
C = [0 1]
.
>>sys = ss([0 1 ; -5 -2], [0; 3], [0 1], 0)
a =
x1
x2
x1
x2
-5
-2
b =
u1
x1
x2
c =
y1
x1
x2
d =
u1
y1
LTI , , ,
.
.
SS > ZPK > TF
LTI SS SS
, SS ZPK ZPK , TF
171
TF .
>>sys = sys1+sys2
sys1 TF , sys2 SS sys
SS .
2) LTI
LTI
Ts() .
sys = tf(num, den, Ts)
sys = zpk(z, p, k, Ts)
sys = ss(a, b, c, d, Ts)
3)
()
172
x(t ) = Ax(t ) + Bu (t )
y (t ) = Cx(t ) + Du (t )
.
y = G ( s )e su
.
G ( s ) = D + C ( sI A) 1 B exp(
s)
.
.
.
173
% 2 , 2
.
tf, zpk, ss LTI
.
( , A, B, C, D )
LTI
.
tfdata, zpkdata, ssdata
.
[num, den, Ts] = tfdata(sys)
% Ts =
G( s) = 2
s + 2 s + 10
1
s
, .
>>[num, den] = tfdata(G)
>>num{1, 1}
ans =
0 1 -1
>>den{1, 1}
175
ans =
1 2 10
.
SISO .
[num, den, Ts] = tfdata(sys, ?v?)
1 LTI
176
.
6
1.
LTI
tf
,
zpk
-, -
ss
,
dss
Decriptor
filt
DSP
set
LTI
get
LTI
tfdata
zpkdata
-
ssdata, dssdata
.
.
.
MATLAB LTI
, .
,
.
.
6
.
.
177
1)
tf, ss, zpk
.
6
2.
tf
zpk
ss
c2d
d2c
d2d
ssbal
ss2ss
canon
ctrbf
obsvf
minreal
balreal
modred
LTI sys .
sys_tf = tf(sys)
% TF
sys_zpk = zpk(sys)
% ZPK
sys_ss = ss(sys)
% SS
>>sys = ss(-2,1,1,3)
.
>>zpk(sys)
178
Zero/pole/gain:
3 (s+2.333)
---------------(s+2)
LTI 3
.
.
.
2)
c2d d2c
. .
sysd = c2d(sysc,Ts)
% Ts =
sysc = d2c(sysd)
/ (ZOH), 1 (FOH), Tustin
.
.
.
sysd = c2d(sysc,Ts,?foh?)
% 1
sysc = d2c(sysd,?tustin?)
% tustin
Gd (z ) d2c ZOH Gd (z )
G(s) .
. ZOH d2c z=0
. z s
. d2c
179
.
[ 6-1] .
>>gd = zpk([],-0.5,1,0.1)
Zero/pole/gain:
1
---------(z+0.5)
Sampling time: 0.1
>>gc = d2c(gd)
% gc 2
Zero/pole/gain:
4.621(s+149.3)
-------------------------(s^2+13.86s+0.035)
>>c2d(gc,0.1)
% c2d gd
Zero/pole/gain:
(z+0.5)
------------(z+0.5)^2
Sampling time: 0.1
LTI
.
sys2 = d2d(sys1, Ts)
sys1 Ts sys2
180
0.1s 2.5 . 6
1 .
6-1.
181
3)
, .
x = Ax + Bu
y = Cx + Du
z = Tx
x = (TAT 1 ) z + (TB )u
y = (CT 1 ) z + Du
. T
.
ss2ss: T .
sys2 = ss2ss(sys1, T)
sys1 A, B, C, D sys2 TAT 1 , TB, CT 1 , D
.
ctrbf: .
.
[Ab, Bb, Cb, T, K] = ctrbf(A, B, C)
[Ab, Bb, Cb, T, K] = ctrbf(A, B, C, tol)
T , K , tol .
182
T
Ab = TAT? , Bb = TB , Cb = CT?
Ab, Bb, Cb .
A
Ab = nC
A21
0
AC
0
Bb =
Bc
Cb = C nC
CC
( AC , BC ) AnC .
C C ( sI AC ) 1 BC = C ( sI A) 1 B
obsvf: .
.
[Ab, Bb, Cb, T, K] = obsvf(A, B, C)
[Ab, Bb, Cb, T, K] = obsvf(A, B, C, tol)
T A, B, C
Ab, Bb, Cb
.
A
Ab = nO
0
A12
AO
B
Bb = nO
BO
C b = [0 C O ]
(C O , AO ) AnO .
canon: .
csys = canon(sys, type)
LTI sys csys . type
.
modal?:
).
183
companion?: .
ctrbf, obsvf, canon
.
.
minreal: , .
sysr = minreal(sys)
sysr = minreal(sys, tol)
sys sysr
sysr
.
sysr sys
.
>>g = zpk([], 1, 1);
>>h = tf([2 1], [1 0]);
>>cloop = inv(1+g*h)*g;
.
>>cloop
Zero/pole/gain:
s(s-1)
--------------------(s-1)(s^2+s+1)
s=1 .
>>cloop = minreal(cloop)
Zero/pole/gain:
s
-------------------184
(s^2 + s + 1)
balreal: .
sysb = balreal(sys)
LTI sys
sysb .
ssbal: .
[sysb, T] = ssbal(sys)
(A,B,C,D) sys
TAT 1 TB
1
0
CT
T
.
(TAT 1 , TB , CT 1 , D)
sysb z=Tx .
modred:
rsys = modred(sys, elim)
rsys = modred(sys, elim, ?mdc?)
rsys = modred(sys, elim, ?del?)
sys
balreal .
rsys = modred(sys, elim) rsys = modred(sys, elim, ?mdc?)
( ) rsys
. elim .
185
.
sys = sys1+sys2
6
LTI .
sys1 sys2
H1, H2
y = ( H 1 + H 2 )u
6
2.
.
( A1 , B1 , C1 , D1 ) sys1, ( A2 , B2 , C 2 , D2 ) sys2 sys1+sys2
.
A1
0
0
A2
B1
B
2
[C1
C2 ] ,
D1 + D2
.
sys = sys1*sys2
6
3 LTI .
186
y = H 1v = H 1 ( H 2 u ) = ( H 1 H 2 )u
.
A1
0
B1C 2 B1 D2
,
, [C
A2 B2 1
D1 C 2 ], D1 D2
6-3 .
inv .
sys = inv(sys)
.
y = Hu u = H 1 y
.
A, B, C, D D
.
.
A BD 1C
BD 1
, D 1 C? D 1
2)
.
sys = [sys1, sys2]
4 .( H 1
187
H 2 sys1 sys2 .)
4.
3 .
.
append: LTI
sys = append(sys1, sys2, , sysN)
6
3.
append
augstate
connect
feedback
ord2
parallel
series
star
LTI
LTI sys1,
,sysN
.( 6
5)
LTI
H 1 ( s ),# , H N ( s )
188
sys .
0
H 1 ( s)
0
H 2 ( s)
"
"
0
0
6
#
0
#
0
$
"
# H N ( s)
( A1 , B1 , C1 , D1 ) , ( A2 , B2 , C 2 , D2 )
5.
sys1, sys2
append(sys1,sys2) .
A
x1 = 1
x 0
2
0 x1 B1
+
A2 x 2 0
y1 C1 0 x1 D1
y. = 0 C x + 0
2 2
2.
0 u1
B2 u 2
0 u1
D2 u 2
x = Ax + Bu
y = Cx + Du
sys augstate x y
.
189
x = Ax + Bu
y C
D
=
x
+
x I
0 u
.
parallel: LTI
sys = parallel(sys1, sys2)
sys = parallel(sys1, sys2, inp1, inp2, out1, out2)
LTI
.
sys = parallel(sys1, sys2)
6
sys = sys1+sys2
6-6
.
sys=parallel(sys1, sys2, inp1, inp2, out1, out2)
( 6
u1 sys2 u2 .
out1 out2 sys1 y1 sys2 y2
. sys [v 1 ; u; v 2 ], [z 1 ;
y; z 2 ] .
series : LTI
sys = series(sys1, sys2)
sys = series(sys1, sys2, outputs1, inputs2)
LTI
190
.
sys = series(sys1, sys2)
6
sys = sys1*sys2
7.
.
sys = series(sys1, sys2, outputs1, input2)
( 6
7)
. outputs1 input2
sys1 y1 sys2 y2
.
sys u, y .
feedback : LTI
8.
.
sys =
feedback(sys1,sys2,feedin,feedout)
( 6-9) sys .
191
.
.
.
.
6
6
4.
4 .
class
size
isct
isct
ctrb
obsv
gram
.
ctrb:
Mc = ctrb(A, B)
Mc = ctrb(sys)
ctrb(A, B) A(n*n) B(n*m)
MC = B
AB # A n 1 B
. n*nm .
Mc = ctrb(sys) sys .
.
Mc = ctrb(sys.A, sys.B)
192
.
rank(Mc)
n .
obsv:
Mo = obsv(A, C)
Mo = obsv(sys)
M o = C T AT C T
( A 2 )T C T
# ( An1 )T C T
. .
gram:
Wc = gram(sys, ?c?)
Wo = gram(sys, ?o?)
, .
(A, B, C, D)
WC = e A BB T e A d
T
WO = e A C T Ce A d
T
. .
WC = A k BB T ( AT ) k
k =0
193
WO = ( AT ) k C T CA k
k =0
A .
6
.
6
5.
pole,eig
tzero
damp
dcgain
pzmap
norm
covar
esort
esort
(DC)
/
LTI (H2 H )
pole: LTI
p = pole(sys)
SISO MIMO LTI sys p .
eig .
tzero: LTI
z = tzero(sys)
[z, gain] = tzero(sys)
z = tzero(a, b, c, d)
SISO MIMO .
z = tzero(sys) LTI sys .
194
k = D-CA B
.
z=1
k = d+C(I-A) 1 B
.
DC .
pzmap: LTI
pzmap(sys)
[p, z] = pzmap(sys)
195
1
2
Trace[ H
( j ) H ( j )]d
w y=Hw .
.
H
= max H ( j )
(SISO)
= max max H ( j )
(MIMO)
.
H ( z)
= max max ( H ( j ))
[ 0 , ]
2]
>>pole(h)
ans =
-1.7971 + 2.2137I
-1.7971 - 2.2137I
-0.2629 + 2.7039I
-0.2629 - 2.7039I
>>tzero(h)
ans =
-0.0500 + 2.7382I
-0.0500 - 2.7382I
-2.0000
>>dcgain(h)
ans =
1
197
ninf =
1.3402
fpeak =
1.8537
.
LTI
. , , ,
.
LTI
6
6 .
.
step(sys)
impulse(sys)
initial(sys, x0)
6
6.
step
impulse
initial
lsim
gensig
% x0
198
10.
10 .
>>step(sys, 10)
% 0 10s
199
. .
>>t = 0:0.01:10
%0.01s 10s
>>step(sys, t)
lsim .
>>t = 0:0.01:10;
>>u = sin(t);
>>lsim(sys, u, t)
LTI sys 10s 0.01s
.
LTI
.
step(sys1, sys2, , sysN)
impulse(sys1, sys2, , sysN)
initial(sys1, sys2, , sysN)
.
,
(3 1 1) .
>>step(sys1, ?r?, sys2, ?b--?)
sys1 , sys2 .
.
[ 6
3] ( 6
11)
sysc = tf(1000, [1 10 1000]);
sysd = c2d(sysc,0.2)
% 0.2
%
200
.
LTI .
, , DC , ,
.
.
7.
bode
nichols
nyquist
sigma
margin
11.
Bode
Nichols
Nyquist
201
evalfr
fregres
p
bode(sys)
nichols(sys)
nyquist(sys)
sigma(sys)
LTI sys , , ,
. sys
.
>>bode(sys, {wmin, wmax})
wmin wmax .
>>bode(sys, [0.1, 100])
% 0.1 100rad/s
.
>>w = logspace(-1, 2, 100)
>>bode(sys, w)
.
bode(sys1, sys2, , sysN)
nichols(sys1, sys2, , sysN)
nyquist(sys1,sys2,# , sysN)
sigma(sys1,sys2, #, sysN)
202
.
bode(sys1, S1, sys2, S2, , sysN, SN)
6
12 .
12.
.
.
, LQG
.
203
.
( )
. 6
13
.
P(s) H(s) k .
q(s) = 1+kP(s)H(s)
.
k
6-13.
k . SISO
.
MATLAB
.( 6
8)
rlocus .
rlocus(sys)
rlocus(sys, k)
k .
6
8.
rlocus
rlocfind
pzmap
-
204
sgrid
zgrid
s-
z-
rlocus(sys, k) k
.
rlocfind k
rlocus .
[k, poles] = rlocfind(sys)
k poles .
SISO sys
. rlocus(sys)
.
[ 6
h( s ) =
4]
2 s 2 + 5s + 1
s 2 + 2s + 3
=0.707 .
h = tf([2 5 1], [1 2 3]);
rlocus(h)
k = rlocfind(h)
%
% =0.707
.
.
. .
x = Ax + Bu
205
u = -Kx
x =(A-BK)x
. (A,B)
K .
.
^
x .
x = A x + Bu + L( y C x Du )
= ( A LC ) x + ( B LD )u + Ly
A-LC
(C, A) L
.
. A BK
.x =
e 0
BK x
A LC e
e= xx
(A-BK) (A-LC)
.
206
.( 6
9)
9.
acker
place
estim
reg
Ackermann (SISO )
(MIMO )
5]
0.4 0 0.01
6.3
x = 1
0
0 x + 0 u
1.4 9.8 0.02
9.8
y=[0 0 1]x
-1+j, -1-j, -2 K .
207
.
>>A = [-0.4, 0, -0.01; 1, 0, 0;-1.4, 9.8, -0.02];
>>B = [6.3; 0; 9.8]; C = [0 0 1]; D = [];
>>sys = ss(A,B,C,D);
>>pole(sys)
ans =
-0.6565
0.1183+0.3678i
0.1183-0.3678i
>>rank(ctrb(A, B))
ans =
3
. -2 -1j
.
>>p = [-2 -1+j -1-j];
>>K = place(A, B, P)
K =
0.4706 1.0000 0.0627
>>pole(ss(A-BK, B, C, D))
ans =
-2.0000
-1.0000+1.0000i
-1.0000-1.0000i
estim:
est = estim(sys, L)
208
sys L /
est . est (SS ) .
x =Ax+Bu
y=Cx+Dw
sys est
x = A x + L( y C x)
C
y = x
x I
y x .
reg:
rsys = reg(sys, K, L)
sys, K,
L rsys . K L
LQG .
.
x = Ax+Bu
y = Cx+Du
.
x = [ A LC ( B LD) K ] x + Ly
u = K x
209
. LQG
2 (LQG)
(trade-off)
.
() .
LQG 6
14 .
y
.
w u
y v = y + v
.
14. LQG
x = Ax + Bu + G
y v = Cx + Du + H + v
w v
E ( T ) = Qn , E (vv T ) = Rn , E (v T ) = N n
.
LQG
.
2
J [u ] = ( x T Qx + 2 x T Nu + u T Ru )dt
0
. Q, N, R .
210
x = A x + Bu + L( y v C x Du )
= ( A LC ) x + ( B LD)u + Ly v
u = K x
.
K L
.
LQG 6
15
.
x = [ A LC ( B LD ) K ] x + Ly v
u = K x
LQG
.
6-15. LQG
6
10.
lqr
dlqr
lqry
lqrd
kalman
kalmd
lqgreg
LQG
LQ
LQ
LQ
LQ
LQG
211
care
dare
lqr: LQ
[K, S, e] = lqr(A, B, Q, R)
[K, S, e] = lqr(A, B, Q, R, N)
(A, B)
x = Ax + Bu
2
J [u ] = ( x T Qx + u T Ru + 2 x T Nu )du
0
u =
Kx
K .
N N = 0 .
S
AT S + SA ( SB + N ) R 1 ( B T S + N T ) + Q = 0
e e = eig(A
B*K). K
K = R 1 ( B T S + N T )
.
dlqr: LQ
[K, S, e] = dlqr(A, B, Q, R)
[K, S, e] = dlgr(A, B, Q, R, N)
212
(A, B)
x(k+1) = Ax(k)+Bu(k)
2
J [u ] = [ x T (k )Qx(k ) + u T (k ) Ru (k ) + 2 x T (k ) Nu (k )]
k =1
u(k) =
Kx(k)
K .
AT SA S ( AT SB + N )( B T SB + R) 1 ( B T SA + N T ) + Q = 0
S
e = eig(A-B*K)
. K
K = ( B T SB + R) 1 ( B T SA + N T )
.
lqry: LQ
[K, S, e] = lqry(sys, Q, R)
[K, S, e] = lqry(sys, Q, R, N)
J [u ] = ( y T Qy + u T Ru + 2 y T Nu )dt
0
A, B C, D
SS sys .
Kalman:
213
x = Ax + Bu + Gw
y v = Cx + Du + Hw + v
{A, [B G], C, [D H]} . Qn, Rn, N
.
kest
x = A x + Bu + L( y v C x Du )
C D
y = x + u
x I 0
. kest [u; y v ] ,
[ y; x] SS .
L , P
.
.
lqgreg: LQG
rlqg = lqgreg(kest, K)
kalman (L) lqr, dlqr lqrg
(K) LQG .
214
x = [ A LC ( B LD ) K ] x + Ly v
u = K x
ss rlqg .( 6
[ 6
6] LQG 6
15)
16
. y .
E (d 2 ) = 1, E (n 2 ) = 0.01
J [u ] = (10 y 2 + u 2 )dt
0
x = Ax + Bu + Bd
y n = Cx + n
. (A, B, C)
16.
100 /( s 2 + s + 100) .
LQG F(s)
.
%
sys = ss(tf(100, [1 1 100]))
% LQ K
K = lqry(sys, 10, 1)
% u d
[A, B, C, D] = ssdata(sys)
% [u; d], y
P = ss(A, [B B], c, [D D])
% Kest
215
17.
216
17
.
. MATLAB
general
ops
graphics
specfun
elfun
lang
elmat
iofun
strfun
polyfun
sparfun
winfun
Windows
matfun
, demos
datafun
specgraph
funfun
datatypes
graph2d
uitools
(GUI)
graph3d
timefun
(General
(General purpose commands)
1) (General
(General Information)
help
helpdesk
whatsnew Readme
ver
helpwin
demo
readme
Readme
2) (Managing
(Managing the workspace)
who
whos
clear
load
save
quit
MATLAB
3) (Managing
(Managing commands and functions)
what
type
edit
M-
lookfor
which
mex
M-
MEX-
4) (Managing
(Managing the search path)
path
addpath
rmpath
editpath
5) (Controlling
(Controlling the command window)
echo
more
diary
format
6) (Operating
(Operating system commands)
cd
copyfile
pwd
dir
delete
getenv
mkdir
!
218
(OS)
dos
DOS
unix
UNIX
computer
7) M-
M-(Debugging
(Debugging M-files)
debug
dbstop
dbclear
dbcont
dbdown
dbstep
dbstatus
dbup
dbtype
M-
dbquit
3. (Elementary
(Elementary math functions)
1)
1)(Trigonometric)
(Trigonometric)
sin
sinh
asin
asinh
cos
cosh
acos
acosh
tan
tanh
atan
atan2
atanh
sec
sech
asec
asech
csc
csch
acsc
acsch
cot
coth
acot
acoth
219
2) (Exponential)
(Exponential)
exp
log
log10
log2
sqrt
pow2
3) (Complex)
(Complex)
abs
angle
conj
imag
real
isreal
cplxpair
4) (Rounding
(Rounding and remainder)
fix
rem
floor
ceil
round
mod
sign
4.
(Elementary
(Elementary matrices and matrix
manipulation)
1) (Elementary
(Elementary matrices)
zeros
ones
eye
repmat
rand
randn
220
linspace
logspace
meshgrid
2) (Basic
(Basic array information)
size
length
ndims
disp
isempty
isnumeric
islogical
logical
3) (Matrix
(Matrix manipulation)
reshape
tril
triu
end
fliplr
flipud
flipdim
rot90
find
diag
90
4) (Special
(Special variables and constants)
ans
eps
realmax
realmin
pi
3.1415926535897....
i, j
inf
NaN
isnan
isinf
isfinite
flops
221
why
5) (Specialized
(Specialized matrices)
compan
toeplitz
hankel
vander
hilb
magic
invhilb
pascal
5. (Character
(Character String Function)
char
double
blanks
deblank
strcmp
strncmp
upper
lower
num2str
int2str
hex2num
16 -10
hex2dec
10 -16
bin2dec
2 -10
dec2bin
10 -2
eval
MATLAB
str2num
6.
(Matrix
(Matrix functions - numerical linear
algebra)
1) (Matrix
(Matrix analysis)
norm
normest
rank
det
trace
null
222
orth
rref
subspace
2) (Linear
(Linear equations)
\ , /
inv
cond
condest
chol
lu
LU
qr
QR
nnls
pinv
lscov
1-
3) (Eigenvalues
(Eigenvalues and singular values)
eig
svd
eigs
svds
poly
polyeig
condeig
hess
qz
schur
4) (Matrix
(Matrix functions)
expm
logm
sqrtm
funm
5) (Factorization
(Factorization utilities)
qrdelete
QR
qrinsert QR
223
balance
rsf2csf
cdf2rdf
planerot
7. (Data
(Data analysis and Fourier transforms)
1) (Basic
(Basic operations)
max
min
mean
median
std
sort
sum
prod
trapz
hist
cumsum
cumprod
2) (Finite
(Finite differences)
diff
gradient
del2
corrcoef
cov
subspace
3) (Filtering
(Filtering and convolution)
filter
filter2
2
2
conv
conv2
convn
deconv
224
4) (Fourier
(Fourier transforms)
fft
fft2
fftn
ifft
ifft2
ifftn
fftshift
ifftshift
FFTSHIFT.
8. (Interpolation
(Interpolation and polynomials)
1) (Data
(Data interpolation)
interp1
interpft
interp3
interp1q
FFT 1 interp2
3
interpn
2
N
2) (Spline
(Spline interpolation)
spline
ppval
3) (Polynomials)
(Polynomials)
roots
poly
polyval
polyvalm
residue
polyfit
polyder
225
9.
(Function
(Function functions and ODE
solvers)
1) (Optimization)
(Optimization)
fmin
fzero
fmins
2) (Numerical
(Numerical integration)
quad
dblquad
quad8
3) (Plotting)
(Plotting)
ezplot
fplot
4) (Ordinary
(Ordinary differential equation solvers)
ode45
ode23
ode113
ode23t
ode15s
ode23s
ode23tb
odefile
odeplot
odephas3
odephas2
226
10. (Handle
(Handle Graphics)
1) (Figure
(Figure window creation and control)
figure
gcf
clf
shg
close
refresh
2) (Axis
(Axis creation and control)
subplot
axes
gca
cla
axis
hold
ishold
3) (Handle
(Handle Graphics objects)
line
patch
image
uicontrol
text
surface
light
light ,
uimenu
uicontextmenu
set
get
reset
delete
gco
drawnow
printopt
227
orient
newplot
11. 2 (Two
(Two dimensional graphs)
1) X-Y (Elementary
(Elementary X-Y graphs)
plot
loglog
semilogx
semilogy
polar
plotyy
2) (Axis
(Axis control)
grid
box
hold
xlim
ylim
zlim
3) (Graph
(Graph annotation)
legend
title
xlabel
X-
ylabel
Y-
text
gtext
12. 3 (Three
(Three dimensional graphs)
1) 3 (Elementary
(Elementary 3-D plots)
plot3
surf
mesh
fill3
228
3
3
2) (Color
(Color control)
colormap
hidden
caxis
3) (Viewpoint
(Viewpoint control)
view
viewmtx
4) (Graph
(Graph annotation)
title
xlabel
ylabel
zlabel
colorbar
13. (File
(File input/output)
1) (File
(File opening and closing)
fopen
fclose
2) 2 (Binary
(Binary file I/O)
fread
fwrite
3) (Formatted
(Formatted file I/O)
fscanf
fprintf
229
fgets
fgetl
input
4) (String
(String conversion)
sprintf
sscanf
5) (File
(File positioning)
ferror
fseek
feof
ftell
6) (File
(File name handling)
matlabroot MATLAB
tempdir
partialpath
tempname
7) (File
(File import/export functions)
load
MAT
save
MAT
dlmread
dlmwrite
imread
imwrite
230
8) (Command
(Command window I/O)
clc
home
pause
(cursor)
disp
14. (Time
(Time and dates)
now
date
clock
datestr
calendar
weekday
tic, toc
etime
cputime
MATLAB CPU
15. (Demonatration)
(Demonatration)
demo
1) MATLAB/
MATLAB/(Matrices)
(Matrices)
intro
inverter
buckydem
matmanip
eigmovie
rrefmovie
delsqdemo
sepdemo
eigshow
svdshow
2) MATLAB/
MATLAB/(Numerics)
(Numerics)
funfuns
e2pi
fitdemo
bench
231
MATLAB
fftdemo
FFT
lotkademo
quaddemo
zerodemo
fplotdemo
quake
3) MATLAB/
MATLAB/(Visualization)
(Visualization)
graf2d
graf2d2
grafcplx
imagedemo
MATLAB
earthmap
xfourier
cplxdemo
4) MATLAB/
MATLAB/(Language)
(Language)
xplang
MATLAB
graf3d
hndlgraf
hndlaxis
5) MATLAB/
MATLAB/(ODE)
(ODE)
odedemo
a2ode
a3ode
b5ode
brussode
vdpode
16. (Programming
(Programming language constructs)
if
elseif
for
IF
else
return
while
232
IF
break
WHILE,FOR switch
case
SWITCH
try
TRY
catch
CATCH
function
global
nargout
otherwise
end
nargin
SWITCH
17.
17.(System
(System Identification Toolbox)
Readme
Readme
iddemo
midprefs
2) (Simulation
(Simulation and prediction)
idsim
poly2th
theta
pe
predict
3) (Data
(Data manipulation)
dtrend
idfilt
idinput
idresamp
233
4) (Nonparametric
(Nonparametric estimation)
covf
cra
etfe
spa
5) (Parameter
(Parameter estimation)
ar
AR
armax
ARMAX
arx
ARX LS
canstart
Theta
bj
B-J
ivar
AR
iv4
ARX n4sid
oe
pem
6) (Model
(Model structure creation)
arx2th
ARX Theta
canform
7) (Model
(Model conversions)
idmodred
th2arx
ARX Theta
th2par
Theta
th2poly
Theta
th2ss
Theta
th2tf
Theta
th2zp
Theta
thc2thd
234
thd2thc
8) (Model
(Model presentation)
bodeplot
idplot
present
ffplot
nyqplot
Theta
zpplot
9) (Information
(Information extraction)
getmfth
m-
getncap
gett
getff
getzp
10) (Model
(Model validation)
compare
pe
idsim
resid
11) (Model
(Model structure selection)
selstruc
struc
ivstruc
arxstruc
ARX
235
12) (Recursive
(Recursive parameter estimation)
rarx
ARX
rarmax
ARMAX
rbj
B-J
roe
rpem
rplr
18.
18.(Signal
(Signal Processing Toolbox)
1)
(Waveform
(Waveform generation)
chirp
diric
pulstran
rectpuls
sinc
sinc
gauspuls
tripuls
sawtooth
square
2) (Filter
(Filter analysis and implementation)
abs
angle
conv
fftfilt
filter
filtic
fft FIR
filtfilt
filter
freqs
freqspace
grpdelay
zplane
latcfilt
freqz
impz
unwrap
236
3) (Linear
(Linear system transformations)
convmtx
residuez Z
sos2ss
sos2tf
sos2zp
ss2tf
tf2ss
zp2sos 2
zp2ss
latc2tf
zp2tf
poly2rc
rc2poly
ss2sos
ss2zp
tf2zp
4) (IIR
(IIR digital filter design)
butter
cheby1
cheby2
ellip
maxflat
yulewalk -
5) (IIR
(IIR filter order selection)
buttord
cheb2ord 2
cheb1ord 1
ellipord
237
6) (FIR
(FIR filter design)
cremez
FIR
fir1
FIR()
fir2
FIR()
fircls
fircls1
firls
, FIR
FIR
intfilt FIR
kaiserord fir1
remez
remezord
Parks-McClellanFIR
Parks-McClellanFIR
7) (Transforms)
(Transforms)
czt
dct
dftmtx
fft
fft2
ifft2
idct
ifft
fftshift
hilbert
8) (Statistical
(Statistical signal processing and spectral
analysis)
cohere
corrcoef
cov
csd
tfe
xcorr
238
xcov
xcorr2
pburg
()
pmusic
(MUSIC)
psd
(Welch)
pyulear
(- AR)
spectrum
pmtm
9) (Windows)
(Windows)
bartlett
blackman
boxcar
chebwin
hamming
hanning
kaiser
triang
10) (Parametric
(Parametric modeling)
invfreqs
levinson
invfreqz
prony
Prony IIR
stmcb
Steiglitz-McBride ARMA
lpc
11) (Specialized
(Specialized operations)
cceps
deconv
decimate
demod
239
interp
interp1
medfilt1
modulate
resample
specgram
vco
spline
rceps
12) (Analog
(Analog lowpass filter prototypes)
besselap
buttap
cheb1ap
cheb2ap
ellipap
13) (Frequency
(Frequency translation)
lp2bp
lp2bs
lp2hp
lp2lp
14) (Filter
(Filter discretization)
bilinear
impinvar
15)
besself
cplxpair
conv2
detrend
240
polystab
stem
sptool
(GUI)
16) (Demonstrations)
(Demonstrations)
cztdemo
z FFT
moddemo
filtdemo
sosdemo
241