You are on page 1of 268

1 MATLAB 6.5 ...........................................................................................................

6
1.1
MATLAB ......................................................................................................... 6
1.1.1 MATLAB ..................................................................................................... 6
1.1.2 MATLAB ............................................................................................. 6
1.2
MATLAB 6.5 ............................................................................................ 7
1.2.1 ..................................................................................................................... 8
1.2.2 ................................................................................................................... 11
1.2.3 ............................................................................................... 11
1.3
MATLAB 6.5 .................................................................................................. 20
1.4 MATLAB 6.5 ................................................................................................. 21
1.4.1 MATLAB ....................................................................................... 21
1.4.2 ........................................................................................................ 22
1.4.3 ........................................................................................................ 23
1.4.4 MATLAB...................................................................................................... 24
1.5 .......................................................................................................................... 24
2 MATLAB ....................................................................................................... 27
2.1 ...................................................................................................................... 27
2.1.1 ................................................................................................................ 27
2.1.2 ........................................................................................................................ 28
2.1.3 ........................................................................................................................ 29
2.2 ...................................................................................................................... 30
2.2.1 ................................................................................................................ 30
2.2.2 .................................................................................................... 33
2.2.3 .................................................................................................................... 39
2.2.4 .................................................................................................... 43
2.2.5 ................................................................................................................ 54
2.3 ........................................................................................................................... 57
2.3.1 .................................................................................................... 57
2.3.2 ............................................................................................ 60
2.3.3 .................................................................................................... 61
2.4 ............................................................................................................................... 61
2.4.1 ................................................................ 61
2.4.2 .................................................................................... 63
2.4.3 ................................................................................................ 64
2.5 ....................................................................................................... 67
2.5.1 ................................................................................................................ 67
2.5.2 ................................................................................................................ 70
2.6 ........................................................................................................................... 73
2.6.1 ............................................................................................ 73
2.6.2 ............................................................................................................ 74
2.6.3 ........................................................................................ 76
2.6.4 ................................................................................................................ 78
3 MATLAB ....................................................................................................... 79
3.1 .......................................................................................................... 79

3.1.1 ....................................................................................................... 79
3.1.2 ....................................................................................... 80
3.1.3 ............................................................................................................... 81
3.2 ................................................................................................... 83
3.2.1 ........................................................................................ 83
3.2.2 ........................................................................... 85
3.2.3 ............................................................................... 86
3.3 ............................................................................................... 87
3.3.1 ............................................................................ 87
3.3.2 ................................................................................................ 88
3.3.3 ................................................................................................ 91
3.3.4 ............................................................................................ 92
3.3.5 ............................................................................................... 94
3.4 ...................................................................................... 95
3.4.1 ................................................................................................................ 95
3.4.2 ................................................................................................................ 96
3.4.3 ................................................................................................................ 98
3.4.4 ................................................................................................................ 99
3.5 ................................................................................................................ 100
3.5.1 (Fourier)........................................................................ 100
3.5.2 (Laplace)................................................................... 101
3.5.3 Z ............................................................................................... 102
3.6 ............................................................................................................. 103
3.6.1 .............................................................................................................. 103
3.6.2 .................................................................................................. 104
3.7 ......................................................................................................... 105
3.7.1 .......................................................................................... 105
3.7.2 .................................................................................. 107
3.8 Maple .......................................................................................................... 107
3.8.1 Maple .................................................................................................. 107
3.8.2 Maple ............................................................................................. 108
4 MATLAB GUI ........................................................................... 109
4.1 ............................................................................................................. 109
4.1.1 plot............................................................................................... 109
4.1.2 .......................................................................................... 113
4.1.3 .......................................................................................... 114
4.1.4 .......................................................................... 116
4.1.5 ...................................................................................... 117
4.1.6 .................................................................................................. 120
4.2 MATLAB ............................................................................................ 121
4.2.1 plot3..................................................................................... 121
4.2.2 .................................................................................. 122
4.2.3 ...................................................................................... 125
4.2.4 .......................................................................................................... 127

4.3 MATLAB ............................................................................................ 130


4.3.1 .................................................................................................................. 130
4.3.2 .................................................................................................. 131
4.3.3 .................................................................................................................. 132
4.3.4 ...................................................................................................................... 133
4.3.5 .......................................................................................................... 134
4.3.6 .......................................................................................... 134
4.3.7 .............................................................................................................. 135
4.3.8 .............................................................................................................. 136
4.4 ............................................................................................................. 137
4.5 ............................................................................................................................. 138
4.6 ......................................................................................................................... 140
4.6.1 ...................................................................................................... 140
4.6.2 .................................................................................................. 141
4.6.3 .............................................................................. 144
4.7 (GUI)............................................................................................... 146
4.7.1 .............................................................................................. 146
4.7.2 ...................................................................................................................... 147
4.7.3 ...................................................................................................................... 148
4.7.5 .............................................................................................................. 150
4.7.6 GUI ...................................................................................................... 150
4.8 ................................................................................................................................. 153
4.8.1 .......................................................................................... 153
4.8.2 .......................................................................................... 153
5 MATLAB ...................................................................................................... 155
5.1 ..................................................................................................... 155
5.1.1 M ..................................................................................................... 155
5.1.2 M ............................................................................................. 155
5.1.3 M ......................................................................................................... 156
5.1.4 M ......................................................................................................... 157
5.2 ................................................................................................................. 158
5.2.1 for ... end .............................................................................................. 158
5.2.2 while ... end ......................................................................................... 159
5.2.3 Ifelseend .............................................................................. 160
5.2.4 switchcase ........................................................................................ 160
5.2.5 try... catch... end ................................................................................... 162
5.2.6 ...................................................................................................... 162
5.3 ..................................................................................................... 164
5.3.1 .............................................................................................. 164
5.3.2 .......................................................................................... 165
5.3.3 .......................................................................................................... 166
5.3.4 .............................................................................................................. 169
5.4 M ............................................................................................ 171
5.4.1 P ............................................................................................................... 171

5.4.2 M ................................................................................................. 171


5.4.3 JIT ........................................................................................................ 172
5.5 ......................................................................................................................... 175
5.6 ................................................................................................. 176
5.6.1 .................................................................................................. 176
5.6.2 feval ........................................................................................ 177
5.7 ......................................................................................... 178
5.7.1 .............................................................................................................. 179
5.7.2 .............................................................................................................. 180
5.7.3 .............................................................................................................. 181
5.7.4 .............................................................................................. 181
6 ............................................................................................. 183
6.1 ............................................................................................................. 183
6.1.1 .................................................................................................. 183
6.1.2 .................................................................................................. 184
6.1.3 ...................................................................................................... 185
6.1.4 .......................................................................................... 185
6.2 ............................................................................................. 188
6.2.1 .................................................................................. 188
6.2.2 ...................................................................... 191
6.2.3 .................................................................................................. 194
6.3 ..................................................................................................... 196
6.4 ..................................................................................................... 204
6.4.1 .................................................................................................. 204
6.4.2 ...................................................................................................... 205
6.4.3 ...................................................................................................... 206
6.4.4 .................................................................................................. 207
6.4.5 .................................................................................................. 209
6.5 ..................................................................................................... 210
6.5.1 .............................................................................................................. 210
6.5.2 .............................................................................................. 211
6.5.3 .......................................................................................... 214
6.5.4 .............................................................................................. 215
6.6 ................................................................................................. 215
6.6.1 .......................................................................................................... 215
6.6.2 .............................................................................................. 217
6.7 ............................................................................................. 220
6.7.1 .......................................................................................................... 220
6.7.2 .................................................................................................. 221
7 Simulink ....................................................................................................... 222
7.1 Simulink .................................................................................... 222
7.2 Simulink ................................................................................. 224
7.2.1 Simulink .......................................................................................... 224
7.2.2 Simulink .......................................................................................... 224

7.3 .................................................................................................................... 226


7.3.1 .......................................................................................................... 226
7.3.2 ...................................................................................................... 228
7.3.3 .......................................................................................... 229
7.4 Simulink ..................................................................................................... 229
7.4.1 .............................................................................................................. 229
7.4.2 .............................................................................. 231
7.5 ................................................................................................. 234
7.5.1 .......................................................................................................... 234
7.5.2 ...................................................................................................... 235
7.5.3 ...................................................................................................... 238
7.5.4 .................................................................................................. 240
7.6 ................................................................................................................. 240
7.6.1 .......................................................................................................... 240
7.6.2 .................................................................................................. 242
7.6.3 ...................................................................................................... 243
7.7 MATLAB Simulink .............................................................. 247
7.7.1 MATLAB Simulink ............................................................... 247
7.7.2 MATLAB Simulink ............................................................... 249
7.8 Simulink ........................................................................ 250
8 MATLAB ........................................................................................................ 250
8.1 MATLAB ....................................................................................................... 250
8.1.1 MEX ............................................................................................................ 250
8.1.2 MATLAB MEX EXE .............................................. 254
8.2 .................................................................................................... 256
8.2.1 .................................................................................................. 256
8.2.2 .................................................................................................. 257
8.2.3 .................................................................................................. 259
8.2.4 .............................................................................................................. 260
8.3 ............................................................................................................ 262
8.4 Notebook......................................................................................................................... 262
8.4.1 Notebook ................................................................................................. 262
8.4.2 Notebook ................................................................................................. 263
8.4.3 Notebook ................................................................................................. 264
8.4.4 Notebook MATLAB ........................................................................... 267

MATLAB 6.5

1.1 MATLAB
z

MATLAB(Matrix Laborator) MathWorks

z
z
z

MATLIB MATLIB 6.5

1.1.1 MATLAB
z MATLAB
MATLAB
z MATLAB

z MATLAB
(Control System Toolbox)
(System Identification Toolbox)
(Signal Processing Toolbox)
(Neural Network Toolbox)
(Fuzzy Logic Toolbox)
(Wavelet Toolbox)
(Model Predictive Control Toolbox)
(Communication Toolbox)
(Image Processing Toolbox)
(Frequency System Identification Toolbox)
(Optimization Toolbox)
(Partial Differential Equation Toolbox)
(Financial Toolbox)
(Statistics Toolbox)

1.1.2 MATLAB
1
(1)
z MATLAB

z MATLAB

(2)
MATLAB
(3)
MATLAB Notebook Word
MATLAB
2
z

3
z
z
z
4

5. Simulink
MATLAB Simulink
Simulink

1.2

MATLAB 6.5

MATLAB6.5 MATLAB (MATLAB


Desktop) 1.1

1.1 MATLAB 6.5

MATLAB
MATLAB6.5 Start

1.2.1
MATLAB File
Edit
View

Web
WindowHelp

1File

1.2 File

1.1 File

New

M-file

M M /

Figure

Model

GUI

(GUI)

Open...

Close Command History

Import Data...

Save Workspace as...

MAT

Page Setup...

Set Path...

Preferences...

MATLAB

Print...

Print Selection...

Exit MATLAB

MATLAB

2Edit
z Edit 1.3 Edit Windows Edit
z Paste SpecialImport Wizard

1.3 Edit

MATLAB
3View

1.4 View

1.2 View

Desktop Layout

()

Undock Command Window

Command Window

Command History

Current Directory

Workspace

Launch Pad

Profiler

Help

4. Web

1.5 Web
1.3 Web

The MathWorks Web Site

MathWorks

MATLAB Central

MATLAB Central

MATLAB File Exchange

MATLAB File Exchange

MATLAB Newsgroup Access

MATLAB Newsgroup Access

Check for Updates

Products

Membership

MathWorks

Technical Support Knowledge Base

MathWorks

5. Windows
Windows
6. Help
Help

1.6 Help

7.
z
z

Desktop ToolsWebPreferencesHelp
Demos

1.2.2
Cut

New M-File

Copy

Simulink

Past

Open Undo

Redo

Help
1.8

1.2.3
1. (Command Window)
MATLAB

z ViewUndock Command Window

MATLAB ViewDock
Command Window

1.9

(1)
z >>
z

z iffor
1.1
>> a=12.7

a =
12.7000
>> b='Hello'

b =
Hello
>> if a>0 c=5 ,end

c =
5

(2)
MATLAB
1.4
1.4

Home

End

Delete

Backspace

PageUp

Esc

Page Down

CTRL+C

MATLAB

(3)
1.5

MATLAB

()

[]

{}

At

MATLAB
1.2
>> a=12.5,b='Hello' %,,
a =
12.5000
b =
Hello
>>c=[1 2;3 4;5 6]

% [ ],,

c =
1

>> d=a*...

%...

(4)
z 4
(Short)

z File Preferences
1.10

1.10

z format
format
1.6

Format

format

1000

314.159 314.1590

format short()

3141.59 3.1416e+003

format short e

3.1416e+000

format short g

format short format short e

3.1416

format long

15

3.14159265358979

format long e

15

3.141592653589793e+000

format long g

format long format long e

3.1415926358979

format rat

355/113

format hex

400921fb54442dl8

format +

format bank

()

3.14

format compact

format loose

(5)

clc
more
2. (Command History)
1.7

Copy

(Copy)

Evaluate Selection

(Evaluate

Selection)

Create M-File

(Create M-File)

M /

1.11

1.11

3. (Current Directory Browser)


(1)
Windows MATLAB
matlab/work

MATLAB matlab/bin/win32matlab.exe
matlab/bin/win32

M MAT

1.13

MATLAB


cd
%
cd
%
cd ..
%
(2)
1.8

Run M

(Run)

M
(Open)

M Open M
M /
M

MAT

MAT Open

(Open)

MAT

MAT

MAT Import Data

Import Wizard

(Import Data)

Finish

(3) M MAT
M MAT

FilepreferencesPreferencesCurrent Directory
Brower Display OptionsShow M-file Comments and MAT-file
CommentsOK
4. (Workspace Browser)
z MATLAB

z
a=12.5
b='Hello'
c=[1 2;3 4;5 6]

1.14

1.14

(1)
1.9

Open

Array Editor

Graph

Save Workspace As

MAT

Save Selection As

MAT

Delete

Confirm DeleteYes
EditDelete

Clear Workspace

(2)

save: MAT
save FileName 1 2
%

FileName MAT
1 2
-ASCII-append
>> save FileName1
>> save FileName2 a b
>> save FileName3 a b -append

% FileName1.mat
% ab FileName2.mat
% ab FileName3.mat

load:
load FileName 1 2
1 2

>> load Filename1


>> load FileName2 a b

% FileName1.mat
% FileName2.mat ab

who: MATLAB
>> who
Your variables are:
a b c

whos: MATLAB

>> whos
Name
Size
Bytes
a
1x1
8
b
1x5
10
c
3x2
48
Grand total is 12 elements using 66 bytes

Class
double array
char array
double array

clear:

>> clear a
>> who
Your variables are:
b c

exist(X) :
i=exist(X)
%X

i=1 X
i=2 X.m
i=3 X.mex
i=4 X.mdl
i=5 X
i=0

5. (Array Editor)
Open

1.15 c=[1 2;3 4;5 6]Array Editor

1.15

Array Editor

Numeric format
by
Size

6. (Launch Pad)
Import WizardProfiler
GUIDE
Help

Demos
Demos
Product Page (Web)

7. M (Editor
Debugger)
M
MATLAB

FileNewM-file M

MATLAB

1.16

FileOpenOpen
M
M (.m) M

1.17 M


1.17 Ex0101.m M
8. (Help NavigatorBrowser)

ViewHelpHelpMATLAB

Help
9. (Profiler)
ViewProfilerprofile viewer
1.18
ViewDock profiler MATLAB

1.18

1.3

MATLAB 6.5

MATLAB6.5
MATLAB
1.
1.19

1.19

1.19 Help Navigator()


Help Browser()

(1) Contents
Begin Here MATLAB
Release Notes For Release R13
Installation
MATLAB
Getting Started MATLAB
Examples
Development Environment MATLAB
Mathematics MATLAB
Programming and Data Types M
Graphics
Printable Documentation PDF
(2) Index
Index MATLAB
(3) Search
Search
(4) Demos
Demos MATLAB Demo
(5) Favorites
Favorites ()
2.
help MATLAB

help topic
%
lookfor

lookfor topic
% M H1 (
)
lookfor topic all % M
helpwin( 1.19 )
helpwin topic
%
3. PDF
MATLAB 6.5 PDF PDF
..\matlab\help\pdf-doc Adobe Acrobat Reader

4.
(1) Demos
Demos
MATLAB
(2) Web
MathWorks MATLAB
MATLAB MATLAB

1.4 MATLAB 6.5


1.4.1 MATLAB
1.
M .mM M
MATLAB M
2.
MAT .mat
save
3.
MEX .mex MATLAB M
M ( 8.1 )
4.
.figFile MATLAB

5.
.mdl Simulink .s

1.4.2
1. MATLAB
MATLAB
MATLAB X
X MATLAB (Built-in Function)
X.mX.mex
MATLAB X.m X.mex

MATLAB

existwhichload MATLAB
2. MATLAB

(1)
MATLAB FileSet Path
pathtool

1.21

Add FolderAdd with Subfolders

(2) path
path (path, )
% MATLAB
>> path(path,'c: :\MyDir ')

1.4.3

what MMATMEX

>> what
M-files in the current directory D:\MATLAB6p1\toolbox\matlab\elfun
Contents
asech
cosh
isreal
sec
abs
asin
cot
log
sech
acos
asinh
coth
log10
sign
acosh
atan
cplxpair
log2
sin
acot
atan2
csc
mod
sinh
acoth
atanh
csch
nextpow2
sqrt
acsc
ceil
exp
pow2
tan
acsch
complex
fix
real
tanh
angle
conj
floor
rem
unwrap
asec
cos
imag
round
MEX-files in the current directory D:\MATLAB6p1\toolbox\matlab\elfun
complex

dir
dir %
>> dir
.
..
Contents.m
abs.m
acos.m
acosh.m
acot.m
acoth.m
acsc.m
acsch.m
angle.m
asec.m
asech.m
asin.m

asinh.m
cplxpair.m pow2.m
atan.m
csc.m
real.m
atan2.m
csch.m
rem.m
atanh.m
exp.m
round.m
ceil.m
fix.m
sec.m
complex.c
floor.m
sech.m
complex.csf imag.m
sign.m
complex.dll isreal.m
sin.m
complex.m
ja
sinh.m
conj.m
log.m
sqrt.m
cos.m
log10.m
tan.m
cosh.m
log2.m
tanh.m
cot.m
mod.m
unwrap.m
coth.m
nextpow2.m

type M
>> type abs.m
%ABS
Absolute value.
% ABS(X) is the absolute value of the elements of X. When
%
X is complex, ABS(X) is the complex modulus (magnitude) of
%
the elements of X.
%
%
See also SIGN, ANGLE, UNWRAP.
%
Copyright 1984-2001 The MathWorks, Inc.
%
$Revision: 5.8 $ $Date: 2001/04/15 12:02:51 $
%
Built-in function.

which M MEX Simulink

>> which abs.m


D:\MATLAB6p1\toolbox\matlab\elfun\abs.m

matlabroot MATLAB
>> matlabroot
ans =
D:\MATLAB6p1

diary()
diary ('file')
%
diary off
% diary
diary on
% diary

1.4.4 MATLAB
MATLAB
MATLAB exit
MATLAB quit
MATLAB

1.5
1.3 MATLAB
MATLAB
(Command Window)
a=[1 2 3; 4 5 6;7 8 9];
b=[1 1 1;2 2 2;3 3 3 ];
c='';
d=a+b*i
(Workspace Browser)

1.22

d(Array Editor) 1.23

1.23

(Command History) 1.24


Create M-File M

1.24

M (EditorDebugger) 1.25
Savec:\MyDir\Ex0103.m

1.25 M

(Current Directory Browser)c:\MyDir


Ex0103.mEx0103
save Ex0103
Ex0103.mat 1.26

1.26

exit MATLAB
MATLAB Ex0103
MATLAB
MATLAB FileSet Path
c:\MyDir
Ex0103

1.27

Ex0103.matFileImport Data
c:\MyDir\ Ex0103.mat
>> type Ex0103
a=[1 2 3; 4 5 6;7 8 9];
b=[1 1 1;2 2 2;3 3 3 ];
c='';
d=a+b*i

MATLAB

MATLAB

2.1
2.1.1

(uint8uint16uint32uint64)(int8int16int32int64)

2.1.2
1.


10-30910309

-25.672.56e-56( 2.5610-56)4.68e204( 4.6810204)


2.
MATLAB
11
1n n1
00
([])
n
3.
MATLAB ij

z=a+b*i z=a+b*j
z=a+bi z=a+bj( b )
z=r*exp(i*theta)
z
a=real(z)
%
b=imag(z)
%

r=abs(z)
%
theta=angle(z)
%

z a=r*cos()
z b=r*sin()
z r = a 2 + b 2
z theta=arctg(b/a)
a=1-2*i
a =
1.0000 - 2.0000i
real(a)
ans =
1
imag(a)
ans =
-2
abs(a)
ans =
2.2361
angle(a)*180/pi

ans =
-63.4349

2.1.3
1.
aA
63 63 MATLAB6.5
31

(%)6ABCAB%C
( ifwhile )
2.

MATLAB MATLAB
2.1

ans

pi

eps

flops

inf

1/0

NaN nan

0/0/0

i j

i=j=

nargin

nargout

realmin

realmax

z MATLAB ans
2*pi
ans =
6.2832

2.2
MATLAB

2.2.1
(1)
(2)
(3)
(4)

([])

1.
% [ ],,
c=[1 2;3 4;5 3*2]
c =
1

c=[1 2
3 4
5 6]
1

2.
(1) from:step:to
from:to
from:step:to

fromstep to
step step=1
step step>0 from>to step<0 from<to
2.1from:step:to
x1=2:5
x1 =
2

x2=2:0.5:4
x2 =
2.0000

