MATLAB基础与应用教程 习题答案 作者 蔡旭晖 刘卫国 蔡立燕 第1-8章答案

合集下载

第01章_MATLAB系统环境_参考解答

第01章_MATLAB系统环境_参考解答

高教社刘卫国《MATLAB程序设计与应用》(第二版)习题参考解答第1章:MATLAB系统基础第1章 MATLAB系统环境教材P16习题一1. 与其他高级语言相比,MATLAB有哪些显著特点?答:与其他高级语言相比,MATLAB简单易学,编程效率高。

有以下显著特点:(1) 可靠的数值计算和符号计算功能;(2) 强大的绘图功能;(3) 简单易学的语言体系;(4) 为数众多的应用工具箱。

2. 怎样理解MATLAB的开放性?试结合自己的专业领域,为MATLAB设计一个工具箱(例如,为一个桥梁专家设计一个MATLAB桥梁设计工具箱)。

答:MATLAB具有很强的开放性。

除内部函数外,所有MATLAB基本文件和各工具箱文件都是可读、可改的源文件,用户可通过对源文件的修改或加入自己编写的文件去构成新的专用工具箱。

比如,通信信号处理工具箱Communication Signal Processing Toolbox,电路设计工具箱Circuit Design Toolbox,等等。

3. 先建立自己的工作目录,再将自己的工作目录设置在MATLAB搜索路径下。

用help 命令能查询到自己的工作目录吗?答:能。

4. 李明同学设计了一个程序文件myprogram.m,并将其保存到了f:\ppp中,但在命令窗口中输入文件名>>myprogram后,MATLAB系统提示:??? Undefined function or variable 'myprogram'.试分析产生错误的原因并给出解决办法。

答:错误原因:myprogram.m不在MATLAB的搜索路径中。

解决办法:将f:\cpp添加到搜索路径列表中。

5. 利用MATLAB的帮助功能分别查询inv、plot、max、round等函数的功能和用法。

答:查询命令help inv (plot/max/round)inv_A=inv(A); %求方阵A的逆plot(x,y); %画以x,y为横纵坐标变量的二维曲线图max_A=max(A); %求矩阵A中每一列的最大元素round(x); %将变量x四舍五入取整6. 访问MathWorks公司的主页,查询有关MATLAB的产品信息。

MAAB程序设计与应用刘卫国编课后实验答案

MAAB程序设计与应用刘卫国编课后实验答案

实验一 MATLAB运算基础1. 先求下列表达式的值,然后显示MATLAB工作空间的使用情况并保存全部变量。

(1)0 122sin851ze =+(2)21ln( 2z x=+,其中2120.455i x+⎡⎤=⎢⎥-⎣⎦(3)0.30.330.3sin(0.3)ln, 3.0, 2.9,,2.9,3.0 22a ae e az a a--+=++=--(4)2242011122123t tz t tt t t⎧≤<⎪=-≤<⎨⎪-+≤<⎩,其中t=0:0.5:2.5解:2. 已知:求下列表达式的值:(1) A+6*B和A-B+I(其中I为单位矩阵)(2) A*B和A.*B(3) A^3和A.^3(4) A/B及B\A(5) [A,B]和[A([1,3],:);B^2]解:运算结果:3. 设有矩阵A和B(1) 求它们的乘积C。

(2) 将矩阵C的右下角3×2子矩阵赋给D。

(3) 查看MATLAB工作空间的使用情况。

解:. 运算结果:4. 完成下列操作:(1) 求[100,999]之间能被21整除的数的个数。

(2) 建立一个字符串向量,删除其中的大写字母。

解:(1) 结果:(2). 建立一个字符串向量例如:ch='ABC123d4e56Fg9';则要求结果是:实验二 MATLAB矩阵分析与处理1. 设有分块矩阵33322322E R A O S ⨯⨯⨯⨯⎡⎤=⎢⎥⎣⎦,其中E 、R 、O 、S 分别为单位矩阵、随机矩阵、零矩阵和对角阵,试通过数值计算验证22E R RS A OS +⎡⎤=⎢⎥⎣⎦。

解: M 文件如下;输出结果:由ans,所以22E R RS A O S +⎡⎤=⎢⎥⎣⎦2. 产生5阶希尔伯特矩阵H 和5阶帕斯卡矩阵P ,且求其行列式的值Hh 和Hp 以及它们的条件数Th 和Tp ,判断哪个矩阵性能更好。

为什么?解:M文件如下:输出结果:因为它们的条件数Th>>Tp,所以pascal矩阵性能更好。

Matlab基础与应用教程答案

Matlab基础与应用教程答案

第1章一、思考题4.( 1) B=A(2:5,1:2:5)(2)A(7)=[](3)A=A+30(4)size(A)( 5) t(find(t==0))=eps(6)t=reshape(x,3,4)(7)abs('matlab')(8)char(93)5.A=[97,67,34,10;-78,75,65,5;32,5,-23,-59;0,-12,54,7] (1)B=A(1:3,:)C=A(:,1:2)D=A(2:4,3:4)E=B*C(2)A(find(A>=50 & A<=100)) 二、实验题=-74/180*pi;y=-27/180*pi;sin(x*x+y*y)/sqrt(tan(abs(x+y)))+pi一・・,exp*a).*sin(a+=[2,4;,5];log(x+sqrt(1+x.*x))/24.A=[3,54,2;34,-45,7;87,90,15];B=[1,-2,67;2,8,74;9,3,0];(1)A*Bans =129 432 41977 -407 - 1052402 591 12489A.*Bans =3 -108 13468 -360 518783 270 0(2) A A3ans =-28917 240246 -4368137883 -259101 27669171333 252504 38673A.A3ans =27 157464 839304 -91125 343658503 729000 3375( 3) A/Bans =B/A ans =(4)[A,B]ans =3 54 2 1 -2 6734 -45 7 2 8 7487 90 15 9 3 0[&[1,3],:)砂2]ans =3 54 287 90 15600 183 -81684 282 72615 6 825=1+2i;b=3+4i; c=exp(pi*i/6) c =+ c+a*b/(a+b) ans =+ 第2章一、思考题=0; for n=0:63s=s+2A n;end disp(s)n=0:63;s=sum(2.An)二、实验题1.x=input( ' 输入一个四位整数:' );y=[fix(x/1000),mod(fix(x/100),10),mod(fix(x/10),10),mod(x,1 0)] z=mod((y+7),10)x=z(3)*1000+z(4)*100+z(1)*10+z(2)2. gh=input( ' 输入工号' ); h=input( ' 输入工时' );dj=84;if h>120gz=dj*120+*dj*(h-120);elseif h<60gz=dj*h-700;else gz=dj*h;end format bank; display([gh,gz])3. 循环结构n=input( 'input n:' );s=0;for k=1:n s=s+1/k A2;end display(sqrt(s*6)) 向量运算n=input( 'input n:' ); k=1:n;display(sqrt(sum(1./k.A2)*6))4.y=0;k=0;while y<3k=k+1; y=y+1/(2*k-1);end display([k-1,y-1/(2*k-1)])5.x0=0;x=1;k=0;a=input( 'a=' ); b=input( 'b=' );while abs(x-x0)>=1e-5 && k<500 x0=x;x=a/(b+x0);k=k+1;end display([k,x]); display([(-b+sqrt(bA2+4*a))/2,(-b-sqrt(bA2+4*a))/2]);6.y=fun(40)/(fun(30)+fun(20))(1)函数文件function f=fun(n) f=n+log(nA2+5);(2)函数文件function f=fun(n) a=1:n;f=sum(a.*(a+1));第3章一、思考题4.t=0::;y=sqrt(3)/2*exp(-4*t).*sin(4*sqrt(3)*t+pi/3);5.x=-10::10; y=linspace(-6,6,size(x,2))z=x.A3+3*x.*y.A2;plot3(x,y,z)6.x=100:100:400;y=100:100:400;z=[636,697,624,478;698,712,630,478;680,674,598,412;662,626,552,334]; [X,Y]=meshgrid(x,y);mesh(X,Y,z)二、实验题1.( 1 ) x=-10::10;plot(x,x-x.A3/3/2)( 2) plot(x,exp(-x.*x/2)/2/pi)(3)x=-8::8;plot(x,sqrt((64-x.*x)/2))( 4) t=0:pi/100:6*pi;plot(t.*sin(t),t.*cos(t))2.( 1) x1=linspace(0,1,100);y1=2*;t=linspace(0,pi,100);x=sin(3*t).*cos(t);y=sin(3*t).*sin(t);plot(x1,y1,'r-',x,y,'b:');text,1,'y='); text,,'x=sin(3t)cos(t)');text,,'y=sin(3t)sin(t)');(2)subplot(1,2,1);scatter(x1,y1,10)title('y=');subplot(1,2,2);scatter(x,y,10)3.subplot(1,2,1);x=1:1:100; y=sin(1./x);plot(x,y) subplot(1,2,2);fplot('sin(1/x)',[1,100])4.subplot(2,2,1);bar(t,y);subplot(2,2,2);stairs(t,y);subplot(2,2,3);stem(t,y) subplot(2,2,4); semilogy(t,y);3.theta=linspace(-pi,pi,100); ro=5.*cos(theta)+4; polar(theta,ro); fi=linspace(0,2*pi,100);a=1 r=a.*(1+cos(fi));polar(fi,r);4.plot3(x,y,z);t=0::1;x=t;y=t.A2;z=t.A3;plot3(x,y,z);7.x=-30::0;y=0::30; [x,y]=meshgrid(x,y);z=10.*sin(sqrt(x.A2+y.A2))./sqrt(1+x.A2+y.A2); meshc(x,y,z);8. x=linspace(-3,3,100);y=linspace(-3,3,100);[x y]=meshgrid(x,y);fxy=-5./(1+x.A2+y.A2);i=find(abs(x)<= & abs(y)<=; fxy(i)=NaN;surf(x,y,fxy)9.u=linspace(1,10,100);v=linspace(-pi,pi,100); [u,v]=meshgrid(u,v);x=3.*u.*sin(v);y=2.*u.*cos(v);z=4*u.A2;x=3*u.*sin(v);y=2*u.*cos(v);z=4*u.A2; surf(x,y,z);shading interp;light('position',[1,0,1]);10. t=0:pi/100:2*pi; y=sin(t);comet(t,y) 第4章一、思考题5.(1)A=eye(3);(2)C=100+(200-100)*rand(5,6);( 3) D=1+sqrt*randn(1,500);( 4) E=ones(size(A));( 5) A=A+30*eye(size(A));( 6) B=diag(diag(A))二、实验题1.P=pascal(5);H=hilb(5); Dp=det(P);Dh=det(H); Kp=cond(P);Kh=cond(H);P矩阵的性能更好,因为Kp较小2.A=[1,-1,2,3;0,9,3,3;7,-5,0,2;23,6,8,3] B=[3,pi/2,45;32,-76,sqrt(37);5,72,;exp(2),0,97] A1=diag(A);B1=diag(B);A2=triu(A);B2=triu(B); A3=tril(A);B3=tril(B); rA=rank(A);rB=rank(B);nA=norm(A);nb=norm(B); cA=cond(A);cB=cond(B);3.A=[31,1,0;-4,-1,0;4,-8,-2] ;[V,D]=eig(A);4.A=diag([-1,-1,-1,-1],-1)+diag([-1,-1,-1,-1],1)+diag([2,2,2,2,2])b=[1,0,0,0,0]';x1=inv(A)*b; x2=A\b; [L,U]=lu(A); x3=U\(L\b); [Q,R]=qr(a); [Q,R]=qr(A); x4=R\(Q\b) R=chol(A); x5=R\(R'\b) 5.B=sparse(A); x1=inv(B)*b; x2=B\b; [L,U]=lu(B); x3=U\(L\b); 第5章一、思考题3.A=randn(10,5); mean(A) std(A) max(max(A)) min(min(A)) sum(A,2) sum(sum(A)) sort(A,1) sort(A,2,'descend') 二、实验题1.A=rand(1,30000); mean(A) std(A) max(A) min(A) size(find(A>)/size(A)2.h=[466,715,950,1422,1635]; w=[,,,,];hh=[500,900,1500]; ww=interp1(h,w,hh,'spline')3.x=linspace(1,10,50); y=log(x);第6章一、思考题2.fx=i nlin e('1./(1+x.A 2)');[I,n]=quad(fx,-100000,100000,1e-3); [I,n]=quadl(fx,-100000,100000,1e-3); x=-100000::100000; y=1./(1+x.*x);f=polyfit(x,y,5); yy=polyval(f,x); plot(x,y,'r-',x,yy,'g.') 4. N=64; T=5;t=linspace(0,T,N); x=3*exp(-t); % dt=t(2)-t(1); f=1/dt; X=fft(x); F=X(1:N/2+1); f=f*(0:N/2)/N;plot(f,abs(F),'-*') % %% 求各采样点样本值 x% % %采样点数 采样时间终点给岀N 个采样时间ti(l=1:N) 采样周期采样频率 (Hz)计算 x 的快速傅立叶变换 X% F(k)=X(k)(k=1:N/2+1)% 使频率轴 f 从零开始 % 绘制振幅 - 频率图xlabel('Frequency');ylabel('|F(k)|')5.(1)p1=[1 2 0 0 7];p2=[1 -2];p3=[1 0 0 5 1];p12=conv(p1,p2);p=p12+[zeros(1,size(p12,2)-size(p3,2)),p3]; roots(p)(2)A=[-1,4,3;2,1,5;0,5,6];Px=polyval(p,A) Pxm=polyvalm(p,A)6.(1) z=fzero('3*x-sin(x)+1',0)(2)建立函数文件 function F=myfun(X)x=X(1);y=X(2); F(1)=x*x+y*y-9;F(2)=x+y-1; 在命令窗口中输入以下命令: x=fsolve(@myfun,[3,0]',optimset('Display','of f'))trapz(x,y);3.(1)fx=inline('-2*y+2*x*x+2*x');[t,y]=ode23(fx,[0,],1)(2)fx=inline('y-exp(x)*cos(x)');[t,y]=ode23(fx,[0,3],1)二、实验题1.for x=1:3fx=[x,xA2,xA3;1,2*x,3*x;0,2,6*x]; diff(fx)end2.(1 ) x=0::1;y=x.A10+10.Ax+1./log10(x);dy=diff(y)/;(2)x=0::1;y=log(1+x);dy=diff(y,2)/;plot(x(1:99),dy)3.(1 ) fx=inline( 'x.A2.*sqrt(2*x.*x+3)');quad(fx,1,5)(2)fx=inline( 'x./sin(x).A2' ); quad(fx,pi/4,pi/3)(3)fx=inline( 'abs(cos(x+y))' ); dblquad(fx,0,pi,0,pi)(4)syms x y;fx=x*y;int(int(fx,yA2,y+2),-1,2)x的积分区间为【0, 2】时fx=inline( 'x.*y' );dblquad(fx,0,2,-1,2)4.x=::;y=[,,,,,,];trapz(x,y)5.(1)yp=i nlin e( '-+si n( 10*x))*y' );[t,y]=ode23(yp,[0,5],1);(2)令x1y, x2y ,x3 y'',则可写出原方程的状态方程形式: x1x2X2 X3cos 1x3cost 5 X3 X2 X1(t 1)2 3 sin t0 1 0 X1 0=>> x20 0 1 X2 01 / 5cos2t X3 12X3 cost3 sint (t 1)2建立函数文件fun cti on y=ztfu n( t,x)b=[0;0;cos(t)];y=[0,1,0;0,0,1;-1/(3+si n( t)),-1,5*cos(2*t)/(t+1)A2]*x+b; 解微分方程组[t,y]=ode23(@ztfu n,[0,5],[1;0;2]);6.建立函数文件fun cti on yy=ztfu n( t,y)yy=[ y( 2)*y(3);-y(1)* y(3) ;*y(1)*y(2)];解微分方程组[t,y]=ode23(@ztfu n,[0,5],[0;1;1])第7章一、思考题3.(1 )数值积分fx=i nlin e('exp(x).*(1+exp(x)).A2'); quad(fx,0,log(2)) 符号积分f=sym('exp(x)*(1+exp(x))A2');v=in t(f,0,log (2));eval(v)(2 )略二、实验题1.A=sym('[1,2,3;x,y, z; 3,2,1]')rank(A)inv(A)det(A)2.(1)y=sym('sqrt(x+sqrt(x+sqrt(x)))');y1=diff(y)y2=diff(y,'x',2)(2)syms x y;fxy=si n( x A2*y)*exp(-x A2-y);diff(diff(fxy,x),y)3.(1)syms xin t(1/(1+xA4))(2)syms x tin t((-2*x*x+1)/(2*x*x-3*x+1)A2,x,cos(t),exp(2*t))4.syms n xsymsum(1/(2* n+1)/(2*x+1)A(2* n+1), n,0,i nf)symsum(1/(2* n+1)/(2*x+1)A(2* n+1), n, 0,5)5.(1)syms xtaylor((exp(x)+exp(-x))/2,5,0)(2)syms a xtaylor(exp(-5*x)*si n(3*x+pi/3),5,a)6.(1)x=solve(sym('xA3+a*x+1=0'))(2)[x y]=solve(sym('sqrt(xA2+yA2)-100=0,3*x+5*y-8=0'))y' y17.方程转化为:’,严y1' y 1 一符号解[y1,y11]=dsolve(,Dy=y1,Dy1+y=1-t A2/pi,,,y(-2)=5,y1(-2)=5',,t,)数值解编写函数文件fun cti on yy=ztfu n( t,y)yy=[y(2);1-tA2/pi-y(1)];在命令窗口输入以下命令[t,y]=ode45(@ztfu n,[-2,7],[-5;5]);t=li nspace(-2,7,49)y2=y8.[x,y]=dsolve('Dx=3*x+4*y,Dy=-4*x+3*y','x(0)=0,y(0)=1')第9章二、实验题1.(1 )新建一个Blank GUI。

