MATLAB程序设计与应用(第二版)刘卫国主编_部分实验答案

合集下载

MATLAB程序设计与应用课后实验答案

MATLAB程序设计与应用课后实验答案

M A T L A B程序设计与应用(刘卫国编)课后实验答案(总66页)本页仅作为文档封面,使用时可以删除This document is for reference only-rar21year.March实验一 MATLAB 运算基础1. 先求下列表达式的值,然后显示MATLAB 工作空间的使用情况并保存全部变量。

(1) 0122sin851z e =+(2) 21ln(2z 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 --+=++=-- (4) 2242011122123t t z t t t t t ⎧≤<⎪=-≤<⎨⎪-+≤<⎩,其中t =0::2. 已知: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 和B1234530166789101769,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 O S +⎡⎤=⎢⎥⎣⎦。

MATLAB程序设计方案与应用第二版刘卫国实验部分

MATLAB程序设计方案与应用第二版刘卫国实验部分

仅供个人学习实验作者:Pan Hon glia ng1.(1)z1=2*si n(85*pi/180”(1+exp(2))(2)>> x=[2,1+2i;-0.45,5];>> z2=(1/2)*log(x+sqrt(1+x*x))⑶>> a=-3.0:0.1:3.0;>> z3=((exp(0.3*a)-exp(-0.3*a))/2).*si n(a+0.3)+log((0.3+a)/2)(4)t=0:0.5:2.5;>>z4=(t.*t).*(t>=0&t<1)+(t.*t-1).*(t>=1 &t<2)+(t.*t-2.*t+1).*(t>=2&t<3)2.(1)>> A=[12,34,-4;34,7,87;3,65,7];>> B=[1,3,-1;2,0,3;3,-2,7];>> A+6*B(2)>> A*B(3)>> A A3(4)>> A/B(5)>> [A,B]3.(1)>>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*B(2)>> D=C(3:e nd,2:e nd)(3)Name value classA <5 x 5double> doubleB <5 x 3double> doubleC <5 x 3double> doubleD [520 397;705 557; …]double4.(1)>> X=100:999;>> len gth(fi nd(~rem(X,21)))(2)>> y='ahHGJjskjJjsdsaSa';>> M=y(fi nd(yv='z' &y>='a'))实验二实验三1 x=input(' 请输入x的值')if x<0 &x~=-3y=x A2+x-6;elseif x>=0 &x<5 &x~=2 &x~=3 y=xA2-5*x+6;elsey=xA2-x-1;endy2(1)x=input('请输入成绩')if x>=90 &x<=100disp(' 等级为A')elseif x>=80 &x<=89disp(' 等级为B')elseif x>=70 &x<=79disp(' 等级为C')elseif x>=60 &x<=69disp(' 等级为D')elseif x>=0 &x<=59disp(' 等级为E')elsedisp(' 成绩有误')end⑵x=input('请输入成绩') switch(x)case{90,100}disp(' 等级为A')case{80,89}disp(' 等级为B')case{70,79}disp(' 等级为C')case{60,69}disp(' 等级为D')case{0,59}disp(' 等级为E')otherwisedisp(' 成绩错误')end>>Num=input('输入员工的工号:');Time=input('该工号员工的工作时数:'); salary=input(' 该员工底薪:'); if Time>=120salary=salary*(1+0.15);elseif Time<=60salary=salary-700;elseif Time>6 0&Time<120salary=Time*84;endsalary=salary实验四1:程序:n=input('输入n:');for a=1: n;b(a)=1/(a A2);endpi=sqrt(6*sum(b))2:.程序:sum=0;for n=1:200;b( n)=1/(2* n-1); sum=sum+b( n); if(sum>=3) break;endendn-14.程序:f(1)=1;max=0;f(2)=0;mi n=0; f(3)=1;c=0; zhengshu=0; fushu=0;zero=0;for n=4:100f(n )=f( n-1)-2*f( n-2)+f( n-3);endfor m=1:100if(f(m)>max)max=f(m);endif(f(m)<mi n)min=f(m);endc=sum(f);if(f>0)zhengshu=zhengshu+1; end if(f(m)<0) fushu=fushu+1;endif(f(m)==O) zero=zero+1;endif(f(m)>0)zhengshu=zhengshu+1; end if(f(m)<0) fushu=fushu+1;endif(f(m)==O)zero=zero+1;enddisp('最大值')maxdisp('最小值')mindisp('和')cdisp('正数的个数')zhengshudisp('负数的个数')fushudisp('零的个数')zero实验五1, 函数文件:文件名:Yunsuan程序:fun cti on[ z,d,s,c]=yun sua n( a,b)z=exp(a+b*i);d=log(a+b*i);s=s in( a+b*i);c=cos(a+b*i);disp([' 复数',num2str(a),屮,num2str(b),'i 的指数为:',num2str⑵,’,对数为:’,num2str(d),' ,正弦为:’,num2str(s),',余弦为:’,num2str(c)])函数调用:程序:clearx=input('请输入复数实部的值');y=input('请输入复数虚部的值');[x,y]=yun sua n( x,y);2函数文件:文件名:zhu程序:fun cti on[ p,q,w]=zhu(p,q,w)A=[p*cos(w),-p,-si n( w),O;p*si n( w),0,cos(w),0;0,q,-si n( w),0;0,0,-cos( w),1];B=[0;9.8*p;0;9.8*q];X=A\B函数调用:程序:clearm1=i nput('m仁');m2=i nput('m2=');th=in put('theta=');theta=pi*th/180;[m1,m2,theta]=zhu(m1,m2,theta);4函数文件:文件名:ha nshu程序:fun cti on[y ]=ha nshu(x) y=1/((x-2).A2-0.1)+1./((x-3).A4+0.01);实验六程序:、x=0:0.02*pi:2*pi;y=(0.5+3*si n(x)./(1+x.*x)).*cos(x)plot(x,y)程序:x=(0:0.02*pi:2*pi)';y1=x.*x;y2=cos(2*x);y3=y1.*y2;plot(x,y1,'y-'),hold onplot(x,y2,'c-'),hold onplot(x,y3,'k-'),hold off程序:x=(0:0.02*pi:2*pi)';y1=x.*x;y2=cos(2*x);y3=y1.*y2;subplot(2,2,1) stairs(x,y1); plot(x,y1,'r-'),hold on subplot(2,2,2) stairs(x,y2);plot(x,y2,'b-'),hold on subplot(2,2,3) stairs(x,y3);plot(x,y3,'k-'),hold off(1)绘制条形图程序:x=(0:0.2*pi:2*pi)'; y1=x.*x; y2=cos(2*x); y3=y1.*y2;subplot(2,2,1) stairs(x,y1); bar(x,y1) subplot(2,2,2) stairs(x,y2); bar(x,y1)subplot(2,2,3) stairs(x,y3); bar(x,y3)(2)绘制梯形图程序:x=(0:0.05*pi:2*pi)'; y1=x.*x; y2=cos(2*x); y3=y1.*y2;subplot(2,2,1) stairs(x,y1); subplot(2,2,2) stairs(x,y2);subplot(2,2,3) stairs(x,y3);(2)杆图:程序:x=(0:0.05*pi:2*pi)';y1=x.*x; y2=cos(2*x); y3=y1.*y2; subplot(2,2,1) stem(x,y1);subplot(2,2,2) stem(x,y2);subplot(2,2,3) stem(x,y3);(3)填充图程序:x=(0:0.05*pi:2*pi)';y1=x.*x; y2=cos(2*x);y3=y1.*y2; subplot(2,2,1) fill(x,y1,'r');subplot(2,2,2) fill(x,y2,'b');subplot(2,2,3) fill(x,y3,'g');1、:程序:x=li nspace(-5,5,1000);y=[];for x0=xif x0<=0y=[y,(x0+sqrt(pi))./exp (2)];else y=[y,0.5*log(x0+sqrt(1+x0.*x0))]; endendplot(x,y)程序:a=input('请输入a的值') b=input('请输入b的值') n=input('请输入n的值')theta=0:0.01:2*pi;rho=a.*s in (b+n.*theta) polar(theta,rho)程序:x=-l in space(-5,5,21);y=li nspace(0,10,31);[x,y]=meshgrid(x,y) z=cos(x).*cos(y).*exp(-0.25*sqrt(x.A2+y.A2)) subplot(1,2,1); title('surf(x,y,z)'); surf(x,y,z);subplot(1,2,2);p=100*ra nd(100,5); [MAXOBJECT,U]=max(p) [MAXOBJECT,V]=mi n(p) EX=mea n(p) % S=std(p) % SUM_1=sum(p,2);[MAXSUM,W]=max(SUM) 序号 [MINSUM,Y]=mi n(SUM) 序号 [zcj,xsxh]=sort(SUM_1); zcj(e nd:-1:1); xsxh(e nd:-1:1); 求五门课总分的最低分,丫记录最低分学生的程序:x=ra nd(1,30000);EX=mean(x) %返回向量x 的算术平均值 S=std(x) % 返回向量x 的标准方差 MAX=max(x) %返回向量x 的最大元素 MIN=min(x) %返回向量x 的最小元素 p=sum(x>0.5)/30000 %满足条件的百分比 程序:%求每门课的最高分,U 记录最大元素的行号求每门课的最低分,V 记录最小元素的行号 每门课的平均分 每门课的标准方差 求每名学生的总成绩 求五门课总分的最高分,W 记录最高分学生的实验九 2、程序:I1=quad('sqrt(cos(t.A 2)+4*si n(2*t).A 2+1)',0,2*pi)I2=quad('(log(1+x))./(1+x.*x)', 0,1)程序:A=[6,5,-2,5;9,-1,4,-1;3,4,2,-2;3,-9,0,2]b=[-4;13;1;11]x=i nv(A)*b实验十 程序:x=sym('6');y=sym('5');z=(x+1)/(sqrt(3+x)-sqrt(y))1、分解因式(1) 程序:syms x y;A=xA4-yA4;factor(A)(2)title('surfc(x,y,z)');surfc(x,y,z); 实验八程序:factor(sym('5135'))3、化简表达式(1)程序:syms betal beta2y=si n(beta1)*cos(beta2)-cos(beta1)*si n( beta2) simple(y)(2)程序:syms xy=(4*xH+8*x+3)/(2*x+1)simple(y)5、用符号方法求下列极限或导数(1)程序:syms xf=(x*(exp(si n(x))+1)-2*(exp(ta n(x))-1))/(si n(x)) limit(f) (2)程序:syms xy=(sqrt(pi)-sqrt(acos(x)))/(sqrt(x+1));limit(f,x,-1,'right')(3)程序:syms xy=(1-cos(2*x))/x;y1=diff(y)y2=diff(y,x,2)6用符号方法求下列积分(1)程序:syms xf=1/(1+x A4+x A8)in t(f)(2)程序:syms xf=1/(((asi n(x))A2)*sqrt(1-xA2))in t(f)(3)程序:syms xf=(x A2+1)/(x A4+1)in t(f,x,O,i nf)(4)程序:syms xf=exp(x)*(1+exp(x)F2 y=i nt(f,x,0,log(2)) double(y)版权申明本文部分内容,包括文字、图片、以及设计等在网上搜集整理。

MATLAB(刘卫国)部分实验答案

MATLAB(刘卫国)部分实验答案
disp(['a=',num2str(a)])
b=fix(rand(1)*89)+10;
disp(['b=',num2str(b)])
x=input('请输入一个四则运算符号','s');
if x=='+'
c=a+b;
elseif x=='-'
c=a-b;
elseif x=='*'
c=a*b;
elseif x=='/'
else
y3=x^2-x-1;
disp(['y= ',num2str(y3)])
end
end
第二题:
clear all
x=input('请输入成绩:');
while (x>100|x<0)
x=input('输入错误,请重新输入成绩:'); end
switch fix(x/10)
case {9,10}
disp(['您本月工资是:',num2str(a)])
elseif y<60
b=84*y-700;
disp(['您本月工资是:',str(b)])
else
c=84*y;
disp(['您本月工资是:',num2str(c)])
end
第四题:
clear all
a=fix(rand(1)*89)+10;
end
实验5
第三题:
function a=prime(b)
x=fix(b/10);

MATLAB程序设计与应用(刘卫国编)课后实验答案

MATLAB程序设计与应用(刘卫国编)课后实验答案

实验一 MATLAB 运算基础1. 先求下列表达式的值,然后显示MATLAB 工作空间的使用情况并保存全部变量。

(1) 0122sin851z e =+(2) 21ln(2z 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 --+=++=--(4) 2242011122123t t z t t t t t ⎧≤<⎪=-≤<⎨⎪-+≤<⎩,其中t =0:: 解:2. 已知: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 和B1234530166789101769,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 O S +⎡⎤=⎢⎥⎣⎦。

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+eyeA*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<D E&D E|D ~E|~D第三章1 从键盘输入一个3位数,将它反向输出,如输入639,出数936。

f=input('输入一个数:','s');f(end :-1:1)2 用if语句score=input('请输入成绩:'); if score>=90&&score<=100 disp('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;Plot(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程序设计教程[第二版]刘卫国课后参考答案及解析

第二章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<D E&D E|D ~E|~D第三章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;Plot(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程序的设计与应用(刘卫国编)课后实验答案

实验一 MATLAB 运算基础1. 先求下列表达式的值,然后显示MATLAB 工作空间的使用情况并保存全部变量。

(1) 0122sin851z e =+(2) 21ln(2z 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 解:2. 已知: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 和B1234530166789101769,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 O S +⎡⎤=⎢⎥⎣⎦。

刘卫国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工作空间的使用情况并保存全部变量。

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

实验六
2_1
clear;
x=linspace(0,2*pi,101);
y1=x.^2;
y2=cos(2.*x);
y3=y1.*y2;
plot(x,y1,'b-',x,y2,'r:',x,y3,'g-.'); %y1蓝色实线,y2红色虚线,y3绿色点画线
2_2
subplot(2,2,1);
%分四个子图(先画2行2列第1块) plot(x,y1);
subplot(2,2,2);
plot(x,y2),
subplot(2,2,3);
plot(x,y3);
2_3 ()
subplot(3,4,1); %y1的四种图形bar(x,y1);
subplot(3,4,2);
stairs(x,y1),
subplot(3,4,3);
stem(x,y1);
subplot(3,4,4);
fill(x,y1,'b');
subplot(3,4,5); %y2
bar(x,y2); %条形图subplot(3,4,6);
stairs(x,y2), %阶梯图subplot(3,4,7);
stem(x,y2); %杆图subplot(3,4,8);
fill(x,y2,'b'); %填充图,注意必须加填充颜色
subplot(3,4,9); %y3
bar(x,y3);
subplot(3,4,10);
stairs(x,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
5.程序:
x=linspace(-5,5,21);
y=linspace(0,10,31);
[x,y]=meshgrid(x,y);
z=cos(x).*cos(y).*exp(-0.25*sqrt(x.^2+y.^2)); subplot(1,2,1);
title('surf(x,y,z)');
surf(x,y,z);
subplot(1,2,2);
title('surfc(x,y,z)');
surfc(x,y,z);
实验七
701
clear all
hf=figure('Color',[1,0,0],'KeyPressFcn','disp(''Left Button Pressed'')');
702
clear all
x=linspace(-2*pi,2*pi,500);
y=x.^2.*exp(2*x);
h=plot(x,y);
set(h,'Color','r','LineStyle',':','LineWidth',5)
title('下图是y=x^2e^{2x}曲线的图像');
703
clear all
a=linspace(-2*pi,2*pi,40);
b=linspace(-2*pi,2*pi,40);
[x,t]=meshgrid(a,b);
v=10*exp(-0.01*x).*sin(2000*pi*t-0.2*x+pi);
axes('view',[-37.5,30]);
h=surface(x,t,v,'FaceColor','w','EdgeColor','flat');
grid on;
title('函数图像如下');
set(h,'FaceColor','flat');
705
%实验七底层绘图操作
clear all;
[x,y,z]=cylinder(3,500);%cylinder是生成柱体的函数
surf(x,y,z)
title('实验七第五题圆柱体的光照和材料处理');
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
axis([-5,5,-5,5,0,1])
grid off;
light('Color','r','Position',[-4,0,0],'Style','infinite');
shading interp;
material shiny;
view(0,10);
lighting phong;
axis off;
实验九
程序:
I1=quad('sqrt(cos(t.^2)+4*sin(2*t).^2+1)',0,2*pi) I2=quad('(log(1+x))./(1+x.*x)',0,1)
程序:
A=[6,5,-2,5;9,-1,4,-1;3,4,2,-2;3,-9,0,2]
b=[-4;13;1;11]
x=inv(A)*b
实验十
程序:
x=sym('6');
y=sym('5');
z=(x+1)/(sqrt(3+x)-sqrt(y))分解因式
(1)
程序:
syms x y;
A=x^4-y^4;
factor(A)
(2)
程序:
factor(sym('5135'))
3、化简表达式
(1)
程序:
syms beta1 beta2
y=sin(beta1)*cos(beta2)-cos(beta1)*sin(beta2)
simple(y)
(2)
程序:
syms x
y=(4*x^2+8*x+3)/(2*x+1)
simple(y)
4.(1)p1=sym(‘[0 1 0;1 0 0;0 01]’);p2= sym(‘[1 0 0;0 1 0;1 0 1]’);A= sym(‘[a b c;d e f;g h l]’);
B=P1*P2*A
(2)inv(B) (3)tril(B) (4)determ(B)
5、用符号方法求下列极限或导数
(1)
程序:
syms x
f=(x*(exp(sin(x))+1)-2*(exp(tan(x))-1))/(sin(x)) limit(f)
(2)
程序:
syms x
y=(sqrt(pi)-sqrt(acos(x)))/(sqrt(x+1));
limit(f,x,-1,'right')
(3)
程序:
syms x
y=(1-cos(2*x))/x;
y1=diff(y)
y2=diff(y,x,2)
6、用符号方法求下列积分
(1)
程序:
syms x
f=1/(1+x^4+x^8)
int(f)
(2)
程序:
syms x
f=1/(((asin(x))^2)*sqrt(1-x^2)) int(f)
(3)
程序:
syms x
f=(x^2+1)/(x^4+1)
int(f,x,0,inf)
(4)
程序:
syms x
f=exp(x)*(1+exp(x))^2 y=int(f,x,0,log(2)) double(y)。

相关文档
最新文档