MATLAB程序设计与应用第二版课后题答案.doc
第二章3.设矩阵A为A=24 23 9 21 6;65 74 24 11 21;34 5 98 75 21;8 42 42 53 121;43 21 45 64 21;(1) B=A(2:5,1:2:5)B = 65 24 21 34 98 21 8 42 121 43 45 21(2)A(7)=A =24 65 34 8 43 23 5 42 21 9 24 98 42 45 21 11 75 53 64 6 21 21 121 21(3)A+30(4)size(A);ndims(A)(5)题目有误(6)reshape(x,3,4)(7)abs(x)(8)char(x)4. L1 = 0 0 0 0 1 0 0 0 0L2 = 1 1 1 1 1 0 0 0 0L3 = 0 0 0 1 1 1 0 0 0L4 = 4 5 65.(1)B=A(1:3,:)C=A(:,1:2)D=A(2:4,3:4)E=B*CB = 23.0000 10.0000 -0.7780 0 41.0000 -45.0000 65.0000 5.0000 32.0000 5.0000 0 32.0000C = 23.0000 10.0000 41.0000 -45.0000 32.0000 5.0000 6.0000 -9.5400D = 65.0000 5.0000 0 32.0000 54.0000 3.1400E = 1.0e+003 * 0.9141 -0.2239 1.2080 2.7123 1.1330 -0.2103(2)E<Dans = 0 1 0 0 0 1E&Dans =1 1 0 1 1 1E|Dans = 1 1 1 1 1 1D|Eans = 0 0 1 0 0 0find(A>=10&A<25)ans = 1 56. all(A)ans = 0any(A)ans = 1isnan(A) ans = 0 1 0 0 0 0 0isinf(A)ans = 0 0 1 1 0 0 0isfinite(A)ans = 1 0 0 0 1 1 17.A(1).x1=学号;A(1).x2=姓名;A(1).x3=专业;A(1).x4.x41=成绩1;.A(2).x1=学号;A(2).x2=姓名;A(2).x3=专业;A(2).x4.x41=成绩1;.A(3).x1=学号;A(3).x2=姓名;A(3).x3=专业;A(3).x4.x41=成绩1;.A(4).x1=学号;A(4).x2=姓名;A(4).x3=专业;A(4).x4.x41=成绩1;.A(5).x1=学号;A(5).x2=姓名;A(5).x3=专业;A(5).x4.x41=成绩1;.8.(1)size(B)ans = 2 2ndims(B)ans = 2(2) B(2)ans =3x3 doubleB(4)ans = 3x3 cell(3)B(3)=B = 1 3x3 double 3x3 cellB3=B = 1 3x3 double 第三章1.(1)A=eye(3)(2) A=100+100*rand(5,6)(3)A=1+sqrt(0.2)*randn(10,50)(4)B=ones(size(A)(5)A+30*eye(size(A)(6)B=diag(diag(A)2.B=rot90(A)C=rot90(A,-1)3.B=inv(A) ;A的逆矩阵C=det(A) ;A的行列式的值D=A*BE=B*AD=E 因此A与A-1是互逆的。4.A=4 2 -1;3 -1 2;12 3 0;b=2;10;8;x=inv(A)*bx = -6.0000 26.6667 27.33335.(1) diag(A) ;主对角线元素ans = 1 1 5 9triu(A) ;上三角阵ans = 1 -1 2 3 0 1 -4 2 0 0 5 2 0 0 0 9tril(A) ;下三角阵ans = 1 0 0 0 5 1 0 0 3 0 5 011 15 0 9rank(A) ;秩ans = 4norm(A) ;范数ans = 21.3005cond(A) ;条件数ans = 11.1739trace(A) ;迹ans =16(2)略6.A=1 1 0.5;1 1 0.25;0.5 0.25 2A = 1.0000 1.0000 0.5000 1.0000 1.0000 0.2500 0.5000 0.2500 2.0000 V,D=eig(A)V = 0.7212 0.4443 0.5315 -0.6863 0.5621 0.4615 -0.0937 -0.6976 0.7103D = -0.0166 0 0 0 1.4801 0 0 0 2.5365第四章1.a=input('请输入一个4位数:');while (a<1000|a>9999)a=input('输入错误,请重新输入一个4位数:');endb=fix(a/1000);c=rem(fix(a/100),10);d=rem(fix(a/10),10);e=rem(a,10);b=b+7;c=c+7;d=d+7;e=e+7;b=rem(b,10);c=rem(c,10);d=rem(c,10);e=rem(e,10);g=b;b=d;d=g;g=c;c=e;e=g;a=1000*d+100*e+10*b+c;disp('加密后:',num2str(a)2.a=input('请输入a: ');b=input('请输入b: ');c=input('请输入c: ');x=0.5:1:5.5;x1=(x>=0.5&x<1.5);x2=(x>=1.5&x<3.5);x3=(x>=3.5&x<=5.5);y1=a.*(x.2)+b.*x+c;y2=a*(sin(b)c)+x;y3=log(abs(b+c./x);y=y1.*x1+y1.*x2+y3.*x3;disp(y)3.x=fix(rand(1,20)*89)+10;x1=fix(sum(x)/20);disp('平均数是:',num2str(x1)m=(rem(x,2)=0&x<x1);n=find(m);disp('小于平均数的数是:',num2str(x(n);4.A=input('请输入20个数的一个行向量:');m=A;a=m;b=m;for m=Aif a>=ma=m;elseif b<=mb=m;endenddisp('最小数是:',num2str(a)disp('最大数是:',num2str(b)5.s=0;a=0;for b=1:64c=2a;a=a+1;s=s+c;enddisp('2的0次方到63次方的和是:',num2str(s)6,sum1=0;for n=1:100x=(-1)(n+1)*(1/n);sum1=sum1+x;enddisp('当n取100时: sum=',num2str(sum1)sum2=0;for n=1:1000x=(-1)(n+1)*(1/n);sum2=sum2+x;enddisp('当n取1000时: sum=',num2str(sum2)sum3=0;for n=1:10000x=(-1)(n+1)*(1/n);sum3=sum3+x;enddisp('当n取10000时:sum=',num2str(sum3)8.clear alla=input('请输入一个矩阵:');b=input('请再输入一个矩阵:(注意:两矩阵要可以相乘)');f1,f2=juzhenji(a,b);9.clear ally=qiuhe(100,1)+qiuhe(50,2)+qiuhe(10,-1);disp(y)10.(1)S=108(2)x=4 12 20 y=2 4 6 第五章1. (1) x=-10:0.1:10;y=100./(1+x.2);plot(x,y)(2) x=-10:0.1:10;y=1/(2*pi)*exp(-x.2/2);plot(x,y)(3) ezplot('x2+y2=1')(4) t=-10:0.1:10; x=t.2;y=5*t.3; plot(x,y)2.(1)theta=0:0.01:2*pi;rho=5*cos(theta)+4;polar(theta,rho)(2)theta=0.001:0.1:2*pi;rho=12./sqrt(theta);polar(theta,rho)(3) theta=0.001:0.1:2*pi;rho=5./cos(theta)-7;polar(theta,rho)(4)theta=0.001:0.1:2*pi;rho=pi/3.*theta.2;polar(theta,rho)3.(1)t=0:pi/100:2*pi;x=cos(t);y=sin(t);z=t;plot3(x,y,z)(2)u=0:pi/100:2*pi;v=0:pi/100:2*pi;x=(1+cos(u).*cos(v);y=(1+cos(u).*sin(v);z=sin(u);plot3(x,y,z)(3)(4)5.plot函数:>> x=linspace(-10,10,200);>> y=;>> for x0=xif x0>0y=y,x0.2+(1+x0).(1/4)+5;elseif x0=0y=y,0;elseif x0<0y=y,x0.3+sqrt(1-x0)-5;endend>> plot(x,y)fplot函数:fplot('(x<0).*(x.3+sqrt(1-x)-5)+(x=0).*0+(x>0).*(x.2+(1+x).(1/4)+5)',-10,10)第六章1.A=randn(10,5)(1)mean(A) ;均值 std(A) ;标准方差(2)max(max(A) ;最大元素 min(min(A) ;最小元素(3)B=sum(A,2) ;A每行元素的和 sum(B) ;A全部元素之和(4)sort(A) ;A的每列元素按升序排列 sort(A,2,descend) ;A的每行元素按将序排列2.(1)(2)X=1 4 9 16 25 36 49 64 81 100;Y=1:10;X1=1:100;Y1=interp1(X,Y,X1,'cubic')3.x=165 123 150 123 141;y=187 126 172 125 148;P=polyfit(x,y,3)P = 1.0e+003 * -0.0000 0.0013 -0.1779 8.4330所以它的线性拟合曲线为:p(x)=1.3x2177.9x+84334.(1)P1=0 3 2;P2=5 -1 2;P3=1 0 -0.5;P=conv(conv(P1,P2),P3)P = 0 15.0000 7.0000 -3.5000 0.5000 -2.0000 -2.0000所以P(x)=15x5+7x4-3.5x3+0.5x2-2x-2(2)roots(P)ans = 0.7071 0.1000 + 0.6245i 0.1000 - 0.6245i -0.7071 -0.6667 (3)i=0:10;xi=0.2*i;polyval(P,xi)ans = -2.0000 -2.3920 -2.6112 -1.7024 2.7104 15.0000 42.1120 94.1408 184.9056 332.5264 560.00005.(1)建立函数文件:function f=fxy(u)x=u(1);y=u(2);f=3.*x.2+2*x.*y+y.2在命令窗口中输入以下命令:U,fmin=fminsearch('fxy',1,1)结果:U = 1.0e-004 * -0.0675 0.1715fmin = 1.9920e-010(2)f=inline('-sin(x)-cos(x.2)');fmax=fminbnd(f,0,pi)fmax =0.73106.(1)x=pi/6 pi/4 pi/3;f=inline('sin(x).2+cos(x).2');dx=diff(f(x,5*pi/12)/(pi/12) 可参见第157页例题6.19dx = 0 0 0x=pi/2时单独计算:x=pi/2;f=inline('sin(x).2+cos(x).2');diff(f(x,pi)/(pi/2)ans = 0(2)x=1:3;f=inline('sqrt(x.2+1)');dx=diff(f(x,4)结果:dx =0.8219 0.9262 0.96087.(1)f=inline('sin(x).5.*sin(5*x)');quad(f,0,pi)ans =0.0982(2)f=inline('(1+x.2)./(1+x.4)');quad(f,-1,1)ans =2.2214(3)f=inline('x.*sin(x)./(1+cos(x).2)');quad(f,0,pi)ans =2.4674(4)f=inline('abs(cos(x+y)');dblquad(f,0,pi,0,pi)ans =6.28328.N=64; %采样点数T=5; %采样时间终点t=linspace(0,T,N); %给出N个采样时间ti(i=1:N)y=exp(-t); %求各采样点样本值ydt=t(2)-t(1); %采样周期f=1/dt; % 采样频率Y=fft(y); %计算y的快速傅里叶变换YF=Y(1:N/2+1); %F(k)=Y(k)f=f*(0:N/2)/N; %使频率轴f从0开始plot(f,abs(F) %绘制振幅-频率图9.(1)矩阵求逆法:A=2 3 5;3 7 4;1 -7 1;b=10;3;5;x=inv(A)*bx = -1.8060 -0.53733.0448矩阵除法法:A=2 3 5;3 7 4;1 -7 1;b=10;3;5;x=Abx = -1.8060 -0.53733.0448矩阵分解法:A=2 3 5;3 7 4;1 -7 1;b=10;3;5;L,U=lu(A);x=U(Lb)x = -1.8060 -0.53733.0448(2)方法同(1)10.函数文件:line_solution(A,b)function x,y=line_solution(A,b)m,n=size(A);y=;if norm(b)>0if rank(A)=rank(A,b)if rank(A)=ndisp('原方程组有唯一解x');x=Ab; elsedisp('原方程组有无穷个解,特解为x,齐次方程组的基础解系为y');x=Ab;y=null(A,'r');enddisp('方程组无解');x=; endelsedisp('原方程组有零解x');x=zeros(n,1);if rank(A)<ndisp('方程组有无穷个解,基础解系为y');y=null(A,'r');endend程序:A=2 1 -1 1;4 2 -2 1;2 1 -1 -1;b=1;2;1;x,y=line_solution(A,b)结果:原方程组有无穷个解,特解为x,齐次方程组的基础解系为yWarning: Rank deficient, rank = 2, tol = 4.3512e-015.> In line_solution at 11方程组无解x = y = -0.5000 0.5000 1.0000 0 0 1.0000 0 011.(1)f=inline('x-sin(x)./x');x=fzero(f,0.5)x =0.8767(2)f=inline('(sin(x).2).*exp(-0.1.*x)-0.5.*abs(x)');x=fzero(f,1.5)x = 1.673812.函数文件:function f=fxy(u)x=u(1)y=u(2)f(1)=x-0.6*sin(x)-0.3*cos(y)f(2)=y-0.6*cos(x)+0.3*sin(y)在命令窗口输入以下命令:x=fsolve('fxy',0.5,0.5,optimset('Display','off')结果:x = 0.6354 0.373415.A=-1 2 0;-1 2 -1;-1 2 -1;-1 2 -1;0 2 -1;d=-1;0;1;B=spdiags(A,d,5,5);b=1 0 0 0 0'x=(inv(B)*b)'x = 0.8333 0.6667 0.5000 0.3333 0.1667