Matlab程序设计与应用第二版刘卫国课后实验答案(20页).doc
-实验一:T1:%第一小题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>=0&t<1).*(t.2)+(t>=1&t<2).*(t.2-1)+(t>=2&t<3).*(t.2-2*t+1)T2:A=12,34,-4;34,7,87;3,65,7B=1,3,-1;2,0,3;3,-2,7disp ('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('A3=');disp(A3);disp('A.3=');disp(A.3);disp('A/B=');disp(A/B);disp('BA=');disp(BA);disp('A,B=');disp(A,B);disp('A(1,3,:);B2=');disp(A(1,3,:);B2);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;C=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>='A'&a<='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,Sdisp('A2=');disp(A2);disp('E,R+RS;O,S2');B=E,R+R*S;O,S2T2: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 a>b disp('帕萨卡矩阵P性能更好');elseif a<b disp('希尔伯特矩阵H性能更好');else disp('两个矩阵性能相同');endT3: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,5V,D=eig(A)T5:A=1/2,1/3,1/4;1/3,1/4,1/5;1/4,1/5,1/6B=0.95,0.67,0.52'X1=ABB(3)=0.53X2=ABdisp('系数矩阵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.0for 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; else y(i)=x(i)2-x(i)-1; endendyT2-if:s=input('please enter the score:');while (s<0|s>100) disp('the score is not reasonable'); s=input('please enter the score:');endif (s>=90&s<=100); disp('A');elseif(s>=80&s<90); disp('B');elseif(s>=70&s<80); disp('C');elseif(s>=60&s<70); disp('D');else disp('E');endT2-switch:s=input('please enter the score:');while (s<0|s>100) disp('the score is not reasonable'); s=input('please enter the score:');endswitch fix(s/10)case9,10disp('A');case8disp('B');case7disp('C');case6disp('D');case0,1,2,3,4,5disp('E');endT3:t=input('请输入工时time=');if t>120 w=120*84+(t-120)*1.15*84;elseif t<60 w=t*84-700;else w=84*t;enddisp('应发工资为:');disp(w);T4:a=10+floor(rand(1)*89)b=10+floor(rand(1)*89)s=input('请输入+或-或*或/','s');while(s='+'&&s='-'&&s='*'&&s='/') disp('输入的符号错误,请重新输入'); s=input('请输入+或-或*或/','s');endswitch s case'+' c=a+b; case'-' c=a-b; case'*' c=a*b; case'/' c=a/b; endcT5:A=rand(5,6)n=input('请输入n:');while (n<1) disp('输入的n有误,请重新输入'); n=input('请输入n:');endif n>5 B=A(5,:)else B=A(n,:)End实验四:T1:n=input('请输入n:');m=0;for i=1:n; m=m+1/i2;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>=3 disp('最大n值为:'); i-1 disp('对应的y值为:'); y-1/(2*i-1) break; endendT3: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.00001&i<500) i=i+1; x(i+1)=a/(x(i)+b);endr1=(-b+sqrt(b2+4*a)/2;r2=(-b-sqrt(b2+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 shortT4: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(f>0)fn=length(find(f<0)f0=length(find(f=0)T5:j=0;s=0;for i=3:50 m=i*(i-1)-1; if isprime(m)=1 m s=s+m; j=j+1; endenddisp('亲密数的对数:',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:n f=f+i*(i+1);endfx: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),1B=0,m1*9.8,0,m2*9.8'disp('a1,a2,N1,N2=');disp(AB');T3:j=1;n=1;k=1;for i=10:99 % 挑选出1099中所有的素数 if isprime(i)=1 m(j)=i; j=j+1; endendfor t=1:length(m); s(n)=10*rem(m(t),10)+fix(m(t)/10); %挑选出的素数进行位置交换 if isprime(s(n)=1 %再挑选出交换绝对素数 r(k)=m(t); k=k+1; end n=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); else y=y,1/2*log(x0+sqrt(1+x02); endendsubplot(1,2,1);plot(x,y);for i=1:length(x) if x(i)<=0 y(i)=(x(i)+sqrt(pi)/exp(2); else y(i)=1/2*log(x(i)+sqrt(1+x(i)2);endendsubplot(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(A>0.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('每门课平均分:');mean(P,1)disp('每门课的标准方差:');std(P,0,1)s=sum(P,2);disp('总分最高分:');Y2,U2=max(s,1)disp('总分最低分:');Y3,U3=min(s,1)zcj,xsxh=sort(s,1,'descend') %descend降序T3:h=6:2:18;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;t=6.5:2:17.5;T1=interp1(h,t1,t,'spline')T2=interp1(h,t2,t,'spline')T4:x=1:10:101;y=log10(x);p=polyfit(x,y,5)x1=1:0.01:101;y1=log10(x1);y2=polyval(p,x1);plot(x1,y1,'r-',x1,y2,'b-.');T5:P1=1,2,4,0,5;P2=0,0,0,1,2;P3=0,0,1,2,3;P4=conv(P2,P3);sp1=length(P1);sp2=length(P4);P=zeros(1,sp2-sp1),P1+P4x=roots(P)A=-1,1.2,-1.4;0.75,2,3.5;0,5,2.5;Ax=polyval(P,A)AA=polyvalm(P,A)实验九:T1:f=inline('det(x x2 x3;1 2*x 3*x2;0 2 6*x)');i=1;x=1;while x<=3.01 g(i)=f(x); i=i+1; x=x+0.01;enddx=diff(g)/0.01;dx(1)dx(101)dx(length(g)-1)T2:f1=inline('sqrt(cos(t.2)+4*sin(2*t).2+1)')f2=inline('log(1+x)./(1+x.2)')I1=quad(f1,0,2*pi)I2=quad(f2,0,2*pi)T3:A=6,5,-2,5;9,-1,4,-1;3,4,2,-2;3,-9,0,2;B=-4,13,1,11'x1=ABL,U=lu(A);x2=U(LB)Q,R=qr(A);x3=R(QB)fun:function F=fun(X);x=X(1);y=X(2);z=X(3);F(1)=sin(x)+y2+log(z)-7;F(2)=3*x+2y-z3+1;F(3)=x+y+z-5;T5:f=inline('3*x+sin(x)-exp(x)');fzero(f,1.5)X=fsolve('fun',1 1 1',optimset('Display','off')实验十:T1:x=sym('6');y=sym('5');z=(x+1)/(sqrt(3+x)-sqrt(y)T2:syms x y; t=sym('5135');factor(x4-y4)factor(t)T3:syms beta1 beta2 x;simplify(sin(beta1)*cos(beta2)-cos(beta1)*sin(beta2)simplify(4*x2+8*x+3)/(2*x+1)T4:syms a b c d e f g h k;p1=0,1,0;1,0,0;0,0,1;p2=1,0,0;0,1,0;1,0,1;A=a,b,c;d,e,f;g,h,k;B=p1*p2*AB1=inv(B)B1*B;tril(B)det(B)T5:syms x t a y f1=(x*(exp(sin(x)+1)-2*(exp(tan(x)-1)/sin(x)3;limit(f1,x,0) %(1)f2=(sqrt(pi)-sqrt(acos(x)/sqrt(x+1);limit(f2,x,-1,'right') %(2)f3=(1-cos(2*x)/x;diff(f3,x,1) %(3)diff(f3,x,2)A=ax,t3;t*cos(x),log(x);diff(A,x,1) &(4)diff(A,t,2)diff(diff(A,x,1),t,1)f5=(x2-2*x)*exp(-x2-y2-x*y);yx=-diff(f5,x,1)/diff(f5,y,1) %(5)fxy=diff(diff(f5,x,1),y,1)x=sym('0');y=sym('1');eval(fxy)T6:x=sym('x');f1=1/(1+x4+x8);int(f1,x)f2=1/(asin(x)2/sqrt(1-x2);int(f2,x)f3=(x2+1)/(x4+1);int(f3,x,0,inf)f4=exp(x)*(1+exp(x)2;int(f4,x,0,log(2)实验十一:T1:syms n x ;S1=symsum(1/(2*n-1),n,1,10)S2=symsum(n2*x(n-1),n,1,inf)S3=symsum(n2/5n,n,1,inf)T2:x=sym('x');f=log(x);taylor(f,x,6,1)T3:x1=solve('log(1+x)-5/(1+sin(x)=2')x2=solve('x2+9*sqrt(x+1)-1=0')x3=solve('3*x*exp(x)+5*sin(x)-78.5=0')x4,y4=solve('sqrt(x2+y2)-100=0','3*x+5*y-8=0')T4:dsolve('D2y+4*Dy+29*y=0','y(0)=0','Dy(0)=15','x')T5:x,y,z=dsolve('Dx=2*x-3*y+3*z','Dy=4*x-5*y+3*z','Dz=4*x-4*y+2*z','t')第 21 页-