刘卫国 Matlab 例题 4-5章

标签:
教育 |
例4.1
首先建立命令文件并以文件名exch.m存盘:
clear;
a=1:10;
b=[11,12,13,14;15,16,17,18];
c=a;a=b;b=c;
a
b
然后在MATLAB的命令窗口中输入exch,将会执行该命令文件。
function [a,b]=exch(a,b)
c=a;a=b;b=c;
然后在MATLAB的命令窗口调用该函数文件:
clear;
x=1:10;
y=[11,12,13,14;15,16,17,18];
[x,y]=fexch(x,y)
a=input('a=?');
b=input('b=?');
c=input('c=?');
d=b*b-4*a*c;
x=[(-b+sqrt(d))/(2*a),(-b-sqrt(d))/(2*a)];
disp(['x1=',num2str(x(1)),',x2=',num2str(x(2))]);
例4.3
file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image002.gifMatlab
x=input('请输入x的值:');
if x==10
else
end
y
也可以用单分支if语句来实现:
x=input('请输入x的值:');
y=cos(x+1)+sqrt(x*x+1);
if x~=10
end
y
或用以下程序:
x=input('请输入x的值:');
if x==10
end
if x~=10
end
y
c=input('请输入一个字符','s');
if c>='A' & c<='Z'
elseif c>='a'& c<='z'
elseif c>='0'& c<='9'
else
end
例4.5
5000≤price
输入所售商品的价格,求其实际销售价格。
price=input('请输入商品价格');
switch fix(price/100)
end
price=price*(1-rate)
例4.6
A=[1,2,3;4,5,6];
B=[7,8,9;10,11,12];
try
catch
end
C
lasterr
例4.7
for m=100:999
m1=fix(m/100);
m2=rem(fix(m/10),10);
m3=rem(m,10);
if m==m1*m1*m1+m2*m2*m2+m3*m3*m3
disp(m)
end
end
y=0;n=100;
for i=1:n
y=y+1/i/i;
end
y
n=100;
i=1:n;
f=1./i.^2;
y=sum(f)
例4.9
a=0;b=3*pi;
n=1000; h=(b-a)/n;
x=a; s=0;
f0=exp(-0.5*x)*sin(x+pi/6);
for i=1:n
end
s
上述程序来源于传统的编程思想。也可以利用向量运算,从而使得程序更加简洁,更赋有MATLAB的特点。程序如下:
a=0;b=3*pi;
n=1000; h=(b-a)/n;
x=a:h:b;
f=exp(-0.5*x).*sin(x+pi/6);
for i=1:n
end
s=sum(s)
s=0;
a=[12,13,14;15,16,17;18,19,20;21,22,23];
for k=a
end
disp(s');
sum=0;
n=0;
x=input('Enter a number (end in 0):');
while (x~=0)
end
if (n>0)
end
例4.12
file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image010.gifMatlab
X=input('Enter X:');
E=zeros(size(X));
F=eye(size(X));
n=1;
while norm(F,1)>0
end
E
expm(X)
for n=100:200
if rem(n,21)~=0
end
break
end
n
例4.14
例4.15
p=1:m; p(1)=0;
for i=2:sqrt(m)
end
n=find(p~=0);
p(n)
m=input('m=');
p=2:m;
for i=2:sqrt(m)
end
p
function [s,p]=fcircle(r)
%CIRCLE
%r
%s
%p
06年2月30日编
s=pi*r*r;
p=2*pi*r;
[s,p]=fcircle(10)
function [rho,theta]=tran(x,y)
rho=sqrt(x*x+y*y);
theta=atan(y/x);
x=input('Please input x=:');
y=input('Please input y=:');
[rho,the]=tran(x,y);
rho
the
function f=factor(n)
if n<=1
else
end
s=0;
for i=1:5
end
s
例4.19
function Y=rndprm1(X)
%RNDPRM1
%RNDPRM1(X)产生行向量X的任意排列
[m,n]=size(X);
if m>1
end
Y=[];
l=n;
for i=1:n
k=1+fix(l*rand);
x=X(k);
end
第二个程序用函数的递归调用:
function Y=rndprm2(X)
%RNDPRM2
%RNDPRM2(X)产生一个X的任意排列
[m,n]=size(X);
l=n;
if m>1
end
if n<=1
else
k=1+fix(l*rand);
x=X(k);
X(k)=[];
end
function fout=charray(a,b,c)
if nargin==1
elseif nargin==2
elseif nargin==3
end
x=[1:3];
y=[1;2;3];
examp(x)
examp(x,y')
examp(x,y,3)
function f=wadd(x,y)
global ALPHA BETA
f=ALPHA*x+BETA*y;
global ALPHA BETA
ALPHA=1;
BETA=2;
s=wadd(1,2)
例5.1
x=0:pi/100:2*pi;
y=2*exp(-0.5*x).*sin(2*pi*x);
plot(x,y)
例5.2
file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image002.gifMatlab
t=-pi:pi/100:pi;
x=t.*cos(3*t);
y=t.*sin(t).*sin(t);
plot(x,y);
例5.3
x=(0:pi/100:2*pi)';
y1=2*exp(-0.5*x)*[1,-1];
y2=2*exp(-0.5*x).*sin(2*pi*x);
x1=(0:12)/2;
y3=2*exp(-0.5*x1).*sin(2*pi*x1);
plot(x,y1,'k:',x,y2,'b--',x1,y3,'rp');
例5.4
x1=0:pi/100:2*pi;
x2=0:pi/100:3*pi;
y1=exp(-0.5*x1).*sin(2*pi*x1);
y2=1.5*exp(-0.1*x2).*sin(x2);
plotyy(x1,y1,x2,y2);
例5.5
file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image004.gifMatlab
x=linspace(0,10,100);
y=[];
for x0=x
end
plot(x,y)
axis([0 10 0 2.5])
title('分段函数曲线');
xlabel('Variable X');
ylabel('Variable Y');
text(2,1.3,'y=x^{1/2}');
text(4.5,1.9,'y=2');
text(7.3,1.5,'y=5-x/2');
text(8.5,0.9,'y=1');
例5.6
x=(0:pi/100:2*pi)';
y1=2*exp(-0.5*x)*[1,-1];
y2=2*exp(-0.5*x).*sin(2*pi*x);
plot(x,y1,'b:');
axis([0,2*pi,-2,2]);
hold on;
plot(x,y2,'k');
legend('包络线','包络线','曲线y');
hold off;
grid
例5.7
x=linspace(0,2*pi,60);
y=sin(x);
z=cos(x);
t=sin(x)./(cos(x)+eps);
ct=cos(x)./(sin(x)+eps);
subplot(2,2,1);
plot(x,y);
title('sin(x)');axis([0,2*pi,-1,1]);
subplot(2,2,2);
plot(x,z);
title('cos(x)');axis([0,2*pi,-1,1]);
subplot(2,2,3);
plot(x,t);
title('tangent(x)');axis([0,2*pi,-40,40]);
subplot(2,2,4);
plot(x,ct);
title('cotangent(x)');axis([0,2*pi,-40,40]);
请看下面的程序。
x=linspace(0,2*pi,60);
y=sin(x);
z=cos(x);
t=sin(x)./(cos(x)+eps);
ct=cos(x)./(sin(x)+eps);
subplot(2,2,1);
stairs(x,y);title('sin(x)-1');axis ([0,2*pi,-1,1]);
subplot(2,1,2);
stem(x,y);title('sin(x)-2');axis ([0,2*pi,-1,1]);
subplot(4,4,3);
plot(x,y);title('sin(x)');axis ([0,2*pi,-1,1]);
subplot(4,4,4);
plot(x,z);title('cos(x)');axis ([0,2*pi,-1,1]);
subplot(4,4,7);
plot(x,t);title('tangent(x)');axis ([0,2*pi,-40,40]);
subplot(4,4,8);
plot(x,ct);title('cotangent(x)');axis ([0,2*pi,-40,40]);
例5.8
x=0:0.35:7;
y=2*exp(-0.5*x);
subplot(2,2,1);bar(x,y,'g');
title('bar(x,y,''g'')');axis([0,7,0,2]);
subplot(2,2,2);fill(x,y,'r');
title('fill(x,y,''r'')');axis([0,7,0,2]);
subplot(2,2,3);stairs(x,y,'b');
title('stairs(x,y,''b'')');axis([0,7,0,2]);
subplot(2,2,4);stem(x,y,'k');
title('stem(x,y,''k'')');axis([0,7,0,2]);
例5.9
theta=0:0.01:2*pi;
rho=sin(2*theta).*cos(2*theta);
polar(theta,rho,'k');
例5.10
x=0:0.1:10;
y=10*x.*x;
subplot(2,2,1);
plot(x,y);
title('plot(x,y)');grid on;
subplot(2,2,2);
semilogx(x,y);
title('semilogx(x,y)');grid on;
subplot(2,2,3);
semilogy(x,y);
title('semilogy(x,y)');grid on;
subplot(2,2,4);
loglog(x,y);
title('loglog(x,y)');grid on;
例5.11
先建立函数文件myf.m:
function y=myf(x)
y=cos(tan(pi*x));
再用fplot函数绘制myf.m函数的曲线:
fplot('myf',[-0.4,1.4],1e-4)
得到如图5.12所示曲线。从图5.12中可看出,在x=0.5附近采样点十分密集。
也可以直接用fplot函数绘制f(x)=cos(tan(πx))的曲线:
fplot('cos(tan(pi*x))',[ -0.4,1.4],1e-4)
例5.12
(1)某次考试优秀、良好、中等、及格、不及格的人数分别为:7,17,23,19,5,试用饼图作成绩统计分析。
(2)绘制复数的相量图:3+2i、5.5-i和-1.5+5i。
subplot(1,2,1);
pie([7,17,23,19,5]);
title('饼图');legend('优秀','良好','中等','及格','不及格');
subplot(1,2,2);
compass([3+2i,5.5-i,-1.5+5i]);title('相量图');
例5.13
file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image006.gifMatlab
t=0:pi/50:2*pi;
x=8*cos(t);
y=4*sqrt(2)*sin(t);
z=-4*sqrt(2)*sin(t);
plot3(x,y,z,'p');
title('Line in 3-D Space');
text(0,0,0,'origin');
xlabel('X'),ylabel('Y'),zlabel('Z');grid;
例5.14
x=7:29;
y=16:35;
[x,y]=meshgrid(x,y);
z=2*x+5*y;
k=find(z==126);
x(k)',y(k)'
例5.15
程序1:
x=0:0.1:2*pi;
[x,y]=meshgrid(x);
z=sin(y).*cos(x);
mesh(x,y,z);
xlabel('x-axis'),ylabel('y-axis'),zlabel('z-axis');
title('mesh');
程序2:
x=0:0.1:2*pi;
[x,y]=meshgrid(x);
z=sin(y).*cos(x);
surf(x,y,z);
xlabel('x-axis'),ylabel('y-axis'),zlabel('z-axis');
title('surf');
程序3:
x=0:0.1:2*pi;
[x,y]=meshgrid(x);
z=sin(y).*cos(x);
plot3(x,y,z);
xlabel('x-axis'),ylabel('y-axis'),zlabel('z-axis');
title('plot3-1');grid;
例5.16
%两个等直径圆管的交线
m=30;
z=1.2*(0:m)/m;
r=ones(size(z));
theta=(0:m)/m*2*pi;
x1=r'*cos(theta);y1=r'*sin(theta);
z1=z'*ones(1,m+1);
x=(-m:2:m)/m;
x2=x'*ones(1,m+1);y2=r'*cos(theta);
z2=r'*sin(theta);
surf(x1,y1,z1);
axis equal,axis off
hold on
surf(x2,y2,z2);
axis equal,axis off
title('两个等直径圆管的交线');
hold off
例5.17
[x,y]=meshgrid(-10:0.2:10);
z1=(x.^2-2*y.^2)+eps;
a=input('a=?');
z2=a*ones(size(x));
subplot(1,2,1);
mesh(x,y,z1);hold
on;mesh(x,y,z2);
v=[-10,10,-10,10,-100,100];axis(v);grid;
hold off;
r0=abs(z1-z2)<=1;
xx=r0.*x; yy=r0.*y; zz=r0.*z2;
subplot(1,2,2);
plot3(xx(r0~=0),yy(r0~=0),zz(r0~=0),'*');
axis(v);grid;
例5.18
file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtmlclip1/01/clip_image008.gifMatlab
的4种三维曲面图。
z=sin(sqrt(x.^2+y.^2))./sqrt(x.^2+y.^2+eps);
subplot(2,2,1);
meshc(x,y,z);
title('meshc(x,y,z)')
subplot(2,2,2);
meshz(x,y,z);
title('meshz(x,y,z)')
subplot(2,2,3);
surfc(x,y,z)
title('surfc(x,y,z)')
subplot(2,2,4);
surfl(x,y,z)
title('surfl(x,y,z)')
例5.19
t=0:pi/20:2*pi;
[x,y,z]= cylinder(2+sin(t),30);
subplot(1,3,1);
surf(x,y,z);
subplot(1,3,2);
[x,y,z]=sphere;
surf(x,y,z);
subplot(1,3,3);
[x,y,z]=peaks(30);
meshz(x,y,z);
例5.20
(1)绘制魔方阵的三维条形图。
(2)以三维杆图形式绘制曲线y=2sin(x)。
(3)已知x=[2347,1827,2043,3025],绘制三维饼图。
(4)用随机的顶点坐标值画出五个黄色三角形。
subplot(2,2,1);
bar3(magic(4))
subplot(2,2,2);
y=2*sin(0:pi/10:2*pi);
stem3(y);
subplot(2,2,3);
pie3([2347,1827,2043,3025]);
subplot(2,2,4);
fill3(rand(3,5),rand(3,5),rand(3,5), 'y' )
例5.21
subplot(1,2,1);
[X,Y,Z]=peaks(30);
waterfall(X,Y,Z)
xlabel('X-axis'),ylabel('Y-axis'),zlabel('Z-axis');
subplot(1,2,2);
contour3(X,Y,Z,12,'k');
xlabel('X-axis'),ylabel('Y-axis'),zlabel('Z-axis');
例5.22
subplot(2,2,1);mesh(peaks);
view(-37.5,30);
title('azimuth=-37.5,elevation=30')
subplot(2,2,2);mesh(peaks);
view(0,90);
title('azimuth=0,elevation=90')
subplot(2,2,3);mesh(peaks);
view(90,0);
title('azimuth=90,elevation=0')
subplot(2,2,4);mesh(peaks);
view(-7,-10);
title('azimuth=-7,elevation=-10')
例5.23
z=peaks(20);colormap(copper);
subplot(1,3,1);surf(z);
subplot(1,3,2); surf(z);shading flat;
subplot(1,3,3);surf(z);shading interp;
例5.24
[x,y,z]=sphere(20);
%生成外面的大球
z1=z;
z1(:,1:4)=NaN;
c1=ones(size(z1));
surf(3*x,3*y,3*z1,c1);
%生成里面的小球
hold on
z2=z;
c2=2*ones(size(z2));
c2(:,1:4)=3*ones(size(c2(:,1:4)));
surf(1.5*x,1.5*y,1.5*z2,c2);
colormap([0,1,0;0.5,0,0;1,0,0]);
grid on
hold off
例5.25
subplot(2,2,1);
ezplot('x^2+y^2-9');axis equal
subplot(2,2,2);
ezplot('x^3+y^3-5*x*y+1/5')
subplot(2,2,3);
ezplot('cos(tan(pi*x))',[ 0,1])
subplot(2,2,4);
ezplot('8*cos(t)','4*sqrt(2)*sin(t)',[0,2*pi])
例5.26
x=0:pi/50:2*pi;
y=sin(x);
z=cos(x);
plot(x,y,'r',x,z,'g');
H=get(gca,'Children');
for k=1:length(H)
end
pause
set(Hg,'LineStyle',':','Marker','p');
hf=figure('Color',[0,1,0],'Position',[1,1,300,150],...
例5.28
x=linspace(0,2*pi,60);
y=sin(x);
z=cos(x);
t=tan(x);
ct=1./(t+eps);;
%命令组待用
C4=['figure(''Name'',''cotangent(x)'',''NumberTitle'',',...
C3=['figure(''Name'',''tangent(x)'',''DeleteFcn'',C4,',...
C2=['figure(''Name'',''cos(x)'',''DeleteFcn'',C3,',...
%先创建1个图形窗口并绘制曲线
figure('Name','sin(x)','DeleteFcn',C2,'NumberTitle','off');
plot(x,y);
axis([0,2*pi,-1,1]);
例5.29
clf;
x=linspace(0,2*pi,20);
y=sin(x);
axes('Position',[0.2,0.2,0.2,0.7] ,'GridLineStyle','-.');
plot(y,x);title('sin(x)-1');
axes('Position',[0.4,0.5,0.2,0.1]);
stairs(x,y);title('sin(x)-2');
axes('Position',[0.55,0.6,0.25,0.3]);
stem(x,y);title('sin(x)-3');
axes('Position',[0.55,0.2,0.25,0.3]);
[x,y]=meshgrid(-8:0.5:8);
z=sin(sqrt(x.^2+y.^2))./sqrt(x.^2+y.^2+eps);
mesh(x,y,z); title('mesh(x,y,z)');
例5.30
t=0:pi/100:pi/2;
y1=sin(2*pi*t);
y2=sqrt(3)/2*exp(-4*t).*sin(4*sqrt(3)*t+pi/3);
figh=figure;
axes('GridLineStyle',':','XLim',[0,pi/2],'YLim',[-1,1]);
line('XData',t,'YData',y1,'LineWidth',1);
line(t,y2);
grid on
例5.31
theta=-pi:.1:pi;
y1=sin(theta);
y2=cos(theta);
h=line(theta,y1,'LineStyle',':','Color','g');
line(theta,y2,'LineStyle','--','Color','b');
xlabel('-\pi \leq \theta \leq \pi')
ylabel('sin(\theta)')
title('Plot of sin(\theta)')
text(-pi/4,sin(-pi/4),'\leftarrow sin(-\pi\div4)','FontSize',12)
set(h,'Color','r','LineWidth',2)
例5.32
x=0:0.1:2*pi;
[x,y]=meshgrid(x);
z=sin(y).*cos(x);
axes('view',[-37.5,30]);
hs=surface(x,y,z,'FaceColor','w','EdgeColor','flat');
grid on;
xlabel('x-axis'),ylabel('y-axis'),zlabel('z-axis');
title('mesh-surf');
pause;
set(hs,'FaceColor','flat');
例5.33
作图时,常常需要改变曲线的属性,例如线型、线宽、颜色等。如果对多根曲线做同样的操作可能是很烦琐的,下面的函数可以简化操作。
%PLINE
%
%
%
%
%
%
function pline(P,w,c,s)
[m,n]=size(P);
if m==2
elseif m==3
else
end
下面是绘制两个等直径圆柱体管交线的程序。
r1=5;
r2=5;
%前视图
% 开始做水平圆柱
P0=[0;0]; P1=[-5;5];
P2=[-13;5]; P3=[-13;-5];
P4=[13;-5]; P5=[13;5];
P6=[5;5];
patch([P0(1),P6(1),P5(1),P4(1),P3(1),P2(1),P1(1)],...
[P0(2),P6(2),P5(2),P4(2),P3(2),P2(2),P1(2)],'y')
axis([-15 30 -27 18]),axis equal,axis off
hold on
%开始做垂直圆柱
P7=[5;15];P8=[-5;15];
patch([P0(1),P6(1),P5(1),P4(1),P3(1),P2(1),P1(1)],...
[P0(2),P6(2),P5(2),P4(2),P3(2),P2(2),P1(2)],'g')
pline([P0 P1 P2 P3 P4 P5 P6
P0],2.5,'k','-')
pline([P6 P7 P8
P1],2.5,'k','-')
%侧视图
t=0:pi/90:2*pi;
xc=23+r1*cos(t); zc=r1*sin(t);
patch(xc,zc,'y')
% 开始做垂直圆柱
z1=0;
P11=[28;z1];P12=[28;15];
P13=[18;15];P14=[18;z1];
%定义交线
ti=0:pi/90:pi;
xi2=23+r1*cos(ti);zi2=r1*sin(ti);
%垂直圆柱
patch([xi2,P11(1),P12(1),P13(1),P14(1)],...
[zi2,P11(2),P12(2),P13(2),P14(2)],'g')
pline([xc;zc],2.5,'k','-')
pline([P11 P12 P13 P14],2,'k','-')
ha=plot([-14 29],[0 0],'k-.');
set(ha,'LineWidth',1.5)
ha=plot([0 0],[-6 16],'k-.');
% 侧视图的垂直轴
set(ha,'LineWidth',1.5)
ha=plot([23 23],[-6 16],'k-.');
% 侧视图的垂直轴
set(ha,'LineWidth',1.5)
ht=title('Intersection of perpendicular,equal-diameter cylinders');
set(ht,'FontSize',14)
t1='horizontal cylinder:
ht=text(-12,-10,t1);
set(ht,'FontSize',14)
t2='vertical cylinder:
ht=text(-12,-15,t2);
set(ht,'FontSize',14)
ht=text(-12,-20,'projection of intersection:z^2-x^2=0');
set(ht,'FontSize',14)
hold off
例5.34
subplot(2,2,1);
rectangle('Position',[2,3,25,15],'LineWidth',3);
subplot(2,2,2);
rectangle('Position',[3,5,15,8], 'Curvature',0.4,'LineWidth',2);
subplot(2,2,3);
rectangle('Position',[5,3,10,15],'Curvature',[1,1]);
subplot(2,2,4);
rectangle('Position',[5,3,10,10],'Curvature',[1,1],...
axis equal
例5.35
subplot(1,4,1);
surf(x,y,z);axis equal;
shading interp;
hold on;
subplot(1,4,2);
surf(x,y,z);axis equal;
light('Position',[0,1,1]);
shading interp;lighting flat;
hold on;
plot3(0,1,1,'p');text(0,1,1,' light');
subplot(1,4,3);
surf(x,y,z);axis equal;
light('Position',[0,1,1]);
shading interp;lighting gouraud;
hold on;
subplot(1,4,4);
surf(x,y,z);axis equal;
light('Position',[0,1,1]);
shading interp;lighting phong;
例5.36
sphere(36);
h=findobj('Type','surface');
set(h,'FaceLighting','phong','FaceColor','interp',...
'EdgeColor',[0.4,0.4,0.4],'BackFaceLighting','lit')
hold on
vert=[2,0,-1;2,1,-1;3,0,0;3,0,-1;2,0,0;2,1,0;3,1,0;3,0,0];
fac=[1,2,3,4;2,6,7,3;4,3,7,8;1,5,8,4;1,2,6,5;5,6,7,8];
patch('Faces',fac,'Vertices',vert,'FaceColor','y');
light('Position',[1,3,2]);
light('Position',[-3,-1,3]);
material shiny
axis equal
hold off
例5.37
[x,cmap]=imread('e:\flower.jpg');
image(x);
colormap(cmap);
axis image off
例5.38
[x,y,z]=sphere(50);
m=moviein(30);
for i=1:30
end
movie(m,10);
例5.39
n=30;
s=0.02;
%产生n个随机点(x,y),处于-0.5到0.5之间
x=rand(n,1)-0.5;
y=rand(n,1)-0.5;
h=plot(x,y,'.');
axis([-1 1 -1 1]) ;
axis square
grid off
set(h,'EraseMode','Xor','MarkerSize',20);
%循环5000次,产生动画效果
for i=linspace(1,10,5000)
end