MATLAB程序设计与应用(刘卫国编)课后实验答案之欧阳家百创编

MATLAB程序设计与应用(刘卫国编)课后实验答案之欧阳家百创编

实验一MATLAB运算基础欧阳家百(2021.03.07)1. 先求下列表达式的值,然后显示MATLAB工作空间的使用情况并保存全部变量。

(1)0 122sin851ze =+(2)21ln( 2z x=+,其中2120.455i x+⎡⎤=⎢⎥-⎣⎦(3)0.30.330.3sin(0.3)ln, 3.0, 2.9,,2.9,3.0 22a ae e az a a--+=++=--(4)2242011122123t tz t tt t t⎧≤<⎪=-≤<⎨⎪-+≤<⎩,其中t=0:0.5:2.5解:M文件:z1=2*sin(85*pi/180)/(1+exp(2))x=[2 1+2*i;-.45 5];z2=1/2*log(x+sqrt(1+x^2))a=-3.0:0.1:3.0;z3=(exp(0.3.*a)-exp(-0.3.*a))./2.*sin(a+0.3)+log((0.3+a)./2)t=0:0.5:2.5;z4=(t>=0&t<1).*(t.^2)+(t>=1&t<2).*(t.^2-1)+(t>=2&t<3) .*(t.^2-2*t+1) 运算结果:z1=2*sin(85*pi/180)/(1+exp(2))x=[2 1+2*i;-.45 5];z2=1/2*log(x+sqrt(1+x^2))a=-3.0:0.1:3.0;z3=(exp(0.3.*a)-exp(-0.3.*a))./2.*sin(a+0.3)+log((0.3+a)./2)t=0:0.5:2.5;z4=(t>=0&t<1).*(t.^2)+(t>=1&t<2).*(t.^2-1)+(t>=2&t<3) .*(t.^2-2*t+1) z1 =0.2375z2 =0.7114 - 0.0253i 0.8968 + 0.3658i0.2139 + 0.9343i 1.1541 - 0.0044iz3 =Columns 1 through 40.7388 + 3.1416i 0.7696 + 3.1416i 0.7871 + 3.1416i 0.7913 + 3.1416i Columns 5 through 80.7822 + 3.1416i 0.7602 + 3.1416i 0.7254 + 3.1416i 0.6784 + 3.1416i Columns 9 through 120.6196 + 3.1416i 0.5496 + 3.1416i 0.4688 + 3.1416i 0.3780 + 3.1416i Columns 13 through 160.2775 + 3.1416i 0.1680 + 3.1416i 0.0497 + 3.1416i -0.0771 + 3.1416i Columns 17 through 20-0.2124 + 3.1416i -0.3566 + 3.1416i -0.5104 + 3.1416i -0.6752 + 3.1416i Columns 21 through 24-0.8536 + 3.1416i -1.0497 + 3.1416i -1.2701 + 3.1416i -1.5271 + 3.1416i Columns 25 through 28-1.8436 + 3.1416i -2.2727 + 3.1416i -2.9837 + 3.1416i -37.0245Columns 29 through 32-3.0017 -2.3085 -1.8971 -1.5978Columns 33 through 36-1.3575 -1.1531 -0.9723 -0.8083Columns 37 through 40-0.6567 -0.5151 -0.3819 -0.2561Columns 41 through 44-0.1374 -0.0255 0.0792 0.1766Columns 45 through 480.2663 0.3478 0.4206 0.4841Columns 49 through 520.5379 0.5815 0.6145 0.6366Columns 53 through 560.6474 0.6470 0.6351 0.6119Columns 57 through 600.5777 0.5327 0.4774 0.4126Column 610.3388z4 =0 0.2500 0 1.2500 1.0000 2.25002. 已知:求下列表达式的值:(1) A+6*B和A-B+I(其中I为单位矩阵)(2) A*B和A.*B(3) A^3和A.^3(4) A/B及B\A(5) [A,B]和[A([1,3],:);B^2]解:M 文件:A=[12 34 -4;34 7 87;3 65 7];B=[1 3 -1;2 0 3;3 -2 7]; A+6.*BA-B+eye(3)A*BA.*BA^3A.^3A/BB\A[A,B][A([1,3],:);B^2]运算结果:A=[12 34 -4;34 7 87;3 65 7];B=[1 3 -1;2 0 3;3 -2 7];A+6.*BA-B+eye(3)A*BA.*BA^3A.^3A/BB\A[A,B][A([1,3],:);B^2]ans =18 52 -1046 7 10521 53 49ans =12 31 -332 8 840 67 1ans =68 44 62309 -72 596154 -5 241ans =12 102 468 0 2619 -130 49ans =37226 233824 48604247370 149188 60076678688 454142 118820ans =1728 39304 -6439304 343 65850327 274625 343ans =16.4000 -13.6000 7.600035.8000 -76.2000 50.200067.0000 -134.0000 68.0000ans =109.4000 -131.2000 322.8000-53.0000 85.0000 -171.0000-61.6000 89.8000 -186.2000ans =12 34 -4 1 3 -134 7 87 2 0 33 65 7 3 -2 7ans =12 34 -43 65 74 5 111 0 1920 -5 403. 设有矩阵A和B(1) 求它们的乘积C。

刘卫国matlab课后习题答案

刘卫国matlab课后习题答案

刘卫国matlab课后习题答案【篇一:matlab程序设计与应用第二版刘卫国课后实验答案】%第一小题z1=2*sin(85*pi/180)/(1+exp(2)) %%第二小题 x=[2,1+2i;-0.45,5];z2=1/2*log(x+sqrt(1+x.^2)); z2%%第三小题 a=-3.0:0.1:3.0;z3=1/2*(exp(0.3*a)-exp(-0.3*a)).*sin(a+0.3)+log((0.3+a)/2)%%第四题 t=0:0.5:2.5z4=(t=0t1).*(t.^2)+(t=1t2).*(t.^2-1)+(t=2t3).*(t.^2-2*t+1)t2:a=[12,34,-4;34,7,87;3,65,7] b=[1,3,-1;2,0,3;3,-2,7] disp (a+6*b=); disp(a+6*b); disp(a-b+i=); disp(a-b+eye(3)); disp(a*b=);disp(a*b); disp(a.*b=); disp(a.*b); disp(a^3=); disp(a^3);disp(a.^3=); disp(a.^3); disp(a/b=); disp(a/b); disp(b\a=);disp(b\a); disp([a,b]=); disp([a,b]);disp([a([1,3],:);b^2]=); disp([a([1,3],:);b^2]); t3: z=1:25;a=reshape(z,5,5);b=[3,0,16;17,-6,9;0,23,-4;9,7,0;4,13,11];1c=a*bd=c(3:5,2:3) t4-1:a=100:999;b=find(rem(a,21)==0); c=length(b) t4-2:a=input(请输入一个字符串:,s); b=find(a=aa=z); a(b)=[]; disp(a); 实验二: t1:e=eye(3),r=rand(3,2),o=zeros(2,3),s=diag([1,2]);a=[e,r;o,s] disp(a^2=); disp(a^2);disp([e,r+rs;o,s^2]); b=[e,r+r*s;o,s^2] t2: h=hilb(5) p=pascal(5) hh=det(h) hp=det(p) th=cond(h) tp=cond(p) a=abs(th-1);b=abs(tp-1); if abdisp(帕萨卡矩阵p性能更好); elseif abdisp(希尔伯特矩阵h性能更好); elsedisp(两个矩阵性能相同); end t3: a=1:25;a=reshape(a,5,5)disp(行列式的值:); disp(det(a));disp(矩阵的秩:); disp(rank(a));disp(矩阵的迹:);disp(trace(a));disp(矩阵的范数:); disp(norm(a)); t4:a=[-29,6,18;20,5,12;-8,8,5] [v,d]=eig(a) t5:a=[1/2,1/3,1/4;1/3,1/4,1/5;1/4,1/5,1/6] b=[0.95,0.67,0.52] x1=a\b b(3)=0.53 x2=a\bdisp(系数矩阵a的条件数:); disp(cond(a)); t6: a=1:25;a=reshape(a,5,5) disp(b1=sqrtm(a)); b1=sqrtm(a)disp(b2=sqrt(a)); b2=sqrt(a) disp(b1*b1); b1*b1disp(b2.*b2); b2.*b2实验三: t1:x=-5.0:2:5.0 for i=1:length(x);if (x(i)0)(x(i)~=-3)y(i)=x(i)^2+x(i)-6; elseif(x(i)=0)(x(i)5)(x(i)~=2)(x(i)~=3);y(i)=x(i)^2-5*x(i)+6; elsey(i)=x(i)^2-x(i)-1; end end y t2-if:s=input(please enter the score:); while (s0||s100)disp(the score is not reasonable); s=input(please enter the score:);endif (s=90s=100); disp(a);elseif(s=80s90); disp(b);elseif(s=70s80); disp(c);elseif(s=60s70); disp(d); elsedisp(e); endt2-switch:s=input(please enter the score:); while (s0||s100)disp(the score is not reasonable); s=input(please enter the score:); endswitch fix(s/10) case{9,10} disp(a); case{8} disp(b); case{7} disp(c); case{6} disp(d);case{0,1,2,3,4,5} disp(e); end t3:t=input(请输入工时time=); if t120w=120*84+(t-120)*1.15*84; elseif t60w=t*84-700; elsew=84*t; enddisp(应发工资为:); disp(w); t4:a=10+floor(rand(1)*89) b=10+floor(rand(1)*89)2s=input(请输入+或-或*或/,s);while(s~=+s~=-s~=*s~=/) disp(输入的符号错误,请重新输入);s=input(请输入+或-或*或/,s); end switch scase{+}c=a+b; case{-}c=a-b; case{*}c=a*b; case{/}c=a/b; end c t5:a=rand(5,6)n=input(请输入n:); while (n1)disp(输入的n有误,请重新输入); n=input(请输入n:); end if n5 b=a(5,:) elseb=a(n,:) end实验四: t1:n=input(请输入n:); m=0; for i=1:n;m=m+1/i^2; endpii=sqrt(6*m) t1-2:n=input(请输入n:); m=1./(1:n).^2; s=sum(m); pii=sqrt(6*s) t2: n=1000;y=0;for i=1:n;y=y+1/(2*i-1); if y=3disp(最大n值为:);i-1disp(对应的y值为:);y-1/(2*i-1)break; end end t3:format longa=input(请输入a:); b=input(请输入b:);x(1)=a/(1.0+b);i=1;x(2)=a/(x(i)+b); while (abs(x(i+1)-x(i))0.00001i500) i=i+1;x(i+1)=a/(x(i)+b); endr1=(-b+sqrt(b^2+4*a))/2; r2=(-b-sqrt(b^2+4*a))/2;disp([x,num2str(i+1),=,num2str(x(i+1),8)]);disp([x,num2str(i),=,num2str(x(i),8)]);disp([r1=,num2str(r1,8),,r2=,num2str(r2,8)]);format short t4:f(1)=1;f(2)=0;f(3)=1;i=4; while(i=100)f(i)=f(i-1)-2*f(i-2)+f(i-3); i=i+1; endfmax=max(f) fmin=min(f) fsum=sum(f)fp=length(find(f0)) fn=length(find(f0)) f0=length(find(f==0)) t5: j=0;s=0; for i=3:50m=i*(i-1)-1; if isprime(m)==1m3s=s+m;j=j+1; end enddisp([亲密数的对数:,num2str(j)]);disp([所有亲密素数之和:,num2str(s)]); 实验五: fn1:function f=fn1(n) f=n+10*log(n.^2+5); fn2:function f=fn2(n) f=0; for i=1:nf=f+i*(i+1); end fx:function f=fx(x)f=1./((x-2).^2+0.1)+1./((x-3).^4+0.01); t1:x=input(请输入一个复数:); fe=exp(x) flg=log(x) fsin=sin(x)fcos=cos(x) t2:m1=input(请输入m1:); m2=input(请输入m2:); t=input(请输入theta:);a=[m1*cos(t),-m1,-sin(t),0;m1*sin(t),0,... cos(t),0;0,m2,-sin(t),0;0,0,-cos(t),1] b=[0,m1*9.8,0,m2*9.8] disp([a1,a2,n1,n2]=); disp([a\b]); t3:j=1;n=1;k=1;for i=10:99% 挑选出10~99中所有的素数if isprime(i)==1 m(j)=i; j=j+1; end endfor t=1:length(m);4s(n)=10*rem(m(t),10)+fix(m(t)/10); %挑选出的素数进行位置交换 if isprime(s(n))==1 %再挑选出交换绝对素数r(k)=m(t);k=k+1; endn=n+1; enddisp([所有两位绝对素数:,num2str(r)]) t5:y1=fn1(40)/(fn1(30)+fn1(20)) %函数调用y2=fn2(40)/(fn2(30)+fn2(20)) 实验六: t1:x=linspace(0,2*pi,101);y=(0.5+3*sin(x)./(1+x.^2)).*cos(x); plot(x,y); t2:x=linspace(0,2*pi,100); y1=x.^2; y2=cos(2*x); y3=y1.*y2; subplot(2,2,1);plot(x,y1,r:,x,y2,y-,x,y3,b-.); subplot(2,2,2); plot(x,y1,r:); subplot(2,2,3); plot(x,y2,y-); subplot(2,2,4); plot(x,y3,b-.); t2-3: x=linspace(0,2*pi,10); y1=x.^2; y2=cos(2*x); y3=y1.*y2; subplot(3,4,1);bar(x,y1,r);axis([0,7,0,40]); subplot(3,4,2);stairs(x,y1,y);axis([0,7,0,40]); subplot(3,4,3);stem(x,y1,b);axis([0,7,0,40]); subplot(3,4,4);fill(x,y1,g);axis([0,7,0,40]);subplot(3,4,5);bar(x,y2,r);axis([-1,7,-1,1]); subplot(3,4,6);stairs(x,y2,y);axis([-1,7,-1,1]); subplot(3,4,7);stem(x,y2,b);axis([-1,7,-1,1]); subplot(3,4,8);fill(x,y2,g);axis([-1,7,-1,1]); subplot(3,4,9);bar(x,y3,r);axis([0,7,-30,50]); subplot(3,4,10);stairs(x,y3,y);axis([0,7,-30,50]); subplot(3,4,11);stem(x,y3,b);axis([0,7,-30,50]); subplot(3,4,12);fill(x,y3,g);axis([0,7,-30,50]); t3:x=linspace(-5,5,100); y=[]; for x0=x; if x0=0;y=[y,(x0+sqrt(pi))/exp(2)]; elsey=[y,1/2*log(x0+sqrt(1+x0^2))]; end endsubplot(1,2,1); plot(x,y);for i=1:length(x) if x(i)=0y(i)=(x(i)+sqrt(pi))/exp(2); elsey(i)=1/2*log(x(i)+sqrt(1+x(i)^2)); end endsubplot(1,2,2); plot(x,y); t4:a=input(请输入a:); b=input(请输入b:); n=input(请输入n:);theta=0:0.001:2*pi; y=a.*sin(b+n.*theta); polar(theta,y); t5:x=linspace(-5,5,21); y=linspace(0,10,31); [x,y]=meshgrid(x,y); z=cos(x).*cos(y).*exp(-sqrt(x.^2+y.^2)/4); subplot(1,2,1);surf(x,y,z); subplot(1,2,2); contour3(x,y,z); t6:s=0:pi/2; t=0:3*pi/2;[s,t]=meshgrid(s,t); x=cos(s).*cos(t); y=cos(s).*sin(t); z=sin(s); surf(x,y,z); shading interp; 实验八: t1:a=rand(1,30000); disp(均值为:); mean(a)disp(标准方差为:); std(a)disp(最大元素为:); max(a)disp(最小元素为:); min(a)k=find(a0.5); b=length(k);disp(大于0.5的随机数个数占总数的百分比:);sprintf(%f%%, b/300) t2:p=45+50*rand(100,5) disp(最高分及序号:); [y,u]=max(p,[],1)disp(最低分及序号:); [y1,u1]=min(p,[],1)disp(每门课平均分:);5【篇二:matlab程序设计与应用(刘卫国编)课后实验答案】xt>1. 先求下列表达式的值,然后显示matlab工作空间的使用情况并保存全部变量。

MATLAB程序设计与应用(刘卫国)》(第二版)-课后实验答案

MATLAB程序设计与应用(刘卫国)》(第二版)-课后实验答案

MATLAB程序设计与应⽤(刘卫国)》(第⼆版)-课后实验答案《MATLAB程序设计与应⽤(刘卫国)》(第⼆版)实验⼀MATLAB运算基础1.(1) z1=2*sin(pi*85/180)/(1+exp(2))(2)x=[2,1+2i;-0.45,5];z2=log(x+sqrt(1+x.^2))/2(3)a=-3.0:0.1:3.0;z3=0.5*(exp(0.3*a)-exp(-0.3*a)).*sin(a+0.3)+log((0.3+a)/2)(4)t=0:0.5:2.5;z4=(t>=0&t<1).*t.^2+(t>=1&t<2).*(t.^2-1)+(t>=2&t<3).*(t.^2-2*t+1)2. A=[12,34,-4;34,7,87;3,65,7];B=[1,3,-1;2,0,3;3,-2,7];(1)a=A+6*Bb=A-B+eye(size(A)) %I=eye(size(A))(2)c=A*Bd=A.*B(3)e=A^3f=A.^3(4)g=A/Bh=B\A(5)m=[A,B]n=[A([1,3],:);B^2]3. A=[1,2,3,4,5;6,7,8,9,10;11,12,13,14,15;16,17,18,19,20;21,22,23,24,25];B=[3,0,16;17,-6,9;0,23,-4;9,7,0;4,13,11];(1)C=A*B(2)D=C(3:end,2:end)4.(1) a=100:999;b=rem(a,21)==0;c=find(b);d=length(c)(2)ch='Just as Bianhaiman said,Xiehong is ...';e=find(ch>='A'&ch<='Z');ch(e)=[]实验⼆ MATLAB矩阵分析与处理R=rand(3,2);O=zeros(2,3);S=diag([1,2]);A=[E,R+R*S;O,S^2];a=A^22. H=hilb(5);P=pascal(5);Hh=det(H)Hp=det(P)Th=cond(H)Tp=cond(P)3. A=rand(5,5);a=det(A) %⾏列式的值b=trace(A) %⾏列式的迹c=rank(A) %⾏列式的秩d=norm(A) %⾏列式的范数4. A=[-29,6,18;20,5,12;-8,8,5];[V,D]=eig(A)5. A=[1/2,1/3,1/4;1/3,1/4,1/5;1/4,1/5,1/6]; b=[0.95,0.67,0.52]';(1)x=inv(A)*b(2)b1=[0.95,0.67,0.53]';x=inv(A)*b1(3)c=cond(A)6. A=rand(3,3);B=sqrtm(A)C=sqrt(A)实验三选择结构程序设计1. x=input('请输⼊x的值:');if x<0&x~=-3y=x*x+x-6;elseif x>=0&x<5&x~=2&x~=3y=x*x-5*x+6;elseendy2.(if语句)score=input('请输⼊成绩:');if score>=0&score<60disp('E');elseif score>=60&score<70disp('D');elseif score>=70&score<80disp('C');elseif score>=80&score<90disp('B');elseif score>=90&score<=100disp('A');elsedisp('您输⼊的成绩有误,请重新输⼊!'); end(switch语句)score=input('请输⼊成绩:');switch fix(score/10)case num2cell(0:5)disp('E');case {6}disp('D');case {7}disp('C');case {8}disp('B');case {9}disp('A');otherwisedisp('您输⼊的成绩有误,请重新输⼊!'); end 3. a=input('请输⼊⼯号:','s');b=input('请输⼊该员⼯的⼯时数:');y=120*84+(b-120)*(1+15/100)*84; elseif b<60y=b*84-700;elsey=b*84;endy4.a=10+fix(89*rand());b=10+fix(89*rand());c=input('请输⼊⼀个运算符:','s'); if c=='+' disp(a+b);elseif c=='-'disp(a-b);elseif c=='*'disp(a*b);elsedisp(a/b);end5. A=rand(5,6);n=1:5;n=input('请输⼊n的值:');tryB=A(n,:);catchB=A(end,:);endBlasterr实验四循环结构程序设计1.(循环结构)n=input('请输⼊n的值:');y=0;for i=1:ny=y+1/i/i;pi=sqrt(6*y);endpi(向量运算)n=input('请输⼊n的值:');i=1:n;y=1./i.^2;f=sum(y);pi=sqrt(f*6)2. y=0;n=0;while 1n=n+1;y=y+1/(2*n-1);if y>=3breakendendny=y-1/(2*n-1)3. a=input('输⼊a的值:');b=input('输⼊b的值:');x0=1.0;n=1;x1=a/(b+x0);while abs(x1-x0)>=10^(-5)|n<=500 x1=a/(b+x0); x0=x1;n=n+1;endx14. f(1)=1;f(2)=0;f(3)=1;for n=4:100f(n)=f(n-1)-2*f(n-2)+f(n-3); enda=max(f) %最⼤值b=min(f) %最⼩值c=sum(f) %各数之和d=length(find(f>0)) %正数的个数e=length(find(f==0)) %零的个数f=length(find(f<0)) %负数的个数5. n=0;s=0;for k=2:49sn=k*(k+1)-1;m=fix(sqrt(sn));for i=2:mif rem(sn,i)==0breakelseendendif i==mn=n+1;s=s+sn;endendns实验五函数⽂件1.(函数⽂件)function [e,l,s,c]=f1(x)e=exp(x);l=log10(x);s=sin(x);c=cos(x);(命令⽂件)x=input('请输⼊⼀个复数:'); [e,l,s,c]=f1(x);elsc2.(函数⽂件)function [x]=f2(A,B)C=B';x=C*inv(A);(命令⽂件)m1=input('Enter m1:');m2=input('Enter m2:');theta=input('Enter theta:'); g=9.8;A=[m1*cos(theta),-m1,-sin(theta),0;m1*sin(theta),0,cos(theta),0;0,m2,-sin(theta),0;0,0,-cos(theta),1]; B=[0,m1*g,0,m2*g]';[x]=f2(A,B);x3.(函数⽂件)function f=f3(x)f=1;for i=2:sqrt(x)if rem(x,i)==0f=0;breakendend(命令⽂件)for i=10:99j=10*rem(i,10)+fix(i/10);if f3(i)&f3(j)disp(i);endend4.(函数⽂件)function f=f4(x)f=1./((x-2).^2+0.1)+1./((x-3).^4+0.01);(命令⽂件)x=[1,2,3;4,5,6];y=f4(x)5.(1)(函数⽂件)function f=f5(n)f=n+10*log(n^2+5);(命令⽂件)y=f5(40)/(f5(30)+f5(20))(2)(函数⽂件)function f=f6(i)g=0;for i=1:nf=g+i.*(i+1);end(命令⽂件)y=f6(40)/(f6(30)+f6(20))实验六⾼层绘图操作1. x=linspace(0,2*pi,101);y=(0.5+3*sin(x)./(1+x.*x));plot(x,y);title('实验六第⼀题');xlabel('X');ylabel('Y');text(3,0.6,'y=0.5+3sin(x)/(1+x^2)');2.(1)x=linspace(0,pi,100);y1=x.^2;y2=cos(2*x);y3=y1.*y2;plot(x,y1,'b',x,y2,'g',x,y3,'r'); title('同⼀坐标系下得三条曲线'); text(2,4,'y1=x^2');text(2,-0,8,'y2=cos(2x)');text(2,-2.2,'y=x^2*cos(2x)');xlabel('X');ylabel('Y');(2)x=linspace(0,pi,100); y1=x.^2;y2=cos(2*x);y3=y1.*y2;subplot(1,3,1);plot(x,y1);title('y1=x^2');xlabel('X');ylabel('Y'); subplot(1,3,2);plot(x,y2);title('y2=cos(x)');xlabel('X');ylabel('Y') subplot(1,3,3);plot(x,y3);title('y3=x^2*cos(x)'); xlabel('X');ylabel('Y');(3)x=linspace(0,2*pi,20); y1=x.^2;y2=cos(2*x);y3=y1.*y2;subplot(4,3,1);bar(x,y1);subplot(4,3,2);bar(x,y2);subplot(4,3,3);bar(x,y3);subplot(4,3,4);stairs(x,y1);subplot(4,3,5);stairs(x,y2);subplot(4,3,6);stairs(x,y3);subplot(4,3,7);stem(x,y1);subplot(4,3,8);stem(x,y2);subplot(4,3,9);stem(x,y3);subplot(4,3,10);fill(x,y1,'b');subplot(4,3,11);fill(x,y2,'r');subplot(4,3,12);fill(x,y3,'g');3. x=linspace(-5,5,100);y=[];for x0=xif x0>=-5&x<=0y=[y,(x0+sqrt(pi))/exp(2)];elsey=[y,log(x0+sqrt(1+x0.^2))/2];endendplot(x,y);title('实验六第三题');xlabel('X');ylabel('Y');4. a=input('请输⼊a:');b=input('请输⼊b:');n=input('请输⼊n:');theta=0:0.01:2*pi;rho=a.*sin(b+n.*theta);polar(theta,rho,'r');title('极坐标曲线');(如:当输⼊a=3,b=4,n=6时,图形为:)5. x=linspace(-5,5,21);y=linspace(0,10,31);[x,y]=meshgrid(x,y);z=cos(x).*cos(y).*exp(-sqrt(x.*x+y.*y)/4); subplot(2,1,1); surf(x,y,z);title('曲⾯图');xlabel('X');ylabel('Y');zlabel('Z'); subplot(2,1,2);surfc(x,y,z);title('等⾼线图');xlabel('X');ylabel('Y');zlabel('Z');6.(⽅法⼀)s=0:pi/100:pi/2;t=0:pi/100:3*pi/2;[s,t]=meshgrid(s,t);x=cos(s).*cos(t);y=cos(s).*sin(t);z=sin(s);surf(x,y,z);shading interp;title('实验六第六题');xlabel('X');ylabel('Y');zlabel('Z');(⽅法⼆)ezsurf('cos(s)*cos(t)','cos(s)*sin(t)','sin(s)',[0,0.5*pi,0,1.5*pi]); shading interp;实验七低层绘图操作1. hf=figure('MenuBar','figure','NumberTitle','off','Color','r',...'Name','图形窗⼝⽰例',...'WindowButtonDownFcn','disp(''Left Button Pressed.'')');2. x=-1:0.1:1;y=x.^2.*exp(2*x);h=line('XData',x,'YData',y);text(0.6,0.36*exp(1.2),'\leftarrow y=x^2*exp(2*x)');set(h,'Color','r','LineStyle','--','LineWidth',2);grid on;title('函数y=x^2*exp(2*x)');3. x=0:0.00001:0.001;[x,t]=meshgrid(x);v=10*exp(-0.01*x).*sin(2000*pi*t-0.2*x+pi);axes('view',[-37.5,30]);hf=surface(x,t,v);grid on;xlabel('X');ylabel('Y');zlabel('Z');title('曲⾯v=10*exp(-0.01*x)*sin(2000*pi*t-0.2*x+pi)');set(hf,'EdgeColor','interp','FaceColor','r','LineStyle',':','LineWidth',2);4. x=linspace(0,2*pi,20);y1=sin(x);y3=sin(x)./(cos(x)+eps);y4=cos(x)./(sin(x)+eps);axes('Position',[0.15,0.1,0.2,0.4]);plot(x,y1);title('y1=sin(x)');axes('Position',[0.05,0.6,0.2,0.3]);plot(x,y2);title('y2=cos(x)');axes('Position',[0.45,0.6,0.3,0.3]);plot(x,y3);title('y3=tan(x)');axes('Position',[0.5,0.2,0.3,0.2]);plot(x,y4);title('y4=cot(x)');5. cylinder(3);light('Position',[1,2,4]); shading interp;lighting gouraud;material shiny实验⼋数据处理与多项式计算1.A=rand(1,30000);a=mean(A) %均值b=std(A,0,2) %标准⽅差c=max(A) %最⼤元素d=min(A) %最⼩元素n=0;for i=1:30000if A(i)>0.5n=n+1; %⼤于0.5的随机数的个数endendm=n/30000 %百分⽐2. P=45+fix(50*rand(100,5));[zgf,xh]=max(P) %每门课的最⾼分及相应的学号[zdf,xh]=min(P) %每门课的最低分及相应的学号pjf=mean(P) %每门课的平均分fc=std(P) %标准⽅差A=sum(P,2); %总分[zfzg,xh]=max(A) %总分最⾼分及学号[zfzd,xh]=min(A) %总分最低分及学号[zcj,xsxh]=sort(A,1,'descend')x=6.5:2:17.5;t1=[18.0 20.0 22.0 25.0 30.0 28.0 24.0]; t2=[15.0 19.0 24.0 28.0 34.0 32.0 30.0]; a=interp1(h,t1,x,'spline')b=interp1(h,t2,x,'spline')4. x=linspace(1,101,10);y=log10(x);p=polyfit(x,y,5);y1=polyval(p,x);plot(x,y,'r:o',x,y1,'-*')。

matlab基础与应用教程习题答案作者蔡旭晖刘卫国蔡立燕第1-8章答案

matlab基础与应用教程习题答案作者蔡旭晖刘卫国蔡立燕第1-8章答案

matlab基础与应用教程习题答案作者蔡旭晖刘卫国蔡立燕第1-8章答案第1章一、思考题4. (1) B=A(2:5,1:2:5)(2)A(7)=[](3) A=A+30(4)size(A)(5) t(find(t==0))=eps(6)t=reshape(x,3,4)(7)abs('matlab')(8)char(93)5. A=[97,67,34,10;-78,75,65,5;32,5,-23,-59;0,-12,54,7] (1) B=A(1:3,:)C=A(:,1:2)D=A(2:4,3:4)E=B*C(2)A(find(A>=50 & A<=100))二、实验题=-74/180*pi;y=-27/180*pi;sin(x*x+y*y)/sqrt(tan(abs(x+y)))+pi=::;exp*a).*sin(a+=[2,4;,5];log(x+sqrt(1+x.*x))/24. A=[3,54,2;34,-45,7;87,90,15];B=[1,-2,67;2,8,74;9,3,0];(1)A*Bans =129 432 41977 -407 -1052402 591 12489A.*Bans =3 -108 13468 -360 518783 270 0(2) A^3ans =-28917 240246 -4368137883 -259101 27669171333 252504 38673A.^3ans =27 157464 8 39304 -91125 343 658503 729000 3375(3)A/Bans =B/Aans =(4)[A,B]ans =3 54 2 1 -2 67 34 -45 7 2 8 74 87 90 15 9 3 0 [A([1,3],:);B^2]ans =3 54 2 87 90 15 600 183 -81 684 282 726 15 6 825 =1+2i;b=3+4i;c=exp(pi*i/6)c =+c+a*b/(a+b)ans =+第2章一、思考题=0;for n=0:63s=s+2^n;enddisp(s)n=0:63;s=sum(2.^n)二、实验题1.x=input('输入一个四位整数:');y=[fix(x/1000),mod(fix(x/100),10),mod(fix(x/10),10),mod(x,1 0) ]z=mod((y+7),10)x=z(3)*1000+z(4)*100+z(1)*10+z(2)2.gh=input('输入工号');h=input('输入工时');dj=84;if h>120gz=dj*120+*dj*(h-120);elseif h<60gz=dj*h-700;elsegz=dj*h;endformat bank;display([gh,gz])3.循环结构n=input('input n:');s=0;for k=1:ns=s+1/k^2;enddisplay(sqrt(s*6))向量运算n=input('input n:');k=1:n;display(sqrt(sum(1./k.^2)*6)) 4.y=0;k=0;while y<3k=k+1;y=y+1/(2*k-1);enddisplay([k-1,y-1/(2*k-1)])5.x0=0;x=1;k=0;a=input('a=');b=input('b=');while abs(x-x0)>=1e-5 && k<500 x0=x;x=a/(b+x0);k=k+1;enddisplay([k,x]);display([(-b+sqrt(b^2+4*a))/2,(-b-sqrt(b^2+4*a))/2]); 6.y=fun(40)/(fun(30)+fun(20))(1)函数文件function f=fun(n)f=n+log(n^2+5);(2)函数文件function f=fun(n)a=1:n;f=sum(a.*(a+1));第3章一、思考题4.t=0::;y=sqrt(3)/2*exp(-4*t).*sin(4*sqrt(3)*t+pi/3);5.x=-10::10;y=linspace(-6,6,size(x,2))z=x.^3+3*x.*y.^2;plot3(x,y,z)6.x=100:100:400;y=100:100:400;z=[636,697,624,478;698,712,630,478;680,674,598,412;662,62 6,55 2,334];[X,Y]=meshgrid(x,y);mesh(X,Y,z)二、实验题1.(1)x=-10::10;plot(x,x-x.^3/3/2)(2)plot(x,exp(-x.*x/2)/2/pi)(3)x=-8::8;plot(x,sqrt((64-x.*x)/2))(4)t=0:pi/100:6*pi;plot(t.*sin(t),t.*cos(t))2. (1)x1=linspace(0,1,100);y1=2*;t=linspace(0,pi,100);x=sin(3*t).*cos(t);y=sin(3*t).*sin(t); plot(x1,y1,'r-',x,y,'b:'); text,1,'y=');text,,'x=sin(3t)cos(t)');text,,'y=sin(3t)sin(t)');(2)subplot(1,2,1);scatter(x1,y1,10)title('y=');subplot(1,2,2);scatter(x,y,10)3.subplot(1,21);x=1:1:100; y=sin(1./x); plot(x,y)subplot(1,2,2);fplot('sin(1/x)',[1,100]) 4.subplot(2,2,1);bar(t,y);subplot(2,2,2);stairs(t,y);subplot(2,2,3);stem(t,y)subplot(2,2,4);semilogy(t,y);5.theta=linspace(-pi,pi,100); ro=5.*cos(theta)+4;polar(theta,ro);fi=linspace(0,2*pi,100);a=1r=a.*(1+cos(fi));polar(fi,r);6.t=0:pi/20:2*pi;x=exp(-t./20).*cos(t);y=exp(-t./20).*sin(t);z=t; plot3(x,y,z);t=0::1;x=t;y=t.^2;z=t.^3;plot3(x,y,z);7.x=-30::0;y=0::30;[x,y]=meshgrid(x,y);z=10.*sin(sqrt(x.^2+y.^2))./sqrt(1+x.^2+y.^2); meshc(x,y,z);8.x=linspace(-3,3,100);y=linspace(-3,3,100);[x y]=meshgrid(x,y);fxy=-5./(1+x.^2+y.^2);i=find(abs(x)<= & abs(y)<=;fxy(i)=NaN;surf(x,y,fxy)9.u=linspace(1,10,100);v=linspace(-pi,pi,100);[u,v]=meshgrid(u,v);x=3.*u.*sin(v);y=2.*u.*cos(v);z=4*u.^2;x=3*u.*sin(v);y=2*u.*cos(v);z=4*u.^2;surf(x,y,z);shading interp;light('position',[1,0,1]);10.t=0:pi/100:2*pi;y=sin(t);comet(t,y)第4章一、思考题5.(1)A=eye(3);(2)C=100+(200-100)*rand(5,6);(3)D=1+sqrt*randn(1,500);(4)E=ones(size(A));(5)A=A+30*eye(size(A));(6)B=diag(diag(A))二、实验题1.P=pascal(5);H=hilb(5);Dp=det(P);Dh=det(H);Kp=cond(P);Kh=cond(H);P矩阵的性能更好,因为Kp较小2.A=[1,-1,2,3;0,9,3,3;7,-5,0,2;23,6,8,3]B=[3,pi/2,45;32,-76,sqrt(37);5,72,;exp(2),0,97] A1=diag(A);B1=diag(B);A2=triu(A);B2=triu(B);A3=tril(A);B3=tril(B);rA=rank(A);rB=rank(B);nA=norm(A);nb=norm(B);cA=cond(A);cB=cond(B);3.A=[31,1,0;-4,-1,0;4,-8,-2];[V,D]=eig(A);A=diag([-1,-1,-1,-1],-1)+diag([-1,-1,-1,-1],1)+diag([2,2,2,2, 2]) b=[1,0,0,0,0]';x1=inv(A)*b;x2=A\b;[L,U]=lu(A);x3=U\(L\b);[Q,R]=qr(a);[Q,R]=qr(A);x4=R\(Q\b)R=chol(A);x5=R\(R'\b)5.B=sparse(A);x1=inv(B)*b;x2=B\b;[L,U]=lu(B);x3=U\(L\b);第5章一、思考题A=randn(10,5);mean(A)std(A)max(max(A))min(min(A))sum(A,2)sum(sum(A))sort(A,1)sort(A,2,'descend')二、实验题1.A=rand(1,30000);mean(A)std(A)max(A)min(A)size(find(A>)/size(A)2.h=[466,715,950,1422,1635];w=[,,,,];hh=[500,900,1500];ww=interp1(h,w,hh,'spline')3.x=linspace(1,10,50);y=log(x);f=polyfit(x,y,5);yy=polyval(f,x);plot(x,y,'r-',x,yy,'g.')4.N=64; % 采样点数T=5; % 采样时间终点t=linspace(0,T,N); % 给出N个采样时间ti(I=1:N) x=3*exp(-t); % 求各采样点样本值xdt=t(2)-t(1); % 采样周期f=1/dt; % 采样频率(Hz)X=fft(x); % 计算x的快速傅立叶变换X F=X(1:N/2+1); %F(k)=X(k)(k=1:N/2+1)f=f*(0:N/2)/N; % 使频率轴f从零开始plot(f,abs(F),'-*') % 绘制振幅-频率图xlabel('Frequency');ylabel('|F(k)|')5.(1)p1=[1 2 0 0 7];p2=[1 -2];p3=[1 0 0 5 1];p12=conv(p1,p2);p=p12+[zeros(1,size(p12,2)-size(p3,2)),p3];roots(p)(2)A=[-1,4,3;2,1,5;0,5,6];Px=polyval(p,A)Pxm=polyvalm(p,A)6.(1) z=fzero('3*x-sin(x)+1',0)(2)建立函数文件function F=myfun(X)x=X(1);y=X(2);F(1)=x*x+y*y-9;F(2)=x+y-1;在命令窗口中输入以下命令:x=fsolve(@myfun,[3,0]',optimset('Display','off'))一、思考题2.fx=inline('1./(1+x.^2)');[I,n]=quad(fx,-100000,100000,1e-3); [I,n]=quadl(fx,-100000,100000,1e-3); x=-100000::100000;y=1./(1+x.*x);trapz(x,y);3.(1) fx=inline('-2*y+2*x*x+2*x');[t,y]=ode23(fx,[0,],1)(2) fx=inline('y-exp(x)*cos(x)'); [t,y]=ode23(fx,[0,3],1)二、实验题1.for x=1:3fx=[x,x^2,x^3;1,2*x,3*x;0,2,6*x]; diff(fx)2.(1)x=0::1;y=x.^10+10.^x+1./log10(x);dy=diff(y)/;(2)x=0::1;y=log(1+x);dy=diff(y,2)/;plot(x(1:99),dy)3.(1)fx=inline('x.^2.*sqrt(2*x.*x+3)'); quad(fx,1,5) (2)fx=inline('x./sin(x).^2');quad(fx,pi/4,pi/3)(3)fx=inline('abs(cos(x+y))'); dblquad(fx,0,pi,0,pi) (4)syms x y;fx=x*y;int(int(fx,y^2,y+2),-1,2)x的积分区间为【0,2】时。

matlab基础与应用教程课后答案

matlab基础与应用教程课后答案

matlab基础与应用教程课后答案matlab基础与应用教程课后答案【篇一:matlab教程基本应用练习题及解答】txt>要求:将每题的答案(命令行和运行结果、图片或m文件的文件名及具体内容)直接拷贝插入到各题的下方:(1)若为命令行,要求将提示符“”一起拷入,并在右侧用“%”注明命令行的每条命令的作用;(2)若为多个运行结果,拷入后要求解释每个结果具体对应题目中的哪个要求,也在右侧用“%”注明;(3)如果为m 文件,除了将文件名和此文件的具体内容全部拷入外,再将所有原始m文件和本练习题电子版放在一个以“专业班级+本人姓名+学号”命名的文件夹内一起上传。

ans =1 0 00 1 00 0 1ans =0 0 00 0 0ans =1 1 11 1 11 1 11 1 120+40*rand(1,10) %生成10个在区间[20,60]上均匀分布的随机数。

ans =44.617351.677556.872549.528327.050636.228257.418856.6 76236 .410855.7460二.1)计算向量(2,4,6,8)的最大值,最小值,平均值,中值,排序,总和值; max([2,4,6,8])%最大值ans =8min([2,4,6,8])% 最小值ans =2mean([2,4,6,8])% 平均值ans =5median([2,4,6,8])% 中值ans =5sort([2,4,6,8])% 排序ans =2 4 6 8sum([2,4,6,8])% 总和值ans =202)在行向量(2,4,6,8)和(1,3,5,7)之间实施加减乘除及幂运算; a=[2 4 6 8];b=[1 3 5 7];a+b%加ans =3 7 11 15a-b%减ans =a.*b%乘ans =2 12 30 56a./b%除ans =2.0000 1.3333 1.2000 1.1429a.^b%幂ans =2 64 7776 20971523)在向量(1,3,5)与标量2之间实施加减乘除及幂运算。

