MATLAB程序.docx
《MATLAB程序.docx》由会员分享,可在线阅读,更多相关《MATLAB程序.docx(28页珍藏版)》请在冰豆网上搜索。
MATLAB程序
一1
echoon
%金庸的3x3魔术矩阵:
magic(3),pause
%AlbertDurer的4x4魔术矩阵:
magic(4),pause
%试做9x9魔术矩阵:
B=magic(9)
%验证!
pause
sum(B),pause
sum(B'),pause
trace(B),pause
trace(rot90(B)),pause
2
%绘制二元函数的三维曲面图
[x,y]=meshgrid(-3:
1/8:
3);
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);,pause
figure
(1),surf(x,y,z),shadinginterp;colorbar,set(gcf,'position',[7305501380]),pause
%求解常微分方程
t_final=100;x0=[0;0;1e-10];
[t,x]=ode45('lorenzeq',[0,t_final],x0);
figure
(1);plot(t,x),set(gcf,'position',[7305501380])
%figure
(2);plot3(x(:
1),x(:
2),x(:
3));axis([1040-2020-2020])
figure
(2);line(x(:
1),x(:
2),x(:
3));axis([1040-2020-2020])
set(gcf,'position',[516305501380])
3
symsx;f='x^2*(sin(x))^2';
%求一阶导数
diff(f);
f1=simple(ans),pause
%求二阶导数
diff(f,x,2);f2=simple(ans),pause
%求三阶导数
diff(f,x,3);f3=simple(ans),pause
%求四阶导数
diff(f,x,4);f4=simple(ans),pause
%求四阶导数的积分
int(f4,x),pause
%美化书写
pretty(ans),pause
%求Taylor级数展开式
taylor(x^2*(sin(x))^2,15,x),pause
4
x=(0:
.1:
6)*pi;
y=abs(sin(x))-abs(cos(x));
plot(x,y)
axis([020-1.21.2])
5
load('data.txt');
x=data(:
1);y=data(:
2);
Dx=diff(x);Dy=diff(y);
h=50;r=sqrt((Dx).^2+(Dy).^2);
sn=Dy./r;cs=Dx./r;
x0=x(1:
end-1)+h*sn;
y0=y(1:
end-1)-h*cs;
h1=plot(x,y,x0,y0,'--');
axis([018002002000])
axissquare
set(gcf,'position',[740400300])
二1
echoon
%金庸的3x3魔术矩阵:
magic(3),pause
%AlbertDurer的4x4魔术矩阵:
magic(4),pause
%试做9x9魔术矩阵:
B=magic(9)
%验证!
pause
sum(B),pause
sum(B'),pause
trace(B),pause
trace(rot90(B)),pause
%行列式的值、矩阵的秩
d=det(B),e=rank(B),pause
%行列式的特征值:
f=eig(B)
三1
clear,formatshort,formatcompact
x=(-6:
0.1:
6)*pi;y=sin(x)./x;plot(x,y)
x=(-6:
0.1:
6)*pi+eps;y=sin(x)./x;plot(x,y)
fori=1:
10,x=i^2,end,i
A=[123;456;789]
B=[1,3,5;7,9,2;4,6,8]
v1=[1234]
v2=[1;2;3;4]
A,A=[A;[135]]
%A,A=[A;[12]]
A,A(2,3)
A,A([134],[13])
A,A(2,6)=1.5
A,A(2,4:
5)=Inf;A(3,4:
6)=NaN
a=0:
0.1:
1.16
a(:
[13])
a(:
)
reshape(ans,4,3)
ans(2:
end,3)
B=[1+9i,2+8i,3+7i;4+6i,5+5i,6+4i;7+3i,8+2i,1i]
B=[123;456;780];B=B+[987;654;321]*1i
2
A,A(2,4:
5)=Inf;A(3,4:
6)=NaN
a=0:
0.1:
1.16
a(:
[13])
a(:
)
reshape(ans,4,3)
ans(2:
end,3)
B=[1+9i,2+8i,3+7i;4+6i,5+5i,6+4i;7+3i,8+2i,1i]
B=[123;456;780];B=B+[987;654;321]*1i
2
A1=[123;456;789];A2=A1';A3=A1-A2;
A4(:
:
1)=A1;A4(:
:
2)=A2;A4(:
:
3)=A3
A4=cat(3,A1,A2,A3)
结果:
A4(:
:
1)=
123
456
789
A4(:
:
2)=
147
258
369
A4(:
:
3)=
0-2-4
20-2
420
A4(:
:
1)=
123
456
789
A4(:
:
2)=
147
258
369
A4(:
:
3)=
0-2-4
20-2
420
3
A=input('Pleaseenteryourname:
','s');
disp(['Yournameis',A])
while1==1
A=input('Pleaseenteryourname:
','s');
ifisempty(A)
warning('你什么也没输入,重来!
')
else
break
end
end
disp(['Yournameis',A])
4
METHOD='LinEaR';
switchlower(METHOD)
case{'linear','bilinear'}
disp('Methodislinear')
case'cubic'
disp('Methodiscubic')
case'nearest'
disp('Methodisnearest')
otherwise
disp('Unknownmethod.')
end
METHOD='LinEaR';
switchlower(METHOD)
case{'linear','bilinear'}
disp('Methodislinear')
case'cubic'
disp('Methodiscubic')
case'nearest'
disp('Methodisnearest')
otherwise
disp('Unknownmethod.')
end
5
x1=1;x2=pi;y=1;I=0;t=0;
whileabs(y)>0.00000000000001
I=I+1;x0=0.5*(x1+x2);
y1=logsin(x1);
y2=logsin(x2);
y=logsin(x0);
ify*y1>0,x1=x0;end
ify*y2>0,x2=x0;end
end,formatlong;x0,I
format
6
r=2.5;L=8;V=20;
%y=find_the_zero_point_of_function('volume',x);
y=fzero('volumea',[0;5],[],r,L,V);
disp(num2str(y))
结果:
>>Warning:
Cannotdeterminefromcallingsequencewhethertousenewor
grandfatheredFZEROfunction.Usingnew;ifcallwasgrandfathered
FZEROsyntax,thismaygiveunexpectedresults.
(Type"warningoffMATLAB:
fzero:
UndeterminedSyntax"tosuppressthiswarning.)
>InD:
\MATLAB6p5p1\toolbox\matlab\funfun\fzero.m(parse_call)atline408
InD:
\MATLAB6p5p1\toolbox\matlab\funfun\fzero.matline103
InC:
\DocumentsandSettings\frank\桌面\MATLAB\ch3\teach10a.matline3
0.92502
>>
7
globalrLV
r=2.5;L=8;V=20;
%y=find_the_zero_point_of_function('volume',x);
y=fzero('volumeb',[05]);
disp(num2str(y))
结果:
Warning:
Thevalueoflocalvariablesmayhavebeenchangedtomatchthe
globals.FutureversionsofMATLABwillrequirethatyoudeclare
avariabletobeglobalbeforeyouusethatvariable.
>InC:
\DocumentsandSettings\frank\桌面\MATLAB\ch3\teach10b.matline1
Warning:
Thevalueoflocalvariablesmayhavebeenchangedtomatchthe
globals.FutureversionsofMATLABwillrequirethatyoudeclare
avariabletobeglobalbeforeyouusethatvariable.
>InC:
\DocumentsandSettings\frank\桌面\MATLAB\ch3\teach10b.matline1
Warning:
Thevalueoflocalvariablesmayhavebeenchangedtomatchthe
globals.FutureversionsofMATLABwillrequirethatyoudeclare
avariabletobeglobalbeforeyouusethatvariable.
>InC:
\DocumentsandSettings\frank\桌面\MATLAB\ch3\teach10b.matline1
1.92502
8
clear
rec.form=99234;
rec.number='001';
rec.name='双建宇';
rec.title='Mr.';
rec.mark=[987586;1009396];
b(1,1)=struct(rec)
rec.form=99234;
rec.number='010';
rec.name='朱 琳';
rec.title='Miss';
rec.mark=[977688;9889100];
b(10,1)=struct(rec)
num=['001';'002';'003';'004';'005';'006';'007';'008'];
num=[num;'009';'010';'011';'012';'013';'014';'015';'016'];
num=[num;'017';'018';'019';'020';'021';'022';'024';'025'];
num=[num;'026';'027';'011';'028';'029';'030';'032';'033'];
num=[num;'034';'035';'036';'037';'038';'039';'040';'041'];
num=[num;'042';'043';'044';'045';'046';'047';'048';'049'];
num=[num;'051';'052';'053';'054';'055';'056';'057';'058'];
num=[num;'059';'060';'061';'062';'063';'064';'065';'066'];
num=[num;'067';'068';'069';'070';'071';'072';'073';'074'];
num=[num;'075';'076';'077';'078';'079';'080';'081';'082'];
%023ismissing.
fork=1:
80
b(k).form=99234;
b(k).number=num(k,:
);
end
b(27).form=99234;%Specialnumber,Oneoddout.
[f1,f2,f3,f4]=textread('list99.txt','%5c%3c,%6c,%2c');
fork=1:
80
b(k).form=f1(k,:
);
b(k).number=f2(k,:
);
b(k).name=f3(k,:
);
b(k).sex=f4(k,:
);
end
b(11)
b(65)
结果:
b=
form:
99234
number:
'001'
name:
'双建宇'
title:
'Mr.'
mark:
[2x3double]
b=
10x1structarraywithfields:
form
number
name
title
mark
ans=
form:
'99234'
number:
'011'
name:
'朱雁冬'
title:
[]
mark:
[]
sex:
'男'
ans=
form:
'99234'
number:
'067'
name:
'李冬松'
title:
[]
mark:
[]
sex:
'男'
9
clear,formatcompact
B(1,:
)={'99234','001','双建宇','Mr.',[987586;1009396]};
B(23,:
)={'99234','024','郭小宝','Mr.',[977688;9889100]};
B
[form,num,names,sx]=textread('list99.txt','%5c%3c,%6c,%2c');
fork=1:
80
B{k,1}=form(k,:
);
B{k,2}=num(k,:
);
B{k,3}=names(k,:
);
B{k,4}=sx(k,:
);
end
B
B{1,:
}
fork=1:
5,B{15,k}=[];end
B
B{23,5}=[100100100;100100100];
B
B(22:
24,5)
B{22:
24,5}
celldisp(B(22:
24,5))
10
disp('5行10000列求值,预先定维')
tic,H3=zeros(5,10000);
fori=1:
5
forj=1:
10000
H3(i,j)=1/(i+j-1);
end
end,toc
pause
clear
disp('5行10000列求值,预先定维,按照列向量计算')
tic,H4=zeros(5,10000);
fori=1:
5
H4(i,:
)=1./(i:
i+9999);
end,toc
四1
t=0:
.1:
2*pi;y1=sin(t);plot(t,y1)
y2=0.01*cos(t);
plot(t,y1,t,y2)
plotyy(t,y1,t,y2)
plotyy(t(10:
end),y1(10:
end),t,y2)
plotyy(t(10:
end),y1(10:
end),t,y2,'semilogx')
y2=y2*100;
y=[y1;y2];
plot(t,y)
y3=y1.*y2;
plot(t,y1,'-',t,y2,':
',t,y3,'x')
plot(t,y1,'--r',t,y2,'-.g',t,y3,'-b')
plot(t,y1,'--or',t,y2,'-.hexagramg',t,y3,'-xb')
gridon
title('Sinusoidalcurves')
xlabel('Time')
ylabel('Magnitude')
axis([-18-1.21.2]);
axis([pi-0.5pi+0.5-0.50.5]);
[u,v,b]=ginput(5);
line(u,v)
text(u,v,'Ah!
Apoint.')
gtext({'2008年!
','新北京,新奥运!
'},'FontName','楷体','Fontsize',16,'color','r')
结果:
2
clear
t=0:
.1:
2*pi;y1=sin(t);y2=cos(t);y3=y1.*y2;
subplot(211),plot(t,y1)
subplot(223),stem(t,y1)
subplot(224),polar(t,y1)
plot(t,y1,'--or',t,y2,'-.hexagramg',t,y3,'-xb')
xlabel('Time')
ylabel('Magnitude')
axis([-18-1.21.2]);
gtext({'别理我!
','烦着呢!
'},'FontName','楷体','Fontsize',16,'color','r')
subplot(4,4,11),fill(t,y1,'r')
subplot(211),plot(t,y1)
subplot(312),plot(t,y1)
subplot(1,1,1),feather(t,y1)
stairs(t,y1)
h=figure
(1);
h1=axes('pos',[0.20.20.60.4]);plot(t,y1)
h2=axes('pos',[0.10.10.80.1]);stem(t,y1)
h3=axes('pos',[0.50.50.40.4]);fill(t,y1,'g')
h4=axes('pos',[0.10.60.30.3]);plot(t,y1,'--',t,y2,':
',t,y3,'o')
set(h)
get(h,'position')
get(h,'children')
set(h1)
set(h4,'pos',[000.60.6])
set(h,'pos',[00560400])
set(h3,'box','off')
set(h3,'xgrid','on')
set(h3,'gridlinestyle','-')
set(h3,'xdir','reverse')
set(h3,'xdir','normal')
ht3=get(h3,'title')
set(ht3,'string','澳洲人的回旋镖')
set(ht3,'rotation',-10)
set(ht3,'fontsize',15)
set(ht3,'color',[24429249]/255)
clf;t=0:
0.4:
2*pi;y=sin(t);
hc=plot(t,y,'-pentagram');
axis([02*pi-2.22.2])
ht=gtext('研究生院');
yy=get(hc,'YData');
set(hc,'Markersize',20,'linestyle','-.','color',[6934120]/255,'YData',yy*2)
set(ht,'string','北京交通大学','fontsize',30,'rotation',10)
set(ht,'color','r','fontweight','bold','fontname','隶书')
set(ht,'string','\int^{\pi}_{0}{\xi^2+5\zeta-10}\approx\infty')
结果:
3
a=[5712684];
plot(a)
title('Amultiline')
xlabel('Number')
ylabel('电压','fontname','楷体')
text(3,8,'Hi!
Isthatyou?
')
结果:
4
x=-2:
0.1:
2;y=sin(x);
subplot(221),bar(x,y,'r');title('(a)bar')
subplot(222),compass(cos(x),y),title('(b)compass')
subplot(223),hist(y,20),title('(c)hist')
subplot(224),
[u,v]=meshgrid(-2:
.2:
2,-1:
.15:
1);
z=u.*exp(-u.^2-v.^2);[px,py]=gradient(z,.2,.15);
contour(u,v,z),holdon
quiver(u,v,px,py),holdoff