2.5000

3.0000

3.5000

x3=5:-1:2
x3 =
5

x4=2:-1:3

2
%

x4 =
Empty matrix: 1-by-0
x5=2:-1:0.5
x5 =
2

x6=[1:2:5;1:3:7]

x6 =
1

(2) linspace logspace


linspace(a,b,n)

4.0000

abn
a b n n 100
logspace linspace

logspace (a,b,n)

abn n 50
10a 10b n
2.2 linspace logspace
x1=linspace(0,2*pi,5)
% 0 2*pi 5
x1 =
0

1.5708

3.1416

4.7124

6.2832

% 1 100 3

x2=logspace(0,2,3)
x2 =
1

10

100

3.
MATLAB 2.2
2.2

zeros(m,n)

mn

zeros(2,3)

ans =

ones(m,n)

mn

ones(2,3)

ans =

1
1

rand(m,n)

rand(2,3)

ans =

0.9501

0.6068

0.8913

0.2311

0.4860

0.7621

-0.4326

0.1253

-1.1465

-1.6656

0.2877

1.1909

0.0
1.0
randn(m,n)

randn(2,3)
ans =

magic(N)

magic(3)

ans =

)
eye(m,n)

mn

eye(3)

ans =

zerosonesrandrandn eye n nn ;
eye(m,n) m n 0
2.3 eye
X1=eye(2,3)
X1 =
1

X2=eye(3,2)
X2 =
1

4. MAT
loadFileImport Data MAT
5. M
M MATLAB
MATLAB M

2.2.2

1.
(1)
mn a
i j a(i,j)

z (i,j)(m,n)
MATLAB
z (i,j)(m,n) MATLAB
0
a=[1 2;3 4;5 6]
a =
1

a(3,3)

% a(3,3)

??? Index exceeds matrix dimensions.


% a(3,3)

a(3,3)=9
a =
1

(2)

mn a a(i,j) s= (i-1)mj
2.

(1)
a 2.2
13 23
a([1 3],[2 3])
ans =
2

13 123
13 23
a(1:3,2:3)
ans =

13 3
a(:,3)
ans =
0
0
9

13 3 end
3
a(1:3,end)
ans =
0
0
9

(2)
1326
a([1 3;2 6])
ans =
1

(3)

0 1
a(L1,L2) L1L2 L1L2 0

2.5 a 2.2
l1=logical([1 0 1])
% l1
l1 =
1

l2=logical([1 1 0])

% l2

l2 =
1
a(l1,l2)
ans =

0
% 13 12

2.5
b=a>1
% b
b =
0

a(b)

ans =
3
5
2
4
6
9

3.
a(i,j)=b a b a

clear a
% 1
a(1:2,1:3)=[1 1 1;1 1 1]
a =
1

a(s)=bb a
% 56
a(5:6)=[2 3]
a =
1

a(:)=b a b a

a=[1 2;3 4;5 6]


a =
1

b=[1 2 3;4 5 6]
b =
1

6
% a

a(:)=b
a =
1
4
2

5
3
6

4.
([])
a=[1 2 0;3 4 0;5 6 9]
a =
1

9
%

a(:,3)=[]
a =
1

6
%

a(1)=[]
a =
3
a=[]

6
%

a =
[]

5.
MATLAB []
a=[1 2 0;3 4 0;5 6 9]
a =

9
% 63

[a;a]
ans =
1

a=[1 2 0;3 4 0;5 6 9]


[a a]

% 36

ans =
1

a=[1 2 0;3 4 0;5 6 9]


[a(1:2,1:2) 10*a(1:2,2:3)]

ans =
1

20

40

6.
a=
1
2
3
4
5
6

0
0
9

2.3

triu(X)

tril(X)

triu(a)

ans =

tril(a)

ans =
1

flipud(X)

flipud(a)

fliplr(X)

fliplr(a)

flipdim(X,dim)

rot90(X)

flipdim(a,1)

ans =

ans =

ans =

dim=1

dim=2

rot90(a)

900

ans =

2.2.3
MATLAB
()
() ASCII
clear
str1='Hello'
str1 =
Hello
str2='I like ''MATLAB'''

str2 =
I like 'MATLAB'
str3='!'
str3 =
!

1.
whos
Name

Size

Bytes Class

str1

1x5

10 char array

str2

1x15

30 char array

str3

1x3

6 char array

Grand total is 23 elements using 46 bytes

2.
length()
double ASCII (ASCII 32)
char ASCII
class ischarclass char
ischar 1
strcmp(x,y) x y 1 0

findstr(x,x1) x x1
deblank(x )
MATLAB ASCII

%
length(str1)
ans =
5
% ASCII

x1=double(str1)
x1 =
72

101

108

108

111
%

x2=str1+1
x2 =
73

102

char(x1)
ans =
Hello
char(x2)

109

109

112
% ASCII

ans =
Ifmmp
class(str1)

ans =
char
class(x1)
ans =
double
ischar(str1)
ans =
1

3.
(1)
,
clear
str1='Hello';
str2='I like ''MATLAB''';
str3='!'
%
str4=[str1,'! ',str2]
str4 =
Hello! I like 'MATLAB'

(2)

() MATLAB
str5=[str1;str3]
??? Error using ==> vertcat
All rows in the bracketed expression must have the same
number of columns.
str5=[str1;str3,' ']
str5 =
Hello
!

% str3

(3) str2matstrvcat char


str2matstrvcat char

str6=str2mat(str1,str2,str3)
str6 =
Hello
I like 'MATLAB'
!
str7=char(str1,str2,str3)
str7 =
Hello
I like 'MATLAB'
!
str8=strvcat(str1,str2)
str8 =
Hello
I like 'MATLAB'
whos
Name

Size

Bytes Class

str1

1x5

10 char array

str2

1x15

30 char array

str3

1x3

6 char array

str4

1x22

44 char array

str5

2x5

20 char array

str6

3x15

90 char array

str7

3x15

90 char array

str8

2x15

60 char array

Grand total is 186 elements using 350 bytes

5.
eval MATLAB

str9='a=2*5'
str9 =
a=2*5

eval(str9)
a =
10

6.
disp (;)
disp(' 2*2 a')
2*2 a
disp(str1)
Hello

2.2.4

1.
a=
1
4
7

2
5
8

3
6
9
2.4

det(X)

det(a)

ans =
0

rank(X)

rank(a)

ans =
2

inv(X)

X det(X)

inv(a )

Warning:

is

close

to

singular or badly scaled.

X-1
X X-1

Matrix

Results

may

be

inaccurate. RCOND = 1.541976e-018.


ans =

1.0e+016 *

[v,d]=eig(X)

[v,d]=eig(a)

-0.4504

0.9007

-0.4504

0.9007

-1.8014

0.9007

-0.4504

0.9007

-0.4504

v =

-0.2320

-0.7858

0.4082

Xv=vd

-0.5253

-0.0868

-0.8165

-0.8187

0.6123

0.4082

v d

d =
16.1168
0
0

diag(X)

diag(a)

-1.1168

-0.0000

ans =
1
5
9

[l,u]=lu(X)

[l,u]=lu(a)

l =

0.1429

1.0000

0.5714

0.5000

1.0000

7.0000
0
0

mn X

[q,r]=qr(a)

8.0000
0.8571
0

9.0000
1.7143
0.0000

q =

-0.1231

0.9045

0.4082

-0.4924

0.3015

-0.8165

-0.8616

-0.3015

0.4082

q X

r =

n m

-8.1240

0
0

[u,s,v]=svd(X)

1.0000

u =

[q,r]=qr(X)

mn X

[u,s,v]=svd(a)

-9.6011 -11.0782
0.9045
0

1.8091
-0.0000

u =

-0.2148

0.8872

0.4082

u,v nn

-0.5206

0.2496

-0.8165

mm s

-0.8263

-0.3879

0.4082

mn

s =
16.8481

n m

1.0684

0.0000

v =
-0.4797

-0.7767

-0.4082

-0.5724

-0.0757

0.8165

-0.6651

0.6253

-0.4082


det(a)=0 det(a) inv(a)
( cond)
MATLAB
a=[1 2 3;4 5 6;7 8 9]
a =
1

inv(a)
Warning: Matrix is close to singular or badly scaled.
Results may be inaccurate. RCOND = 1.541976e-018.
ans =
1.0e+016 *
-0.4504

0.9007

-0.4504

0.9007

-1.8014

0.9007

-0.4504

0.9007

-0.4504

2.
(1)
z A B
z AB
(2) *
z A B
z .* A B A B

x1=[1 2;3 4;5 6];


x2=eye(3,2)
x2 =
1

0
%

x1+x2
ans =
2

x1.*x2
ans =
1

0
% x1 x2

x1*x2

??? Error using ==> *


Inner matrix dimensions must agree.
x3=eye(2,3)
x3 =
1

0
%

x1*x3
ans =
1

(3)
\/
A\B=A-1*B
A/B=A*B-1
A-1 inv(A)

A.\BA./B
A B
2x 1 x 2 + 3x 3 = 5

2.12 3x 1 + x 2 5x 3 = 5
4x x + x = 9
2
3
1

AX=B

2 1 3
5
A = 3 1 5 B = 5
4 1 1
9

A=[2 -1 3;3 1 -5;4 -1 1]

A =
2

-1

-5

-1

B=[5;5;9]
B =
5
5
9
X=A\B
X =
2
-1
0

z A*X=B mn A m n

z n=mA A\Binv(A)*B
z m > nX=inv(A*A)*(A*B)
z m < X n-m X=inv(A*A)*(A*B)
(4)
A^B A
A
B A B
B A |B|
B
B A A=W*D/WD A^B=W*D^B/W
A
B A A=W*D/WD A^B=W*diag(D.^B)/W
A.^B
A B A(i,j) B
A B A B A(i,j) B(i,j)
A B A^ B(i,j) i j
2.13
x1=[1 2;3 4];
x2=eye(2)
x2 =
1

x1/x2
ans =
1

4
%

inv(x1)
ans =
-2.0000

1.0000

1.5000

-0.5000
%

x1\x2
ans =
-2.0000

1.0000

1.5000

-0.5000
%

x1./x2

Warning: Divide by zero.


(Type "warning off MATLAB:divideByZero" to suppress this warning.)
ans =
1

Inf

Inf

4
%

x1.\x2
ans =
1.0000
0

0
0.2500
%

x1^2
ans =
7

10

15

22

x1^-1

%-1 inv

ans =
-2.0000

1.0000

1.5000

-0.5000

x1^0.2

ans =
0.8397 + 0.3672i

0.2562 - 0.1679i

0.3842 - 0.2519i

1.2239 + 0.1152i
%

2^x1
ans =
10.4827

14.1519

21.2278

31.7106
%

2.^x1
ans =
2

16
%

x1.^x2
ans =
1

3.

A A A

A. A A
2.14
x1=[1 2;3 4];
x2=eye(2);
x3=x1+x2*i
x3 =
1.0000 + 1.0000i
3.0000
x3'

2.0000
4.0000 + 1.0000i

ans =
1.0000 - 1.0000i
2.0000
x3.'

3.0000
4.0000 - 1.0000i

ans =
1.0000 + 1.0000i
2.0000

3.0000
4.0000 + 1.0000i

4.
MATLAB 2.5
2.5

abs

rat

sqrt

mod

real

round

4 5

imag

fix

conj

floor

sin

ceil

cos

sign

tan

rem

asin

exp

acos

log

atan

log10

10

atan2

pow2

sinh

bessel

cosh

gamma

tanh

2.15
t=linspace(0,2*pi,6)
t =
0

1.2566

2.5133

3.7699

5.0265

6.2832

-0.5878

-0.9511

-0.0000

y=sin(t)
y =
0

0.9511

0.5878

y1=abs(y)
y1 =
0

0.9511

1-exp(-t).*y

0.5878

0.5878

0.9511

0.0000

ans =
1.0000

0.7293

0.9524

1.0136

1.0062

1.0000

S AB
2.6

A+B

A+B

A-B

A-B

S.*B

S B

S*B

A.*B

A*B

S./B

S B

S\B

B S

A./B

A B

A/B

A B A B

B.\A

B\A

A B()

A.^S

A S

A^S

A S

A^S

S A

A.^S

S A

S.^B

S^B

B S

A.

conj(A)

expm(A)

exp(A)

e A

log(A)

logm(A)

sqrt(A)

sqrtm(A)

f(A)

funm(A,
FUN)

funm(A,FUN) A FUN
5.
(1)

<<=>>= = =() ~=()

(1)(0)
0
1

0 1
<<= >>=
= = ~=
(2)

&()|()~() xor()
&&() 1()

|| () 1()
1()

0 (1)0 (0) 0 1
2.7
2.7
a

a &b

a| b

~a

xor(a,b)

01

a=0;b=5; c=10;
(a~=0)&&(b<c)
ans =
0
(a~=0)||(b<c)
ans =
1

2.16 y1 2.3
t=linspace(0,3*pi,10);
y=sin(t)
%
y =
Columns 1 through 6
0

0.8660

0.8660

0.0000

-0.8660

-0.8660

Columns 7 through 10
-0.0000

0.8660

0.8660

0.0000

t1=(t<pi)|(t>2*pi)
t1 =
1
y1=t1.*y
y1 =

% 0 23

Columns 1 through 6
0

0.8660

0.8660

Columns 7 through 10
0

0.8660

0.8660

0.0000

(3)
MATLAB (1)(0)

a=
b=
1
Inf
0
1
0
2
1
0
2.8

all(A)

any(A)

isequal(A,B)

isempty(A)

isfinite(A)

isinf(A)

isnan(A)

isnumeric(A)

A00
1

all(a)

ans =
0

A0 any(a)
1

ans =

AB
1

ans =

isequal(a,b)

ans =

A1

ans =

ANAN
1

isfinite(a)

isinf(a)

isnan(a)

isnumeric(a)

ans =
0

ans =
0

ans =
1

isreal(A)

A1 isempty(a)
0

A1

A isreal(a)
1

ans =
1

isprime(A)

A1

isspace(A)

A1

find(A)

A0

isprime(b)

isspace(a)

find(b)

ans =
0

ans =
0

ans =
2
3

6.
MATLAB
'()^().'().^()
~()
*()/()\().*()./().\()
+-()
: ()
<<=>>=~=
&()
|()
&&()
||()

2.2.5
1.
(1)
a(:,:,2)=[1 2;3 4]

b=[1 1;2 2]

%
%

b =
1

b(:,:,2)=5
b(:,:,1) =
1

b(:,:,2) =
5

(2) oneszerosrand randn


rand
rand(2,4,3)
ans(:,:,1) =
0.9501

0.6068

0.8913

0.4565

0.2311

0.4860

0.7621

0.0185

0.8214

0.6154

0.9218

0.1763

0.4447

0.7919

0.7382

0.4057

0.9355

0.4103

0.0579

0.8132

0.9169

0.8936

0.3529

0.0099

ans(:,:,2) =

ans(:,:,3) =

(3)

cat(,p1,p2,)
p1p2

repmat(p)
repmat(p, )
p

reshape(p)
reshape(p, )
p

2.18
a=[1 2;3 4];
b=[1 1;2 2];
c= cat(2,a,b)
% c
c =
1

cat(3,a,b)
ans(:,:,1) =
1

ans(:,:,2) =
1

repmat(a,[2 2 2])

% a

ans(:,:,1) =
1

ans(:,:,2) =
1

reshape(c,[2 2 2])

% 2 2 2

ans(:,:,1) =
1

ans(:,:,2) =
1

2.

ndims (p)

[m,n,]=size(p)
%
m=size(p,x)
%
p m n
x=1 ()x=2 ()

length(p)
length(p) max(size(p))
2.19
a=[1 2;3 4;5 6]

a =
1

ndims(a)

ans =
2
size(a)

ans =
3

size(a,2)

ans =
2
length(a)

ans =
3

2.3
0 0
(Sparse Matrix)

2.3.1
0

1. sparse
sparse

sparse(i,j,s,m,n)
%
sparse(p)
% p
ij 0 s 0 mn s
ijs s(k) i(k) j(k)
p
2.20
a=eye(3);
a(4,:)=[-5 -2 -3]
a =
1

-5

-2

-3

b=sparse(a)

b =
(1,1)

(4,1)

-5

(2,2)

(4,2)

-2

(3,3)

(4,3)

-3

c=sparse([1 4 2 4 3 4],[1 1 2 2 3 3],[1 -5 1 -2 1 -3])

% b

c =
(1,1)

(4,1)

-5

(2,2)

(4,2)

-2

(3,3)

(4,3)

-3

sparse full

full(p)
% p
2.20
full(b)
ans =
1

-5

-2

-3

2. spdiags
spdiags

spdiags(D,k,m,n)
D k (0
1 1 )m
n
2.20 spdiags
D=[3 2 9;2 4 9;1 1 4]
D =
3

d=[0 1 2];
s=spdiags(D,d,4,3)

% 4 3

s =
(1,1)

(1,2)

(2,2)

(1,3)

(2,3)

(3,3)

full(s)
ans =
3

s D
3 2 12 4 12
9 9 49 9
3. spconvert
load spconvert (*.dat)
save load MAT
MAT

spconvert (Filename)
2.20 load spconvert
ASCspr.dat
111
4 1 -5
221
4 2 -2
331
4 3 -3
1 2 3
load spr.dat
%
b= spconvert(spr)
b=
(1,1)
1
(4,1)
-5
(2,2)
1
(4,2)
-2
(3,3)
1

(4,3)
save spr b
clear
load spr b
b
b=
(1,1)
(4,1)
(2,2)
(4,2)
(3,3)
(4,3)

-3
% spr.mat
% spr.mat

1
-5
1
-2
1
-3

2.3.2
MATLAB
nnz
nonzeros
nzmax nnz>nzmax MATLAB
nzmax
spy
2.20 2.4 spy

2.4

nnz(b)

ans =
6
nonzeros(b)

ans =
1
-5
1
-2
1
-3
nzmax(b)
ans =
6
spy(b)

2.3.3

+
-
*
\ &
.*
cat[]

2.4
p1(x)= x3+21x2+20x
p1=[1 21 20 0]

% 0

2.4.1
1.
polyval

polyval(p,s)
p , s
2.21 p(x)= x3+21x2+20x
p1=[1 21 20 0];
polyval(p1,2)
% x=2
ans =
132
x=0:0.5:3;

% x

polyval(p1,x)
ans =
0

15.3750

42.0000

80.6250

132.0000

196.8750

276.0000

2.
roots

r=roots(p)
p r
roots poly

p=poly (r)
2.21 p1(x)= x3+21x2+20x
roots(p1)
%
ans =
0
-20
-1
poly([0;-20;-1])

ans =
1

21

20

3.
s poly
roots

p=poly (s)
s p
2.21
s=[1 2;3 4]
s =
1

p2=poly(s)

p2 =
1.0000
roots(p2)

-5.0000

-2.0000
%

ans =
5.3723
-0.3723

p2=x2-5x-2 s 5.3723 -0.3723 s


eig s
4.
residue
r
r
B(s)
= 1 + 2 +
A(s) s p1 s p 2

rn
s pn

+ k(s)

[r,p,k]=residue(b,a)
b a r [r1 r2 rn]p [p1
p2 pn]k
100(s + 2)
2.21

s(s + 1)(s + 20)


p1=[1 21 20 0];
p3=[100 200];
[r,p,k]=residue(p3,p1)
r =
-4.7368
-5.2632
10.0000
p =
-20
-1
0
k =
[]

100(s + 2)
4.7368 + 5.2632 + 10
s(s + 1)(s + 20)
s + 20
s +1
s

2.4.2
1.

p=conv(pl,p2)
p p1 p2

[q,r]=deconv(pl,p2)

p1 p2 q
r
2.22 s(s + 1)(s + 20)
a1=[1 0];
% s
a2=[1 1];
% s+1
a3=[1 20];
% s+20
p1=conv(a1,a2)
p1 =
1

p1=conv(p1,a3)

% s(s+1)(s+20)

p1 =
1

21

20

[p2,r]=deconv(p1,a3)

p2 =
1

r =
0

conv(p2,a3)+r

%*+

ans =
1

21

20

2.
polyder
MATLAB [p./length(p):-1:1,k]
k
2.22
p4=polyder(p1)
%
p4 =
3

42

20

s=length(p4):-1:1
s =
3

p1=[p4./s,0]

%, k=0

p1 =
1

21

20

p4(x)= 3x2+42x+20 p1(x)= x3+21x2+20x

2.4.3
1.
polyfit
2

f(x i ) y i

f(x)

i =1

p=polyfit(x,y,n)
xy N n p
p n+1
2.23 y1=2x13-x12+5x1+10
2.5

2.5

x1=1:10;
p=[2 -1 5 10];
y0=polyval(p,x1)
y0 =
Columns 1 through 6
16

32

70

142

1432

1960

Columns 7 through 10
682

1010

p1=polyfit(x1,y0,1)

p1 =
204.8000 -522.4000
p2=polyfit(x1,y0,2)
p2 =

260

436

32.0000 -147.2000 181.6000


p3=polyfit(x1,y0,3)

p3 =
2.0000

-1.0000

5.0000

10.0000

2.

1.
interp1

yi=interp1(x,y,xi,method)
xy xi x yi y
method linear()nearest
splinecubic
2.23 9.5 2.6

2.6

x1
y0
y01=interp1(x1,y0,9.5)

x1 =
1

10

y0 =
Columns 1 through 6
16

32

70

142

1432

1960

Columns 7 through 10
682
y01 =

1010

260

436

1696
y02=interp1(x1,y0,9.5,'spline')

y02 =
1682

2.
interp2

zi=interp2(x,y,z,xi,yi,method)
method linear()nearest
cubic

2.5
MATLAB (Cell Array)(Structure Array)