Matlab程序设计与应用(刘卫国 第二版)答案

Matlab程序设计与应用(刘卫国 第二版)答案

%y1 蓝色实线,y2 红色虚线,y3 绿色点画线
%分四个子图(先画 2 行 2 列第 1 块)
%y1 的四种图形
%y2 %条形图 %阶梯图 %杆图 %填充图,注意必须加填充颜色 %y3
subplot(3,4,11); stem(x,y3); subplot(3,4,12); fill(x,y3,'b'); 3 clear; x=-5:0.1:5; if x<=0 y=(x+sqrt(pi)/exp(2)); else y=0.5.*log(x+sqrt(1+x.^2)); end plot(x,y); 4 M 文件,假设文件名为 Untitled6 a=input('a='); b=input('b='); %b 单位为 pi/4 b=b*pi/4; n=input('n='); q=linspace(-2*pi,2*pi,100); p=a*sin(b+n*q); plot(q,p); hold on; %保持图形 命令窗口调用情况 >> Untitled6 a=1 b=1 n=1 >> Untitled6 a=2 b=2 n=2
n=input('n='); m=1:n; y=(6*sum(1./m.^2))^(1/2) 2:y=0; k=0; while 1 k=k+1; y=y+1/(2*k-1); if y>=3 break end end format long y=y-1/(2*k-1) k=k-1 3:a=input('a='); b=input('b='); n=1; x0=1; x1=a/(x0+b); while abs(x0-x1)>=10^(-5)|n<=500 k=a/(x1+b); x0=x1; x1=k; n=n+1; end x1 4:f(1)=1; f(2)=0; f(3)=1; for n=4:100 f(n)=f(n-1)-2*f(n-2)+f(n-3); end zuidazhi=max(f) zuixiaozhi=min(f) sn=sum(f) a=find(f>0); b=find(f<0); c=find(f==0); zhengshu=length(a) fushu=length(b) ling=length(c)

