当前位置:文档之家› matlab程序题复习

matlab程序题复习

1.编写m 文件要求利用matlab 数值绘图功能画出衰减振荡曲线t ey t 3sin 3-=及其它的包络线30t e y -=。

t 的取值范围是]4,0[π,数值采样间隔点为pi/50,t ey t 3sin 3-=在图中用红色是线段绘出,3t e y -=用蓝色虚线绘出,且两函数作在同一张图上。

1.t=0:pi/50:4*pi; y0=exp(-t/3);y=exp(-t/3).*sin(3*t); figure;plot(t,y,'-r') hold on;plot(t,y0,':b'); plot(t,-y0,':b'); hold off;title(‘y=exp(-t/3)sin(3t)’); xlabel(‘x ’) ylabel(‘y ’)2.利用matlab 数值绘图功能,画出2222)sin(yx y x z ++=所表示的三维曲面。

y x ,的取值范围是]8,8[-,要求去除当x,y 均等于0时,函数z 的不连续点,数值坐标采样间隔为0.5.2.clear;x=-8:0.5:8;y=x';[X Y ]=meshgrid(x,y); R=sqrt(X.^2+Y.^2)+eps; Z=sin(R)./R; surf(X,Y,Z); (mesh(X,Y,Z))colormap(cool)xlabel('x'),ylabel('y'),zlabel('z')Title(‘z=sin(sqrt(x^2+y^2)/sqrt(x^2+y^2));3.利用matlab 数值绘图功能,绘制t 在区间[-2pi 2pi ]时函数y=sin(t)/t 的图形,采样间隔为pi/10,要求利用逻辑运算求出x=0时函数近似极限,并修补图形缺口。

3.t=-2*pi:pi/10:2*pi;y=sin(t)./t; tt=t+(t==0)*eps; yy=sin(tt)./tt;subplot(1,2,1),plot(t,y),axis([-7,7,-0.5,1.2]), xlabel('t'),ylabel('y');subplot(1,2,2),plot(tt,yy),axis([-7,7,-0.5,1.2])4.编制一个解数论问题的函数文件:取任意整数,若是偶数,则用2除,否则乘3加1,重复此过程,直到整数变为1。

4.function c=collatz(n)c=n;while n>1if rem(n,2)==0n=n/2;elsen=3*n+1;endc=[c n];end5.有一周期为4 的正弦波上叠加了方差为0.1的正态分布的随机噪声的信号,用循环结构编制一个三点线性滑动平均的程序。

(提示:①用0.1*randn(1,n)产生方差为0.1的正态分布的随机噪声;②三点线性滑动平均就是依次取每三个相邻数的平均值作为新的数据,如x1(2)=(x(1)+x(2)+x(3))/3,x1(3)=( x(2)+x(3)+x(4))/3……)。

5. t=0:pi/50:4*pi;n=length(t);y=sin(t)+0.1*randn(1,n);ya(1)=y(1);for i=2:n-1ya(i)=sum(y(i-1:i+1))/3;endya(n)=y(n);plot(t,y,'c',t,ya,'r','linewidth',2)6.有一组测量数据如下表所示,数据具有y=x2的变化趋势,用最小二乘法求解y。

并要求图示之。

6.>> x=[1 1.5 2 2.5 3 3.5 4 4.5 5]'>> y=[-1.4 2.7 3 5.9 8.4 12.2 16.6 18.8 26.2]'>> e=[ones(size(x)) x.^2]>> c=e\y>> x1=[1:0.1:5]';>> y1=[ones(size(x1)),x1.^2]*c;>> plot(x,y,'ro')hold on;plot(x1,y1,'k');xlabel(x);ylabel(y);title('拟合曲线');legend;7.22y xxe z --=,当x 和y 的取值范围均为-2到2时,用建立子窗口的方法在同一个图形窗口中绘制出三维线图、网线图、表面图和带渲染效果的表面图。

7>> [x,y]=meshgrid([-2:.2:2]);>> z=x.*exp(-x.^2-y.^2); >> mesh(x,y,z)>> subplot(2,2,1), plot3(x,y,z) >> title('plot3 (x,y,z)') >> subplot(2,2,2), mesh(x,y,z) >> title('mesh (x,y,z)') >> subplot(2,2,3), surf(x,y,z) >> title('surf (x,y,z)')>> subplot(2,2,4), surf(x,y,z), shading interp >> title('surf (x,y,z), shading interp')8.利用matlab 绘图功能,分别以条形图、阶梯图、杆图和填充图形式绘制曲线y=2sin(x),要求作在同一张图上,并标注标题。

8.解:x=0:pi/10:2*pi; y=2*sin(x); subplot(2,2,1); bar(x,y,'g');title('bar(x,y,''g'')'); axis([0 7 –2 2]); subplot(2,2,2); stairs(x,y,'b');title('stairs(x,y,''b'')');axis([0 7 –2 2]); subplot(2,2,3); stem(x,y,'k');title('stem(x,y,''k'')');axis([0 7 –2 2]); subplot(2,2,4); fill(x,y,'y');title('fill(x,y,''y'')');axis([0 7 –2 2]);必考 9.使用 MATLAB 画一个圆心在原点、半径等于 10 的圆,并在圆周上依逆时钟方向取 任意四点 A 、B 、C 、D ,将线段 AB 、AC 、AD 、BC 、BD 、CD 用直线画出。

计算线段 AB 、AC 、AD 、BC 、BD 、CD 的长度。

clear all;t=linspace(0, 2*pi,50); r=10; x=r*cos(t); y=r*sin(t);a=r*[cos(0.50*pi), sin(0.50*pi)];b=r*[cos(0.90*pi), sin(0.90*pi)];c=r*[cos(1.25*pi), sin(1.25*pi)];d=r*[cos(1.80*pi), sin(1.80*pi)];plot(x, y, 'b', a(1), a(2), '.k', b(1), b(2), '.k', c(1), c(2), '.k', d(1), d(2), '.k'); axis imagetext(a(1), a(2), ' A');text(b(1), b(2), ' B');text(c(1), c(2), ' C');text(d(1), d(2), ' D');line([a(1), b(1)], [a(2), b(2)], 'color', 'r'); t=(a+b)/2; text(t(1), t(2), 'AB');line([b(1), c(1)], [b(2), c(2)], 'color', 'r'); t=(b+c)/2; text(t(1), t(2), 'BC');line([c(1), d(1)], [c(2), d(2)], 'color', 'r'); t=(c+d)/2; text(t(1), t(2), 'CD');line([d(1), a(1)], [d(2), a(2)], 'color', 'r'); t=(d+a)/2; text(t(1), t(2), 'DA');line([a(1), c(1)], [a(2), c(2)], 'color', 'r'); t=(a+c)/2; text(t(1), t(2), 'AC');line([b(1), d(1)], [b(2), d(2)], 'color', 'r'); t=(b+d)/2; text(t(1), t(2), 'BD'); ab=sqrt((a(1)-b(1)).^2+(a(2)-b(2)).^2);fprintf('ab = %f\n', ab);bc=sqrt((b(1)-c(1)).^2+(b(2)-c(2)).^2);fprintf('bc = %f\n', bc);cd=sqrt((c(1)-d(1)).^2+(c(2)-d(2)).^2);fprintf('cd = %f\n', cd);ad=sqrt((a(1)-d(1)).^2+(a(2)-d(2)).^2);fprintf('ad = %f\n', ad);ac=sqrt((a(1)-c(1)).^2+(a(2)-c(2)).^2);fprintf('ac = %f\n', ac);bd=sqrt((b(1)-d(1)).^2+(b(2)-d(2)).^2);fprintf('bd = %f\n', bd);必考 10.试写一函数 regPolygon(n),其功能为画出一个圆心在 (0, 0)、半径为 1 的圆,并在圆内画出一个内接正 n 边形,其中一顶点位于 (0, 1)。

相关主题