2.5.1
1.
(Cell)

(1) {}
2.24{}
clear
A={'This is the first Cell.',[1 2;3 4];eye(3),{'Tom','Jane'}}
A =
[1x23 char ]

[2x2 double]

[3x3 double]

{1x2 cell }

whos
Name

Size

2x2

Bytes Class
524 cell array

Grand total is 49 elements using 524 bytes

A(1,1)A(1,2)A(2,1)
A(2,2)
(2)
2.24
B(1,1)={'This is the second Cell.'}

B =
'This is the second Cell.'
B(1,2)={5+3*i}
B =
[1x24 char]

[5.0000+ 3.0000i]

B(1,3)={[1 2;3 4; 5 6]}


B =
[1x24 char]

[5.0000+ 3.0000i]

[3x2 double]

(3)
2.24
C{1,1}='This is the third Cell.';
C{2,1}=magic(4)
C =
'This is the third Cell.'
[4x4 double]

2.
MATALB
2.24 A
A
A =
[1x23 char ]

[2x2 double]

[3x3 double]

{1x2 cell }

(1) celldisp
celldisp(A)
A{1,1} =
This is the first Cell.
A{2,1} =
1

A{1,2} =
1

A{2,2}{1} =
Tom
A{2,2}{2} =
Jane
celldisp(B)

B{1} =
This is the second Cell.
B{2} =
5.0000 + 3.0000i
B{3} =
1

celldisp(C)
C{1} =
This is the third Cell.
C{2} =
16

13

11

10

12

14

15

{}A{2,2}{1} 2 2
1
(2) cellplot
2.24 cellplot 2.7
cellplot(A)

2.7

3.
(1)
2.24 A(1,2)
x1=A{1,2}
% A(1,2)
x1 =
1

x2=A{1,2}(2,2)

% A(1,2)

x2 =
4

x1 x2
(2)
x3=A(1,2)
x3 =
[2x2 double]

x3
(3) deal
[x4,x5,x6]=deal(A{[2,3,4]})
x4 =
1

x5 =

x6 =
'Tom'

'Jane'

2.5.2
(Structure)(Fields)
1.
(1)
2.25
ps(1).name=' 1'
ps =
name: ' 1'
ps(1).color='red'
ps =
name: ' 1'
color: 'red'
ps(1).position=[0,0,300,300]
ps =
name: ' 1'
color: 'red'
position: [0 0 300 300]

ps(2).name=' 2';
ps(2).color='blue';
ps(2).position=[100,100,300,300]
ps =
1x2 struct array with fields:
name
color
position

ps ps(1) ps(2)namecolor position


