当前位置:文档之家› 《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矩阵分析与处理1.E=eye(3);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;elsey=x*x-x-1;endy2.(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('您输入的成绩有误,请重新输入!'); end3. a=input('请输入工号:','s');b=input('请输入该员工的工时数:');if b>120y=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);y2=cos(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')3. h=6:2:18;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,'-*')5. p1=[1,2,4,0,5];p2=[1,2];p3=[1,2,3];p4=conv(p2,p3);p41=[0,p4];p=p1+p41 %P(x)=P1(x)+P2(x)*P3(x) x=roots(p) %根A=[-1,1.2,-1.4;0.75,2,3.5;0,5,2.5];y1=polyval(p,A)y2=polyvalm(p,A)。

相关主题