Professional Documents
Culture Documents
MATLAB
MATLAB
MATLAB MATLAB
MATLAB
2-1
MATLAB +-*/
^ MATLAB
MATLAB Command
Window>> Enter
>>
(5*2+3.5)/5
ans =
2.7000
MATLAB ans MATLAB
Answer
2.7000
MATLAB
;
>>
(5*2+3.5)/5;
MATLAB
8 >> MATLAB Command W indow
Command Prompt MATLAB
8 >> MATLAB
W indows 95/98
W indows NT
MATLAB MATLAB
2-2
MATLAB
Chapter
MATLAB
MATLAB
ans
ans
>>
ans
ans =
2.7000
x
>>
x = (5*2+3.5)/5
x =
2.7000
MATLAB %
Comments
>>
y = (5*2+3.5)/5;
% y
>>
z = y^2
% z
z =
7.2900
MATLAB
MATLAB
MATLAB ,;
>>
z =
7.5000
>>
z = 10*sin(pi/3)*...
2-3
MATLAB
sin(pi/3);
>>
8
8
8 31 MATLAB MATLAB 4
19
2-2
MATLAB Scalars
MATLAB VectorsMatrix
>>
s = [1 3 5 2];
>>
t = 2*s+1
% []
t =
3
11
MATLAB [] [1 3 5 2 ]
14 [1 3 5 2 ] s
[3 7 11 5] t
2-4
MATLAB
Chapter
MATLAB
8 S = [1 3 5 2] S = [1, 3, 5, 2]
MATLAB
>>
% t 2
t(3) = 2
t =
3
>>
5
% t 10
t(6) = 10
t =
3
>>
10
% t
t(4) = []
t =
3
>>
10
% s
s(2)*3 + t(4)
% t
ans =
9
>>
% t
t(2:4) - 1
ans =
6
-1
mn m
n
;
>>
A = [1 2 3 4; 5 6 7 8; 9 10 11 12];
% 34 A
>>
% A
2-5
MATLAB
A =
1
5
9
2
6
10
3
7
11
4
8
12
% A 5
A(2,3) = 5
>>
A =
1
5
9
>>
2
6
10
3
5
11
4
8
12
% A
B = A(2,1:3)
% B
B =
5
>>
5
% B A
A = [A B']
A =
1
5
9
>>
2
6
10
A(:, 2) = []
3
5
11
4
8
12
5
6
5
% A []
A =
1
5
9
>>
3
5
11
4
8
12
5
6
5
% A
A = [A; 4 3 2 1]
A =
1
5
2-6
3
5
4
8
5
6
MATLAB
Chapter
9
4
>>
11
3
12
2
MATLAB
5
1
%
A([1 4], :) = []
% []
A =
5
9
5
11
8
12
6
5
MATLAB
8 MATLAB
Column-orientedVector
IndexSubscript
A
A(2, 3) A(6) A
Column Vector
8 A(:) A MATLAB
A(:) 81
2-7
MATLAB
2-3
MATLAB
>>
x = 4;
>>
y = abs(x)
% x
y =
4
>>
y = sin(x)
% x
y =
-0.7568
>>
y = exp(x)
% exp(x)
y =
54.5982
>>
y = log(x)
% ln(x)
y =
1.3863
MATLAB i j
>>
z = 5 + 4j
% z = 5 + 4 1
z =
5.0000 + 4.0000i
>>
y = angle(z)
y =
0.6747
2-8
MATLAB
% z
Chapter
>>
y = real(z)
MATLAB
% z
y =
5
>>
y = imag(z)
% z
y =
4
>>
y = conj(z)
% z
y =
5.0000 - 4.0000i
>>
y = exp(j*pi/6)
e j = cos + j sin
y =
0.8660 + 0.5000i
>>
x = [4 2j 9];
>>
y = sqrt(x)
y =
2.0000
% x
1.0000 + 1.0000i
3.0000
sqrt x
>>
x = [1 2 3 0 12];
>>
y = min(x)
% x
y =
0
>>
y = max(x)
% x
y =
2-9
MATLAB
12
>>
% x
y = mean(x)
y =
3.6000
>>
% x
y = sum(x)
y =
18
>>
% x
y = sort(x)
y =
0
12
>>
>>
y = median(x)
% x
y =
4
>>
6
% x
y = prod(x)
y =
28
80
162
MATLAB help
On-line Help
>>
2-10
help sort
MATLAB
% sort
Chapter
MATLAB
SORT
MATLAB
2-11
MATLAB
8 lookfor lookfor
inverseMATLAB inverse
help lookfor
M
8 helpwin MATLAB
2-4
MATLAB LoopCondition
Flow Control for
For-loop
for =
;
end
for end
6
Harmonic Sequence
2-12
MATLAB
Chapter
>>
x = zeros(1,6);
>>
for i = 1:6
MATLAB
% x 16
x(i) = 1/i;
>>
>>
end
>>
disp(x)
1.0000
% x
0.5000
0.3333
0.2500
0.2000
0.1667
x 16 for
i 1 6 [1 2 3 4 5 6 ]
x i 1/i
while While-loop
while
;
end
for while
>>
>>
>>
x = zeros(1,6);
% x 16
i = 1;
while i <= 6
>>
x(i) = 1/i;
>>
i = i + 1;
>>
end
>>
disp(x)
1.0000
0.5000
0.3333
0.2500
0.2000
0.1667
2-13
MATLAB
8 zeros Pre-allocate
MATLAB
zeros ones
MATLAB if
else end
if
;
else
;
end
>>
>>
>>
else
disp('Given random number is smaller than 0.5.');
>>
>>
end
2-14
MATLAB
Chapter
MATLAB
2-5 M
MATLAB
m MATLAB
MATLAB m M
M-files
test.m M MATLAB
test
>>
pwd
ans =
d:\work
>>cd
d:\mlbook\examples
>>type
test.m
% test.m
% test.m
test
% test.m
Start of test.m!
i = 1 ---> i^3 = 1
i = 2 ---> i^3 = 8
i = 3 ---> i^3 = 27
End of test.m!
2-15
MATLAB
H1 Help Line
8 test.m
M lookfor
M test.m test
lookfor testMATLAB test M
test.m
M
Scripts
Functions
test.m MATLAB
MATLAB
FORTRAN
Subroutines
Factorial
MATLAB fact1.m
>>
type fact1.m
>>
2-16
y = fact1(5)
MATLAB
Chapter
MATLAB
y =
120
fact1 fact.m
fact(5) MATLAB Temperary
Workspace n 5
n i
output
MATLAB output y
MATLAB Recursive
n! = n*(n-1)!
>>
type fact2.m
of
given
positive
integer
if n == 1, % Terminating condition
output = 1;
return;
end
output = n*fact(n-1);
2-17
MATLAB
8 MATLAB
n n! prod(1:n)
gamma gamma(n-1)
Terminating Condition
n==1
output 1
M M
2-6
test.m d:\mlbook\examples
MATLAB M
MATLAB
test.m
MATLAB
8 MATLAB test
1. test
2. test
3. test M
4. test M
5. MATLAB
2-18
MATLAB
Chapter
MATLAB
MATLAB path
>>
path
MATLABPATH
d:\mlbook\examples
C:\MATLABR11\toolbox\matlab\general
C:\MATLABR11\toolbox\matlab\ops
Toolboxes
which
>>
which demo
C:\MATLABR11\toolbox\matlab\demos\demo.m
>>
cd(matlabroot);
>>
which test
% MATLAB
>>
addpath('d:\mlbook\examples');
d:\mlbook\examples MATLAB
path MATLAB
test.m
>>
which test
d:\mlbook\examples\test.m
2-19
MATLAB
test
test.m
MATLAB
8 MATLAB
MATLAB
8 addpath
addpath d:\mlbook\examples -end
rmpath rmpath d:\mlbook\examples
8 MATLB pathtool
2-20
MATLAB
Chapter
MATLAB
cd
dir
OS MATLAB OS
win98 !dir DOS dir MATLAB
2-7
MATLAB
Base Workspace
Workspace MATLAB
MATLAB
MATLAB
Workspace who
>>
who
ans
i
s
t
x
y
2-21
MATLAB
whos
>>
whos
Name
Size
Bytes
A
B
ans
i
s
t
x
y
z
2x4
1x3
1x18
1x1
1x4
1x5
1x6
1x1
1x1
64
24
36
8
32
40
48
8
16
Class
double array
double array
char array
double array
double array
double array
double array
double array
double array (complex)
8 MATLAB
8 MATLAB workspace
clear
2-22
>>
clear A
% A
>>
clear all
MATLAB
Chapter
MATLAB
>>
pi
ans =
3.1416
MATLAB
ij
eps
Floating-point
inf
1/0
nan NaN
Not A Number0/0
pi
= 3.1415926...
realmax
realmin
nargin
nargin
MATLAB
save
load
Optionssave
Binary mat
l save matlab.mat
2-23
MATLAB
save
>>cd
>>clear
>>x
d:\mlbook\examples
all
% x, y
>>whos
Name
Size
x
y
1x1
1x3
Bytes
8
24
Class
double array
double array
save test x y
% x y test.mat
>>
dir *.mat
% MAT
test.mat
>>
tguide1.mat
delete test.mat
tguide2.mat
% test.mat
MS Word
ASCII
2-24
MATLAB
Chapter
MATLAB
ASCII
8 save ascii
1. save mat mat
MATLAB
2. save
load load
3. load
4.
5. ASCII
load
2-25
MATLAB
ASCII
>>
clear all;
>>
x = 1:10;
>>
%
% x ASCII
% testfile.dat
>>
load testfile.dat
% testfile.dat
>>
who
ASCII
testfile x
MATLAB
8 MATLAB Load W izard
MATLAB File/Import Data...
MATLAB
2-26
MATLAB
Chapter
MATLAB
2-8 MATLAB
MATLAB
1. exit
2. quit
3. MATLAB
2-27