第二章3.设矩阵A为A=[24239216;6574241121;345987521;8424253121;4321456421];(1)B=A(2:5,1:2:5)B=652421349821842121434521(2)A(7)=[]A=246534843235422192498424521117553646212112121(3)A+30(4)size(A);ndims(A)(5)题目有误(6)reshape(x,3,4)(7)abs(x)(8)char(x)4.L1=000010000L2=111110000L3=000111000L4=4565.(1)B=A(1:3,:)C=A(:,1:2)D=A(2:4,3:4)E=B*CB=23.000010.0000-0.7780041.0000-45.000065.00005.000032.00005.0000032.0000C=23.000010.000041.0000-45.000032.00005.00006.0000-9.5400D=65.00005.0000032.000054.00003.1400E=1.0e+003*0.9141-0.22391.20802.71231.1330-0.2103(2)EDans=010001E&Dans=110111E|Dans=111111~D|~Eans=001000find(A=10&A25)ans=156.all(A)ans=0any(A)ans=1isnan(A)ans=0100000isinf(A)ans=0011000isfinite(A)ans=10001117.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=22ndims(B)ans=2(2)B(2)ans=[3x3doubleB(4)ans={3x3cell}(3)B(3)=[]B=[1][3x3double]{3x3cell}B{3}=[]B=[1][3x3double][]第三章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=[42-1;3-12;1230];b=[2;10;8];x=inv(A)*bx=-6.000026.666727.33335.(1)diag(A);主对角线元素ans=1159triu(A);上三角阵ans=1-12301-4200520009tril(A);下三角阵ans=100051003050111509rank(A);秩ans=4norm(A);范数ans=21.3005cond(A);条件数ans=11.1739trace(A);迹ans=16(2)略6.A=[110.5;110.25;0.50.252]A=1.00001.00000.50001.00001.00000.25000.50000.25002.0000[V,D]=eig(A)V=0.72120.44430.5315-0.68630.56210.4615-0.0937-0.69760.7103D=-0.01660001.48010002.5365第四章1.a=input('请输入一个4位数:');while(a1000|a9999)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&x1.5);x2=(x=1.5&x3.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&xx1);n=find(m);disp(['小于平均数的数是:',num2str(x(n))]);4.A=input('请输入20个数的一个行向量:');m=A;a=m;b=m;form=Aifa=ma=m;elseifb=mb=m;endenddisp(['最小数是:',num2str(a)])disp(['最大数是:',num2str(b)])5.s=0;a=0;forb=1:64c=2^a;a=a+1;s=s+c;enddisp(['2的0次方到63次方的和是:',num2str(s)])6,sum1=0;forn=1:100x=(-1)^(n+1)*(1/n);sum1=sum1+x;enddisp(['当n取100时:sum=',num2str(sum1)])sum2=0;forn=1:1000x=(-1)^(n+1)*(1/n);sum2=sum2+x;enddisp(['当n取1000时:sum=',num2str(sum2)])sum3=0;forn=1:10000x=(-1)^(n+1)*(1/n);sum3=sum3+x;enddisp(['当n取10000时:sum=',num2str(sum3)])8.clearalla=input('请输入一个矩阵:');b=input('请再输入一个矩阵:(注意:两矩阵要可以相乘)');[f1,f2]=juzhenji(a,b);9.clearally=qiuhe(100,1)+qiuhe(50,2)+qiuhe(10,-1);disp(y)10.(1)S=108(2)x=41220y=246第五章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('x^2+y^2=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=[];forx0=xifx00y=[y,x0.^2+(1+x0).^(1/4)+5];elseifx0==0y=[y,0];elseifx00y=[y,x0.^3+sqrt(1-x0)-5];endendplot(x,y)fplot函数:fplot('(x0).*(x.^3+sqrt(1-x)-5)+(x==0).*0+(x0).*(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=[149162536496481100];Y=1:10;X1=1:100;Y1=interp1(X,Y,X1,'cubic')3.x=[165123150123141];y=[187126172125148];P=polyfit(x,y,3)P=1.0e+003*-0.00000.0013-0.17798.4330所以它的线性拟合曲线为:p(x)=1.3x2—177.9x+84334.(1)P1=[032];P2=[5-12];P3=[10-0.5];P=conv(conv(P1,P2),P3)P=015.00007.0000-3.50000.5000-2.0000-2.0000所以P(x)=15x5+7x4-3.5x3+0.5x2-2x-2(2)roots(P)ans=0.70710.1000+0.6245i0.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.70242.710415.000042.112094.1408184.9056332.5264560.00005.(1)建立函数文件:functionf=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.06750.1715fmin=1.9920e-010(2)f=inline('-sin(x)-cos(x.^2)');fmax=fminbnd(f,0,pi)fmax=0.73106.(1)x=[pi/6pi/4pi/3];f=inline('sin(x).^2+cos(x).^2');dx=diff(f([x,5*pi/12]))/(pi/12)可参见第157页例题6.19dx=000x=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.82190.92620.96087.(1)f=inline('sin(x