收藏 分销(赏)

MATLAB课后习题.doc

上传人:天**** 文档编号:4361304 上传时间:2024-09-13 格式:DOC 页数:9 大小:53.50KB 下载积分:6 金币
下载 相关 举报
MATLAB课后习题.doc_第1页
第1页 / 共9页
MATLAB课后习题.doc_第2页
第2页 / 共9页


点击查看更多>>
资源描述
5、利用rand函数产生(0,1)间得均匀分布得10*10随机矩阵A,然后统计A中大于等于0、6得元素得个数。 解: A=rand(10); B=A >= 0、6; C=sum(B); count=sum(C) 运行结果(每次运行结果就是不同得,仅作参考): count=32 6、利用randn函数产生均值为0,方差为1得10*10随机矩阵A,然后统计A中大于-0、5且小于0、5得元素得个数。 解: A=randn(10); B=(A<0、5)&(A>-0、5); C=sum(sum(B)) 运行结果(每次运行结果就是不同得,仅作参考):C=48 1、 解: if and(a<1,b<=0、5) 语句1; elseif and(a<1,b>0、5) 语句2; elseif and(a>=1,b<=0、5) 语句3; else 语句4; 2、 有一矩阵A,找出矩阵中值等于1得元素,并将它们重新排列成列向量B。 解: A=2*rand(4); k=find(A<=1); A(k)=[];%删除下标为k得元素 B=A' 运行结果(每次运行结果就是不同得,仅作参考) B = 1、4769 1、8348 1、5310 1、1524 1、3667 1、0932 1、2889 1、2952 1、3580 3、 在一测量矩阵A(100*3)中,存在有奇异值(假设大于100得置认为就是奇异值),编程实 现删去奇异值所在得行。 解: A=120*randn(10,3); [i,j]=find(A>100); A(i,:)=[] %删去存在奇异值得行 4、 在给定得100*100矩阵中,删去整行为0得行,删去整列为0得列。 解: A=diag([1 2 3 4],1) B=any(A) [i,j]=find(B==0) A(:,i)=[] %删除全为0得列 B=any(A') [i,j]=find(B==0) A(j,:)=[] %删除全为0得行 运行结果: 初始值:A = 0 1 0 0 0 0 0 2 0 0 0 0 0 3 0 0 0 0 0 4 0 0 0 0 0 操作后:A = 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4 1、将窗口分割成四格,分别绘制正弦、余弦、正切与余切函数曲线,并加上适当得标注。 程序为: x=0:pi/50:2*pi; k=[1 26 51 76 101]; x(k)=[];%删除正切与余切得奇异点 figure(1) subplot(2,2,1) plot(x,sin(x),'k--'),grid on legend('\ity=sin(x)') title('y=sin(x)') xlabel('x'), ylabel('y') subplot(2,2,2) plot(x,cos(x),'r--'),grid on legend('\ity=cos(x)') title('y=con(x)') xlabel('x'), ylabel('y') subplot(2,2,3) plot(x,tan(x),'k'),grid on legend('\ity=tan(x)') title('y=tan(x)') xlabel('x'), ylabel('y') subplot(2,2,4) plot(x,cot(x),'b-'),grid on legend('\ity=cot(x)') title('y=cot(x)') xlabel('x'), ylabel('y') 运行如下: 2、绘制多峰函数peaks与三角函数多条曲线。 多峰函数peaks: [x,y]=meshgrid(-3:0、15:3); z=peaks(x,y); x1=x(1,:); figure(1) plot(x1,z),grid on title('二维多峰函数') 图形为: [x,y]=meshgrid(-3:0、15:3); z=peaks(x,y); figure(1) plot3(x,y,z),grid on title('三维多峰函数') 三角函数多条曲线: 程序为: t=-pi:pi/20:pi; y1=sinh(t); %双曲正弦 y2=cosh(t); %双曲余弦 figure(1) subplot(2,1,1) plot(t,y1,'r--',t,y2,'k-'),grid on legend('\ity1=sinh(t)','\ity2=cosh(t)') title('三角函数1') xlabel('t'), ylabel('y') subplot(2,1,2) plot(t,sin(t),'k-'),grid on hold on %保持原有图像函数 plot(t,cos(t),'r--') legend('\ity2=cos(t)','\ity1=sin(t)') title('三角函数2') xlabel('t'), ylabel('y') 运行图形为: 3、将图形窗口分成两个,分别绘制以下函数在[-3,3]区间上得曲线,并利用axis调整轴刻度,使她们具有相同缩放尺度。y1=2x+5; y2=x2-3x+1。 程序为: x=-3:0、1:3; y1=2*x+5; y2=x、^2-3*x+1; figure(1) subplot(2,2,1) plot(x,y1,'r-'),grid on legend('\ity1=2*x+5') title('y1=2x+5') xlabel('x'), ylabel('y1') subplot(2,2,2) plot(x,y2,'k-'),grid on legend('\ity2=x、^2-3*x+1') title('y2=x^2-3x+1') xlabel('x'), ylabel('y2') subplot(2,2,3) plot(x,y1,'r-'),grid on legend('\ity1=2*x+5') title('调整后得y1=2x+5') axis([-3 3 -10 10]) xlabel('x'), ylabel('y1') subplot(2,2,4) plot(x,y2,'k-'),grid on legend('\ity2=x、^2-3*x+1') title('调整后得y2=x^2-3x+1') axis([-3 3 -10 10]) %调整坐标轴 xlabel('x'), ylabel('y2') 运行后得图形: 4、绘制饼图。 程序为: x=[190 33 45 42 45]; explode=[0 1 0 0 0]; figure(1) subplot(2,1,1) colormap hsv pie(x,explode) gtext('生活费') gtext('资料费') gtext('电话费') gtext('衣服') gtext('其它') title('二维饼图') subplot(2,1,2) colormap hsv pie3(x,explode) title('三维饼图') 图形为: 5、画出函数z=(x-2)2+(y-1、2)2+sin(xy)得三维曲线与网格曲线。 程序为: [x,y]=meshgrid(0:0、5:10); %为三维绘图产生x,y数据矩阵 z=(x-2)、^2+(y-1、2)、^2; figure(1) subplot(2,1,1) mesh(x,y,z),grid on %绘制网格曲线 title('网格曲线') subplot(2,1,2) plot3(x,y,z),grid on title('三维曲线') 6、画出下列函数得曲面及等高线图z=x2+y2+sin(xy)。 程序为: [x,y]=meshgrid(0:pi/10:2*pi); z=x、^2+y、^2+sin(x*y); figure(1) subplot(2,1,1) surfc(x,y,z), grid on title('曲面与等高线') subplot(2,1,2) [c,h]=contour(x,y,z); set(h,'showtext','on','textstep',get(h,'levelstep')*2); title('等高线') 1、将图形窗口分成两个,分别绘制正割与余割曲线,并加上标注。 程序为: x1=0:pi\10:2*pi; figure(1) subplot(2,1,1) plot(x,sec(x),'k-'),grid on legend('\ity=sec(x)') title('y=sec(x)') xlabel('x'), ylabel('y') subplot(2,1,2) plot(x,csc(x),'k-'),grid on legend('\ity=csc(x)') title('y=csc(x)') xlabel('x'), ylabel('y') 运行后图形为: 2、画出对数与指数曲线并加上标注。 x=0、01:0、1:10; y1=log10(x); y2=exp(x); figure(1) subplot(2,1,1) plot(x,y1,'k-'),grid on legend('\ity1=log-{10}(x)') title('y1=log-{10}(x)') xlabel('x'), ylabel('y1') subplot(2,1,2) plot(x,y2,'k-'),grid on legend('\ity2=exp(x)') title('y2=exp(x)') xlabel('x'), ylabel('y2') 3、设有函数y=exp(x+5)+x、^3,在半对数坐标系中绘制曲线。 程序为: x=1:0、01:10; y=exp(x+5)+x、^3; figure(1) subplot(2,1,1) plot(x,y,'r-'),grid on legend('\ity=exp(x+5)+x、^3') title('平面坐标') xlabel('x'), ylabel('y') subplot(2,1,2) semilogx(x,y,'k-'),grid on %半对数坐标轴 legend('\ity=exp(x+5)+x、^3') title('半对数坐标') xlabel('x'), ylabel('y') 4、画出各种大小与形状得球与柱体。 绘制柱体得程序为: t=0:pi/10:2*pi; figure(1) subplot(2,1,1) [x,y,z]=cylinder(2+cos(t)); surf(x,y,z),axis square title('复杂柱面体') subplot(2,1,2) cylinder, axis square title('简单柱体') 绘制球得程序为: figure(1) subplot(2,1,1) sphere axis equal title('半径为1得球') subplot(2,1,2) [x,y,z]=sphere; x=2*x; y=2*y; z=2*z; surf(x,y,z),axis square title('半径为2得球') 运行后得图形: 5、绘制三维条形图: 程序为: Y=cool(7); figure(1) subplot(2,2,1),bar3(Y,'detached'),title('Detached') subplot(2,2,2),bar3(Y,0、25,'detached'),title('Width=0、25') subplot(2,2,3),bar3(Y,'grouped'),title('Grouped') subplot(2,2,4),bar3(Y,'stacked'),title('Stacked') 运行后得图形为: 6、绘制二维条形图 程序为: Y=round(rand(5,3)*10); figure(1) subplot(2,2,1),bar(Y,'group'),title('Group') subplot(2,2,2),bar(Y,'stack'),title('Stack') subplot(2,2,3),barh(Y,'stack'),title('Stack') subplot(2,2,4),bar(Y,1、5),title('Width=1、5') 运行后得图形: 1、编写M函数实现:求一个数就是否为素数,在编写一主程序,要求通过键盘输入一个整数,然后完成判断其就是否为素数。 解: function prime(x) n=fix(sqrt(x)); for i=2:n if rem(x,i)==0 a='fasle' return else a='true' end end 运行结果: >> x=56; >> prime(x) a = fasle 2、编写程序完成从表示字符得响亮中删去空格,并求出字符个数。 解: function [nstr,n]=del(str) nstr=[]; k=find(str~=' '); nstr=str(k); n=length(nstr); end 运行后为: str='dr hy fgh gtesd hgfds'; >> [nstr,n]=del(str) nstr = drhyfghgtesdhgfds n = 17 3、编写M函数统计十进制数值中’0‘得个数,然后编写脚本文件,实现统计所有自然数1~2006中0得个数。 解: M函数为: function y=geshu(x) s=num2str(x); n=length(s); m=0; if s(1)=='0' disp('x is error'); return end for i=2:n if s(i)=='0' m=m+1; end end y=m; 脚本文件为 'jiu4': sum=0; for x=1:2006 y=geshu(x); sum=sum+y; end disp(sum) 运行结果为: >> jiu4 504 4、利用menu函数输入选择参数ch。当ch=1时,产生[-10,10]之间均匀分布得随机数;当ch=2时,产生[-5,5]之间均匀分布得随机数;当ch=3时,产生[-1,1]之间均匀分布得随机数;当ch=4时,产生均值为0,方差为1得正态分布随机数。要求使用switch函数。 解: s=menu('ch','1','2','3','4'); n=[]; switch s case 1,n=20*rand(3)-10 case 2,n=10*rand(3)-5 case 3,n=2*rand(3)-1 case 4,n=randn(3) otherwise disp('error') end 运行后: 按下2后: n = 4、2274 0、4366 3、3897 3、0037 4、8478 -0、6674 -2、1405 2、1568 -0、2938 5、求阵列x得平均值与标准差 解: function [mean1,stdev]=stat2(x) [m,n]=size(x); if m==1 m=n; end s1=sum(x);s2=sum(x、^2); mean1=s1/m; stdev=sqrt(s2/m-mean1、^2); 运行后: >> x=rand(4,4)+2; >> [mean1,stdev]=stat2(x) mean1 = 2、5207 2、3922 2、6498 2、2539 stdev = 0、1713 0、1892 0、1725 0、2027 6、测试程序执行时间 % tech1、m tic i=0; for t=0:、01:100 i=i+1; y(i)=sin(t); end toc % tech2、m tic t=0:、01:100; y=sin(t); Toc 运行后: Elapsed time is 0、015217 seconds、 Elapsed time is 0、000508 seconds、 1、产生menu选择输出颜色 解: s=menu('color selection','red','green','blue','yellow','black') switch s case 1,scolor='red'; case 2,scolor='green'; case 3,scolor='blue'; case 4,scolor='yellow'; case 5,scolor='black'; otherwise disp('error') end Scolor 2、企业发放得奖金按个人完成得利润(I)提成。分段提成比例 wei 即如王某完成25万元利润时,个人可得 y=10 x 10% + 10 x 5% + 5 x 2% (万元) 据此编写程序,求企业职工得奖金。 解 function bonus=bon(I) n=fix(I/100000) if(n>4) n=4; end bon1=100000*0、1; bon2=0、05*(200000-100000); bon3=0、02*(400000-200000); switch n case 0,bonus=I*100000; case 1 bonus=bon1+0、05*(I-100000); case {2,3} bonus=bon1+bon2+0、02*(I-200000); case 4,bonus=bon1+bon2+bon3+0、01*(I-400000); end 运行后: >> I=1700000; >> bonus=bon(I) n = 17 bonus = 32000 3、有一分数序列2/1,3/2,5/3/,8/5……求前15项与。 解:s=1;t=2;sum=0; x=t/s; sum=sum+x; for i=1:15 z=t;t=s+t;s=z; x=t/s; sum=sum+x; end sum 运行后: >> qiuhe sum = 26、1881 4、约瑟夫环 解: n=input('please input n:'); m=input('please input m:'); b=1:n; i=1;c=0;s=0; while s<n if b(i)~=0 c=c+1; if c==m s=s+1; a(s)=b(i); b(i)=0; c=0; end end if i==n i=0; end i=i+1; end a 运行后: >> yuese please input n:12 please input m:3 a = Columns 1 through 8 3 6 9 12 4 8 1 7 Columns 9 through 16 2 11 5 10 3 16 5 20 Columns 17 through 23 11 9 2 10 19 15 1 5、编写程序计算x在(-3,3)上,并画出曲线。 解:function y=func2(x) n=length(x); for i=1:n; if (x(i)>=-3)&&(x(i)<-1) y(i)=[-x(i)、^2-4*x(i)-3]/2; elseif (x(i)>=-1)&&(x(i)<1) y(i)=-x(i)、^2+1; else (x(i)>=1)&&(x(i)<3) y(i)=[-x(i)、^2+4*x(i)-3]/2; end end 脚本为: x=-3:、01:3; y=func2(x); figure(1) plot(x,y),grid on title('y=func2(x)') xlabel('x'), ylabel('y') 运行后: 1、求矩阵 与 得逆矩阵与行列式。 解: a=[5 3 5;3 7 4;7 9 8]; b=[2 4 2;6 7 9;8 3 6]; c1=inv(a) c2=det(a) d1=inv(b) d2=det(b) 运行后: c1 = 10、0000 10、5000 -11、5000 2、0000 2、5000 -2、5000 -11、0000 -12、0000 13、0000 c2 = 2、0000 d1 = 0、1531 -0、1837 0、2245 0、3673 -0、0408 -0、0612 -0、3878 0、2653 -0、1020 d2 = 98、0000 2、解方程组 解: A=[3 2 1;1 -1 3;2 4 -4]; b=[7 6 -2]; A\b' 运行后: ans = 1、0000 1、0000 2、0000 2、对一组数据进行分别采用y1(t)=c1+c2exp(-t),y2(t)=d1+d2t、*exp(-t)拟合、 解: t=[1 2 3 4 5 6 7 8 9 10]'; y=[4、842 4、362 3、754 3、368 3、169 3、083 3、034 3、016 3、012 3、005]'; a=[ones(size(t)) exp(-t)]; C=a\y; b=[ones(size(t)) t、*exp(-t)]; D=b\y; T=[10:-1:1]'; y1=[ones(size(T)) exp(-T)]*C; y2=[ones(size(T)) T、*exp(-T)]*D; plot(T,y1,'r--',T,y2,'k-',t,y,'o'); legend('\ity1(t)=c1+c2exp(-t)','\ity2(t)=d1+d2t、*exp(-t)') title('曲线拟合') xlabel('\itt'),ylabel('\ity') 运行后: 4、矩阵 ,分别对a进行特征值分解、奇异值分解、LU分解、QR分解。 解: >> [v,d]=eig(a,b) v = -0、4330 -0、2543 -0、1744 -0、5657 0、9660 -0、6091 -0、7018 0、0472 0、7736 d = 13、5482 0 0 0 4、8303 0 0 0 3、6216 >> a=[9 1 2;5 6 3;8 2 7]; >> [u,s,v]=svd(a) u = -0、5601 0、5320 -0、6350 -0、4762 -0、8340 -0、2788 -0、6779 0、1462 0、7204 s = 15、5234 0 0 0 4、5648 0 0 0 3、3446 v = -0、8275 0、3917 -0、4023 -0、3075 -0、9156 -0、2592 -0、4699 -0、0907 0、8781 >> [l,u]=lu(a) l = 1、0000 0 0 0、5556 1、0000 0 0、8889 0、2041 1、0000 u = 9、0000 1、0000 2、0000 0 5、4444 1、8889 0 0 4、8367 >> [q,r]=qr(a) q = -0、6903 0、3969 -0、6050 -0、3835 -0、9097 -0、1592 -0、6136 0、1221 0、7801 r = -13、0384 -4、2183 -6、8260 0 -4、8172 -1、0807 0 0 3、7733 5、求解微分方程 。 解: function dy=funf(t,y) dy=[5*y(1)-5*y(2)-6*y(3);3*y(1)-2*y(2)+5*y(3);2*y(1)-y(2)-4*y(3)]; 脚本文件: x0=[1,-4,5]'; tspan=[30,100]; [t,x]=ode45('funf',tspan,x0); plot3(x(:,1),x(:,2),x(:,3)),grid on title('微分方程曲线') 运行后: 微分方程组x’=10(-x+y);y’=28x-y-xz;z’=xy-8z/3,x0=[12,2,9],求微分方程在[0,30]上得解,并画出系统轨迹。 解: 脚本文件: 二维图: 三维图: 2、分别用多项式与指数函数进行拟合。 y1(t)=c1+c2t+c3t2,y2(t)=d1+d2exp(t) 解: t=[0 0、2 0、4 0、6 0、8 1、0 2、0 5、0]'; y=[1、0 1、51 1、88 2、13 2、29 2、40 2、60 -4、00]'; B1=[ones(size(t)) t t、*t]; B2=[ones(size(t)) exp(t)]; A=B1\y; C=B2\y; T=[0:、1:6]'; Y1=[ones(size(T)) T T、*T]*A; Y2=[ones(size(T)) exp(T)]*C; plot(T,Y1,'-',T,Y2,'--',t,y,'o') legend('\itY1','\itY2') 3、 将(x-6)(x-3)(x-8)展开为系数多项式得形式。 解: >> a=[6 3 8]; >> pa=poly(a); >> ppa=poly2sym(pa) ppa = x^3-17*x^2+90*x-144 4、 求解多项式x3-7x2+2x+40得根。 解: >> r=[1 -7 2 40]; >> p=roots(r); -0、2151 0、4459 0、7949 0、2707 5、 求解在x=8时多项式(x-1)(x-2) (x-3)(x-4)得值。 解: >> p=poly([1 2 3 4]); >> polyvalm(p,8) ans = 840 6、 计算多项式乘法(x2+2x+2)(x2+5x+4)。 解: >> c=conv([1 2 2],[1 5 4]) c = 1 7 16 18 8 7、 计算多项式除法(3x3+13x2+6x+8)/(x+4)。 解: >> d=deconv([3 13 6 8],[1 4]) d = 3 1 2 9、微分方程组 当t=0, =1; =-0、5,求微分方程组t~【0,25】上得解,并画出x1-x2得系统轨迹。 解: function dy=fund(t,y) dy=[0、5-y(1);y(1)-4*y(2)]; 脚本文件: x0=[1,-0、5]; tspan=[0,20]; [T,Y]=ode23('fund',tspan,x0); figure(1) plot(T,Y(:,1),'r--',T,Y(:,2)) legend('\itx1','\itx2') 1.利用下标建立多维阵列。 产生一个3×3×2得多维矩阵A >>A=[5 7 2; 0 1 2; 3 4 2]; %产生一个3*3矩阵 >>A(:, :, 2)=[2 7 3; 4 2 8; 2 0 3] A(:,:,1) = 5 7 2 0 1 2 3 4 2 A(:,:,2) = 2 7 3 4 2 8   2 0 3 2.利用MATLAB函数产生多维阵列。 利用MATLAB得函数(如rand、randn、ones、zeros等)都可直接产生多维阵列,在函数调用时可指定每一维得尺寸。例如,为产生100×3×2维得正态分布随机数R,可输入   >>R=randn(100, 3, 2);   >>A=5*ones(3, 4, 2); %产生元素相同得多维阵列 >>B=repmat(5, [3 4 2]); %产生元素相同得多维阵列 3.利用cat函数建立多维阵列  >>A=[2 8; 0 5]; B=[1 8; 2 4]; >>C=cat(3,A,B); >>D=cat(4,A,B); >> size(C) ans = 2 2 2 >> size(D) ans = 2 2 1 2 这说明得到得C为2×2×2维,而D为2×2×1×2维。 1、冒泡法排序 function y=bubblesort(x) %冒泡法排序、 n=length(x); for i=1:n-1 for j=i+1:n if x(i)>x(j) temp=x(i); x(i)=x(j); x(j)=temp; end end end y=x; 运行结果: >> x=[12 34 654 2 5 76 23]; >> y=bubblesort(x) y = 2 5 12 23 34 76 654 以上为按照升序排列得,若要降序,则 if x(i)<x(j) temp=x(i); x(i)=x(j); x(j)=temp; 即可 运行结果: >> x=[12 21 2 4 5 19 45 30]; >> y=bubblesort(x) y = 45 30 21 19 12 5 4 2 2、傅里叶变换 应用付立叶变换并求频谱图 clc; clf; clear all; fs=1000; t=0:1/fs:0、6; f1=200; f2=300; x=sin(2*pi*f1*t)+sin(2*pi*f2*t); subplot(4,1,1); plot(n, x); title('f1(100Hz)\f2(300Hz)得正弦信号,初相0'); xlabel('序列(n)'); grid on; number=512; y=fft(x,number); n=0:length(y)-1; f=fs*n/length(y); subplot(4,1,2); plot(f,abs(y)/max(abs(y))); hold on; plot(f,abs(fftshift(y))/max(abs(y)),'r'); title('f1\f2得正弦信号得FFT(512点)'); xlabel('频率Hz'); grid on; x=x+randn(1,length(x)); subplot(4,1,3); plot(n, x); title('原f1\f2得正弦信号(含随机噪声)'); xlabel('序列(n)'); grid on; y=fft(x,number); n=0:length(y)-1; f=fs*n/length(y); subplot(4,1,4); plot(f,abs(y)/max(abs(y))); title('原f1\f2得正弦信号(含随机噪声)得FFT(512点'); xlabel('频率Hz'); grid on; 4、绘图工具得应用 ,当x与y得取值
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服