MATLAB基础与应用教程 习题答案 作者 蔡旭晖 刘卫国 蔡立燕 第1-8章答案

MATLAB基础与应用教程  习题答案 作者 蔡旭晖 刘卫国 蔡立燕 第1-8章答案

第1章一、思考题4. (1) B=A(2:5,1:2:5)(2)A(7)=[](3) A=A+30(4)size(A)(5) t(find(t==0))=eps(6)t=reshape(x,3,4)(7)abs('matlab')(8)char(93)5. A=[97,67,34,10;-78,75,65,5;32,5,-23,-59;0,-12,54,7](1) B=A(1:3,:)C=A(:,1:2)D=A(2:4,3:4)E=B*C(2)A(find(A>=50 & A<=100))二、实验题1.x=-74/180*pi;y=-27/180*pi;sin(x*x+y*y)/sqrt(tan(abs(x+y)))+pi2.a=-3.0:0.1:3.0;exp(-0.3*a).*sin(a+0.3)3.x=[2,4;-0.45,5];log(x+sqrt(1+x.*x))/24. A=[3,54,2;34,-45,7;87,90,15];B=[1,-2,67;2,8,74;9,3,0]; (1)A*Bans =129 432 41977 -407 -1052402 591 12489A.*Bans =3 -108 13468 -360 518783 270 0(2) A^3ans =-28917 240246 -4368137883 -259101 27669171333 252504 38673A.^3ans =27 157464 839304 -91125 343658503 729000 3375(3)A/Bans =-5.8845 5.3549 -0.20286.3554 -5.6596 4.3293-6.6325 6.2078 9.0241B/Aans =30.2855 19.2643 -8.561433.4394 21.1547 -9.3974-0.7443 -0.3938 0.2830(4)[A,B]ans =3 54 2 1 -2 6734 -45 7 2 8 7487 90 15 9 3 0[A([1,3],:);B^2]ans =3 54 287 90 15600 183 -81684 282 72615 6 8255.a=1+2i;b=3+4i;c=exp(pi*i/6)c =0.8660 + 0.5000ic+a*b/(a+b)ans =1.6353 + 1.8462i第2章一、思考题3.s=0;for n=0:63s=s+2^n;enddisp(s)n=0:63;s=sum(2.^n)二、实验题1.x=input('输入一个四位整数:');y=[fix(x/1000),mod(fix(x/100),10),mod(fix(x/10),10),mod(x,10)] z=mod((y+7),10)x=z(3)*1000+z(4)*100+z(1)*10+z(2)2.gh=input('输入工号');h=input('输入工时');dj=84;if h>120gz=dj*120+1.15*dj*(h-120);elseif h<60gz=dj*h-700;elsegz=dj*h;endformat bank;display([gh,gz])3.循环结构n=input('input n:');s=0;for k=1:ns=s+1/k^2;enddisplay(sqrt(s*6))向量运算n=input('input n:');k=1:n;display(sqrt(sum(1./k.^2)*6))4.y=0;k=0;while y<3k=k+1;y=y+1/(2*k-1);enddisplay([k-1,y-1/(2*k-1)])5.x0=0;x=1;k=0;a=input('a=');b=input('b=');while abs(x-x0)>=1e-5 && k<500x0=x;x=a/(b+x0);k=k+1;enddisplay([k,x]);display([(-b+sqrt(b^2+4*a))/2,(-b-sqrt(b^2+4*a))/2]);6.y=fun(40)/(fun(30)+fun(20))(1)函数文件fun.mfunction f=fun(n)f=n+log(n^2+5);(2)函数文件fun.mfunction f=fun(n)a=1:n;f=sum(a.*(a+1));第3章一、思考题4.t=0:0.01:1.5;y=sqrt(3)/2*exp(-4*t).*sin(4*sqrt(3)*t+pi/3);5.x=-10:0.01:10;y=linspace(-6,6,size(x,2))z=x.^3+3*x.*y.^2;plot3(x,y,z)6.x=100:100:400;y=100:100:400;z=[636,697,624,478;698,712,630,478;680,674,598,412;662,626,552,334]; [X,Y]=meshgrid(x,y);mesh(X,Y,z)二、实验题1.(1)x=-10:0.01:10;plot(x,x-x.^3/3/2)(2)plot(x,exp(-x.*x/2)/2/pi)(3)x=-8:0.01:8;plot(x,sqrt((64-x.*x)/2))(4)t=0:pi/100:6*pi;plot(t.*sin(t),t.*cos(t))2. (1)x1=linspace(0,1,100);y1=2*x1-0.5;t=linspace(0,pi,100);x=sin(3*t).*cos(t);y=sin(3*t).*sin(t);plot(x1,y1,'r-',x,y,'b:');text(0.4,1,'y=2x-0.5');text(-0.4,0.5,'x=sin(3t)cos(t)');text(-0.4,0.3,'y=sin(3t)sin(t)');(2)subplot(1,2,1);scatter(x1,y1,10)title('y=2x-0.5');subplot(1,2,2);scatter(x,y,10)3.subplot(1,21);x=1:1:100; y=sin(1./x);plot(x,y)subplot(1,2,2);fplot('sin(1/x)',[1,100])4.subplot(2,2,1);bar(t,y);subplot(2,2,2);stairs(t,y);subplot(2,2,3);stem(t,y)subplot(2,2,4);semilogy(t,y);5.theta=linspace(-pi,pi,100);ro=5.*cos(theta)+4;polar(theta,ro);fi=linspace(0,2*pi,100);a=1r=a.*(1+cos(fi));polar(fi,r);6.t=0:pi/20:2*pi;x=exp(-t./20).*cos(t);y=exp(-t./20).*sin(t);z=t; plot3(x,y,z);t=0:0.01:1;x=t;y=t.^2;z=t.^3;plot3(x,y,z);7.x=-30:0.1:0;y=0:0.1:30;[x,y]=meshgrid(x,y);z=10.*sin(sqrt(x.^2+y.^2))./sqrt(1+x.^2+y.^2); meshc(x,y,z);8.x=linspace(-3,3,100);y=linspace(-3,3,100);[x y]=meshgrid(x,y);fxy=-5./(1+x.^2+y.^2);i=find(abs(x)<=0.8 & abs(y)<=0.5);fxy(i)=NaN;surf(x,y,fxy)9.u=linspace(1,10,100);v=linspace(-pi,pi,100);[u,v]=meshgrid(u,v);x=3.*u.*sin(v);y=2.*u.*cos(v);z=4*u.^2;x=3*u.*sin(v);y=2*u.*cos(v);z=4*u.^2;surf(x,y,z);shading interp;light('position',[1,0,1]);10.t=0:pi/100:2*pi;y=sin(t);comet(t,y)第4章一、思考题5.(1)A=eye(3);(2)C=100+(200-100)*rand(5,6);(3)D=1+sqrt(0.2)*randn(1,500);(4)E=ones(size(A));(5)A=A+30*eye(size(A));(6)B=diag(diag(A))二、实验题1.P=pascal(5);H=hilb(5);Dp=det(P);Dh=det(H);Kp=cond(P);Kh=cond(H);P矩阵的性能更好,因为Kp较小2.A=[1,-1,2,3;0,9,3,3;7,-5,0,2;23,6,8,3]B=[3,pi/2,45;32,-76,sqrt(37);5,72,4.5e-4;exp(2),0,97]A1=diag(A);B1=diag(B);A2=triu(A);B2=triu(B);A3=tril(A);B3=tril(B);rA=rank(A);rB=rank(B);nA=norm(A);nb=norm(B);cA=cond(A);cB=cond(B);3.A=[31,1,0;-4,-1,0;4,-8,-2];[V,D]=eig(A);4.A=diag([-1,-1,-1,-1],-1)+diag([-1,-1,-1,-1],1)+diag([2,2,2,2,2]) b=[1,0,0,0,0]';x1=inv(A)*b;x2=A\b;[L,U]=lu(A);x3=U\(L\b);[Q,R]=qr(a);[Q,R]=qr(A);x4=R\(Q\b)R=chol(A);x5=R\(R'\b)5.B=sparse(A);x1=inv(B)*b;x2=B\b;[L,U]=lu(B);x3=U\(L\b);第5章一、思考题3.A=randn(10,5);mean(A)std(A)max(max(A))min(min(A))sum(A,2)sum(sum(A))sort(A,1)sort(A,2,'descend')二、实验题1.A=rand(1,30000);mean(A)std(A)max(A)min(A)size(find(A>0.5))/size(A)2.h=[466,715,950,1422,1635];w=[7.04,4.28,3.40,2.52,2.13];hh=[500,900,1500];ww=interp1(h,w,hh,'spline')3.x=linspace(1,10,50);y=log(x);f=polyfit(x,y,5);yy=polyval(f,x);plot(x,y,'r-',x,yy,'g.')4.N=64; % 采样点数T=5; % 采样时间终点t=linspace(0,T,N); % 给出N个采样时间ti(I=1:N) x=3*exp(-t); % 求各采样点样本值xdt=t(2)-t(1); % 采样周期f=1/dt; % 采样频率(Hz)X=fft(x); % 计算x的快速傅立叶变换XF=X(1:N/2+1); % F(k)=X(k)(k=1:N/2+1) f=f*(0:N/2)/N; % 使频率轴f从零开始plot(f,abs(F),'-*') % 绘制振幅-频率图xlabel('Frequency');ylabel('|F(k)|')5.(1)p1=[1 2 0 0 7];p2=[1 -2];p3=[1 0 0 5 1];p12=conv(p1,p2);p=p12+[zeros(1,size(p12,2)-size(p3,2)),p3];roots(p)(2)A=[-1,4,3;2,1,5;0,5,6];Px=polyval(p,A)Pxm=polyvalm(p,A)6.(1)z=fzero('3*x-sin(x)+1',0)(2)建立函数文件myfun.mfunction F=myfun(X)x=X(1);y=X(2);F(1)=x*x+y*y-9;F(2)=x+y-1;在命令窗口中输入以下命令:x=fsolve(@myfun,[3,0]',optimset('Display','off')) 第6章一、思考题2.fx=inline('1./(1+x.^2)');[I,n]=quad(fx,-100000,100000,1e-3);[I,n]=quadl(fx,-100000,100000,1e-3);x=-100000:0.01:100000;y=1./(1+x.*x);trapz(x,y);3.(1)fx=inline('-2*y+2*x*x+2*x');[t,y]=ode23(fx,[0,0.5],1)(2)fx=inline('y-exp(x)*cos(x)');[t,y]=ode23(fx,[0,3],1)二、实验题1.for x=1:3fx=[x,x^2,x^3;1,2*x,3*x;0,2,6*x];diff(fx)end2.(1)x=0:0.01:1;y=x.^10+10.^x+1./log10(x);dy=diff(y)/0.01;(2)x=0:0.01:1;y=log(1+x);dy=diff(y,2)/0.01;plot(x(1:99),dy)3.(1)fx=inline('x.^2.*sqrt(2*x.*x+3)');quad(fx,1,5)(2)fx=inline('x./sin(x).^2');quad(fx,pi/4,pi/3)(3)fx=inline('abs(cos(x+y))');dblquad(fx,0,pi,0,pi)(4)syms x y ;fx=x*y;int(int(fx,y^2,y+2),-1,2)x 的积分区间为【0,2】时fx=inline('x.*y');dblquad(fx,0,2,-1,2)4. x=0.3:0.2:1.5;y=[0.3895,0.6598,0.9147,1.1611,1.3971,1.6212,1.8325]; trapz(x,y)5.(1)yp=inline('-(1.2+sin(10*x))*y');[t,y]=ode23(yp,[0,5],1);(2)令''3,,21y x y x y x ='==,则可写出原方程的状态方程形式: 123233221sin 31)1(2cos 5cos x t x x t t t x x x x x +--++='='='⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡+⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡⎥⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎢⎣⎡+-+-=⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡'''t x x x t t tx x x cos 00)1(2cos 51sin 311000103212321 建立函数文件ztfun.m function y=ztfun(t,x)b=[0;0;cos(t)];y=[0,1,0;0,0,1;-1/(3+sin(t)),-1,5*cos(2*t)/(t+1)^2]*x+b; 解微分方程组[t,y]=ode23(@ztfun,[0,5],[1;0;2]);6.建立函数文件ztfun.mfunction yy=ztfun(t,y) yy=[y(2)*y(3);-y(1)*y(3);-0.51*y(1)*y(2)];解微分方程组 [t,y]=ode23(@ztfun,[0,5],[0;1;1])第7章一、思考题3.(1)数值积分fx=inline('exp(x).*(1+exp(x)).^2');quad(fx,0,log(2))符号积分f=sym('exp(x)*(1+exp(x))^2');v=int(f,0,log(2));eval(v)(2)略二、实验题1.A=sym('[1,2,3;x,y,z;3,2,1]')rank(A)inv(A)det(A)2.(1) y=sym('sqrt(x+sqrt(x+sqrt(x)))');y1=diff(y)y2=diff(y,'x',2)(2) syms x y;fxy=sin(x^2*y)*exp(-x^2-y);diff(diff(fxy,x),y)3.(1) syms xint(1/(1+x^4))(2) syms x tint((-2*x*x+1)/(2*x*x-3*x+1)^2,x,cos(t),exp(2*t)) 4.syms n xsymsum(1/(2*n+1)/(2*x+1)^(2*n+1),n,0,inf)symsum(1/(2*n+1)/(2*x+1)^(2*n+1),n,0,5)5.(1) syms xtaylor((exp(x)+exp(-x))/2,5,0)(2) syms a xtaylor(exp(-5*x)*sin(3*x+pi/3),5,a)6.(1)x=solve(sym('x^3+a*x+1=0'))(2)[x y]=solve('sqrt(x^2+y^2)-100=0,3*x+5*y-8=0')7. 方程转化为: π21'11't y y y y -=+=⎩⎨⎧符号解[y1,y11]=dsolve('Dy=y1,Dy1+y=1-t^2/pi','y(-2)=5,y1(-2)=5','t') 数值解编写函数文件ztfun.mfunction yy=ztfun(t,y)yy=[y(2);1-t^2/pi-y(1)];在命令窗口输入以下命令[t,y]=ode45(@ztfun,[-2,7],[-5;5]);t=linspace(-2,7,49)y2=y8.[x,y]=dsolve('Dx=3*x+4*y,Dy=-4*x+3*y','x(0)=0,y(0)=1')。

MATLAB程序设计与应用(刘卫国编)课后实验答案之欧阳语创编

MATLAB程序设计与应用(刘卫国编)课后实验答案之欧阳语创编

实验一MATLAB运算基础1. 先求下列表达式的值,然后显示MATLAB工作空间的使用情况并保存全部变量。

(1)0 122sin851ze =+(2)21ln( 2z x=,其中2120.455i x+⎡⎤=⎢⎥-⎣⎦(3)0.30.330.3sin(0.3)ln, 3.0, 2.9,,2.9,3.0 22a ae e az a a--+=++=--(4)2242011122123t tz t tt t t⎧≤<⎪=-≤<⎨⎪-+≤<⎩,其中t=0:0.5:2.5解:M文件:z1=2*sin(85*pi/180)/(1+exp(2))x=[2 1+2*i;-.45 5];z2=1/2*log(x+sqrt(1+x^2))a=-3.0:0.1:3.0;z3=(exp(0.3.*a)-exp(-0.3.*a))./2.*sin(a+0.3)+log((0.3+a)./2)t=0:0.5:2.5;z4=(t>=0&t<1).*(t.^2)+(t>=1&t<2).*(t.^2-1)+(t>=2&t<3) .*(t.^2-2*t+1) 运算结果:z1=2*sin(85*pi/180)/(1+exp(2))x=[2 1+2*i;-.45 5];z2=1/2*log(x+sqrt(1+x^2))a=-3.0:0.1:3.0;z3=(exp(0.3.*a)-exp(-0.3.*a))./2.*sin(a+0.3)+log((0.3+a)./2)t=0:0.5:2.5;z4=(t>=0&t<1).*(t.^2)+(t>=1&t<2).*(t.^2-1)+(t>=2&t<3) .*(t.^2-2*t+1)z1 =0.2375z2 =0.7114 - 0.0253i 0.8968 + 0.3658i0.2139 + 0.9343i 1.1541 - 0.0044iz3 =Columns 1 through 40.7388 + 3.1416i 0.7696 + 3.1416i 0.7871 + 3.1416i 0.7913 + 3.1416iColumns 5 through 80.7822 + 3.1416i 0.7602 + 3.1416i 0.7254 + 3.1416i 0.6784 + 3.1416iColumns 9 through 120.6196 + 3.1416i 0.5496 + 3.1416i 0.4688 + 3.1416i 0.3780 + 3.1416iColumns 13 through 160.2775 + 3.1416i 0.1680 + 3.1416i 0.0497 + 3.1416i -0.0771 + 3.1416iColumns 17 through 20-0.2124 + 3.1416i -0.3566 + 3.1416i -0.5104 + 3.1416i -0.6752 + 3.1416iColumns 21 through 24-0.8536 + 3.1416i -1.0497 + 3.1416i -1.2701 + 3.1416i -1.5271 + 3.1416iColumns 25 through 28-1.8436 + 3.1416i -2.2727 + 3.1416i -2.9837 + 3.1416i -37.0245 Columns 29 through 32-3.0017 -2.3085 -1.8971 -1.5978Columns 33 through 36-1.3575 -1.1531 -0.9723 -0.8083Columns 37 through 40-0.6567 -0.5151 -0.3819 -0.2561Columns 41 through 44-0.1374 -0.0255 0.0792 0.1766Columns 45 through 480.2663 0.3478 0.4206 0.4841Columns 49 through 520.5379 0.5815 0.6145 0.6366Columns 53 through 560.6474 0.6470 0.6351 0.6119Columns 57 through 600.5777 0.5327 0.4774 0.4126Column 610.3388z4 =0 0.2500 0 1.2500 1.0000 2.25002. 已知:求下列表达式的值:(1) A+6*B和A-B+I(其中I为单位矩阵)(2) A*B和A.*B(3) A^3和A.^3(4) A/B及B\A(5) [A,B]和[A([1,3],:);B^2]解:M 文件:A=[12 34 -4;34 7 87;3 65 7];B=[1 3 -1;2 0 3;3 -2 7]; A+6.*BA-B+eye(3)A*BA.*BA^3A.^3A/BB\A[A,B][A([1,3],:);B^2]运算结果:A=[12 34 -4;34 7 87;3 65 7];B=[1 3 -1;2 0 3;3 -2 7]; A+6.*BA-B+eye(3)A*BA.*BA^3A.^3A/BB\A[A,B][A([1,3],:);B^2]ans =18 52 -1046 7 10521 53 49ans =12 31 -332 8 840 67 1ans =68 44 62309 -72 596154 -5 241ans =12 102 468 0 2619 -130 49ans =37226 233824 48604247370 149188 60076678688 454142 118820ans =1728 39304 -6439304 343 65850327 274625 343ans =16.4000 -13.6000 7.600035.8000 -76.2000 50.200067.0000 -134.0000 68.0000ans =109.4000 -131.2000 322.8000-53.0000 85.0000 -171.0000-61.6000 89.8000 -186.2000ans =12 34 -4 1 3 -134 7 87 2 0 33 65 7 3 -2 7ans =12 34 -43 65 74 5 111 0 1920 -5 403. 设有矩阵A和B(1) 求它们的乘积C。

matlab课后习题答案刘

matlab课后习题答案刘

matlab课后习题答案刘MATLAB课后习题答案刘在本学期的MATLAB课程中,我们学习了MATLAB的基础知识和一些高级功能。

为了帮助同学们更好地复习和巩固所学知识,以下是针对课后习题的一些参考答案。

1. 向量和矩阵的基本操作- 创建一个3x3的单位矩阵。

```matlabA = eye(3);```- 计算矩阵A和B的乘积,其中B是A的转置。

```matlabB = A';C = A * B;```2. 条件语句和循环- 编写一个循环,打印出1到10的所有奇数。

```matlabfor i = 1:2:10disp(i);end```- 编写一个条件语句,检查一个数是否为素数。

```matlabfunction isPrime(n)if n <= 1isPrime = false;elsefor i = 2:sqrt(n)if mod(n, i) == 0isPrime = false;return;endendisPrime = true;endend```3. 函数和脚本- 编写一个函数,计算给定半径的圆的面积。

```matlabfunction area = circleArea(radius)area = pi * radius^2;end```- 编写一个脚本来调用上述函数,并打印出半径为5的圆的面积。

```matlabradius = 5;disp(['Area of circle with radius ', num2str(radius), ' is ', num2str(circleArea(radius))]);```4. 数据结构和文件I/O- 读取一个文本文件中的数据到一个cell数组。

```matlabfilename = 'data.txt';fileID = fopen(filename, 'r');data = textscan(fileID, '%s');fclose(fileID);```- 将一个矩阵写入到一个CSV文件中。

MATLAB语言基础与应用(第二版)第1章 习题答案

MATLAB语言基础与应用(第二版)第1章 习题答案

第1章习题与答案1.1 MA TLAB桌面主要有哪些窗口构成?这些窗口主要功能是什么?请参考1.3节1.2计算下列表达式(1) 3+6 (2) sin(1+i)(3) (1+2i)×(2-i) (4) 2×4+3解答:(1) >> 3+6ans = 9(2) >> sin(1+i)ans = 1.2985 + 0.6350i(3) >> (1+2*i)*(2-i)ans = 4.0000 + 3.0000i(4) >> 2*4+3ans = 111.3借助“help”函数了解“meshgrid”函数的意义。

解答:>> help meshgridmeshgrid Cartesian grid in 2-D/3-D space[X,Y] = meshgrid(xgv,ygv) replicates the grid vectors xgv and ygv toproduce the coordinates of a rectangular grid (X, Y). The grid vectorxgv is replicated numel(ygv) times to form the columns of X. The gridvector ygv is replicated numel(xgv) times to form the rows of Y.[X,Y,Z] = meshgrid(xgv,ygv,zgv) replicates the grid vectors xgv, ygv, zgvto produce the coordinates of a 3D rectangular grid (X, Y, Z). The gridvectors xgv,ygv,zgv form the columns of X, rows of Y, and pages of Zrespectively. (X,Y,Z) are of size numel(ygv)-by-numel(xgv)-by(numel(zgv).[X,Y] = meshgrid(gv) is equivalent to [X,Y] = meshgrid(gv,gv).[X,Y,Z] = meshgrid(gv) is equivalent to [X,Y,Z] = meshgrid(gv,gv,gv).The coordinate arrays are typically used for the evaluation of functionsof two or three variables and for surface and volumetric plots.meshgrid and NDGRID are similar, though meshgrid is restricted to 2-Dand 3-D while NDGRID supports 1-D to N-D. In 2-D and 3-D the coordinates output by each function are the same, the difference is the shape of theoutput arrays. For grid vectors xgv, ygv and zgv of length M, N and Prespectively, NDGRID(xgv, ygv) will output arrays of size M-by-N whilemeshgrid(xgv, ygv) outputs arrays of size N-by-M. Similarly,NDGRID(xgv, ygv, zgv) will output arrays of size M-by-N-by-P whilemeshgrid(xgv, ygv, zgv) outputs arrays of size N-by-M-by-P.Example: Evaluate the function x*exp(-x^2-y^2)over the range -2 < x < 2, -4 < y < 4,[X,Y] = meshgrid(-2:.2:2, -4:.4:4);Z = X .* exp(-X.^2 - Y.^2);surf(X,Y,Z)Class support for inputs xgv,ygv,zgv:float: double, singleinteger: uint8, int8, uint16, int16, uint32, int32, uint64, int64 See also surf, slice, ndgrid.Overloaded methods:codistributed/meshgridgpuArray/meshgridReference page in Help browserdoc meshgrid1.4通过帮助浏览器窗口了解函数sin和cos。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第1章一、思考题4. (1) B=A(2:5,1:2:5)(2)A(7)=[](3) A=A+30(4)size(A)(5) t(find(t==0))=eps(6)t=reshape(x,3,4)(7)abs('matlab')(8)char(93)5. A=[97,67,34,10;-78,75,65,5;32,5,-23,-59;0,-12,54,7] (1) B=A(1:3,:)C=A(:,1:2)D=A(2:4,3:4)E=B*C(2)A(find(A>=50 & A<=100))二、实验题=-74/180*pi;y=-27/180*pi;sin(x*x+y*y)/sqrt(tan(abs(x+y)))+pi=::;exp*a).*sin(a+=[2,4;,5];log(x+sqrt(1+x.*x))/24. A=[3,54,2;34,-45,7;87,90,15];B=[1,-2,67;2,8,74;9,3,0];(1)A*Bans =129 432 41977 -407 -1052402 591 12489 A.*Bans =3 -108 13468 -360 518783 270 0(2) A^3ans =-28917 240246 -4368 137883 -259101 27669 171333 252504 38673 A.^3ans =27 157464 8 39304 -91125 343 658503 729000 3375(3)A/Bans =B/Aans =(4)[A,B]ans =3 54 2 1 -2 6734 -45 7 2 8 74 87 90 15 9 3 0 [A([1,3],:);B^2]ans =3 54 287 90 15600 183 -81684 282 72615 6 825=1+2i;b=3+4i;c=exp(pi*i/6)c =+c+a*b/(a+b)ans =+第2章一、思考题=0;for n=0:63s=s+2^n;enddisp(s)n=0:63;s=sum(2.^n)二、实验题1.x=input('输入一个四位整数:');y=[fix(x/1000),mod(fix(x/100),10),mod(fix(x/10),10),mod(x,10)] z=mod((y+7),10)x=z(3)*1000+z(4)*100+z(1)*10+z(2)2.gh=input('输入工号');h=input('输入工时');dj=84;if h>120gz=dj*120+*dj*(h-120);elseif h<60gz=dj*h-700;elsegz=dj*h;endformat bank;display([gh,gz])3.循环结构n=input('input n:');s=0;for k=1:ns=s+1/k^2;enddisplay(sqrt(s*6))向量运算n=input('input n:');k=1:n;display(sqrt(sum(1./k.^2)*6))4.y=0;k=0;while y<3k=k+1;y=y+1/(2*k-1);enddisplay([k-1,y-1/(2*k-1)])5.x0=0;x=1;k=0;a=input('a=');b=input('b=');while abs(x-x0)>=1e-5 && k<500x0=x;x=a/(b+x0);k=k+1;enddisplay([k,x]);display([(-b+sqrt(b^2+4*a))/2,(-b-sqrt(b^2+4*a))/2]); 6.y=fun(40)/(fun(30)+fun(20))(1)函数文件function f=fun(n)f=n+log(n^2+5);(2)函数文件function f=fun(n)a=1:n;f=sum(a.*(a+1));第3章一、思考题4.t=0::;y=sqrt(3)/2*exp(-4*t).*sin(4*sqrt(3)*t+pi/3);5.x=-10::10;y=linspace(-6,6,size(x,2))z=x.^3+3*x.*y.^2;plot3(x,y,z)6.x=100:100:400;y=100:100:400;z=[636,697,624,478;698,712,630,478;680,674,598,412;662,626,552,334]; [X,Y]=meshgrid(x,y);mesh(X,Y,z)二、实验题1.(1)x=-10::10;plot(x,x-x.^3/3/2)(2)plot(x,exp(-x.*x/2)/2/pi)(3)x=-8::8;plot(x,sqrt((64-x.*x)/2))(4)t=0:pi/100:6*pi;plot(t.*sin(t),t.*cos(t))2. (1)x1=linspace(0,1,100);y1=2*;t=linspace(0,pi,100);x=sin(3*t).*cos(t);y=sin(3*t).*sin(t);plot(x1,y1,'r-',x,y,'b:');text,1,'y=');text,,'x=sin(3t)cos(t)');text,,'y=sin(3t)sin(t)');(2)subplot(1,2,1);scatter(x1,y1,10)title('y=');subplot(1,2,2);scatter(x,y,10)3.subplot(1,21);x=1:1:100; y=sin(1./x); plot(x,y)subplot(1,2,2);fplot('sin(1/x)',[1,100]) 4.subplot(2,2,1);bar(t,y);subplot(2,2,2);stairs(t,y);subplot(2,2,3);stem(t,y)subplot(2,2,4);semilogy(t,y);5.theta=linspace(-pi,pi,100); ro=5.*cos(theta)+4;polar(theta,ro);fi=linspace(0,2*pi,100);a=1 r=a.*(1+cos(fi));polar(fi,r);6.t=0:pi/20:2*pi;x=exp(-t./20).*cos(t);y=exp(-t./20).*sin(t);z=t; plot3(x,y,z);t=0::1;x=t;y=t.^2;z=t.^3;plot3(x,y,z);7.x=-30::0;y=0::30;[x,y]=meshgrid(x,y);z=10.*sin(sqrt(x.^2+y.^2))./sqrt(1+x.^2+y.^2); meshc(x,y,z);8.x=linspace(-3,3,100);y=linspace(-3,3,100);[x y]=meshgrid(x,y);fxy=-5./(1+x.^2+y.^2);i=find(abs(x)<= & abs(y)<=;fxy(i)=NaN;surf(x,y,fxy)9.u=linspace(1,10,100);v=linspace(-pi,pi,100); [u,v]=meshgrid(u,v);x=3.*u.*sin(v);y=2.*u.*cos(v);z=4*u.^2;x=3*u.*sin(v);y=2*u.*cos(v);z=4*u.^2;surf(x,y,z);shading interp;light('position',[1,0,1]);10.t=0:pi/100:2*pi;y=sin(t);comet(t,y)第4章一、思考题5.(1)A=eye(3);(2)C=100+(200-100)*rand(5,6);(3)D=1+sqrt*randn(1,500);(4)E=ones(size(A));(5)A=A+30*eye(size(A));(6)B=diag(diag(A))二、实验题1.P=pascal(5);H=hilb(5);Dp=det(P);Dh=det(H);Kp=cond(P);Kh=cond(H);P矩阵的性能更好,因为Kp较小2.A=[1,-1,2,3;0,9,3,3;7,-5,0,2;23,6,8,3]B=[3,pi/2,45;32,-76,sqrt(37);5,72,;exp(2),0,97]A1=diag(A);B1=diag(B);A2=triu(A);B2=triu(B);A3=tril(A);B3=tril(B);rA=rank(A);rB=rank(B);nA=norm(A);nb=norm(B);cA=cond(A);cB=cond(B);3.A=[31,1,0;-4,-1,0;4,-8,-2];[V,D]=eig(A);4.A=diag([-1,-1,-1,-1],-1)+diag([-1,-1,-1,-1],1)+diag([2,2,2,2,2]) b=[1,0,0,0,0]';x1=inv(A)*b;x2=A\b;[L,U]=lu(A);x3=U\(L\b);[Q,R]=qr(a);[Q,R]=qr(A);x4=R\(Q\b)R=chol(A);x5=R\(R'\b)5.B=sparse(A);x1=inv(B)*b;x2=B\b;[L,U]=lu(B);x3=U\(L\b);第5章一、思考题3.A=randn(10,5); mean(A)std(A)max(max(A))min(min(A))sum(A,2)sum(sum(A))sort(A,1)sort(A,2,'descend')二、实验题1.A=rand(1,30000); mean(A)std(A)max(A)min(A)size(find(A>)/size(A)2.h=[466,715,950,1422,1635];w=[,,,,];hh=[500,900,1500];ww=interp1(h,w,hh,'spline')3.x=linspace(1,10,50);y=log(x);f=polyfit(x,y,5);yy=polyval(f,x);plot(x,y,'r-',x,yy,'g.')4.N=64; % 采样点数T=5; % 采样时间终点t=linspace(0,T,N); % 给出N个采样时间ti(I=1:N) x=3*exp(-t); % 求各采样点样本值xdt=t(2)-t(1); % 采样周期f=1/dt; % 采样频率(Hz)X=fft(x); % 计算x的快速傅立叶变换X F=X(1:N/2+1); % F(k)=X(k)(k=1:N/2+1)f=f*(0:N/2)/N; % 使频率轴f从零开始plot(f,abs(F),'-*') % 绘制振幅-频率图xlabel('Frequency');ylabel('|F(k)|')5.(1)p1=[1 2 0 0 7];p2=[1 -2];p3=[1 0 0 5 1];p12=conv(p1,p2);p=p12+[zeros(1,size(p12,2)-size(p3,2)),p3];roots(p)(2)A=[-1,4,3;2,1,5;0,5,6];Px=polyval(p,A)Pxm=polyvalm(p,A)6.(1)z=fzero('3*x-sin(x)+1',0)(2)建立函数文件function F=myfun(X)x=X(1);y=X(2);F(1)=x*x+y*y-9;F(2)=x+y-1;在命令窗口中输入以下命令:x=fsolve(@myfun,[3,0]',optimset('Display','off'))第6章一、思考题2.fx=inline('1./(1+x.^2)');[I,n]=quad(fx,-100000,100000,1e-3);[I,n]=quadl(fx,-100000,100000,1e-3);x=-100000::100000;y=1./(1+x.*x);trapz(x,y);3.(1) fx=inline('-2*y+2*x*x+2*x');[t,y]=ode23(fx,[0,],1)(2) fx=inline('y-exp(x)*cos(x)');[t,y]=ode23(fx,[0,3],1)二、实验题1.for x=1:3fx=[x,x^2,x^3;1,2*x,3*x;0,2,6*x];diff(fx)end2.(1)x=0::1;y=x.^10+10.^x+1./log10(x);dy=diff(y)/;(2)x=0::1;y=log(1+x);dy=diff(y,2)/;plot(x(1:99),dy)3.(1)fx=inline('x.^2.*sqrt(2*x.*x+3)'); quad(fx,1,5)(2)fx=inline('x./sin(x).^2');quad(fx,pi/4,pi/3)(3)fx=inline('abs(cos(x+y))'); dblquad(fx,0,pi,0,pi)(4)syms x y;fx=x*y;int(int(fx,y^2,y+2),-1,2)x的积分区间为【0,2】时fx=inline('x.*y');dblquad(fx,0,2,-1,2)4.y=[,,,,,,];trapz(x,y)5.(1)yp=inline('-+sin(10*x))*y');[t,y]=ode23(yp,[0,5],1);(2)令''3,,21y x y x y x ='==,则可写出原方程的状态方程形式: 123233221sin 31)1(2cos 5cos x t x x t t t x x x x x +--++='='='⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡+⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡⎥⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎢⎣⎡+-+-=⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡'''t x x x t t tx x x cos 00)1(2cos 51sin 311000103212321 建立函数文件function y=ztfun(t,x)b=[0;0;cos(t)]; y=[0,1,0;0,0,1;-1/(3+sin(t)),-1,5*cos(2*t)/(t+1)^2]*x+b; 解微分方程组[t,y]=ode23(@ztfun,[0,5],[1;0;2]);6.建立函数文件function yy=ztfun(t,y)yy=[y(2)*y(3);-y(1)*y(3);*y(1)*y(2)];解微分方程组[t,y]=ode23(@ztfun,[0,5],[0;1;1])第7章一、思考题(1)数值积分fx=inline('exp(x).*(1+exp(x)).^2');quad(fx,0,log(2))符号积分f=sym('exp(x)*(1+exp(x))^2');v=int(f,0,log(2));eval(v)(2)略二、实验题1.A=sym('[1,2,3;x,y,z;3,2,1]')rank(A)inv(A)det(A)2.(1) y=sym('sqrt(x+sqrt(x+sqrt(x)))');y1=diff(y)y2=diff(y,'x',2)(2) syms x y;fxy=sin(x^2*y)*exp(-x^2-y);diff(diff(fxy,x),y)3.(1) syms xint(1/(1+x^4))(2) syms x tint((-2*x*x+1)/(2*x*x-3*x+1)^2,x,cos(t),exp(2*t)) 4.syms n xsymsum(1/(2*n+1)/(2*x+1)^(2*n+1),n,0,inf)symsum(1/(2*n+1)/(2*x+1)^(2*n+1),n,0,5)5.(1) syms xtaylor((exp(x)+exp(-x))/2,5,0)(2) syms a xtaylor(exp(-5*x)*sin(3*x+pi/3),5,a)6.(1)x=solve(sym('x^3+a*x+1=0'))(2)[x y]=solve('sqrt(x^2+y^2)-100=0,3*x+5*y-8=0')7. 方程转化为: π21'11't y y y y -=+=⎩⎨⎧符号解[y1,y11]=dsolve('Dy=y1,Dy1+y=1-t^2/pi','y(-2)=5,y1(-2)=5','t') 数值解编写函数文件function yy=ztfun(t,y)yy=[y(2);1-t^2/pi-y(1)];在命令窗口输入以下命令[t,y]=ode45(@ztfun,[-2,7],[-5;5]);t=linspace(-2,7,49)y2=y8.[x,y]=dsolve('Dx=3*x+4*y,Dy=-4*x+3*y','x(0)=0,y(0)=1')。

相关文档
最新文档