MATLAB程序设计与应用刘卫国编课后实验答案81653
MATLAB程序设计与应用 实验答案 第六章 刘卫国
1.(1)A=randn(10,5)A =-0.4326 -0.1867 0.2944 -0.3999 -1.6041 -1.6656 0.7258 -1.3362 0.6900 0.2573 0.1253 -0.5883 0.7143 0.8156 -1.0565 0.2877 2.1832 1.6236 0.7119 1.4151 -1.1465 -0.1364 -0.6918 1.2902 -0.8051 1.1909 0.1139 0.8580 0.6686 0.5287 1.1892 1.0668 1.2540 1.1908 0.2193 -0.0376 0.0593 -1.5937 -1.2025 -0.9219 0.3273 -0.0956 -1.4410 -0.0198 -2.1707 0.1746 -0.8323 0.5711 -0.1567 -0.0592 B=mean(A)B =0.0013 0.2310 0.0253 0.3588 -0.4197 C=std(A)C =0.9034 0.8829 1.1898 0.7832 1.0821 (2)D=max(max(A))D =2.1832E=min(min(A))E =-2.1707(3)F=sum(A,2)F =-2.3288-1.32870.01056.2215-1.48953.36024.9201-3.6964-3.3998-0.3025G=sum(sum(A))G =1.9666(4)H=sort(A)H =-1.6656 -0.8323 -1.5937 -1.2025 -2.1707-1.1465 -0.5883 -1.4410 -0.3999 -1.6041-0.4326 -0.1867 -1.3362 -0.1567 -1.0565-0.0376 -0.1364 -0.6918 -0.0198 -0.92190.1253 -0.0956 0.2944 0.6686 -0.80510.1746 0.0593 0.5711 0.6900 -0.05920.2877 0.1139 0.7143 0.7119 0.21930.3273 0.7258 0.8580 0.8156 0.25731.1892 1.0668 1.2540 1.1908 0.52871.19092.1832 1.6236 1.2902 1.4151L=-sort(-A,2,'descend')L =-1.6041 -0.4326 -0.3999 -0.1867 0.2944-1.6656 -1.3362 0.2573 0.6900 0.7258-1.0565 -0.5883 0.1253 0.7143 0.81560.2877 0.7119 1.4151 1.6236 2.1832-1.1465 -0.8051 -0.6918 -0.1364 1.29020.1139 0.5287 0.6686 0.8580 1.19090.2193 1.0668 1.1892 1.1908 1.2540-1.5937 -1.2025 -0.9219 -0.0376 0.0593-2.1707 -1.4410 -0.0956 -0.0198 0.3273-0.8323 -0.1567 -0.0592 0.1746 0.57112.(1)x=[0 15 30 45 60 75 90];y=[0 0.2588 0.5000 0.7071 0.8660 0.9659 1.0000];x1=0:90;y1=interp1(x,y,x1,'spline')y1 =Columns 1 through 110 0.0175 0.0349 0.0524 0.0698 0.0872 0.1045 0.1219 0.1392 0.1564 0.1737Columns 12 through 220.1908 0.2079 0.2249 0.2419 0.2588 0.2756 0.2923 0.3090 0.3255 0.3420 0.3583Columns 23 through 330.3746 0.3907 0.4067 0.4226 0.4384 0.4540 0.4695 0.4848 0.5000 0.5150 0.5299Columns 34 through 440.5446 0.5592 0.5736 0.5878 0.6018 0.6157 0.6293 0.6428 0.6561 0.6691 0.6820Columns 45 through 550.6947 0.7071 0.7193 0.7313 0.7431 0.7547 0.7660 0.7771 0.7880 0.7986 0.8090Columns 56 through 660.8191 0.8290 0.8387 0.8480 0.8571 0.8660 0.8746 0.8829 0.8910 0.8987 0.9062Columns 67 through 770.9135 0.9204 0.9271 0.9335 0.9396 0.9454 0.9510 0.9563 0.9612 0.9659 0.9703Columns 78 through 880.9744 0.9782 0.9817 0.9849 0.9878 0.9904 0.9927 0.9946 0.9963 0.9977 0.9987Columns 89 through 910.9995 0.9999 1.0000x=[0 15 30 45 60 75];y=[0 0.2679 0.5774 1.0000 1.7320 3.7320];x1=0:75;y1=interp1(x,y,x1,'spline')y1 =Columns 1 through 110 0.0184 0.0365 0.0545 0.0724 0.0902 0.1079 0.1255 0.1431 0.1607 0.1784Columns 12 through 220.1961 0.2138 0.2317 0.2497 0.2679 0.2863 0.3048 0.3236 0.3427 0.3620 0.3817Columns 23 through 330.4017 0.4221 0.4429 0.4641 0.4858 0.5079 0.5305 0.5537 0.5774 0.6017 0.6266Columns 34 through 440.6520 0.6780 0.7046 0.7317 0.7593 0.7876 0.8163 0.8456 0.8754 0.9058 0.9367Columns 45 through 550.9681 1.0000 1.0325 1.0658 1.1003 1.1364 1.17431.2145 1.2572 1.3028 1.3516Columns 56 through 661.4041 1.4604 1.5211 1.5863 1.6565 1.7320 1.8131 1.9002 1.99362.0937 2.2008Columns 67 through 762.3152 2.4374 2.5675 2.7060 2.85323.0095 3.17523.3506 3.5361 3.7320x=[0 15 30 45 60 75 90];y=[0 0.2588 0.5000 0.7071 0.8660 0.9659 1.0000];y1=polyfit(x,y,5)y1 =0.0000 0.0000 -0.0000 0.0000 0.0174 0.0000x=[0 15 30 45 60 75];y=[0 0.2679 0.5774 1.0000 1.7320 3.7320];y1=polyfit(x,y,5)y1 =0.0000 -0.0000 0.0000 -0.0010 0.0245 0.0000(2)x=[1 4 9 16 25 36 49 64 81 100];y=1:10;x1=1:100;y1=interp1(x,y,x1,'cubic')y1 =Columns 1 through 111.0000 1.3729 1.71252.0000 2.2405 2.4551 2.64942.82923.0000 3.1636 3.3186Columns 12 through 223.4661 3.6069 3.7422 3.87294.0000 4.1237 4.24354.3599 4.4730 4.5832 4.6907Columns 23 through 334.7958 4.89885.0000 5.0993 5.1966 5.2921 5.38575.4777 5.5681 5.6570 5.7446Columns 34 through 445.8309 5.91606.0000 6.0829 6.1647 6.2454 6.32496.4035 6.4810 6.5577 6.6334Columns 45 through 556.7082 6.7823 6.8556 6.92817.0000 7.0712 7.14167.2113 7.2804 7.3487 7.4164Columns 56 through 667.4835 7.5500 7.6159 7.6812 7.7459 7.8102 7.8739 7.9372 8.0000 8.0623 8.1242Columns 67 through 778.1855 8.2464 8.3068 8.3668 8.4263 8.4854 8.5441 8.6024 8.6603 8.7178 8.7749Columns 78 through 888.8317 8.8881 8.9442 9.0000 9.0555 9.1107 9.16559.2201 9.2744 9.3284 9.3821Columns 89 through 999.4354 9.4884 9.5412 9.5935 9.6456 9.6973 9.7486 9.7996 9.8502 9.9005 9.9505Column 10010.00003.xi=[165 123 150 123 141];yi=[187 126 172 125 148];P=polyfit(xi,yi,3)P =1.0e+003 *-0.0000 0.0013 -0.1779 8.4330线性拟合曲线为:p(x)=1.3x^2—177.9x+84334.(1)P1=[0,3,2];P2=[5,-1,2];P3=[1,0,-0.5];P=conv(P1,conv(P2,P3))P =0 15.0000 7.0000 -3.5000 0.5000 -2.0000 -2.0000 (2)P1=[0,3,2];P2=[5,-1,2];P3=[1,0,-0.5];P=conv(P1,conv(P2,P3));Y=roots(P)Y =0.70710.1000 + 0.6245i0.1000 - 0.6245i-0.7071-0.6667(3)P1=[0,3,2];P2=[5,-1,2];P3=[1,0,-0.5];P=conv(P1,conv(P2,P3));k=0:10;xi=0.2*k;Y=polyval(P,xi)Y =-2.0000 -2.3920 -2.6112 -1.7024 2.7104 15.0000 42.1120 94.1408 184.9056 332.5264 560.00005.(1)[U,fmin]=fminsearch('xiti651',[1,1])U =1.0e-004 *-0.0675 0.1715fmin =1.9920e-010(2)f=inline('-sin(x)-cos(x.^2)');fminbnd(f,0,pi)ans =0.73106.(1)x=[pi/6 pi/4 pi/3 pi/2];f=inline('sin(x).^2+cos(x).^2');dx=diff(f([x,5*pi/12]))/(pi/12)dx =0 0 0 0(2)x=1:3;f=inline('sqrt(x.^2+1)');dx=diff(f([x,4]))dx =0.8219 0.9262 0.96087.(1)g=inline('sin(x).^5.*sin(5*x)');I=quadl(g,0,pi)I =0.0982(2)g=inline('(1+x.^2)./(1+x.^4)');I=quad(g,-1,1)I =2.2214(3)g=inline('(x.*sin(x))./(1+cos(x).^2)'); I=quadl(g,0,pi)I =2.4674(4)f=inline('abs(cos(x+y))');dblquad(f,0,pi,0,pi)ans =6.28329.(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=A\bx =-1.8060-0.53733.0448矩阵分解法:A=[2,3,5;3,7,4;1,-7,1];b=[10;3;5];[Q,R]=qr(A);x=R\(Q\b)x =-1.8060-0.53733.0448(2)矩阵求逆法:A=[5,1,-1,0;1,0,3,-1;-1,-1,0,5;0,0,2,4]; b=[1;2;3;-1];x=inv(A)*bx =1.4000-5.90000.1000-0.3000矩阵除法:A=[5,1,-1,0;1,0,3,-1;-1,-1,0,5;0,0,2,4]; b=[1;2;3;-1];x=A\bx =1.4000-5.90000.1000-0.3000矩阵分解法:A=[5,1,-1,0;1,0,3,-1;-1,-1,0,5;0,0,2,4];b=[1;2;3;-1];[Q,R]=qr(A);x=R\(Q\b)x =1.4000-5.90000.1000-0.300010.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.50001.0000 00 1.00000 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.x=fsolve('xiti612',[0.5,0.5],optimset('Display','off')) x =0.5000y =0.5000f =-0.0509f =-0.0509 0.1173 x =0.5000y =0.5000f =-0.0509f =-0.0509 0.1173 x =0.5000y =0.5000f =-0.0509f =-0.0509 0.1173 x =0.6459y =0.3739f =0.0055f =0.0055 0.0044 x =0.6459y =0.3739f =0.0055f =0.0055 0.0044 x =0.6459y =0.3739f =0.0055f =0.0055 0.0044 x =0.6355y =0.3734f =1.9417e-005f =1.0e-004 *0.1942 0.2589 x =0.6355y =0.3734f =1.9425e-005f =1.0e-004 *0.1942 0.2589x =0.6355y =0.3734f =1.9419e-005f =1.0e-004 *0.1942 0.2591x =0.6354y =0.3734f =2.3754e-010f =1.0e-009 *0.2375 0.2957x =0.6354y =0.3734f =7.9432e-009f =1.0e-008 *0.7943 0.5602x =0.6354y =0.3734f =1.8684e-009f =1.0e-007 *0.0187 0.1936x =0.6354 0.373413.x0=0;xf=20;y1=0;[x,y]=ode45('xiti6131',[x0,xf],y1) x =0.50001.00001.50002.00002.50003.00003.50004.00004.50005.00005.50006.00006.50007.00007.50008.00008.50009.00009.500010.000010.500011.000011.500012.000012.500013.000013.500014.000014.500015.000015.500016.000016.500017.000017.500018.000018.500019.000019.500020.0000 y =NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN(2)x0=0;xf=20;[x,y]=ode45('xiti6132',[x0,xf],[-3,2])[x,y]此题结果过长,疑似错误或不会做。
matlab程序设计与应用(第二版) 刘卫国 课后实验报告
0 0
0 0
4.0000 0
0 9.0000
A,H,Trace,Rank,Norm 结果 H = -3.2509e+004 Trace =33 Rank =5 Norm =25.3208 4、 A=[-29,6,18;20,5,12;-8,8,5]; [V,D]=eig(A); 结果 A = -29 20 -8 5、 5 8 6 18 12 5
0 0 2、 H=hilb(5); P=pascal(5); Hh=det(H); Hp=det(P); Th=cond(H); Tp=cond(P); H,P,Hh,Hp,Th,Tp 结果 Hh = 3.7493e-012 Hp =1 Th = 4.7661e+005 Tp =8.5175e+003 3、 A=fix(10*rand(5)); H=det(A); Trace=trace(A); Rank=rank(A); Norm=norm(A);
b=?1 ans =1 x1 =0.6180 r10 =0.6180 r20 =-1.6180 4、 for i=1:100 if i==1 f(i)=1; elseif i==2 f(i)=0; elseif i==3 f(i)=1; else f(i)=f(i-1)-2*f(i-2)+f(i-3); end end a=max(f); b=min(f); c=sum(f); disp(['最大值',num2str(a)]); disp(['最小值',num2str(b)]); disp(['各数之和',num2str(c)]); zhengshu=length(find(f>0)); zeroshu=length(find(f==0)); fushu=length(find(f<0)); 结果 最大值;4.377633e+011 最小值:-8.994121e+011 各数之和:-7.427456e+011 zhengshu =49 zeroshu = 2 fushu =49 5、 s=0;
MATLAB程序设计与应用 刘卫国主编 高等教育出版社课后答案
y='A'; elseif x>=80
y='B'; elseif x>=70
y='C'; elseif x>=60
y='D'; else
y='E'; end y
3:
x=input('请输入工作小时数');
天天learn
if 84*x-700<=0 y=0;
1、 A=rand(100,300)
(1) mean(A) std(A,0,1);
(2) [B,C]=max(A) [E,F]=max(B)
[B1,C1]=min(A)
(3) Length(find(A>0.5))/30000
[E1,F2]=min(B1)
2、 P=45+(95-45)*rand(100,5) (1) [Y,U]=max(P) [Y1,U1]=min(P) (2) mean(P) std(P,0,1) (3) K=sum(P,2) [E,F]=max(K) [E1,F1]=min(K) (4) [zcj,xsxh]=-sort(-K,1)
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
《MATLAB_程序设计与应用》刘卫国高等教育出版社-第4章课后答案
第四章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*b+100*c+10*d+e;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)if语句法:a=input('请输入a: ');b=input('请输入b: ');c=input('请输入c: ');for x=0.5:1:5.5if x>=0.5 & x<1.5y=a.*(x.^2)+b.*x+celseif x>=1.5 & x<3.5y=a*(sin(b)^c)+xelseif x>=3.5 & x<5.5y=log(abs(b+c./x))endendswitch语句法:a=input('请输入a: ');b=input('请输入b: ');c=input('请输入c: ');for x=0.5:1:5.5switch floor(x/0.5)case {1,2}y=a.*(x.^2)+b.*x+c;case {3,4,5,6}y=a*(sin(b)^c)+x;case {7,8,9,10}y=log(abs(b+c./x)); enddisp(y)end3.x=fix(rand(1,20)*89)+10;x1=mean(x);n=find(rem(x,2)==0 & x<x1);disp(['小于平均数的偶数是:',num2str(x(n))]);4.(1)A=input('请输入20个数的一个行向量:');a=A(1);b=A(1);for m=Aif a>=ma=m;elseif b<=mb=m;endenddisp(['最小数是:',num2str(a)])disp(['最大数是:',num2str(b)])(2)A=input('请输入20个数的一个行向量:'); maxval=max(A)minval=min(A)5.s=0;for a=0:63c=2^a;s=s+c;enddisp(['2的0次方到63次方的和是:',num2str(s)])k=0:63n=2.^ks=sum(n)6.(1)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)])(2)sum1=0;n1=0;for n=1:2:100x=(-1)^n1*(1/n);sum1=sum1+x;n1=n1+1;enddisp(['当n取100时: sum=',num2str(sum1)])sum2=0;n2=0;for n=1:2:1000x=(-1)^n2*(1/n);sum2=sum2+x;n2=n2+1;enddisp(['当n取1000时: sum=',num2str(sum2)])sum3=0;n3=0;for n=1:2:10000x=(-1)^n3*(1/n);sum3=sum3+x;n3=n3+1;enddisp(['当n取10000时:sum=',num2str(sum3)])(3)sum1=0;for n=1:100x=1/(4^n);sum1=sum1+x;enddisp(['当n取100时: sum=',num2str(sum1)])sum2=0;for n=1:1000x=1/(4^n);sum2=sum2+x;enddisp(['当n取1000时: sum=',num2str(sum2)])sum3=0;for n=1:10000x=1/(4^n);sum3=sum3+x;enddisp(['当n取10000时:sum=',num2str(sum3)])(4)sum1=1;for n=1:100x=4*n*n/(2*n-1)/(2*n+1);sum1=sum1*x;enddisp(['当n取100时: sum=',num2str(sum1)])sum2=1;for n=1:1000x=4*n*n/(2*n-1)/(2*n+1);sum2=sum2*x;enddisp(['当n取1000时: sum=',num2str(sum2)])sum3=1;for n=1:10000x=4*n*n/(2*n-1)/(2*n+1);sum3=sum3*x;enddisp(['当n取10000时:sum=',num2str(sum3)])7.函数文件function f=fibnacci(n)if n==1 | n==2f=1;elsef=fibnacci(n-1)+fibnacci(n-2);end命令文件:shulie=[];for k=1:nshulie=[shulie fibnacci(k)];endshulie8.function [f1,f2]=juzhenji(x1,x2)f1=x1*x2;f2=x1.*x2;命令文件:clear alla=input('请输入一个矩阵:');b=input('请再输入一个矩阵:(注意:两矩阵要可以相乘)'); [f1,f2]=juzhenji(a,b);disp(f1)disp(f2)9.function sum=qiuhe(n,m)if n<=1sum=0;elsesum=n^m+qiuhe(n-1,m);end命令文件:clear ally=qiuhe(100,1)+qiuhe(50,2)+qiuhe(10,-1);disp(y)10.s=0;a=[12,13,14;15,16,17;18,19,20;21,22,23];for k=afor j=1:4if rem(k(j),2)~=0 s=s+k(j);endendendss =108(2)global xx=1:2:5;y=2:2:6;sub(y);xyfunction fun=sub(z) global xz=3*x;x=x+z;x =4 12 20 y =2 4 6。
matlab程序设计与应用刘卫国编课后实验答案8
实验一 MATLAB 运算基础1. 先求下列表达式的值,然后显示MATLAB 工作空间的使用情况并保存全部变量。
(1) 0122sin851z e =+ (2) 221ln(1)2z x x =++,其中2120.455i x +⎡⎤=⎢⎥-⎣⎦ (3) 0.30.330.3sin(0.3)ln , 3.0, 2.9,,2.9,3.022a a e e a z a a --+=++=--L (4) 2242011122123t t z t t t 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 =0.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.1416i-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.5978 Columns 33 through 36-1.3575 -1.1531 -0.9723 -0.8083 Columns 37 through 40-0.6567 -0.5151 -0.3819 -0.2561 Columns 41 through 44-0.1374 -0.0255 0.0792 0.1766 Columns 45 through 480.2663 0.3478 0.4206 0.4841 Columns 49 through 520.5379 0.5815 0.6145 0.6366 Columns 53 through 560.6474 0.6470 0.6351 0.61190.5777 0.5327 0.4774 0.4126 Column 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 -10 46 7 105 21 53 49 ans =12 31 -3 32 8 84 0 67 1 ans =68 44 62309 -72 596154 -5 241ans =12 102 468 0 2619 -130 49ans =37226 233824 48604 247370 149188 600766 78688 454142 118820 ans =1728 39304 -64 39304 343 658503 27 274625 343 ans =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 -1 34 7 87 2 0 3 3 65 7 3 -2 7 ans =12 34 -43 65 74 5 111 0 1920 -5 403. 设有矩阵A和B(1) 求它们的乘积C。
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. 先求下列表达式的值,然后显示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运算基础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程序设计教程[第二版]刘卫国课后参考答案及解析
Matlab程序设计教程[第二版]刘卫国课后参考答案及解析第二章1 求下列表达式的值。
(1)w=sqrt(2)*(1+0.34245*10^(-6))(2)a=3.5;b=5;c=-9.8;x=(2*pi*a+(b+c)/(pi+a*b*c)-exp(2))/tan(b+c)+a(3)a=3.32;b=-7.9;y=2*pi*a^(2)*[(1-pi/4)*b-(0.8333-pi/4)*a](4)t=[2,1-3*i;5,-0.65];z=1/2*exp(2*t)*log(t+sqrt(1+t^(2)))2 求下列表达式A=[-1,5,-4;0,7,8;3,61,7];B=[8,3,-1;2,5,3;-3,2,0];(1)A+6*B A^2-B+eye(2)A*B A.*B B.*A(3)A/B B\A(4)[A,B] [A([1,3],:);B^2]3 根据已知,完成下列操作(1)A=[23,10,-0.778,0;41,-45,65,5;32,5,0,32;6,-9.54,54,3.14];K=find(A>10&A<25);A(K)(2)A=[23,10,-0.778,0;41,-45,65,5;32,5,0,32;6,-9.54,54,3.14];B=A(1:3,:) C=A(:,1:2) D=A(2:4,3:4) E=B*C(3)E<="">第三章1 从键盘输入一个3位数,将它反向输出,如输入639,出数936。
f=input('输入一个数:','s');f(end :-1:1)2 用if语句score=input('请输入成绩:');if score>=90&&score<=100disp('A');elseif score>=80&&score<=89disp('B');elseif score>=70&&score<=79disp('C');elseif score>=60&&score<=69;disp('D');elseif score<60&&score>=0;disp('E');elsedisp('出错');end用switch语句score=input('请输入成绩:');switch fix(score/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');otherwisedisp('出错');end第四章1题1) X=0:10;Y=x-x.^3/6;P lot(x,y)2) t=0:0.01:2*pi;x=8.*cos(t);y=4*sqrt(2).*sin(t);plot(x,y)2题M文件:t=-pi:pi/10:pi;y=1./(1+exp(-t));subplot(2,2,1);bar(t,y,'b');title('bar(t,y,''b'')');axis([-5,5,-3,3]); subplot(2,2,2);stairs(t,y,'k');title('stairs (t,y,''k'')');axis([-5,5,-3,3]); subplot(2,2,3);stem(t,y,'m');title('stem (t,y,''m'')');axis([-5,5,-3,3]); subplot(2,2,4);loglog(t,y,'g');title('loglog(t,y,''g'')');axis([-5,5,-3,3]);3题1)t=0:pi/100:2*pi;y=5*cos(t)+4;polar(t,y,'-*')2)t=-pi/3:pi/100:pi/3;r=5*sin(t).^2./cos(t);polar(t,r,'-*')4题1)t=0:pi/100:2*pi;x=exp(-t/20).*cos(t);y=exp(-t/20).*sin(t);z=t;plot3(x,y,z)第五章:1题A=randn(10,5)1) X=mean(A)Y=std(A,0,1)2) max(max(A))min(min(A))3) B=sum(A,2)sum(B)4) sort(A);sort(A,2,'descend')2题1)t=0:15:90;x1=[0,0.2588,0.5000,0.7071,0.8660,0.9659,1.0000];a1=0:1:90;y1=interp1(t,x1,a1,'spline')x2=[0,0.2679,0.5774,1.0000,1.7320,3.7320,NaN]; a2=0:1:75; y2= interp1(t,x2,a2,'spline')p1=polyfit(t,x1,5);z1=polyval(p1,a1)p2=polyfit(t,x2,5);z2=polyval(p2,a2)4题P=[2,-3,5,13];Q=[1,5,8];p=polyder(P)p1=polyder(P,Q)[p,q]= polyder(P,Q)5题P1=[1,2,4,0,5];P2=[1,0];P3=[1,2,3];1) P4=conv(P2,P3)P4=[0,1,2,3,0];P=P1+P42) x=roots(P)3) A=[-1,1.2,-1.4;0.75,2,3.5;0,5,2.5];Y=polyval(P,A)4) Z=polyvalm(P,A)第8章1.分解因式. (2)factor(sym(‘5135’))(1)syms A x y;A=x^4-y^4;factor(A)2.求函数的极限(1)syms x; (2)syms x;limit((x-2)/(x^2-4),x,2)f=(sqrt(pi)-sqrt(acos(x)))/sqrt(x+1);limit(f,x,-1,'right')3.求函数的符号导数。
Matlab程序设计与应用第二版刘卫国课后实验答案
Matlab 程序设计与应用第二版刘卫国课后实验答案实验一: C=A*BT1: D=C(3:5,2:3)T4-1: %%第一小题z1=2*sin(85*pi/180)/(1+exp(2)) a=100:999; %% 第二小题b=find(rem(a,21)==0); x=[2,1+2i;-0.45,5]; c=length(b)z2=1/2*log(x+sqrt(1+x42)); T4-2:z2 a=input(' 请输入一个字符串:','s'); %% 第三小题b=find(a>='A'&a<='Z'); a=-3.0:0.1:3.0; a(b)=[];z3=1/2*(exp(0.3*a)-exp(-0.3*a)).*sin(a+0.3)+ldisp(a);og((0.3+a)/2) 实验二:%%第四题T1:t=0:0.5:2.5 E=eye(3),R=rand(3,2),O=zeros(2,3),S=diag([1,2z4=(t>=0&t<1).*(t.A2)+(t>=1 &t<2).*(t.A2-1)+(t]);>=2&t<3)*(t.A2-2*t+1) A=[E,R;O,S]disp('A A2='); T2: disp(A A2); A=[12,34,-4;34,7,87;3,65,7] disp('[E,R+RS;O,S A2]'); B=[1,3,-1;2,0,3;3,-2,7] B=[E,R+R*S;O,SA2] disp ('A+6*B='); T2:disp(A+6*B); H=hilb(5)disp('A-B+I='); P=pascal(5) disp(A-B+eye(3)); Hh=det(H)disp('A*B='); Hp=det(P)disp(A*B); Th=cond(H) disp('A.*B='); Tp=cond(P) disp(A.*B);a=abs(Th-1); disp('AA3='); b=abs(Tp-1); disp(AA3); if a>bdisp('A43='); disp(' 帕萨卡矩阵P 性能更好');disp(A43); elseif a<bdisp('A/B='); disp(' 希尔伯特矩阵H性能更好');disp(A/B); elsedisp('B\A='); disp(' 两个矩阵性能相同'); disp(B\A); end disp('[A,B]='); T3:disp([A,B]); a=1:25;disp('[A([1,3],:);B A2]='); A=reshape(a,5,5) disp([A([1,3],:);B A2]);disp(' 行列式的值:'); T3: disp(det(A)); z=1:25; disp(' 矩阵的秩:');A=reshape(z,5,5)'; disp(rank(A)); B=[3,0,16;17,-6,9;0,23,- 4;9,7,0;4,13,11]; disp(' 矩阵的迹:');1disp(trace(A)); enddisp(' 矩阵的范数:'); if (s>=90&s<=100);disp('A'); disp(norm(A));T4: elseif(s>=80&s<90); A=[-29,6,18;20,5,12;-8,8,5] disp('B'); [V,D]=eig(A)elseif(s>=70&s<80); T5: disp('C'); A=[1/2,1/3,1/4;1/3,1/4,1/5;1/4,1/5,1/6]elseif(s>=60&s<70); B=[0.95,0.67,0.52]' disp('D'); X1=A\B elseB(3)=0.53 disp('E'); X2=A\B enddisp('系数矩阵A的条件数:');T2-switch:disp(cond(A)); s=input('please enter the score:');T6: while (s<0||s>100) a=1:25; disp('the score is not reasonable');A=reshape(a,5,5) s=input('please enter the score:');disp('B1=sqrtm(A)'); endB1=sqrtm(A) switch fix(s/10) disp('B2=sqrt(A)'); case{9,10}B2=sqrt(A) disp('A');disp('B1*B1'); case{8}B1*B1 disp('B');disp('B2.*B2'); case{7}B2.*B2 disp('C');实验三: case{6}T1: disp('D');x=-5.0:2:5.0 case{0,1,2,3,4,5} for i=1:length(x); disp('E');if (x(i)<0)&(x(i)~=-3) endy(i)=x(iF2+x(i)-6; T3:elseif t=input(' 请输入工时time=');(x(i)>=0)&(x(i)<5)&(x(i)~=2)&(x(i)~=3); if t>120 y(i)=x(i)A2-5*x(i)+6; w=120*84+(t-120)*1.15*84;else elseif t<60y(i)=x(i)A2-x(i)-1; w=t*84-700;end elseend w=84*t; y endT2-if: disp(' 应发工资为:'); s=input('please enter the score:');disp(w);while (s<0||s>100) T4:disp('the score is not reasonable'); a=10+floor(rand(1)*89)s=input('please enter the score:'); b=10+floor(rand(1)*89)2s=i nput('请输入+ 或-或* 或/','s'); for i=1: n;while(s~='+'&&s~='-'&&s~='*'&&s~='/') y=y+1/(2*i-1);if y>=3 disp(' 输入的符号错误,请重新输入');s=input('请输入+ 或-或*或/','s'); disp(' 最大n 值为:');end i-1switch s disp(' 对应的y 值为:');case{'+'} y-1/(2*i-1)c=a+b; break;case{'-'} endend c=a-b;T3: case{'*'}c=a*b; format longa=input(' 请输入a:'); case{'/'}b=input(' 请输入b:'); c=a/b;x(1)=a/(1.0+b);i=1;x(2)=a/(x(i)+b);end while (abs(x(i+1)-x(i))>0.00001&i<500)i=i+1; cx(i+1)=a/(x(i)+b); T5:A=rand(5,6) endn=input('请输入n:'); r1=(-b+sqrt(b A2+4*a))/2;r2=(-b-sqrt(b A2+4*a))/2; while (n <1)disp(['x',num2str(i+1),'=',num2str(x(i+1),8)]); disp(' 输入的n 有误,请重新输入');n=input(' 请输入n:'); disp(['x',num2str(i),'=',num2str(x(i),8)]);end disp(['r1=',num2str(r1,8),',r2=',num2str(r2,8)])if n>5 ;B=A(5,:) format shortelse T4:B=A(n,:) f(1)=1;f(2)=0;f(3)=1;i=4; End while(i<=100) f(i)=f(i-1)-实验四: 2*f(i-2)+f(i-3);T1: i=i+1;n=input(' 请输入n:'); endm=0; fmax=max(f)for i=1:n; fmin=min(f)m=m+1/i A2; fsum=su m(f)end fp=length(find(f>0)) pii=sqrt(6*m) fn=length(find(f<0)) T1-2:f0=length(find(f==0)) n=input(' 请输入n:'); T5:m=1./(1:n).A2; j=0;s=0;s=sum(m); for i=3:50pii=sqrt(6*s) m=i*(i-1)-1; T2: if isprime(m)==1 n=1000;y=0; m3s=s+m; s(n)=10*rem(m(t),10)+fix(m(t)/10); %j=j+1; 挑选出的素数进行位置交换if isprime(s(n))==1 % 再挑选出交换endend 绝对素数disp([' 亲密数的对数:',num2str(j)]); r(k)=m(t);disp([' 所有亲密素数之和:',num2str(s)]); k=k+1;实验五: endfn1: n=n+1;function f=fn1(n) endf=n+10*log( n. A2+5); disp(['所有两位绝对素数:',num2str(r)]) fn2: T5: function f=fn2(n) y1=fn1(40)/(fn1(30)+fn1(20)) % 函数调用f=0;y2=fn2(40)/(fn2(30)+fn2(20)) for i=1:n 实验六:f=f+i*(i+1); T1:end x=linspace(0,2*pi,101); fx: y=(0.5+3*sin(x)./(1+x.A2)).*cos(x);function f=fx(x) plot(x,y);f=1./((x-2).A2+0.1)+1./((x-3).A4+0.01); T2:T1: x=linspace(0,2*pi,100); x=input(' 请输入一个复数:'); y1=x.A2;fe=exp(x) y2=cos(2*x);flg=log(x) y3=y1.*y2;fsin=sin(x) subplot(2,2,1);fcos=cos(x) plot(x,y1,'r:',x,y2,'y-',x,y3,'b-.'); T2: subplot(2,2,2);m1=input(' 请输入m1:'); plot(x,y1,'r:');m2=input(' 请输入m2:'); subplot(2,2,3);t=input(' 请输入theta:'); plot(x,y2,'y-');A=[m1*cos(t),-m1,-sin(t),0;m1*sin(t),0,... subplot(2,2,4);cos(t),0;0,m2,-sin(t),0;0,0,-cos(t),1] plot(x,y3,'b-.');B=[0,m1*9.8,0,m2*9.8]' T2-3:disp('[a1,a2,N1,N2]='); x=linspace(0,2*pi,10); disp([A\B]'); y1=x.A2;T3: y2=cos(2*x);j=1;n=1;k=1; y3=y1.*y2;for i=10:99 % 挑选出1 0~99中所subplot(3,4,1);有的素数bar(x,y1,'r');axis([0,7,0,40]);if isprime(i)==1 subplot(3,4,2);m(j)=i; stairs(x,y1,'y');axis([0,7,0,40]);j=j+1; subplot(3,4,3);end stem(x,y1,'b');axis([0,7,0,40]); end subplot(3,4,4);for t=1:length(m); fill(x,y1,'g');axis([0,7,0,40]);4subplot(3,4,5); theta=0:0.001:2*pi; bar(x,y2,'r');axis([-1,7,-1,1]); y=a.*sin(b+n.*theta);polar(theta,y); subplot(3,4,6);stairs(x,y2,'y');axis([-1,7,-1,1]); T5:subplot(3,4,7); x=linspace(-5,5,21); stem(x,y2,'b');axis([-1,7,-1,1]); y=linspace(0,10,31); subplot(3,4,8); [x,y]=meshgrid(x,y);fill(x,y2,'g');axis([-1,7,-1,1]); z=cos(x).*cos(y).*exp(- sqrt(x.A2+y.A2)/4);subplot(3,4,9); subplot(1,2,1); bar(x,y3,'r');axis([0,7,-30,50]); surf(x,y,z);subplot(3,4,10); subplot(1,2,2); stairs(x,y3,'y');axis([0,7,-30,50]); contour3(x,y,z); subplot(3,4,11); T6:stem(x,y3,'b');axis([0,7,-30,50]); s=0:pi/2;subplot(3,4,12); t=0:3*pi/2; fill(x,y3,'g');axis([0,7,-30,50]);[s,t]=meshgrid(s,t); T3: x=cos(s).*cos(t); x=linspace(-5,5,100);y=cos(s).*sin(t); y=[]; z=sin(s);for x0=x; surf(x,y,z);if x0<=0; shading interp;y=[y,(x0+sqrt(pi))/exp(2)]; 实验八:else T1:y=[y,1/2*log(xO+sqrt(1+xOA2))]; A=ra nd(1,30000);end disp(' 均值为 :');end mean(A)subplot(1,2,1); disp(' 标准方差为 :'); plot(x,y); std(A) disp(' 最大元素为 :'); max(A)disp(' 最小元素为 :'); for i=1:length(x) min(A) if x(i)<=0 k=find(A>0.5); y(i)=(x(i)+sqrt(pi))/exp(2); b=length(k);else disp(' 大于 0.5 的随机数个数占总数的百分y(i)=1/2*log(x(i)+sqrt(1+x(i)A 2)); 比:');end sprintf('%f%%', b/300) end T2:subplot(1,2,2); P=45+50*rand(100,5) plot(x,y); disp(' b=input(' 请输入 b:'); [Y1,U1]=min(P,[],1) n=input(' 门课平均分 :');mean(P,1) i=i+1;disp(' 每门课的标准方差 :'); x=x+0.01;end std(P,0,1)s=sum(P,2); dx=diff(g)/0.01; disp(' 总分最高分 :'); dx(1)[Y2,U2]=max(s,[],1) dx(101)disp(' 总分最低分 :'); dx(length(g)-1) [Y3,U3]=min(s,[],1) T2:[zcj,xsxh]=sort(s,1,'descend') %descend f1=i nli ne('sqrt(cos(t42)+4*si n(2*t)42+1)')序 f2=i nli ne('log(1+x)丿(1+X.A2)')T3: I1=quad(f1,0,2*pi) h=6:2:18; I2=quad(f2,0,2*pi) 最高分及序号 :');T4: [Y,U]=max(P,[],1) a=input(' 请输入 a:'); disp(' 最低分及序号 :'); 请输入 n:'); disp(' 每t1=[18.0,20.0,22.0,25.0,30.0,28.0,24.0]; T3:t2=[15.0,19.0,24.0,28.0,34.0,32.0,30.0]; A=[6,5,-2,5;9,-1,4,- 1;3,4,2,-2;3,-9,0,2];t=6.5:2:17.5; B=[-4,13,1,11]'; T1=interp1(h,t1,t,'spline') x1=A\BT2=interp1(h,t2,t,'spline') [L,U]=lu(A); T4: x2=U\(L\B)x=1:10:101; [Q,R]=qr(A); y=log10(x); x3=R\(Q\B)p=polyfit(x,y,5) fun:x1=1:0.01:101; function F=fun(X); y1=log10(x1); x=X(1); y2=polyval(p,x1);y=X(2);plot(x1,y1,'r-',x1,y2,'b-.'); z=X(3);T5: F(1)=si n(x)+yA2+log(z)-7; P仁[1,2,4,0,5]; F(2)=3*x+2A y-z A3+1;P2=[0,0,0,1,2]; F(3)=x+y+z-5; P3=[0,0,1,2,3]; T5:P4=conv(P2,P3); f=inline('3*x+sin(x)-exp(x)');sp1=length(P1); fzero(f,1.5) sp2=length(P4); X=fsolve('fun',[1 11]',optimset('Display','off'))P=[zeros(1,sp2-sp1),P1]+P4 实验十:x=roots(P) T1:A=[-1,1.2,-1.4;0.75,2,3.5;0,5,2.5]; x=sym('6'); Ax=polyval(P,A)y=sym('5'); AA=polyvalm(P,A) z=(x+1)/(sqrt(3+x)-sqrt(y)) 实验九: T2: T1: syms x y;f=inlin e('det([x x A2 x A3;1 2*x 3*x A2;0 2 t=sym('5135'); 6*x])'); factor(x A4-y A4) i=1;x=1; factor(t)while x<=3.01 T3:g(i)=f(x); syms beta1 beta2 x;6simplify(sin(beta1)*cos(beta2)-cos(beta1)*sinsyms n x ;(beta2)) S1=symsum(1/(2*n-1),n,1,10)S2=symsum(nA2*xA(n-1),n,1,inf) simplify((4*xA2+8*x+3)/(2*x+1))T4: S3=symsum(nA2/5An,n,1,inf) syms a b c d e f g h k; T2:p1=[0,1,0;1,0,0;0,0,1]; x=sym('x');p2=[1,0,0;0,1,0;1,0,1]; f=log(x);A=[a,b,c;d,e,f;g,h,k]; taylor(f,x,6,1)B=p1*p2*A T3:B1=inv(B) x1=solve('log(1+x)-5/(1+sin(x))=2') B1*B;x2=solve('xA2+9*sqrt(x+1)-1=0') tril(B) x3=solve('3*x*exp(x)+5*sin(x)- 78.5=0') det(B) [x4,y4]=solve('sqrt(xA2+yA2)-100=0','3*x+5*y-T5: 8=0')syms x t a y T4:f1=(x*(exp(sin(x))+1)-2*(exp(tan(x))-1))/sin(x)dsolve('D2y+4*Dy+29*y=0','y(0)=0','Dy(0)=15'A3; ,'x')limit(f1,x,0) %(1) T5:f2=(sqrt(pi)-sqrt(acos(x)))/sqrt(x+1); [x,y,z]=dsolve('Dx=2*x- 3*y+3*z','Dy=4*x-5*y+limit(f2,x,-1,'right') %(2) 3*z','Dz=4*x-4*y+2*z','t') f3=(1-cos(2*x))/x;diff(f3,x,1) %(3)diff(f3,x,2)A=[a A x,t A3;t*cos(x),log(x)];diff(A,x,1) &(4)diff(A,t,2)diff(diff(A,x,1),t,1)f5=(xA2-2*x)*exp(-xA2-yA2-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+xA4+xA8);int(f1,x)f2=1/(asin(x))A2/sqrt(1-xA2);int(f2,x)f3=(xA2+1)/(xA4+1);int(f3,x,0,inf)f4=exp(x)*(1+exp(x))A2;int(f4,x,0,log(2))实验十一:T1: 7。
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解: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 =z2 =z3 =Columns 1 through 4Columns 5 through 8Columns 9 through 12Columns 13 through 16Columns 17 through 20Columns 21 through 24Columns 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 =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]解: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 =ans =ans =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.(2) 将矩阵C的左下角3×2子矩阵赋给D.(3) 查看MATLAB处事空间的使用情况.解:. 运算截止:E=(reshape(1:1:25,5,5))';F=[3 0 16;17 -6 9;0 23 -4;9 7 0;4 13 11]; C= E*FH=C(3:5,2:3)C =93 150 77258 335 237423 520 397588 705 557753 890 717H =520 397705 557890 7174. 完毕下列支配:(1) 供[100,999]之间能被21整除的数的个数.(2) 修坐一个字符串背量,简略其中的大写字母.解:(1) 截止:m=100:999;n=find(mod(m,21)==0);length(n)ans =43(2). 修坐一个字符串背量比圆:ch='ABC123d4e56Fg9';则央供截止是:ch='ABC123d4e56Fg9';k=find(ch>='A'&ch<='Z');ch(k)=[]ch =真验二MATLAB矩阵分解与处理1. 设有分块矩阵33322322E RAO S⨯⨯⨯⨯⎡⎤=⎢⎥⎣⎦,其中E、R、O、S分别为单位矩阵、随机矩阵、整矩阵战对于角阵,试通过数值估计考证22E R RS AO S+⎡⎤=⎢⎥⎣⎦.解: M文献如下;输出截止:S =1 00 2A =0 0 0 1.0000 0a =0 0 0 1.0000 0 ans =0 0 0 0 00 0 0 0 00 0 0 0 00 0 0 0 00 0 0 0 0由ans,所以22E R RS AO S+⎡⎤=⎢⎥⎣⎦2. 爆收5阶希我伯特矩阵H战5阶帕斯卡矩阵P,且供其止列式的值Hh战Hp以及它们的条件数Th战Tp,推断哪个矩阵本能更佳.为什么?解:M文献如下:输出截止:H =P =1 1 1 1 1123d4e56g91 2 3 4 51 3 6 10 151 4 10 20 351 5 15 35 70Hh =Hp =1Th =4.7661e+005Tp =8.5175e+003果为它们的条件数Th>>Tp,所以pascal矩阵本能更佳.3. 修坐一个5×5矩阵,供它的止列式值、迹、秩战范数.解: M文献如下:输出截止为:A =17 24 1 8 1523 5 7 14 164 6 13 20 2210 12 19 21 311 18 25 2 9d =5070000t =65c1 =c2 =cinf =4. 已知供A的特性值及特性背量,并分解其数教意思.解:M文献如图:输出截止为:V =D =-25.3169 0 00 -10.5182 0数教意思:V的3个列背量是A的特性背量,D的主对于角线上3个是A的特性值,特别的,V的3个列背量分别是D的3个特性值的特性背量.5. 底下是一个线性圆程组:(1) 供圆程的解.(2) 将圆程左边背量元素b3改为0.53再供解,并比较b3的变更妥协的相对于变更.(3) 估计系数矩阵A的条件数并分解论断.解: M文献如下:输出截止:X =X2 =C =1.3533e+003由截止,X战X2的值一般,那表示b的微弱变更对于圆程解也做用较小,而A的条件数算得较小,所以数值宁静性较佳,A是较佳的矩阵.6. 修坐A矩阵,试比较sqrtm(A)战sqrt(A),分解它们的辨别.解:M文献如下:运止截止有:A =16 6 1820 5 129 8 5b1 =b2 =b =分解截止知:sqrtm(A)是类似A的数值仄圆根(那可由b1*b1=A的截止瞅出),而sqrt(A)则是对于A中的每个元素启根号,二则辨别便正在于此.真验三采用结构步调安排一、真验手段1. 掌握修坐战真止M文献的要领.2. 掌握利用if语句真止采用结构的要领.3. 掌握利用switch语句真止多分支采用结构的要领.4. 掌握try语句的使用.二、真验真质1. 供分段函数的值.用if语句真止,分别输出x=-5.0,-3.0,1.0,2.0,2.5,3.0,5.0时的y值.解:M文献如下:运算截止有:f(-5)y =14>> f(-3)y =11>> f(1)y =2>> f(2)y =1>> f(2.5)y =>> f(3)y =5>> f(5)y =192. 输进一个百分造结果,央供输出结果等第A、B、C、D、E.其中90分~100分为A,80分~89分为B,79分~79分为C,60分~69分为D,60分以下为E.央供:(1) 分别用if语句战switch语句真止.(2) 输进百分造结果后要推断该结果的合理性,对于分歧理的结果应输出堕落疑息.解:M文献如下试算截止:score=88grade =Bscore=123过失:输进的结果没有是百分造结果3. 硅谷公司职工的人为估计要领如下:(1) 处事时数超出120小时者,超出部分加收15%.(2) 处事时数矮于60小时者,扣收700元.(3) 其余按每小时84元计收.试编程按输进的工号战该号职工的工时数,估计应收人为.解:M文献下4. 安排步调,完毕二位数的加、减、乘、除四则运算,即爆收二个二位随机整数,再输进一个运算标记,干相映的运算,并隐现相映的截止.解:M文献如下;运算截止例:a =38b =33输进一个运算符:^c =falsea =92b =40输进一个运算符:+c =1325. 修坐5×6矩阵,央供输出矩阵第n止元素.当n值超出矩阵的止数时,自动转为输出矩阵末尾一止元素,并给出堕落疑息.解:M文献如下:运算截止如下:输进一个5止6列矩阵A=[1 2 3 4 5 5;2 3 4 5 7 6;2 2 2 2 2 3;11 2 3 9 7 3;2 3 4 5 6 7]输进一正整数n=411 2 3 9 7 3输进一个5止6列矩阵A=[1 2 3 4 5 5;2 3 4 5 7 6;2 2 2 2 2 3;11 2 3 9 7 3;2 3 4 5 6 7]输进一正整数n=62 3 4 5 6 7ans =Error using ==> dispToo many input arguments.真验四循环结构步调安排一、真验手段1. 掌握利用for语句真止循环结构的要领.2. 掌握利用while语句真止循环结构的要领.3. 认识利用背量运算去代替循环支配的要领.二、真验真质1. 根据2222211116123nπ=++++,供π的近似值.当n分别与100、1000、10000时,截止是几?央供:分别用循环结媾战背量运算(使用sum函数)去真止.解:M文献如下:运止截止如下:K>> %循环结构估计pi值y=0;n=input('n=');for i=1:ny=y+1/i/i;endpi=sqrt(6*y)n=100pi =n=1000pi =n=10000pi =%背量要领估计Pi值n=input('n=');i=1./(1:n).^2;s=sum(i);pi=sqrt(6*s)n=100pi =n=1000pi =n=100002. 根据11113521yn=++++-,供:(1) y<3时的最大n值.(2) 与(1)的n值对于应的y值.解:M—文献如下:运止截止如下:K>> y=0;n=0;while y<3n=n+1;y=y+1/(2*n-1);endynif y>3n=n-1;endny =n =57n =563. 思量以下迭代公式:其中a、b为正的教数.(1) 编写步调供迭代的截止,迭代的末止条件为|x n+1-x n|≤10-5,迭代初值x0=1.0,迭代次数没有超出500次.(2) 如果迭代历程支敛于r,那么r(a,b)的值与(1,1)、(8,3)、(10,0.1)时,分别对于迭代截止战准确值举止比较.解:M文献如下:运算截止如下;请输进正数a=1请输进正数b=1x =r =r =s =请输进正数a=8请输进正数b=3x =r =r =s =0.0请输进正数a=10x =r =r =s =4. 已知供f1~f100中:(1) 最大值、最小值、各数之战.(2) 正数、整、背数的个数.解:M—文献以下是运算截止:max(f)=437763282635min(f)=-899412113528sum(f)=-742745601951c1=49c2=2c3=495. 若二个连绝自然数的乘积减1是素数,则称那二个边陲自然数是亲稀数对于,该素数是亲稀素数.比圆,2×3-1=5,由于5是素数,所以2战3是亲稀数,5是亲稀素数.供[2,50]区间内:(1) 亲稀数对于的对于数.(2) 与上述亲稀数对于对于应的所有亲稀素数之战.解:M文献:运算截止为:j =29s =23615真验五函数文献一、真验手段1. 明白函数文献的观念.2. 掌握定义战调用MATLAB函数的要领.二、真验真质1. 定义一个函数文献,供给定复数的指数、对于数、正弦战余弦,并正在下令文献中调用该函数文献.解:M文献如下:函数fushu.M文献:function [e,l,s,c] = fushu(z)%fushu 复数的指数,对于数,正弦,余弦的估计%e 复数的指数函数值%l 复数的对于数函数值%s 复数的正弦函数值%c 复数的余弦函数值e=exp(z);l=log(z);s=sin(z);c=cos(z);下令文献M:z=input('请输进一个复数z=');[a,b,c,d]=fushu(z)运算截止如下:z=input('请输进一个复数z=');[a,b,c,d]=fushu(z)请输进一个复数z=1+ia =b =c =d =2. 一物理系统可用下列圆程组去表示:从键盘输进m1、m2战θ的值,供a1、a2、N1战N2的值.其中g与9.8,输进θ时以角度为单位.央供:定义一个供解线性圆程组AX=B的函数文献,而后正在下令文献中调用该函数文献.解: M文献函数fc.M文献:function X= fc(A,B)%fc fc是供解线性圆程的函数%A A是已知矩阵的系数矩阵X=A\B;下令M文献:clc;m1=input('输进m1=');m2=input('输进m2=');theta=input('输进theta=');x=theta*pi/180;g=9.8;A=[m1*cos(x) -m1 -sin(x) 0m1*sin(x) 0 cos(x) 00 m2 -sin(x) 00 0 -cos(x) 1];B=[0;m1*g;0;m2*g];X=fc(A,B)运算截止:输进m1=1输进m2=1输进theta=30X =3. 一个自然数是素数,且它的数字位子通过任性对于换后仍为素数.比圆13是千万于素数.试供所有二位千万于素数.央供:定义一个推断素数的函数文献.解:M文献:function [p] = prime(p)% 输进p的范畴,找出其中的素数m=p(length(p));for i=2:sqrt(m)n=find(rem(p,i)==0&p~=i);p(n)=[];%将p中能被i整除,而却没有等于i的元素,即下标为n的元素剔除,其余的即为素数endp;下令文献:clc;p=10:99;p=prime(p); %找出10到99内的所有素数p=10*rem(p,10)+(p-rem(p,10))/10;%将p素数矩阵每个元素个位十位变更程序p=prime(p)%再对于对于换后的素数矩阵找出所有的素数运算截止:。
Matlab程序设计教程(第二版)刘卫国课后参考答案解析
Matlab程序设计教程(第⼆版)刘卫国课后参考答案解析第⼆章1 求下列表达式的值。
(1)w=sqrt(2)*(1+0.34245*10^(-6))(2)a=3.5;b=5;c=-9.8;x=(2*pi*a+(b+c)/(pi+a*b*c)-exp(2))/tan(b+c)+a(3)a=3.32;b=-7.9;y=2*pi*a^(2)*[(1-pi/4)*b-(0.8333-pi/4)*a](4)t=[2,1-3*i;5,-0.65];z=1/2*exp(2*t)*log(t+sqrt(1+t^(2)))2 求下列表达式A=[-1,5,-4;0,7,8;3,61,7];B=[8,3,-1;2,5,3;-3,2,0];(1)A+6*B A^2-B+eye(2)A*B A.*B B.*A(3)A/B B\A(4)[A,B] [A([1,3],:);B^2]3 根据已知,完成下列操作(1)A=[23,10,-0.778,0;41,-45,65,5;32,5,0,32;6,-9.54,54,3.14]; K=find(A>10&A<25);A(K)(2)第三章1 从键盘输⼊⼀个3位数,将它反向输出,如输⼊639,出数936。
f=input('输⼊⼀个数:','s');f(end :-1:1)2 ⽤if语句score=input('请输⼊成绩:');if score>=90&&score<=100disp('A');elseif score>=80&&score<=89disp('B');elseif score>=70&&score<=79disp('C');elseif score>=60&&score<=69;disp('D');elseif score<60&&score>=0;disp('E');elsedisp('出错');end⽤switch语句score=input('请输⼊成绩:');switch fix(score/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');otherwise第四章1题1) X=0:10;Y=x-x.^3/6;P lot(x,y)2) t=0:0.01:2*pi;x=8.*cos(t);y=4*sqrt(2).*sin(t);plot(x,y)2题M⽂件:t=-pi:pi/10:pi;y=1./(1+exp(-t));subplot(2,2,1);bar(t,y,'b');title('bar(t,y,''b'')');axis([-5,5,-3,3]); subplot(2,2,2);stairs(t,y,'k'); title('stairs (t,y,''k'')');axis([-5,5,-3,3]); subplot(2,2,3);stem(t,y,'m'); title('stem (t,y,''m'')');axis([-5,5,-3,3]); subplot(2,2,4);loglog(t,y,'g'); title('loglog(t,y,''g'')');axis([-5,5,-3,3]);3题1)t=0:pi/100:2*pi;y=5*cos(t)+4;polar(t,y,'-*')2)t=-pi/3:pi/100:pi/3;r=5*sin(t).^2./cos(t);polar(t,r,'-*')4题1)t=0:pi/100:2*pi;x=exp(-t/20).*cos(t);y=exp(-t/20).*sin(t);z=t;plot3(x,y,z)第五章:1题1) X=mean(A)Y=std(A,0,1)2) max(max(A))min(min(A))3) B=sum(A,2)sum(B)4) sort(A);sort(A,2,'descend')2题1)t=0:15:90;x1=[0,0.2588,0.5000,0.7071,0.8660,0.9659,1.0000];a1=0:1:90;y1=interp1(t,x1,a1,'spline')x2=[0,0.2679,0.5774,1.0000,1.7320,3.7320,NaN]; a2=0:1:75; y2= interp1(t,x2,a2,'spline')p1=polyfit(t,x1,5);z1=polyval(p1,a1)p2=polyfit(t,x2,5);z2=polyval(p2,a2)4题P=[2,-3,5,13];Q=[1,5,8];p=polyder(P)p1=polyder(P,Q)[p,q]= polyder(P,Q)5题P1=[1,2,4,0,5];P2=[1,0];P3=[1,2,3];1) P4=conv(P2,P3)P4=[0,1,2,3,0];P=P1+P42) x=roots(P)3) A=[-1,1.2,-1.4;0.75,2,3.5;0,5,2.5];4) Z=polyvalm(P,A)第8章1.分解因式. (2)factor(sym(‘5135’)) (1)syms A x y;A=x^4-y^4;factor(A)2.求函数的极限(1)syms x; (2)syms x;limit((x-2)/(x^2-4),x,2)f=(sqrt(pi)-sqrt(acos(x)))/sqrt(x+1);limit(f,x,-1,'right')3.求函数的符号导数。
MATLAB程序设计与应用 实验答案 第六章 刘卫国
1.(1)A=randn(10,5)A =-0.4326 -0.1867 0.2944 -0.3999 -1.6041 -1.6656 0.7258 -1.3362 0.6900 0.2573 0.1253 -0.5883 0.7143 0.8156 -1.0565 0.2877 2.1832 1.6236 0.7119 1.4151 -1.1465 -0.1364 -0.6918 1.2902 -0.8051 1.1909 0.1139 0.8580 0.6686 0.5287 1.1892 1.0668 1.2540 1.1908 0.2193 -0.0376 0.0593 -1.5937 -1.2025 -0.9219 0.3273 -0.0956 -1.4410 -0.0198 -2.1707 0.1746 -0.8323 0.5711 -0.1567 -0.0592 B=mean(A)B =0.0013 0.2310 0.0253 0.3588 -0.4197 C=std(A)C =0.9034 0.8829 1.1898 0.7832 1.0821 (2)D=max(max(A))D =2.1832E=min(min(A))E =-2.1707(3)F=sum(A,2)F =-2.3288-1.32870.01056.2215-1.48953.36024.9201-3.6964-3.3998-0.3025G=sum(sum(A))G =1.9666(4)H=sort(A)H =-1.6656 -0.8323 -1.5937 -1.2025 -2.1707-1.1465 -0.5883 -1.4410 -0.3999 -1.6041-0.4326 -0.1867 -1.3362 -0.1567 -1.0565-0.0376 -0.1364 -0.6918 -0.0198 -0.92190.1253 -0.0956 0.2944 0.6686 -0.80510.1746 0.0593 0.5711 0.6900 -0.05920.2877 0.1139 0.7143 0.7119 0.21930.3273 0.7258 0.8580 0.8156 0.25731.1892 1.0668 1.2540 1.1908 0.52871.19092.1832 1.6236 1.2902 1.4151L=-sort(-A,2,'descend')L =-1.6041 -0.4326 -0.3999 -0.1867 0.2944-1.6656 -1.3362 0.2573 0.6900 0.7258-1.0565 -0.5883 0.1253 0.7143 0.81560.2877 0.7119 1.4151 1.6236 2.1832-1.1465 -0.8051 -0.6918 -0.1364 1.29020.1139 0.5287 0.6686 0.8580 1.19090.2193 1.0668 1.1892 1.1908 1.2540-1.5937 -1.2025 -0.9219 -0.0376 0.0593-2.1707 -1.4410 -0.0956 -0.0198 0.3273-0.8323 -0.1567 -0.0592 0.1746 0.57112.(1)x=[0 15 30 45 60 75 90];y=[0 0.2588 0.5000 0.7071 0.8660 0.9659 1.0000];x1=0:90;y1=interp1(x,y,x1,'spline')y1 =Columns 1 through 110 0.0175 0.0349 0.0524 0.0698 0.0872 0.1045 0.1219 0.1392 0.1564 0.1737Columns 12 through 220.1908 0.2079 0.2249 0.2419 0.2588 0.2756 0.2923 0.3090 0.3255 0.3420 0.3583Columns 23 through 330.3746 0.3907 0.4067 0.4226 0.4384 0.4540 0.4695 0.4848 0.5000 0.5150 0.5299Columns 34 through 440.5446 0.5592 0.5736 0.5878 0.6018 0.6157 0.6293 0.6428 0.6561 0.6691 0.6820Columns 45 through 550.6947 0.7071 0.7193 0.7313 0.7431 0.7547 0.7660 0.7771 0.7880 0.7986 0.8090Columns 56 through 660.8191 0.8290 0.8387 0.8480 0.8571 0.8660 0.8746 0.8829 0.8910 0.8987 0.9062Columns 67 through 770.9135 0.9204 0.9271 0.9335 0.9396 0.9454 0.9510 0.9563 0.9612 0.9659 0.9703Columns 78 through 880.9744 0.9782 0.9817 0.9849 0.9878 0.9904 0.9927 0.9946 0.9963 0.9977 0.9987Columns 89 through 910.9995 0.9999 1.0000x=[0 15 30 45 60 75];y=[0 0.2679 0.5774 1.0000 1.7320 3.7320];x1=0:75;y1=interp1(x,y,x1,'spline')y1 =Columns 1 through 110 0.0184 0.0365 0.0545 0.0724 0.0902 0.1079 0.1255 0.1431 0.1607 0.1784Columns 12 through 220.1961 0.2138 0.2317 0.2497 0.2679 0.2863 0.3048 0.3236 0.3427 0.3620 0.3817Columns 23 through 330.4017 0.4221 0.4429 0.4641 0.4858 0.5079 0.5305 0.5537 0.5774 0.6017 0.6266Columns 34 through 440.6520 0.6780 0.7046 0.7317 0.7593 0.7876 0.8163 0.8456 0.8754 0.9058 0.9367Columns 45 through 550.9681 1.0000 1.0325 1.0658 1.1003 1.1364 1.17431.2145 1.2572 1.3028 1.3516Columns 56 through 661.4041 1.4604 1.5211 1.5863 1.6565 1.7320 1.8131 1.9002 1.99362.0937 2.2008Columns 67 through 762.3152 2.4374 2.5675 2.7060 2.85323.0095 3.17523.3506 3.5361 3.7320x=[0 15 30 45 60 75 90];y=[0 0.2588 0.5000 0.7071 0.8660 0.9659 1.0000];y1=polyfit(x,y,5)y1 =0.0000 0.0000 -0.0000 0.0000 0.0174 0.0000x=[0 15 30 45 60 75];y=[0 0.2679 0.5774 1.0000 1.7320 3.7320];y1=polyfit(x,y,5)y1 =0.0000 -0.0000 0.0000 -0.0010 0.0245 0.0000(2)x=[1 4 9 16 25 36 49 64 81 100];y=1:10;x1=1:100;y1=interp1(x,y,x1,'cubic')y1 =Columns 1 through 111.0000 1.3729 1.71252.0000 2.2405 2.4551 2.64942.82923.0000 3.1636 3.3186Columns 12 through 223.4661 3.6069 3.7422 3.87294.0000 4.1237 4.24354.3599 4.4730 4.5832 4.6907Columns 23 through 334.7958 4.89885.0000 5.0993 5.1966 5.2921 5.38575.4777 5.5681 5.6570 5.7446Columns 34 through 445.8309 5.91606.0000 6.0829 6.1647 6.2454 6.32496.4035 6.4810 6.5577 6.6334Columns 45 through 556.7082 6.7823 6.8556 6.92817.0000 7.0712 7.14167.2113 7.2804 7.3487 7.4164Columns 56 through 667.4835 7.5500 7.6159 7.6812 7.7459 7.8102 7.8739 7.9372 8.0000 8.0623 8.1242Columns 67 through 778.1855 8.2464 8.3068 8.3668 8.4263 8.4854 8.5441 8.6024 8.6603 8.7178 8.7749Columns 78 through 888.8317 8.8881 8.9442 9.0000 9.0555 9.1107 9.16559.2201 9.2744 9.3284 9.3821Columns 89 through 999.4354 9.4884 9.5412 9.5935 9.6456 9.6973 9.7486 9.7996 9.8502 9.9005 9.9505Column 10010.00003.xi=[165 123 150 123 141];yi=[187 126 172 125 148];P=polyfit(xi,yi,3)P =1.0e+003 *-0.0000 0.0013 -0.1779 8.4330线性拟合曲线为:p(x)=1.3x^2—177.9x+84334.(1)P1=[0,3,2];P2=[5,-1,2];P3=[1,0,-0.5];P=conv(P1,conv(P2,P3))P =0 15.0000 7.0000 -3.5000 0.5000 -2.0000 -2.0000 (2)P1=[0,3,2];P2=[5,-1,2];P3=[1,0,-0.5];P=conv(P1,conv(P2,P3));Y=roots(P)Y =0.70710.1000 + 0.6245i0.1000 - 0.6245i-0.7071-0.6667(3)P1=[0,3,2];P2=[5,-1,2];P3=[1,0,-0.5];P=conv(P1,conv(P2,P3));k=0:10;xi=0.2*k;Y=polyval(P,xi)Y =-2.0000 -2.3920 -2.6112 -1.7024 2.7104 15.0000 42.1120 94.1408 184.9056 332.5264 560.00005.(1)[U,fmin]=fminsearch('xiti651',[1,1])U =1.0e-004 *-0.0675 0.1715fmin =1.9920e-010(2)f=inline('-sin(x)-cos(x.^2)');fminbnd(f,0,pi)ans =0.73106.(1)x=[pi/6 pi/4 pi/3 pi/2];f=inline('sin(x).^2+cos(x).^2');dx=diff(f([x,5*pi/12]))/(pi/12)dx =0 0 0 0(2)x=1:3;f=inline('sqrt(x.^2+1)');dx=diff(f([x,4]))dx =0.8219 0.9262 0.96087.(1)g=inline('sin(x).^5.*sin(5*x)');I=quadl(g,0,pi)I =0.0982(2)g=inline('(1+x.^2)./(1+x.^4)');I=quad(g,-1,1)I =2.2214(3)g=inline('(x.*sin(x))./(1+cos(x).^2)'); I=quadl(g,0,pi)I =2.4674(4)f=inline('abs(cos(x+y))');dblquad(f,0,pi,0,pi)ans =6.28329.(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=A\bx =-1.8060-0.53733.0448矩阵分解法:A=[2,3,5;3,7,4;1,-7,1];b=[10;3;5];[Q,R]=qr(A);x=R\(Q\b)x =-1.8060-0.53733.0448(2)矩阵求逆法:A=[5,1,-1,0;1,0,3,-1;-1,-1,0,5;0,0,2,4]; b=[1;2;3;-1];x=inv(A)*bx =1.4000-5.90000.1000-0.3000矩阵除法:A=[5,1,-1,0;1,0,3,-1;-1,-1,0,5;0,0,2,4]; b=[1;2;3;-1];x=A\bx =1.4000-5.90000.1000-0.3000矩阵分解法:A=[5,1,-1,0;1,0,3,-1;-1,-1,0,5;0,0,2,4];b=[1;2;3;-1];[Q,R]=qr(A);x=R\(Q\b)x =1.4000-5.90000.1000-0.300010.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.50001.0000 00 1.00000 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.x=fsolve('xiti612',[0.5,0.5],optimset('Display','off')) x =0.5000y =0.5000f =-0.0509f =-0.0509 0.1173 x =0.5000y =0.5000f =-0.0509f =-0.0509 0.1173 x =0.5000y =0.5000f =-0.0509f =-0.0509 0.1173 x =0.6459y =0.3739f =0.0055f =0.0055 0.0044 x =0.6459y =0.3739f =0.0055f =0.0055 0.0044 x =0.6459y =0.3739f =0.0055f =0.0055 0.0044 x =0.6355y =0.3734f =1.9417e-005f =1.0e-004 *0.1942 0.2589 x =0.6355y =0.3734f =1.9425e-005f =1.0e-004 *0.1942 0.2589x =0.6355y =0.3734f =1.9419e-005f =1.0e-004 *0.1942 0.2591x =0.6354y =0.3734f =2.3754e-010f =1.0e-009 *0.2375 0.2957x =0.6354y =0.3734f =7.9432e-009f =1.0e-008 *0.7943 0.5602x =0.6354y =0.3734f =1.8684e-009f =1.0e-007 *0.0187 0.1936x =0.6354 0.373413.x0=0;xf=20;y1=0;[x,y]=ode45('xiti6131',[x0,xf],y1) x =0.50001.00001.50002.00002.50003.00003.50004.00004.50005.00005.50006.00006.50007.00007.50008.00008.50009.00009.500010.000010.500011.000011.500012.000012.500013.000013.500014.000014.500015.000015.500016.000016.500017.000017.500018.000018.500019.000019.500020.0000 y =NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN(2)x0=0;xf=20;[x,y]=ode45('xiti6132',[x0,xf],[-3,2])[x,y]此题结果过长,疑似错误或不会做。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
0、3388
z4 =
0 0、2500 0 1、2500 1、0000 2、2500
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]
A+6、*B
A-B+eye(3)
A*B
A、*B
A^3
A、^3
A/B
B\A
[A,B]
[A([1,3],:);B^2]
ans =
18 52 -10
46 7 105
21 53 49
ans =
12 31 -3
32 8 84
0 67 1
ans =
68 44 62
309 -72 596
154 -5 241
ans =
实验一
1、先求下列表达式的值,然后显示MATLAB工作空间的使用情况并保存全部变量。
(1)
(2) ,其中
(3)
(4) ,其中t=0:0、5:2、5
解:
M文件:
z1=2*sin(85*pi/180)/(1+exp(2))
x=[2 1+2*i;-、45 5];
z2)
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)
(3)计算系数矩阵A的条件数并分析结论。
解: M文件如下:
输出结果:
X =
1、2000
0、6000
0、6000
X2 =
1、2000
0、6000
0、6000
C =
1、3533e+003
由结果,X与X2的值一样,这表示b的微小变化对方程解也影响较小,而A的条件数算得较小,所以数值稳定性较好,A就是较好的矩阵。
12 102 4
68 0 261
9 -130 49
ans =
37226 233824 48604
247370 149188 600766
78688 454142 118820
ans =
1728 39304 -64
39304 343 658503
27274625 343
ans =
16、4000 -13、6000 7、6000
运算结果:
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)
4、4721 2、2361 3、4641
3、0000 2、8284 2、2361
b =
16、0000 6、0000 18、0000
20、0000 5、0000 12、0000
9、0000 8、0000 5、0000
分析结果知:sqrtm(A)就是类似A的数值平方根(这可由b1*b1=A的结果瞧出),而sqrt(A)则就是对A中的每个元素开根号,两则区别就在于此。
-1、3575 -1、1531 -0、9723 -0、8083
Columns 37 through 40
-0、6567 -0、5151-0、3819 -0、2561
Columns 41 through 44
-0、1374 -0、0255 0、0792 0、1766
Columns 45 through 48
实验三
一、实验目的
1、掌握建立与执行M文件的方法。
2、掌握利用if语句实现选择结构的方法。
3、掌握利用switch语句实现多分支选择结构的方法。
4、掌握try语句的使用。
二、实验内容
1、求分段函数的值。
用if语句实现,分别输出x=-5、0,-3、0,1、0,2、0,2、5,3、0,5、0时的y值。
解:M文件如下:
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
C= E*F
H=C(3:5,2:3)
C =
93 150 77
258 335 237
423 520 397
588 705 557
753 890 717
H =
520 397
705 557
890 717
4、完成下列操作:
(1)求[100,999]之间能被21整除的数的个数。
(2)建立一个字符串向量,删除其中的大写字母。
34 7 87 2 0 3
3 65 7 3 -2 7
ans =
12 34 -4
3 65 7
4 5 1
11 0 19
20 -5 40
3、设有矩阵A与B
(1)求它们的乘积C。
(2)将矩阵C的右下角3×2子矩阵赋给D。
(3)查瞧MATLAB工作空间的使用情况。
解:、运算结果:
E=(reshape(1:1:25,5,5))';F=[3 0 16;17 -6 9;0 23 -4;9 7 0;4 13 11];
0、2663 0、3478 0、4206 0、4841
Columns 49through 52
0、5379 0、5815 0、6145 0、6366
Columns 53 through 56
0、6474 0、6470 0、6351 0、6119
Columns 57 through 60
0、5777 0、5327 0、4774 0、4126
Columns 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、5978
Columns 33 through 36
6、建立A矩阵,试比较sqrtm(A)与sqrt(A),分析它们的区别。
解:M文件如下:
运行结果有:
A =
16 6 18
20 5 12
9 8 5
b1 =
3、8891-0、1102 3、2103
3、2917 2、1436 0、3698
0、3855 2、0760 1、7305
b2 =
4、0000 2、4495 4、2426
ch =
123d4e56g9
实验二
1、设有分块矩阵 ,其中E、R、O、S分别为单位矩阵、随机矩阵、零矩阵与对角阵,试通过数值计算验证 。
解: M文件如下;
输出结果:
S =
1 0
0 2
A =
1、0000 0 0 0、5383 0、4427
01、0000 0 0、9961 0、1067
0 0 1、0000 0、0782 0、9619
10 12 19 21 3
11 18 25 2 9
d =
5070000
t =
65
c1 =
6、8500
c2 =
5、4618
cinf =
6、8500
4、已知
求A的特征值及特征向量,并分析其数学意义。
解:
M文件如图:
输出结果为:
V =
0、7130 0、2803 0、2733
-0、6084 -0、7867 0、8725
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、2375
z2 =
0、7114 - 0、0253i0、8968 + 0、3658i
0、2139 + 0、9343i 1、1541 - 0、0044i
35、8000 -76、2000 50、2000
67、0000 -134、0000 68、0000
ans =
109、4000 -131、2000 322、8000
-53、0000 85、0000 -171、0000
-61、6000 89、8000 -186、2000
ans =
12 34 -4 13 -1
0 0 0 1、0000 0
0 0 0 0 2、0000
a =
1、0000 0 0 1、0767 1、3280
01、0000 0 1、9923 0、3200
0 0 1、0000 0、1564 2、8857