(2) struct
2.25 struct
ps(1)=struct('name','

1','color','red','position',[0,0,300,300]);
ps(2)=struct('name','

2','color','blue','position',[100,100,300,300])
ps =
1x2 struct array with fields:
name
color
position

2.
(1) .
2.25
x1=ps(1)
x1 =
name: ' 1'
color: 'red'
position: [0 0 300 300]
x2=ps(1).position
x2 =
0

300

300

x3=ps(1).position(1,3)
x3 =
300

x1 x2 x3
(2) getfield
x4=getfield(ps,{1},'color')
x4 =

red
x5=getfield(ps,{1},'color',{1})
x5 =
r

(3) setfield
ps=setfield(ps,{1},'color','green');
ps(1)
ans =
name: ' 1'
color: 'green'
position: [0 0 300 300]

3.
(1) fieldnames
x6=fieldnames(ps)
x6 =
'name'
'color'
'position'

x6 2.8

2.8

(2)
[]
all_x=[ps.name]
all_x =
1 2

cat
cat(1,ps.position)
%
ans =

0
100

0
100

300

300

300

300

cat(2,ps.position)

ans =
0

300

300

cat(3,ps.position)

100

100

300

300

ans(:,:,1) =
0

300

300

ans(:,:,2) =
100

100

300

300

2.6

2.6.1

43 a
2.9

2.9

5.30

13.00

0.40

5.10

11.80

-1.70

3.70

8.10

0.60

1.50

7.70

-4.50

MATLAB 2.10
2.10

max(X)

5.3000

13.0000

0.6000

min(X)

1.5000

7.7000

-4.5000

mean(X)

3.9000

10.1500

-1.3000

3.9000

10.1500

-1.3000

std(X)

(mean)

median(X)

4.4000

9.9500

-0.6500

var(X)

3.0667

7.0167

5.6333

3.0667

4.0867

3.0667

4.0867

7.0167

2.7100

3.0667

2.7100

5.6333

1.0000

0.8810

0.7378

0.8810

1.0000

0.4310

0.7378

0.4310

1.0000

1.5000

7.7000

-4.5000

3.7000

8.1000

-1.7000

5.1000

11.8000

0.4000

5.3000

13.0000

0.6000

C=cov(X)

S=corrcoef(X)

S(i,j)=C(i,j)/ C (i, i)C ( j, j )


x y

sort(a,1)=
[S,k]=sort(X,n)

n k S

2.6.2
MATLAB
2.11
a
a=
5.3000 13.0000
0.4000
5.1000 11.8000
-1.7000
3.7000
8.1000
0.6000
1.5000
7.7000
-4.5000
2.11

n m

diff(X,m,n)

[fx,fy]=gradient(Z)

sum(X)

cumsum(X,n)

diff(a,1,1) %

-0.2000

-1.2000

-2.1000

-1.4000

-3.7000

2.3000

-2.2000

-0.4000

-5.1000

7.7000

-2.4500 -12.6000

Z xy

gradient(a) %

6.7000

-3.4000 -13.5000

4.4000

-1.5500

6.2000

-3.0000 -12.2000

sum(a)

15.6000

40.6000

-7.5000
-5.2000

5.3000

18.3000

18.7000

cumsum(a,2) %

5.1000

16.9000

15.2000

3.7000

11.8000

12.4000

1.5000

9.2000

4.7000

cumprod(X,n)

5.3000

68.9000

27.5600

cumprod(a,2) %

5.1000

60.1800

-102.3060

3.7000

29.9700

17.9820

1.5000

11.5500

-51.9750

12.2000 30.2500

trapz(X,y)

-3.1500

trapz(a)

x
y

0
cumtrapz(X,y,n)

n y

cumtrapz(a)%

5.2000

12.4000

-0.6500

9.6000

22.3500

-1.2000

12.2000

30.2500

-3.1500

2.26 y = e 0.2 sin(t) t [0 10] y y


2.9
t=0:0.5:10;
y=exp(-0.2).*sin(t)
y =
Columns 1 through 7
0

0.3925

0.6889

0.8167

0.7445

0.4900

0.1155

Columns 8 through 14
-0.2872

-0.6196

-0.8003

-0.7851

-0.5776

-0.2288

0.8100

0.6537

0.3374

-0.0615

0.1761
Columns 15 through 21
0.5379

0.7680

-0.4454
d=[0 diff(y)]

d =
Columns 1 through 7
0

0.3925

0.2964

0.1277

-0.0722

-0.2545

-0.3744

Columns 8 through 14
-0.4027

-0.3324

-0.1807

0.0152

0.2075

0.3489

-0.1563

-0.3163

-0.3989

0.4049
Columns 15 through 21
0.3618

0.2301

0.0420

-0.3839
s1=0.5*cumsum(y)

% 0.5

s1 =
Columns 1 through 7
0

0.1963

0.5407

0.9491

1.3213

1.5663

1.6241

Columns 8 through 14
1.4805

1.1707

0.7705

0.3779

0.0891

-0.0253

0.0628

1.4476

1.6163

1.5856

1.3629

Columns 15 through 21
0.3317

0.7157

1.1207

s2=cumtrapz(t,y)

s2 =
Columns 1 through 7
0

0.0981

0.3685

0.7449

1.1352

1.4438

1.5952

Columns 8 through 14
1.5523

1.3256

0.9706

0.5742

0.2335

0.0319

0.0188

1.2842

1.5320

1.6009

1.4742

Columns 15 through 21
0.1973

0.5237

0.9182

2.9

2.6.3
1.
conv deconv
(2.4.2 )
conv

conv(x,y)
x y x y
conv2
deconv


[q,r]=deconv(x,y)
x=conv(y,q)+r
2.
fft

X=fft(x,N) %
x N x X
N X N N 2
x
fft abs angle
ifft

X=ifft(x,N) %
2.27

0
0
A(n) =
B(n) =
1
1
n
2,3,
10
n
4,5, 9
=
=

A=ones(1,10);
A(1)=0
A =
0

B=ones(1,9);
B([1 2 3])=0
B =
0

C=conv(A,B)

C =
Columns 1 through 13
0

6
Columns 14 through 18
5

N=32;
AF=fft(A,N);
BF=fft(B,N);
CF=AF.*BF;
CC=real(ifft(CF));

1
% 32
%

2.10

C CC
2.10 N 2 length(A)+length(B)-1

2.6.4
()

cross(a,b)
()

dot(a,b)
ab 3
2.28
a=[1 2 3];
b=[4 5 6];
c=cross(a,b)
c =
-3

c=dot(a,b)
c =
32

-3

MATLAB

()
MATLAB (Symbolic Math Toolbox) MATLAB
Maple

3.1
Symbolic Math Toolbox2.1

3.1.1
sym

sym()
%

>> a=sym('sin(2)')
a=
sin(2)

sym

sym(,)
%
dfer 3.1
3.1

( 32 )

() p/qp*q
10^qpi/q2^q sqrt(p)

a=sym('sin(2)')
a =
sin(2)

a=sym(sin(2))
a =
8190223105242182*2^(-53)

3.1
a1=2*sqrt(5)+pi
%
a1 =
7.6137
a2=sym('2*sqrt(5)+pi')

a2 =
2*sqrt(5)+pi
a3=sym(2*sqrt(5)+pi)

a3 =
8572296331135796*2^(-50)
a4=sym(2*sqrt(5)+pi,'d')

a4 =
7.6137286085893727261009189533070
a31=a3-a1

a31 =
0
a5='2*sqrt(5)+pi'

a5 =
2*sqrt(5)+pi

3.1

3.1

3.1.2
sym syms
1. sym

sym(,)
%
positive real
unreal positive realunreal

3.2
syms x y real
%
z=x+i*y;
% z
real(z)
% z x
ans =
x
sym('x','unreal');
real(z)

%
% z

ans =
1/2*x+1/2*conj(x)

xy z

sym()
%
3.2
f1=sym('a*x^2+b*x+c')
f1 =
a*x^2+b*x+c

2. syms

syms(arg1, arg2, ,)
%
syms arg1 arg2 ,
%

syms
sym
3.2 syms
syms a b c x
%
f2=a*x^2+b*x+c
%
f2 =
a*x^2+b*x+c
syms('a','b','c','x')
f3=a*x^2+b*x+c;

abcxf2f3 f1

3.1.3
sym syms
A=sym('[a,b;c,d]')
A =
[ a, b]
[ c, d]

syms
syms a b c d
A=[a b;c d]
A =
[ a, b]
[ c, d]

3.3
A=sym('[a,b;c,d]')
%
A =
[ a, b]
[ c, d]
B='[a,b;c,d]'

B =
[a,b;c,d]
C=[a,b;c,d]

??? Undefined function or variable 'a'.

abcd MATLAB
C=sym(B)
%
C =
[ a, b]
[ c, d]
whos
Name

Size

Bytes Class

2x2

312 sym object

1x9

18 char array

2x2

312 sym object

Grand total is 25 elements using 642 bytes

A 22

3.2

3.2.1

1.
(1)

*
\
/
^

./
.\
.^
.*

.

(2)

~=

= =
1 0
2.
(1)
sincostan sinhcoshtanh atan2
asinacosatan

(2)
sqrtexpexpm
log( ln) log2 log10
(3)
conj real imag abs
MATLAB
(4)

MATLAB diagtriutrilinvdetrank polyexpm


eig

a 11
a 21

a 12

a 22

3.4 A =

syms a11 a12 a21 a22


A=[a11 a12;a21 a22]

A =
[ a11, a12]
[ a21, a22]
det(A)

ans =
a11*a22-a12*a21
A.'

ans =
[ a11, a21]
[ a12, a22]
eig(A)

ans =
[ 1/2*a11+1/2*a22+1/2*(a11^2-2*a11*a22+a22^2+4*a12*a21)^(1/2)]
[ 1/2*a11+1/2*a22-1/2*(a11^2-2*a11*a22+a22^2+4*a12*a21)^(1/2)]

3.5 f=2x2+3x+4 g=5x+6


f=sym('2*x^2+3*x+4')
f =
2*x^2+3*x+4
g=sym('5*x+6')
g =
5*x+6
f+g

ans =
2*x^2+8*x+10
f*g

ans =
(2*x^2+3*x+4)*(5*x+6)

3.2.2
1. Symbolic Math Toolbox
Symbolic Math Toolbox
MATLAB
Maple
VPA Maple
2.
VPA digits vpa

digits(n)
%
n digits
Maple
digits 32

S=vpa(s,n)
% s n
s S n
digits vpa s
digits
3.6 2 5 +
a=sym('2*sqrt(5)+pi')
a =
2*sqrt(5)+pi
digits

Digits = 32
vpa(a)

ans =
7.6137286085893726312809907207421
vpa(a,20)

ans =
7.6137286085893726313
digits(15)
vpa(a)

%
% digits

ans =
7.61372860858937

3. Symbolic Math Toolbox


3.6 2/3
a1 =2/3
%
a1 =
0.6667
a2 = sym(2/3)

a2 =
2/3
a3 =vpa('2/3',32)

%VPA

a3 =
.66666666666666666666666666666667



VPA

a1
formatformat
format long
a1
a1 =
0.66666666666667

3.2.3
1.
sym vpa
VPA
2.
double numeric VPA

N=double(S)
% S N
N=numeric(S)
% S N
3.7 2 5 +
clear
a1=sym('2*sqrt(5)+pi')

a1 =
2*sqrt(5)+pi
b1=double(a1)

b1 =
7.6137
a2=vpa(sym('2*sqrt(5)+pi'),32)
a2 =
7.6137286085893726312809907207421
b2=numeric(a2)

b2 =
7.6137

3.7
b3=eval(a1)
b3 =
7.6137

whos b1b2b3
whos
Name

Size

Bytes Class

a1

1x1

148 sym object

a2

1x1

190 sym object

b1

1x1

8 double array

b2

1x1

8 double array

b3

1x1

8 double array

Grand total is 50 elements using 362 bytes

3.3
3.3.1
1.
MATLAB
i j
x

x x x
x

2. findsym
findsym

findsym(EXPR,n)
%
EXPR n n
EXPR
3.8
f=sym('a*x^2+b*x+c')
f =
a*x^2+b*x+c
findsym(f)

ans =
a, b, c, x
g=sym('sin(z)+cos(v)')
g =
sin(z)+cos(v)
findsym(g,1)

ans =
z

z v x x z

3.3.2
f(x)

f(x)=x3+6x2+11x-6
f(x)=(x-1)(x-2)(x-3)
f(x)=x(x(x-6)+11)-6
3.9
f=sym('x^3-6*x^2+11*x-6')
%
f =
x^3-6*x^2+11*x-6
g= sym('(x-1)*(x-2)*(x-3)')
g =

(x-1)*(x-2)*(x-3)
h= sym(' x*(x*(x-6)+11)-6')

h =
x*(x*(x-6)+11)-6

1. pretty
3.9
pretty(f)

x - 6 x + 11 x - 6

2. collect
3.9
collect(g)
ans =
x^3-6*x^2+11*x-6

xy

f1=sym('x^3+2*x^2*y+4*x*y+6')
f1 =
x^3+2*x^2*y+4*x*y+6
collect(f1,'y')

% y

ans =
(2*x^2+4*x)*y+x^3+6

3. expand
3.9
expand(g)
ans =
x^3-6*x^2+11*x-6

4. horner
3.9
horner(f)
ans =
x*(x*(x-6)+11)-6

5. factor
3.9
factor(f)
ans =
(x-1)*(x-2)*(x-3)

6. simplify
3.9 cos2x-sin2x
y=sym('cos(x)^2-sin(x)^2')
y =
cos(x)^2-sin(x)^2
simplify(y)
ans =
2*cos(x)^2-1

7. simple
simple pretty collectexpandfactorsimplify
radsimpcombinecombine(trig)convert

3.9 simple cos2x-sin2x


simple(y)

simplify:
2*cos(x)^2-1
radsimp:
cos(x)^2-sin(x)^2
combine(trig):
cos(2*x)
factor:
(cos(x)-sin(x))*(cos(x)+sin(x))
expand:

cos(x)^2-sin(x)^2
combine:
cos(2*x)
convert(exp):
(1/2*exp(i*x)+1/2/exp(i*x))^2+1/4*(exp(i*x)-1/exp(i*x))^2
convert(sincos):
cos(x)^2-sin(x)^2
convert(tan):
(1-tan(1/2*x)^2)^2/(1+tan(1/2*x)^2)^2-4*tan(1/2*x)^2/(1+tan(1/2*x)
^2)^2
collect(x):
cos(x)^2-sin(x)^2
ans =
cos(2*x)

cos(2*x)

3.3.3
1. subexpr

subexpr(s,s1)
% s1 s
subexpr

a b
3.10 subexpr

c d
syms a b c d x
s=eig([a b;c d])

s =
[ 1/2*a+1/2*d+1/2*(a^2-2*a*d+d^2+4*b*c)^(1/2)]
[ 1/2*a+1/2*d-1/2*(a^2-2*a*d+d^2+4*b*c)^(1/2)]
subexpr(s,x)

% x

ans =
[ 1/2*a+1/2*d+1/2*(a^2-2*a*d+d^2+4*b*c)^(1/2)]
[ 1/2*a+1/2*d-1/2*(a^2-2*a*d+d^2+4*b*c)^(1/2)]

2. subs
subs

subs(s)
% s
subs(s,new)
% new s
subs(s,old,new)
% new s old
3.10 subs (x+y)2+3(x+y)+5
f=sym('(x+y)^2+3*(x+y)+5')
%
f =
(x+y)^2+3*(x+y)+5
x=5;
f1=subs(f)

% x

f1 =
(5+y)^2+20+3*y
f2=subs(f,'x+y','s')

% s x+y

f2 =
((s))^2+3*((s))+5
f3=subs(f,'x+y',5)

% 5 x+y

f3 =
45
f4=subs(f,'x','z')

% z x

f4 =
((z)+y)^2+3*((z)+y)+5

3.3.4
MATLAB finverse

finverse(f,v)
% v f(v)
v
1.
3.11 tex
f=sym('t*e^x')
%

f =
t*e^x
g=finverse(f)

g =
log(x/t)/log(e)
% t

g=finverse(f,'t')
g =
t/(e^x)

t 't'
syms t
g=finverse(f,t)
2.
3.11 tex ay2+by+c
f=sym('t*e^x');
%
g=sym('a*y^2+b*y+c');
%
h1=compose(f,g)
% f(g(x))
h1 =
t*e^(a*y^2+b*y+c)
h2=compose(g,f)

% g(f(x))

h2 =
a*t^2*(e^x)^2+b*t*e^x+c
h3=compose(f,g,'z')

% f(g(z))

h3 =
t*e^(a*z^2+b*z+c)

3.11 tex y2
f1=sym('t*e^x');
g1=sym('y^2');
h1=compose(f1,g1)
h1 =
t*e^(y^2)
h2=compose(f1,g1,'z')

% f(g(z))

h2 =
t*e^(z^2)
h3=compose(f1,g1,'t','y')
h3 =

% t f(g(z))

y^2*e^x
h4=compose(f1,g1,'t','y','z') % t f(g(z)) z y
h4 =
z^2*e^x
% h5 h4

h5=subs(h3,'y','z')
h5 =
(z)^2*e^x

3.3.5
1.
f(x)MATLAB
sym2poly poly2sym
(1) sym2poly
3.12 2x+3x2+1
f=sym('2*x+3*x^2+1')
f =
2*x+3*x^2+1
sym2poly(f)

ans =
3

f1=sym('a*x^2+b*x+c')
f1 =
a*x^2+b*x+c
sym2poly(f1)
??? Error using ==> sym/sym2poly
Input has more than one symbolic variable.

(2) poly2sym
3.12
g=poly2sym([1 3 2])
% x
g =
x^2+3*x+2
g=poly2sym([1 3 2],sym('y')) %y
g =
y^2+3*y+2

2.
() numden

[n,d]=numden(f)
n d f
1
1
3.13 numden 2
2 + 3s + 2
s + 3s + 2
s
f1=sym('1/(s^2+3*s+2)')
f1 =
1/(s^2+3*s+2)
f2=sym('1/s^2+3*s+2')
f2 =
1/s^2+3*s+2
[n1,d1]=numden(f1)
n1 =
1
d1 =
s^2+3*s+2
[n2,d2]=numden(f2)
n2 =
1+3*s^3+2*s^2
d2 =
s^2

3.4
3.4.1
Symbolic Math Toolbox
limit limit 3.2
3.2 limit

lim f(x)

limt(f)

x 0

lim f(x)

limt(f,x,a)

x a

x0

xa

lim f(x)

limt(f,x,a, left)

x a

lim f(x)

limt(f,x,a, right)

x a

x a

x a +

3.14 1/x 0
f=sym('1/x')
f =
1/x
limit(f)

% x 0

ans =
NaN
limit(f,'x',0)

% x 0

ans =
NaN
limit(f,'x',0,'left')

% 0

ans =
-inf
limit(f,'x',0,'right')

% 0

ans =
inf

NaN
f' (x) = lim
t 0

f(x + t) f(x)

3.14 cos(x)
syms t x
limit((cos(x+t)-cos(x))/t,t,0)
ans =
-sin(x)

3.4.2
diff

diff(f)
% f
diff(f,t)
% f t
diff(f,n)
% f n

diff(f,t,n)
% f t n
2
3.15 f(x)ax +bx+c f(x)
f=sym('a*x^2+b*x+c')
f =
a*x^2+b*x+c
diff(f)

% x

ans =
2*a*x+b
diff(f,'a')

% a

ans =
x^2
diff(f,'x',2) % x
ans =
2*a
% x

diff(f,3)
ans =
0

diff
2x
t2
3.15

x
tsin(x) e

syms t x
g=[2*x t^2;t*sin(x) exp(x)] %
g =
[

2*x,

t^2]

[ t*sin(x),

exp(x)]
% x

diff(g)
ans =
[

2,

0]

[ t*cos(x),

exp(x)]
% t

diff(g,'t')
ans =
[

0,

[ sin(x),
diff(g,2)

2*t]
0]
% x

ans =
[

0,

0]

[ -t*sin(x),

exp(x)]

diff
3.15 diff
x1=0:0.5:2;
y1=sin(x1)
y1 =
0

0.4794

0.8415

diff(y1)

0.9975

0.9093

ans =
0.4794

0.3620

0.1560

-0.0882

3.4.3
int

int(f,t)
% t
int(f,t,a,b)
% t
int(f,t,m,n) % t
t t a b [a,b]m
n [m,n]
MATLAB MATLAB

3.16 cos(x)

cos(x)

f=sym('cos(x)');
int(f)
%
ans =
sin(x)
int(f,0,pi/3)

ans =
1/2*3^(1/2)
int(f,'a','b')

ans =
sin(b)-sin(a)
int(int(f))

ans =
-cos(x)

diff int f
f='cos(x)';
2x
t2
3.16

x
tsin(x) e

syms t x
g=[2*x t^2;t*sin(x) exp(x)] %
g =
[

2*x,

t^2]

[ t*sin(x),

exp(x)]
% x

int(g)
ans =
[

x^2,

t^2*x]

[ -t*cos(x),

exp(x)]
% t

int(g,'t')
ans =
[

2*x*t,

[ 1/2*t^2*sin(x),

1/3*t^3]
exp(x)*t]

int(g,sym('a'),sym('b'))

% x

ans =
[

b^2-a^2,

[ -t*cos(b)+t*cos(a),

t^2*(b-a)]
exp(b)-exp(a)]

3.4.4
1. symsum

symsum(s,x,a,b)
% s
x x s [a,b] x

1
1
1
3.17 1 + 2 + 2 + + 2 + 1+x+x2++xk+
2
3
k
syms x k
s1=symsum(1/k^2,1,10)
% 10
s1 =
1968329/1270080

s2=symsum(1/k^2,1,inf)

s2 =
1/6*pi^2
s3=symsum(x^k,'k',0,inf)

% k

s3 =
-1/(x-1)

2. taylor

taylor (F,x,n)
%
x F F n n
5
1
1
1
3.17 ex 1 + x + x 2 +
x 3 + + x k 1 +
2
23
k!
syms x
s1=taylor(exp(x),8)
% 8

s1 =
1+x+1/2*x^2+1/6*x^3+1/24*x^4+1/120*x^5+1/720*x^6+1/5040*x^7
s2=taylor(exp(x))

% 5

s2 =
1+x+1/2*x^2+1/6*x^3+1/24*x^4+1/120*x^5

3.5
3.5.1 (Fourier)
fourier int fourier ifourier

1. fourier

Ffourier(f,t ,w)
% f(t) fourier F
F w w w
f t t x
2. fourier

f=ifourier (F)
% F fourier f(t)
f=ifourier (F,w,t)
ifourier fourier

3.18 f(t)=

1
fourier F F fourier
t

syms t w
F=fourier(1/t,t,w)

%fourier

F =
i*pi*(Heaviside(-w)-Heaviside(w))
f=ifourier(F,t)

%fourier

f =
1/t
f=ifourier(F)

%fourier x

f =
1/x
1 t 0
Heaviside(t)
Heaviside
0 t < 0

3.18 fourier
fourier(sym('Heaviside(t)'))
ans =
pi*Dirac(w)-i/w

Dirac

3.5.2 (Laplace)
1. Laplace

F=laplace(f,t,s)
% f Laplace F
F s s F 's'f t
t 't'
3.19 sin(at) Laplace
syms a t s
F1=laplace(sin(a*t),t,s)
% sinat Laplace
F1 =
a/(s^2+a^2)
F2=laplace(sym('Heaviside(t)'))
F2 =
1/s

2. Laplace

% Laplace


% F Laplace f

filaplace(F,s,t)
3.19

1
1 Laplace
s+a

syms s a t
f1=ilaplace(1/(s+a),s,t)

% 1/s+a Laplace

f1 =
exp(-a*t)
f2=ilaplace(1,s,t)

% 1 Laplace

f2 =
Dirac(t)

3.5.3 Z
1. ztrans

Fztrans(f,n, z)
% f Z F
F z n 'n'
z 'z'
3.20 e-at Z
syms a n z t
Fz1=ztrans(sym('Heaviside(t)'),n,z)
% Z
Fz1 =
Heaviside(t)*z/(z-1)
Fz2=ztrans(sym('Dirac(t)'),n,z)

% Z

Fz2 =
Dirac(t)*z/(z-1)
Fz3=ztrans(exp(-a*t),n,z)

% e-at Z

Fz3 =
exp(-a*t)*z/(z-1)

2. iztrans

fiztrans(F,z,n)
% F z f
3.20 Z e-at Z
syms n z t
f1=iztrans(Fz1,z,n)
f1 =

Heaviside(t)
f2=iztrans(Fz2,z,n)
f2 =
Dirac(t)
f3=iztrans(Fz3,z,n)
f3 =
exp(-a*t)

3.6
3.6.1
MATLAB solve

solve(eq,v)
%
solve(eq1, eq2,v1,v2,) %
eq
eq=0 v

3.21 ax2+bx+c=0 sinx=0


f1=sym('a*x^2+b*x+c')
%
f1 =
a*x^2+b*x+c
solve(f1)

% x

ans =
[ 1/2/a*(-b+(b^2-4*a*c)^(1/2))]
[ 1/2/a*(-b-(b^2-4*a*c)^(1/2))]
f2=sym('sin(x)')
f2 =
sin(x)
solve(f2,'x')
ans =
0

sinx=0 0

x 2 + 2x + 1 = 0

3.22 x + 3z = 4
y * z = 1

eq1=sym('x^2+2*x+1');
eq2=sym('x+3*z=4');
eq3=sym('y*z=-1');
[x,y,z]=solve(eq1,eq2,eq3)

% x,y,z

x =
-1
y =
-3/5
z =
5/3

S=solve(eq1,eq2,eq3)

S=
x: [1x1 sym]
y: [1x1 sym]
z: [1x1 sym]

3.6.2
MATLAB dsolve

dsolve(eq,con,v)
%
dsolve(eq1,eq2,con1,con2,v1,v2) %
eqconv
x t
y eq
y
y n

dy
dy

Dy
dx
dt
dn y
dx n

dn y
dt n

Dny

'con''y(a)=bDy(c)=d'
C1C2

3.23 x

d2y
2

dy
= x 2 y(1)=0y(0)=0
dx

dx
y=dsolve('x*D2y-3*Dy=x^2','x')

y =
-1/3*x^3+C1+C2*x^4
y=dsolve('x*D2y-3*Dy=x^2','y(1)=0,y(5)=0','x')

y =
-1/3*x^3+125/468+31/468*x^4

3.24

dx
dy
= y,
= x
dt
dt

[x,y]=dsolve('Dx=y,Dy=-x')
x =
cos(t)*C1+sin(t)*C2
y =
-sin(t)*C1+cos(t)*C2

tC1C2

[x,y]=dsolve('Dx=y,Dy=-x','t')

3.7
3.7.1
1. ezplot ezplot3
ezplot ezplot3

ezplot(F,[xmin,xmax],fig)
%
F [xmin,xmax][2
2]fig
3.25 3.23 y(x) 3.2
y =sym('-1/3*x^3+1/3*x^4')
y =
-1/3*x^3+1/3*x^4
ezplot(y)
ezplot(y,[0,100])

% y [0,100]

3.2 (a) y(x)[-22]

(b) y(x)[0100]

3.26 ezplot3 3.3


x=sym('sin(t)');
z=sym('t');
y=sym('cos(t)');
ezplot3(x,y,z,[0,10*pi],'animate')
% t [0,10*pi]

3.3

2.
MATLAB 3.3
3.3

ezcontour

ezcontour('x*sin(t)',[-4,4])

ezcontourf

ezcontourf('x*sin(t)',[-4,4])

ezmesh

ezmesh('sin(x)*exp(-t)','cos(x)*exp(-t)','x',[0,2*pi])

ezmeshc

ezmeshc('sin(x)*t',[-pi,pi])

ezpolar

ezpolar('sin(t)',[0,pi/2])

ezsurf

ezsurf('x*sin(t)','x*cos(t)','t',[0,10*pi])

ezsurfc

ezsurfc('x*sin(t)','x*cos(t)','t',[0,pi,0,2*pi])

3.7.2
Symbolic Math Toolbox
funtool.m MATLAB funtool
3.4

3.4 (a) Figure No. 1

(b) Figure No. 2

3.4 (c) Figure No. 3

3.8 Maple
MATLAB Maple Maple
2000

3.8.1 Maple
1. maple
maple MATLAB


maple(MapleStatement)

% Maple

maple(fun,arg1,arg2)

% arg1,arg2 Maple

MapleStatement
fun
3.27 Maple discrim
maple('discrim(a*x^2+b*x+c,x)')
ans =
-4*a*c+b^2

syms a b c x
maple('discrim',a*x^2+b*x+c,'x')
ans =
-4*a*c+b^2

MapleStatement Maple mhelp


maple
2. mfun
mfun Maple

mfun('fun',p1,p2,)
'fun'p1,p2,
3.28 gcd
mfun('gcd',20,30)
ans =
10

3.8.2 Maple
1. mfunlist
mfunlist mfun Maple
2. mhelp
mhelp Maple
(1) mhelp index Maple
(2) mhelp index [] Maple
mhelp index 3.4
3.4 Maple

expression

function

3.28 gcd
mhelp gcd

4 MATLAB GUI
MATLAB

4.1
4.1.1 plot
1. plot(x) x
plot MATLAB

plot(x)
% x
plot(x,y)
% x y
x y
4.1 plot(x) 4.1

4.1 (a) x1

x1=[1 2 3]
x1 =
1
plot(x1)

(b) x2

x2=[0 1 0]
x2 =
0

plot(x2)

2. plot(x,y) x y
4.2 y=sin(x) 4.2
x1=0:0.1:2*pi;
y1=sin(x1);
%y1 x1
plot(x1,y1);
x2=[0 1 1 2 2 3 ];
y2=[1 1 0 0 1 1 ];
plot(x2,y2);
axis([0 4 0 2])
% 0-4 0-2

4.2 (a)

(b)

3. plot(x) x
4.3 4.3

4.3 (a) x1

x1=[1 2 3;4 5 6];


plot(x1);
x2=peaks;

(b) x2

% 49*49

plot(x2);

a x1
b peaks 4949 49
4. plot(x,y)
plot(x,y) x y
x y x y x
y x y x y
x y y x y
x y
x y y x

x y x y
4.4 4.4
x1=[1 2 3];
y1=[1 2 3;4 5 6]
y1 =
1

plot(x1,y1)
%
y2=[1 2 ;3 4; 5 6]
y2 =
1

plot(x1,y2)
plot(y1,x1)
plot(y2,x1)
x2=[1 1 1;2 2 2]

4.4 (a) (x1,y1)

(b) (x2,y1)

4.4 ( c) (y1,x1)

(d) (y2,x1)

x2 =
1

plot(x2,y1)

4.4 (e) (x2,y1)

5. plot(z)
4.4 4.4(e)

z1=x2+i*y1
z1 =
1.0000 + 1.0000i

1.0000 + 2.0000i

1.0000 + 3.0000i

2.0000 + 4.0000i

2.0000 + 5.0000i

2.0000 + 6.0000i

plot(z1)

6. plot(x1,y1,x2,y2,)
plot MATLAB
(xi,yi)

4.5 4.5
x=0:0.1:2*pi;
plot(x,sin(x),x,cos(x),x,sin(3*x)) %

4.5

4.1.2
4.1
4.1

Figure No.1

()

()()

()

1 3 MATLAB

2
123

4.1.3
1.
figure

figure(n)
%

Figure No.n
2.
plot subplot

subplot(m,n,k)
%(mn) k
mn k ,
1
4.6 subplot 4.6
x=0:0.1:2*pi;
subplot(2,2,1)
% 2*2
plot(x,sin(x))
subplot(2,2,2)
%
plot(x,cos(x))
subplot(2,2,3)
%
plot(x,sin(3*x))
subplot(224)
%
plot(x,cos(3*x))

4.6

clf
clf

3.
hold

hold on
%
hold off
%
hold
%
hold on
MATLAB
4.7 sinx [0 2] cosx [- ]
4.7(a)
x1=0:0.1:2*pi;
plot(x1,sin(x1))
hold on
x2=-pi:.1:pi;
plot(x2,cos(x2))
02-2
4.

plotyy(x1,y1,x2,y2) %
(x1,y1)(x2,y2)

4.7 plotyy 4.7(b)


plotyy(x1,sin(x1),x2,cos(x2))

plotyy
-2

4.7 (a) hold on

(b) plotyy

4.1.4
plot 4.2
4.2

y(Yellow)

()

()

m(Magenta)

c(Cyan)

-.

r(Red)

--

g(Green)

b(Blue)

w(White)

k(Black)

<

>

plot(x,y,s)
x y s s

4.8 sinx cosx 4.8


x=0:0.1:2*pi;

plot(x,sin(x),'r-.')
hold on
plot(x,cos(x),'b:o')

%
%

4.8

4.1.5
1.
axis 4.3
4.3

axis auto

axis equal

axis manual

axis fill

manual

axis off

axis image

axis on

axis normal

axis ij

axis square

axis xy

axis tight

axis([xmin,xmax,

axis vis3d

ymin,ymax])

xmin<xmax,ymin<ymax

inf -inf

2.
(1) grid

grid on
grid off
grid

%
%
%

MATLAB

(2) box

box on
%
box off
%
box
%

4.9 4.9
x=0:0.1:2*pi;
subplot(2,1,1)
plot(sin(x),cos(x))
axis equal
%
grid on
%
subplot(2,1,2)
plot(x,exp(-x))
axis([0,3,0,2])
%

4.9

3.
(1)

title(s)
%
s
(2)

xlabel(s)
%
ylabel(s)
%
(3)

legend(s,pos)
%
legend off
%
s s1,s2, pos
4.4

4.4

pos

pos

()

legend

(4)

text(xt,yt,s)
%(xt,yt)
4.10 4.10
x=0:0.1:2*pi;
plot(x,sin(x))
hold on
plot(x,cos(x),'ro')
title('y1=sin(x),y2=cos(x)') %
xlabel('x')
%
legend('sin(x)','cos(x)',4)
%
text(pi,sin(pi),'x=\pi')
% pi,sin(pi)

4.10

4.
4.5

\ alpha

\ eta

\ nu

\ upsilon

\ beta

\ theta

\ xi

\ Upsilon

\ epsilon

\ Theta

\ Xi

\ phi

\ gamma

\ iota

\ pi

\ Phi

\ Gamma

\ zeta

\ Pi

\ chi

\ delta

\ kappa

\ rho

\ psi

\ Delta

\ mu

\ tau

\ Psi

\ omega

\ lambda

\ sigma

\ Omega

\ Lambda

\ Sigma

\approx

\oplus

\neq

\leq

\geq

\pm

\times

\div

\int

\exists

\infty

\in

\sim

\forall

\angle

\perp

\cup

\cap

\vee

\wedge

\surd

\otimes

\uparrow

\downarrow

\leftarrow

\leftrightarrow

\oplus

\rightarrow

\updownarrow

4.6

4.6

\fontname{s}

s Times New Roman Courier

\fontsize{n}

n 10(points)

\s

s bf()it()sl()rm()

^{s}

_{s}

4.11 MATLAB y() =

y(t)e jt dt 16

4.11
figure(1)
title('\fontsize{16}y(\omega)=\int^{\infty}_{0}y(t)e^{-j\omegat}dt')

4.11

4.1.6
1. ginput
ginput

[x,y]=ginput(n)

% n (x,y)

n xy

2. gtext
gtext

gtext(s)
%
s
s

4.12 y=sin(x)(0)(20)(20)
2
x=0:0.1:2*pi;
plot(x,sin(x))
[m,n]=ginput(2)
m=
3.1532
6.2984
n=
-0.0029
-0.0088
gtext('2\pi')

% 2

ginput
(0)(20)gtext 2

4.2 MATLAB
4.2.1 plot3
plot3 plot

plot3(x,y,z, 's')
%
plot3(x1,y1,z1, 's1',x2,y2,z2, 's2',) %
xyz xyz xy
z s

4.13 4.12
x=0:0.1:20*pi;
plot3(x,sin(x),cos(x))

4.12

4.2.2
1. meshgrid
MATLAB x m y n
meshgrid xy x-y

[X,Y]meshgrid(x,y)
XY xy
x(1m) y(1n)(nm)
x=[1 2 3 4];
y=[5 6 7];
[xx,yy]=meshgrid(x,y)
xx =
1

yy =

4.14 peaks meshgrid mesh meshgrid

MATLAB peaks 4.13 x y [-3


3] 4949 z xy

z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ...


- 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ...
- 1/3*exp(-(x+1).^2 - y.^2)
x=linspace(-3,3,49);
y=linspace(-3,3,49);
[xx,yy]=meshgrid(x,y) ;
% 49*49
mesh(xx)
% xx
mesh(yy)

xx yy 4949 4.13 xx yy

4.13 (a) xx

(b) yy

zz=3*(1-xx).^2.*exp(-(xx.^2) - (yy+1).^2) ...


- 10*(xx/5 - xx.^3 - yy.^5).*exp(-xx.^2-yy.^2) ...
- 1/3*exp(-(xx+1).^2 - yy.^2);
% peaks
plot3(xx,yy,zz)

4.14 peaks

2.

mesh(z)
%
mesh(x,y,z,c)
z z x z y
xy xy xyz c c
z xyzc

4.14 mesh peaks 4.15


mesh(xx,yy,zz)

4.15 peaks

3.

surf (z)
surf (x,y, z,c)

mesh c
4.14 surf peaks 4.16
surf (xx,yy,zz)

4.16 peaks

4.
meshc meshz
4.14 meshz meshc peaks 4.17
meshz(xx,yy,zz)
meshc(xx,yy,zz)

4.17 (a) peaks

(b) peaks

4.2.3
1
hidden off

hidden off
%
hidden on
%
4.15 4.18
[x,y,z]=peaks;
%peaks
mesh(x,y,z)
%
hidden off
%

4.18 peaks

2
view

view([az,el])
%
view([vx,vy,vz])
%
az el vxvyvz
4.15 peaks 4.19
view(0,0)
view(0,90)
view(-37.5,30)
%

4.19

(a) (0,0) peaks

(b) (0,90) peaks

(0,0)(x,z)(0,90)(x,y)

3
4.15 peaks 4.20
z(10:20,10:20)=nan;
% nan
surf(x,y,z)
%

4.20 peaks

4.2.4
1(colormap)
(1) RGB
RGB RGB 01
4.7
4.7

RGB
RGB

Red()

Green()

Blue()

Black()

White()

Red()

Green()

Blue()

Yellow()

Magenta()

Cyan()

Gray()

0.5

0.5

0.5

Dark red()

0.5

Copper()

0.62

0.4

0.49

0.83

Aquamarine()

4.16

% peaks

peaks;
colormap

size(colormap)
ans =
64

peaks 4.15 colormap 643


RGB

(2)
4.8

hsv

HSV ()

hot

cool

summer

gray

copper

autumn

winter

spring

bone

pink

flag

jet

HSV

prim

643 m3

4.16
colormap hot(8)
% peaks
colormap
ans =
0.3333

0.6667

1.0000

1.0000

0.3333

1.0000

0.6667

1.0000

1.0000

1.0000

1.0000

0.5000

1.0000

1.0000

1.0000

hot(8) 83

2
(1)
rgbplot

rgbplot(map)
map 4.8 rgbplot
RGB
colorbar
colorbar
4.17 rgbplot colorbar 4.21
subplot(2,1,1)
rgbplot(cool)
%
subplot(2,1,2)
peaks;
colormap cool
% peaks
colorbar
%

4.21 rgbplot colorbar

rgbplot 064 01colorbar

(2) shading
shading shading
4.9
4.9

shading

shading interp

shading flat

shading faceted

flat
()

4.18 peaks 4.22


subplot(1,2,1)
peaks;
shading interp
subplot(1,2,2)
peaks;
shading faceted
(3) brighten
4.18 peaks

4.22 interp faceted

peaks;
brighten(0.5)
colormap

4.3 MATLAB
4.3.1

4.10
4.10

bar

bar3

barh

bar3h

bar(x,y,width,'')
%
bar3(y,z,width,'')
%
x 1:mm y y
mn m

n width 0.8 1
'' grouped() stacked() groupedbar3
y 1:m'' grouped stacked
detached()
4.19 3 6 y
4.23

x=3:6;
y=[5.3000 13.0000
0.4000
5.1000 11.8000 -1.7000
3.7000
8.1000
0.6000
1.5000
7.7000 -4.5000]
bar(x,y)
%
bar3(x,y)
%

4.23 (a)

(b)

4.3.2
1.
area

area(y)
%
area(x,y)
y y plot
y x x
1:size(y,1)
2.
fill

fill(x,y,c)
%

c 'r''g''b' 'c''m''y''w''k' RGB

4.19 4.24
area(x,y)
%
fill(x,y,'r')
%

4.24 (a)

(b)

4.3.3

hist(y,m)
%
hist(y,x)
m 10x
y
4.20 4.25
y=randn(10,2)
% 10*2
y =
-1.1878

-1.1859

-2.2023

-1.0559

0.9863

1.4725

-0.5186

0.0557

0.3274

-1.2173

0.2341

-0.0412

0.0215

-1.1283

-1.0039

-1.3493

-0.9471

-0.2611

-0.3744

0.9535

x=-2:0.5:2;
hist(y,x)

4.25

y x -2

4.3.4
pie pie3

pie(x,explode,label)
%
pie3(x,explode,label)
%
x explode x
label
4.21 4.26
y=[200 100 250 400];
%
explode=[0 0 1 0];
pie(y,explode,{'','','',''})

4.26

4.3.5
MATLAB stemstem3stairs scatter
4.22 y = e

x=0:0.1:2*pi;
y=sin(x).*exp(-2*x);
subplot(3,1,1)
stem(x,y,'filled')
subplot(3,1,2)
stairs(x,y)
subplot(3,1,3)
scatter(x,y)

2 x

sin( x) 4.27

%
%
%

4.27

'filled'

4.3.6
1.
semilogxsemilogy loglog

semilogx(x,y,'')
% x
semilogy(x,y,'')
% y
loglog(x,y,'')
% xy
plot
1
4.23 G (s) =
4.28
s(0.5s + 1)
w
w=logspace(-2,3,20);
% w 0.01 1000
Aw=1./(w.*sqrt((0.5*w).^2+1));
%

Lw=20*log10(Aw);
semilogx(w,Lw)
title('')

4.28

2.
polar

polar(theta,radius,'')
%
theta radius
4.23 Nyquist 4.29
w=logspace(-2,3,20);
Fw=-90-atan(0.5*w);
polar(Fw,Aw)

4.29

4.3.7

contour(Z,n)
contour(x,y,z,n)

% Z
% x y xy

n
4.24 peaks 4.30
[x,y,z]=peaks;
contour(x,y,z)
%
contour3(z,30)
% 30

4.30 (a)

(b)

4.3.8
1. compass
compass

compass(u,v)
%
compass(Z)
uv Z
compass(real(Z),imag(Z))
2. feather
feather (k,0)

feather(u,v)
%
feather (Z)
4.25 4.31
theta=0:0.2:2*pi;
z=sin(theta).*exp(j*theta);
compass(z)
feather(z)

4.31 (a)

(b)

(k,0)k 1nn Z

4.4
1
MATLAB 4.32

4.32

2
Edit Insert
Edit Insert 4.33

4.33 (a) Edit

(b) Insert

Edit Figure Properties


Axes PropertiesCurrent Object Properties
4.34

4.34

Insert

4.5
1.
inputdlg

answer = inputdlg(prompt,title,lineno,defans,addopts) %

answer prompt
title lineno
defans addopts
on off off
() on

4.26 4.35
prompt={'',''};
defans={'0.707','1'};
p=inputdlg(prompt,'',1,defans)

4.35

promptdefans p Cancel

2.

msgbox(message,title,icon,icondata,iconcmap,CreateMode) %

message title
icon none()error()help()
warn()custom()custom icondata
iconcmap CreateMode
modal()replace()non-modal(
)
4.26 1 4.36
msgbox('','','warn')

4.36

msgbox
4.26 1
warndlg('','')
4.36
4.26 0 4.37
errordlg('','')
4.26 4.38
helpdlg(' 0 1','')

4.38

4.39

4.26 4.39
button=questdlg('','Are you sure?','Yes','No','Yes')
YesYes
NoNo
3.

(1) uigetfile
uigetfile

[FileName, PathName] = uigetfile(FiltrEspec, Title,x,y)


FileName PathName
0FiltrEspec *
*.mTitle
xy ()
4.27 MATLAB license.txt 4.40

[fname,pname]=uigetfile('*.*','',100,100)

4.40

(100100)
fname pname
(2) uiputfile
uiputfile

[FileName, PathName] = uiputfile(FiltrEspec, Title,x,y)


uigetfile
4.27
[fname1,pname1]=uiputfile('Ex0431.mat','')

4.6
4.6.1

4.41

4.6.2

Root()

Figure()

Axes()

Uicontrol()

Uimenu()

Line

Surface

Rectangle

Patch

Image

Text

Light

()

()

()

()

()

()

()

4.41

1
4.11

4.11

h_ figure =figure(n)

h_axes =axes(position,[left,bottom,width,height])

h_ line =line(x,y,z)

h_ surface=surface(x,y,z,c)

xyz c

h_ rectangle=

rectangle (position,[x,y,w,h],curvature,[xc,yc])

xy wh
xcyc

h_ patch=patch(faces,fac,veitices,vert)

fac
vert

h_ image=image(x)

h_ text=text(x,y,string)

xy string

h_ light=light(PropertyName,Propertyvalue)

h_ uicontrol =uicontrol(PropertyName,Propertyvalue)

PropertyName Propertyvalue

propertyName Propertyvalue

h_uimenu = uimenu (propertyName, Propertyvalue)

2
(1)
MATLAB gcfgcagco

gcf
%
gca
%
gco
%
4.28
x=0:0.1:2*pi;
y=sin(x).*exp(-x);
plot(x,y)
text(pi,0,'\leftarrowexp(-x)*sin(x)=0')
h_fig=gcf
%

0.35

0.3

0.25

0.2

0.15

0.1

0.05

exp(-x)*sin(x)=0

-0.05
0

h_fig =
1
h_axes=gca

h_axes =
101.0013
h_obj=gco

h_obj =
3.0017

plot(x,y)
1 100.0013 3.0017
(2)

findobj

h=findobj
%

h=findobj(h_obj)
%
h=findobj(PropertyName,PropertyValue)
%

h=findobj(h_obj, PropertyName, PropertyValue)


%
h_obj PropertyName PropertyValue
4.28 findobj 4.42
findobj
%
ans =
0
1.0000
100.0013
101.0038
3.0017
h_text=findobj(h_fig,'string','\leftarrowexp(-x)*sin(x)=0') %

h_text =
101.0038

0 1
100.0013 101.0038 3.0017
string
(3)

h_parent=get(h_obj,parent)
%
h_children=get(h_obj,children)
%
4.28
h_children=get(h_axes,'children')

h_children =
101.0038
3.0017
h_parent=get(h_axes,'parent')
h_parent =
1

3
delete(h_obj)

4.28
delete(h_axes)

4.6.3
1

4.29
h_fig=figure('color','red','menubar','none','position',[0,0,300,300])

ps.color='red';
ps.position=[0,0,300,300];
ps.menubar='none';
h_fig=figure(ps)
h_fig =
1

(0,0)
300
2. get
get

get(h_obj)
get(h_obj, 'PropertyName')
4.29
p=get(h_fig,'position')

%
%

p =
0

300

300

c=get(h_fig,'color')
c =
1

RGB
3. set
set

set(h_obj)


set(h_obj, 'PropertyName')

set(h_obj, 'PropertyName', ' PropertyValue ') %

set(h_obj, 'PropertyStructure')

4.30 4.43
h_fig=figure('color','red','menubar','none','position',[0,0,300,300])
;
x=0:0.1:2*pi;
y=sin(x).*exp(-x);
h_line1=plot(x,y,'b');
title('y=exp(-x)*sin(x)')
set(gca,'ygrid','on')
% y
line1width=get(h_line1,'linewidth')
%

line1width =
0.5000

set(h_line1,'linewidth',3)

h_title =get(gca,'title')

h_title =
115.0016

titlefontsize=get(h_title_fontsize,'fontsize')

set(h_title_fontsize,'fontsize',13)

h_text1=text(pi,0,'\downarrow');
text1pos=get(h_text1,'position')

%
%
%
%

h_text2=text(text1pos(1,1),text1pos(1,2)+0.025,'exp(-x)*sin(x)=0');
%
set(h_text1,'fontsize',13,'color','red')
%

set(h_text2,'fontsize',13,'color','red')

4.43

get(h_obj, 'DefaultObjectTypePropertyName')
%

set(h_obj, 'DefaultObjectTypePropertyName', PropertyValue)


%
set(h_obj, 'DefaultObjectTypePropertyName', 'Remove')
%
DefaultObjectTypePropertyName Default++
DefaultLineLineWidth

4.7 (GUI)
MATLAB

4.7.1
MATLAB Guide
(1) File NewGUI
(2) GuideGuide Filename Guide
4.44

4.44 Guide

Guide Create New GUIOpen Existing GUI


Blank GUI(Default)OK
4.45 4.44Blank
GUI(Default)GUI with Uicontrols

4.45

(Align Objects)
(Menu Editor)(Property Inspector)(Object Browser)

4.7.2
1.
4.31
4.47

4.47(a) MATLAB
4.47(b)

4.47 (a)

(b)

4.47(a)FilePaint 4.47(b)Paint

CircleColorRedBlue

2.
4.31 4.47(b)
h_fig=gcf
h_fig =
1
h_menu=uimenu(h_fig,'label','Paint');
h_menu1=uimenu(h_menu,'label','Circle');
h_menu2=uimenu(h_menu,'label','Color');
h_menu21=uimenu(h_menu2,'label','Red');
h_menu22=uimenu(h_menu2,'label','Blue');

% Paint
% Paint Circle
% Paint Color
% Color Red
% Color Blue

label 4.46 label


3.
4.31
h_menu21=uimenu(h_menu2,'label','Red','callback','set(h_fig,''color'',''red'')')
% Color Red
h_menu22=uimenu(h_menu2,'label','Blue','callback','set(h_fig,''color'',''blue'')')
% Color Blue

4.7.3
1.
4.12
4.12

PropertyName

PushButton

ToggleButton

RadioButton

CheckBox

EditText

StaticText

Slider

Frame

ListBox

PopupMenu

Axes

2.
(1)

4.48
(2) unicontrol

4.48


h_control=uicontrol(h_Parent,PropertyName,ProperValue,)

4.50

4.7.5
4.7.6 GUI
4.32

1.
guide Guide Blank GUI

4.52

4.52

2.
4.13
4.13

String()

String()

String()

Tag()

pushbutton_red

String()

Tag()

pushbutton_blue

String()

0.3
0.5
0.707
Tag()

popupmenu_zeta

XLim(x )

[0 20]

YLim(y )

[0 1.5]

ToolsActivate Figure

3.
ViewObject
CallbacksCallback functionTag
popupmenu_zetapushbutton_redpushbutton_blue.m

% -------------------------------------------------------------------function varargout = pushbutton_red_Callback(h, eventdata, handles, varargin)


% -------------------------------------------------------------------function varargout = pushbutton_blue_Callback(h, eventdata, handles, varargin)
% -------------------------------------------------------------------function varargout = popupmenu_zeta_Callback(h, eventdata, handles, varargin)

% -------------------------------------------------------------------function varargout = popupmenu_zeta_Callback(h, eventdata, handles, varargin)


val=get(h,'value')
switch val
case 1
handles.data=0
case 2
handles.data=0.3
case 3
handles.data=0.5
case 4
handles.data=0.707
end
guidata(h,handles)

h 'Value'
switch
handles
(1)
Tag
(2) function handles guidata
handles function

% -------------------------------------------------------------------function varargout = pushbutton_red_Callback(h, eventdata, handles, varargin)


x=0:0.1:20;
zeta=handles.data
y=1-1/sqrt(1-zeta^2)*exp(-zeta*x).*sin(sqrt(1-zeta^2)*x+acos(zeta));
plot(x,y,'r')

% -------------------------------------------------------------------function varargout = pushbutton_blue_Callback(h, eventdata, handles, varargin)


x=0:0.1:20;
zeta=handles.data
y=1-1/sqrt(1-zeta^2)*exp(-zeta*x).*sin(sqrt(1-zeta^2)*x+acos(zeta));
plot(x,y,'b')

zeta handles
data

4.53

4.53

4.8
4.8.1

(1) getframe
M
(2) movie(M,k) M k
4.33 4.54

n=20;
for i=1:n
x=0:0.1:i;
y=1-1/sqrt(1-0.3^2)*exp(-0.3*x).*sin(sqrt(1-0.3^2)*x+acos(0.3));
plot(x,y)
axis([0,20,0,1.5]);
%
M(i)=getframe;
%
end
movie(M,3)
% 3

for 20 M
3

4.54

4.8.2
1 EraseMode
EraseMode EraseMode
EraseMode
normal
xor xor

background

none
EraseMode
normal > xor > background > none
2

xdata x
ydata y
3
drawnow

(1) EraseMode xorbackground none


(2) xdata ydata()
(3) drawnow
4.34 4.55

4.55

x=0:0.1:20;
y=1-1/sqrt(1-0.3^2)*exp(-0.3*x).*sin(sqrt(1-0.3^2)*x+acos(0.3));
plot(x,y)
h=line(0,0,'color','red','marker','.','markersize',40,'erasemode','xo
r');
%
for i=1:length(x)
set(h,'xdata',x(i),'ydata',y(i)); %
pause(0.005)
% 0.005
drawnow
%
end

xor EraseMode background


drawnow MATLAB set
pause drawnow

MATLAB

5.1
M M M

5.1.1 M
MATLAB M M (EditorDebugger)

MATLAB

FileNewM-file

M M M
5.1
M

5.1 M /

5.1.2 M
M y x
y = 1

1
1 2

e x sin( 1 2 x + a cos ) 5.1 M 5.2 M

5.1 M
%EX0501

% 0.3
x=0:0.1:20;
y1=1-1/sqrt(1-0.3^2)*exp(-0.3*x).*sin(sqrt(1-0.3^2)*x+acos(0.3))
plot(x,y1,'r')

5.2 zeta
function y=Ex0502(zeta)
% EX0502
Step response of quadratic system.
%
% zeta
%y

%
% copyright 2003-08-01
x=0:0.1:20;
y=1-1/sqrt(1-zeta^2)*exp(-zeta*x).*sin(sqrt(1-zeta^2)*x+acos(zeta))
plot(x,y)

H1 (%)
(%)
(%)

help lookfor
help Ex0502
EX0502
Step response of quadratic system.

zeta
y

lookfor ''
Ex0502.m: %

5.1.3 M

(1)
(2) MATLAB
MATLAB
(3) MATLAB
(workspace) clear

5.1 M M

(1) MATLAB

(2) M
Ex0501.m
%EX0501

x=0:0.1:20;
y1=1-1/sqrt(1-0.3^2)*exp(-0.3*x).*sin(sqrt(1-0.3^2)*x+acos(0.3))
plot(x,y1,'r')
% 0.3
hold on
y2=1-1/sqrt(1-0.707^2)*exp(-0.707*x).*sin(sqrt(1-0.707^2)*x+acos(0.707))
plot(x,y2,'g')
% 0.707
y3=1-exp(-x).*(1+x)
plot(x,y3,'b')
% 1

(3) M DebugRun 5.2

5.2

whos
Name

Size

Bytes Class

1x201

1608 double array

y1

1x201

1608 double array

y2

1x201

1608 double array

y3

1x201

1608 double array

Grand total is 804 elements using 6432 bytes

5.1.4 M

(1) function

function [] = ()
(2)
(3) return

(4) M
MATLAB
5.2 M M
MATLAB
M
(1)
function y=Ex0502(zeta)
%EX0502

x=0:0.1:20;
y=1-1/sqrt(1-zeta^2)*exp(-zeta*x).*sin(sqrt(1-zeta^2)*x+acos(zeta))
plot(x,y)

(2) Ex0502.m
(3) MATLAB f
f=Ex0502(0.3)

Ex0502 zeta
y
xy
x
??? Undefined function or variable 'x'.
y
??? Undefined function or variable 'y'.

M M MATLAB

5.2
5.2.1 for ... end

for =array

end
array array
array
5.3 for ... end array 1+3+5...+100
% EX0503

for

sum=0;
for n=1:2:100
sum=sum+n;
end
sum
sum =
2500

sum =2500
nn 1:2:100 n

5.4 for ... end array


% EX0504

for

sum=zeros(6,1);
for n=eye(6,6)
sum=sum+n;
end
sum
sum =
1
1
1
1
1
1

n eye(6,6) n [1;0;0;0;0;0]
n [0;1;0;0;0;0] 6

5.2.2 while ... end

while

end

nanMATLAB
5.5 5.3 1+3+5...+100
% EX0505

while

sum=0;
n=1;
while n<=100
sum=sum+n;
n=n+2 ;
end
sum
n
sum =
2500
n =
101

while ... end n=101

5.2.3 Ifelseend

if 1
1
elseif 2
2
...
else
n+1
end
1 elseif 2
else n+1Ifelseend
elseif else
5.6 If 0<zeta<1 zeta=1

function y=Ex0506(zeta)
% EX0506
if
x=0:0.1:20;
if (zeta>0)&(zeta<1)
y=1-1/sqrt(1-zeta^2)*exp(-zeta*x).*sin(sqrt(1-zeta^2)*x+acos(zeta));
elseif zeta==1
y=1-exp(-x).*(1+x);
end
plot(x,y)

5.2.4 switchcase

switch
case 1
1
case 2
2
...
otherwise
n
end

(1) case 1
2 otherwise n

(2)
(3) case

5.7 switchcase
% EX0507

switch

for month=1:12;
switch month
case{3,4,5}
season='spring'
case{6,7,8}
season='summer'
case{9,10,11}
season='autumn'
otherwise
season='winter'
end
end
season =
winter
season =

winter
season =
spring
season =
spring
season =
spring
season =
summer
season =
summer
season =
summer
season =
autumn
season =
autumn
season =
autumn
season =
winter

1:12case

5.2.5 try... catch... end

try
1
catch
2
end
1
lasterr 2 2

5.8 try... catch... end


% EX0508

try

n=4;
a=magic(n);
m=3;
b=eye(3);
try
c=a*b
catch

c=a(1:m,1:m)*b
end
lasterr
c =
16

11

10

ans =
Error using ==> *
Inner matrix dimensions must agree.

catch
a b

5.2.6
1. break
break break for while
end break If
5.9 5.5 If break while 1+3+5...+100
1000
% EX0509

break while

sum=0;
n=1;
while n<=100
if sum<1000
sum=sum+n;
n=n+2;
else
break
end
end
sum
n
sum =
1024
n =
65

whileend Ifelseend sum 1024


while
2. continue
continue for while

5.10 If continue 1100


100 2 n
% EX0510

continue while

sum=2;ss=0;
for n=3:100
for m=2:fix(sqrt(n))
if mod(n,m)==0
ss=1;
break;
else
ss=0;

% ss 1
%
% ss 0

end
end
if ss==1
continue;

end
sum=sum+n;
end
sum
sum =
1060

fix(sqrt(n)) n for
if mod(n,m)==0 break continue

3. return
return

Ctrl+break
4. pause
pause

pause
%
pause(n)
% n
5. keyboard
keyboard return

6. input
input
a=input('input a number:')
input a number:45
a=
45
b=input('input a number:','s')
input a number:45
b=
45
input('input a number:')
input a number:2+3
ans =
5

% a

% b

5.3
5.3.1
1.
M

(1) M

(2) (
)
(3)
(4) help lookfor
5.11 5.2

function Ex0511()
% EX0511

z1=0.3;
Ex0502(z1);
% Ex0502
hold on
z1=0.5
Ex0502(z1)
% Ex0502
z1=0.707;
Ex0502(z1)
% Ex0502
function y=Ex0502(zeta)
%
x=0:0.1:20;
y=1-1/sqrt(1-zeta^2)*exp(-zeta*x).*sin(sqrt(1-zeta^2)*x+acos(zeta))
plot(x,y)

Ex0511 Ex0502
Ex0511.m

2.
private M
(1) private M

(2) M
(3) MATLAB
3.
MATLAB



5.3.2
1.
(Local Variables)

2.
(Global Variables) MATALB

5.12 5.11
function Ex0512()
% EX0512

global X
X=0:0.1:20;
z1=0.3;
Ex0502(z1);
hold on
z1=0.5;
Ex0502(z1);
z1=0.707;
Ex0502(z1);
function Ex0502(zeta)
%
global X
y=1-1/sqrt(1-zeta^2)*exp(-zeta*X).*sin(sqrt(1-zeta^2)*X+acos(zeta));
plot(X,y);

X global
X
global X

who
Your variables are:
X

5.3.3
1.
5.13 5.11
5.3
function Ex0513()
% EX0513
z1=0.3;
[x1,y1]=Ex0502(z1);
plot(x1,y1)
hold on
z1=0.5;

function [x,y]=Ex0502(zeta)

[x2,y2]=Ex0502(z1);

plot(x2,y2)

x=0:0.1:20;

z1=0.707;

y=1-1/sqrt(1-zeta^2)*exp(-zeta*x).*sin...

[x3,y3]=Ex0502(z1);

(sqrt(1-zeta^2)*x+acos(zeta));
5.3

Ex0513 Ex0502 zeta


z1 zeta x y x1y1

2.
(1) nargin nargout
nargin nargout nargin
nargout

nargin
%
nargout
%
nargin(fun)
%
nargout(fun)
%
5.14
function [sum,n]=Ex0514(x,y)
% EX0514
x y
if nargin==1
sum=x+0;
% 0
elseif nargin==0
sum=0;
% 0

else
sum=x+y;

end

Ex0514 2 1
[y,n]=Ex0514(2,3)
y =
5
n =
2
[y,n]=Ex0514(2)
y =
2
n =
1
[y,n]=Ex0514
y =
0
n =
0

[y,n]=Ex0514(1,2,3)
??? Error using ==> ex0514
Too many input arguments.

nargin('Ex0514')
ans =
2

5.14 nargout
if nargout==0
sum=0;
end

% 0 0

Ex0514
Ex0514(2,3) % 0
ans =
0
y=Ex0514(2,3)

% 1

y =
5
[y,n,x]=Ex0514

??? Error using ==> ex0514


Too many output arguments.

0 0
ans
(2) varargin varargout
varargin varargout
5.15
function [y,n]=Ex0515(varargin)
% EX0515
varargin
if nargin==0
% 0
disp('No Input variables.')
y=0;
elseif nargin==1
%
y=varargin{1};
else
n=nargin;
y=0;
for m=1:n
y=varargin{m}+y;
%
end
end
n=nargin;

MATLAB Ex0515
[y,n]=Ex0515(1,2,3,4)
% 4
y =
10
n =
4
[y,n]=Ex0515(1)

% 1

y =
1
n =
1
[y,n]=Ex0515

No Input variables.
y =
0
n =
0

n y

5.3.4
5.16
y = 1

1
1

e x sin( 1 2 x + a cos ) y = 1 (1 + x )e x

2
( 2 1 ) x
e ( + 1 ) x
e

y = 1

+ 1 2
2 2 1 1 2

M
function y=Ex0516(z1)
% EX0516

t=0:0.1:20;
if (z1>=0)&(z1<1)
y=plotxy1(z1,t);
elseif z1==1
y=plotxy2(z1,t);
else
y=plotxy3(z1,t);
end
function y1=plotxy1(zeta,x)
%
y1=1-1/sqrt(1-zeta^2)*exp(-zeta*x).*sin(sqrt(1-zeta^2)*x+acos(zeta));
plot(x,y1)
function y2=plotxy2(zeta,x)
%
y2=1-exp(-x).*(1+x);
plot(x,y2)
function y3=plotxy3(zeta,x)
%
y3=1-1/(2*sqrt(zeta^2-1))*(exp(-((zeta-sqrt(zeta^2-1))*x))./(zeta-sqrt(zeta^2-1))...
-exp(-((zeta+sqrt(zeta^2-1))*x))./(zeta+sqrt(zeta^2-1)));
plot(x,y3)

Ex0516 plotxy1plotxy2plotxy3
Ex0516.m
y=Ex0516(0.3);
hold on
y=Ex0516(0.707);
y=Ex0516(1);
y=Ex0516(2);

5.4

5.4

5.17 M
0
0

y = 0

n
n 1
n 2

1 2 3
0 1 2
0 0 1
0 0 0

function y=Ex0517(m)
% EX0517
y=0;
m=m-1;
for n=1:m
y=[0,y];
% 0
end
for n=1:m
a=[1:1:n];
b=a;
for k=m:-1:n
b=[0,b];
end
y=[b;y];
n=n+1;
end

m y
Ex0517.m
Ex0517
y=Ex0517(5)
y =
0

5.4 M
5.4.1 P
1. P
P pcode P M .p

pcode Filename.m
% Filename.p
pcode Filename.m -inplace
% Filename.m Filename.p
pcode Ex0517.m
P Ex0517.p
2. P
(1) P M
(2) M P P
(3) P
Ex0517.p

5.4.2 M
1.
MATLAB
(1)
(2)

(3) Mex
C Fortran Mex
MATLAB
(DLL)( 8.1.1 )
2.

5.18 5.17 zeros


0
0

y = 0

1 2 3
0 1 2
0 0 1
0 0 0

function y=Ex0518(m)
% EX0518
m=m-1;
y=zeros(m);
for n=1:m-1

n
n 1
n 2

a=1:m-n;
y(n,n+1:m)=a;
end
y

Ex0517 Ex0518
y=Ex0517(200)
y=Ex0518(200)

3.
MATLAB C

4.

5. M M
M

5.4.3 JIT
1. JIT
JIT
3


M MEX
forwhileifelseifswitch


i j
Intel CPU Windows Linux
2.
(1)

ViewProfilerprofile viewer
5.5

5.5

(2) MATLAB
MATLAB
Start Profiling
Start Profiling

Start ProfilingStart Profiling


Stop Profiling
MATLAB Stop
Profiling
Stop ProfilingStop Profiling

(3)
(Profile Summary)

3.24
Ex0324.m
%
[x,y]=dsolve('Dx=y,Dy=-x')
[x,y]=dsolve('Dx=y,Dy=-x','t')

dx
dy
= y,
= x
dt
dt

Ex0324Start Profiling
5.6 Ex0324

5.6

3. JIT
MATLAB6.5 JIT JIT

feature JIT on
% JIT
feature JIT off
% JIT
feature accel on
%
feature accel off
%
0
0

5.18 y = 0

1 2 3
0 1 2
0 0 1
0 0 0

n
n 1
n 2 JIT

5.6 y=Ex0518(200)
JIT
0.43
feature JIT off JIT 0.451 JIT
0.481
JIT M

5.5
1.
inline

inline(string,arg1,arg2,)
%
string(=)arg1 arg2
5.19 f ( x , z) = sin( x )e zx
f=inline('sin(x)*exp(-z*x)','x','z')

f =
Inline function:
f(x,z) = sin(x)*exp(-z*x)
y=f(5,0.3)

% f

y =
-0.2140

2.
MATLAB charclass argnames

char(inline_fun)
%
class(inline_fun)
%
argnames(inline_fun)
%
5.19
char(f)
ans =
sin(x)*exp(-z*x)
class(f)
ans =
inline
argnames(f)
ans =
'x'
'z'

3.
vectorize


vectorize(inline_fun)
%
5.19
ff=vectorize(f)
% f
ff =
Inline function:
ff(x,z) = sin(x).*exp(-z.*x)
x=0:0.1:20;
y=ff(x,0.3);

4.
feval

[y1y2,]=feval(inline_fun,arg1,arg2)
5.19
x=0:0.1:20;
z=0:0.05:10;
y=feval(ff,x,z)

5.6
5.6.1
1.

h_fun=@fun
%
h_fun=str2func(fun)
%
h_array=str2func({fun1,fun2,})
%
fun h_fun h_array
5.20 MATLAB
h_sin=@sin;
%
h_cos=str2func('cos');
%
h_array=str2func({'sin','cos','tan'})
h_array =
@sin

@cos

@tan

2.
(1)
M P

(2)

(3)
(4)

5.6.2 feval
feval feval

[y1,y2,]=feval(h_fun,arg1,arg2)
[y1,y2,]=feval(funname,arg1,arg2)
h_fun funnamearg1arg2y1y2

5.21 5.17

function y=Ex0521(z1)
% EX0521

t=0:0.1:20;
h_plotxy1=str2func('plotxy1') %
h_plotxy2=str2func('plotxy2') %
h_plotxy3=str2func('plotxy3') %
if (z1>=0)&(z1<1)
y=feval(h_plotxy1,z1,t); %
elseif z1==1
y=feval(h_plotxy2,z1,t); %
else
y=feval(h_plotxy3,z1,t); %
end
function y1=plotxy1(zeta,x)
%
y1=1-1/sqrt(1-zeta^2)*exp(-zeta*x).*sin(sqrt(1-zeta^2)*x+acos(zeta));
plot(x,y1)
function y2=plotxy2(zeta,x)
%
y2=1-exp(-x).*(1+x);
plot(x,y2)
function y3=plotxy3(zeta,x)
%
y3=1-1/(2*sqrt(zeta^2-1))*(exp(-((zeta-sqrt(zeta^2-1))*x))./(zeta-sqrt(zeta^2-1))...
-exp(-((zeta+sqrt(zeta^2-1))*x))./(zeta+sqrt(zeta^2-1)));
plot(x,y3)

5.17
MATLAB Ex0521
(1) feval
h_Ex0521=str2func('Ex0521')

h_Ex0521 =
@Ex0521
y=feval(h_Ex0521,1);
h_plotxy1 =
@plotxy1
h_plotxy2 =
@plotxy2
h_plotxy3 =
@plotxy3

(2) feval
y=feval('Ex0521',1);

1.4

1.2

0.8

0.6

0.4

0.2

0
0

10

12

14

16

18

20

(3)
y=Ex0521(1);

5.7
MATLAB

[]=(h_fun,)
[]=(funname,)
h_fun M funname
M

5.7.1
1. fminbnd
fminbnd

[x,y]=fminbnd(h_fun,x1,x2,options)
[x,y]=fminbnd(funname,x1,x2,options)
h_fun funnameoptions
0 x fun x1<x<x2
y
5.22 fminbnd humps
[x,y]=fminbnd(@humps,0.5,0.8)
% 0.50.8

x =
0.6370
y =
11.2528

humps MATLAB M humps.m @humps


humps humps 5.7 (0.637011.2528)
10-4

5.7 humps

2. fminsearch
fminsearch

x=fminsearch(h_fun,x0)
x=fminsearch(funname,x0)
x0
5.23 Banana f(x,y)=100(y-x2)2+(1-x)2
x=1y=1
fn=inline('100*(x(2)-x(1)^2)^2+(1-x(1))^2' ,'x')
% inline
,x y

fn =
Inline function:
fn(x) = 100*(x(2)-x(1)^2)^2+(1-x(1))^2
y=fminsearch(fn,[0.5,-1])

%(0.5,-1)

y =
1.0000

1.0000

5.7.2
fzero f(x)=0

x=fzero(h_fun,x0,tol,trace)
x=fzero(funname,x0,tol,trace)
h_fun x0
tol epstrace
0tol trace
5.24 humps humps 5.8
xzero=fzero(@humps,1)
% 1
xzero =
1.2995
xzero=fzero(@humps,[0.5,1.5])

% 0.51.5

xzero =
1.2995
xzero=fzero(@humps,[0.5,1])

% 0.51

??? Error using ==> fzero


The function values at the interval endpoints must differ in sign.

0.51

5.8 humps

5.7.3
quad quad8 quad8 quad

s=quad(h_fun,x1,x2,tol,trace,p1,p2,)
s=quad(funname,x1,x2,tol,trace,p1,p2,)
s=quad8(h_fun,x1,x2,tol,trace,p1,p2,)
s=quad8(funname,x1,x2,tol,trace,p1,p2,)
x1 x2 tol 0.001
trace 1 0 p1,p2

5.25 y=humps(x)
x=0:0.01:1;
y=humps(x);
area=trapz(x,y)
%
area =
29.8571
area1=quad(@humps,0,1)

% quad

area1 =
29.8583
area2=quad8(@humps,0,1)

% quad8

Warning: QUAD8 is obsolete. QUADL is its recommended replacement.


(Type "warning off MATLAB:quad8:ObsoleteFunction" to suppress this
warning.)
> In D:\MATLAB\toolbox\matlab\funfun\quad8.m at line 35
area2 =
29.8583

trapz
quad quad8

5.7.4
MATLAB ode23ode45 ode113

[t,y]=ode23(h_fun,tspan,y0,options,p1,p2)
[t,y]=ode23(funname,tspan,y0,options,p1,p2)

[t,y]=ode45(h_fun,tspan,y0,options,p1,p2)
[t,y]=ode45(funname,tspan,y0,options,p1,p2)

[t,y]=ode113(h_fun,tspan,y0,options,p1,p2)
[t,y]=ode113(funname,tspan,y0,options,p1,p2)
h_fun dx t,y tspan=[ ]
y0 options
p1,p2
5.26(Van der Pol)
d2x
2

(1 x 2 )

dx
+x=0
dt

dt
(1)
y1=xy2=dx/dt
dy1
dt
y2

dy =

(1
y
)y
y
1
2
1
2
dt

(2) vdpol.m =2
yprime y1 y2 y
M vdpol.m
%
function yprime=vdpol(t,y)
yprime=[y(2);2*(1-y(1)^2)*y(2)-y(1)]

(3) y1 y2
tspan=[0,30];
y0=[1;0];
[t,y]=ode45(@vdpol,tspan,y0);
y1=y(:,1);
y2=y(:,2);
figure(1)
plot(t,y1,':b',t,y2,'-r')
figure(2)
plot(y1,y2)

% 0 30
%
%

%
%

y1 y2 5.9

(t,y(1))

(t,y(2))

5.9

y(1)y(2) 5.10

5.10

6
MATLAB (Control System Toolbox)

6.1
6.1.1
MATLAB
ss dss

G=ss(a,b,c,d)
% abcd
G=dss(a,b,c,d,e)
% abcde
6.1

d 2 y(t)
dt

+ 2 n

zeta=0.707;wn=1;
A=[0 1;-wn^2 -2*zeta*wn];
B=[0;wn^2];
C=[1 0];

dy(t)
+ n 2 y(t) = n 2 u(t) =0.707 n =1
dt

D=0;
G=ss(A,B,C,D)

a =
x1
x2

x1

x2

-1 -1.414

b =
u1
x1

x2

c =
x1 x2
y1

d =
u1
y1

Continuous-time model.

6.1.2
MATLAB tf

G=tf(num,den)
%
num num=[b1,b2,,bm,bm+1]den den=[a1,a2,,an-1,an]
6.1
num=1;
den=[1 1.414 1];
G=tf(num,den)
%

Transfer function:
1
----------------s^2 + 1.414 s + 1

6.1.3
MATLAB zpk

G=zpk(z,p,k)
%
z p k
6.1
z=roots(num)
z =
Empty matrix: 0-by-1
p=roots(den)
p =
-0.7070 + 0.7072i
-0.7070 - 0.7072i
zpk(z,p,1)

Zero/pole/gain:
1
------------------(s^2 + 1.414s + 1)

roots

r
r
r
G(s) = 1 + 2 + + n + k(s)
s p1 s p 2
s pn
6.1
[r,p,k]=residue(num,den)
r =
0 - 0.7070i
0 + 0.7070i
p =
-0.7070 + 0.7072i
-0.7070 - 0.7072i
k =
[]

6.1.4
1.

ss dss

G=ss(a,b,c,d,Ts)
% abcd
G=dss(a,b,c,d,e,Ts)
% abcde
Ts -1
6.2
a=[-1.5 -0.5;1 0];
b=[1;0];
c=[0 0.5];
d=0;
G=ss(a,b,c,d,0.1)
% 0.1s

a =
x1

x2

x1 -1.5 -0.5
x2

b =
u1
x1

x2

c =
x1
y1

x2

0 0.5

d =
u1
y1

Sampling time: 0.1


Discrete-time model.

2.
tf

G=tf(num,den,Ts)
%
Ts -1 'z'
6.2 G(z) =
num1=[0.5 0];

0.5z
z 2 1.5z + 0.5

0.5z 1
1 1.5z 1 + 0.5z 2

den=[1 -1.5 0.5];


G1=tf(num1,den,-1)

Transfer function:
0.5 z
----------------z^2 - 1.5 z + 0.5
Sampling time: unspecified

MATLAB filt

G=filt(num,den,Ts)
%
Ts Ts -1 'z-1'

6.2 filt
num2=[0 0.5];
G2=filt(num2,den)

Transfer function:
0.5 z^-1
----------------------1 - 1.5 z^-1 + 0.5 z^-2
Sampling time: unspecified

filt z z-1[0
0.5]
3.
zpk

G=zpk(z,p,k,Ts)
%
6.2 zpk
G3=zpk([0],[0.5 1],0.5,-1)

Zero/pole/gain:
0.5 z
------------(z-0.5) (z-1)
Sampling time: unspecified

6.2
6.2.1
MATLAB5.3 6.1

6.1

tf2ss

[a,b,c,d]=tf2ss(num,den)

tf2zp

[z,p,k]=tf2zp(num,den)

ss2tf

[num,den]=ss2tf(a,b,c,d,iu)

ss2zp

[z,p,k]=ss2zp(a,b,c,d,iu)

zp2ss

[a,b,c,d]=zp2ss(z,p,k)

zp2tf

[num,den]=zp2tf(z,p,k)

1.
(1)
ss dss

G=ss()
%
G=ss()
%
6.3 G 1 (s) =

num=[0 3 2;
1 2 3];
den=[3 5 2 1];
G11=tf(num(1,:),den)

Transfer function:
3 s + 2
----------------------3 s^3 + 5 s^2 + 2 s + 1
G12=tf(num(2,:),den)

Transfer function:
s^2 + 2 s + 3
-----------------------

3s + 2
s 2 + 2s + 5

3s 3 + 5s 2 + 2s + 1

3 s^3 + 5 s^2 + 2 s + 1
G=ss([G11;G12])

a =
x1
x1

-1.667

x2

x3

-0.3333 -0.08333

x2

x3

b =
u1
x1

x2

x3

c =
x1
y1

x2

x3

0.5 0.1667

y2 0.3333 0.3333

0.25

d =
u1
y1

y2

Continuous-time model.

(2)
tf

G=tf()
%
G=tf()
%
6.3
G1=tf(G)

Transfer function from input to output...


s + 0.6667
#1: ----------------------------------s^3 + 1.667 s^2 + 0.6667 s + 0.3333

0.3333 s^2 + 0.6667 s + 1


#2: ----------------------------------s^3 + 1.667 s^2 + 0.6667 s + 0.3333

(3)
zpk

G=zpk()
%
G=zpk()
%
6.3
G2=zpk(G)
%

Zero/pole/gain from input to output...


(s+0.6667)
#1: ----------------------------------(s+1.356) (s^2 + 0.3103s + 0.2458)
0.33333 (s^2 + 2s + 3)
#2: ----------------------------------(s+1.356) (s^2 + 0.3103s + 0.2458)
G2=zpk(G1);

2.

[a,b,c,d]=ssdata(G)
[a,b,c,d,e]=dssdata(G)
[num,den]=tfdata(G)
[z,p,k]=zpkdata(G)
6.3
[a,b,c,d]=ssdata(G1)
a =
-1.6667
2.0000

-0.3333

-0.0833

2.0000

0.5000

b =
1
0
0
c =
0.1667

%
%
%
%
%

0.3333

0.3333

0.2500

d =
0
0
[num,den]=tfdata(G2)

num =
[1x4 double]
[1x4 double]
den =
[1x4 double]
[1x4 double]
[z,p,k]=zpkdata(G)

z =
[

-0.6667]

[2x1 double]
p =
[3x1 double]
[3x1 double]
k =
1.0000
0.3333

3.
6.3
class(G)
%
ans =
ss
isa(G,'tf')

ans =
0

6.2.2
6.2

class

class(G)

isa

isa(G, '')

G 1(True)

isct

isct(G)

G 1(True)

isdt

isdt(G)

G 1(True)

issiso

issiso(G)

G SISO 1(True)

1. c2d
c2d

Gd=c2d(G,Ts,method)
% Ts method

G Gd Ts method
zoh()foh()tustin()prewarp(
)mached()
6.4
a=[0 1;-1 -1.414];
b=[0;1];
c=[1 0];
d=0;
G=ss(a,b,c,d);
Gd=c2d(G,0.1)

a =
x1
x1

x2

0.9952

0.0931

x2 -0.0931

0.8636

b =
u1
x1 0.004768
x2

0.0931

c =
x1 x2
y1

d =
u1
y1

Sampling time: 0.1


Discrete-time model.

2. d2c
d2c c2d


G=d2c(Gd,method)
%
method c2d foh()
6.4
G=d2c(Gd)

a =
x1

x2

x1 5.551e-016
x2

-1

1
-1.414

b =
u1
x1 -2.776e-016
x2

c =
x1 x2
y1

d =
u1
y1

Continuous-time model.

3. d2d
d2d

Gd2=d2d(Gd1,Ts2)
% Ts2
Gd1 Ts2
Gd2
6.4
Gd2=d2d(Gd,0.3)

a =
x1
x1

x2

0.961

0.2408

x2 -0.2408

0.6205

b =
u1
x1 0.03897
x2

0.2408

c =
x1 x2
y1

d =
u1
y1

Sampling time: 0.3


Discrete-time model.

6.2.3
1.
sstf zpk
6.3 6.4
6.3

Ts

0 -1

Td

InputName

OutputName

Notes

Userdata

6.4

tf

ss

den

num

variable

s,p,z,q,z-1

zpk

StateName

-1

variable

s,p,z,q,z

6.3 6.4 MATLAB get


set
2. get set
(1) get

get(G)
%
get(G,PropertyName,)
%
G PropertyName
(2) set

set(G,PropertyName,PropertyValue,) %
1
6.5 G(s) = 2

s + 1.414s + 1

1
2

z + 2z + 1

num=1;
den=[1 1.414 1];
G=tf(num,den);
get(G)

num: {[0 0 1]}


den: {[1 1.41 1]}
Variable: 's'
Ts: 0
ioDelay: 0
InputDelay: 0
OutputDelay: 0
InputName: {''}
OutputName: {''}
InputGroup: {0x2 cell}
OutputGroup: {0x2 cell}
Notes: {}
UserData: []
set(G,'den',[1 2 1],'Variable','z')
G

Transfer function:
1
------------z^2 + 2 z + 1
Sampling time: unspecified

3.
6.5
G.den=[1 1.414 1];
G

Transfer function:
1
----------------z^2 + 1.414 z + 1
Sampling time: unspecified

6.3
1.
SISO 6.1
u(t)

y(t)
G1(s)

G2(s)
6.1

G=G1*G2
G=series(G1,G2)
2.
SISO
6.2

G=G1+G2
G=parallel(G1,G2)

G1(s)
u(t)
G2(s)
6.2

3.
6.3

y(t)

u(t)

G1(s)

y(t)

G2(s)
6.3

G=feedback(G1,G2,Sign)
Sign Sign=-1
6.6 6.4
1
1
1
1
G 1 (s) = 2
G 2 (s) =
G 3 (s) =
G 4 (s) =
s +1
2s + 1
s
s + 2s + 1
G1(s

G2 (s)
G3 (s)
G4 (s)
6.4

G1=tf(1,[1 2 1])

Transfer function:
1
------------s^2 + 2 s + 1
G2=tf(1,[1 1]);
G3=tf(1,[2 1]);
G4=tf(1,[1 0]);
G12=G1+G2

Transfer function:
s^2 + 3 s + 2
--------------------s^3 + 3 s^2 + 3 s + 1
G34=G3-G4

Transfer function:
-s - 1
--------2 s^2 + s
G=feedback(G12,G34,-1) %

Transfer function:
2 s^4 + 7 s^3 + 7 s^2 + 2 s
------------------------------------2 s^5 + 7 s^4 + 8 s^3 + s^2 - 4 s - 2

G1 G2 G1

G1=ss(tf(1,[1 2 1]));
%
G2=tf(1,[1 1]);
G1+G2

a =
x1
x1

x2

x3

-2 -0.25

x2

x3

-1

b =
u1
x1 0.5
x2

x3

c =
x1
y1

x2

x3

0 0.5

d =
u1
y1

Continuous-time model.

4.

(1)
(2) append
G=append(G1,G2,G3,)
(3) Q
INPUTS
OUTPUTS
(4) connect
Sys=connect(G,Q,INPUTS,OUTPUTS)
blkbuild

nblocks
blkbuild
6.7 6.5
1/s+1
-

u(t)
1/s

y(t)
1/(s2+s)

1/(s2+s)

1/s

6.5

append
(1) 6.6
-1/s+1
7

1/s

1/(s2+s)

1/(s2+s)

1/s
6
-1
5
6.6

(2) append
G1=tf(1,[1 0]);
G2=tf(1,[1 1 0]);
G3=tf(1,[1 1 0]);

-2
4

-2

G4=tf(-2,1);
G5=tf(-1,1);
G6=tf(1,[1 0]);
G7=tf(-1,[1 1]);
Sys=append(G1,G2,G3,G4,G5,G6,G7)

Transfer function from input 1 to output...


1
#1: s
#2: 0
#3: 0
#4: 0
#5: 0
#6: 0
#7: 0
Transfer function from input 2 to output...
#1: 0
1
#2: ------s^2 + s
#3: 0
#4: 0
#5: 0
#6: 0
#7: 0
Transfer function from input 3 to output...
#1: 0
#2: 0

1
#3: ------s^2 + s
#4: 0
#5: 0
#6: 0
#7: 0
Transfer function from input 4 to output...
#1: 0
#2: 0
#3: 0
#4: -2
#5: 0
#6: 0
#7: 0
Transfer function from input 5 to output...
#1: 0
#2: 0
#3: 0
#4: 0
#5: -1
#6: 0
#7: 0
Transfer function from input 6 to output...

#1: 0
#2: 0
#3: 0
#4: 0
#5: 0
1
#6: s
#7: 0
Transfer function from input 7 to output...
#1: 0
#2: 0
#3: 0
#4: 0
#5: 0
#6: 0
-1
#7: ----s + 1

append Sys
Sys
(3)
Q=[1 6 5;
% 1 6 5
2 1 7;
% 2 1 7
3 2 0;
% 3 2
4 3 0;
5 4 0;
6 2 0;
7 3 0;]
INPUTS=1;
% 1
OUTPUTS=4;
% 4

Q =
1

Q 7
INPUTS OUTPUTS

(4) connect
G =connect(Sys,Q,INPUTS,OUTPUTS)

Transfer function:
-2 s^2 - 2 s
---------------------------------------------------------s^7 + 3 s^6 + 3 s^5 + s^4 - s^3 - 3 s^2 - 3 s - 6.661e-016

connect
blkbuild
(2)
nblocks=7;
% 7
n1=1; d1=[1 0];
% 1
n2=1; d2=[1 1 0];
n3=1; d3=[1 1 0];
n4=-2; d4=1;
n5=-1; d5=1;
n6=1; d6=[1 0];
n7=-1;d7=[1 1];
nblocks 7 ni
di
blkbuild
blkbuild
State model [a,b,c,d] of the block diagram has 7 inputs and 7 outputs.

7 a
bcd
(3) Q Q
(4) connect
[A,B,C,D]=connect(a,b,c,d,Q,INPUTS,OUTPUTS)
A =

-1

-1

-1

-1

-2

B =
1
0
0
0
0
0
0
C =
0
D =
0

6.4
6.4.1
MATLAB initial

initial(G,x0, Ts)
%
initial(G1,G2,,x0, Ts)
%
[y,t,x]=initial(G,x0, Ts)
%
G x0 Ts
y t
x
6.8
G1 =

12
1
H =

s+4
s+3

[1 2] 6.7
G1=tf(12,[1 4]);
H=tf(1,[1 3]);
GG=feedback(G1,H)

Transfer function:
6.7

12 s + 36
-------------s^2 + 7 s + 24
G=ss(GG);
initial(G,[1 2])

2.
dinitial

dinitial(a,b,c,d,x0)
%
y= dinitial (a,b,c,d,x0)
%
[y,x,n]= dinitial (a,b,c,d,x0)
% n
abcd x0 y t
x n

6.4.2
1.
impluse

impulse(G, Ts)
%
[y,t,x]=impulse(G, Ts)
%
G y
t x t x Ts
6.8 6.8
impulse(G)
%
t=0:0.1:10;
y=impulse(G,t)
% t

6.8

2.
dimpulse


dimpluse(a,b,c,d,iu)
[y,x]=dimpluse(a,b,c,d,iu,n)
[y,x]=dimpluse(num,den,iu,n)

%
% n
% n

iu n y n x

6.9 6.9

6.9

a=[-2 0;0 -3];


b=[1;1];
c=[1 -4];
d=1;
dimpulse(a,b,c,d,1,10)

% 10

6.4.3
1.
step

step(G, Ts)
%
[y,t,x]=step(G, Ts)
%
impulse
6.10 6.6
6.10
G1=tf(12,[1 4]);
H=tf(1,[1 3]);
G=feedback(G1,H)

Transfer function:
12 s + 36
------------- 6.10

s^2 + 7 s + 24
step(G)

step t 6.11
t1=0:0.1:5;
y1=step(G,t1);
plot(t1,y1)
t2=0:0.5:5;
y2=step(G,t2);
plot(t2,y2)

6.11 (a)

(b)

2.
dstep dimpluse

6.4.4
1.
lsim

lsim(G,U,Ts)
%
lsim(G1,G2,U,Ts)
%
[y,t,x]=lsim(G,U,Ts)
%
U Ts U Ts
t x

6.11
6.12

6.12

t=0:0.1:5;
u=sin(t);
G1=tf(1,[1 1.41 1])

Transfer function:
1
---------------s^2 + 1.41 s + 1
G2=tf(1,[1 0.6 1])

Transfer function:
1
--------------s^2 + 0.6 s + 1
lsim(G1,'r',G2,'bo',u,t)

2.
dlsim

dlsim(a,b,c,d,U)
%
[y,x]=dlsim(num,den,U)
%

[y,x]=dlsim(a,b,c,d,U)
%
U

6.12 Z G(z) =

2 + 5z 1 + z 2
1 + 2z 1 + 3z 2

6.13
num=[2 5 1];
den=[1 2 3];
t=0:0.1:5;
u=sin(t);
y=dlsim(num,den,u);

6.13

6.4.5
1.
(1) pole

p=pole(G)

(2) tzero

z=tzero(G)
%
[z,gain]=tzero(G)
%
tzero
5s + 100
6.13 G(s) = 4

s + 8s 3 + 32s 2 + 80s + 100


num=[5 100];
den=[1 8 32 80 100];
G=tf(num,den);
p=pole(G)
p =
-1.0000 + 3.0000i
-1.0000 - 3.0000i

-3.0000 + 1.0000i
-3.0000 - 1.0000i
[z,gain]=tzero(G)

z =
-20
gain =
5

2.
damp n

[wn,zeta]=damp(G)
3.
dcgain

k=dcgain(G)
%
6.13 n
[wn,zeta]=damp(G)
wn =
3.1623
3.1623
3.1623
3.1623
zeta =
0.9487
0.9487
0.3162
0.3162
dcgain(G)

ans =
1

6.5
6.5.1

Gw=polyval(num,j*w)./polyval(den,j*w)
mag=abs(Gw)

pha=angle(Gw)
j
6.14 G(s) =

1
2

s + 1.414s + 1

num=1;
den=[1 1.414 1];
w=1 ;
Gw=polyval(num,j*w)./polyval(den,j*w)

Gw =
0 - 0.7072i
Aw=abs(Gw)

Aw =
0.7072
Fw=angle(Gw)

Fw =
-1.5708

6.5.2
1. bode
bode

bode(G,w)
% bode
[mag,pha]=bode(G,w)
% w
[mag,pha,w]=bode(G)
%
G w mag pha
1
6.15 G(s) =
bode 6.14(a)
s(s + 1)(s + 2)
num=1;
den=conv([1 1],[1 ,2])
den =
1

G=tf(num,[den 0])

Transfer function:
1
----------------s^3 + 3 s^2 + 2 s

bode(G)

% bode

6.15 semilogx 6.14(b)

6.14 (a) Bode

(b) semilogx

w=logspace(-1,2);
[m,p]=bode(num,den,w);
subplot(2,1,1)
semilogx(w,20*log10(m))
subplot(2,1,2)
semilogx(w,p)

2. nyquist
nyquist nyquist

nyquist (G,w)
% nyquist
nyquist (G1,G2,w)
% nyquist
[Re,Im]= nyquist (G,w)
% w
[Re,Im,w]= nyquist (G)
%
G w
{wmin,wmax}Re
Im
6.16
1
1
G2(s) =

G1(s) =
s(s + 1)(s + 2)
(s + 1)(s + 2)
G3(s) =

1
nyquist
s(s + 1)

6.15
num=1;
den1=[conv([1 1],[1 2]),0];
G1=tf(num,den1)

Transfer function:

6.15

nyquist

1
----------------s^3 + 3 s^2 + 2 s
den2=[conv([1 1],[1 2])];
G2=tf(num,den2)

Transfer function:
1
------------s^2 + 3 s + 2
den3=[1 1 0];
G3=tf(num,den3)

Transfer function:
1
------s^2 + s
nyquist(G1,'r',G2,'b:',G3,'g-.',{0.1,180/57.3})
{0.1,180/57.3}

w=1:2;
[re,im]=nyquist(G1,w)
re(:,:,1) =
-0.3000
re(:,:,2) =
-0.0750
im(:,:,1) =
-0.1000
im(:,:,2) =
0.0250

re im (Ny, Nu, Length(w)) Ny Nu

3. nichols
nichols nichols

nichols (G,w)
% nichols
nichols (G1,G2,w)
% nichols
[Mag,Pha]= nichols (G,w)
% w

[Mag,Pha,w]= nichols (G)


%
G(s)/(1+G(s)) nichols
M N M MATLAB nichols M
ngrid

ngrid(new)
% M
new M
nichols new M -40db40db 360o0o

1
6.16 G1(s) =
M nichols
s(s + 1)(s + 2)
6.16

6.16 nichols

ngrid('nichols1')
% M
nichols(G1)
% nichols
w=1:2;
[Mag,Pha]=nichols(G1,w)
%
Mag(:,:,1) =
0.3162
Mag(:,:,2) =
0.0791
Pha(:,:,1) =
-161.5651
Pha(:,:,2) =
-198.4349

ngrid M

6.5.3

% bode

margin(G)

[Gm,Pm,Wcg,Wcp]=margin(G)
%
Gm Wcg Pm Wcp
() Wcg Wcp nan Inf Gm Pm
1
6.16 G1(s) =

s(s + 1)(s + 2)
G1

Transfer function:
1
----------------s^3 + 3 s^2 + 2 s
[Gm,Pm,Wcg,Wcp]=margin(G1)
Gm =
6.0000
Pm =
53.4109
Wcg =
1.4142
Wcp =
0.4457

6.5.4
6.17 G(z) =

2 + 5z 1 + z 2
1 + 2z 1 + 3z 2

bode 6.17

6.17 bode

dnum=[2 5 1];
dden=[1 2 3];
dbode(dnum,dden,0.1)

% bode 0.1s

6.6
6.6.1
MATLAB rlocus

rlocus(G)
%
rlocus(G1,G2,)
%
[r,k]=rlocus(G)
% K
r= rlocus(G,k)
% K
1.
k
6.18 G(s) =
6.18
s(s + 4)(s + 2 4j)(s + 2 + 4j)

num=1 ;
den=[conv([1,4],conv([1 -2+4i],[1 -2-4i])),0]
den =
1

80

G=tf(num,den)

Transfer function:
1
-----------------s^4 + 4 s^2 + 80 s
rlocus(G)
[r,k]=rlocus(G);

%
%

6.18

2.
1-G(s)=0
k(s + 2)
6.19 G(s) =

(s + 3)(s 2 + 2s + 2)
6.19
num=[-1 -2];
den=conv([1 3],[1 2 2]);
G=tf(num,den)

Transfer function:
-s - 2
--------------------s^3 + 5 s^2 + 8 s + 6
rlocus(G)

6.19

6.6.2
1.
MATLAB rlocfind K

[k,p]=rlocfind(G)
%
k p
Select a point in the
graphics window
k p
6.19 6.19 rlocfind
[k,p]=rlocfind(G)
Select a point in the graphics window

selected_point =
-8.2323e-001 -1.5326e-001i
k=
1.8558e+000
p=
-3.3843e+000
-8.0785e-001 +1.5352e-001i
-8.0785e-001 -1.5352e-001i

k
p
2. n

sgrid('new')
%n
sgrid(zeta,wn,'new')
%n
'new'n
new M zeta wn n
k
6.20 G1(s) =
6.20
s(s + 1)(s + 2)
=0.707
num=1;
den=[conv([1 1],[1 2 ]),0];
G1=tf(num,den);
rlocus(G1)
sgrid(0.707,10)

%
%=0.707 n=10

n =0.707

[k,p]=rlocfind(G1)
Select a point in the graphics window
selected_point =
-0.3791 + 0.3602i
k=
0.6233
p=
-2.2279
-0.3861 + 0.3616i
-0.3861 - 0.3616i
G=feedback(k*G1,1)
Transfer function:
0.6233
-------------------------s^3 + 3 s^2 + 2 s + 0.6233
figure(2)
step(G)

6.20 (a) n

(b)

3. RLTool
MATLAB
rltool

rltool
%
rltool(G)
%
k
6.21

s(s + 4)(s 2 + 4s + 20)

num=1;
a=[1 0];
b=[1 4];
c=[1 4 20];
den=conv(a,b);
den=conv(den,c);
G=tf(num,den);
rltool(G);

6.21

sisotool(G)

6.7
6.7.1
1.
MATLAB acker

k=acker(A,B,p)
%SISO
AB p
1
0
0
0

6.22 x = Ax(t) + Bu(t) A = 0 0


1 B = 0
1 5 6
1

p=[-2+2j,-2-2j,-10] k
A=[0 1 0;0 0 1;-1 -5 -6];
B=[0;0;1];
p=[-2+2j -2-2j -10];
k=acker(A,B,p)
k =
79

43

2.
3.

6.7.2
1.
MATLAB lqr

[K,P,E]=lqr(A,B,Q,R)
%
ABQR P Riccati K E

1
0
0
0
6.23 x = Ax(t) + Bu(t) A = 0
0
1 B = 0
35 27 9
1
1 0 0
Q = 0 1 0 R=1
0 0 1

a=[0 1 0;0 0 1;-35 -27 -9];


b=[0;0;1];
q=eye(3);
r=1;
[k,p,e]=lqr(a,b,q,r)
k =
0.0143

0.1107

0.0676

4.2625

2.4957

0.0143

2.4957

2.8150

0.1107

0.0143

0.1107

0.0676

p =

e =
-5.0958
-1.9859 + 1.7110i
-1.9859 - 1.7110i

k e Riccati
2.
MATLAB dlqr

[K,P,E]=dlqr(A,B,Q,R)
%
3.

J=

1
2

[y(t) T Qy(t) + u(t) T Ru(t)]dt

MATLAB lqry Riccati

[K,P,E]=lqry(A,B,Q,R)

Simulink

Simulink

7.1 Simulink
7.1

(1) MATLAB simulink

Simulink (Simulink Library Browser) 7.1

7.1 Simulink

(2)

FileNewModel

untitled

(3) Source+( Source)


Simulink Source

(4) Sine Wave()


untitledSine Wave untitled Sine
Waveadd to 'untitled'Sine Wave
untitled 7.2

7.2 Simulink

(5) SinksScope()
untitled
(6) untitledSine Wave
Scope
7.3
(7) untitled

Simulink

StartScope
7.4

7.3 Simulink
7.4

(8)

Ex0701.mdl

7.2 Simulink
7.2.1 Simulink
1.

MATLAB FileNewModel
7.1 Simulink FileNewModel

7.3 Simulink FileNewModel

2.

MATLAB
Ex0701
MATLAB FileOpen

7.1 Simulink FileOpen

.mdl
7.3 Simulink FileOpen

7.2.2 Simulink

7.5

1.
2.
7.6

exe

7.6

3.
Simulink 7.1
7.1

File

Edit

View

NewModel

Model properties

Preferences

SIMULINK

Print

Close

Simulink

Exit MATLAB

MATLAB

Create subsystem

Mask subsystem

Look under mask

Update diagram

Go to parent

Model browser options

Block data tips options

Library browser

Simulation

Format

Tools

Fit system to view

Normal

(100%)

StartStop

PauseContinue

Simulation Parameters

Normal

Simulink

Accelerator

Simulink

Text alignment

Filp name

ShowHide name

Filp block

Rotate Block

Library link display

ShowHide drop shadow

Sample time colors

Wide nonscalar lines

Signal dimensions

Port data types

Storage class

Data explorer

Simulink debugger

Simulink

Data class designer

Linear Analysis

7.3
7.3.1
1.


Shift
7.7

7.7


EditSelect all
2.
(1) ()

CopyPaste
CopyPaste
(2) ( 7.8 )

Ctrl
CopyPaste

7.8

3.


Shift

4.
Delete EditClearCut

Cut
5.

6.
180
FormatFlip Block 180 7.9
180

90
FormatRotate Block 90 7.9

7.9

7.


FormatFont

FormatHide /Show name

FormatFlip name

7.3.2
1.

2.
(1)

Ctrl 7.10

7.10

(2)
Shift
7.11

7.11

3. (label)


Ctrl

4.

7.3.3
(1)

(2)

7.4 Simulink
7.4.1
Simulink 9
1. (Sources)
7.2
7.2

Constant

Step

Ramp

Sine Wave

Signal Generator

From File

From Workspace

Clock

In

2. (Sinks)
7.3
7.3

Scope

Display

XY Graph

X-Y

To File

To Workspace

Stop Simulation

Out

3. (Continuous)
7.4
7.4

Integrator

Derivative

State-Space

Transfer Fcn

Zero-Pole

Transport Delay

4. (Discrete)
7.5

7.5

Discrete Transfer Fcn

Discrete Zero-Pole

Discrete State-Space

Discrete Filter

Zero-Order Hold

First-Order Hold

Unit Delay

7.4.2
1.
(1) (Sine Wave)
7.13

7.13
Sine type
Time-based Sample-basedAmplitude
Bias Frequency
Phrase Sample time
(2) (Step)

7.14
Step time
initial value Final value
Sample time
7.13

7.14

(3) (From workspace)

7.2 t y
t=0:0.1:10;
y=sin(t);
t=t';
y=y';

From Workspace 7.15(a)Data


[t,y]OK 7.15(b)

7.15 (a)

(b)

DataFrom Workspace
Data+1

t=0:0.1:2*pi;
y=sin(t);
y1=[t;y];
save Ex0702 y1

%Ex0702.mat

(4) (From file)


mat
7.2.mat
t=0:0.1:2*pi;
y=sin(t);
y1=[t;y];
save Ex0702 y1
%
Ex0702.mat
From File
7.16 File name
Ex0702.matOK

(5) (Transfer function)

7.16 From File

7.18

7.17

7.17
Denominator[1 1.414 1] 7.18
(6) (Scope)

7.19

X-Y

7.19

2.
7.22

7.22

(1) (Description)

(2) (Priority)

(3) (Tag)

(4) (Open function)


MATLAB
(5) (Attributes format string)

7.23

7.5
Simulink

7.5.1
SimulationSimulation parameters
7.24

7.24 Solver

1. Solver
(1)
(Start time)
(Stop time)
(2)
Solve options

(3)
Type
(4)
(Output options)

2. Workspace I/O()
7.25 Simulink

7.25 Workspace I/O

(1) (Load from workspace)


(2) (Save to workspace)
Time
Time ()( tout)

States
States ( xout)

Output
Out Output
( yout)
Final state
Final state ( xFinal)

(3) (Save options)


Save options Save to workspace

7.5.2
7.3

1
2

s + 0.6s

(1) SourcesStepContinuousTransfer Fcn


Math OperationsSumSinksScope
(2)

(3) Sum 7.26 Icon shape


rectangularList of signs|+-|
Transfer FcnDenominator[1 0.6

7.26 Sum

0]
StepStep time 0
(4)
7.27

7.27

(5)
Start simulation
Simulink SimulationSimulation parameters
SolverStop time 15Start simulation
15
Y Y Y-min 0Y-max 2
Title 7.28

7.28

(1) (Integrator) (zero-pole) Math


OperationsGain-1
(2) GainFlip Block
(3) zero-poleZeros[]Poles
[-0.6]
GainGain-1 7.29

7.29

SinksOut

Workspace I/O ()TimeOutput


toutyout
7.30
plot(tout,yout)

7.30 plot

7.5.3
7.4

(1) StepTransfer FcnSum


ScopeGainDiscreteDiscrete
FilterZero-Order Hold
(2) GainZero-Order Hold
(3) Discrete FilterZero-Order HoldSample time
0.1s
(4) 7.31

7.31

(5) Simulink
FormatSample time color

(6) Simulink Simulation


Simulation parameters
Max step size 0.05sScopeScope1 7.32

7.32 T=Tk=0.1 (a) d(k)

(b) y(t)

T=Tk=0.1
(7) Discrete FilterSample time 0.6s
Zero-Order Hold
Sample timeEditUpdate diagram
Discrete Filter 7.33
T=0.6 Tk=0.1

7.33 T=0.6 Tk=0.1 (a) d(k)

(b) y(t)

(8) Discrete Filter Zero-Order HoldSample time


0.6s 7.34
T=Tk=0.6

7.34 T=0.6 Tk=0.1 (a) d(k)

(b) y(t)

7.5.4

7.5 7.4 7.35

7.35

Ex0705_1.m
% Ex0705_1

T=0.1;
Tk=0.6;
k=0.03;
zt1=1.44;zt2=-1.26;zt3=1;zt4=-1;
tf11=6.7;tf12=0.1;tf13=1;
tf21=1;tf22=3;tf23=1

%
%
%Gain

MATLAB
Ex0705_1

7.6
7.6.1

1.
7.6 7.4

EditCreate subsystem 7.36

7.36

Subsystem 7.37
In1Out1
2.
7.7 7.6 7.6

7.36 Subsystem
7.37 7.38

7.37
7.38

7.39

7.39

7.6.2
1. (Enabled Subsystem)

7.41 (a) Enable

(b) Out1

7.8
Sine waveScope
Enabled Subsystem

7.42 (a)

(b)

Sine waveEnabled Subsystem


7.42(a)
Enabled Subsystem
7.42(b)
2. (Triggered Subsystem)
7.9

7.43 (a)

(b)

Sine waveScope
Triggered SubsystemSourcesPulse Generator

Pulse GeneratorTriggered Subsystem


7.43(a)
Triggered SubsystemPulse Generator
7.43(b)
3. (Enabled and Triggered Subsystem)

Simulink 0 0

7.6.3
1.
(1)
(2) EditMask subsystem
(3) IconParametersInitialization
Documentation
2. Icon
Icon 7.44

7.44 Icon

(1) Drawing commands


Drawing
commands Examples of drawing commands plot
disptextport_labelimagepatchcolordrootsdploy fprintf
(2) Icon Options

3. Parameters
Parameters 7.45

7.45 Parameters

(1) Add

Delete

Move up

Move down

(2) Dialog Parameters


Prompt
Variable
Edit
Checkbox
type
Popup
EvaluatetypeEvaluate
Literal
7.6
7.6 Assignment
Evaluate
type

on

off

Edit

Checkbox

1 0

on off

Popup

(3) Options for selected parameter


PopstypePopup
Callback
4. Initialization
Initialization
5. Documentation
Documentation Help Mask
typeMask DescriptionMask help

(1) Mask type

(2) Mask Description

(3) Mask help

6.
ApplyUnmask

7.10
zeta
wn
(1) zeta wn
7.46

7.46

(2) EditCreate Subsystem


7.47

7.47

(3) EditMask subsystem zeta


wn
Icon Drawing commands
disp('')
plot([0 1 2 3 10],-exp(-[0 1 2 3 10]))

Parameters Add Prompt


typePopupedit
Variablezetawn Popups0 0.3 0.5 0.707 1 2 7.48(a)

Initialization 7.48(b)

7.48 (a) Parameters

(b) Initialization

Documentation 7.48(c)

7.48(c) Documentation

OK 7.49(a)
7.49(b)
zeta wn

7.49 (a)

(b)

7.7 MATLAB Simulink


7.7.1 MATLAB Simulink
1. Simulink
(1)
new_system MATLAB Simulink

new_system(newmodel,option)
%
newmodeloption LibraryModel
Model
(2)
open_system Simulink

open_system(model)
%
model
(3)
save_system .mdl

save_system(model,)
%
model

7.11 MATLAB
new_system('Ex0711model')
open_system('Ex0711model')
save_system('Ex0711model','Ex0711')

%
%
%

2.
(1)
add_block

add_block(, 1, 1 2,

2,)
Simulink
built-in/Sine Wave

(2)
add_line

add_line(,/, /)
add_line(,m)
m

7.11 MATLAB
add_block('built-in/Step','Ex0711/Step','position',[20,100,40,120])
%
add_block('built-in/Sum','Ex0711/Sum','position',[60,100,80,120])
% Sum
add_block('built-in/Transfer Fcn','Ex0711/Fcn1','position',[120,90,200,130])
%
add_block('built-in/Scope','Ex0711/Scope','position',[240,100,260,120]) %
add_line('Ex0711','Step/1','Sum/1')
%
add_line('Ex0711','Sum/1','Fcn1/1')
add_line('Ex0711','Fcn1/1','Scope/1')
add_line('Ex0711','Fcn1/1','Sum/2')

'position''Ex0711'
7.50

7.50

3.
(1)
7.11
f1=simget('Ex0711')
f1 =
AbsTol: 'auto'
Debug: 'off'
Decimation: 1
DstWorkspace: 'current'
FinalStateName: ''
FixedStep: 'auto'
InitialState: []
InitialStep: 'auto'
MaxOrder: 5
SaveFormat: 'Array'

%
%
% 1 1
%
%
%
%
%
%
%

MaxDataPoints: 1000
MaxStep: 'auto'
MinStep: []
OutputPoints: 'all'
OutputVariables: 'ty'
Refine: 1
RelTol: 0.0010
Solver: 'ode45'
SrcWorkspace: 'base'
Trace: ''
ZeroCross: 'on'

%
%
%
%
%
%
%
%
%
%

(2)
(3)
7.11 7.3
set_param('Ex0711','StopTime','15')
%
set_param('Ex0711/Step','time','0')
%
set_param('Ex0711/Sum','Inputs','+-')
% Sum
set_param('Ex0711/Fcn1','Denominator','[1 0.6 0]')
%

7.51

7.51

4.
(1)

delete_block('Ex0711/Scope')

(2)

7.7.2 MATLAB Simulink


sim

[t,x,y]=sim(model,timespan,options,ut)
%

[t,x,y1,y2,]=sim(model,timespan,options,ut)%

modeltimespan [t0,tf]
[]options
ut t x y
timespan options ut
7.11 7.50

[t,x,y]=sim('Ex0711',[0,15]);
plot(t,x(:,2))

7.50

7.8 Simulink
Simulink 7.1 (Block and
Toolboxes)

(Control System Toolbox)


(Communications Blockset )
(DSP Blockset)
(NCD Blockset)
(Fixed-Point Blockset)
(StateFlow)
(System ID Blocks)
(Neural Network Blockset)
(Fuzzy Logic Toolbox)

8 MATLAB
8.1 MATLAB
8.1.1 MEX
MEX
(1) C FORTRAN MEX MATLAB
M
(2) MATLAB for
MEX
(3) A/DD/A PC MEX
(4) MEX Windows
1. MEX
Microsoft Visual C/C++ 6.0 mex -setup
MEX
>> mex -setup

Please choose your compiler for building external interface (MEX) files:
Would you like mex to locate installed compilers [y]/n? y

mex y

Select a compiler:
[1] Digital Visual Fortran version 6.0 in C:\Program Files\Microsoft Visual Studio
[2] Lcc C version 2.4 in D:\MATLAB6P1\sys\lcc
[3] Microsoft Visual C/C++ version 6.0 in D:\Program Files\Microsoft Visual Studio
[0] None

MEX 3

Please verify your choices:


Compiler: Microsoft Visual C/C++ 6.0
Location: D:\Program Files\Microsoft Visual Studio
Are these correct?([y]/n):

y
The default options file:
"D:\Documents and Settings\ZJDCY\Application Data\MathWorks\MATLAB\R12\mexopts.bat"
is being updated from D:\MATLAB6P1\BIN\WIN32\mexopts\msvc60opts.bat...
Installing the MATLAB Visual Studio add-in ...
Updated D:\Program Files\Microsoft Visual Studio\common\msdev98\template\MATLABWizard.awx
from D:\MATLAB6P1\BIN\WIN32\MATLABWizard.awx

Updated D:\Program Files\Microsoft Visual Studio\common\msdev98\template\MATLABWizard.hlp


from D:\MATLAB6P1\BIN\WIN32\MATLABWizard.hlp
Updated D:\Program Files\Microsoft Visual Studio\common\msdev98\addins\MATLABAddin.dll
from D:\MATLAB6P1\BIN\WIN32\MATLABAddin.dll
Merged D:\MATLAB6P1\BIN\WIN32\usertype.dat
with D:\Program Files\Microsoft Visual Studio\common\msdev98\bin\usertype.dat

2.
MATLAB MATLAB extern\examples\mex

mex yprime.c
extern\examples\mex yprime.dll yprime.dll

>> which yprime


D:\MATLAB6p1\extern\examples\mex\yprime.dll

yprime.dll
>> yprime(1,1:2:7)
ans =
3.0000
14.9925

7.0000

-1.0377

3. C MEX

(1)
MATLAB
mexFunction
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] )
{
/* MATLAB */
}

mexFunction
nrhs
prhs
nlhs
plhs
(2)

4. C MEX
(1) MATLAB API
MEX MATLAB API
mxGetM
mxGetN
mxGetPr

mexErrMsgTxt MATLAB
mxCreateString
mxCreateDoubleMatrix
mxIsDouble
(2) C MEX
MEX
8.1 Microsoft Visual C++6.0 MEX

Microsoft Visual C++6.0 C++ Source File


Ex0801.cpp

#include "mex.h"
#include "math.h"
/* Ex0801*/
void Ex0801(double y[],double x[])
{
y[0]=1/(x[0]*x[0]);
return;
}
/* mexFunction*/
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
double *x,*y;
unsigned int m,n;
/**/
if(nrhs!=1)
{
mexErrMsgTxt("Only one input argument allowed.");
}
/**/
else if(nlhs!=1){
mexErrMsgTxt("Only one output argument allowed.");
}
m = mxGetM(prhs[0]);
n = mxGetN(prhs[0]);
/**/
if (!mxIsDouble(prhs[0]) || mxIsSparse(prhs[0]) ||mxIsComplex(prhs[0])||
! (m==1&&n==1))
{
mexErrMsgTxt("Input argument must be a scalar.");
}
/**/
plhs[0]=mxCreateDoubleMatrix(m,n,mxREAL);
y = mxGetPr(plhs[0]);
x = mxGetPr(prhs[0]);
/**/
Ex0801(y,x);
}

MATLAB
MEX
mex Ex0801.cpp
>> y=Ex0801(2)
y=
0.2500

cpp C++ Ex0801.dll Ex0801.dll

M
MATLAB Ex0801.dll
Ex0801.M
%function y=Ex0801(x)
%
% y=1/(x^2)
% copyright @ 2003-10-10

MATLAB help M
>> help Ex0801
function y=Ex0801(x)

y=1/(x^2)
copyright @ 2003-10-10

8.1.2 MATLAB MEX EXE


1. MEX EXE
MEX MATLAB EXE MATLAB

2.
MEX 8.1 MATLAB Compiler
EXE

8.1 MATLAB

3. M C MEX
MATLAB M MEX mcc x
8.2 MATLAB M
MATLAB / Ex0802.m

function Ex0802
% Ex0802
x=0:0.1:2*pi;
y=sin(x);
plot(x,y)

MATLAB M MEX
>> mcc -x Ex0802
>> Ex0802

% Ex0802.dll
% Ex0802.dll

Ex0802.dll dll
Ex0802.cEx0802.h Ex0802_mex.c
Ex0802.m M mcc

>> mcc -x Ex0802


???
Error:
Cannot
create
MEX
File
"Ex0802"
Documents\CAOYi\BookMATLAB\20031008\exe\Ex0802.dll".
Error in ==> D:\MATLAB6p1\toolbox\compiler\mcc.dll

from

MEX

File

"C:\My

4. EXE
8.3 mbuild C EXE
matlab\extern\examples\cmathex1.c EXE
matlab\extern\examples\cmath mbuild
LCC Microsoft Visual C/C++ MATLAB

>> mbuild ex1.c


Please choose your compiler for building standalone MATLAB applications:
Would you like mbuild to locate installed compilers [y]/n? y

Select a compiler:
[1] Lcc C version 2.4 in D:\MATLAB6P1\sys\lcc
[2] Microsoft Visual C/C++ version 6.0 in D:\Program Files\Microsoft Visual Studio
[0] None
Compiler: 2

mbuild -setup
(1) mbuild C EXE
8.3 mbuild C EXE
mbuild ex1.c
>> mbuild ex1.c

ex1.exe MATLAB
bin
(2) mcc M EXE
8.4 mcc 8.2 M EXE
>> mcc -B sglcpp Ex0802.m

% C++

MCC mbuild-cmcc -mc


mcc -pc

8.2
8.2.1
1.

fid = fopen(filename,permission)
%
[fid,message] = fopen(filename,permission)
%
fid (File Identifier)-1
message
filename MATLAB
permission
'r'
'r+'
'w'
'w+'
'a'

'a+'

fopen permission
t permission b'wb'
'rb+'
2.

status=fclose(fid)
%
status=fclose('all')
%
status 0-1
fid
8.5
Ex0805.txt MATLAB
type Ex0805.txt
a 1 2 3
b 4 5 6

fopen fclose
[fid,message]=fopen('Ex0805.txt','w+')

fid =
3
message =
''
if fid==-1
disp('')
else
disp('')
end

status=fclose(fid)

status =
0

8.2.2
1. fscanf
fscanf

[a,count]=fscanf(fid,format,size)
%
fid format
%
%c%d%e%f%g%i%o%s%u%x ( C )count
a size
size
n n
inf
[m,n] mn
0
8.5 Ex0805.txt
fid=fopen('Ex0805.txt')
fid =
3
a1=fscanf(fid,'%s',4)
a1 =
a123
fclose(fid)
ans =

2. fprintf
fprintf

count=fprintf(fid,format,a,)
%
fid a a fid
format fscanf %E%G%X
-()+()0()count
8.6 Ex0805.txt
a='%This is a example.';
fid=fopen('Ex0805.txt','a+')
% Ex0803.txt
fid =
3
fprintf(fid,'%s',a)

% a

ans =
19
fclose(fid)

ans =
0
fid=fopen('Ex0805.txt','r')

% Ex0803.txt

fid =
3
fscanf(fid,'%s')

ans =
a123b456%Thisisaexample.%Thisisaexample.%Thisisaexample.%Thisisae
xample.
fclose(fid)
ans =
0

3. fgetl fgets
fgetl fgets fgetl fgets

tline=fgetl(fid)
%
tline=fgets(fid)
%
tline=fgets(fid,nchar)
%
fid tline -1nchar

8.6 Ex0805.txt
fid=fopen('Ex0805.txt','r') ;
% Ex0803.txt
fgetl(fid)
%
ans =
a 1 2 3
fgets(fid)

ans =
b 4 5 6
fgets(fid,10)

% 10

ans =
%This is a
fgets(fid,10)
ans =
example.%

8.2.3
fread

[a,count]=fread(fid,size,precision,skip)
%
fid size fscanf precision
'uchar''schar''int8''int16''int32''int64''unit8''unit16'
'unit32''unit64''single''float32''double''float64'a count
skip
2.
fwrite

count=fwrite(fid,a,precision,skip)
%
fid a precision skip fread count

8.7 MAT
x1=1:10;
[fid,message]=fopen('Ex0805.mat','a')
%

fid =
4
message =
''
count1=fwrite(fid,x1)

count1 =
10
x2=11:15;
count2=fwrite(fid,x2)

count2 =
5
status=fclose(fid);
fid=fopen('Ex0805.mat','r');
a1=fread(fid,[2,5])

%
%

a1 =
1

10

a2=fread(fid,[1,5])
a2 =
1

fclose(fid);

8.2.4
1. fseek
fseek

status=fseek(fid,offset,origin)
%
fid offset offset >0
0 status 0-1origin

'bof' -1
'cof' 0
'eof' 1
2. ftell
ftell


pos=ftell(fid)
pos

3. frewind
frewind

frewind(fid)
4. feof
feof 1 0

feof(fid)
8.8 mat Ex0808_1.mat 110
Ex0808_2.mat 123

Ex0808.m
% Ex0808
x=1:10;
s=0;
fid1=fopen('Ex0808_1.mat','w+')
fwrite(fid1,x);
frewind(fid1);
while feof(fid1)==0
a1=fread(fid1,1)
if isempty(a1)==0
s=a1+s
end
end
fclose(fid1);
y=[1 2 3];
fid2=fopen('Ex0808_2.mat','w+')
fwrite(fid2,y)
fseek(fid2,-2,'eof')
a2=fread(fid2,1)
s=s*a2
fclose(fid2);

%
%
%
%
%
%
%

%
%
%
%

s=
110


while
feof 0
isempty
feof 1
fseek(fid2,-2,'eof') 2

8.3
MATLAB
.fig

tifbmpjpg
FileExport
Export

8.2

8.4 Notebook
8.2 Export

8.4.1 Notebook
MATLAB 6.5 Notebook
(1) MATLAB MATLAB
(2) notebook -setup

>> notebook -setup


Welcome to the utility for setting up the MATLAB Notebook
for interfacing MATLAB to Microsoft Word
Choose your version of Microsoft Word:
[1] Microsoft Word 97
[2] Microsoft Word 2000
[3] Microsoft Word 2002 (XP)
[4] Exit, making no changes
Microsoft Word Version:

(3) Word 2

Notebook
Notebook setup is complete.

winword.exenormal.exe

8.4.2 Notebook
1. M-book

(1) Word Notebook


M-book
Word m-book.dot
8.3 Word2000
The MATLAB Notebook v1.doc

8.3 word

(2) MATLAB Notebook


MATLAB notebook Notebook

notebook
% M-book
notebook FileName
% M-book
FileName
M-book
>> notebook 'C:\My Documents\MyMbook0801.doc'

2. M-book
M-book Word NotebookNotebook 8.4

8.4 Notebook

8.1
8.1 Notebook

Define Input Cell

Alt+I

Define AutoInit Cell

Alt+A

Define Calc Zone

Alt+Z

Undefine Cells

Alt+U

Purge Output Cells

Alt+P

Group Cells

Alt+G

Ungroup Cells

Alt+p

Hide/Show Cell Markers

Alt+C

Toggle Graph Output for cell

Evaluate Cell

Ctrl+Enter

Evaluate Calc Zone

Alt+Enter

Evaluate M-book

Alt+M

M-book

Evaluate Loop

Alt+L

Bring MATLAB to front

MATLAB

Notebook Options

Alt+O

8.4.3 Notebook
Notebook MATLAB AutoInitCalcError
InputNographOutput Word Notebook
8.2
8.2 M-book

Input

10 (points) Courier New

Output

()

10 (points) Courier New

AutoInit

10 (points) Courier New

Error

10 (points) Courier New

1.
(1)
MATLAB
Alt-DNotebookDefine Input Cell

8.9 M-book ( M-book )

x=1:0.1:3;

x=1:0.1:3;Alt-D
x=1:0.1:3;

(2)
MATLAB
Ctrl-EnterNotebookEvaluate Cell

8.9
MATLAB
y=sin(x)

y=sin(x)Ctrl-Enter

y=sin(x)
y =
Columns 1 through 7
0.8415
0.8912
0.9320
Columns 8 through 14
0.9917
0.9738
0.9463
Columns 15 through 21
0.6755
0.5985
0.5155

0.9636

0.9854

0.9975

0.9996

0.9093

0.8632

0.8085

0.7457

0.4274

0.3350

0.2392

0.1411

x MATLAB M-book MATLAB


y=sin(x)

2.
MATLAB
NotebookDefine AutoInit Cell MATLAB
AutoInit
3.

(1) MATLAB NotebookDefine


Input CellNotebookDefine AutoInit Cell
(2) Notebook
Group Cells
8.9
M-book
x=1:0.1:3;
y=sin(x)

plot(x,y)

NotebookGroup Cells

x=1:0.1:3;
y=sin(x)
plot(x,y)

4.
MATLAB
8.9

Ctrl-Enter
y []

x=1:0.1:3;
y=sin(x)
plot(x,y)
y =
Columns 1 through 7
0.8415
0.8912
0.9320
0.9636
Columns 8 through 14
0.9917
0.9738
0.9463
0.9093
Columns 15 through 21
0.5985
0.5155
0.4274
0.6755

0.9854
0.8632
0.3350

0.9975
0.8085
0.2392

0.9996
0.7457
0.1411

0.9

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1
1

1.2

1.4

1.6

1.8

2.2

2.4

2.6

2.8

5.
8.10
Word NotebookDefine Calc Zone
NotebookDefine
Input CellNotebook
Evaluate Calc Zone
[]

1. Ifelseend
Ifelseend
x=100*rand(1);
if x>=90
score=''
elseif x>=80
score=''
elseif x>=70
score=''
elseif x>=60
score=''
else
score=''
end
score =

6.
NotebookUndefine Cells
Alt-UNormal

8.4.4 Notebook MATLAB


1.

clear
2. M-book
NotebookEvaluate M-book M-book
MATLAB
3.
NotebookNotebook Options 8.5

(1)
8.5 Numeric Format 8
Short

LongHexBankPlusShorteRational
(2)
8.5 LooseCompact

(3)
Embed Figures in M-book
M-book
(4)
8.5 Figure Options

8.5 Notebook Options

UnitsWidthHeightUnits
InchesCentimeters Points
(5)
(6)
Word /
/
Notebook OptionsEmbed Figures in M-book
OK
MATLAB
8.11 peaks
NotebookNotebook OptionsWidthHeight
2Evaluate M-book M-book

peaks [x,y,z]=peaks;
mesh(x,y,z)

4.
Notebook
Notebook
Evaluate Loop 8.6

5. M-book

NotebookPurge
Selected output Cells

8.6

You might also like