matlab程序设计与应用第二版习题答案

合集下载

matlab程序设计与应用第二版习题答案

matlab程序设计与应用第二版习题答案

matlab程序设计与应用第二版习题答案matlab程序设计与应用第二版习题答案【篇一:matlab程序设计与应用(第二版)实验答案】%实验一 matlab运算基础%第1题%(1)z1=2*sin(85*pi/180)/(1+exp(2))%(2)x=[2,1+2i;-0.45,5];z2=0.5*log(x+sqrt(1+x.^2))%(3)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)t=0:0.5:2.5;z4=t.^2.*(t=0t1)+(t.^2-1).*(t=1t2)+(t.^2-2*t+1).*(t=2t3)%第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(size(a))a*ba.*ba^3a.^3a/bb\a[a,b][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]c=a*bf=size(c)d=c(f(1)-2:f(1),f(2)-1:f(2))whos%第4题%(1):a=100:999;b=rem(a,21);c=length(find(b==0))%(2):a=lsdhksdlkklsdkl;k=find(a=aa=z);a(k)=[]%实验二 matlab矩阵分析与处理 %第1题e=eye(3);r=rand(3,2);o=zeros(2,3);s=diag([2,3]);a=[e,r;o,s];a^2b=[e,(r+r*s);o,s^2]%第2题h=hilb(5)p=pascal(5)hh=det(h)hp=det(p)th=cond(h)tp=cond(p)%第3题a=fix(10*rand(5))h=det(a)trace=trace(a)rank=rank(a)norm=norm(a)%第4题a=[-29,6,18;20,5,12;-8,8,5][v,d]=eig(a)%数学意义略%第5题方法一%(1):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]; x=inv(a)*b%(2):b=[0.95,0.67,0.53];x=inv(a)*b%(3):cond(a)%第5题方法二a=hilb(4)a(:,1)=[]a(4,:)=[]b=[0.95,0.67,0.52];x=inv(a)*bb1=[0.95,0.67,0.53];x1=inv(a)*b1n=cond(b)n1=cond(b1)na=cond(a) %矩阵a为病态矩阵%第6题a=[1,4,9;16,25,36;49,64,81]b=sqrtm(a)c=sqrt(a) %sqrtm函数是以矩阵为单位进行计算,sqrt函数是以矩阵中的元素进行计算%实验三选择程序结构设计%第1题程序一x=[-5.0,-3.0,1.0,2.0,2.5,3.0,5.0];y=[]; %建立存放所有y值的矩阵for x0=xif x00x0~=-3y=[y,x0*x0+x0-6];elseif x0=0x05x0~=2x0~=3y=[y,x0*x0-5*x0+6];elsey=[y,x0*x0-x0-1];endendx%输出所有xy%输出所有y%第1题程序二x=[-5,-3,1,2,2.5,3,5];y=[];for a=1:7if x(a)0x(a)~=-3y=[y,(x(a))^2+x(a)-6];elseif x(a)=0x(a)5x(a)~=2x(a)~=3y=[y,(x(a))^2-5*x(a)+6];elsey=[y,x(a)*x(a)-x(a)-1];endend%第2题程序一x=input(请输入一个百分制成绩:);if x100|x0disp(您输入的成绩不是百分制成绩,请重新输入。

Matlab程序设计与应用第二版刘卫国课后实验答案

Matlab程序设计与应用第二版刘卫国课后实验答案

实验一:T1:%%第一小题z1=2*sin(85*pi/180)/(1+exp(2))%%第二小题x=[2,1+2i;,5];z2=1/2*log(x+sqrt(1+x.^2));z2%%第三小题a=::;z3=1/2*(exp*a)-exp*a)).*sin(a++log(+a )/2)%%第四题t=0::z4=(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,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];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=d iag([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 a>bdisp('帕萨卡矩阵P性能更好'); elseif a<bdisp('希尔伯特矩阵H性能更好'); elsedisp('两个矩阵性能相同');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,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=[,,]'X1=A\BB(3)=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=:2: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;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');elsedisp('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)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');endT3:t=input('请输入工时time=');if t>120w=120*84+(t-120)**84;elseif t<60w=t*84-700;elsew=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 scase{'+'}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>5B=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;endendT3:format longa=input('请输入a:');b=input('请输入b:');x(1)=a/+b);i=1;x(2)=a/(x(i)+b);while (abs(x(i+1)-x(i))>&i<500)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=',num2 str(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:50m=i*(i-1)-1;if isprime(m)==1ms=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:nf=f+i*(i+1);endfx:function f=fx(x)f=1./((x-2).^2++1./((x-3).^4+;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*,0,m2*]'disp('[a1,a2,N1,N2]=');disp([A\B]');T3:j=1;n=1;k=1;for i=10:99 % 挑选出10~99中所有的素数if isprime(i)==1m(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;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=+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))];endendsubplot(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)); endendsubplot(1,2,2);plot(x,y);T4:a=input('请输入a:');b=input('请输入b:');n=input('请输入n:');theta=0::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>;b=length(k);disp('大于的随机数个数占总数的百分比:');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') %desc end降序T3:h=6:2:18;t1=[,,,,,,];t2=[,,,,,,];t=:2:;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::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,,;,2,;0,5,];Ax=polyval(P,A)AA=polyvalm(P,A)实验九:T1:f=inline('det([x x^2 x^3;1 2*x 3*x^2;0 2 6*x])');i=1;x=1;while x<=g(i)=f(x);i=i+1;x=x+;enddx=diff(g)/;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=A\B[L,U]=lu(A);x2=U\(L\B)[Q,R]=qr(A);x3=R\(Q\B)fun:function F=fun(X);x=X(1);y=X(2);z=X(3);F(1)=sin(x)+y^2+log(z)-7;F(2)=3*x+2^y-z^3+1;F(3)=x+y+z-5;T5:f=inline('3*x+sin(x)-exp(x)');fzero(f,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(x^4-y^4)factor(t)T3:syms beta1 beta2 x;simplify(sin(beta1)*cos(beta2)-cos(be ta1)*sin(beta2))simplify((4*x^2+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 yf1=(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=[a^x,t^3;t*cos(x),log(x)];diff(A,x,1) &(4)diff(A,t,2)diff(diff(A,x,1),t,1)f5=(x^2-2*x)*exp(-x^2-y^2-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+x^4+x^8);int(f1,x)f2=1/(asin(x))^2/sqrt(1-x^2);int(f2,x)f3=(x^2+1)/(x^4+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(n^2*x^(n-1),n,1,inf)S3=symsum(n^2/5^n,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('x^2+9*sqrt(x+1)-1=0')x3=solve('3*x*exp(x)+5*sin(x)=0')[x4,y4]=solve('sqrt(x^2+y^2)-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')。

MATLAB程序设计及应用(第二版)课后实验答案

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.52. 已知:1234413134787,2033657327A B --⎡⎤⎡⎤⎢⎥⎢⎥==⎢⎥⎢⎥⎢⎥⎢⎥-⎣⎦⎣⎦求下列表达式的值:(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 和B123453166789101769,111213141502341617181920970212223242541311A 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 +⎡⎤=⎢⎥⎣⎦。

Matlab程序设计与应用第二版刘卫国课后实验答案

Matlab程序设计与应用第二版刘卫国课后实验答案

实验一:T1:%%第一小题z1 =2*sin( 85*pi/1 80) /(1+exp(2 )) %%第二小题x=[2 ,1+2i;- 0、45, 5];z2=1/2*log(x+s qr t(1+x、人2));z 2 %%第三小题a =—3、0:0、1:3、0;z3=1 /2 * (ex p (0、3*a)- e xp (-0、3 *a)卜*sin(a+0、3)+log( (0、3 +a) /2)%%第四题9, 7, 0; 4,1 3,11] ; C =A* BD =C(3:5, 2 : 3)T4-1:a =100: 9 99;b=fi nd( r e m (a,21) ==0 );c=1 ength(b )T4—2:a = in p Ute请输入一个字符串:',‘ sO ;b = find (a〉= A/ &a〈='ZO ;a( b)=[];disp( a) ;实验二:t =0 :0、5 :2、5z 4=( 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 , 3 4,— 4;34, 7, 8 7; 3 ,65,7]B = [ 1 , 3,— 1;2, 0, 3;3, -2,7]disp ( 'A +6* B=') ;di sp( A+6* B);disp( A'—B+ I=');dis p(A-B+eye(3));dis p('A*B= ');dis p(A*B);d isp( 'A、*B =');disp( A、*B) ;d isp ('AG ';disp (A 人3);disp ('A、人3=');di sp (A、人3);d isp('A/B=') ;di sp(A/B) ;disp ( '\AB=' ) ;dis p (B\A);disp(/[A,B]=');di sp([A,B] );d i s p ( ' : A([1,3 ],: );B^2]=');d i sp ([A (: 1,3],:)砂2]);T3:z=1 :25;A=res hape (z, 5, 5) ';B= [3, 0, 16; 17,—6 ,9;0 ,23, —4;T1 :E=eye(3), R=rand(3, 2) ,O=zero s(2,3), S= dia g ([1, 2]);A=[E,R;O,S]disp (A^2=');di s p (A 2);disp('[E ,R +RS O, S^2:');B=[ E, R+ R*S; O,S 人2:T2:H=hi 1 b(5)P=pasc al (5 )H h=de t( H)Hp=de t(P)Th=co nd( H)Tp =co nd (P)a =ab s (Th- 1 );b = abs ( T p - 1);i f a > bdisp ('帕萨卡矩阵P 性能更好') ;e1s e i fa<bdisp (/希尔伯特矩阵H 性能更好' );elsedis P C两个矩阵性能相同');en dT3:a=1: 25;A=res ha pe(a,5,5)dis p('行列式得值:');disp(de t (A));dis p( '矩阵得秩:');disp( rank(A ));d i s p(' 矩阵得迹 :' ); disp(tr ace( A)); disp ( '矩阵得范数: )); dis p(nO rm(A) ); T4:A=[-2 9, 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, 5, 1/6]B= : 0、9 5,0、6 7, 0、5 2 ]' X1=A \B B ( 3) =0、 53 X2=A\Bdis p ('系数矩阵 A 得条件数:/); disp(con d(A)) ;T6:disp('the scOr e is not reas ona b le));s=i n p ut( / pie as e en tercore: /) ; endi f (s>= 90&S < = 10 0);d isp( 'A');el s eif(s > = 8 0 & s <90);d isp()B/) ;e1 se if(s 〉 =70&sV 80);di sp( ) C));elseif (s> = 60 & sV 70);dis p( 'D'); el s edis p('E') ;endT 2- switch :a=1:25; A =r e sha pe ( a, 5,5 ) di sp( ' B1=sqrtm)('A); B 1=sq r t m (A) disp( B2 =sq rt( A ) ‘); B2= sq r t(A) di sp( 'B1*B 1'); B1* B1 disp (/ B2、 *B2) ; B2、 *B2 实验三 : T1: X =——5、 0:2: 5、0 : length(X) ; (x (i) <0) &(x(i) ~= -3) yel s ei & (x(i)〜=2) y(i ) else y (i)=x(i)人2 —x( i)-1 ;endfor i=1 if (i) = x( i)八 2 +x (i) —6; f (x(i)>=0)&(x(i)V5)& (x (i )〜=3);=x(i)A2 — 5* x(i)+6; end s=input ('p lease en ter ');whi 1 e (s < 0| | s 〉disp ( 'the reasonab1 e' );s=input('p 1 ease re:' ); end switc h fix(s/1 0 ) cas e{9, 1 0}disp(' A'); case{8} di s p CE '); c ase{ 7} d isp ('C ' case {6} dis p(' D'); ca se{ 0, d isp(' E/ e nd T 3: t=inp ut i ft>120 SCO re : );100) scor en 1, 2,3, 4,5});'请输入工时 s nO tt h e sc o me=');y T2- if: s =i npu t(' pie a se ent er the s c ore:,);w=120 *84+(t-120) * 1、 15* 84;elseif t < 60 w =t * 8 4 -701/the senden delse w= 8 4*t; pii=sq T1—2: rt(6 *m)end d isp ('应发工资为:’); di sp(w); T 4: a=10 +f1 oor(rand(1)* 89) b =1 0 + f loor( ra nd (1) * 89) s=inp ut (/ 请输入 + 或一或 * 或 /' , ' s'); w hi 1 e ( s 〜='+ ' & s 〜-—,&& s 〜-* '&&s~ =' /') d isp ('输入得符号错误, 请重新输入 ') ; s=inp ut (/ 请输入 + 或-或*或/' , 's'); end switch s case{ ' +' c=a + b;c ase{ '—'} c=a —b ; ca se{'* ' c=a* b; ca se{ ' / '} c =a/b ; t(/请输入 n : (1 : n)、人 2; s=sum( m ); pii=sq rt(6 *s) T2:n=inpum=1 、 / n=1 0 00;y=0; for i=1 : n;y = y+ 1/ (2 * i —1if end T 3:endc T5:A = n=inp u t('请输入 n :'); whi 1 e (n <1) d isp ('输入得n 有误,请重新输入/ ); n=i npu t ('请输入 n:'); rand(5,6) if n >5 B=A ( 5, :) eIse B = A(n,:) E nd 实验四 :T1: n=in p ut (/ 请输入 n :'); m =0; for i=1 : n; m=m+1/iA 2;'););y >= 3d isp ( ' 最大 n i — 1disp ('对应得 值为:');y 值为 :' ); y -1/( 2*i-1) break;dt a =in put('请输入 a:'); b =inp ut ('请输入 b:');x (1)= a/(1、0 +b) ; i = 1;x (2 +b) ; whil e (a b s (x (i+ 1) — x(i))> 1& i 〈 50 0) for m Ion g i =i+1 ;x(i+1)= a/(x (i) + b); endr 仁(—b+s qrt(b 人 2+4*a)) /2; r2 =( -b — s qr t( b^ 2+ 4 *a)) )=a / (x(i) 0、00 0 0d is p ([' x ' ,nm 2st r (i+ 1 ), n um2s t r(x( i+1), 8)] );disp (['x',num 2 str( i),'=',num 2str (x( i),8 )]);di sp(['r1=',nu m2s tr( r1,8),', r2=', num2str(r 2,8 )]); fo r mat short T4:f(1 )=1;f( 2)=0;f(3 )=1 ;i=4; w h il e (i<= 10 0)f(i) =f( i —1 )-2* f( i —2) +f( i —3 ); i=i+1;f m a x= max (f )fmin = min (f) fs um=s um(f) fp= 1 ength(f in d( f> 0)) fn=length (fin d (fv0 )) fO=lengt h( f i nd (f= =0)) T5:j =0; s= 0;for i=3:50m=i*( i-1)—1;i f i s prime (m)= =1s = s +m ;j =j +l;endendd i sp(['亲密数得对数:',nu m 2s tr (j):); dis p (['所有亲密素数之与:',n um2str( s )] );实验五:f n1: func t ion f = fn1 ( n)f= n +10 * l o g( n、人2 +5); fn2:f un ctio n f=f n2( n) f = 0;f or i= l: n nction f=fx ( x)/ ((x— 2)、八 2 + 0、1) +1、/ ((x—3 )、4+0、0 1);Tl :x =in p ut('请输入一个复数:');fe =exp (x)f lg= log( x) fsin=sin( x) f co s=cos(x)T2:m 1=in p ut ('请输入m 1/);m2=input ()请输入m 2:'; t=input ('请输入th et a:));A=[m1*cos(t ), -m1,-si n(t), 0;m 1 * si n(t), 0 ,、、、co s( t) , 0 ;0,m2, —sin (t), 0; 0, 0, —C o s(t) ,1]B= [0, m1 * 9、8, 0,m2dis p('[ al ,a2 ,N 1,N2: d i sp([A\B :';T 3: j=1 ; n= l;k =1;for i=10:99 中所有得素数if isprime(i)==1m(j)=i;j=j+1;endendf o r t=1:le n gth( m);s( n)=l 0* r em(m(t ), 10)+fix(m (t )/10); %挑选出得素数进行位置交换if i sp rime(s(n)) = = 1%再挑选出交换绝对素数r( k)= m(t);k=k+l ;endn=n+ l ;enddisp (['所有两位绝对素数:',num2str (r) ])T 5:y 1=f n 1 ( 4 0 ) /(f n 1(3 0 ) +fn1( 2 0 )) %函数调用y2 =f n2(40)/( fn 2( 30)+fn2 (20))实验六:T1: x=linspa ce (0, 2*pi,101 );y=(0、5+ 3* sin(x)、/ (l + x、人2))、* c os ( x) ;plot( x, y) ;T2: x=1in sp ace(0, 2*p i,10 0);y1=x、人2;y2=cos( 2*x);y3 =y1 、*y2 ;subplot(2,2, 1);plot(x,y1,r':',x,y2, 'y-', x, y3, 'b -、en f=f +i * (i + 1); dfx:fu f=1 、*9、8]'=/ );%挑选出1 0 ~99sub pl o t(2, 2,2) ; el se pl ot(X,y 1,'r:'); y =[y,1 /2 *l og(x0+sqr t(1 s u b plot ( 2,2,3); + x 0 人 2))]; p lot(x,y2, ' -y'); e nd subplot(2 ,2, 4); en d pl ot( x,y 3, 'b —、 '); subplot (1, 2,1); T 2-3 : pl ot(X, y); x=lin spa ce(0 ,2*pi,1 0 ); fo r i =1:l ength( x) y 仁X 、人2 ; if X(i) <=0y2 =cos(2* x); y(i)= (x (i)+sq rt( pi )) / e x p y 3 = y1、*y2 ; (2 ); sub plot (3, 4,1);else b ar( x, y1,'r ');axis( [0, 7 ,0, 40:); y( i) =1/2 *l og(x (i)+sqr s ub pl ot(3, 4 ,2); t (1+x(i) A2)); stairs(x, y1,'y ') ;aX is( [0,7,0, 40]); end subp lo t(3, 4, 3);e nd stem(x, y1,b' ') ;aX is([0,7 ,0 ,40]); subp lot(1,2, 2); '); y =[y,(xO+sqrt(pi)) /exp( 2)]; su bp l 0 t (3,4, 4);fill( x,y1, 'g ') ;axis ([0, 7,0 ,40]) ; sub plot( 3, 4,5);bar(x, y2,'r' );ax(i[s —1,7,—1, 1:) subplot( 3, 4, s tair s(X ,y2, ' 1:);subpl ot (3, 4,7);ste m( x, y2, 'b )'1: ); sub pl ot( 3,4,8);fil l(X,y 2,'g '); sub p lot (3 ,4, 9); bar (x,y3,'r') ;aXi s([0,7 ,-30,50]) ; subplot (3, 4,10 );sta irs(x,y3, ')' ;yaxi s([0, 7,-30 ,50:); su bplot(3 ,4, 11);s tem( x,y3 ,'b') ;axi sub pl o t(3,4 ,12); fill(x,y3,'g '); 50]);T3: 6); y);'axis([—1, 7,-1, ;a xis (:-l ,7, - 1, axi s (:-1,7,-1 ,1:); s ([0,7, -30,5 0:);a x is ( [ 0, 7 ,-30,x=li nsp ace( -5,5,1 y=[:; for X0 =x ; i f x0〈 =0;0 0); plo t (x, y );T4:a=i n pu t C 请输入 a:'); b=i np ut ('请输入 b: / ); n= i npu t ('请输入 n :');t he ta=0: 0、0 0 1 :2*p i; y=a 、 *sin(b+n 、 *theta) ; polar(thet a,y); T5: X =lin s pace( —5 ,5, 2 1); y= l in s pace(0,10,3 1); :x, y : =me s hg rid (x,y); z =cos(x)、*c os (y)、* exp( — sq r t ( x 、 人2 + y 、人 2) /4) s u bp lot (1, surf( x,y,z): sub p l o t(1, co ntour3(x,y, T6:s=0:pi /2; t=0:3*pi/2; [s,t : =mes hgri d(s,t) ;x= c os(s)、* c os (t );y= c os ( s)、* s i n (t); z=sin( s) ; s urf(X ,y, z);2, 1); 2, 2);z);s h a d in g interp ;实验八:T1:?A=rand(1, 30000);dis p ('均值为:') ;mean (A)di sp('标准方差为:/);s td ( A)di s p('最大元素为:');ma x( A)d i s P C最小元素为:');min(A)k=find ( A〉0、5) ; b= le ngd h( k);disp ('大于0、5 得随机数个数占总数得百分比:');sp rintf ('% f % % , b / 3 0 0 )T2: ?P =45+50* ran d(100, 5) disp('最高分及序号:/ );[Y,U:=max(P,[:,1) disp ('最低分及序号: ' );[Y1, U1:= min(P,[:,1)di sp ('每门课平均分:') ;mean( P,1)di sp( '每门课得标准方差:');std(P ,0, 1)s=sum(P,2) ;d i s p( '总分最高分: ' );[Y2, U2: =maX( s, [: ,1) di sp( '总分最低分:');[Y3, U3]=min(s,[], 1)[zcj , X sxh:end') % desc T3: =sord ( s ,1 ,' descen d降序p=p olyfit ( x, y, 5) x1=1:0、01:101;y1=log10(x1);y 2 =poly V al (p, x1);plot( X 1, y1, ' r -' ,x1 y2 ,'£';)T 5:P1=[1,2,4,0 ,5];P2=[0 ,0, 0, 1,2];P3 =[0,0 , 1,2 , 3:;P4= c onv( P2 , P 3);sp 1 = length( P 1);sp2=len gth( P4);P =[ z er os (1 ,sp2 —s p1 ), P1: + P4x= roots(P)A=[-1, 1、2, -1、4;0、75,2,3、5; 0,5 ,2、5];Ax=p o l y val (PA)A A=po l y valm(P, A)实验九:T1 :f= i nline( ed * X 人2;0 26*x :i=1 ;x=1;whi 1 e x<=3、(:x xA 2 xA 3)1 2* x 3') ;h= 6:2:18;t1=[1 8、0,2 0、28、0, 24、0];t 2 = [1 5、0,1 9、32、0, 30、t=6、5:2:1T1=interp1(h,t T2=inde rpT4: 0,22、0,25、0, 24、0,28、0];7、5;1 ,t, 'sp1 ine ') 1(h, t2,d , 'ps1 0 ,30、0,0,34、0,ine )'x=1 :10:101;y=log101g( i) =f(X);i =i+1;x=X +0、0 1 ;endd x=di f f (g)/ 0、01;dX (1)dx(1 01 )dx(length(g)—1)T2:f1 =in line (‘s qrt (co s(t、人2)+4*sin(2* t )、人2+1)')f2=in lin e('log(1+x)、/( 1+X、A2) ')pi )pi)I 1 = quI2=q u adT3 :ad(f1,0,(f 2, 0,2*2*A=[6, 5, —2;3,—9,0,2: ;B=[—4, 13,1,1 1] ;'x1=A\ B-2, 5; 9,-1 ,4 , —1;3,4,2 ,:L ,U]=I u( A);( L\B)[Q,R ]=q r (A);(Q\B)f u n:f u n ction F = fu n( X) x=X(1);y =X( 2);z =X (3);F (1)=sin(X)+ y^2+l o g (z)—7 ;F(2)=3*x+ 2 Ay— z 人3+1 ;F(3) =x+y+z-5;T5:f =inl ine( '3*x+sin(x) —exp(x) ') ;fzero( f,1 、5)X=fso Ive ('让n', : 1 1 1]',o pt i m set CDi sp l ay/, ' f f 0 ))实验十:T1:x= sym('6 ';y =sym(' 5';z=(x+ 1) /(sqrt( 3 +x) —sq r t (y ))T2:Sy m s x y;t =sym ('51350 ;factor(xA4 — yM) factor(t)T 3:s y ms betal b eta2 x;Sim pli f y( sin( b e t a 1) * cos(beta2) —cos (beta1)*sins i m p li fy(1))T4:d p 1= [ 0,1,0; 1 ,p2=[1,0,0 ;0,1,A = : a,b,c;d,e,f;B= p 1*p 2 * AB1= inv (B)B1* B;tr i l( B ) d et (B)T5:sy ms x t a yf1=(X *( eX p(sin(x) )+1 )—2*(e xp(tan(x))—1) )/sin ( x)A3 ;l imit(f1, x, 0) %( 1)f 2=( sqrt(pi )-sqrt( acos(X )) )/sqrt(x+1);limi t(f 2, X ,— 1 , ' grih t/ % (2)f3=(1— co s(2*x))/x;di ff(f3, x, 1) %(3)di ff (f 3, x,2)A=[a Ax,tA3 ; t*cos(X),log(x): ;dif f(A, x, 1)&( 4)d iff(A ,t,2 )diff(diff (A,x,1),f5=( xA2—2*x) * e yx= -d iff(f5,x, 1) (5)fxy=diff( di ffX= sym ('0');y=sym ( ' 1' ;e val (f xy)T 6: x=sym( ' x;')f1=1/(1+x 人4 +x人8 );int(f1, x)f 2 = 1/(asin (x))人2 /sq r t (1 — x^2);i n t ( f 2,x)f3 =(X A2+1) /(xA4+1);i nt( f 3,X ,0,inf)f4=exp(x)*( 1+eX p( x) )A2;in t(f4 ,X,0,l og(2))实验十一:sym s n X ;S1 = s ymsu m (1/(2* n —1) , n, 1, 1 0) S 2 = symsum( n人2*乂人(门一1),n ,1,inf)S3=s ym s u m (n人2/5人n,n ,1, inf)T 2: x=s ym('X/);f= I og (x);bet a2))4*x 人2+8* x+3 )/ (2 * x +syms a b c e f g h k;0,0 ;0 ,0 ,1];0 ;1,0, 1:;t, 1)X p( -xA2 — y 人2 -X*y);f5,x,1),y, 1)T1:ta ylor (f ,x,6, 1 )T3:x1=s ol ve( '(lo1g+x)—5/(1+sin(X)) =2') x2 =sol ve( '2xA+9* s qrt (x+1)—1=0') x3=sol ve(/3* x* eX p( x)+5* sin( x)—7 8、5 =0 ‘)[x4,y4]=s o lve( S qrt(x^2+y 人2)- 10 0= 0 ' ' *x+5 * y-8= O’)T4 :d so 1 ve(' D2 + 4 *Dy + 2 9 * y = 0' ' 0 )= 0 '/ Dy(0)=15/,'x') T5 :[x,y ,z]=dsol V e ('Dx=2*x —3*y+3*z/ / Dy=4* x-5 * y+3* z ',' D z=4 *x —4*y+ 2*z',/ t ')。

最新Matlab程序设计与应用第二版刘卫国课后实验答案

最新Matlab程序设计与应用第二版刘卫国课后实验答案

实验一: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)+l og((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,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]; 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,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 a>bdisp('帕萨卡矩阵P性能更好');elseif a<bdisp('希尔伯特矩阵H性能更好');elsedisp('两个矩阵性能相同');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,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\BB(3)=0.53X2=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.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;elsey(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');elsedisp('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)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');endT3:t=input('请输入工时time=');if t>120w=120*84+(t-120)*1.15*84; elseif t<60w=t*84-700;elsew=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 scase{'+'}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>5B=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;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(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 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:50m=i*(i-1)-1;if isprime(m)==1ms=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:nf=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),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)==1m(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;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))];endendsubplot(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)); 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 x^2 x^3;1 2*x 3*x^2;0 2 6*x])');i=1;x=1;while x<=3.01g(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=A\B[L,U]=lu(A);x2=U\(L\B)[Q,R]=qr(A);x3=R\(Q\B)fun:function F=fun(X);x=X(1);y=X(2);z=X(3);F(1)=sin(x)+y^2+log(z)-7;F(2)=3*x+2^y-z^3+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(x^4-y^4)factor(t)T3:syms beta1 beta2 x;simplify(sin(beta1)*cos(beta2)-cos(beta1)*sin (beta2))simplify((4*x^2+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 yf1=(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=[a^x,t^3;t*cos(x),log(x)];diff(A,x,1) &(4)diff(A,t,2)diff(diff(A,x,1),t,1)f5=(x^2-2*x)*exp(-x^2-y^2-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+x^4+x^8);int(f1,x)f2=1/(asin(x))^2/sqrt(1-x^2);int(f2,x)f3=(x^2+1)/(x^4+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(n^2*x^(n-1),n,1,inf)S3=symsum(n^2/5^n,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('x^2+9*sqrt(x+1)-1=0')x3=solve('3*x*exp(x)+5*sin(x)-78.5=0')[x4,y4]=solve('sqrt(x^2+y^2)-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')2020年普通高等学校招生全国统一考试(山东模拟卷)语文一、现代文阅读(35分)(一)现代文阅读I(本题共5小题,19分)阅读下面的文字,完成1~5题。

刘卫国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程序设计与应用(第二版)实验参考答案%实验一MATLAB运算基础%第一题%(1)z1=2*sin(85*pi/180)/(1+exp(2))%(2)x=[2,1+2i;-0.45,5];z2=0.5*log(x+sqrt(1+x.^2))%(3)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)t=0:0.5:2.5;z4=t.^2.*(t>=0&t<1)+(t.^2-1).*(t>=1&t<2)+(t.^2-2*t+1).*(t>=2&t<3)%第二题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(size(A))A*BA.*BA^3A.^3A/BB\A[A,B][A([1,3],:);B^2]%第三题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]C=A*BF=size(C)D=C(F(1)-2:F(1),F(2)-1:F(2))whos%第四题%(1):A=100:999;B=rem(A,21);C=length(find(B==0))%(2):A='lsdhKSDLKklsdkl';k=find(A>='A'&A<='Z');A(k)=[]%实验二MATLAB矩阵分析与处理%第一题E=eye(3);R=rand(3,2);O=zeros(2,3);S=diag([2,3]);A=[E,R;O,S];A^2B=[E,(R+R*S);O,S^2]%第二题H=hilb(5)P=pascal(5)Hh=det(H)Hp=det(P)Th=cond(H)Tp=cond(P)%第三题:A=fix(10*rand(5))H=det(A)Trace=trace(A)Rank=rank(A)Norm=norm(A)%第四题:A=[-29,6,18;20,5,12;-8,8,5][V,D]=eig(A)%数学意义略%第五题方法一:%(1):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]';x=inv(A)*b%(2):B=[0.95,0.67,0.53]';x=inv(A)*B%(3):cond(A)%第五题方法二:A=hilb(4)A(:,1)=[]A(4,:)=[]B=[0.95,0.67,0.52]';X=inv(A)*BB1=[0.95,0.67,0.53]';X1=inv(A)*B1N=cond(B)N1=cond(B1)Na=cond(A) %矩阵A为病态矩阵%第六题A=[1,4,9;16,25,36;49,64,81]B=sqrtm(A)C=sqrt(A) %sqrtm函数是以矩阵为单位进行计算,sqrt函数是以矩阵中的元素进行计算%实验三选择程序结构设计%第一题程序一x=[-5.0,-3.0,1.0,2.0,2.5,3.0,5.0];y=[]; %建立存放所有y值的矩阵for x0=xif x0<0&x0~=-3y=[y,x0*x0+x0-6];elseif x0>=0&x0<5&x0~=2&x0~=3y=[y,x0*x0-5*x0+6];elsey=[y,x0*x0-x0-1];endendx %输出所有xy %输出所有y%第一题程序二x=[-5,-3,1,2,2.5,3,5];y=[];for a=1:7if x(a)<0&x(a)~=-3y=[y,(x(a))^2+x(a)-6];elseif x(a)>=0&x(a)<5&x(a)~=2&x(a)~=3y=[y,(x(a))^2-5*x(a)+6];elsey=[y,x(a)*x(a)-x(a)-1];endend%第二题程序一:x=input('请输入一个百分制成绩:');if x>100|x<0disp('您输入的成绩不是百分制成绩,请重新输入。

(完整word版)MATLAB程序设计与应用第二版课后题答案2

(完整word版)MATLAB程序设计与应用第二版课后题答案2

第二章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 2134 98 218 42 12143 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 041.0000 -45.0000 65.0000 5.000032.0000 5.0000 0 32.0000C =23.0000 10.000041.0000 -45.000032.0000 5.00006.0000 -9.5400D =65.0000 5.00000 32.000054.0000 3.1400E =1.0e+003 *0.9141 -0.22391.20802.71231.1330 -0.2103(2)E<Dans =0 10 00 1E&Dans =1 10 11 1E|Dans =1 11 11 1~D|~Eans =0 01 00 0find(A>=10&A<25)ans =156.all(A)ans =any(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 cell}B{3}=[]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是互逆的。

MATLAB程序设计与应用(第二版)课后实验答案

MATLAB程序设计与应用(第二版)课后实验答案

e an dAl l th i ng si nt he i rb ei n ga re go od fo r矩阵和对角阵,试通过数值计算验证。

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

为什么?解:M 文件如下:an dAl l t h i ng si nt he i rb ei n ga re go od fo r因为它们的条件数Th>>Tp,所以pascal 矩阵性能更好。

3. 建立一个5×5矩阵,求它的行列式值、迹、秩和范数。

解: M 文件如下:4. 已知2961820512885A -⎡⎤⎢⎥=⎢⎥⎢⎥-⎣⎦求A 的特征值及特征向量,并分析其数学意义。

解:M 文件如图:b ei n ga re go od f数学意义:V 的3个列向量是A 的特征向量,D 的主对角线上3个是A 的特征值,特别的,V 的3个列向量分别是D 的3个特征值的特征向量。

5. 下面是一个线性方程组:111⎡⎤e an dAl l t h i ng si nt he i rb ei n ga re go od fo rs o 输出结果:由结果,X 和X2的值一样,这表示b 的微小变化对方程解也影响较小,而A 的条件数算得较小,所以数值稳定性较好,A 是较好的矩阵。

6. 建立A 矩阵,试比较sqrtm(A)和sqrt(A),分析它们的区别。

解:M 文件如下:分析结果知:sqrtm(A)是类似A 的数值平方根(这可由b1*b1=A 的结果看出),而sqrt(A)则是对A 中的每个元素开根号,两则区别就在于此。

e an dAl l t h i ng si nt he i rb ei n ga re 实验三 选择结构程序设计1. 求分段函数的值。

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程序设计与应用第二版刘卫国课后实验答案

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+x.^2)); 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.^2)+(t>=1&t<2).*(t.^2-1)+(t]);>=2&t<3).*(t.^2-2*t+1) A=[E,R;O,S]disp('A^2='); T2: disp(A^2); A=[12,34,-4;34,7,87;3,65,7]disp('[E,R+RS;O,S^2]'); B=[1,3,-1;2,0,3;3,-2,7] B=[E,R+R*S;O,S^2] 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('A^3='); b=abs(Tp-1); disp(A^3); if a>bdisp('A.^3='); disp('帕萨卡矩阵P性能更好'); disp(A.^3); elseif a<b disp('A/B='); disp('希尔伯特矩阵H性能更好'); disp(A/B); else disp('B\A='); disp('两个矩阵性能相同'); disp(B\A); enddisp('[A,B]='); T3:disp([A,B]); a=1:25;disp('[A([1,3],:);B^2]='); A=reshape(a,5,5) disp([A([1,3],:);B^2]); 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(i)^2+x(i)-6; T3:elseif t=input('请输入工时time=');(x(i)>=0)&(x(i)<5)&(x(i)~=2)&(x(i)~=3); if t>120y(i)=x(i)^2-5*x(i)+6; w=120*84+(t-120)*1.15*84;else elseif t<60y(i)=x(i)^2-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=input('请输入+或-或*或/','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^2+4*a))/2;r2=(-b-sqrt(b^2+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^2; fsum=sum(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).^2; 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.^2+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.^2)).*cos(x); function f=fx(x) plot(x,y);f=1./((x-2).^2+0.1)+1./((x-3).^4+0.01); T2:T1: x=linspace(0,2*pi,100); x=input('请输入一个复数:'); y1=x.^2;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.^2;T3: y2=cos(2*x);j=1;n=1;k=1; y3=y1.*y2;for i=10:99 % 挑选出10~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.^2+y.^2)/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(x0+sqrt(1+x0^2))]; A=rand(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)^2)); 比:');end sprintf('%f%%', b/300) end T2:subplot(1,2,2); P=45+50*rand(100,5) plot(x,y); disp('最高分及序号:'); T4: [Y,U]=max(P,[],1) a=input('请输入a:'); disp('最低分及序号:');b=input('请输入b:'); [Y1,U1]=min(P,[],1) n=input('请输入n:'); disp('每门课平均分:');5mean(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=inline('sqrt(cos(t.^2)+4*sin(2*t).^2+1)')序 f2=inline('log(1+x)./(1+x.^2)')T3: I1=quad(f1,0,2*pi) h=6:2:18; I2=quad(f2,0,2*pi)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\B T2=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)=sin(x)+y^2+log(z)-7; P1=[1,2,4,0,5]; F(2)=3*x+2^y-z^3+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 1 1]',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=inline('det([x x^2 x^3;1 2*x 3*x^2;0 2 t=sym('5135'); 6*x])'); factor(x^4-y^4) 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(n^2*x^(n-1),n,1,inf) simplify((4*x^2+8*x+3)/(2*x+1)) T4: S3=symsum(n^2/5^n,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('x^2+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(x^2+y^2)-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'^3; ,'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^x,t^3;t*cos(x),log(x)];diff(A,x,1) &(4)diff(A,t,2)diff(diff(A,x,1),t,1)f5=(x^2-2*x)*exp(-x^2-y^2-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+x^4+x^8);int(f1,x)f2=1/(asin(x))^2/sqrt(1-x^2);int(f2,x)f3=(x^2+1)/(x^4+1);int(f3,x,0,inf)f4=exp(x)*(1+exp(x))^2;int(f4,x,0,log(2)) 实验十一:T1:7。

MATLAB程序设计与应用第二版课后题答案2(可编辑修改word版)

MATLAB程序设计与应用第二版课后题答案2(可编辑修改word版)

第二章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 2134 98 218 42 12143 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 041.0000 -45.0000 65.0000 5.000032.0000 5.0000 0 32.0000C =23.0000 10.000041.0000 -45.000032.0000 5.00006.0000 -9.5400D =65.0000 5.00000 32.000054.0000 3.1400E =1.0e+003 *0.9141 -0.22391.20802.71231.1330 -0.2103(2)E<Dans =0 10 00 1E&Dans =1 10 11 1E|Dans =1 11 11 1~D|~Eans =0 01 00 0find(A>=10&A<25)ans =156.all(A)ans =any(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 cell}B{3}=[]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 是互逆的。

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

matlab程序设计与应用第二版习题答案
Matlab程序设计与应用第二版习题答案
Matlab是一种强大的数学软件,广泛应用于科学计算、数据分析和工程设计等
领域。

《Matlab程序设计与应用》是一本经典的教材,对于学习和掌握Matlab
编程语言具有重要的意义。

本文将为大家提供《Matlab程序设计与应用第二版》中部分习题的答案,帮助读者更好地理解和应用Matlab。

第一章:Matlab基础
1.1 基本操作
1. a = 3; b = 4; c = sqrt(a^2 + b^2); disp(c);
2. x = linspace(-pi, pi, 100); y = sin(x); plot(x, y);
3. A = [1 2 3; 4 5 6; 7 8 9]; B = [9 8 7; 6 5 4; 3 2 1]; C = A + B; disp(C);
1.2 控制结构
1. for i = 1:10
disp(i);
end
2. n = 0; sum = 0; while sum < 100
n = n + 1;
sum = sum + n;
end
disp(n);
3. x = 5; if x > 0
disp('x is positive');
elseif x < 0
disp('x is negative');
else
disp('x is zero');
end
第二章:向量和矩阵运算
2.1 向量运算
1. A = [1 2 3]; B = [4 5 6]; C = A .* B; disp(C);
2. A = [1 2 3]; B = [4 5 6]; C = A ./ B; disp(C);
3. A = [1 2 3]; B = [4 5 6]; C = dot(A, B); disp(C);
2.2 矩阵运算
1. A = [1 2 3; 4 5 6]; B = [7 8; 9 10; 11 12]; C = A * B; disp(C);
2. A = [1 2 3; 4 5 6]; B = [7 8; 9 10; 11 12]; C = B * A; disp(C);
3. A = [1 2 3; 4 5 6]; B = [7 8; 9 10; 11 12]; C = A .* B; disp(C); 第三章:函数和脚本文件
3.1 函数
1. function y = myfunc(x)
y = x^2 + 3*x + 2;
end
2. function [y1, y2] = myfunc(x1, x2)
y1 = x1^2 + 3*x1 + 2;
y2 = x2^2 + 3*x2 + 2;
end
3. function [y1, y2] = myfunc(x)
y1 = x^2 + 3*x + 2;
y2 = sin(x);
end
3.2 脚本文件
1. x = linspace(0, 2*pi, 100); y = sin(x); plot(x, y);
2. x = linspace(-10, 10, 100); y = x.^2 + 3*x + 2; plot(x, y);
3. x = linspace(0, 2*pi, 100); y1 = sin(x); y2 = cos(x); plot(x, y1, x, y2);
通过以上习题的答案,读者可以对Matlab程序设计的基本语法和常用函数有一个初步的了解。

当然,这只是习题的一小部分,还有更多有趣的问题等待着你去探索和解决。

希望本文能够对你在学习和应用Matlab中起到一定的帮助和指导作用。

祝愿大家在Matlab的世界里能够不断进步,创造出更多令人惊喜的成果!。

相关文档
最新文档