Professional Documents
Culture Documents
1
1.1
MATLAB ........................... 1
1.2
MATLAB ............... 2
1.3
MATLAB ....................... 3
1.4
MATLAB ....................... 4
1.5
MATLAB ............................... 5
1.5.1
(Command Window)... 5
1.5.2
(Command
History) ............................... 9
1.5.3
(Current Directory)
2.4
2.5
(Workspace)...... 12
1.5.5
(Help)........................ 13
1.6
MATLAB ......................... 14
1.7
MATLAB ......................... 14
1.7.1
....... 14
1.7.2
.............. 15
1.8
MATLAB ..................... 16
1.9
...................................................... 18
1.10
.................................................... 18
2.2
2.3
.............................................. 20
2.1.1
MATLAB ................. 20
2.1.2
.............................. 21
2.1.3
....... 22
2.1.4
...................................... 23
2.1.5
...................................... 23
2.1.6
... 26
.............................................. 27
2.2.1
.............................. 27
2.2.2
........... 28
2.2.3
.............. 29
.............................................. 31
2.3.1
.............. 31
2.3.3
.............................. 34
2.3.4
....................... 40
............................................... 48
2.4.1
....... 48
2.4.2
....................... 48
2.4.3
....................... 51
2.4.4
........... 54
2.4.5
... 57
........................................... 59
.......................................... 59
2.5.2
............... 60
2.5.3
........................... 62
2.6
...................................................... 63
2.7
...................................................... 63
3 MATLAB ............................. 65
3.1
2 MATLAB ............................ 20
2.1
... 31
2.5.1
.......................................... 11
1.5.4
2.3.2
3.2
3.3
................................................... 65
3.1.1
............... 65
3.1.2
................... 65
3.1.3
........... 68
3.1.4
........................... 70
........................................... 71
3.2.1
................... 71
3.2.2
....................... 77
........................................... 79
3.3.1
.............................. 79
3.3.2
-
.......................................... 81
3.4
3.5
........................... 83
3.4.1
...................................... 84
3.4.2
...................................... 85
............................................... 89
3.5.1
....................... 90
MATLAB
VI
3.6
3.5.2
...................... 92
3.5.3
... 93
5.6.1
............. 149
........................... 95
5.6.2
() ...... 150
3.6.1
...................................... 96
5.6.3
() ...... 152
3.6.2
5.6
..................................... 149
- ........................ 99
5.7
.................................................... 154
3.7
.....................................................102
5.8
.................................................... 154
3.8
.....................................................102
4 ...................104
4.1
4.2
M ................................................ 157
.............................................104
6.1.1
............. 157
4.1.1
.....................104
6.1.2
M ............ 158
4.1.2
.....................106
6.1.3
................................ 159
.............................................113
6.1.4
................................ 160
4.2.1
.....................113
6.1.5
................................ 161
4.2.2
.....................115
4.2.3
.........................123
6.2.1
................................ 164
4.3
.....................................................124
6.2.2
................................ 169
4.4
.....................................................124
6.2.3
......................... 173
5 MATLAB ...........................128
5.1
5.2
5.3
5.4
5.5
VI
6.3
......................128
5.1.1
.....................128
5.1.2
.............................130
5.1.3
.................................131
.....................132
5.2.2
.....................133
.............134
5.3.2
.............................135
..................................140
5.4.1
.................................140
5.4.2
.................................141
5.4.3
.................................141
5.4.4
Taylor .............142
.....................................144
(input).............. 173
6.3.2
6.3.3
M /
6.3.4
/
(fprintf / fscanf)............... 174
6.3.5
/
(fwrite/ fread) .................. 174
......................134
5.3.1
6.3.1
.....................................132
5.2.1
............................. 173
6.3.6
/
(fgetl / fgets)............................ 175
6.4
6.5
..................................... 177
6.5.1
........................................ 177
6.5.2
..... 179
5.5.1
..........144
6.7
............................................. 182
5.5.2
......145
6.8
.................................................... 183
5.5.3
Z ..................147
6.9
.................................................... 183
7 MATLAB .......................185
7.1
7.2
7.3
7.4
.............................................185
7.1.1
MATLAB ............186
7.1.2
.................187
7.1.3
.................189
7.1.4
.............................191
7.1.5
.........................193
7.1.6
.........................................194
VII
.............................................195
7.2.1
.............................196
7.2.2
.............................196
7.2.3
.................................200
7.2.4
.........................202
.....................................................205
7.3.1
.................205
7.3.2
.............................207
.............................................208
7.4.1
.........................208
7.4.2
.........................209
7.5
.....................................................212
7.6
.....................................................212
......................................... 217
8.2.1
............. 217
8.2.2
................................ 218
8.2.3
............................ 221
8.2.4
......................... 223
8.2.5
8.2.6
8.2.7
............................ 235
8.2.8
................................ 239
.................................................... 245
8.5
.................................................... 246
VII
MATLAB
MATLAB
Cleve Moler MATLAB
MATLAB MATLAB
MATLAB
1.1
MATLAB
20 70
Cleve Moler
Fortran EISPACK LINPACK MATLAB
MATLAB Matrix Laboratory() MATLAB
MATLAB EISPACK LINPACK
Fortran
MATLAB 20 80 John Little
MATLAB C MATLAB1984 Cleve Moler
John Little MathWorks MATLAB
MATLAB
1993
MathWorks PC Windows MATLAB 4.0
1994 4.2 4.0
MATLAB
MATLAB
MATLAB MATLAB
1.2
MATLAB
MATLAB
MATLAB MATLAB
MATLAB
MATLAB
MATLAB ()
MATLAB
MATLAB
1.
MATLAB
MATLAB MATLAB
C Fortran
2.
MATLAB
C Fortran
MATLAB
3.
MATLAB
()
4.
MATLAB ()
5.
MATLAB
MATLAB
6. C Fortran
MEX C Fortran
MATLAB C Fortran
MATLAB
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
(11)
(12)
(13)
1.3
MATLAB
MATLAB MATLAB
MATLAB
MATLAB
MATLAB
MATLAB Internet
MATLAB 40
(1) (Bioinformatics Toolbox)
(2) (Communication Toolbox)
(3) (Control System Toolbox)
(4) (Curve Fitting Toolbox)
(5) (Data Acquisition Toolbox)
(6) (Filter Design Toolbox)
(7) (Financial Toolbox)
(8) (Frequency System Identification Toolbox)
(9) (Fuzzy Logic Toolbox)
(10) (Genetic Algorithm and Direct Search Toolbox)
(11) (Image Processing Toolbox)
(12) (Mapping Toolbox)
(13) (Model Predictive Control Toolbox)
(14) (Neural Network Toolbox)
(15) (Optimization Toolbox)
(16) (Partial Differential Equation Toolbox)
(17) (Signal Processing Toolbox)
(18) (Simulink Toolbox)
(19) (Statistics Toolbox)
(20) (Symbolic Math Toolbox)
(21) (System Identification Toolbox)
(22) (Wavelet Toolbox)
1.4
MATLAB
MATLAB MATLAB
MATLAB
(1) Windows 2000Windows XP
(2) Pentium IIIPentium IV CPU
(3) 128MB
(4) 10GB
(5) 16 800600
4
MATLAB
(6)
MATLAB MATLAB
MATLAB
1.5
MATLAB
MATLAB 1.1
MATLAB 4
(Command Window)(Command History)(Current
Directory) (Workspace)MATLAB 6.5
(Start)
1.1 MATLAB
MATLAB 5
1.5.1
(Command Window)
MATLAB MATLAB
Command Window
MATLAB
M MEX
MATLAB MATLAB
5
MATLAB
MATLAB
Desktop Undock
Command Window
1.2
Desktop Dock Command Window
1.2
1.
1.2 >>(
) Enter MATLAB
1.2
iffor
2.
() 1-1
1-1
Home
End
Delete
Backspace
MATLAB
MATLAB
DOS MATLAB
DOS 1.8
3.
MATLAB 1-2 MATLAB
1-2
1-2 MATLAB
''
()
[]
{}
%">>"
"%"
MATLAB
4.
MATLAB
1-3
short
1-3 e
short()
2.7183
short e
2.7183e+000
short g
2.7183
4 3 short e
1 4
5 10 5
short e
long
long e
2.71828182845905
2.718281828459046e+000
14 2 16
long e
15
15 10 +15 -5
long g
2.71828182845905
long e
rational
1457/536
hex
4005bf0a8b14576a
bank
2.72
compact
loose
2
MATLAB IEEE
5.
MATLAB File Preferences
( 1.3 ) format long
format long
FilePreferences 1.3
MATLAB
1.3 Preferences
6.
MATLAB Edit|Clear
Command Window clc
1.5.2
(Command History)
MATLAB
MATLAB 1.4
1.4
M
1.
1-4
Windows Ctrl Shift
MATLAB
10
1-4
Edit Copy
Evaluate
Selection
Create M-File
M / M
(1)
(2) Shift
(3) Edit | Copy Copy
(4) Paste
1.5
1.5
(1)
(2) Ctrl
(3) Evaluate Selection
2.
Edit Clear Command History
10
1
1.5.3
MATLAB
11
(Current Directory)
MATLAB Windows
MATLAB MATLAB
M
MAT
1.6
1.6
MATLAB
MATLAB \MATLAB\work
cd
cd
cd
cd f:\matfiles
cd ..
cd
11
MATLAB
12
(Workspace)
1.5.4
MATLAB
1.7
MATLAB () MATLAB
(.MAT )
(1)
1.7
(2)
1.
1-6
1-6
Save Workspace As
MAT
Save Selection As
MAT
Delete
2.
MATLAB 3
1.8
(1) save .mat
save 1 2 3
12
MATLAB
13
% dataf.mat
% AB var_ab.mat
% C var_ab.mat
>>save dataf
>>save var_ab A B
>>save var_ab C-append
load 1 2 3
.mat
% dataf.mat
% var_ab.mat AB
>>load dataf
>>load var_ab A B
(3) clear
clear 1 2 3
>>clear
>>clear A B
%
% AB
clear
1.5.5
(Help)
1.8
13
14
MATLAB
ContentsIndexSearch Demos
(1) Contents
HTML
(2) Index MATLAB
(3) Search
(4) Demos MATLAB Demo
1.6
MATLAB
MATLAB
MATLAB M
MEX
(1) M .m M M MATLAB
MATLAB () M ASCII
1.7
MATLAB
MATLAB
1.7.1
lx04_01.m D MATLAB
M 4
D:\MATLAB \M \ 4 M
D:\MATLAB \M \ 4 \lx04_01.m
14
MATLAB
15
MATLAB
MATLAB
MATLAB
M MEX
>> xt xtMATLAB
(1) MATLAB xt
(2) xt MATLAB xt
(3) xt.mxt.mex
xt
(4) MATLAB xt.m
xt.mex xt
(5) 4 xt MATLAB
1.7.2
MATLAB
1.
MATLAB File Set Path
1.9
1.9
15
MATLAB
16
1.10
1.8
%begin
%end
MATLAB
MATLAB
MATLAB
16
MATLAB
17
MATLAB
MATLAB
clc clear cd
MATLAB 4
1-7 1-10
1-7
save lx01
save
save lx02 A B
load
load lx01
who
who
whos
whos
clear
clear A
1-8
format bank
format
format compact
1-3
MATLAB on off
echo
more
more(10)
clc
clc
clf
clf
cla
cla
close
close all
all
1-9
pwd
pwd
cd
cd d:\xt_mat\04
cd
17
MATLAB
18
mkdir
mkdir xt_mat
dir
dir
what
what
MMATMEX 3
which
which inv.m
type
type xt06.m
delete
delete xt01.m
1-10
help
help mkdir
lookfor
lookfor Z
helpwin
helpwin graphics
MATLAB M
1.9
MATLAB
MATLAB
5
MATLAB MATLAB
()
MATLAB
1.10
1.
(1) (
A. clear
B. clc
C. clf
D. cls
(2) MATLAB (
)
A.
B. workspace
C.
D.
18
MATLAB
(3) (
A.
B.
C.
D.
19
2.
(1) MATLAB
(
)
A.
B.
C.
D.
E.
(2) MATLAB
(
)
A.
B.
C.
D.
E. MATLAB
(3) MATLAB
(
)
A.
B.
C.
D.
E.
F.
(4) (
)
A.
B.
C. M
D.
3.
(1) MATLAB __________
(2) MATLAB __________()
(3) MATLAB MATLAB 4
__________Command HistoryWorkspace Current Directory
19
MATLAB
MATLAB
MATLAB
MATLAB
MATLAB
MATLAB
2.1
MATLAB
2.1.1
MATLAB
MATLAB
MATLAB 2.1
MATLAB C MATLAB
MATLAB C++
MATLAB
MATLAB
MATLAB
MATLAB
21
uint8
uint16
uint32
uint64
int8
int16
int32
int64
2.1 MATLAB
1.7.1
2.1.2
y=0.618*x 0.618
s='Tomorrow and Tomorrow'
Tomorrow and Tomorrow
MATLAB pi
3.1415926 C 2-1
MATLAB
(1) 3
(( )% )
21
MATLAB
22
2-1 MATLAB
ij
i 2 = j2 = 1
Inf inf
NaN
0/0 / 0*
pi
eps
eps
PC 2-52
Realmin realmin
2-1022
Realmax realmax
21023
(2) aA
(3) 63 63 MATLAB 6.5
31
(4) ( ifwhile )
(5) 2-1
f(x)y'y''A2
2.1.3
MATLAB
(1)
MATLAB
2.1.5 2.4
(2) MATLAB
MATLAB
MATLAB
(3) MATLAB
n 1n n1
(4) MATLAB
11
22
MATLAB
23
MATLAB
(5) MATLAB
a=[1 2;3 4] a
a
2.1.5
(6)
2.1.4
MATLAB 1.1
MATLAB S='I Have a Dream.'
S
MATLAB
ASCII
2.5
2.1.5
MATLAB
1.
2-2
2-3
2-2
C=A+B
C(i,j)=A(i,j)+B(i,j)
C=A-B
C(i,j)=A(i,j)-B(i,j)
C=A*B
C=A/B
C=A\B
C=A^B
AB
'
B=A'
B A
23
MATLAB
24
2-3
.*
C=A.*B
C(i,j)=A(i,j)*B(i,j)
./
C=A./B
C(i,j)=A(i,j)/B(i,j)
.\
C=A.\B
C(i,j)=B(i,j)/A(i,j)
.^
C=A.^B
C(i,j)=A(i,j)^B(i,j)
.'
A.'
2-2 2-3
(1)
MATLAB
(2) 2-3
(3)
(4) 2-3
2.
MATLAB 2-4
2-4
<
A<B
<=
A<=B
>
A>B
>=
A>=B
3AB A B
==
A==B
A B
2AB
10
10
=
A=B
4===
MATLAB
2-4
3.
MATLAB MATLAB
2-5
24
MATLAB
25
2-5
&
A&B
A|B
&&
A&&B
||
A||B
1 0
3AB A B
A B
10
4
MATLAB
MATLAB
1()
1()
1()
4.
MATLAB
2-6
2-6 MATLAB
'()^().'().^()
()
* /()\().*()./().\()
()
<<=>>===()=()
&()
|()
&&()
||()
MATLAB 2-6
25
MATLAB
26
2.1.6
MATLAB
MATLAB
1.
1 clear
addpath F:\ MATLAB \M -end
MATLAB
general
2.
MATLAB MATLAB
MATLAB
MATLAB MATLAB
MATLAB MATLAB
MATLAB
( 1 2)
sin(A)A
2.4.5
3.
()
MATLAB
A+B&C-sin(A*pi)
(A+B)&C-sin(A*pi)
4.
MATLAB MATLAB
=
F=(A+B)&C-sin(A*pi)
26
MATLAB
27
MATLAB
2.2
(a,b)(a,b)(a,b,c)
(a,b,c) n n
n
MATLAB n
MATLAB
2.2.1
MATLAB 3
1.
=[a1,a2,a3,]
2.1
>>A=[2,3,4,5,6],B=[1;2;3;4;5],C=[4 5 6 7 8 9];
%
C
A =
2
B =
1
2
3
4
5
2.
a1:step:an a1 an
step 1
2.2
>>A=1:2:10,B=1:10,C=10:-1:1,D=10:2:4,E=2:-1:10
27
MATLAB
28
A =
1
10
B =
C =
10
9
8
7
D =
Empty matrix: 1-by-0
E =
Empty matrix: 1-by-0
DE
3.
linspace( )
logspace( )
A=linspace(a1,an ,n) a1 an
n a1 an n-2 n
100
2.3 MATLAB
>>A=linspace(1,50),B=linspace(1,30,10)
2.4 MATLAB
>>A=logspace(0,49),B=logspace(0,4,5)
(1) an
an an an
(2)
an
(3)
2.2.2
MATLAB
2.5
28
MATLAB
29
E1 =
4
6
8
10
12
E2 =
-2
-2
-2
-2
-2
F =
-2
-2
-2
-2
-2
G1 =
3
6
9
12
15
G2 =
1.0000
1.3333
1.6667
??? Error using ==> +
Matrix dimensions must agree.
2.0000
2.3333
H=A+C
2.2.3
MATLAB
1.
(AB)
dot(A,B)AB
2.6
>>A=1:10;B=linspace(1,10,10); AT=A';BT=B';
>>e=dot(A,B),f=dot(AT,BT)
e =
385
f =
385
2.
AB CC A B
29
MATLAB
30
A=Ax i + Ay j + Az k
B=Bx i + By j + Bz k
C=AB=(Ay Bz-Az By)i + (Az Bx -Ax Bz )j + (Ax By-Ay Bx )k
cross(A,B) AB A
B
2.7
>>A=1:3,B=3:5
>>E=cross(A,B)
A =
1
-2
-2
B =
E =
2.8 ()
>>A=1:4,B=3:6,C=[1 2],D=[3 4]
>>E=cross(A,B),F=cross(C,D)
A =
1
B =
C =
D =
3
4
??? Error using ==> cross
A and B must have at least one dimension of length 3.
3.
2.9
>>A=[1 2 3],B=[3 3 4],C=[3 2 1]
>>D=dot(C,cross(A,B))
A =
1
B =
C =
30
2
3
MATLAB
31
D =
4
2.3
MATLAB (
)MATLAB
MATLAB
2.3.1
mn A i j
i j A(i,j)
MATLAB
1 2 34
B 2-7
2-7 B
B(1,1)
B(1,2)
B(1,3)
10
B(1,4)
B(2,1)
B(2,2)
B(2,3)
11
B(2,4)
B(3,1)
B(3,2)
B(3,3)
12
B(3,4)
() C
1 2
2.3.2
MATLAB
1.
MATLAB
31
MATLAB
32
(1)
mn A i j
A(i,j)
(2) mn
A A(i,j) A(s) s = (j-1)mi
ijs
2.10
2.10
>>A=[1 2 3;6 5 4;8 7 9]
A =
1
2
3
6
5
4
8
7
9
>>A(2,3),A(6) % A(2,3) A(6)
ans =
4
ans =
7
>>A(1:2,3)
% A 12 3
ans =
3
4
>>A(6:8)
% A 68
ans =
7
3
4
2.
3
(1)
2.11
>>clear
>>A(1:2,1:3)=[1 1 1;1 1 1]
A =
1
1
1
1
1
1
>>A(3,3)=2
%
% A 12 13 1
%
% 0
A =
1
1
0
1
1
0
1
1
2
(2)
32
MATLAB
33
2.12 ( 2.11)
>>A(3:6)=[-1 1 1 -1]
A =
1
1
1
1
1
1
-1
-1
2
>> A(3)=0;A(6)=0
A =
1
1
1
1
1
1
0
0
2
(3) B A A(:)=B AB
2.13
>> A(:)=1:9
% AA
A =
1
4
7
2
5
8
3
6
9
>> A(3,4)=16,B=[11 12 13;14 15 16;17 18 19;0 0 0]
% A 43 B
A =
1
4
7
0
2
5
8
0
3
6
9
16
B =
11
12
13
14
15
16
17
18
19
0
0
0
>> A(:)=B
% 43 B 34 A
A =
11
0
18
16
14
12
0
19
17
15
13
0
3.
MATLAB ([])
2.14
>>clear
>>A(2:3,2:3)=[1 1;2 2]
A =
0
0
0
0
1
1
0
2
2
>> A(2,:)=[]
% A
% A 2
33
MATLAB
34
A =
0
0
0
2
>> A(1:2)=[]
A =
0
2
>> A=[]
A =
[]
0
2
% A
0
2
%
2.3.3
MATLAB 7
M
MATLAB
(1) ([])
(2)
(3)
(4)
2.4
1.
>>
2.15
>>x=27;y=3;
>>A=[1 2 3;4 5 6];B=[2,3,4;7,8,9;12,2*6+1,14];
>>C=[3 4 5
7 8 x/y
10 11 12];
%
>>A,B,C
A =
1
4
2
5
3
6
2
7
12
3
8
13
4
9
14
3
7
10
4
8
11
5
9
12
B =
C =
34
MATLAB
35
2.
()
2.3.2
1)
2.16
>>clear
>>A=[1 2 3 4;5 6 7
A =
1
2
3
5
6
7
9
10
11
13
14
15
>>B=A(1:3,2:3)
B =
2
3
6
7
10
11
>>C=A([1 3],[2 4])
C =
2
4
10
12
>>D=A(4,:)
D =
13
14
15
>>E=A([2 4],end)
E =
8
16
% A 13 24 C
% A 4
16
% 14 end
2)
2.17
>>clear
>>A=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16]
A =
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
>>B=A([4:6;3 5 7;12:14])
B =
13
2
6
9
2
10
15
4
8
A 46 1 357 3
2 1214 3 33 B
B=A([4:6;[3 5 7];12:14])
35
MATLAB
36
MATLAB
C=A([4:5,7,10:13]) C=[13 2 10 7 11 15
4]
3.
2.18
>> A=[1 2 3;4 5 6;7 8 9],B=[9 8;7 6;5 4],C=[4 5 6;7 8 9]
A =
1
2
3
4
5
6
7
8
9
B =
9
8
7
6
5
4
C =
4
5
6
7
8
9
>> E=[A B;B A]
%
E =
1
2
3
9
8
4
5
6
7
6
7
8
9
5
4
9
8
1
2
3
7
6
4
5
6
5
4
7
8
9
>> F=[A;C]
%AC
1
2
3
4
5
6
7
8
9
4
5
6
7
8
9
4.
MATLAB
2-8
2-9
2-8
zeros(m,n)
mn 0
ones(m,n)
mn 1
36
MATLAB
37
rand(m,n)
01
randn(m,n)
eye(m,n)
mn
2-9
compan
Companion
magic
gallery
Higham
pascal
hadamard
Hadamard
rosser
hankel
Hankel
toeplitz
Toeplitz
hilb
Hilbert
vander
invhilb
Hilbert
wilkinson
Wilkinson's
2-8 eye
(2.4.2 ) eye(m,n)
2.19
>>A=ones(3,4),B=eye(3,4),C=magic(3)
A =
1
1
1
1
1
1
1
1
1
1
1
1
B =
1
0
0
0
0
1
0
0
0
0
1
0
C =
8
1
6
3
5
7
4
9
2
>> format rat;D=hilb(3),E=pascal(4)
D =
1
1/2
1/3
1/2
1/3
1/4
1/3
1/4
1/5
E =
1
1
1
1
1
2
3
4
1
3
6
10
1
4
10
20
%rat
n (n3+n)/2
3 15
37
MATLAB
38
5.
cat repmat
38
1
9
4
1
9
4
2
8
5
2
8
5
3
7
6
3
7
6
2
>> repmat(A1,2,1)
ans =
1
2
3
9
8
7
4
5
6
1
2
3
9
8
7
4
5
6
>> repmat(A1,1,3)
ans =
1
2
3
9
8
7
4
5
6
1
9
4
2
8
5
MATLAB
3
7
6
1
9
4
2
8
5
39
3
7
6
reshape
reshape reshape(A,m,n)m
n
2.22
>>A=linspace(2,18,9)
A =
2
4
6
8
10
12
14
16
18
>>B=reshape(A,3,3)
%
B =
2
8
14
4
10
16
6
12
18
>>a=20:2:24;b=a.';
% 3 b B 34 C
>>C=[B b],D=reshape(C,4,3) % 34 C 43 D
C =
2
8
14
20
4
10
16
22
6
12
18
24
D =
2
10
18
4
12
20
6
14
22
8
16
24
6.
.mat MATLAB
.mat
MATLAB .mat
()
MATLAB
39
MATLAB
40
% sl2_19.mat
>> who
% 1.8 1.8
Your variables are:
A
>> A
A =
%
4
1
9
5
2
8
6
3
7
7
4
6
7. M
M
(.mat) M .m
M M
2.3.4
MATLAB
MATLAB
1.
det(A)
2.24
>> A=[3 2 4;1
A =
3
2
1
-1
2
-1
D1 =
24
40
-1 5;2 13],D1=det(A)
4
5
3
MATLAB
41
>> B=ones(3),D2=det(B),C=pascal(4),D3=det(C)
B =
1
1
1
1
1
1
1
1
1
D2 =
0
C =
1
1
1
1
1
2
3
4
1
3
6
10
1
4
10
20
D3 =
1
2.
2-2
2.25
1 3
2 1
A=
3 0
1 2
B=
A+B2A2A-3BAB
>>A=[1 3;2 1];B=[3 0;1 2];
>>A+B
ans =
4
3
3
1
>> 2*A
ans =
2
6
4
-2
>> 2*A-3*B
ans =
-7
6
1
-8
>> A*B
ans =
6
6
5
-2
3.
MATLAB n
inv(A)
2.26 A
41
MATLAB
42
4.
MATLAB
2.27
x1 + 4 x2 7 x3 + 6 x4 = 0
2 x2 + x3 + x4 = 8
x2 + x3 + 3x4 = 2
x1 +
x3 x4 = 1
MATLAB
43
-1.0000
1.0000
A\B inv(A)*B
X=[x1 x2 x3 x4] A=[1 0 0 1;4 2 1 0;-7 1 1 1;6 1 3 -1] B=[0
8 2 1] XA=B
>>A=[1 0 0 1;4 2 1 0;-7 1 1 1;6 1 3 -1],B=[0 -8 -2 1],x=B/A
A =
1
0
0
1
4
2
1
0
-7
1
1
1
6
1
3
-1
B =
0
-8
-2
1
x =
3.0000
-4.0000
-1.0000
1.0000
>> B*inv(A)
ans =
3.0000
-4.0000
-1.0000
1.0000
A/B B*inv(A)
MATLAB
5.
MATLAB
rank(A)
2.28
>> B=[1 3 -9 3;0 1 -3 4;-2 -3 9 6],rb=rank(B)
B =
1
3
-9
3
0
1
-3
4
-2
-3
9
6
rb =
2
6.
MATLAB A
[X,]=eig(A)[X,]=eigs(A) 6
2.29 A
>> A=[1 -3 3;3 -5 3;6 -6 4], [X,Lamda]=eig(A)
43
MATLAB
44
A =
1
3
6
-3
-5
-6
3
3
4
X =
0.4082
0.4082
0.8165
0.4082
-0.4082
-0.8165
-0.1203
-0.7595
-0.6393
Lamda =
4.0000
0
0
0
-2.0000
0
0
0
-2.0000
Lamda A 1 =4 2 = 3 =-2
X X 1 1 2 2
A
7.
MATLAB
A A3 3A
MATLAB
()
MATLAB
sqrtm(A)
2.30
>> A=[1
>> A^3
ans =
28
36
72
-36
-44
-72
36
36
64
>> A^1.2
ans =
1.7097 - 0.6752i
3.5683 + 0.6752i
7.1367 + 1.3504i
>> 3^A
ans =
40.5556
40.4444
80.8889
44
-40.4444
-40.3333
-80.8889
-3.5683 - 0.6752i
-5.4270 - 2.0256i
-7.1367 - 1.3504i
40.4444
40.4444
81.0000
3.5683 + 0.6752i
3.5683 + 0.6752i
5.2780 - 0.0000i
MATLAB
>> A1=sqrtm(A)
A1 =
1.0000 + 0.7071i
1.0000 - 0.7071i
2.0000 - 1.4142i
-1.0000 + 0.7071i
-1.0000 + 2.1213i
-2.0000 + 1.4142i
1.0000 - 0.7071i
1.0000 - 0.7071i
2.0000 - 0.0000i
>> A1^2
ans =
1.0000 - 0.0000i
3.0000 - 0.0000i
6.0000 - 0.0000i
-3.0000 + 0.0000i
-5.0000 + 0.0000i
-6.0000 + 0.0000i
3.0000
3.0000 - 0.0000i
4.0000 - 0.0000i
45
A X
Lamda A^p=Lamda*X.^p/Lamda
2.4.3
8.
-1 1;2 -4 1;1 -5 3]
-1
-4
-5
1
1
3
>> Ae=expm(A)
Ae =
1.3719
-3.7025
0.3987
-2.3495
-2.5254
-7.6138
4.4810
2.9241
9.5555
>> Ael=logm(Ae)
Ael =
1.0000
-1.0000
2.0000
-4.0000
1.0000
-5.0000
1.0000
1.0000
3.0000
9.
MATLAB
2-2
45
MATLAB
46
transpose(Z)
2.32
>> a=1:9
a =
1
2
3
4
5
6
7
>> A=reshape(a,3,3)
A =
1
4
7
2
5
8
3
6
9
>> B=A'
B =
1
2
3
4
5
6
7
8
9
>> Z=A+i*B
Z =
1.0000 + 1.0000i 4.0000 + 2.0000i
2.0000 + 4.0000i 5.0000 + 5.0000i
3.0000 + 7.0000i 6.0000 + 8.0000i
>> Z'
ans =
1.0000 - 1.0000i 2.0000 - 4.0000i
4.0000 - 2.0000i 5.0000 - 5.0000i
7.0000 - 3.0000i 8.0000 - 6.0000i
>> transpose(A)
ans =
1
2
3
4
5
6
7
8
9
>> transpose(Z)
ans =
1.0000 + 1.0000i 2.0000 + 4.0000i
4.0000 + 2.0000i 5.0000 + 5.0000i
7.0000 + 3.0000i 8.0000 + 6.0000i
7.0000 + 3.0000i
8.0000 + 6.0000i
9.0000 + 9.0000i
3.0000 - 7.0000i
6.0000 - 8.0000i
9.0000 - 9.0000i
3.0000 + 7.0000i
6.0000 + 8.0000i
9.0000 + 9.0000i
10.
MATLAB
2-10
2-10
triu(A)
A 0
tril(A)
A 0
diag(A)
46
MATLAB
47
flipud(A)
fliplr(A)
flipdim(A,dim)
A dim=1dim=2
rot90(A)
A 90
2.33
>> a=linspace(1,23,12)
a =
1
3
5
7
>> A=reshape(a,4,3)'
A =
1
3
5
7
9
11
13
15
17
19
21
23
>> fliplr(A)
ans =
7
5
3
1
15
13
11
9
23
21
19
17
>> flipdim(A,2)
ans =
7
5
3
1
15
13
11
9
23
21
19
17
>> flipdim(A,1)
ans =
17
19
21
23
9
11
13
15
1
3
5
7
>> triu(A)
ans =
1
3
5
7
0
11
13
15
0
0
21
23
>> tril(A)
ans =
1
0
0
0
9
11
0
0
17
19
21
0
>> diag(A)
ans =1
11
21
11
13
15
17
19
21
23
47
MATLAB
48
2.4
MATLAB MATLAB
2.4.1
2.3.1
()
mnl A mn ()
i () j ()
k A () A i j k
A(i,j,k )
322 B 2-11
2-11 B
B(1,1,1)
B(1,2,1)
B(1,1,2)
10
B(1,2,2)
B(2,1,1)
B(2,2,1)
B(2,1,2)
11
B(2,2,2)
B(3,1,1)
B(3,2,1)
B(3,1,2)
12
B(3,2,2)
2-11 6 1 712 2
1 2
2.4.2
1.
48
MATLAB
49
2.34
>> A=[1,2,3;4 5 6;7,8,9];B=reshape([10:18],3,3).';
%
>> C(:,:,1)=A; C(:,:,2)=B;
% AB 12
>> C
C(:,:,1)
1
4
7
C(:,:,2)
10
13
16
% C
=
2
5
8
=
11
14
17
3
6
9
12
15
18
2.
2.3.3 2-8 eye( )
0.8913
49
MATLAB
50
0.2311
0.4860
ans(:,:,2) =
0.4565
0.8214
0.0185
0.4447
0.7621
0.6154
0.7919
>> randn(2,2,2)
ans(:,:,1) =
-0.4326
0.1253
-1.6656
0.2877
ans(:,:,2) =
-1.1465
1.1892
1.1909
-0.0376
3.
2.3.3
2.37 reshape
>> A=1:18
>> reshape(A,3,3,2)
ans(:,:,1) =
50
2
1
4
2
5
3
6
ans(:,:,2) =
10
13
11
14
12
15
MATLAB
51
7
8
9
16
17
18
2.4.3
1.
2-2 2-3
2.38
>> A1=[6 5 4 3 2 1];B1=[1 2 3 4 5 6];
>> C1=A1+B1,C2=C1-B1,C3=A1.*B1
C1 =
7
7
7
7
7
7
C2 =
6
5
4
3
2
1
C3 =
6
10
12
12
10
6
>> A2=reshape(A1,2,3),B2=reshape(B1,2,3)
A2 =
6
4
2
5
3
1
B2 =
1
3
5
2
4
6
>> D1=A2+B2,D2=3.*A2,D3=A2.*B2
D1 =
7
7
7
7
7
7
D2 =
18
12
6
15
9
3
D3 =
6
12
10
10
12
6
2.39 ( 2.38)
>> A3=cat(3,D2,D3),B3=repmat(D1,[1,1,2])
A3(:,:,1) =
51
MATLAB
52
18
12
15
9
A3(:,:,2) =
6
12
10
12
B3(:,:,1) =
7
7
7
7
B3(:,:,2) =
7
7
7
7
>> A3.*B3
ans(:,:,1) =
126
84
105
63
ans(:,:,2) =
42
84
70
84
6
3
10
6
7
7
7
7
%
42
21
70
42
2.
2-3
2.40 2.38
>> D1./4
ans =
1.7500
1.7500
>> 4./D1
ans =
0.5714
0.5714
>> A3./B3
ans(:,:,1)
2.5714
2.1429
ans(:,:,2)
0.8571
1.4286
>> B3.\A3
ans(:,:,1)
2.5714
2.1429
ans(:,:,2)
0.8571
1.4286
1.7500
1.7500
1.7500
1.7500
0.5714
0.5714
0.5714
0.5714
% B3.\A3
1.7143
1.2857
0.8571
0.4286
1.7143
1.7143
1.4286
0.8571
1.7143
1.2857
0.8571
0.4286
1.7143
1.7143
1.4286
0.8571
3.
2-3 .^ sqrt
52
MATLAB
53
2.41 23 A
>> A=[1 2 3;4 5 6]; A2p=A.^2, App=A.^1.5
A2p =
1
4
9
16
25
36
App =
1.0000
2.8284
5.1962
8.0000 11.1803 14.6969
>> As=sqrt(A)
As =
1.0000
1.4142
1.7321
2.0000
2.2361
2.4495
>> App1=sqrt(A.^3)
App1 =
1.0000
2.8284
5.1962
8.0000 11.1803 14.6969
% A.^1.5
4.
exp( ) log( )
2.42 A
>> A=[1 2 3;4 5 6]
A =
1
2
3
4
5
6
>> Ae=exp(A),Al=log(A)
Ae =
2.7183
7.3891 20.0855
54.5982 148.4132 403.4288
Al =
0
0.6931
1.0986
1.3863
1.6094
1.7918
5.
2-3 .' '
2.43 A
>> a=[1 2 3;4 5 6];b=[2 3 4;5
>> A=a+i*b
A =
1.0000 + 2.0000i 2.0000 +
4.0000 + 5.0000i 5.0000 +
>> B=A.'
B =
1.0000 + 2.0000i 4.0000 +
6 7];
3.0000i
6.0000i
3.0000 + 4.0000i
6.0000 + 7.0000i
5.0000i
53
MATLAB
54
2.0000 + 3.0000i
3.0000 + 4.0000i
5.0000 + 6.0000i
6.0000 + 7.0000i
()()
MATLAB
2.4.4
2.1.5 MATLAB
(
)
1.
2-4 6
2.44 6 3 1
>> A=magic(6)
A =
35
1
6
3
32
7
31
9
2
8
28
33
30
5
34
4
36
29
>> P=mod(A,3)==0
P =
0
0
1
1
0
0
0
1
0
0
0
1
1
0
0
0
1
0
54
26
21
22
17
12
13
19
23
27
10
14
18
24
25
20
15
16
11
0
1
0
0
1
0
0
0
1
0
0
1
1
0
0
1
0
0
MATLAB
55
mod(A,B) A B 0
mod(A,B)==0 1 0 P
2.
6 2-5 5
xor( ) 3 and( )
or( )not( )
2.45
>> A=pascal(3),B=eye(3)
A =
1
1
1
1
2
3
1
3
6
B =
1
0
0
0
1
0
0
0
1
>> A&B
ans =
1
0
0
0
1
0
0
0
1
>> A|B
ans =
1
1
1
1
1
1
1
1
1
>> ~B
ans =
0
1
1
1
0
1
1
1
0
>> xor(A,B)
ans =
0
1
1
1
0
1
1
1
0
>> a=0;b=1;
>> a&&b
ans =
0
>> a=1;b=0;
>> a||b
ans =
1
MATLAB
55
MATLAB
56
3.
MATLAB
2-12
2-12
A 0 1 0
all(A,n)
n=1n=2
A 0 1 0
any(A,n)
n=1n=2
isnan(A)
A (NaN) 1 0
isinf(A)
A 1 0
isnumeric(A)
A 1 0
isreal(A)
A 1 0
isempty(A)
A 1 0
find(A)
A 0
2.46
>> A=[1 2 3;0
A =
1
2
0
4
8
9
>> all(A,1)
ans =
0
1
>> all(A,2)
ans =
1
0
0
>> B=1:4
B =
1
2
>> any(B)
ans =
1
>> any(B,1)
ans =
1
1
>> any(B,2)
ans =
1
>> isnan(A)
56
4 5;8 9 0]
3
5
0
MATLAB
57
ans =
0
0
0
0
0
0
0
0
0
>> isnumeric(A)
ans =
1
2.4.5
MATLAB
MATLAB
MATLAB
A funm(A,sqrt) sqrt
sqrtm(A) A funm(A,log)
2-13
abs
log10
10
sqrt
round
angle
fix
real
floor
imag
ceil
conj
rem(a,b)
a/b
exp
mod(c,m)
c/m
ln
sign
log2
2
57
MATLAB
58
2-14
sin
sinh
cos
cosh
tan
tanh
asin
asinh
acos
acosh
atan
atanh
2-15
besselj
gamma
bessely
gammainc
besselh
ellipj
Jacobi
legendre
ellipke
beta
erf
betainc
rat
2.47 2 sin( )
>> ang=0:2:90;angle1=ang.*pi/180;
>> sin(angle1)
ans =
Columns 1 through 7
0
0.0349
0.0698
0.1045
Columns 8 through 14
0.2419
0.2756
0.3090
0.3420
Columns 15 through 21
0.4695
0.5000
0.5299
0.5592
Columns 22 through 28
0.6691
0.6947
0.7193
0.7431
Columns 29 through 35
0.8290
0.8480
0.8660
0.8829
Columns 36 through 42
0.9397
0.9511
0.9613
0.9703
Columns 43 through 46
0.9945
0.9976
0.9994
1.0000
0.1392
0.1736
0.2079
0.3746
0.4067
0.4384
0.5878
0.6157
0.6428
0.7660
0.7880
0.8090
0.8988
0.9135
0.9272
0.9781
0.9848
0.9903
2
090 46 MATLAB
2
58
MATLAB
59
2.48
>> A=[2 3 4;5 6 7;8 9 10]
A =
2
3
4
5
6
7
8
9
10
>> B=funm(A,'log10')
B =
-2.4810 + 0.9456i
5.4987 + 0.2274i
5.6293 + 0.0903i
-10.1738 + 0.0217i
-2.1288 - 0.7650i
5.8906 - 0.1840i
-2.3901 - 0.4908i
5.7600 - 0.0469i
-1.9584 + 0.3971i
A 2.45
>> Ba=log10(A)
Ba =
0.3010
0.4771
0.6990
0.7782
0.9031
0.9542
0.6021
0.8451
1.0000
2.5
MATLAB C
2.5.1
MATLAB
MATLAB
( char array)
1.
2.49 3 S1S2S3 3
>> S1='go home',S2='',S3='go home. '
S1 =
go home
S2 =
S3 =
go home.
59
MATLAB
60
2.
2.50 3
>>Sa=['I love my teacher, ' 'I' ' love truths ' 'more profoundly.']
Sa =
I love my teacher, I love truths more profoundly.
>>Sb=char('a':2:'r')
%
Sb =
acegikmoq
>>Sc=char(linspace('e','t',10))
%
Sc =
efhjkmoprt
char( )2.5.2
Sa size 4
Sa
2.5.2
2
ans =
3
MATLAB
61
2.
ASCII
A 65
32
110
111
98
111
100
121
32
110
111
98
111
100
121
3.
strcmp(S1,S2) MATLAB S1 S2 1
0
2.53 strcmp( )
>> S1='I am nobody';
>> S2='I am nobody.';
>> strcmp(S1,S2)
ans =
0
>> strcmp(S1,S1)
ans =
1
4.
findstr(S,s) S s
2.54 findstr( )
>> S='I believe that love is the greatest thing in the world.';
>> findstr(S,'love')
ans =
16
5.
disp( )
61
MATLAB
62
2.55 disp( )
>> disp(''),Result=strcmp(S1,S1),disp(' 1
0 ')
num2str
int2str
Result =
1
1 0
MATLAB
2.5.3
2.56 S1S2S3S4 4
>> S1='';
>> S2='';
>> S3='';
>> S4=' !';
>> S=[S1;S2,' ';S3;S4,' ']
%
S =
!
>> S=[S1;S2,' ';S3;S4]
%
??? Error using ==> vertcat
All rows in the bracketed expression must have the same
number of columns.
2.54 char( )
strvcat( ) str2mat( ) 3
2.57 char( )strvcat( ) str2mat( )
>> S1a='I''m nobody,'; S1b=' who are you?';
>> S2='Are you nobody too?';
>> S3='Then there''s a pair of us.';
>> SS1=char([S1a,S1b],S2,S3)
SS1 =
I'm nobody, who are you?
Are you nobody too?
Then there's a pair of us.
>> SS2=strvcat(strcat(S1a,S1b),S2,S3)
SS2 =
I'm nobody, who are you?
Are you nobody too?
62
%
%
MATLAB
63
2.6
MATLAB
MATLAB
MATLAB
MATLAB C
C
2.7
1.
(1) (
)
A.
B.
C.
D.
(2) ones(n,m)(
)
A.
B.
C. 1
D.
(3) MATLAB log(x) x (
)
A. 2
B 10
C. x
(4) a=-3.2-4(
A. fix
B. round
C. ceil
3
2
(5) ax +by MATLAB (
D. e
)
D. floor
)
A. ax3+by2
B. a*x3+b*y2
C. ax3+by2
D. a*x^3+b*y^2
(6) a=0:1:4b=5:-1:1(
A. a+b
B. a*b
C. a'*b
D. a./b
(7) a=[1 2 3;4 5 6;7 8 9] b=[3 6 9;2 5 8;1 4 7](
)
A. b=a'
B. b=flipud(a)
C. b=mfliplr(a)
D. b=rot90(a)
63
64
MATLAB
2.
(1) zeros(5) 5
(2) MATLAB
(3) A.*B A B
(4) AB A>B 0 1
(5) strcat( ) strvcat( )
(6) abs( )
(7) length( )
(8) funm(A,log) logm(A)
3.
(1) A=[1 2 3 45 6 7 89 10 11 1213 14 15 16] x=[2,4]
C=A(x,:)=_____________________________________
(2) x 0 4 0.1 ____________________________________
_____________________________________
(3) x=logspace(0,2,3) x ____________________________________
(4) A=[4 2 3 416 6 7 89 10 11 121 14 15 5] B=A(:,[1,3])
B=_____________________________________
(5) A=linspace(2,18,9);B=reshape(A,3,3)
B=_____________________________________
64
MATLAB
-
3 -
MATLAB
3.1
f ( x) = a0 x n + a1 x n 1 + a2 x n 2 + + an 1 x + an x
,
3.1.1
MATLAB
MATLAB
s 4 + 3s 3 15s 2 2 s + 9
MATLAB
x = [1 3 -15 -2 9]
MATLAB n+1 n
s4 + 1
MATLAB
y = [1 0 0 0 1]
3.1.2
MATLAB
66
a( x) = x3 + 2 x 2 + 3 x + 4 b( x) = x3 + 4 x 2 + 9 x + 16
(1) c( x) = a ( x) + b( x)
c( x) = 2 x 3 + 6 x 2 + 12 x + 20
(2) d ( x) = a( x) b( x)
d ( x) = 2 x 2 6 x 12
(3) e( x) = a( x)b( x)
e( x) = x 6 + 6 x5 + 20 x 4 + 50 x3 + 75 x 2 + 84 x + 64
e( x )
(4) f ( x) =
= a ( x)
b( x )
f ( x) = x3 + 2 x 2 + 3x + 4
polyadd
Justin Shriver ( 6.1 )
function[poly]=polyadd(poly1,poly2)
%polyadd(poly1,poly2) adds two polynominals possibly of uneven length
if length(poly1)<length(poly2)
short=poly1;
long=poly2;
else
short=poly2;
long=poly1;
end
mz=length(long)-length(short);
if mz>0
poly=[zeros(1,mz),short]+long;
else
poly=long+short;
end
polyadd.m MATLAB
polyadd MATLAB
3.1 polyadd a( x) = x3 + 2 x 2 + 3 x + 4
b( x) = x3 + 4 x 2 + 9 x + 16
>> a=[1 2 3 4];
>> b=[1 4 9 16];
>> c=polyadd(a,b)
c=
2
6
12
20
3.2 polyadd m( x) = x + 2 n( x) = x 2 + 4 x + 7
66
MATLAB
67
3.3 a( x) = x3 + 2 x 2 + 3 x + 4 b( x) = x3 + 4 x 2 + 9 x + 16
50
75
84
64
3.5 m( x) = x + 2 n( x) = x 2 + 4 x + 7
>> m=[1 2];
>> n=[1 4 7];
>> p = conv(m,n)
p =
1
6
15
14
deconv
deconv [q,r]=deconv(a,b) qr
e( x )
a ( x)
3.6 3.4 f ( x) =
b( x )
>> [f, r] = deconv(e,b)
f =
1
2
3
4
r =
0
0
0
0
f 3.4 a ( x) e b r
3.7
( s 2 + 1)( s + 2)( s + 1)
s3 + s + 1
67
MATLAB
68
>> p1=conv([1,0,1],conv([1,2],[1,1]));
>> p2=[1 0 1 1];
>> [q,r]=deconv(p1,p2)
q =
1
3
r =
0
0
2
-1
-1
%
%
s+3 2s2-s-1
3.1.3
1.
MATLAB polyval polyval
y = polyval(p,x)
p x x y=polyvalm(p,x)
v
3.10 P (X) = X^3 2X I X X=[1 2 1; -1
0 2; 4 1 2]
>> X = [1 2 1; -1 0 2; 4 1 2];
>>P=[1 -2 -1];
>>Y = polyvalm(P,X)
Y =
0
-1
5
9
-1
-1
3
8
5
2.
x x
68
MATLAB
69
f(x)=0
3.11 s 4 + 3s 3 12 s 2 2s + 8
>>roots([1 3 -12 -2 8])
ans =
-5.18325528043789
2.17062070347062
-0.83694739215044
0.84958196911772
(long)(short)
MATLAB Fle | Preferences
3.12 8
x8 36 x 7 + 546 x6 4536 x5 + 22449 x 4 67284 x3 + 118124 x 2 109584 x + 40320 = 0
>> p=[1 -36 546 -4536 22449
>> roots(p)
ans =
8.00000000000060
7.00000000000532
5.99999999997983
5.00000000002646
3.99999999998295
3.00000000000559
1.99999999999921
1.00000000000003
-67284 118124
-109584 40320]
7 -36 -37 8
>> p(2)=-37;
>> roots(p)
ans =
16.11915507295279
5.03509581022879 +
5.03509581022879 2.82103813323916 +
2.82103813323916 2.08438753810761 +
2.08438753810761 0.99980196389608
5.14974937822547i
5.14974937822547i
1.72812158500609i
1.72812158500609i
0.24935240473904i
0.24935240473904i
roots
polyval
69
MATLAB
70
3.13 x 2 3 x + 2
>> p=[1 -3 2];
>> roots(p)
ans =
2
1
>> polyval(p,2), polyval(p,1)
ans =
0
ans =
0
polyval
3.1.4
3.15 s 4 + 3s 3 15s 2 2s + 9
>> T=[1 3 -15 -2 9];
%
>> r=roots(T);
%
>> poly(r)
%
ans =
1.0000
3.0000 -15.0000
-2.0000
9.0000
3-1
3-1
conv(a,b)
[q,r]=deconv(a,b)
poly(r)
polyadd(x,y)
polyval(p, x)
poly2sym(p)
roots(a)
70
MATLAB
71
3.2
y = f ( x) [a, b]
[a, b] 3-2
x y = g ( x) y = f ( x)
y = g ( x)
MATLAB
MATLAB
3-2
3.2.1
y = f ( x )
x1
x2
xn
f ( x)
f ( x1 )
f ( x2 )
f ( xn )
a = x0 < x1 <
< xn = b n + 1 ( xi , yi ) i = 0,1,
( xi , yi ) i = 0,1,
, n xi
, n y = f ( x) f
x xi
yi
y = g ( x) g
yi = g ( xi ) i = 0,1,
, n g ( x) f ( x)
()
( x1 , y1 ),( x2 , y2 ), ,( xn , yn )
g ( x) = a3 x3 + a2 x 2 + a1 x + a0 () g ( x)
min
a0 , a1 , a2 , a3
(a x
i =1
3
3 i
+ a2 xi2 + a1 xi + a0 yi )2
71
MATLAB
72
MATLAB interp1(
)interp1q()interpft( FFT )spline(
)interp2()interp3()interpn(n )
MATLAB interp1
polyfit
1(interp1)
yi = interp1(x,y,xi,method) yi = g ( xi ) x y x
y xi method nearest
linearspline
cubicnearest
linearspline
cubicsplinespline
f
n
()
S ( x)(a x b) 3
(1) [ xi 1 , xi ](i = 1,
, n)
(2) a x b
(3) S ( xi ) = yi , (i = 0,1, , n) S ( x)
x=0:10; y=cos(x);
xi=0:.25:10;
y0=cos(xi);
%
y1=interp1(x,y,xi);
%
y2=interp1(x,y,xi,'cubic');
%
y3=interp1(x,y,xi,'spline');
%
plot(xi,y0,'o',xi,y1,xi,y2,'-.',xi,y3)
3 3.1 3
72
MATLAB
73
3.2 3.2
3.1 3
3.2 3
20
60
68
77
110
>>
>>
>>
y1
64
>>
2.5
2.5 64
t=[0 1 2 3 4 5]';
y=[0 20 60 68 77 110]';
y1=interp1(t,y,2.5)
=
%
%
%
%
y1=interp1(x,y,[2.5 4.3])
% 2.5,4.3[ ]
73
MATLAB
74
y1 =
64
86.9
>> y1=interp1(x,y,2.5,'cubic')
y1 =
64.6078
>> y1=interp1(x,y,2.5,'spline')
y1 =
66.8750
% 2.5
% 2.5 64.6078
% spline 2.5
% 2.5 66.8750
2 polyfit
MATLAB polyfit
p=polyfit(x,y,n)
[p,s]=polyfit(x,y,n)
x,y n p
s polyval
n
polyfit f ( x) = an x n + an 1 x n 1 +
a n , a n 1 ,
+ a1 x + a0 polyfit
, a1 , a 0 pn
n+1
polyfit polyval( 3.1.3 ) polyfit
a n , a n 1 ,
, a1 , a 0 polyval
3.18 X=[-2.8 -1 0.2 2.1 5.2 6.8] Y=[3.1 4.6 2.3 1.2 2.3 -1.1]
345
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
3.3
5 7 3.4
(7 ) 5
74
MATLAB
75
3.3 35
3.4 57
3.19
3-4
3-4
x
106.42
11
110.59
108.26
14
110.60
109.58
15
110.90
109.50
16
110.76
110.00
18
111.00
109.93
19
111.20
10
110.49
75
MATLAB
76
3
>> x=[2 3 4 5 7 8 10 11 14 15 16 18 19];
>> y=[106.42 108.26 109.58 109.5 110 109.93 110.49 110.59 110.6 110.9 110.76
111 111.2];
>> v=polyfit(x,y,3)
% 3
v=
0.0033
-0.1224
1.5113 104.4824
>> t=1:0.5:19;
>> u=polyval(v,t);
% t
>> plot(t,u,x,y,'*')
%
3.5
3.5 3
5
>> v=polyfit(x,y,5)
% 5
v=
0.0001
-0.0055
0.1176
-1.2012
5.9223
3.6
3.6 5
76
98.5719
MATLAB
77
table 3-5
3-5
y1(3 )
y2(5 )
y-y1
y-y2
106.42
107.0413
106.4682
-0.62132
-0.04819
108.26
108.0024
108.2841
-0.25762
-0.02412
109.58
108.7772
109.2686
-0.802772
0.31139
109.5
109.3854
109.7409
-0.1146
-0.24089
110
110.1799
110.0137
-0.17987
-0.01374
109.93
110.4053
110.0754
-0.47526
-0.14542
10
110.49
110.61
110.3096
-0.12001
-0.180446
11
110.59
110.6285
110.4777
-0.03846
-0.112291
14
110.6
110.5826
110.8392
-0.017417
-0.23916
15
110.9
110.5987
110.8342
-0.30132
-0.065845
16
110.76
110.663
110.7818
-0.096962
-0.02177
18
111
111.0147
110.8533
-0.0147
-0.146667
19
111.2
111.3411
111.2833
-0.14108
-0.08335
-1.50470114
-0.314867848
5 3
3.2.2
3.2.1
y = 0 + 1r1 ( x) + + m rm ( x)
r1 ( x), r2 ( x),
( xi , yi ) i = 1, 2,
, rm ( x) m () n
n, n > m
yi 0 + 1r1 ( xi ) + + m rm ( xi ) i = 1, 2, , n
0 ,1 , , m
0 ,1 , , m yi = 0 + 1r1 ( xi ) + + m rm ( xi ) i = 1, 2, , n
77
MATLAB
78
y i 0 ,1 ,
n
min
0 ,1 , , m
(y
i =1
yi 0 + 1r1 ( xi ) +
, m
( 0 + 1r1 ( xi ) +
+ m rm ( xi ))) 2
+ m rm ( xi )
y
k
k
y = aebx a, b
( ) ln y = ln a + bx
Y = ln y, A = ln a Y = A + bx
MATLAB polyfit
3.20 3-6
3-6
I
xi
12
15
18
21
24
yi
57.6
41.9
31.0
22.7
16.6
12.2
8.9
6.5
xi yi
y = aebx a, b
y = aebx lg y = (b lg e) x + lg a Y = lg y
B = 0.4343b lg a = m Y = Bx + m ( xi , Yi )
3-7
( xi , Yi ) B = 0.4343b lg a = m
a, b
x=[3 6 9 12 15 18 21 24];
y=[1.7604 1.6222 1.4914 1.3560 1.2201 1.0864 0.9494 0.8129];
% y y Y
p1=polyfit(x,y,1)
b=p1(1)/0.4343
a=10.^p1(2);
y1=polyval(p1,x);
%
p1= -0.04502936507937
b =
-0.10368262739895
a =
78.56813216117476
1.89524642857143
4 y = 78.59e 0.1037 x xi
78
MATLAB
79
3-7
3-7
I
xi
12
15
18
21
24
yi
57.6
41.9
31.0
22.7
16.6
12.2
8.9
6.5
Yi
1.7604
1.6222
1.4914
1.3560
1.2201
1.0864
0.9494
0.8129
1.7601
1.6250
1.4900
1.3550
1.2198
1.0847
0.9496
0.8145
3.3
-
()
-
3.3.1
y n = f (n) (n = 1,2, )
n = 1
y n =
y n
= f (n + 1) f (n)
n
( n )
y = f (x) x0 h
x n = x0 + nh(n = 0,1,2, )
y n = f ( x0 + nh) = f (n)
y = f (1) f (0) = f ( x0 + h) f ( x0 ) y = f ( x) x0( n=0)
MATLAB diff 4
diff(x) x
diff(x,'t') x t
diff(x,n) x n
diff(x,'t',n) x t n
x x k , k = 1, , n
dy ( x) / dx dy=diff(y)./diff(x)
3.21 x=[1 3 5 7 9]y=[1 4 9 16 25] diff
79
MATLAB
80
>> x=[1 3 5 7 9];
>> y=[1 4 9 16 25];
>> diff(x)
ans =
2
2
2
2
>> diff(y)
ans =
3
5
7
9
3.22 y = x 3 x 8 x + 7 x + 3 x 5 [-4, 5]
5
>>
>>
>>
>>
>>
>>
>>
x=linspace(-4,5);
p= [1 -3 -8 7 3 -5];
f=polyval(p,x);
subplot(2,1,1);plot(x,f)
title('') ;
dfb=diff(f)./diff(x);
xd=x(2:length(x));
>> subplot(2,1,2);plot(xd,dfb );
>> title('') ;
% 100 x
% 100 x
%
% diff(f) diff(x)
% 99 df x2,x3, ,
%x100
%
3.7
3.7
diff 4
>> S1 = '6*x^3-4*x^2+b*x-5'; %( 5 )
>> S2 = 'sin(a)';
>> S3 = '(1 - t^3)/(1 + t^4)';
>> diff(S1)
% x
ans=
18*x^2-8*x+b
>> diff(S1,2)
% x
ans=
36*x-8
>> diff(S1,'b')
% b
80
MATLAB
81
ans=
x
>> diff(S2)
% a
ans=
cos(a)
>> diff(S3)
% t
ans=
-3*t^2/(1+t^4)-4*(1-t^3)/(1+t^4)^2*t^3
3.3.2
-
b
f ( x)dx( f ( x) 0) ab
a
f(x)
y=f(x) x=ax=b x f(x)
1.
cumsum xcumsum(x)
i x i x
n 1
x I = h f ( xi ) MATLAB
i =0
n 1
cumsum(x)*h h cumsum(x)
f (x )
i =0
MATLAB
82
1
5
12
2
7
15
3
9
18
>>
>>
>>
>>
%[0] 100
x=linspace(0,pi,100);
y=sin(x);
T=cumsum(y)*pi/(100-1);
I=T(100)
I=
1.9998
% pi/(100-1)
%[0]
2.
trapz trapz
(1) z=trapz(y) y trapz(y) y
trapz(y) y
n trapz(y)
(2) z=trapz(x,y) y x x y
x y x
(3) z=trapz(x,y,dim) trapz(y,dim) y dim
x size(y,dim)
>> x=linspace(0,pi,100);
>> y=sin(x);
>> t=trapz(x,y)
t =
1.9998
x=linspace(0,pi,150)
t= 1.9999
3.
quad quad
(1) q=quad('f',a,b) a b f(x)
1e-3 'f'
MATLAB
83
>>q=quad('sin',0,pi)
q= 2.0000
3.28 f ( x) =
1
dx
x3 2 x 5
1
>> quad('1./(x.^3-2*x-5)',0,2)
ans =
-0.4605
2
>> F='1./(x.^3-2*x-5)';
>> quad(F,0,2)
ans=
-0.4605
4.
quadl( l L )quadl
(1)
(2)
(3)
(4)
q = quadl(fun,a,b)
q = quadl(fun,a,b,tol)
q = quadl(fun,a,b,tol,trace)
[q,fcnt] = quadl(fun,a,b,...)
1
3.29 e x dx
2
>> z=quadl('exp(-x.^2)',-1,1)
z =
1.4936
3.30 f ( x) =
1
dx
x3 2 x 5
>> quadl('1./(x.^3-2*x-5)',0,2)
ans =
-0.4605
4 trapz quadquadl
trapz
3.4
83
MATLAB
84
3.4.1
1.
x1 + x2 + 3x3 = 3
3
2 x1 + 3 x2 + 5 x3 = 2
%A 33 n=m
% n=m A
x1 = 4, x2 = 3, x3 = 2 B
x1 x2 + x3 x4 = 1
3.32 x1 x2 x3 + x4 = 0
x x 2 x + 2 x = 0.5
3
4
1 2
>> a=[1 -1 1 -1;1 -1 -1 1;1 -1 -2 2 ];
>> b=[1;0;-0.5];
>> c=a\b
c =
0
-0.5000
0.5000
0
%A 34 n>m
% n>m
3.33
1
1
2 x1 + 3 x2 + x3 = 1
x + 5 x + 3x = 3
3
1 3 2
2 x + 4 x + 5 x = 2
3
1 3 2
x1 + 2 x2 + x3 = 2
3
>> a=[1/2 1/3 1;1 5/3 3;2 4/3 5;1 2/3 1]; %A 43 n<m
84
3
>> b=[1;3;2;2];
>> c=a\b
c =
1.1930
2.3158
-0.6842
MATLAB
85
% n<m
2.
([A B])
x1 x2 + x3 x4 = 1
3.34 x1 x2 x3 + x4 = 0
x x 2 x + 2 x = 0.5
3
4
1 2
>> a=[1 -1 1 -1 1;1 -1 -1 1 0;1 -1 -2 2 -0.5];
%[A B]
>> rref(a)
ans =
1.0000
-1.0000
0
0
0.5000
0
0
1.0000
-1.0000
0.5000
0
0
0
0
0
Gauss
, X (k ) ,
, x1n )T
{ X ( k ) } lim X ( k ) = X * X *
k
AXB Jacobi
x (0) = ( x1(0) , x2(0) , , xn(0) ) '
()
n
(i = 1, 2, , n)
x ( k +1) = (b a x( k ) ) / a
i
i
ij j
ii
(k = 0,1, 2, )
j =1
j i
jacobi Jacobi ( 6.1 )
85
MATLAB
86
function tx=jacobi(A,b,imax,x0,tol)
% jacobi AX=b
% x0 imax
% tol
% del
del=10^-10;
tx=[x0] ; n=length(x0);
for i=1:n
dg=A(i,i);
if abs(dg)< del
disp('diagonal element is too small');
return
end
end
for k = 1:imax
%Jacobi
for i = 1:n
sm=b(i) ;
for j = 1:n
if j~=i
sm = sm -A(i,j)*x0(j) ;
end
end %for j
x(i)=sm/A(i,i) ;
%
end
tx=[tx ;x] ;
% tx
if norm(x-x0)<tol
return
else
x0=x ;
end
end
%Jacobi
3.35 Jacobi
10 x1 x 2 + 2 x3 = 6
x + 11x x + 3 x = 25
1
2
3
4
2 x1 x 2 + 10 x3 x 4 = 11
3 x 2 x3 + 8 x 4 = 15
x (0) = [0,0, 0,0]' 10 106
>>
>>
>>
>>
>>
>>
>>
86
3
6
7
8
9
10
11
0.988991
1.003199
0.998128
1.000625
0.999674
1.000119
2.011415
1.992241
2.002307
1.998670
2.000448
1.999768
MATLAB
-1.010286
-0.994522
-1.001972
-0.999036
-1.000369
-0.999828
87
1.021351
0.994434
1.003594
0.998888
1.000619
0.999786
[1 2 -1 1]
2Gauss-Seidel
AX=B
n
a
j =1
ij
x j = bi
(i = 1,2,
, n)
Gauss-Seidel
xi( k +1) =
i 1
1
[bi aij x (jk +1)
aii
j =1
j =i +1
ij
x (jk ) ] (i = 1,2,
, n)
k gseidel Gauss-Seidel
function tx= gseidel( A,b,imax,x0,tol)% Gauss-Seidel
%AX=b x0 imax
% tol
del=10^-10;
% del
tx=[x0]; n=length(x0);
for i=1:n
dg=A(i,i);
if abs(dg)< del
disp('diagonal element is too small');
return
end
end
for k = 1:imax
%Gauss-Seidel
x=x0;
for i = 1:n
sm=b(i);
for j = 1:n
if j~=i
sm = sm -A(i,j)*x(j);
end
end
x(i)=sm/A(i,i);
end
tx=[tx;x];
% tx
if norm(x-x0)<tol
return
else
x0=x;
end
end
% Gauss-Seidel
87
MATLAB
88
(10 )
Gauss-Seidel Jacobi Gauss-Seidel
5 Jacobi 10
3SOR()
Gauss-Seidel
i 1
n
( k +1)
(k )
( k +1)
=
x
(1
)
x
[
b
a
x
aij x (jk ) ] (i = 1, 2, , n)
i
i
i
ij j
a
j =1
j = i +1
ii
X (0) = ( x (0) , x(0) , , x (0) )T
=
(
1,
2,
)
k
n
1
2
=1 Gauss-Seidel
SOR sor
function tx = sor( A,b,imax,x0,tol,w) % Gauss-Seidel
%AX=b x0 imax
% tol w
del=10^-10;
% del
tx=[x0] ; n=length(x0);
for i=1:n
dg=A(i,i);
if abs(dg)< del
disp('diagonal element is too small');
return
end
end
for k = 1:imax
%SOR
x=x0 ;
for i = 1:n
88
MATLAB
89
sm=b(i);
for j = 1:n
if j~=i
sm = sm -A(i,j)*x(j);
end
end
x(i)=sm/A(i,i);
%
x(i)=w*x(i)+(1-w)*x0(i);
end
tx=[tx;x];
% tx
if norm(x-x0)<tol
return
else
x0=x;
end
end
%SOR
3.37 3.35
>>
>>
>>
>>
>>
>>
>>
>>
1
2
3
4
5
6
7
8
9
SOR Gauss-Seidel
MATLAB solve ()()
5.6
3.5
0
(Sparse Matrix)
(Full Matrix)
89
MATLAB
90
MATLAB
3.5.1
1 sparse
MATLAB sparse
(1) S = sparse(A) A S S
sparse(S) S
(2) S = sparse(i,j,s,m,n,nzmax) ij s mn nzmax
A () ij s s
i j s i j
(3) S = sparse(i,j,s,m,n) i j s m n S
(i,j) s
(4) S = sparse(i,j,s) s i
j
(5) S = sparse(m,n) sparse([],[],[],m,n,0) mn
3.38 A
>> A=[1 2 0;0 2 3;1 0 2];
>> S=sparse(A)
S =
(1,1)
1
(3,1)
1
(1,2)
2
(2,2)
2
(2,3)
3
(3,3)
2
>>B = full(A)
B =
1
2
0
0
2
3
1
0
2
3.39 45
6 0 0
0 0 7
A=
0 0 0
0 0 0
>>
>>
>>
>>
90
i
j
s
A
=
=
=
=
[1 2 4];
[1 3 5];
[6 7 8];
sparse(i,j,s)
0 0
0 0
0 0
0 8
3
A =
(1,1)
(2,3)
(4,5)
MATLAB
91
6
7
8
2 spdiags
spdiags spdiags
(1) [B,d]=spdiags(A) mn A B min(m,n) p
A p d p1 A
(2) B= spdiags(A,d) A d
(3) A= spdiags(B,d,A) B A d
(4) A= spdiags(B,d,m,n) B mn A
d
3.40 A
>> A=[0
5
0
10
0
0
6
0
3
0
0
7
1
4
0
0
0
2
5
0
>> [B, d] =spdiags(A)
B =
0
0
5
10
0
0
6
11
0
3
7
12
1
4
8
0
2
5
9
0
d =
-3
-2
1
3
>> s=spdiags(B,d,A)
s =
(3,1)
3
(4,1)
1
(1,2)
5
(4,2)
4
(5,2)
2
0
0
A = 3
1
0
5
0
0
4
2
0
11
0
8
0
0
0
12
0
9];
0
6
0
0
5
10
0
7
0
0
0
11
0
8
0
0
0
12
0
9
% s= spdiags(B,d,5,6)
91
MATLAB
92
(2,3)
(5,3)
(1,4)
(3,4)
(2,5)
(4,5)
(3,6)
(5,6)
6
5
10
7
11
8
12
9
A -3-213
B B A s
A 3.8
0
-1
1
3.8
3.5.2
MATLAB
MATLAB
()
MATLAB 3
mn nnz nzmax
nzmax nnz MATLAB 3
(1) nzmax
(2) nzmax
(3) n+1 n
n+1
nzmax nzmax+n+1
8B 4B
8nzmax+4(nzmax+n+1)
nzmax n
m n n
m
92
MATLAB
93
MATLAB spalloc
S = spalloc(m,n,nzmax)
3.41 mn (2 ,2) mn (2,220)
20
>> S1 = spalloc(2^20,2,1);
>> S2 = spalloc(2,2^20,1);
>> whos S1 S2
Name
Size
Bytes Class
S1
1048576x2
24 double array (sparse)
S2
2x1048576
4194320 double array (sparse)
Grand total is 2 elements using 4194344 bytes
S1 S2 4194344B S1 24B
3.42 1010 A B=sparse(A)
>> A=eye(10);
>> B=sparse(A);
>> whos A B
Name
Size
Bytes
A
10x10
800
B
10x10
164
Grand total is 110 elements using 964 bytes
Class
double array
double array (sparse)
3.5.3
3.4
MATLAB
MATLAB 9 3-8
AX=B(pcg)A
minres symmlq A lsqr A gmres
A 4
3-8
3-8
bicg
Bi
minres
bicgstab
Bi
pcg
cgs
qmr
gmres
symmlq
LQ
lsqr
pcg
93
MATLAB
94
[x,flag,relres,iter,resvec]=pcg(A,b,tol,Maxit)
tol Maxit x
flag flag=0
flag relres
iter resvec
3.43 AX=B
2 1
1
1 2 1
A= 1 2
B=
500
1 2 500500
MATLAB
>>
>>
>>
>>
n=500;
A1=sparse(1:n,1:n,2,n,n);
A2=sparse(1:n-1,2:n,1,n,n);
A=A1+A2+A2';
>> b=[1:n]';
>> tic;x1=A\b;t1=toc
t1 =
0.0160
>> A3=full(A);
>> tic;x2=A3\b;t2=toc
t2 =
0.0780
>> max(abs(x1-x2))
ans =
5.8719e-011
>> min(abs(x1-x2))
ans =
2.8188e-013
%
%-1
% A ( 1
%-1 )
%
t2 t1 5 n
3.44 pcg 3.35
10 106
>> A=[10 -1 2 0;-1 11 -1 3;2 -1 10 -1;0 3 -1 8];
>> b=[6 25 -11 15]';
%
>> [x,flag,relres,iter,resvec]=pcg(A,b,10.^( -6),10)
x =
1.0000
2.0000
-1.0000
1.0000
flag =
94
MATLAB
95
0
relres =
7.9165e-017
iter =
4
resvec =
31.7333
5.1503
1.0433
0.1929
0.0000
4relres 7.9e-017
3.6
MATLAB
dsolve ()( 5.6 )
95
MATLAB
96
MATLAB
dy
= f ( x, y )
dx
f ( x, y ) x y y ' = f ( x, y )
y
x = x0
= y0
y ' = f ( x, y )
y x = x0 = y0
( x0 , y0 )
3.6.1
y ( xn +1 ) y ( xn )
[ xn , xn +1 ]
y ' ( x)
h
f ( x, y ( x)) x [ xn , xn +1 ] xn f ( xn , y ( xn ))
y ( xn ) yn yn y ( xn ) yn +1 y ( xn +1 )
yn +1 = yn + hf ( xn , yn ) f ( x, y ( x)) x
[ xn , xn +1 ] xn +1 f ( xn , y ( xn )) yn +1 = yn + hf ( xn +1 , yn +1 )
xn +1 , yn +1 yn f ( x, y ) y
h
yn +1 = yn + [ f ( xn , yn ) + f ( xn +1 , yn +1 )]
2
yn +1 = yn + hf ( xn , yn ) yn +1
h
yn +1 = yn + [ f ( xn , yn ) + f ( xn +1 , yn +1 )]
2
(1) Euler1
function[xout,yout]=euler1(ypfun,xspan,y0,h) %
x=xspan(1):h:xspan(2);y(:,1)=y0(:);
for i=1:length(x)-1,
y(:,i+1)=y(:,i)+h*feval(ypfun,x(i),y(:,i));
end
96
MATLAB
97
xout=x';yout=y';
[xout,yout]=euler1('ypfun',xspan,y0,h)
[xout,yout]=euler2('ypfun',xspan,y0,h)
(1)
y ' = y 2 x / y (0 x 1)
3.46
y (0) = 1
h = 0.1 y = 1 + 2 x
exam1fun.m
function f=exam1fun (x,y)
f=y-2*x./y;
f=f(:);
% exam1fun.m
% f
>>
>>
>>
>>
x1
xspan=[0 1];
y0=1;
h=0.1;
[x1,y1]=euler1('exam1fun',xspan,y0,h)
=
0
0.1000
0.2000
0.3000
0.4000
0.5000
0.6000
0.7000
0.8000
0.9000
1.0000
97
MATLAB
98
y1 =
1.0000
1.1000
1.1918
1.2774
1.3582
1.4351
1.5090
1.5803
1.6498
1.7178
1.7848
>> [x2,y2]= euler2('exam1fun',xspan,y0,h)
x2 =
0
0.1000
0.2000
0.3000
0.4000
0.5000
0.6000
0.7000
0.8000
0.9000
1.0000
y2 =
1.0000
1.0959
1.1841
1.2662
1.3434
1.4164
1.4860
1.5525
1.6165
1.6782
1.7379
3.9 3-9
3-9
n
0
0.1000
0.2000
0.3000
0.4000
0.5000
98
1.0000
1.1000
1.1918
1.2774
1.3582
1.4351
1.0000
1.0959
1.1841
1.2662
1.3434
1.4164
1.0000
1.0954
1.1832
1.2649
1.3416
1.4142
MATLAB
99
0.6000
1.5090
1.4860
1.4832
0.7000
1.5803
1.5525
1.5492
0.8000
1.6498
1.6165
1.6125
0.9000
1.7178
1.6782
1.6733
1.0000
1.7848
1.7379
1.7321
3.9 0.1
3.6.2
[ xi , xi +1 ]
--
-MATLAB
- ode23ode45ode113ode23sode15s
ode23(-) ode45(-)
1-(ode23)
ode23
(1) [T,Y]=ODE23('F',TSPAN,Y0) 'F'
f(x,y) M TSPAN=[T0 TFINAL]Y0
ode23 Y0 T0 TFINAL y ' = F (t , y )
F(T, Y) T Y T
Y T T
T0,T1,
99
100
MATLAB
TSPAN=[T0 T1 TFINAL]TSPAN
(2) [T,Y]=ODE23('F',TSPAN,Y0,OPTIONS) options
ODESET Options RelTol( 1e-3)
AbsTol( 1e-6)
(3) [T,Y]=ODE23('F',TSPAN,Y0,OPTIONS,P1,P2,) P1,P2,
F F(T,Y,FLAG,P1,P2,) OPTIONS OPTIONS=[ ]
ODE ( ODEFILE ) TSPANY0 OPTIONS
TSPAN Y0 ODE23 ODE [TSPAN, Y0,OPTIONS] =
F([ ],[ ],'init ') ODE23
ODE23 ODE23('F')
(4) [T,Y,TE,YE,IE]=ODE23('F',TSPAN,Y0,OPTIONS) options
'on'ODE P(T,Y,'events')
ODEFILE TE YE TE
IE
2-(ode45)
y (0) = 1
h = 0.1 dsolve y = 1/( x + 1 2e x )
-
exam2fun.m
function f=exam2fun (x,y)
f=-y-x*y.^2;
f=f(:);
>> [x1,y1]=ode23('exam2fun',[0:0.1:1],1)
x1 =
0
0.1000
0.2000
0.3000
0.4000
0.5000
0.6000
0.7000
0.8000
0.9000
1.0000
100
MATLAB
101
y1 =
1.0000
0.9006
0.8046
0.7144
0.6314
0.5563
0.4892
0.4296
0.3772
0.3312
0.2910
>> [x2,y2]=ode45('exam2fun',[0:0.1:1],1)
x2 =
0
0.1000
0.2000
0.3000
0.4000
0.5000
0.6000
0.7000
0.8000
0.9000
1.0000
y2 =
1.0000
0.9006
0.8046
0.7144
0.6315
0.5563
0.4892
0.4296
0.3772
0.3312
0.2910
-- 3-10
3-10 -
n
ODE23
ODE45
1.0000
1.0000
1.0000
1.0000
0.1000
0.9010
0.9006
0.9006
0.9006
0.2000
0.8053
0.8046
0.8046
0.8046
0.3000
0.7153
0.7144
0.7144
0.7144
0.4000
0.6326
0.6314
0.6315
0.6315
0.5000
0.5576
0.5563
0.5563
0.5563
101
MATLAB
102
ODE23
ODE45
0.6000
0.4906
0.4892
0.4892
0.4892
0.7000
0.4311
0.4296
0.4296
0.4296
0.8000
0.3786
0.3772
0.3772
0.3772
0.9000
0.3326
0.3312
0.3312
0.3312
1.0000
0.2924
0.2910
0.2910
0.2910
--
-
3.7
MATLAB
3.8
1. roots x 2 x 1 = 0
2. y = sin x, 0 x 2 n (n 511)
m (m 50100)
n
3. p x p = 1.0332e ( x + 500) / 7756
3-11
102
MATLAB
103
3-11
/m
300
600
/Pa
0.9689
0.9322
0.8969
1000
1500
2000
0.8519
0.7989
0.7491
x2
1 3
e dx
2 3
-55 x
1 xdx
5. - 4 2
0 x +4
n=4 4
6. Jacobi Gauss-Seidel
5 1 2 1 x1 9
2 5 1 1 x 9
2 =
1 2 10 2 x3 15
1 2 2 10 x4 15
4.
x(0)=[0,0,0,0]T 5
7. SOR 6 =1.2
8. 6
9. -
y ' = 2 xy 0 x 1.2
y (0) = 1
103
MATLAB
4.1
(Structure Array)
4.1
student(record number)
.number
'20050731025'
()
.name
''
.course
' 1' ' 1' ' 1' ' 1' '' '' ''
' 2' ' 2' ' 2' ' 2' '' '' ''
()
90
85
63
70
84
92
65
()
91
76
82
88
75
87
91
()
.score
()
4.1
()
student (Structure)
numbernamecoursescore (Filed)
.
4.1.1
(1)
(2) struct
105
struct_name(record#).field_name=data
11
4.1 4.1 student
>> student.number='20050731025';
>> student.name='';
>> student.course={' 1' ' 1' ' 1' ' 1' '' '' ''; ...
' 2' ' 2' ' 2' ' 2' '' '' ''};
>> student.score=[90 85 63 70 84 92 65;91 76 82 88 75 87 91];
student.course={} 4.2
>> student
student =
number: '20050731025'
name: ''
course: {2x7 cell}
score: [2x7 double]
>> size(student)
ans =
1
1
student 11
4.2 4.1 student
>> student(2).number='20050731026';
>> student(2).name='';
>> student(2).course=[' 1' ' 1' ' 1' ' 1' '' '' ''; ...
' 2' ' 2' ' 2' ' 2' '' '' ''];
>> student(2).score=[80 95 70 90 64 82 75;81 66 92 78 85 67 81];
>> student
student =
1x2 struct array with fields:
number
name
course
score
student 12
2 struct
struct
105
MATLAB
106
(1)
(2) 4.4
student
>> student=struct('number',{'20050731025','20050731026'},'name',{'
',''},...
'course',{{' 1' ' 1' ' 1' ' 1' '' '' ''; ' 2' '
2' ' 2'...' 2' '' '' ''}},'score',{[90 85 63 70 84 92 65;91
76 82 88 75 87 91]});
>> student(1,1).score
%
ans =
90
85
63
70
84
92
65
91
76
82
88
75
87
91
>> student(1,2).score
%
ans =
90
85
63
70
84
92
65
91
76
82
88
75
87
91
4.1.2
4-1
106
107
4-1
struct
isstruct
getfield
isfield
fieldnames
rmfield
setfield
orderfields
1.
()
total
>> student(2).total
ans =
[]
2.
fieldnames
fieldnames(struct_name)
3.
rmfield
108
MATLAB
4.
5.
1)
(1) struct_name.field_name(m,n)
(2) struct_name(i,j).field_name(m,n)
(1) 11
mn(2) 11
109
gaoshu1 =
90
gaoshu2 =
91
2) getfield
(1) getfield (struct_name, field_name)
(2) getfield (struct_name, {i,j}, field_name,{m,n})
(1) struct_name.field_name(2) struct_name(i,j).field_name (m,n)
4.11 getfield (1) 4.9
>> xuehao=getfield(student,'number');
>> xingming=getfield(student,'name');
>> gaoshu1=getfield(student,'score',{1,1});
>> gaoshu2=getfield(student,'score',{2,1});
>> xuehao,xingming,gaoshu1,gaoshu2
xuehao =
20050731025
xingming =
gaoshu1 =
90
gaoshu2 =
91
110
MATLAB
>> chengji2=getfield(student,{1,2},'score');
>> chengji22=chengji2(2,:);
>> number, name, gaoshu21, chengji22
number =
'20050731025'
'20050731026'
name =
''
''
gaoshu21 =
91
chengji22 =
81
66
92
78
85
67
81
%
%
%
3) deal
[Y1,Y2,Y3,...]=deal(struct_name(i,j).field_name1,struct_name(i,j).field
_name2, struct_ name(i,j).field_name3,...)
Y1=struct_name(i,j).field_name1;Y2=struct_name(i,j).field_name2;
Y3=struct _name(i,j).field_name3;
name2 =
gaoshu21 =
91
chengji22 =
81
66
92
78
85
67
81
6
(1)
struct_name.field_name(m,n)= field_value
struct_name{i,j}.field_name(m,n) = field_value
11
mn 11
111
>> student(2).score(2,7)=66;
>> student(1).name,student(2).number,student(2).score(2,7)
ans =
ans =
20050731028
ans =
66
(2) setfield
struct_name =setfield(struct_name,'field', field_value)
struct_name =setfield(struct_name,{i,j},'field',{m,n}, field_value)
struct_name.field_name(m,n)= field_value struct_
name{i,j}.field_name(m,n) = field_value
4.15 setfield 4.14
>> student=setfield(student,{1,1},'name','');
>> student=setfield(student,{1,2},'number','20050731028');
>> student=setfield(student,{1,2},'score',{2,7},66);
>> student(1).name,student(2).number,student(2).score(2,7)
ans =
ans =
20050731028
ans =
66
7
(1) struct_name = orderfields(struct_name1)
(2) struct_name = orderfields(struct_name1, struct_name2)
(3) struct_name = orderfields(struct_name1,c)
(4) struct_name = orderfields(struct_name1, perm)
(5) [struct_name, perm] = orderfields(...)
(1) struct_name1 ASCII (2)
struct_name1 struct_name2 struct_name2
struct_name1 (3) struct_name1 c
c struct_name1 (4) struct_name1
perm perm struct_name1
(5)(14) struct_name
perm
4.16 4.4 student
>> [snew1, perm1] = orderfields(student)
snew1 =
1x2 struct array with fields:
course
111
112
MATLAB
name
number
score
perm1 =
3
2
1
4
>> [snew2, perm2] = orderfields(student,{'name','number','course','score'})
snew2 =
1x2 struct array with fields:
name
number
course
score
perm2 =
2
1
3
4
>> [snew3, perm3] = orderfields(student,[2 4 1 3])
snew3 =
1x2 struct array with fields:
name
score
number
course
perm3 =
2
4
1
3
>> snew4 = orderfields(student,[4 3 1 2])
snew4 =
1x2 struct array with fields:
score
course
number
name
8
(1) tf = isstruct(A)
(2) tf = isfield(struct_name,field_name)
(1) A tf =1 tf =0(2) field _name
struct_name tf =1 tf =0
4.17 4.4 student
>> tf = isstruct(student)
tf =
1
112
113
4.2
(Cell Array)
(Cell)
4.2 22 (1,1)(1,2)
(2,1)(2,2)
Cell 1,1
'20050731025'
'20050731026'
Cell 1,2
''
''
''
''
''
''
Cell 2,1
Cell 2,2
''
''
90 85 63
70 84 92 65
91 76 82
88 75 87 91
80 95 70
90 64 82 75
81 66 92
78 85 67 81
4.2 22
4.2.1
(1)
(2) cell
cell_name{i,j} = {value}
4.18 4.2
>> student{1,1}=['20050731025';'20050731026'];
113
114
MATLAB
>> student{2,1}={'';''};
>> student{1,2}={' 1' ' 1' ' 1' ' 1' '' '' '
'; ...
' 2' ' 2' ' 2' ' 2' '' '' ''};
>> student{2,2}={[90 85 63 70 84 92 65; 91 76 82 88 75 87
91]; ...
[80 95 70 90 64 82 75; 81 66 92 78 85 67 81]};
>> student
student =
[2x11 char]
{2x7 cell}
{2x1 cell}
{2x1 cell}
>> student(1,1)={['20050731025';'20050731026']};
>> student(2,1)={{'';''}};
>> student(1,2)={{' 1' ' 1' ' 1' ' 1' '' '' '
'; ...
' 2' ' 2' ' 2' ' 2' '' '' ''}};
>> student(2,2)={{[90 85 63 70 84 92 65; 91 76 82 88 75 87
91]; ...
[80 95 70 90 64 82 75; 81 66 92 78 85 67 81]}};
>> student
student =
[2x11 char]
{2x7 cell}
{2x1 cell}
{2x1 cell}
(1)
>> student{1,2}
ans =
' 1'
' 1'
' 2'
' 2'
>> student(1,2)
ans =
{2x7 cell}
' 1'
' 2'
' 1'
' 2'
''
''
''
''
''
''
114
4.3
115
student
>> cellplot(student)
2 cell
(1) cell_name = cell(n)
(2) cell_name = cell(m,n) cell_name = cell([m n])
(3) cell_name = cell(m,n,p,...) cell_name = cell([m n p ...])
(4) cell_name = cell(size(A))
(1) nn (2) mn (3)
mnp(4) A
cell
1
4.2.2
4-2
4-2
celldisp
iscell
iscellstr
cellstr
char
cell2struct
struct2cell
mat2cell
cell2mat
num2cell
115
116
MATLAB
1.
2.
()
()
4.22 4.21 student(1,3)
3
>> student{1,3}=[]
student =
[2x11 char]
{2x7
{2x1 cell}
{2x1
>> student(:,3)=[]
student =
[2x11 char]
{2x7
{2x1 cell}
{2x1
cell}
cell}
[]
[]
cell}
cell}
117
ans =
3.
celldisp
celldisp(cell_name)
4.24 4.18 student
>> celldisp(student)
student{1,1}{1} =
20050731025
20050731026
student{2,1}{1} =
student{1,2}{1}{1,1} =
1
student{1,2}{1}{2,1} =
2
student{1,2}{1}{1,2} =
1
student{1,2}{1}{2,2} =
2
student{1,2}{1}{1,3} =
1
student{1,2}{1}{2,3} =
2
student{1,2}{1}{1,4} =
1
student{1,2}{1}{2,4} =
2
student{1,2}{1}{1,5} =
student{1,2}{1}{2,5} =
student{1,2}{1}{1,6} =
student{1,2}{1}{2,6} =
student{1,2}{1}{1,7} =
student{1,2}{1}{2,7} =
student{2,2}{1} =
90
85
63
70
91
76
82
88
student{2,2}{2} =
80
95
70
90
81
66
92
78
84
75
92
87
65
91
64
85
82
67
75
81
117
118
MATLAB
4.
(1)
cell_name{i,j}
(2)
cell_name{i,j}(m,n)
>> cell_11=student(1,1)
cell_11 =
[2x11 char]
>> cell_11a=student{1,1}
cell_11a =
20050731025
20050731026
>> cell_111=student{1,1}(1,:)
cell_111 =
20050731025
>> cell_1114=student{1,1}(1,4)
cell_1114 =
5
>> cell_12=student(1,2)
cell_12 =
{2x7 cell}
>> cell_12a=student{1,2}
cell_12a =
' 1'
' 1'
' 1'
' 2'
' 2'
' 2'
>> cell_1222=student{1,2}(2,2)
cell_1222=
' 2'
>> cell_1222c=student{1,2}{2,2}
% student(1,1)
% student(1,1)
% student(1,1)
% 1
% student(1,1)
% 1 4
% student(1,2)
% student(1,2)
' 1'
''
''
''
' 2'
''
''
''
% student(1,2)
%(2,2)
% student(1,2)
%(2,2)
cell_1222c =
2
>> cell_1222c1=student{1,2}{2,2}(1)% student(1,2)
%(2,2)
% 1
cell_1222c1 =
>> cell_22=student(2,2)
% student(2,2)
cell_22 =
{2x1 cell}
118
>> cell_22=student{2,2}
cell_22 =
[2x7 double]
[2x7 double]
>> cell_221=student{2,2}(1)
cell_221 =
[2x7 double]
>> cell_221a=student{2,2}{1}
119
% student(2,2)
% student(2,2) (1)
% student(2,2) (1)
%
cell_221a =
90
85
63
70
84
92
65
91
76
82
88
75
87
91
>> cell_2211=student{2,2}{1}(1,:) % student(2,2) (1)
% 1
cell_2211 =
90
85
63
70
84
92
65
>> cell_22112=student{2,2}{1}(1,2)% student(2,2) (1)
% 1 2
cell_22112 =
85
5.
(1) tf = iscell(A)
( A )
(2) tf = iscellstr(A)
( A )
4.26
>> data1=[1 2 3;4 5 6];
>> data2={'123';'456'};
>> data={data1 data2};
>> tf1=iscell(data1)
tf1 =
0
>> tfs1=iscellstr(data1)
tfs1 =
0
>> tf2=iscell(data2)
tf2 =
1
>> tfs2=iscellstr(data2)
tfs2 =
1
>> tf=iscell(data)
tf
=
1
>> tfs=iscellstr(data)
tfs =
0
6.
1)
119
120
MATLAB
4.27
>> char_1=['';'';'';'']
char_1 =
>> cell_1=cellstr(char_1)
cell_1 =
''
''
''
''
>> cell_2={'' '' ''}
cell_2 =
''
''
''
>> char_2=char(cell_2)
char_2 =
>> len_21=length(char_2(1,:))
len_21 =
4
>> len_22=length(char_2(2,:))
len_22 =
4
>> len_23=length(char_2(3,:))
len_33 =
4
2)
(1) struct_name = cell2struct (cell_name, fields,dim) ()
(2) cell_name = struct2cell (struct _name)
()
(Fields)(Dim)
4.28
>> cell_1 = {'','',20; '','',19}
cell_1 =
''
''
[20]
''
''
[19]
>> struct_1=cell2struct(cell_1,{'name','sex','age'},2)
struct_1 =
120
121
3)
(1) cell_name = mat2cell(x,m,n) ()
(2) x = cell2mat(cell_name)
()
(1)
mn
m n
n
4.29
>> a=[1 2 3 4; 5 6 7 8; 9 10 11 12]
a =
1
2
3
4
5
6
7
8
9
10
11
12
>> c=mat2cell(a,[1,2],[3,1])
c =
[1x3 double]
[4]
[2x3 double]
[2x1 double]
>> celldisp(c)
c{1,1} =
1
2
3
c{2,1} =
5
6
7
9
10
11
c{1,2} =
4
c{2,2} =
8
121
122
MATLAB
12
>> c=mat2cell(a,[1,2],[4])
c =
[1x4 double]
[2x4 double]
>> c=mat2cell(a,[1,1,1])
c =
[1x4 double]
[1x4 double]
[1x4 double]
>> a1=cell2mat(c)
a1 =
1
2
3
4
5
6
7
8
9
10
11
12
4)
cell_name = num2cell(x,dims)
mn dims
dims=1 1
dims=2 1
dims=[1,2]
4.30
>> a=[1 2 3 4; 5 6 7 8;
a =
1
2
3
4
5
6
7
8
9
10
11
12
>> c1=num2cell(a)
c1 =
[1]
[2]
[3]
[5]
[6]
[7]
[9]
[10]
[11]
>> c2=num2cell(a,1)
c2 =
[3x1 double]
[3x1
>> celldisp(c2)
c2{1} =
1
5
9
c2{2} =
2
6
10
c2{3} =
3
7
122
9 10 11 12]
[4]
[8]
[12]
double]
[3x1 double]
[3x1 double]
123
11
c2{4} =
4
8
12
>> c3=num2cell(a,2)
c3 =
[1x4 double]
[1x4 double]
[1x4 double]
>> celldisp(c3)
c3{1} =
1
2
3
4
c3{2} =
5
6
7
8
c3{3} =
9
10
11
12
>> c4=num2cell(a,[1,2])
c4 =
[3x4 double]
>> celldisp(c4)
c4{1} =
1
2
3
4
5
6
7
8
9
10
11
12
4.2.3
4.31
>> clear all
>> c_strct{1}.number='20050731025';
>> c_strct{1}.student={''};
>> c_strct{1}.course={'' ''};
>> c_strct{1}.score=[86 90];
>> c_strct{2}.ID=[1 2];
>> c_strct{2}.teacher={'' ''};
>> c_strct{2}.course={'' ''};
>> c_strct
c_strct =
[1x1 struct]
[1x1 struct]
>> c_strct{1}
ans =
number: '20050731025'
student: {''}
course: {'' ''}
score: [86 90]
>> c_strct{2}
ans =
123
MATLAB
124
ID: [1 2]
teacher: {'' ''}
course: {'' ''}
c_strct
4.3
MATLAB
4.4
1
(1) ____________________
(2) ____________________
(3) ____________________
__________
__________
(4) ____________________
__________
(5) ____________________
(6) ____________________
__________
(7) __________
__________
(8) __________
__________
(9) ____________________
2
(1) MATLAB
>> teacher =struct('name',{'John','Smith'},'age',{25,30});
124
125
>> fieldnames(teacher)
Aans =
'name'
'age'
Cans =
Bans =
name
age
Dans =
name: 'John'
age: 25
name: 'Smith'
age: 30
Smith __________
(3) (1) teacher
Agetfield(teacher,'age(2)')
Bgetfield(teacher(2),'age')
Cteacher.age(2)
Dteacher(2).age
(4) MATLAB
>> teacher =struct('name',{'John','Smith'},'age',{25,30});
name: 'John'
age: 25
C a2 =
John
(5) MATLAB
name: John
age: 25
Da2 =
'John'
25
C
30
Da1 =
5
3
(1) 4-3 4-4 2005 4.4
teacher05 student05
125
MATLAB
126
.id
.number
.name
.name
.course
.course
.score
4.4
teacher05 student05
4-3 2005
(number)
(name)
(score)
(course )
20050731021
708778
20050731031
829088
20050731036
889291
4-4 2005
(id)
(name)
(course )
xx010
xx016
Cell 1,2
teacher05 (struct)
student05 (struct)
4.5 12
(name)
(score)
(course )
20040734005
7580
20040734036
5665
126
127
4-6 2004
(id)
(name)
(course )
xx012
xx016
celldisp(class)
class{2}
class(2)
class{2}(2)
class{2}{2}
class{2}{2}
class{2}{2}(2)
class{2}{2}(2).name,class{2}{2}(2).course,class{2}{2}(2).score
class{2}{2}(2).score(1)=85;class{2}{2}(2).course(1),class{2}{2}(2).score(1)
127
MATLAB
MATLAB
MATLAB
MATLAB
MATLAB
5.1
3 MATLAB
MATLAB MATLAB
5.1.1
sym( )
syms
1.
sym( ) A
sym(A) sym(A,flag)
flag 4 'r''d''e''f '
MATLAB
129
e
f
sym( )
5.1
>>log(2)
ans =
0.6931
>>(3*4-2)/5+1
ans =
3
>>f1=sym('log(2)')
f1 =
log(2)
>>f2=sym ('(3*4-2)/5+1')
f2 =
(3*4-2)/5+1
% f1
%
%
5.2 sym( )
>> num=log(2)
num =
0.6931
>> a=sym(log(2),'d')
a =
.69314718055994528622676398299518
>> b=sym(log(2),'f')
b =
'1.62e42fefa39ef'*2^(-1)
>> c=sym(log(2),'r')
c =
6243314768165359*2^(-53)
>> d=sym(log(2))
d =
6243314768165359*2^(-53)
>> e=sym(log(2),'e')
e =
6243314768165359*2^(-53)
% log(2)
% 32
num abcd
e
2.
(1) sym( )
sym('x')
sym('x','real')
sym('x','unreal')
129
MATLAB
130
(2) syms
syms arg1 arg2 ...
syms arg1 arg2 ... real
syms arg1 arg2 ... unreal
'real''unreal'
5.3 sym( )
>> sym('x')
ans =
x
>> sym('y','real')
ans =
y
>> sym('z','unreal')
ans =
z
% x
% y
% z
5.4 syms
>>syms a b c
>>syms m n real
>>syms x y z unreal
syms sym( )
5.1.2
5.5
>> syms x y z r s t;
>> x^2+2*x+1
ans =
x^2+2*x+1
>> exp(y)+exp(z)^2
ans =
exp(y)+exp(z)^2
>> r^2+sin(x)+cos(y)+log(s)+exp(t)
ans =
r^2+sin(x)+cos(y)+log(s)+exp(t)
>> f1= r^2+sin(x)+cos(y)+log(s)+exp(t)
f1 =
r^2+sin(x)+cos(y)+log(s)+exp(t)
>> f2=sym(r^2+sin(x)+cos(y)+log(s)+exp(t))
f2 =
r^2+sin(x)+cos(y)+log(s)+exp(t)
>> f3=sym('r^2+sin(x)+cos(y)+log(s)+exp(t)')
f3 =
r^2+sin(x)+cos(y)+log(s)+exp(t)
130
MATLAB
131
f1f2f3
>> f4='r^2+sin(x)+cos(y)+log(s)+exp(t)';
>> g='sin(a)+cos(b)' ;
% f4 g
5.1.3
MATLAB
sym
5.6
>> S=sym('[l,x,y,z;n,u,v,w;a,b,c,d;g,h,j,p]')
S =
[ l,
x, y, z
n,
u, v, w
a,
b, c, d
g,
h, j, p ]
>> H=sym('[cos(t), -sin(t); sin(t), cos(t)]')
H=
[cos(t),
-sin(t)]
[sin(t),
cos(t)]
5.7 sym()
sym
>>S=sym(M)
S=
[11/10,
6/5,
21/10,
11/5,
31/10,
16/5,
13/10
23/10
33/10]
sym( )
sym
>>A=[sin(1) cos(2)]
A=
0.84147098480790 -0.41614683654714
>>sym(A)
ans=
[7579296827247854*2^(-53), -7496634952020485*2^(-54)]
size( )()
131
MATLAB
132
5.8 size()
>>s=size(A)
s=
1
2
>>[s_r,s_c]=size(A)
s_r=
1
s_c=
2
>>s_r=size(A,1)
s_r=
1
>> s_c=size(A,2)
s_c=
2
5.9
>> B=sym('[a,b,c;d,e,f;g,h,k]')
B=
[a, b, c
d, e, f
g, h, k]
>>B(2,3)
ans=
f
5.2
MATLAB
2
5.2.1
A+BA-B A B A B
A+BA-B A B
5.10
f = 2 x 2 + 3x 5
g = x2 x + 7
>> syms x fx gx
>> fx = 2*x^2+3*x-5
fx =
2*x^2+3*x-5
>> gx= x^2-x+7
132
MATLAB
133
gx=
x^2-x+7
>> fx+gx
ans=
3*x^2+2*x+2
>> fx-gx
ans=
x^2+4*x-12
5.11
>> syms a b c d e f g h;
>> A=[a b;c d];B=[e f;g h];
>> A+B
ans =
[ a+e, b+f]
[ c+g, d+h]
>> A-B
ans =
[ a-e, b-f]
[ c-g, d-h]
5.2.2
A*BA/B A B A.*B
5.12
>> syms a b c d e f g h;
>> A = [a b; c d];
>> B = [e f; g h];
>> C1 = A.*B
C1 =
[ a*e, b*f]
[ c*g, d*h]
>> C2 = A*B/A
C2 =
[ (d*a*e+d*b*g-c*a*f-c*b*h)/(d*a-c*b), -(b*a*e+b^2*g-a^2*f-b*h*a)/(d*a-c*b)
(d^2*g+d*c*e-c^2*f-c*d*h)/(d*a-c*b), -(d*b*g+b*c*e-c*a*f-d*h*a)/(d*a-c*b)]
>> C3 = A.*A-A^2
C3 =
[
-c*b, b^2-b*a-d*b
c^2-c*a-d*c,
-c*b]
>> syms a11 a12 a21 a22 b1 b2;
>> A = [a11 a12; a21 a22];
>> B = [b1 b2];
>> X = B/A;
% X*A=B
>> x1 = X(1)
x1 =
-(-a22*b1+b2*a21)/(-a12*a21+a11*a22)
>> x2 = X(2)
133
MATLAB
134
x2 =
(-a12*b1+a11*b2)/(-a12*a21+a11*a22)
>> syms x fx gx
>> fx = 3*x^5-x^4+2*x^3+x^2+3
fx =
3*x^5-x^4+2*x^3+x^2+3
>>gx= 1/3*x^3+x^2-3*x-1
gx =
1/3*x^3+x^2-3*x-1
>> fx*gx
ans =
(3*x^5-x^4+2*x^3+x^2+3)*(1/3*x^3+x^2-3*x-1)
>> expand(fx*gx)
%
ans =
x^8+8/3*x^7-28/3*x^6+7/3*x^5-4*x^4-4*x^3+2*x^2-9*x-3
>> fx/gx
ans =
(3*x^5-x^4+2*x^3+x^2+3)/(1/3*x^3+x^2-3*x-1)
>> expand(fx/gx)
%
ans =
3/(1/3*x^3+x^2-3*x-1)*x^5-1/(1/3*x^3+x^2-3*x-1)*x^4+2/(1/3*x^3+x^2-3*x-1
)*x^3+1/(1/3*x^3+x^2-3*x-1)*x^2+3/(1/3*x^3+x^2-3*x-1)
5.3
5.3.1
MATLAB
(1) i j
(2) x x
(3) x x
x x
x w y y
findsym( ) MATLAB
(1) r = findsym(S) S (
i j ) S
findsym()
(2) r = findsym(S,n) S x n
5.14
134
5
>> syms
>> S1 =
S1 =
t
>> S2 =
S2 =
NaN, x,
>> S3 =
S3 =
y,a
5.3.2
MATLAB
135
a x y z t alpha beta
findsym(sin(pi*t*alpha+beta),1)
findsym(x+i*y-j*z+eps-nan)
y, z
findsym(a+y,2)
MATLAB (collect)
(expand)(factor)(simplify)(simple)(numden)
(pretty)
1. (collect)
collect( )
(1) R = collect(S) S
(2) R = collect(S,v) v
1
3
5.15 f = x 2 y + xy x 2 2 x g = xe 2 x + e 2 x f x
4
16
g e2 x
>> syms x y a b c
>> f=x^2*y+y*x-x^2-2*x;
>> g=-1/4*x*exp(-2*x)+3/16*exp(-2*x);
>> fx=collect(f)
% x f
fx =
(y-1)*x^2+(y-2)*x
>> gepx=collect(g,exp(-2*x))
gepx =
(-1/4*x+3/16)*exp(-2*x)
2. (expand)
expand( )
R = expand(S)
S
5.16
>> syms x y a b c t
>> E1 = expand((x-2)*(x-4)*(y-t))
E1 =
x^2*y-x^2*t-6*y*x+6*x*t+8*y-8*t
>> E2 = expand(cos(x+y))
E2 =
135
136
MATLAB
cos(x)*cos(y)-sin(x)*sin(y)
>> E3 = expand(exp((a+b)^3))
E3 =
exp(a^3)*exp(b*a^2)^3*exp(a*b^2)^3*exp(b^3)
>> E4 = expand(log(a*b/sqrt(c)))
E4 =
log(a*b/c^(1/2))
>> E5 = expand([sin(2*t), cos(2*t)])
E5 =
[2*sin(t)*cos(t),
2*cos(t)^2-1]
>> E6 = expand((x+1)^3)
E6 =
x^3+3*x^2+3*x+1
3. (factor)
factor( )
factor(X)
X X factor(X)
X X factor(X)
16 sym
5.17
>> syms a b x y
>> F1 = factor(x^4-y^4)
F1 =
(x-y)*(x+y)*(x^2+y^2)
>> F2 = factor([a^2-b^2, x^3+y^3])
F2 =
[(a-b)*(a+b), (x+y)*(x^2-x*y+y^2)]
>> F3 = factor(sym('12345678901234567890'))
F3 =
(2)*(3)^2*(5)*(101)*(3803)*(3607)*(27961)*(3541)
4. (simplify)
MATLAB simplify( )
(Bessel gamma )
R = simplify(S)
Maple S
5.18 simplify( )
>> syms x a b c
>> R1 = simplify(sin(x)^4 + cos(x)^4)
R1 =
2*cos(x)^4+1-2*cos(x)^2
>> R2 = simplify(exp(c*log(sqrt(a+b))))
R2 =
136
MATLAB
137
(a+b)^(1/2*c)
>> S = [(x^2+5*x+6)/(x+2),sqrt(16)];
>> R3 = simplify(S)
R3 =
[ x+3, 4]
>> simplify( log(2*x/y) )
ans=
log(2)+log(x)-log(y)
>> simplify( sin(x)^2+3*x+cos(x)^2-5 )
ans=
3*x-4
>> simplify( (-a^2+1)/(1-a) )
ans=
a+1
5. (simple)
simple
(1) r = simple(S)
(2) [r,how] = simple(S)
(1) S S
S
r
(2) r
how
5.19 simple
3
1
6 12
+ 2 + +8
f =
3
x
x
x
>> f=sym('(1/x^3+6/x^2+12/x+8)^(1/3)')
f=
(1/x^3+6/x^2+12/x+8)^(1/3)
>> simple(f)
simplify:
((2*x+1)^3/x^3)^(1/3)
radsimp:
(2*x+1)/x
combine(trig):
((1+6*x+12*x^2+8*x^3)/x^3)^(1/3)
factor:
((2*x+1)^3/x^3)^(1/3)
expand:
(1/x^3+6/x^2+12/x+8)^(1/3)
combine:
(1/x^3+6/x^2+12/x+8)^(1/3)
convert(exp):
(1/x^3+6/x^2+12/x+8)^(1/3)
137
138
MATLAB
convert(sincos):
(1/x^3+6/x^2+12/x+8)^(1/3)
convert(tan):
(1/x^3+6/x^2+12/x+8)^(1/3)
collect(x):
(1/x^3+6/x^2+12/x+8)^(1/3)
mwcos2sin:
(1/x^3+6/x^2+12/x+8)^(1/3)
ans =
(2*x+1)/x
simple
>> simple(sym(cos(x)+sqrt(-sin(x)^2)))
simplify:
cos(x)+(-1+cos(x)^2)^(1/2)
radsimp:
cos(x)+i*sin(x)
combine(trig):
cos(x)+1/2*(-2+2*cos(2*x))^(1/2)
factor:
cos(x)+(-sin(x)^2)^(1/2)
expand:
cos(x)+(-sin(x)^2)^(1/2)
combine:
cos(x)+1/2*(-2+2*cos(2*x))^(1/2)
convert(exp):
1/2*exp(i*x)+1/2/exp(i*x)+1/4*4^(1/2)*((exp(i*x)-1/exp(i*x))^2)^(1/2)
convert(sincos):
cos(x)+(-sin(x)^2)^(1/2)
convert(tan):
(1-tan(1/2*x)^2)/(1+tan(1/2*x)^2)+(-4*tan(1/2*x)^2/(1+tan(1/2*x)^2)^2)^
(1/2)
collect(x):
cos(x)+(-sin(x)^2)^(1/2)
mwcos2sin:
cos(x)+(-sin(x)^2)^(1/2)
ans =
cos(x)+i*sin(x)
6. (numden)
numden( )
[N,D] = numden(A)
A
N D
138
MATLAB
139
5.20
>> syms x y a b c d;
>> [n1,d1] = numden(sym(sin(4/5)))
n1 =
6461369247334093
d1 =
9007199254740992
>> [n2,d2] = numden(x/y + y/x)
n2 =
x^2+y^2
d2 =
y*x
>> A = [a, 1/b;1/c d];
>> [n3,d3] = numden(A)
n3 =
[ a, 1
1, d]
d3 =
[ 1, b
c, 1]
numden( )
7. (pretty)
pretty
(1) pretty(S) 79 S
(2) pretty(S,n) n
5.21
>> syms x t; f=(x^2+x*exp(-t)+1)*(x+exp(-t));
>> f1=collect(f)
f1 =
x^3+2*exp(-t)*x^2+(1+exp(-t)^2)*x+exp(-t)
>> pretty(f1)
3
2
2
x + 2 exp(-t) x + (1 + exp(-t) ) x + exp(-t)
>> f2=collect(f,'exp(-t)')
f2 =
x*exp(-t)^2+(2*x^2+1)*exp(-t)+(x^2+1)*x
>> pretty(f2)
2
2
2
x exp(-t) + (2 x + 1) exp(-t) + (x + 1) x
139
MATLAB
140
5.4
MATLAB
5.4.1
MATLAB limit( )
(1) limit(F,v,a) F va
(2) limit(F,a) F a
(3) limit(F) F 0
(4) limit(F,v,a,'right') limit(F,v,a,'left') F v a
v a +
5.22
>> syms x a t h n;
>> L1 = limit((cos(x)-1)/x)
L1 =
0
>> L2 = limit(1/x^2,x,0,'right')
L2 =
inf
>> L3 = limit(1/x,x,0,'left')
L3 =
-inf
>> L4 = limit((log(x+h)-log(x))/h,h,0)
L4 =
1/x
>> v = [(1+a/x)^x, exp(-x)];
>> L5 = limit(v,x,inf,'left')
L5 =
[exp(a),
0]
>> L6 = limit((1+2/n)^(3*n),n,inf)
L6 =
exp(6)
5.23 f ( x ) = lim
x 0
sin x
g ( x ) = lim sin ( x + 2 y )
y 0
x
>> syms x y
>> f=sin(x)/x;
>> g=sin(x+2*y);
>> fx=limit(f)
fx=
1
>> gx=limit(g,y,0)
140
%
%
% f(x)
% f(x)
MATLAB
141
gx=
sin(x)
5.4.2
MATLAB diff( )
(1) diff(S,'v') S v 1
(2) diff(S) S 1
(3) diff(S,n) S n
(4) diff(S,'v',n) S v n
5.24
>> syms x n
>> y=sin(x)^n*cos(n*x);
>> Xd=diff(y)
Xd =
sin(x)^n*n*cos(x)/sin(x)*cos(n*x)-sin(x)^n*sin(n*x)*n
>> Nd=diff(y, n)
Nd =
sin(x)^n*log(sin(x))*cos(n*x)-sin(x)^n*sin(n*x)*x
5.25
>> syms t
>> f=exp(-t)*sin(t);
>> diff(f,t,2)
ans =
-2*exp(-t)*cos(t)
5.26
>> syms x
>> f1=2*x^2+log(x);
>> f2=1/(x^3+1);
>> f3=exp(x)/x;
>> F=[f1 f2 f3];
>> diff(F,2)
ans =
[4-1/x^2, 18/(x^3+1)^3*x^4-6/(x^3+1)^2*x, exp(x)/x-2*exp(x)/x^2+2*exp(x)/x^3]
>> Fdd=simple(diff(F,2))
Fdd =
[ (4*x^2-1)/x^2, 6*x*(2*x^3-1)/(x^3+1)^3, exp(x)*(x^2-2*x+2)/x^3]
5.4.3
MATLAB int( )
(1) R = int(S,v) S v
R S C
141
MATLAB
142
(2) R = int(S) S
(3) R = int(S,v,a,b) S v a b
(4) R = int(S,a,b) S a b
5.27
>> syms x z t alpha
>> INT1 = int(-2*x/(1+x^3)^2)
INT1 =
2/9*log(x+1)-1/9*log(x^2-x+1)-2/9*3^(1/2)*atan(1/3*(2*x-1)*3^(1/2))-2/9
*(2*x-1)/
(x^2-x+1)-2/9/(x+1)
>> INT2 = int(x/(1+z^2),z)
INT2 =
x*atan(z)
>> INT3 = int(INT2,x)
INT3 =
1/2*x^2*atan(z)
>> INT4 = int(x*log(1+x),0,1)
INT4 =
1/4
>> INT5 = int(2*x, sin(t), 1)
INT5 =
1-sin(t)^2
>> INT6 = int([exp(t),exp(alpha*t)])
INT6 =
[exp(t), 1/alpha*exp(alpha*t)]
MATLAB
5.4.4
Taylor
(1) r=taylor(f,n,v) f v( f
) n-1 Maclaurin ( v=0) v
142
MATLAB
143
143
MATLAB
144
T9 =
sin(a+1/3*pi)+cos(a+1/3*pi)*(x-a)-1/2*sin(a+1/3*pi)*(x-a)^2-1/6*cos(a+1
/3*pi)
*(x-a)^3+1/24*sin(a+1/3*pi)*(x-a)^4+1/120*cos(a+1/3*pi)*(x-a)^5
5.5
Z
MATLAB
MATLAB
5.5.1
1
f ( x ) (Fourier) f = f ( x ) F = F ( w )
F ( w) =
f ( x )e jwx dx
MATLAB fourier( )
(1) F = fourier(f) f f x F
w f = f ( x ) F = F ( w ) f = f ( w ) fourier(f) t
F = F ( t )
(2) F = fourier(f,v) f f x F
v
f = f ( x ) F = F (v ) =
f ( x)e ivx dx
(3) F = fourier(f,u,v) f f u F
v
f = f (u ) F = F (v) = f (u )e ivu dx
5.29
>> syms x w u v
>> f = sin(x)*exp(-x^2); F1 = fourier(f)
F1 =
-i*pi^(1/2)*sinh(1/2*w)*exp(-1/4*w^2-1/4)
>> g = log(abs(w)); F2 = fourier(g)
F2 =
fourier(log(abs(w)),w,t)
>> h = x*exp(-abs(x)); F3 = fourier(h,u)
F3 =
-4*i/(1+u^2)^2*u
>> syms x real
>> k= cosh(-x^2*abs(v))*sinh(u)/v; F4 = fourier(k,v,u)
144
MATLAB
145
F4 =
sinh(u)*fourier(cosh(x^2*abs(v))/v,v,u)
2
f ( x ) =
1 +
F ( w )eiwx dw MATLAB ifourier( )
2
(1) f = ifourier(F) F F w f
x F = F ( w ) f = f ( x ) F = F ( x ) ifourier(F) t
F = F ( x ) f = f (t)
(2) f = ifourier(F,u) F F w
f u
1 +
f (u ) =
F ( w )eiwu dw
2
(3) f = ifourier(F,v,u) F F v
f u
1 +
f (u ) =
F ( v )eiwu dv
2
5.30
>> syms w v x t
>> syms a real
>> f = sqrt(exp(-w^2/(4*a^2)));
>> IF1 = ifourier(f)
IF1 =
ifourier(exp(-1/4*w^2/a^2)^(1/2),w,x)
>> g = exp(-abs(x));
>> IF2 = ifourier(g)
IF2 =
1/(1+t^2)/pi
>> h = sinh(-abs(w)) -1;
>> IF3 = simple(ifourier(h,t))
IF3 =
-ifourier(sinh(abs(w)),w,t)-Dirac(t)
>> syms w real
>> k = exp(-w^2*abs(v))*sin(v)/v;
>> IF4 = ifourier(k,v,t)
IF4 =
1/2*(atan((t+1)/w^2)-atan((t-1)/w^2))/pi
5.5.2
1
(Laplace)
L (s) =
f ( t )e st dt
145
146
MATLAB
MATLAB laplace()
(1) L=laplace(f) f f t L
s f = f (t ) L = L( s ) f = f ( s ) fourier(F) t
L =L (t)
(2) L=laplace(f,t) f
L t
L (t ) =
f ( x )e tx dx
(3) fourier(F,w,z) f f w L
z
L( z) =
f ( w )e zw dw
5.31
>> syms x s t v
>> f1= sqrt(t);
>> L1 = laplace(f)
L1 =
laplace(exp(-1/8*w^2/a^2),w,s)
>> f2 = 1/sqrt(s);
>> L2 = laplace(f2)
L2 =
(pi/t)^(1/2)
>> f3 = exp(-a*t);
>> L3 = laplace(f3,x)
L3 =
1/(x+a)
>> f4 = 1 - sin(t*v);
>> L4 = laplace(f4,v,x)
L4 =
1/x-t/(x^2+t^2)
2
f ( t ) =
c + i
c i
L ( s )e st dt c L ( s )
s = c
ilaplace()
(1) f = ilaplace(L) L L s
f t L = L ( s ) f = f ( t ) L = L ( t ) ifourier(L) x
f
L = L (t ) f = f ( x)
(2) f = ilaplace(L,y) L L s
f y
f ( y) =
c + i
c i
146
L ( s )e sy ds
MATLAB
147
(3) F = ilaplace(L,y,x) L L y
f x
f ( x) =
c + i
c i
L ( y ) e xy dy
5.32
>> syms a s t u v x
>> f = exp(x/s^2);
>> IL1 = ilaplace(f)
IL1 =
ilaplace(exp(x/s^2),s,t)
>> g = 1/(t-a)^2;
>> IL2 = ilaplace(g)
IL2 =
x*exp(a*x)
>> k = 1/(u^2-a^2);
>> IL3 = ilaplace(k,x)
IL3 =
1/a*sinh(a*x)
>> y = s^3*v/(s^2+v^2);
>> IL4 = ilaplace(y,v,x)
IL4 =
s^3*cos(s*x)
5.5.3
1Z
f (n)
n =0
zn
f Z F ( z ) =
MATLAB ztrans( )
3
(1) F = ztrans(f) f Z f n F
z f = f ( n ) F = F ( z ) f = f ( z ) F w
f = f ( z ) F = F ( w)
(2) F = ztrans(f,w) f Z f n F
w
f n
( )
F ( w) = n
n =0 w
(3) F = ztrans(f,k,w) f Z f k F
w
f k
( )
F ( w) = n
n =0 w
5.33 Z
>> syms a k w x n z
>> f1 = n^4;
>> ZF1 = ztrans(f1)
147
148
MATLAB
ZF1 =
z*(z^3+11*z^2+11*z+1)/(z-1)^5
>> f2 = a^z;
>> ZF2 = ztrans(f2)
ZF2 =
w/a/(w/a-1)
>> f3 = sin(a*n);
>> ZF3 = ztrans(f3,w)
ZF3 =
w*sin(a)/(w^2-2*w*cos(a)+1)
>> f4 = exp(k*n^2)*cos(k*n);
>> ZF4 = ztrans(f4,k,x)
ZF4 =
(x/exp(n^2)-cos(n))*x/exp(n^2)/(x^2/exp(n^2)^2-2*x/exp(n^2)*cos(n)+1)
2Z
1
n 1
Z f ( n ) = 2i v z = R F ( z ) z dz n = 1, 2,3," R
F ( z ) z R
Z
MATLAB Z iztrans( )
(1) f=iztrans(F) F Z F z f
n F = F ( z ) f = f ( n ) F = F ( n ) f k
F = F (n) f = f (k )
(2) f=iztrans(F,k) F Z F z f
k
1
f (k ) =
F ( z )z k 1dz k = 1, 2,3,"
v
=
R
z
2i
(3) f=iztrans(F,w,k) F Z F w f
k
1
f (k ) =
F ( w )wk 1dw k = 1, 2,3,"
2i v w = R
5.34 Z
>> syms a n k x z
>> f1= 2*z/(z^2+2)^2;
>> IZ1 = iztrans(f1)
IZ1 =
-1/8*sum(1/_alpha*(1/_alpha)^n,_alpha =
RootOf(1+2*_Z^2))+1/8*sum(1/_alpha*(1/_alpha)^n,_alpha = RootOf(1+2*_Z^2))*n
>> f2 = n/(n+1);
>> IZ2 = iztrans(f2)
IZ2 =
(-1)^k
>> f3 = z/sqrt(z-a);
>> IZ3 = iztrans(f3,k)
148
MATLAB
149
IZ3 =
iztrans(z/(z-a)^(1/2),z,k)
>> f4 = exp(z)/(x^2-2*x*exp(z));
>> IZ4 = iztrans(f4,x,k)
IZ4 =
1/4*(-charfcn[0](k)-2*charfcn[1](k)*exp(z)+2^k*exp(z)^k)/exp(z)
5.6
MATLAB
() solve( )() dsolve( )
5.6.1
MATLAB solve( )
x+y+z=10
3x+2y+z=14
2x+3y-z=1
>>
>>
>>
>>
g=
L1='x+y+z=10';
L2='3*x+2*y+z=14';
L3='2*x+3*y-z=1';
g=solve(L1,L2,L3)
%L1L2L3 3
x: [1x1 sym]
y: [1x1 sym]
z: [1x1 sym]
>> g.x
ans =
1
>> g.y
ans =
149
MATLAB
150
2
>> g.z
ans =
7
5.36
x1cos(sita)-x2sin(sita)=a
x1sin(sita)+x2cos(sita)=b
>>
>>
>>
>>
syms x1 x2 a b sita;
L1=x1 * cos(sita)-x2 * sin(sita)-a;
L2=x1 * sin(sita)+x2 * cos(sita)-b;
[x1,x2]=solve(L1,L2,x1,x2)
%L1L2
%
x1 =
cos(sita)*a+b*sin(sita)
x2 =
-a*sin(sita)+cos(sita)*b
()
5.6.2
solve( )
5.6.1
(1) g=solve(eq) eq
solve(eq) g
g
(2) g=solve(eq,var) eq var
eq(var)=0
5.37 ax 2 + bx + c = 0
>> f=sym('a*x^2+b*x+c=0');
>> xf=solve(f)
xf =
[1/2/a*(-b+(b^2-4*a*c)^(1/2))]
[1/2/a*(-b-(b^2-4*a*c)^(1/2))]
5.38 yz
uy2+vz+w=0
y+z+w=0
>> syms y z u v w;
>> eq1=u*y^2+v*z+w;
>> eq2=y+z+w;
>> [y z]=solve(eq1,eq2, y, z)
y =
[-1/2/u*(-2*u*w-v+(4*u*w*v+v^2-4*u*w)^(1/2))-w]
[-1/2/u*(-2*u*w-v-(4*u*w*v+v^2-4*u*w)^(1/2))-w]
z =
150
MATLAB
151
[1/2/u*(-2*u*w-v+(4*u*w*v+v^2-4*u*w)^(1/2))]
[1/2/u*(-2*u*w-v-(4*u*w*v+v^2-4*u*w)^(1/2))]
5.39
a+b+ x= y
2ax by = 1
2
( a + b) = x + y
ay + bx = 4
>>
>>
>>
>>
>>
>>
e1=sym('a+b+x=y');
e2=sym('2*a*x-b*y=-1');
e3=sym('(a+b)^2=x+y');
e4=sym('a*y+b*x=4');
[a,b,x,y]=solve(e1,e2,e3,e4);
%
a=double(a), b=double(b), x=double(x), y=double(y) %
%
a =
1.0000
23.6037
0.2537 - 0.4247i
0.2537 + 0.4247i
b =
1.0000
-23.4337
-1.0054 - 1.4075i
-1.0054 + 1.4075i
x =
1.0000
-0.0705
-1.0203 + 2.2934i
-1.0203 - 2.2934i
y =
3.0000
0.0994
-1.7719 + 0.4611i
-1.7719 - 0.4611i
4
solve( )
5.40
xx = 4
xy + y = 1
>> [x,y]=solve('x^x=2','x*y+y=1')
x=
log(2)/lambertw(log(2))
y=
lambertw(log(2))/(log(2)+lambertw(log(2)))
151
MATLAB
152
()
5.6.3
MATLAB dsolve()()
()
dsolve y ''+ 2 y ' = x
MATLAB dsolve
(1) r = dsolve('equ')
(2) r = dsolve('equ', 'v')
(3) r = dsolve('equ,'cond1,cond2,','v')
(4) r = dsolve('equ1,equ2,','v')
(5) r = dsolve('equ1,equ2,','cond1,cond2,','v')
equ1,equ2,()
v () t
cond1,cond2,()()
y(a)=bDy(c)=dD2y(e)=f
y ( x) x = a = b y ( x) x = c = d
y ( x) x = e = f
r ()() r
C1C2
() equ D ( x)
D=d/dx D2=d2/d2x D
dsolve 12 (
12 )
sym
ode23 ode45
5.41
dy
= ax
dx
>> y = dsolve('Dy+a*x=0', 'x')
y =
-1/2*a*x^2+C1
C1
>> y = dsolve('Dy+a*x=0') % t
y =
-a*x*t+C1
152
MATLAB
153
5.42
d2 y
+ 2x = 2 y
dx 2
>> y = dsolve('D2y+2*x=2*y', 'x')
y =
exp(2^(1/2)*x)*C2+exp(-2^(1/2)*x)*C1+x
5.43
y ( 2 ) = 5 y ' (1) = 2
d2 y
+ 2x = 2 y
dx 2
5.44
f '' = f + 3g + sin x
f ' ( 2 ) = 0 f ' ( 3) = 3 g ( 5 ) = 1
(1)
>> [g_f,g_g]=dsolve('D2f=f+3*g+sin(x)','Dg=Df+4+cos(x)','x')
g_f =
1/2*exp(2*x)*C2-1/2*exp(-2*x)*C1-4/5*sin(x)-3*x+C3
g_g =
1/2*exp(2*x)*C2-1/2*exp(-2*x)*C1+1/5*sin(x)+x-1/3*C3
(2)
>> [f,g]=dsolve('D2f=f+3*g+sin(x),Dg=Df+4+cos(x)','Df(2)=0,Df(3)=3,g(5)
=1','x')
f =
1/10*exp(2*x)*(8*cos(1)^2*exp(4)+11*exp(4)-16*cos(1)^3*exp(6)+12*cos(1)
*exp(6)-30*exp(6))/(-exp(12)+exp(8))+1/10*exp(-2*x)*(-16*exp(8)*cos(1)^3*exp
(6)+12*exp(8)*cos(1)*exp(6)-30*exp(8)*exp(6)+8*cos(1)^2*exp(4)*exp(12)+11*ex
p(4)*exp(12))/(-exp(12)+exp(8))-4/5*sin(x)-3*x+3/10*(8*cosh(10)*cos(1)^2*exp
(4)+11*cosh(10)*exp(4)-16*cosh(10)*cos(1)^3*exp(6)+12*cosh(10)*cos(1)*exp(6)
-30*cosh(10)*exp(6)+8*sinh(10)*cos(1)^2*exp(4)+11*sinh(10)*exp(4)-16*sinh(10
)*cos(1)^3*exp(6)+12*sinh(10)*cos(1)*exp(6)-30*sinh(10)*exp(6)-16*cosh(10)*e
xp(8)*cos(1)^3*exp(6)+12*cosh(10)*exp(8)*cos(1)*exp(6)-30*cosh(10)*exp(8)*ex
p(6)+8*cosh(10)*cos(1)^2*exp(4)*exp(12)+11*cosh(10)*exp(4)*exp(12)+16*sinh(1
0)*exp(8)*cos(1)^3*exp(6)-12*sinh(10)*exp(8)*cos(1)*exp(6)+30*sinh(10)*exp(8
)*exp(6)-8*sinh(10)*cos(1)^2*exp(4)*exp(12)-11*sinh(10)*exp(4)*exp(12)-2*sin
(5)*exp(12)+2*sin(5)*exp(8)-40*exp(12)+40*exp(8))/(-exp(12)+exp(8))
g =
1/10*exp(2*x)*(8*cos(1)^2*exp(4)+11*exp(4)-16*cos(1)^3*exp(6)+12*cos(1)
*exp(6)-30*exp(6))/(-exp(12)+exp(8))+1/10*exp(-2*x)*(-16*exp(8)*cos(1)^3*exp
153
MATLAB
154
(6)+12*exp(8)*cos(1)*exp(6)-30*exp(8)*exp(6)+8*cos(1)^2*exp(4)*exp(12)+11*ex
p(4)*exp(12))/(-exp(12)+exp(8))+1/5*sin(x)+x-1/10*(8*cosh(10)*cos(1)^2*exp(4
)+11*cosh(10)*exp(4)-16*cosh(10)*cos(1)^3*exp(6)+12*cosh(10)*cos(1)*exp(6)-3
0*cosh(10)*exp(6)+8*sinh(10)*cos(1)^2*exp(4)+11*sinh(10)*exp(4)-16*sinh(10)*
cos(1)^3*exp(6)+12*sinh(10)*cos(1)*exp(6)-30*sinh(10)*exp(6)-16*cosh(10)*exp
(8)*cos(1)^3*exp(6)+12*cosh(10)*exp(8)*cos(1)*exp(6)-30*cosh(10)*exp(8)*exp(
6)+8*cosh(10)*cos(1)^2*exp(4)*exp(12)+11*cosh(10)*exp(4)*exp(12)+16*sinh(10)
*exp(8)*cos(1)^3*exp(6)-12*sinh(10)*exp(8)*cos(1)*exp(6)+30*sinh(10)*exp(8)*
exp(6)-8*sinh(10)*cos(1)^2*exp(4)*exp(12)-11*sinh(10)*exp(4)*exp(12)-2*sin(5
)*exp(12)+2*sin(5)*exp(8)-40*exp(12)+40*exp(8))/(-exp(12)+exp(8))
5.7
MATLAB MATLAB
() MATLAB
MATLAB MATLAB
MAPLEMaths MAPLE MATLAB
5.8
1 3
(1) f=x^3-6*x^2+11*x-6
(2) g=(x-1)*(x-2)*(x-3)
(3) h=-6+(11+(-6+x)*x)*x
f h g
2 2
e e5 e9
sin1 sin 2 sin 3
2
e e6 e10
A = sin 4 sin 5 sin 6
B = 3 7 11
e e e
sin 7 sin 8 sin 9
4 8 12
e e e
3 pretty f
( x + y ) ( a + bc )
=
2
( x + a)
g =
x ( a + bc )
( x + a)
y ( a + bc )
( x + a)
MATLAB
7
4 f = ( ax 2 + bx + c 3) a ( cx5 + 4bx 1) 18b ( 2 + 5 x ) a + c x
a f
1
3
5 f = x 2 y + xy x 2 2 x g = xe2 x + e2 x f x
4
16
3
154
MATLAB
155
g e 2 x
6
(1) f = x 3 3x 2 3x + 1
(2) g = x3 7 x + 6
7
(1) f1 = x ( x ( x 2 ) + 5 ) 1
(2) f 2 = ln ( xy ) + ln z
(3) f 3 = 3 xe x e y + z
(4) f 4 = sin 2 x + cos 2 x 1
8
(1) f1 = cos x + sin 2 x
(2) f 2 = x3 + 3 x 2 + 3x + 1
9 1 3
10 f ( x ) = ax 2 + bx + c g ( x ) = e x + x sin x
11
log( x)
(2) f ( x ) = cos ( 2 x ) sin ( 2 x )
(1) f ( x) = x2
( , )
e
ax bx 2
(3) F ( x ) = 3
abcds
ds
cx
12
6
0
sin t
( sin x + 2 )dx 03 x y dy 2
13
4txdx
+
1
1
dx
dx
2
2
1
x
x +1
sin x
g ( x ) = lim sin ( x + 2 y )
y 0
x
2
y
15 sin x 2 + (100) 3
z
14 f ( x ) = lim
x 0
16 f ( x ) = x 2 + x
17 f ( x ) = e x
2
18 F ( t ) = sin ( xt + 2t ) v
1
s +1
20 f ( n ) = 2n g ( n ) = sin ( kn ) h ( n ) = cos ( kn ) Z
19 L ( s ) =
21 F ( z ) =
n ( n + 1)
z
G (n) = 2
Z
z2
n + 2n + 1
22
155
MATLAB
156
x1 + 2 x2 = 2
2x + x + x = 3
2
3
(1) 1
+
+
4
5
7
x
x
x3 = 0
2
1
x1 + x2 + 5 x3 = 5
x1 2 x2 + 3 x3 4 x4 = 4
(3) x2 x3 + x4 = 3
x x + 2 x = 4
1
2
4
x1 + x2 + 3x3 x4 = 2
x x + x =1
3
4
(2) 2
+
+
2
x
x
x
2
3 + 2 x4 = 4
1
x1 x2 + x3 x4 = 0
x1 4 x2 + 2 x3 = 0
(4) 2 x2 x3 = 0
x + 2 x x = 0
2
3
1
23 f ( x ) = sin x + tan x + 1 = 0
1 3
24 X ' =
X
3 4
25
(1) y ' = ( x + y )( x y )
(2) xy ' = y ln ( y / x )
(3) y ' = x sin x / cos y
y (10 ) = 1
y ( 2) = 1
26
d2 y
= cos ( 2 x ) y
dx 2
27
(1) f ' = f + 3 g , g ' = f + 4
dy
(0) = 0
dx
y ( 0) = 1
156
MATLAB
MATLAB MATLAB
M MATLAB
MATLAB
MATLAB
M MATLAB
5 M
MATLAB
. m MATLAB ( M ) MATLAB
M
M MATLAB
6.1
MATLAB
6.1.1
MATLAB
global
global A B C
ABC 3
M
MATLAB
MATLAB
158
6.1.2
MATLAB MATLAB
MATLAB .m
M ASCII M MATLAB
MATLAB M
(1)
MATLAB
edit edit filename filename
M Yes filename
M
MATLAB File|New M-file
(2)
MATLAB (Open)
M
edit filename filename M
MATLAB File|Open (Open)
M
(3)
M MATLAB
/ 6.1
6.1
(Editor/Debug)
M MATLAB /
M
158
MATLAB
159
( Word)(Edit)
MATLAB
(4)
M
M (Save file
as)( M )
M
File|Save
File|Save As M File|Save
M (Save file as)
6.1.3
M
MATLAB
clear
6.1
%average_1.m
x=input('x=');
[m,n] =size(x);
if~((m==1)|(n==1))| ((m==1)& (n==1))
error('')
end
average=sum(x)/length(x)
%
%
% x
average_1.m[1 2 3]
x=[1 2 3]
average =
2
159
MATLAB
160
[1;2;3]
x=[1;2;3]
average =
2
[1 2 3; 4 5 6]
x=[1 2 3; 4 5 6]
??? Error using ==> average_1
1.7
Debug|Save and Run F5
6.2
MATLAB
6.2
6.1.4
M function
MATLAB
6.2 average_2( )
function y=average_2(x)
[m,n]=size(x);
if~((m==1)|(n==1))| ((m==1)& (n==1))
error('')
end
y=sum(x)/length(x);
160
%
%
%
%
%
%
%
average_2(x)
x y
% x
MATLAB
161
average_2.m() average_2
MATLAB
MATLAB 199
>> z=1:99;
>> average_2(z)
ans =
50
6.1 6.2
(1) function
MATLAB
function [x,y,z]=sphere(theta,phi,rho)
function printresults(x)
(2) H1 H1
lookfor
MATLAB M H1
average
lookfor average
>> lookfor average_2
average_2.m: % average_2(x)
(3)
help
>> help average_2
average_2(x)
x y
(4)
/
(5) %MATLAB
%
[m,n]=size(x);
6.1.5
[]=()
161
162
MATLAB
(1)
>> [x,y]=sin(pi)
??? Error using ==> sin
Too many output arguments.
>> y=linspace(2)
??? Input argument "n" is undefined.
Error in ==> linspace at 21
(2) narginnargout
nargin
nargout nargin
nargout function
162
MATLAB
163
(3) [m,n]=size(x)
[m,n] m n[m,n]
y=[m,n] y m n
(4)
function t=toc
% TOC Read the stopwatch timer.
% TOC, by itself, prints the elapsed time (in seconds) since TIC was used.
% t = TOC; saves the elapsed time in t, instead of printing it out.
% See also TIC, ETIME, CLOCK, CPUTIME.
%
Copyright(c)1984-94byTheMathWorks,Inc.
% TOC uses ETIME and the value of CLOCK saved by TIC.
Global TICTOC
If nargout<1
elapsed_time=etime(clock,TICTOC)
else
t=etime(clock,TICTOC);
end
toc t
>> tic
>> toc
elapsed_time =
4.0160
elapsed_time MATLAB
t t
toc t
>> tic
>> out=toc
out =
2.8140
MATLAB
164
MATLAB
(7) MATLAB
MATLAB
(8)
(11) return
return
6.2
MATLAB
MATLAB
6.2.1
1. for
for for
for = 1: 2: 3
end
164
MATLAB
165
1 2
1 2
3 for
for
6.3 s =
10
n
n =1
s=0;
for n=1:10
s=s+n;
end
s
s =
55
ans =
-2.0000
-1.7500
-1.5000
-1.2500
-1.0000
-0.7500
0.5000
0.4286
0.3333
0.2000
0
-0.3333
for
(1) for
x=0;
for n=1:4
x =x+1
n=5;
end
165
MATLAB
166
x =
1
x =
2
x =
3
x =
4
n
(2) for = [ 1: 2: 3]
[1:2:10]=[ 0 2 4 6 8 10]
for =
end
n =
-2
n =
5
n =
3
n =
6
n =
-2
k =
5
s =
10
5 i a(i) a
6.6
data=[3 9 45 6; 7 16
k=0;
for n=data
166
-1 5]
MATLAB
167
n
%
k=k+1;
%
x=n(1)-n(2)
end
k
%
data =
3
7
n =
3
7
x =
-4
n =
9
16
x =
-7
n =
45
-1
x =
46
n =
6
5
x =
1
k =
4
9
16
45
-1
6
5
4 i n(i,:)
6.7
data(:,:,1)=[3 9 45 6; 7 16 -1 5];
data(:,:,2)=[1 2 3 4; 8 7 6 5];
data
% data
k=0;
for n=data
n
%
k=k+1;
%
x=n(1)-n(2)
end
k
%
6.4 6.6 m1 m2 mn x
for m2 mn i n x(i, j,,k)j
167
MATLAB
168
k 2~n 1m2,,1mn
(3) for
6.8 for 1+2++10!
s=0;
for i=1:10
p=1;
for j=1:i
p=p*j;
end
s=s+p;
end
s
s =
4037913
(4) for
6.9
(a) for n=1:10
x(n)=sin(n*pi/10);
end
x
(b) n=1:10;
x=sin(n*pi/10)
x =
0.3090
0.0000
0.5878
0.8090
0.9511
1.0000
0.9511
0.8090
0.5878
0.3090
% x
2. while
for while
while
end
while end
168
MATLAB
169
6.10
num=0; EPS=1;
while (1+EPS)>1
EPS=EPS/2;
num=num+1;
end
num=num-1
EPS=2*EPS
num =
52
EPS =
2.2204e-016
n>t?
No
Yes
Yes
?
No
(a) for
(b) while
6.2.2
MATLAB
170
1. if
if
1
else
2
end
if
1 2
else 2
if for while
6.11 for EPS
EPS=1;
for n=1:100
EPS=EPS/2;
if (1+EPS)<=1
EPS=EPS*2
break
end
end
num = n-1
EPS =
2.2204e-016
num =
52
#
if n
n
else
n+1
end
end
end
170
MATLAB
171
if 1
1
elseif 2
2
#
elseif n
n
else
n+1
end
6.12 for if
5 1 0 0
1 5 1 0
A = 0 1 5 1
0 0 1 5
0 0 0 1
0
0
0
1
5
A=[ ];
for k=1:5
for j=1:5
if k==j
A(k,k)=5;
elseif abs(k-j)==1
A(k,j)=1;
else
A(k,j)=0;
end
end
end
A
A =
5
1
0
0
0
1
5
1
0
0
0
1
5
1
0
0
0
1
5
1
0
0
0
1
5
2. switch
switch
switch
case 1
1
case 2
2
171
MATLAB
172
... ...
case n
n
otherwise
n+1
end
1 1 2
2... n n
case n+1
end
6.13 switch score
(1) score 90 (2) 90score 80 (3) 80score70
(4) 70score 60 (5) 60score
score=input('score=');
switch fix(score/10)
case 9
grade=''
case 8
grade=''
case 7
grade=''
case 6
grade=''
otherwise
grade=''
end
score=87
grade =
3. try
try MATLAB
try
1
catch
2
end
1 lasterr
2 end
172
MATLAB
173
try
picture=imread('pic.bmp','bmp');
filename='pic.bmp';
catch
picture=imread('pic.jpg','jpg');
filename='pic.jpg';
end
filename
filename =
pic.jpg
lasterr
ans =
Error using ==> imread
File "pic.bmp" does not exist.
6.2.3
breakreturn
1. break
for while end
2. return
3. pause
(1) pause
(2) pause(n) n
(3) pause on/off/
4. continue
for while
6.3
MATLAB
6.3.1
(input)
(1) x = input('prompt')'prompt' x
173
MATLAB
174
(2) x = input('prompt','s')'prompt' x
6.3.2
(disp)
disp disp(x)
6.3.3
M /(save / load)
1. save
Z filename
(3) load filename X Y Z filename XY
XYZ
6.3.4
/(fprintf / fscanf)
1fprintf
count = fprintf(fid,format,A,...) format
fopen ( fid) count
2fscanf
/(fwrite/ fread)
1fwrite
count = fwrite(fid,A,precision) precision A
fid count
174
MATLAB
175
2fread
[A,count] = fread(fid,size,precision) fid
precision A count
6.3.6
/(fgetl / fgets)
1fgetl
tline = fgetl(fid) fid
2. fgets
6.4
MATLAB
6.3 6-1
MATLAB
6-1
fopen
fclose
all
fread
fwrite
fscanf
C fscanf
fprintf
C fprintf
fgetl
fgets
ferror
feof
fseek
ftell
frewind
tempdir
tempname
175
MATLAB
176
1fopen
permission
'r'
'w'
'a'
'r+'
/
/
'w+'
'a+'
'A'
'W'
2. fclose
MATLAB
177
fid=fopen(s,'r');count=0;
% m
while ~feof(fid);
line=fgetl(fid);
%
if isempty(line)||strncmp(deblanks(line),'%',1);
%
continue;
%
end
count=count+1;
%
end
y=count;
function st=deblanks(s);
%
st=fliplr(deblank(fliplr(deblank(s))));
lenm.m
>> sfile='lenm';
>> y = lenm(sfile)
y =
17
6.5
(Object-Oriented ProgrammingOOP)(Object)
(Class)(Encapsulation)(Inherit)(Polymorphism)(Message)
MATLAB
6.5.1
MATLAB
1.
(1) @
(2) @
(3) MATLAB MATLAB
curve c:\my_classes
c:\my_classes\@curve addpath MATLAB
addpath c:\my_classes;
2.
MATLAB
(Methods)
177
MATLAB
178
3.
MATLAB
MATLAB MATLAB 6-3
6-3 MATLAB
class constructor
display
set / get
subsref / subsasgn
end
A(1:end)
subsindex
converters
MATLAB doublechar
(1) MATLAB
(class constructor)
@
(class tag)
(2) MATLAB display
(overloaded)
MATLAB
(3) 5
char
4.
(
+)
178
MATLAB
179
5.
MATLAB 6-4
6-4
class(object )
class(object,class, parent1, parent2,...)
object
object class
parent1,parent2,...
isa(object,class)
object class 1 0
isobject(x)
x 1 0
superiorto(class1, class2,...)
inferiorto(class1,class2,...)
methods class
6.5.2
superiorto
inferiorto
class
6.16 curve
(1) @curve c:\my_classes c:\my_classes\@curve
addpath MATLAB
addpath c:\my_classes;
(2)
function c=curve(a)
%curve
%c = curve curve
% a 12 a{1}a{2}
% fplot
y = f(x)
fplot
% x
%
if nargin==0
c.fcn='';
c.descr='';
c=class(c,'curve');
% curve
elseif isa(a, 'curve')
c=a;
% curve
elseif (ischar(a{1}) & ischar(a{2}))
c.fcn=a{1};
c.descr=a{2};
c=class(c, 'curve');
% curve
else
disp('Curve class error #1: Invalid argument.')
%
end
179
180
MATLAB
(3) plot1
curve plot
function p=plot1(c, limits)
% curve.plot1 limits curve
% limits x ([xmin xmax]),
% xy ([xmin xmax ymin ymax]).
step=(limits(2)-limits(1))/40;
x=limits(1):step:limits(2);
%
fplot(c.fcn,limits);
title(c.descr);
6.4
6.4
plot 1
(4)
curve @curve M
function ctot=plus(c1,c2)
% c1 c2
fcn = strcat(c1.fcn,'+',c2.fcn);
description=strcat(c1.descr,'plus',c2.descr);
ctot=curve({fcn description});
MATLAB
181
6.5
6.5
plus
(5)
curve @curve display
MATLAB
6.6
MATLAB
MATLAB
(1) MATLAB
double repmat 8
A Arepmat (int8(0),5000,5000) A=int8zeros(5000,5000)) 25
0 8
zeros(5000,5000)) double
(2)
MATLAB
MATLAB
182
6.7
MATLAB
M
db 6-5
6-5
dbstop in fname
M fname
dbstop at r in fname
M fname r
dbstop if v
v v error
NaN inf naninf/infnan
dstop if warning
dbclear at r in fname
fname r
fname
dbclear all
dbclear in fname
fname
dbclear if v
v dbstop if v
dbstatus fname
fname
Mdbstatus
dbstatus
dbstep
dbstep n
dbstep in
dbcont
dbquit
182
MATLAB
183
MATLAB
K> > MATLAB
6.8
MATLAB
MATLAB M
6.9
1. M MATLAB
2. M M
3. project1.m
4.
5.
(1) for i=-1000:1000
(2) for j=1:2:20
6. var
(1) var=1;
while mod(var,10)~=0
var=var+1
end
(2) var=2;
while var<=100
var=var^2;
end
(3) var=3;
while var>100
var=var^2;
end
7. A ASCII
A 6-6
183
MATLAB
184
6-6
1
1
4
2
5
3
6
184
MATLAB
MATLAB
7.1
MATLAB
MATLAB
MATLAB
7.1
7.1 MATLAB
MATLAB
3
7 MATLAB
MATLAB
186
MATLAB
7.1.1
MATLAB
1
MATLAB
MATLAB
MATLAB
MATLAB
()
MATLAB figure
figure
7.2
7.2 MATLAB
figure
(1) figure('PropertyName',PropertyValue,...)
PropertyName PropertyValue
(2) figure(h) h h
h
(3) h = figure(...) figure
186
MATLAB
187
2.
close
(1) close close(gcf)
(2) close(h) h
(3) close name name
(4) close all
(5) close all hidden
(6) status = close(...) close 1 0
(1) clf
(2) clf reset
(UnitsPaperPosition PaperUnits )
7.1.2
MATLAB
(1) plotx y
(2) loglogx y
(3) semilogxx y
(4) semilogyx y
(5) plotyy:
plot
plot(Y) Y Y
Y plot(real(Y),imag(Y)) Y
Y Y
plot(X,Y) XY X Y
X Y Y X
Y XY X Y
plot(X1,Y1,X2,Y2,Xn,Yn) Xi Yi(i=1,2,...,n)
% x 0 5
% y
%
187
188
MATLAB
7.3
7.3
7.2 plot
x=0:0.05:5;
y1=0.2*x-0.8;
y2=sin(x.^2);
figure
plot(x,y1,x,y2);
%
%
%
%
%
x 0 5
y1
y2
7.4
7.4
7.3 plot
x=0:pi/180:2*pi;
y1=sin(x);
y2=sin(2*x);
y3=sin(3*x);
X=[x; x; x]';
Y=[y1; y2; y3]';
plot(X ,Y,x,cos(x))
x~cos(x)
%
%
%
%
%
%
%
x
y1
y2
y3
X
Y
4
:x~sin(x),x~sin(2x),x~sin(3x)
7.5
loglog semilogx semilogy plot
plotyy
plotyy(x1,y1,x2,y2)
188
MATLAB
7.5
189
7.3
x1y1 x2y2
x1y1 x2y2
7.4 y1 = e 0.3 x cos(2 x) y 2 = 10e 1.5 x
x=0:pi/180:2*pi;
y1=exp(-0.3*x).*cos(2*x);y2=10*exp(-1.5*x);
plotyy(x,y1,x,y2)
7.6
7.6
7.1.3
MATLAB
7-1
189
MATLAB
190
7-1
bar(x,y)
x y
fplot(y,[a b])
y [a b]
polar(,r)
stairs(x,y)
x y
stem(x,y)
x y
fill(x,y,b)
x y , b
scatter(x,y,s,c)
s c
pie(x)
7.5
x = -2.9:0.2:2.9;
bar(x,exp(-x.*x));
7.7
7.7
7.6
t=0:.01:2*pi;
figure
polar(t,abs(cos(2*t)));
%.
7.8
7.8
190
MATLAB
191
7.7
x = 0:0.1:4;
y = (x.^0.8).*exp(-x);
stem(x,y)
7.9
7.9
7.8
x=0:0.25:10;
figure
stairs(x,sin(2*x)+sin(x));
7.10
7.9
x=[43,78,88,43,21];
pie(x)
7.11
7.10
7.1.4
7.11
MATLAB
191
MATLAB
192
plot
() 7-2
7-2 plot
-.
--
'square' s
'diamond' d
<
>
7.10
x=0:0.2:8;
y1=0.2+sin(-2*x);
% y1
y2=sin(x.^0.5);
% y2
figure
plot(x,y1,'g-+',x,y2,'r--d');
% y1 y2
%
7.12 ()
7.12
192
7
7.1.5
MATLAB
193
MATLAB
7.10 x 08 7.12 x 08
MATLAB axis
axis([xmin,xmax,ymin,ymax])
xmin,xmax,ymin,ymax x y
7.10 axis([-0.55-0.51.3]) 7.13
7.13
MATLAB ( 7-3)
hold on/off
7-3
axis on/off
xlabel('option')
x option
ylabel('option')
title('option')
legend('option')
grid on/off
box on/off
7.11
x=0:0.05:5 ;
figure
y1=exp(0.4.^x)-1.5;y2=sin(x*4);
plot(x,y1,x,y2,'r-.')
% y2
193
MATLAB
194
line([0,5],[0,0])
%(0,0)(5,0),
xlabel('Input');ylabel('Output');
%x Input, y Output
title('Two Function');
%Two Function
legend('y1=exp(0.4.^x)-1.5','y2=sin(x*4)')
%
grid on
%
7.14
7.14
7.1.6
subplot
(1) subplot(m,n,p) mn p
7.15 p
subplot(2,3,1)
subplot(2,3,2)
subplot(2,3,3)
subplot(2,3,4)
subplot(2,3,5)
subplot(2,3,6)
7.15
194
MATLAB
195
(2) subplot(m,n,p,'replace')(1)
(5) h = subplot(...)
subplot
plot
7.12
x=linspace(0,2*pi,100);
%x 0~2 100
subplot(2,2,1);plot(x,sin(x));
% sin(x),
xlabel('x');ylabel('y'); title('sin(x)') %x x,y y,
%sin(x)
subplot(2,2,2);plot(x,cos(x));
xlabel('x');ylabel('y'); title('cos(x)');
subplot(2,2,3);plot(x,exp(x));
xlabel('x');ylabel('y'); title('exp(x)');
subplot(2,2,4);plot(x,exp(-x));
xlabel('x');ylabel('y'); title('exp(-x)');
7.16
7.16
7.2
MATLAB
MATLAB
195
MATLAB
196
7.2.1
plot3
(1) plot3(X1,Y1,Z1,...)X1Y1Z1
X1,Y1,Z1,... Xn,Yn,Zn
7.17
7.13 subplot title
xlabel grid text(x,y,z,string)
xyz
7.2.2
1 surfsurfc
MATLAB
197
7.17 3
7.18
(2) surf(X,Y,Z) Z XY
XY XY m= size(X)n =
size(Y)[m,n] = size(Z)
(3) surf(X,Y,Z,C) Z C XY
(4) surf(...,'PropertyName',PropertyValue)
(5) surfc(...) surfc surf
(6) h = surf(...) surf h
(7) h = surfc(...) surfc h
7.14
figure
[X,Y,Z]=sphere(30);
%
surf (X,Y,Z);
%
xlabel('x'),ylabel('y'),zlabel('z');
title('SURF OF SPHERE');
197
198
MATLAB
7.19
7.19
shading
(1) shading faceted
(2) shading flat
(3) shading interp
7.20
7.15
[x,y] = meshgrid(-3:1/4:3);
z = peaks(x,y);
surfc(x,y,z);
198
% 0.25
MATLAB
199
7.21
7.21
7.16 surfl
[x,y] = meshgrid(-3:1/8:3);
z = peaks(x,y);
surfl(x,y,z);
shading interp
colormap(gray);
axis([-3 3 -3 3 -8 8]);
% 0.125
7.22
7.22
2
(1) sphere
[x,y,z]=sphere(n)
199
MATLAB
200
(n+1)(n+1) x,y,z 3 1 n
20
(2) cylinder
[x,y,z]=cylinder(R,n)
R n n
20
(3) peaks
2
2
2
2
2
2
x
1
3 x, y 3
f ( x, y ) = 3(1 x 2 )e x ( y +1) 10( x3 y 5 )e x y e ( x +1) y
5
3
z=peaks(n)
nn zn 48 z=peaks(x,y) x,y
z
7.17
t=0:pi/20:2*pi;
[x,y,z]=sphere;
subplot(1,3,1);
surf(x,y,z);xlabel('x'),ylabel('y'),zlabel('z');
title('')
[x,y,z]=cylinder(2+sin(2*t),30);
subplot(1,3,2);
surf(x,y,z);xlabel('x'),ylabel('y'),zlabel('z');
title('')
[x,y,z]=peaks(20);
subplot(1,3,3);
surf(x,y,z);xlabel('x'),ylabel('y'),zlabel('z');
title('');
7.23 R 2+sin(2*t)
7.23
7.2.3
MATLAB
201
-y
7.24
MATLAB view
(1) view(az,el) view([az,el]) az el
(2) view([x,y,z])(x,y,z)
(3) view(2)az=0el=90
(4) view(3)az=-37.5,el=30
(5) view(T) T T viewmtx 44
(6) [az,el] = view
(7) T = view 44 T
7.18
x=-4:4;y=-4:4;
[X,Y]=meshgrid(x,y);
Z=X.^2+Y.^2;
subplot(2,2,1)
surf(X,Y,Z);
%
ylabel('y'),xlabel('x'),zlabel('z');title('(a) ')
subplot(2,2,2)
surf(X,Y,Z);
%
ylabel('y'),xlabel('x'),zlabel('z');title('(b) 55-37.5')
view(-37.5,55)
% 55-37.5
subplot(2,2,3)
surf(X,Y,Z);
%
ylabel('y'),xlabel('x'),zlabel('z');title('(c) (2,1,1)')
view([2,1,1])
%(2,1,1)
subplot(2,2,4)
surf(X,Y,Z);
%
ylabel('y'),xlabel('x'),zlabel('z');title('(d) 90 10')
view(10,90)
% 90 10
7.25
201
MATLAB
202
(a)
(b) 55-37.5
(c) (2,1,1)
(d) 90 10
7.25
7.2.4
mesh (X,Y,Z)
meshc (X,Y,Z)
meshz (X,Y,Z)
waterfall (X,Y,Z)
quiver (X,Y,DX,DY)
clabel (cs)
7.19
[X,Y,Z]=peaks(20);
figure
subplot(2,2,1);mesh(X,Y,Z);title('(a) mesh of peaks');
subplot(2,2,2);surf(X,Y,Z);title('(b) surf of peaks');
subplot(2,2,3);meshc(X,Y,Z);title('(c) meshc of peaks');
subplot(2,2,4);meshz(X,Y,Z);title('(d) meshz of peaks');
7.26
202
MATLAB
(a)
(c)
203
(b)
(d)
7.26
7.20 quiver
[X,Y] = meshgrid(-2:.2:2);
Z = X.*exp(-X.^2 - Y.^2);
[DX,DY] = gradient(Z,.2,.2);
contour(X,Y,Z)
hold on
quiver(X,Y,DX,DY)
colormap hsv
grid off
hold off
7.27
7.27 quiver
203
204
MATLAB
7.21 waterfall
[X,Y,Z]=peaks(30);
figure
waterfall (X,Y,Z);title('waterfall of peaks');
7.28
7.22 clabe
[X,Y,Z]=peaks(30);
[C,h] = contour(X,Y,Z);
clabel(C,h);
7.29
(1) bar3(x,y) x y x y
7.28 waterfall
7.29 clabe
7.23
(1)
(2) z=cos(x) ;
(3) x={45,76,89,222,97}
(4)
subplot(2,2,1);bar3(magic(5));
x=0:pi/10:2*pi;y=x;z=cos(x);
subplot(2,2,2);stem3(x,y,z);
view([2,1,1]) ;
%
subplot(2,2,3);pie3([45,76,89,222,97])
subplot(2,2,4);fill3(rand(6,1),rand(6,1),rand(6,1),'k')
204
MATLAB
205
7.30
7.30 7.23
7.3
MATLAB
MATLAB
MATLAB (double) 64
MATLAB (unit8)
MATLAB unit8 double
7.3.1
1.
MATLAB 4
(RGB)
1)
m3
(RGB)RGB [01]0
1
2)
206
MATLAB
3)
0 1
unit8 unit8
unit8
4) (RGB)
RGB 3 (RGB)
mn3mn
2.
MATLAB imshow
(1) imshow (I,n) n n 256 64
I = imread('peppers.png');
%
subplot(2,2,1);subimage(I);title('(a) RGB ')
% 1
[X,map] = rgb2ind(I,1000);
%
subplot(2,2,2);subimage(X,map);title('(b) ')% 2
X = rgb2gray(I);
%
subplot(2,2,3);subimage(X);title('(c) ')
% 3
X= im2bw(I,0.6);
%
subplot(2,2,4);subimage(X);title('(d) ')
% 4
7.31
206
(a) RGB
(c)
MATLAB
207
(b)
(d)
7.31
7.25
I = imread('peppers.png');
%
iptsetpref('ImshowBorder', 'tight'); %('loose')
iptsetpref('ImshowAxesVisible','off');%('on')
imshow(I);
7.32
7.32
7.3.2
BMP(Windows )HDF(
)JPEG()PCX(Windows )TIF(
)XWD(X Windows Dump )
imread
207
MATLAB
208
(1) A = imread(filename,fmt) A
MN MN3 fmt
7-5
7-5
'bmp'
Windows (Bitmap)
'pgm'
'cur'
'png'
(Graphics Interchange
'gif'
Format)
'pnm'
(Portable Graymap)
(Portable
Network Graphics))
(Portable
Anymap)
(Portable
'hdf'
'ppm'
'ico'
'ras'
(Sun Raster)
'jpg'
(Joint Photographic
'tif'
(Tagged Image
'jpeg'
Experts Group)
'tiff'
File Format)
'pbm'
(Portable Bitmap)
'xwd'
'pcx'
PC (Paintbrush)
Pixmap)
Windows (X Windows
Dump)
7.4
MATLAB
7.4.1
ezplot
(1) ezplot(f) x (-2*pi<x<2*pi) f=f(x) f=f(x,y)
xy -2*pi< x <2*pi,-2*pi< y<2*p f(x,y) = 0
208
MATLAB
209
7.33
7.33
7.27
x='3*t*sin(t)';
y='t*cos(t)';
ezplot(x,y,[0,8*pi])
7.34
7.4.2
z=f(x,y) ezmesh
meshgrid z for( while) z
7.3
1. ezmesh
209
210
MATLAB
7.34
7.28
syms x,y;
f='sqrt(1-x^2-y^2)';
ezmesh(f)
7.35
Sprt(1-x^2-y^2)
7.35
7.29
syms x y z s t;
x='s*cos(t)';y='s*sin(t)';z='t';
ezmesh(x,y,z,[0,pi,0,5*pi])
210
MATLAB
211
7.36
7.36
2. meshgrid z
z=f(x,y) x y z x y z
MATLAB z z
z(i, : ) = f(x,y(i))
z(:, j ) = f(x(j),y)
z=f(x,y) meshgrid z
z=f(x,y)
7.30 z=f(x,y)=x3+y3
x=0:0.2:5;
% x
y=-3:0.2:1;
% y
[X,Y]=meshgrid(x,y); % X Y
Z=X.^3+Y.^3;
surf(X,Y,Z);xlabel('x'),ylabel('y'),zlabel('z');
title('z=x^3+y^3')
7.37
z=x^3+y^3
7.37
211
MATLAB
212
3
7.31 7.30
x=0:0.2:5;
y=-3:0.2:1;
z1=y.^3;
z2=x.^3;
nz1=length(z1);
nz2=length(z2);
Z=zeros(nz1,nz2);
for r=1:nz1
for c=1:nz2
Z(r,c)=z1(r)+z2(c);
end
end
surf(x,y,Z); ;xlabel('x'),ylabel('y'),zlabel('z');
title('z=x^3+y^3')
7.30
7.5
7.6
1
(1) r=3(1-cos )
y1(t) y2(t)
4
(1) y=sin2xcos3x(2) y=0.4 x
x y
212
MATLAB
213
5 z(x,y)
1
1
z (x,y ) =
( 3 x 3, 3 y 3)
2
2
2
(x + 1) + (y + 1) + 1 (x 1) + (y 1) 2 + 1
6 z = c d
x2 y 2
a 2 b2
(1) a=5b=4c=3d=1
(2) a=5jb=4c=3d=1
213
SIMULINK
SIMULINK MATLAB
SIMULINK
SIMULINK
8.1
SIMULINK
SIMULINK MATLAB
SIMULINK
MATLAB
8.1.1
SIMULINK
1
SIMULINK SIMULINK
(
)
SIMULINK
2
SIMULINK MATLAB
SIMULINK
MATLAB
SIMULINK
215
3(Blocksets)
SIMULINK DSP Blocksets Communication Blocksets
SIMULINK DSP
4 MATLAB
MATLAB SIMULINK
8.1.2
SIMULINK
1. MATLAB SIMULINK
(1) MATLAB simulink
(2) MATLAB SIMULINK
SIMULINK SIMULINK 8.1
8.1 SIMULINK
215
MATLAB
216
SIMULINK SIMULINK
SIMULINK SIMULINK
Control System Toolbox ()
Communications Blockset()
8.1
SIMULINK Continuous()Discrete()
Sinks()Sources() SIMULINK
2. SIMULINK
(1) MATLAB File|New
(2)
untited 8.2
8.2
SIMULINK
SIMULINK
1
SIMULINK
SIMULINK
216
SIMULINK
217
SIMULINK MATLAB
SIMULINK SIMULINK
SIMULINK (
) SIMULINK
8.2
SIMULINK
SIMULINK
SIMULINK
8.2.1
1.
SIMULINK
.mdl ASCII
SIMULINK
SIMULINK
SIMULINK
SIMULINK 3
(Source)
SIMULINK
(Sink)
SIMULINK
SIMULINK 3
2.
( File Save Save as )
SIMULINK .mdl
SIMULINK MATLAB
1)
united (8.1 )
217
MATLAB
218
2)
MATLAB (.mdl)
MATLAB
MATLAB File|Open
3)
SIMULINK .mdl ASCII
63
MATLAB
4)
SIMULINK
File|Print
Current system
Current system and above
Current system and below
All system
Include print log
Frame
8.2.2
SIMULINK ()
SIMULINK
1.
SIMULINK
(1)
218
SIMULINK
219
SIMULINK ( Word )
3.
SIMULINK (
)
4.
6.
SIMULINK ()
219
220
MATLAB
8.
Format Foreground|ColorBackground|Color
Screen|Color
9.
SIMULINK
(1)
(2) Format|Font(Set
Font)
(3)
()
()
Format|Flip Name
10.
SIMULINK
( Model properties
)
11. /
220
SIMULINK
221
8.2.3
(
)
1.
(1) ()
()
(2) Ctrl
2.
(1)
(2)
Delete
Edit|Delete
clear cut
3.
(1)
8.3
8.3
Ctrl
221
222
MATLAB
Ctrl
(2) 8.4
8.4
Shift
Shift
8.5
8.5
4.
8.6
8.6
5.
(1)
8.7
(2)
Format|Wide nonscalar lines
222
SIMULINK
223
8.8
8.7
8.8
SIMULINK
Format|Sample Time ColorSIMULINK
8.2.4
SIMULINK
MATLAB 8.9
8.9
(1)
(2)
(3)
(4) Shift Delete Backspace
(5)
Format
8.2.5
Source
Source
223
MATLAB
224
8-1 Source
8-1 Sources
Chirp Signal
Clock
Constant
Digital Clock
From Workspace
MATLAB
From File
Ground
In1
Pulse Generator
Ramp
Random number
Repeating sequence
Signal Generator
Signal buider
Sine Wave
Step
1. Chirp Signal()
4
(1) Initial frequency 0.1Hz
(2) Target time
100
(3) Frequency at target time 1Hz
(4) Interpret vector parameters as 1-D
2. Clock()
Digital
Clock
224
SIMULINK
225
Clock 2
(1) Display time
(2) Decimation 10
3. Constant()
Constant
Interpret vector parameters as 1-D
8.10 Constant
8.10 Constant
226
MATLAB
Sample based() 5
(1) Amplitude 1
(2) Bias 0
(3) Samples per period 10
(4) Number of offset samples 0
(5) Sample time 0 0
5. Repeating Sequence ()
2
(1) Time values0,2
4
(1) Wave form 4
226
SIMULINK
227
8.12
7. Step()
8.1 (t 2)
8.13 8.14 (t 2)
8.13 8.1
8.14 8.1
228
MATLAB
8. Ramp()
(1) Slope 1
(2) Start time 0
(3) Initial output 0
9. Pulse Generator()
(1) Amplitude 1
(2) Period 2 s
(3) Pulse width 50%
(4) Phase delay 0
10. Digital Clock()
Sample time() 1s
11. From workspace ()
MATLAB
(1) Data
(2) Sample time 0
(3) Interpolate data
(4) Form output after final data value by
t2 " tfinal
t1
u
m m-1
2
(1) File name untitled.mat
228
SIMULINK
229
14. In1()
3
(1) Noise power(PSD) 0.1
(2) Sample time 0.1
(3) Seed 23341
16. Random Number()
Uniform
Random Number
4
(1) Mean 0
(2) Variance 1
(3) Initial seed 1
(4) Sample time 0
Initial seed
4
229
MATLAB
230
(1)
(2)
(3)
(4)
8.2.6
Minimum-1
Maximum 1
Initial seed 0
Sample time 0
Sink
Sink
8-2 Sink
8-2 Sink
Display
Floating Scope
Out1
Scope
Stop simulation
Terminator
To File
To Workspace
XY Graph
MATLAB X-Y
Source
1. Display
(1) Format
(2)
8.15 8.15(a)
8.15(b)
(b)
(a)
8.15 Display
230
SIMULINK
231
8.16
(1) Terminator
(SIMULINK )
(2) 3 Shift
8.17 8.2
8.18 8.2
231
232
MATLAB
Scope xy
Axes properties
Y-min 0Y-max 60Title() Scope
Scope Sink Scope
( xy )
x y
(1)
(2) Scope properties(
)General Data history( 8.19 )
(a) General
8.19
General
Number of axes() Scope
1
Time range( x )
Tick labels 3 all(
)none()bottom axis only( x )
Floating scope Scope Floating scope
Sampling Decimation
Sample time
Data histroy
Limit data points to lastScope
n (n )
Save data to workspace Scope
232
SIMULINK
233
MATLAB 3
(Structure with time)(Structure)(Array)
Sink To workspace
(3)
x y x y
()
( x y )
(4)
Autoscale
(5)
Scope
(6)
(a)
(b)
8.20
Signal Selector
Shift
233
MATLAB
234
Simulink
()()
Scope
3. Out1
Source In1
8.3 Out1
8.21
Out1 (
Configuration Parameters Data Import/Export 8.3 )
()
tout yout
MATLAB
>> plot(tout,yout)
MATLAB 8.22
8.21 8.3
8.22 8.3
4. To Workspace()
MATLAB
(1) Variable name simout
(2) Limit data points to lastTo Workspace
n (n ) inf
(3) Decimation 1
(4) Sample time-1
(5) Save format
5. XY Graph(XY )
MATLAB X-Y
(1) x-minx -1
(2) x-maxx 1
(3) y-miny -1
(4) y-maxy 1
234
SIMULINK
235
6. To file
Mat
(1) Filename untitled.mat
MATLAB
(2) Variable name ans
(3) Decimation 1
(4) Sample time-1
Scope XY
Graph Floating Scope Display Out1
MATLAB To Workspace
MATLAB
MATLAB
8.2.7
Simulation|
Configuration
Parameters 8.23
235
236
MATLAB
8.23
Ode23-
Ode45
Ode113 Ode45
Ode15s
Ode45
Ode15s
Ode23s
Ode15s Ode15s stiff
Ode23t stiff
Ode23tb Ode15s
Ode5 Ode45
Ode4-
236
SIMULINK
237
Ode3-
Ode2
Ode1
Ode14X
(3)
Multitasking
2. Data Import/Export(/)
MATLAB
Data Import/Export 8.24
4
(1) Load from workspace
Input[t,u] n
n
237
238
MATLAB
MATLAB
Ode45
SIMULINK
239
Refine factor
Optimazation Block
reduction optimization
Real-time
MATLAB
8.2.8
(1) Simulation|Start
(2)
(3) sim('model')
SIMULINK MATLAB
8.3
SIMULINK
3 SIMULINK
(1)
Simulink
Source Sine Wave
Math Operations Product
Sink Scope
8.25
(2)
239
240
MATLAB
8.25 8.4
8.26 8.4
SIMULINK
241
(1)
SIMULINK
Source Signal Generator 2sin2t( Sine )
Source Pulse Generator
Source Clock
Source Constant
Logical and Bit operations Relational Operator
8.27 8.5
(2)
(3)
Start time 0Stop time 30.0( 15s
)
(4) 8.28
SIMULINK
h=/50 0.6
Solver Max step size
0.1 8.29
241
242
MATLAB
8.28 8.5
8.29 8.5
us(t)
8.30 RLC
242
uc(t)
SIMULINK
243
(1)
d2
1
1
R d
u (t ) + uc (t ) +
uc (t ) =
us (t )
2 c
dt
LC
LC
L dt
8.31
8.31 RLC
(2)
Step Step Time 0
Gain Gain2 1/(L*C)
Gain1 R/L
Sum Icon shape: rectangularList of signs:+ - -
To Workspace Variable name timeout( )Save format Array
()
To Workspace1 Variable name ucout() To Workspace
(3) Solver 1s
(4)
MATLAB RLC
R=100L=0.25C=100e-6
8.32
(5) To Workspace MATLAB 8.31
To Workspace () ucout
timeout ( ucout timeout To
Workspace ) Clock To Workspace
MATLAB plot(timeoutucout)MATLAB
8.33 ( 3 )
Save format
243
244
MATLAB
(a)
(c)
(b)
(d)
8.32 8.6
8.33 To Workspace
SIMULINK
245
8.35 To File
To File
Filename()respond.mat
Variable name()uc
respond.mat MATLAB
Work
MATLAB
>> uc
uc uc
8.4
SIMULINK
245
246
MATLAB
SIMULINK
SIMULINK
8.5
1 step
Step time 1
2 8.36 Signal Gernerator
1 1rad/s
8.36
246
MATLAB
MATLAB
MATLAB
(1) MATLAB
(2) MATLAB 5
(3) MATLAB
1. MATLAB 5
Command Window
()
Workspace
()
Command History
()
Current Directory
()
Help Window
()
(1) (Command Window)
>>x=1
>>y=[1 2 3
4 5 6
7 8 9];
>>z1=[1:10], z2=[1:2:5];
>>w=linspace(1,10,10);
>>t1=ones(3) ,t2=ones(1,3),t3=ones(3,1)
>>t4=zeros(3),t5=eye(4)
linspace( ) help
>>help
linspace
248
MATLAB
(2) (Workspace)
MATLAB
who, whos()
Array Editor ()
save my_var.mat
>>save my_var.mat
>>clear all %
load
>>load my_var.mat
>>clc
Copy Paste
Evaluate Selection
MATLAB
248
MATLAB
249
shortlongshort elong e
>>format
>>pi
>>format
>>pi
>>format
>>pi
>>format
>>pi
>>-pi
long
short
long
+
3.
>>pi
>>sin(pi);
>>exist('pi')
>>pi=0;
>>exist('pi')
>>pi
>>clear pi
>>exist('pi')
>>pi
3 exist('pi')
pi 0
249
MATLAB
250
MATLAB
MATLAB ()
(1)
(2)
(3)
(4)
1.
1)
>>A=[2,3,4,5,6] %
>>B=[1;2;3;4;5] %
>>A=1:2:10,B=1:10,C=10:-1:1
linspace( ) logspace( )
>>A=linspace(1,10),B=linspace(1,30,10)
>>A=logspace(0,4,5)
logspace( ) 14 10
2)
>>A=[1 2 3 4 5]B=3:7
>>AT=A'BT=B'
%
>>E1=A+B,E2=A-B
%
>>F=AT-BT,
%
>>G1=3*A,G2=B3,
%
>>A=ones(1,10);B=(1:10); BT=B;
>>E1=dot(A,B)
>>E2=A*BT
% E1 E2
250
MATLAB
251
>>clear
>>A=1:4,B=3:6,
>>E=cross(A,B)
2.
1)
mn
>>A=[1 2 3;4 5 6]
>>B=[1 4 7
2 5 8
3 6 9]
>> A(1)
>>A(4:end)
>>B(:,1)
>>B(:)
>>B(5)
%
%end
>>clear
>>A=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16]
>>B=A(1:3,2:3)
% A 13 23
>>C=A([1 3],[2 4])
% A 13 24
>>D=A([1 3;2 4])
%
>>clear
>>A=ones(3,4)
>>B=zeros(3)
>>C=eye(3,2)
>>D=magic(3)
>>clear
>>A=ones(3,4)
>>B=zeros(3)
>>C=eye(4)
>>D=[A B]
>>F=[A;C]
>>clear
>>A=[0 1;1 1]
>>B=2*ones(2)
>>cat(1,A,B,A)
>>cat(2,A,B,A)
>>repmat(A,2,2)
>>repmat(A,2)
251
MATLAB
252
A 34
1 B 33 0 C 33 D 33 E C
D F E 25 G F 34
G (repmat) 68 H
2)
1
A=
3
1
2
B =
1
1
AB2A2A3BAB
>>format rat;A=[1 0 1;2 1 2;0 4 6]
>>A1=inv(A)
>>A*A1
>>a=[1 2 1;3 1 4;2 2 1],b=[1 1 2],d=b
>>c1= b*inv(a), c2= b/a
%
>>c3=inv(a)*d , c4= a\d
%
c1 c2c3 c4
(1) x=[x1x2x3]
6 x1 + 3x2 + 4 x3 = 3
2 x1 + 5 x2 + 7 x3 = 4
8 x x 3x = 7
3
1 2
(2)
(3)
(4)
(5)
(6)
3.
1)
>> A1=[1,2,3;4 5 6;7,8,9];A2=reshape([10:18],3,3)
>>T1(:,:,1)=ones(3);T1(:,:,2)=zeros(3)
%
>>T2=ones(3,3,2)
%
>>T3=cat(3,A1,A2),T4=repmat(A1,[1,1,2])
%
252
MATLAB
253
2)
>> A=[1:6];B=ones(1,6);
>> C1=A+B,C2=A-B
>> C3=A.*B,C4=B./A,C5=A.\B
>> I=A>3,C6=A(I)
>> A1=A-3,I2=A1&A
>> I3=I
% I2
1 3
1 2
3 5
A
4 2
2 1
7 1
reshape BB 3 2 2
4.
1)
>>S1='Ilike MATLAB'
>>S2='I''m a student.'
>>S3=[S2,'and',S1]
2)
>> length(S1)
>> size(S1)
% length() size()
3)
>>
>>
>>
>>
CS1=abs(S1)
CS2=double(S1)
char(CS2)
setstr(CS2)
% ASCII
char( ) AaBbCcDdXxYyZz
A a ASCII 6597
253
MATLAB
254
MATLAB
MATLAB
(1)
(2)
(3)
(4)
1.
(1) MATLAB
s + 3s 5s + 9
4
MATLAB
>>S=[ 1 3 -5 0 9]
(2)
0 2s 2 + 3s + 9 s 4 + 3s 3 5s 2 + 4 s + 7
>>S1=[0 0 2 3 11 ]
>>S2=[1 3 -5 4 7 ]
>>S3=S1+S2
(4) roots
>> S1=[ 2 4 2 ]
>> roots(S1)
(5) polyval
>>
>>
>>
>>
S1=[ 2 4 1 -3 ]
polyval(S1,3)
x1:10
y=ployval(S1,x)
% x3
% x y
( s 2 + 1)( s + 3 )( s + 1)
s3 + 2s + 1
254
MATLAB
255
2.
1-1
1-1
X
10
16
32
70
142
260
436
682
1010
1432
1960
()() X=9.5 Y
>>x=1:10
>>y=[16 32 70 142 260 436 682 1010 1432 1960]
>>p1=ployfit(x,y,1)
%
>>y1=ployval(p1,9.5)
% p1 x9.5
3.
(1) diff
>>x=1:2:9
>>diff(x)
(2)
>>x=linspace(0,2*pi,100);
>>y=sin(x);
>>plot(x,y)
>>y1=diff(y)./diff(x);
>>plot(x(1:end-1),y1)
A1
X Y
X Y
Y Y1 Y2 1 18mm 40km
41228km2
1-2( mm)
1-2
X
Y1
Y2
7
44
44
10.5
45
59
13
47
70
17.5
50
72
34
50
93
40.5
38
100
44.5
30
110
48
30
110
56
34
110
61
36
117
68.5
34
118
76.5
41
116
80.5
45
118
91
46
118
255
MATLAB
256
96
101
104
106.5 111.5
118
123.5 136.5
Y1
43
37
33
28
32
65
55
Y2
121
124
121
121
121
116
122
142
146
150
157
158
54
52
50
66
66
68
83
81
82
86
85
68
A1
4.
(1)
>>
>>
>>
>>
student.number='20050731001';
student.name='Jack';
student(2).number='20050731002';
student(2).name ='Lucy';
struct
>> student = struct('number',{ '001', '002'},'name',{ 'Jack', 'Lucy'});
(2)
>>
>>
>>
>>
>>
>>
>>
>>
student(1).subject=[]
% subject
student(1).sorce=[]
student
fieldnames(student)
getfield(student,{2},'name')
student=rmfield(student, 'subject')
% subject
student=setfield(student,{1},'sorce',90);
student(2).sorce=88;
%
stusorce
22
(3)
>> A={'How are you!',ones(3);[1 2;3 4],{'cell'}};
%
>> B(1,1)={'Hello world'};
%
256
MATLAB
257
>> B(1,2)={magic(3)};
>> B(2,1)={[ 1 2 3 4]};
cell
>>
>>
>>
>>
C=cell(1,2);
% 12
C(1,1)={'Hello world'};
C(1,2)={magic(3)};
C(1,3)={[ 1 2 3 4]};
(4)
>>
>>
>>
>>
>>
>>
ans1=A(1,1)
ans2=A{1,1}
whos ans1 ans2
elldisp(A)
a1=A{2,1}(1,2)
[a2 a3]=deal(A{1:2})
%ans1 ans2
%
% A 2 1 1 2
% A 1 2 a2a3
22 stucell
257
MATLAB
258
MATLAB
MATLAB symbol
(1)
(2)
(3)
(4)
1.
sin x
x 0
x
x0 0 MATLAB
lim
x0
>>f=sym('sin(pi*x)/x ')
>>limit(f,x,0)
2.
1) sym( )
Workspace ABf
>>A=sym('1')
>>B=sym('x')
>>f=sym('2*x^2+3y-1')
>>clear
>>f1=sym('1+2')
>>f2=sym(1+2)
>>f3=sym('2*x+3')
>>f4=sym(2*x+3)
>>x=1
>>f4=sym(2*x+3)
%
%
%
%
%
%
MATLAB sym( )
2) syms
>>clear
>>syms x y z
>>x,y,z
258
% x,y,z
MATLAB
259
>>f1=x^2+2*x+1
>>f2=exp(y)+exp(z)^2
>>f3=f1+f2
(
)
>>f1=sym('x^2+y +sin(2)')
>>syms x y
>>f2=x^2+y+sin(2)
>>x=sym('2') , y=sym('1')
>>f3=x^2+y+sin(2)
>>y=sym('w')
>>f4=x^2+y+sin(2)
syms x x=sym('x')
3.
>>syms a1 a2 a3 a4
>>A=[a1 a2;a3 a4]
>>A(1),A(3)
e2t
4.
1)
>>a=sym(5);b=sym(7);
>>c1=a*b
>>c2=a/b
>>a=sym(5);B=sym([3 4 5]);
>>C1=a*B, C2=a\B
>>syms a b
>>A=[5 a;b 3]; B=[2*a b;2*b a];
>>C1=A*B, C2=A.*B
>>C3=A\B, C4=A./B
2)
VPA digits() vpa(
)
259
260
MATLAB
>>a=sym('2*sqrt(5)+pi')
>>b=sym(2*sqrt(5)+pi)
>>digits
>>vpa(a)
>>digits(15)
>>vpa(a)
>>c1=vpa(a,56)
>>c2=vpa(b,56)
c1 c2 c1 c2
3)
sym vpa
VPA double,numeric VPA
>>clear
>>a1=sym('2*sqrt(5)+pi')
>>b1=double(a1)
%
>>b2=numeric(a1)
%
>>a2=vpa(a1,70)
%
5.
1)
MATLAB
( ij ) x x
x
'3*y+z'y 'sin(a*t+b)'t
>>clear
>>f=sym('a+b+i+j+x+y+xz')
>>findsym(f)
>>findsym(f,1) , findsym(f,2) , findsym(f,3)
>>findsym(f,4) , findsym(f,5) , findsym(f,6)
2)
(pretty) (collect)
(expand)(factor)(simple simplify)
(collect) x e
2
(x +xe-t+1)(x+e-t)
>>syms x t; f=(x^2+x*exp(-t)+1)*(x+exp(-t));
>>f1=collect(f)
>>f2=collect(f,exp(-t))
(pretty)
260
MATLAB
261
>>pretty(f1)
>>pretty(f2)
f1 f2
(expand) (x-1)12
>>clear all
>>syms x;
>>f=(x-1)^12;
>>pretty(expand(f))
(factor) x121
>>clear all
>> syms x; f=x^12-1;
>>pretty(factor(f))
(simple simplify)
1
6 12
f= 3 3 + 2 + + 8
x
x
x
>>clear all, syms x; f=(1/x^3+6/x^2+12/x+8)^(1/3);
>>g1=simple(f)
>>g2=simplify(f)
6.
subs
>>clear
>>f=sym('(x+y)^2+4*x+10')
>>f1=subs(f, 'x', 's')
>>f2=subs(f, 'x+y', 'z')
% s x
% F xa
% F *a
% F 0(a=0)
% F xa
% F xa
>>clear
>>f=sym('sin(x)/x+a*x')
261
262
>>limit(f,x,0)
>>limit(f,a,0)
>>limit(f)
>>findsym(f)
MATLAB
% x
% a
% x
%
>>clear
>>f=sym('sqrt(1+1/n)');
>>limit(f,n,inf)
% n
2)
int(F)
int(F,v)
int(F,a,b)
int(F,v,a,b)
% F
% F v
% F a b
% F v a b
3)
diff(F)
diff(F,v)
diff(F,n)
diff(F, v,n)
% F
% F v
% F n n 123
% F v n
8.
1)
g = solve(eq)
%()eq
g = solve(eq,var)
%()eq var
g = solve(eq1,eq2,...,eqn,var1,var2,...,varn)
%()
eq1,eq2,...,eqn var1,var2,...,varn
ax2+bx+c=0
Seq=solve(a*x^2+b*x+c)
Seq=solve(a*x^2+b*x+c=0)
eq=a*x^2+b*x+c;
eq=a*x^2+b*x+c=0;
Seq=solve(eq)
syms x a b c;
eq= a*x^2+b*x+c;
Seq=solve(eq)
2)
dsolve()
()
r = dsolve('eq1,eq2,...','cond1,cond2,...', 'v')
r = dsolve('eq1','eq2',...,'cond1','cond2',...,'v')
262
MATLAB
263
t
12
dy
d2 y
MATLAB Dy
D2y 2
dx
dx
y(a) = b Dy(a) = b y ab
C1C2
y' =2x
y=dsolve('Dy=2*x', 'x')
(1) lim
x2
x2 1
x 2 3x + 2
6
0
( sin x + 2 )dx
(4)
x + y + z = 10
3x + 2y + z = 14
2x + 3y z = 1
(5) y(0)=2z(0)=7
dy
dx z = sin x
dz
+ y =1+ x
dx
263
MATLAB
264
MATLAB
MATLAB MATLAB
(1)
(2)
(3)
(4)
1. M
MATLAB File|New|M-file M
exp1.m
% M
% 1100
s=0;
for n=1:100
s=s+n;
end
s
exp1
M exp2.m
% M
% 1x
function s=exp2(x)
s=0;
for n=1:x
s=s+n;
end
>>clear
>>s=exp2(100)
open
>>open conv
2.
1) for
for n=1:10
264
% M
% conv
MATLAB
265
n
end
for
n=10:-1:5
for i=n
i
end
% n
2) while
>>clear,clc;
x=1;
while 1
x=x*2
end
3) if-else-end
if-else-end 3
(a) if
1
end
(b) if
1
else
2
end
(c) if
elseif
A
1
B
2
elseif
3
else
n
end
265
MATLAB
266
4) switch-case
M exp3.m
%
n=input('n=');
if isempty(n)
error('please input n')
end
switch mod(n,2)
case 1
A=''
case 0
A=''
end
3.
g ( x) =
n!
(x1,2,3)
n =1
function g=exp4(x)
g=0;
for n=1:x
g=g+fact(n);
end
function y=fact(k)
y=1;
for i=1:k
y=y*i;
end
nargin sinplot2( ) w
sinplot2( ) p 0
function y=sinplot (w,p)
if nargin>2
erro(too many input)
end
if nargin==1
p=0;
end
x=linspace(0,2*pi,500);
z=sin(x.*w+p);
(1) rect
(2) nch=my_code( ch , x) ch x
ch ASCII x
nch ASCII (32126) ASCII
266
MATLAB
267
126 93 nch=my_dcode( ch , x)
char(32:126) ASCII 32126
4.
MATLAB
global
summ.m
function s=summ
global BEG END
k=BEG:END;
s=sum(k);
M use.m summ.m
267
MATLAB
268
MATLAB
MATLAB
(1)
(2)
(3)
1
(1) plot
>>
>>
>>
>>
>>
>>
>>
clear all;
x=linspace(0,2*pi,100);
y1=sin(x);
plot(x,y)
hold on
%
y2=cos(x)
plot(x,y)
(2) plot
>>
>>
>>
>>
>>
>>
>>
close all
%
x=linspace(0,2*pi,100);
y1=sin(x);
y2=cos(x);
A=[y1 ; y2]';
%
B=[x ; x]'
plot(B,A)
(3)
>> close all
%
>> plot(x,y1,'g+',x,y2, 'r:')
>> grid on
%
(4)
>> title('')
>> ylabel('')
>> xlabel('')
268
MATLAB
269
(5)
>> axis equal
>> axis normal
>> axis([0 pi 0 1.5])
(6)
>>subplot(2,2,1)
>>t1=0:0.1:3;
>>y1=exp(-t1);
>>bar(t1,y1);
>>subplot(2,2,2)
>>t2=0:0.2:2*pi;
>>y2=sin(t2);
>>stem(t2,y2);
>>subplot(2,2,3)
>>t3=0:0.1:3;
>>y3=t3.^2+1;
>>stairs(t3,y3);
>>subplot(2,2,4)
>>t4=0:.01:2*pi;
>>y4= abs(cos(2*t4));
>>polar(t4,y4);
A2
A2
269
MATLAB
270
(1)( 3 )(2)
()(3)(4)
2.
(1) plot3
>>z=0:0.1:6*pi;
>>x=cos(z);
>>y=sin(z);
>>plot3(x,y,z);
meshgrid( )[X,Y]
[X,Y] Z
mesh( ) surf( )
z = x 2 e ( x
+ y2 )
x=[-2,2]y=[-2,2] z ( 0.1)
z A3
270
MATLAB
271
A3
3.
>>
>>
>>
>>
>>
>>
>>
x=imread('cameraman.tif')
imshow(x)
y=255-double(x);
y=uint8(y);
figure
imshow(y)
imwrite(y,'reverse.tif')
%
%
271
MATLAB
272
SIMULINK
SIMULINK SIMULINK
(1) SIMULINK
(2)
1Simulink
(1)
MATLAB simulink MATLAB
SIMULINK SIMULINK
MATLAB File|New|Model
untitle
SourcesSine Wave
Math OperationsProductSinks Scope
(2)
Sine WaveFrequency 100
Scope
Number of axes 3
A4
A4
(3)
Simulation|Start
Scope
272
MATLAB
273
0 .6 s 1
StepStep time 0Max step size
0.01
SourcesClock
SinksTo workspace
Clock
To workspaceVariable name y t A5
y t
>>y1=y.signals.values;
>>t1=t.signals.values;
>>plot(t1,y1)
A5
273