You are on page 1of 69

MATLAB

MATLAB Cleve Moler



Fortran EISPACK LINPACK
1980
Moler

MATLAB (MATrix LABoratory),


EISPACK LINPACK
Fortran

MATLAB

C Fortran

MATLAB Fortran

MATLAB (

Cleve Moler John Little The MathWorks


Cleve Moler 1984
MATLAB MATLAB C

MATLAB

www.mathworks.com

MATLAB (

The MathWorks 1992 MATLAB


4.0 1993 , Microsoft Windows
1994 4.2
4.0

1997 MATLAB 5.0

1999 MATLAB 5.3


MATLAB

MATLAB (

1.
2.
3.
4.

2000 10 MATLAB 6.0 (Release 12)

MATLAB

MATLAB
MATLAB

MATLAB

20069MATLAB7.3( R2006b
MATLAB
MATLAB
MATLABJava
Windows Compute Cluster Server 2003

MATLAB (

MATLAB
MATLAB
,

MATLAB

MATLAB

MATLAB

MATLAB

MATLAB
1Matrix Laboratory

2
MATLAB

MATLAB
exp2_1.m

MATLAB
1MATLAB
MATLABMATLAB

MATLAB

MATLAB

2
1

2
more off more on
more(n)
q

S=1-12+13+4+
9-4-18;


1
1
19

2
ans ij
pi
nargin
eps nargout
inf realmin
realmax
nan
flops

a
a
2
MATLAB

ans
format
format
MATLAB

format (short)599.1253
format long15 99.12345678900000
format short ee 9.9123e+001
format long ee 9.912345678900000e+001
format bank2 99.12
format hex

exp2_2.m
1
*/\^

MATLAB

MATLAB

2
abs,sin,cos,tan,asin,acos,atan,sqrt,exp,imag,rea
l,sign,
log,log10,conj

MATLAB
1MATLAB

who

whos

clear
clear

2workspace
1save filename variables
variables
filename
Variables

variables

.mat-
ASCIIV4

2load filename variables


savevariables
MATLAB
load save

Variables

variables

3
quit exit



whatdirtype
deletecdwhich
whatmatlab

dir
which

cd pathpath
cd ..
cd
type filenamefilename
delete filenamefilename


1help
MATLBA
MATLAB

help matfun
help general
help graphics
help elfun
help elmat
help datafun
help ops

help polyfun
help lang
help strfun
help control
2helpwin
3helpdesk
4lookfor
5demo

MATLAB

1
a=1; b=2; c=3;
x=[5 b c; a*b a+c c/b]
x=
5.000 2.000 3.000
2.000 4.000 1.500

y=[2,4, 5
3 6 8]
y=
245
368

2
1start:step:end
a=[1:2:10]
startstepend
a=
1step
1 3 5 7 9 step
2a=linspace(n1,n2,n)
n1n2
nn100
a=linspace(1,10,10)
a=
1 2 3 4 5 6 7 8 9 10

3a=logspace(n1,n2,n)
10n110n2
nn50

a=logspace(1,3,3)
a=
10 100 1000
4
eye(m,n); eye(m)
zeros(m,n); zeros(m)
ones(m,n); ones(m)
V=[a1,a2,,an] A=diag(V)
rand(m,n)mn

eye(2,3)
ans=
100
010
zeros(2,3)
ans=
000
000
ones(2,3)
ans=
111
111
V=[5 7 2]; A=diag(V)
A=
500
070
002

eye(2)
ans=
10
01
zeros(2)
ans=
00
00
ones(2)
ans=
11
11

AV=diag(A)
AV


1.

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

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

b=[1+2i 2-7i]'

a=

a=

b=

1.0000 - 2.0000i

2.0000 + 7.0000i

b=[1+2i 2-7i].'
b=
1.0000 + 2.0000i
2.0000 - 7.0000i

2
+ - * \/ ^.* .\ ./.^
a=[1 2;3 4]b=[ 3 5; 5 9]

c=a+b
d=a-b
c=
d=
4 7
-2 -3

8 13
-2 -5
a*b=[13 23; 29 51]
a/b=[-0.50 0.50;3.50 1.50]
a\b=[-1 -1;2 3]
a^3=[37 54; 81 118]
a.*b=[3 10;15 36]

a./b=[0.33 0.40;0.60 0.44]


a.\b=[3.00 2.50;1.67 2.25]
a.^3= [1 8; 27 64]

a\ba*x=b
a/bx*b=a

3
inv(A)
det(A)

a=[1 2 3; 4 5 6; 2 3 5];
b=inv(a)
b=
-2.3333 0.3333 1.0000
2.6667 0.3333 -2.0000
-0.6667 -0.3333 1.0000
det(a)
ans =
-3

4
MATLABexpsqrt

m
expm,sqrtm


1
MATLAB

A(m,n)mn
A(:,n)n
A(m,:)m
A(m1:m2,n1:n2)m1m2n1
n2
A(:)

[ ]

2
[m,n]=size(A,x)mnx=1
mx=2n
length(A)=max(size(A))
rank(A)
a=[1 2 3;3 4 5];

length(a)

rank(a)

[m,n]=size(a)

ans =

ans =

m=
2
n=
3

max(size(a))
ans =
3

3flipudfliplrrot90


1
MATLAB
4
3
2
x

12
x
+
0
x
+ 25 x + 116

p=[1 -12 0 25 116]roots


0
0poly
r=roots(p)
p=poly(r)
r=
p=
1 -12 -0 25 116
11.7473
2.7028
-1.2251 + 1.4672i
-1.2251 - 1.4672i

2
conv
a=[1 2 3] ; b=[1 2] c=conv(a,b)=1 4 7 6
convconv(conv(a,b),c)
deconv
[q,r]=deconv(c,b)
q=1 2 3

r=0 0 0

polyder
polyder(a)=2 2
polyval(p,n)n
polyval(a,2)=11

3*

p=polyfit(x,y,n)xy
np
exp2_15.m
4*

interp1interp2
interp3
method
Help polyfun

y=interp1(xs,ys,x,method)
xsysxy
method4
nearest
linear
spline
cubicspline
exp2_16.m

*MATLAB
1
A
1 U*S*V=AUV
[U,S,V]=svd(A)
U*U=IS
A
a =
[u,s,v]=svd(a)
9 8
u=
6 8
0.7705 -0.6375

0.6375 0.7705
s=
u*u=I
15.5765
0
v*v=I
0
1.5408
u*s*v=a
v=
0.6907 -0.7231
0.7231 0.6907

2
AV
[V,D]=eig(A)
DA*V=V*DD
a =
V
9 8
D=eig(A)
6 8

[v,d]=eig(a)
v=
A*V=V*D
0.7787 -0.7320
0.6274 0.6813
d=
15.4462
0
0 1.5538

3
A
[Q,R]=qr(A)
Q*R=AQ
a =
1
9 8
norm(Q)=1)R
6 8

[q,r]=qr(a)
q=
norm(q) q*r
-0.8321 -0.5547
ans =
ans =
-0.5547 0.8321
1
9.0000 8.0000
r=
6.0000 8.0000
-10.8167 -11.0940
0 2.2188

4
[L,U]=lu(A)
AA=L*U,L
U
L
P
L11
[L1,U1,P]=lu(A)
a=[1 2 3;4 5 6;7 8 9]
[l1,u1,p]=lu(a)
[l,u]=lu(a)

l1 =
1.00
0
0.14 1.00
0.57 0.50
u1 =
7.00 8.00
0 0.86
0
0
p=
0 0 1
1 0 0
0 1 0

0
0
1.00
9.00
1.71
0.00

l=
0.14
0.57
1.00
u=
7.00
0
0

1.00
0
0.50 1.00
0
0
8.00 9.00
0.86 1.71
0 0.00

u1=uinv(p)*l1=l
a=l*u
p*a=l1*u1

2*
1fplot()
2fmin,fmins
3fzero()
4ffty=FFT(x)unwrap()abs
angle
5max,min,mean,sum,prod

6trap2,quad,quad8
3*
[t,x]=ode23(xfun,t0,tf,x0,tol)
[t,x]=ode45(xfun,t0,tf,x0,tol)


MATLAB
help graph2d
help graph3d

1
plotx1,y1,option1,x2,y2,option2,
x1,y1x,yoption1
1
plot
plot(x,y)plot(x,y,option)
option

exp2_3.m exp2_3_.m

2
figure1figure2figure(n)

3grid on
grid off
4hold on

hold off
exp2_4.m

exp2_4_.m

5
axis[xmin xmax ymin ymax]
exp2_5.m
axis(equal)xy
6
text(x,y,)
(x,y)
gtext()

title()

xlabel()ylabel()
xy
\
7legend(1,2,,n)

exp2_5_.m

8subplotmnk
m:n:k:
exp2_6.m
9semilogxx10y

semilogyy10x

exp2_17.m

10

baxx,yhisty,x
stairsx,ystemx,y
exp2_7.m

0x6
sin(2x)sinx2sin2x
x=linspace(0,6)
y1=sin(2*x),y2=sin(x.^2),y3=(sin(x)).^2;
plot(x,y1,x, y2,x, y3)
1
0 .8
0 .6
0 .4
0 .2
0
-0 .2
-0 .4
-0 .6
-0 .8
-1

cos(x)*sin(y)
1
0 .5
0
- 0 .5
-1
4
2
0
-2
-4

4
3
1
2
0 .5
1
0
0
- 0 .5

-1

-1
4

-2
2

5
0

-3

-2
-4

-5

-4
-4

-2

20
40
60
80
100
120
140
160
180
200

50

100

150

200

250

30

MATLAB

MATLBA
1

2clear

clear
3
MATLAB

4input

MATLAB

MATLBA

6Debugger

GUI

7MATLAB

8MATLAB

workspaceclear,close

MATLAB

for,if then,switch,while


end

simulink

MMATLAB
MATLABEditor/Debugger

MATLAB

cd
path browser

MATLAB
MATLAB
MM
function
1M

MATLAB
2
.mMATLAB

myhilb1.m

myhilb.m


help

1function
function[out1,out2,..]=filename(in1,in2,..)
narginnargout
MATLAB
2H1
%lookfor

myhilb1.m

myhilb.m

3
%

4
function

MATLAB

exp2_9.m
2

global
exp2_10.m

disp(text string) disp


text string

disp
a=['this is a';'text string']
??? All rows in the bracketed expression must have
the same number of columns.19
21112

aa=['this is a ';'text string'] disp(aa)


aa =
aa=
this is a
this is a
text string
text string

MATLAB
Meval

fct=prod(1:n); 10
n=10;eval(fct)
ans=3628800

exp2_8.m
pausempause(n)
n
echo on/off
keyboard
return
x=input(prompt)
x

MATLAB

1
A=[1 2 -1 -5]
<
A<B
>
A>B
<= A<=B
>= A>=B
==
A=B
~=
A~=B

B=[0 2 3 1]
ans=[0 0 1 1] A<1 ans=[0 0 1 1]
ans=[1 0 0 0] A>1 ans=[0 1 0 0]
ans=[0 1 1 1]
ans=[1 1 0 0]
ans=[0 1 0 0] ; A=1 ans=[1 0 0 0]
ans=[1 0 1 1] ; A~=1 ans=[0 1 1 1]

2
0
10MATLAB
001
A=[5 -4 0 -0.5]

B=[0 1 0 9]

&
A&B=[0 1 0 1] A&1=[1 1 0 1]
|
A|B=[1 1 0 1]
~
~A=[0 0 1 0]

A|1=[1 1 1 1]

~1=0


1for

for

end
1

Editor

(exp2_11.m)

2while

while

end

exp2_12.m
Whileforwhile
for

3ifelseelseif
1if
end

2if 3 if 1
1
1
else
elseif 2
2
2
end

end
if-else1
22
if-elseif11
22
exp2_13.m exp2_13_.m

4switch
switch
case 1

1
(case)
case 2

2
otherwise
.

otherwise
otherwise
3

end

exp2_14.m


MATLABMATLAB

MATLAB

help
MATLAB

MATLAB

MATLABM