Professional Documents
Culture Documents
MATLAB
MATLAB
MATLAB
MATLAB
m MATLAB
1
MATLAB
1
2
3 19
4
1MATLAB
2%
3...
3
MATLAB
4M
MATLAB
MATLAB . M
function
function
=
M
1. Matlab :File->New->M-file
2.
3. File->SaveM
Matlab M
2
f(x1,x2)=100(x2-x1 ) +(1-x1)
1. M fun.m
function f=fun(x)
f=100*(x(2)-x(1)^2)^2+(1-x(1))^2
2. fun.m
f(1,2), Matlab
x=[1 2]
fun(x)
MATLAB MATLAB
MATLAB MATLAB
1
matlab
1
x=[a b c d e f ]
x=firstlast
first 1 last
x=firstincrementlast
first increment last
x=linspace(firstlastn
first last n
x=logspace(firstlastn
n
2
ix(i) x i
iix(a b c) x a b c
c
b b 1
iiix([a b c d]) x abcd
[x(a) x(b) x(c) x(d)]
3
c=[1234]
4
i-
a=[a1,a2,,an], c=
a+c=[a1+c,a2+c,,an+c]
a.*c=[a1*c,a2*c,,an*c]
a./c= [a1/c,a2/c,,an/c](
a.\c= [c/a1,c/a2,,c/an] (
a.^c= [a1^c,a2^c,,an^c]
c.^a= [c^a1,c^a2,,c^an]
ii-
a=[a1,a2,,an], b=[b1,b2,,bn]
a+b= [a1+b1,a2+b2,,an+bn]
a.*b= [a1*b1,a2*b2,,an*bn]
a./b= [a1/b1,a2/b2,,an/bn]
a.\b=[b1/a1,b2/a2,,bn/an]
a.^b=[a1^b1,a2^b2,,an^bn]
2
Enter
m=[1 2 3 4 5 6 7 89 10 11 12]
p=[1 1 1 1
2222
3 3 3 3]
a=[ ]
b=zeros(mn) m n
c=ones(mn)
m n 1
d=eye(mn)
m n
2
a A r Ar
b A r A
r
c A A A
detA
invA
[VD]=eig[A]
MATLAB
Matlab
Matlab CFORTRAN
Matlab
1
Matlab /
Matlab
1
Matlab
3
MATLAB for while if-else-end
MATLAB MATLAB
MATLAB
ifor
for x=array
{commands}
end
for end {commands}array
x n x=array(
n)
iiWhile
for while
while expression
{commands}
end
(expression) while end
{commands}
iiiIf-Else-End
a
if expression
{commands}
end
(expression) if end
{commands}
x 2 1 x 1
f ( x)
, f (2), f (1)
2
x
x
M fun1.m fx
Matlab fun1(2),fun1(-1)
b
if expression1
{commands1}
else if expression2
{commands2}
else if expression3
{commands3}
else if
else
{commands}
end
end
end
end
x 2 1 x 1
f(x) 2 x
0 x 1, f (2), f (0.5), f (1)
x3
x0
M fun2.m x
f
Matlab fun2(2),fun2(0.5), fun2(-1)
MATLAB
Matlab
1.
Matlab
Matlab
PLOT(X,Y,S)
X,Y
S
y
c
r
m
.
-
o
:
xx- -. +
--
PLOT(X,Y)
PLOT(X,Y1,S1,X,Y2,S2,,X,Yn,Sn)
[0,2*pi] sin(x), cos(x)
x=linspace(0,2*pi,30);
y=sin(x);
z=cos(x);
plot(x,y,'r',x,z,g0')
2.()
(1) ezplot
ezplot(f(x),[a,b])
a<x<b f=f(x)
ezplot(f(x,y),[xmin,xmax,ymin,ymax])
xmin<x<xmax ymin<y<ymax
f(x,y)=0
ezplot(x(t),y(t),[tmin,tmax])
tmin<t<tmax x=x(t),y=y(t)
[0,pi] y=cos(x)
ezplot(sin(x),[0,pi])
3
[0,2*pi] x cos t y sin 3 t
ezplot(cos(t)^3,sin(t)^3,[0.2*pi])
[-20.5][02] e x sin( xy) 0
ezplot('exp(x)+sin(x*y)',[-2,0.5,0,2])
(2) fplot
fplot(fun,lims) fun lims=[xmin,xmax]
[1] fun M x
[2] fplot
[-12] y e 2 x sin(3x 2 )
M myfun1.m
function Y=myfun1(x)
Y=exp(2*x)+sin(3*x.^2)
fplot(myfun1,[-1,2])
[-2,2] tanh
fplot(tanh,[-2,2])
xy [- 2 2 ] tanh(x),sin(x),cos(x)
fplot([tanh(x),sin(x),cos(x)],2*pi*[-1 1 1 1])
3
(1)PLOT3(x,y,z,s)
XYZ n
S
[010*pi] x=sin(t)y=cos(t)z=t
t=0:pi/50:10*pi;
plot3(sin(t),cos(t),t)
rotate3d %
(2)PLOT3(x,y,z)
xyz m*n .
Z=(X+Y).^2.
x=-3:0.1:3;y=1:0.1:5;
[X,Y]=meshgrid(x,y);
Z=(X+Y).^2;
plot3(X,Y,Z)
meshgrid(x,y) x y
4
(1)surf(x,y,z) xyz
Z=(X+Y).^2 .
x=-3:0.1:3;
y=1:0.1:5;
[X,Y]=meshgrid(x,y);
Z=(X+Y).^2;
surf(X,Y,Z)
shading flat
(2)Mesh(x,y,z)
Z=(X+Y).^2 .
x=-3:0.1:3; y=1:0.1:5;
[X,Y]=meshgrid(x,y);
Z=(X+Y).^2;
mesh(X,Y,Z)
(3)meshz(X,Y,Z) curtain (,)
peaks
:
[X,Y]=meshgrid(-3:.125:3);
Z=praks(X,Y);
Meshz(X,Y,Z)
5
1polar (theta,rho,s)
theta rho s
2scatterX,Y,S,C
X Y X Y
3contour (x,y,z,n) n
1contour 3(x,y,z,n) n
2scatter3X,Y,Z,S,C
X,Y Z
X,Y Z
Matlab
ASCII
1
1fopen
fopen
fopen
Fid= fopen(filenamepermission)
filename permission
r
w
a
r+
w+
a+
W
A
std.dat
Fid=fopen( std.dat, r )
ASCII
tr t ASCII
2fclose
Sta=fclose(Fid) Fid
Sta 01
2
1
fread
[A,COUNT]=fread(Fid,size,precision)
A COUNT size
N
N
inf
[M,N] MN
precision
ucharFid=fopen(std.dat, r);
A=fread(Fid, 100, long);
Sta=fclose(fid);
std.dat 100
A
fwrite
COUNT=fwrite (Fid, A, precision)
Fid=fopen(magic5.bin, wb);
fwrite(Fid, magic, int32);
magic magic5.bin 32
test.dat A
A=[-0.6515 -0.2727 -0.4354 -0.3190 -0.9047
-0.7534 -0.4567 -0.3212 -0.4132 -0.3583
-0.9264 -0.8173 -0.7823 -0.3265 -0.0631
-0.1735 -0.7373 -0.0972 -0.3267 -0.6298
s=fscanf(fid, %s)
a=fscanf(fid, %5d)
5
b= fscanf(fid, %6.2d)
fprintf ASCII ,
COUNT= fprintf(Fid, format, A,)
A format A Fid
x = 0: 0.1: 1;
y = [x; exp(x)];
Fid = fopen('exp.txt', 'w');
fprintf(Fid,'%6.2f %12.8f\n',y);
fclose(Fid);
MATLAB
1
MATLAB max min
1
X
(1) y=max(X) X y X
(2) [y,I]=max(X) X y I X
X min(X) max(X)
x=[-43,72,9,16,23,47];
y=max(x)
% x
[y,l]=max(x)
% x
2
A 3
(1) max(A) i A i
(2) [Y,U]=max(A) Y UY A U
sum(X) X
prod(X) X
sum(A) i A i
prod(A) i A i
sum(A,dim) dim 1 sum(A) dim 2
i A i
prod(A,dim) dim 1 prod(A) dim 2
i A i
3
mean median
mean(X) X
median(X) X
mean(A) i A i
median(A) i A i
mean(A,dim) dim 1 mean(A) dim 2
i A i
cumsum(X) X
cumprod(X) X
cumsum(A) i A i
cumprod(A) i A i
cumsum(A,dim) dim 1 cumsum(A) dim 2
i A i
cumprod(A,dim) dim 1 cumprod(A) dim 2
i A i
5
1
MATLAB std Xstd(X)
Astd(A) A
std
Y=std(A,flag,dim) dim 1 2 dim=1 dim=2
flag 0 1 flag=0 1
flag=1 2 flag=0dim=1
2
MATLAB corrcoef corrcoef
corrcoef(X) X X
X
corrcoef(X,Y)X,Y corrcoef([X,Y])
100005 5
X=randn(10000,5);
M=mean(X)
D=std(X)
R=corrcoef(X)
6
MATLAB X sort(X) X
sort A
[Y,I]=sort(A,dim) dim A dim=1 dim=2
Y I Y A
7
MATLAB
a1 x n a2 x n 1 an x an 1
[ a1 a 2 a n 1 ]
1
i
ii
AB CONV
C=conv(A,B)
C
x 8 x 10 2 x x 3
4
A=[1 8 0 0 -10];
B=[2 -1 3]
B=
2
-1
C=conv(A,B)
C=
2
15
-5
24
-20
10
-30
(iii)
AB DECONV
[Q,r]=deconv(A,B)
A B Q
r 0
A=[1 8 0 0 -10];
B=[2 -1 3];
[P,r]=deconv(A,B)
P=
0.5000
4.2500
1.3750
r=
0
0
0 -11.3750 -14.1250
P 0.5x2+4.25x+1.375
r -11.375x-14.125
2
n n MATLAB
roots
x=roots(P)
P x x(1),x(2),,x(n)
n
x 8 x 10
4
A=[1,8,0,0,-10];
x=roots(A)
poly
P=poly(x)
x n poly(x) x
P
6-22 f(x)
(1) f(x)=0
(2) f(x)=0 g(x) f(x)
P=[3,0,4,-5,-7.2,5];
X=roots(P)
% f(x)=0
G=poly(X)
% g(x)
3
MATLAB polyval polyvalm
P x
i
polyval
Y=polyval(P,x)
x x
x 8 x 10 x=1.2 23
4
A=[1 8 0 0 -10];
% 7.8 4
x=1.2;
%
y1=polyval(A,x)
y1 =
-97.3043
x=[-1 1.2 -1.4;2 -1.8 1.6]
% x
x=
-1.0000
1.2000 -1.4000
ii
polyvalm polyval polyvalm
x A P
x 3 5 x 2 8 polyvalm(P,A)
A*A*A-5*A*A+8*eye(size(A))
polyval(P,A)
A.*A.*A-5*A.*A+8*ones(size(A))
4
p=polyder(P) P
p=polyder(P,Q) PQ
[p,q]=polyder(P,Q) P/Q p q
P,Q p,q
x 5
2
P=[1];
Q=[1,0,5];
[p,q]=polyder(P,Q)
MATLAB
MATLAB
1
1
i
Ax=b\
x=A\b
A=[2,1,-5,1;1,-5,0,7;0,2,1,-1;1,6,-1,-4];
b=[13,-9,6,0]';
x=A\b
ii
aLU
LU
A LU
MATLAB lu LU
LU
A=[2,1,-5,1;1,-5,0,7;0,2,1,-1;1,6,-1,-4];
b=[13,-9,6,0]';
[L,U]=lu(A);
x=U\(L\b)
LU 2
[L,U ,P]=lu(A);
x=U\(L\P*b)
bQR
X QR X Q R
QR MATLAB qr QR
[Q,R]=qr(X) Q R X=QR
[Q,R,E]=qr(X) Q R E
XE=QR
QR Ax=b x=R\(Q\b) x=E(R\(Q\b))
QR
A=[2,1,-5,1;1,-5,0,7;0,2,1,-1;1,6,-1,-4];
b=[13,-9,6,0]';
[Q,R]=qr(A);
x=R\(Q\b)
QR 2
[Q,R,E]=qr(A);
x=E*(R\(Q\b))
cCholesky
X Cholesky X
R X=R'RMATLAB chol(X)
X Cholesky
R=chol(X) R R'R=X X
[R,p]=chol(X) X p=0R
p X R q=p-1
R'R=X(1:q,1:q)
A=[2,1,-5,1;1,-5,0,7;0,2,1,-1;1,6,-1,-4];
b=[13,-9,6,0]';
R=chol(A)
??? Error using ==> chol
Matrix must be positive definite
A
2
Jacobi Gauss-Serdel
iJacobi
Ax=b A aii0(i=1,2,,n) A
A=D-L-U D A L U A
Ax=b
x=D-1(L+U)x+D-1b
x(k+1)=D-1(L+U)x(k)+D-1b
Jacobi {x(k+1)} x x Ax=b
Jacobi MATLAB Jacobi.m
function [y,n]=jacobi(A,b,x0,eps)
if nargin==3
eps=1.0e-6;
elseif nargin<3
error
return
end
D=diag(diag(A));
% A
L=-tril(A,-1);
% A
U=-triu(A,1);
% A
B=D\(L+U);
f=D\b;
y=B*x0+f;
n=1;
%
while norm(y-x0)>=eps
x0=y;
y=B*x0+f;
n=n+1;
end
Jacobi 0 10-6
Jacobi.m
A=[10,-1,0;-1,10,-2;0,-2,10];
b=[9,7,6]';
[x,n]=jacobi(A,b,[0,0,0]',1.0e-6)
iiGauss-Serdel
Jacobi
Dx(k+1)=(L+U)x(k)+b Dx(k+1)=Lx(k+1)+Ux(k)+b
x(k+1)=(D-L)-1Ux(k)+(D-L)-1b
Gauss-Serdel Jacobi Gauss-Serdel
a=[1,2,-2;1,1,1;2,2,1];
b=[9;7;6];
[x,n]=jacobi(a,b,[0;0;0])
[x,n]=gauseidel(a,b,[0;0;0])
2
1
MATLAB fzero
z=fzero('fname',x0,tol,trace)
fname x0 fzero
x0 tol tol=epstrace
1 0 trace=0
f(x)=x-10x+2=0 x0=0.5
(1) funx.m
function fx=funx(x)
fx=x-10.^x+2;
(2) fzero
z=fzero('funx',0.5)
z=
0.3758
2
F(X)=0 fsolve fsolve
X=fsolve('fun',X0,option)
X fun X0
option 20
optimset optimset()
Display offiter
finaloptimset(Display,off) Display off
(0.5,0.5)
(1) myfun.m
function q=myfun(p)
x=p(1);
y=p(2);
q(1)=x-0.6*sin(x)-0.3*cos(y);
q(2)=y-0.6*cos(x)+0.3*sin(y);
(2) x0=0.5,y0=0.5 fsolve
x=fsolve('myfun',[0.5,0.5]',optimset('Display','off'))
x=
0.6354
0.3734
q=myfun(x)
q =1.0e-009 *
0.2375
0.2957
3
MATLAB fmin fmins
x=fmin('fname',x1,x2)
x=fmins('fname',x0)
fmin fname
x1 x2 fmins
x0
MATLAB -f(x)(a,b)
f(x)(a,b) fmin(f,x1,x2) f(x)(x1,x2)
f(x)=x3-2x-5 [0,5]
(1) mymin.m
function fx=mymin(x)
fx=x.^3-2*x-5;
(2) fmin
x=fmin('mymin',0,5)
x=
0.8165
1
MATLAB interp1
Y1=interp1(X,Y,X1,'method')
X,Y X1 X,Y
X1 Y1 X1 method
linear nearest
cubic
spline
x xi x
1-12 11 1 5891525
29313022252724 1/10
hours=1:12;
temps=[5 8 9 15 25 29 31 30 22 25 27 24];
h=1:0.1:12;
t=interp1(hours,temps,h,'spline'); ()
plot(hours,temps,'+',h,t,hours,temps,'r:')
%
xlabel('Hour'),ylabel('Degrees Celsius)
2
MATLAB interp2
Z1=interp2(X,Y,Z,X1,Y1,'method')
X,Y Z X1,Y1
Z1 method
X,Y,Z
x,y x,y x y xi,yi
x,y
3
griddata
cz =griddataX,Y,Z,X1,Y1,method
X1 Y1
4
MATLAB polyfit
a=polyfit(x,y,m)
XY m
xi
0.1
0.2
0.4
0.5
0.6
0.7
0.8
0.9
yi
1.978
3.28
6.16
7.34
7.66
9.58
9.48
9.30
11.2
x=0:0.1:1;
y=[-0.447 1.978 3.28 6.16 7.08 7.34 7.66 9.56 9.48 9.30 11.2];
A=polyfit(x,y,2)
z=polyval(A,x);
plot(x,y,'k+',x,z,'r')
%
5
Matlab lsqcurvefit lsqnonlin
M- fun.m f(x) f(x)
1lsqcurvefit
xdata=xdata1xdata2xdatan
ydata=ydata1ydata2ydatan
lsqcurvefit x F(x,xdata)=Fxxdata1
Fx
xdatan
T x()
x = lsqcurvefit (fun,x0,xdata,ydata,options);
fun F(x,xdata) M-, x xdatax0
xdata,ydata options
2lsqnonlin
xdata=xdata1xdata2xdatan
ydata=ydata1ydata2ydatan
lsqnonlin x f(x)=(f1(x),f2(x),,fn(x)) T x
fi(x)=f(xxdataiydatai=F(x,xdatai)-ydatai
x= lsqnonlin funx0options
c (t ) a be
0.02 kt
abk
100
200
300
400
500
600
700
800
900
1000
4.54
4.99
5.35
5.65
5.90
6.10
6.26
6.39
6.50
6.59
1. lsqcurvefit
1 M- curvefun1.m
function f=curvefun1(x,tdata)
f=x(1)+x(2)*exp(-0.02*x(3)*tdata) % x(1)=a; x(2)=bx(3)=k;
2
tdata=100:100:1000
cdata=1e-03*[4.54,4.99,5.35,5.65,5.90,6.10,6.26,6.39,
6.50,6.59];
x0=[0.2,0.05,0.05];
x=lsqcurvefit ('curvefun1',x0,tdata,cdata)
f= curvefun1(x,tdata)
3
f =0.0043
0.0051
0.0056
0.0059
0.0061
0.0062
0.0062
0.0063
0.0063
0.0063
x = 0.0063 -0.0034
0.2542
4a=0.0063, b=-0.0034, k=0.2542
2
lsqnonlin
1 M- curvefun2.m
function f=curvefun2(x)
tdata=100:100:1000;
cdata=1e-03*[4.54,4.99,5.35,5.65,5.90,
6.10,6.26,6.39,6.50,6.59];
f=x(1)+x(2)*exp(-0.02*x(3)*tdata)- cdata
2:
x0=[0.2,0.05,0.05];
x=lsqnonlin('curvefun2',x0)
f= curvefun2(x)
3
f =1.0e-003 *(0.2322 -0.1243 -0.2495 -0.2413
-0.1668
-0.0724 0.0241
0.1159
0.2030 0.2792
x =0.0063 -0.0034
0.2542
4 a=0.0063 b=-0.0034 k=0.2542
,
Matlab
1
min z=cX
s.t. AX b
Aeq X beq
VLBXVUB
[1] x=linprogcAbAeq,beq, VLBVUB
[2] x=linprogcAbAeq,beq, VLBVUB, X0
[1] , Aeq=[ ], beq=[ ].
[2] X0
min z 6 x1 3 x 2 4 x3
s.t.
x1 x 2 x3 1 2 0
x1 30
0 x2 50
x3 20
: M xxgh2.m
c=[6 3 4];
A=[0 1 0];
b=[50];
Aeq=[1 1 1];
beq=[120];
vlb=[30,0,20];
vub=[];
[x,fval]=linprog(c,A,b,Aeq,beq,vlb,vub)
2
min F(X)
s.t AX<=b
Aeq X beq
G(X) 0
Ceq(X)=0
VLB XVUB
X n G(X) Ceq(X)
. Matlab
1 M fun.m, FX:
function f=fun(X);
f=F(X);
2 :G(X) 0 Ceq(X)=0,Mnonlcon.m
G(X)Ceq(X):
function [G,Ceq]=nonlcon(X)
G=...
Ceq=...
3. . fmincon,:
x=fmincon(fun,X0,A,b,Aeq,beq,VLB,VUB,nonlcon,options)
[2] fmincon
BFGS Hessian
[3] fmincon X0
min f ( x) e 1 (4 x1 2 x2 4 x1 x2 2 x2 1)
x
s.t.
x1+x2=0
1.5+x1*x2- x1-x2 0
-x1*x2 10 0
1 M fun4.m,:
function f=fun4(x);
f=exp(x(1))*(4*x(1)^2+2*x(2)^2+4*x(1)*x(2)+2*x(2)+1);
2 M mycon.m
function [g,ceq]=mycon(x)
g=[x(1)+x(2);1.5+x(1)*x(2)-x(1)-x(2);-x(1)*x(2)-10];
3 youh3.m :
x0=[-1;1];
A=[];b=[];
Aeq=[1 1];beq=[0];
vlb=[];vub=[];
[x,fval]=fmincon('fun4',x0,A,b,Aeq,beq,vlb,vub,'mycon')
4
x = -1.2250
1.2250
fval = 1.8951
1
1
b=regress( Y, X )
1
1
X
...
Y1
Y
Y 2
. . .
Yn
b 1
...
p
x11
x12
x21
x22
...
xn1
...
xn 2
... x1 p
... x2 p
... ...
... xnp
2
[b, bint,r,rint,stats]=regress(Y,X,alpha)
bint r rint stats
r2F F p r2 1
F > F1-kn-k-1 H0F F
p H0alpha 0.05
3rcoplotrrint
16
88
85
88
91
92
93
93
95
155
156
157 158
159
160
162
164
96
98
97
98
99
100
102
96
x y
1
x=[143 145 146 147 149 150 153 154 155 156 157 158 159
160 162 164]';
X=[ones(16,1) x];
Y=[88 85 88 91 92 93 93 95 96 98 97 96 98 99 100 102]';
2
[b,bint,r,rint,stats]=regress(Y,X)
b,bint,stats
b =
bint =
-16.0730
-33.7071
0.7194
0.6047
stats =
0.9282 180.9531
0.0000
1.5612
0.8340
y=-16.073+0.7194x
4
z=b(1)+b(2)*x
plot(x,Y,'k+',x,z,'r')
2
rstoolxymodel, alpha
x nm y n alpha 0.05
model 4 1
linear y 0 1 x1 m x m
purequadratic
interaction y
y 0 1 x1 m x m jj x 2j
j 1
0 1 x1 m x m
quadratic y
1 j k m
0 1 x1 m x m
jk x j x k
1 j , k m
jk x j x k
1000 6
100
75
80
70
50
65
90
100
110
60
1000
5
600
7
1200
6
500
6
300
8
400
7
1300
5
1100
4
1300
3
300
9
y 0 1 x1 2 x 2 11 x12 22 x 22
x1=[1000 600 1200 500 300 400 1300 1100 1300 300];
x2=[5 7 6 6 8 7 5 4 3 9];
y=[100 75 80 70 50 65 90 100 110 60]';
x=[x1' x2'];
rstool(x,y,'purequadratic')
1000 6
Predicted Y 88.47981 1000 6
88.4791all, betarmse
residuals Matlab
Matlab beta, rmse
beta =
110.5313
0.1464
-26.5709
-0.0001
1.8475
rmse =
4.5362
y 110.5313 0.1464x1 26.5709x 2 0.0001x12 1.8475x 22
4.5362,
3
1
i
[betarJ]=nlinfitxymodel, beta0
beta r J Jacobian xy n m
n x n model m-
beta0
iinlintoolxymodel, beta0alpha
2
[YDELTA]=nlpredcimodel, xbetarJ
nlinfit nlintool x Y 1-alpha
Y DELTA.