MATLAB实验题答案

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

1、求以下变量的值,并在MATLAB 中验证。

( 1 ) a = 1 : 2 : 5 a =

1 3 5

( 2 ) b = [ a' , a' , a' ;a ]

b = 1 1 1 3 3 3 5 5 5 1 3 5 ( 3 )

c = a + b ( 2 , : ) c = 4 6 8

2、下列运算是否合法,为什么?如合法,结果

是多少?

>> result2=a*b Error using * Inner matrix dimensions must agree. >> result3=a+b result3 = 3 6 2 5 8 11 >> result4=b*d result4 = 31 22 22 40 49 13 >> result5=[b;c']*d result5 =

31 22 22 40 49 13 -5 -8 7 >> result6=a.*b result6 =

2 8 -

3

4 1

5 30 >> result7=a./b result7 =

0.5000 0.5000 -3.0000 4.0000 1.6667 1.2000

>> result8=a.c

Attempt to reference field of non-structure array. >> result9=a.\b result9 = 2.0000 2.0000 -0.3333 0.2500 0.6000 0.8333 >> result10=a.^2 result10 = 1 4 9 16 25 36 >> result11=2.^a

result11 = 2 4 8 16 32 64 3、用MATLAB 求解下面的的方程组。 (1)⎥

⎥⎥

⎤⎢⎢⎢⎢⎣⎡-=⎥⎥⎥⎥⎦⎤⎢⎢⎢⎢⎣⎡⨯⎥⎥⎥⎥⎦⎤⎢⎢⎢

⎢⎣⎡----017413231511222315921274321x x x x >> A=[7 2 1 -2;9 15 3 -2;-2 -2 11 5;1 3 2 13] >> B=[4 7 -1 0] >> B=B' >> x=inv(A)*B (2)⎪⎪⎩⎪⎪⎨⎧=-++=--=-++=++5

65333328

21w z y x w y x w z y x z y x >> A1=[1 1 1 0;1 2 1 -1;2 -1 0 -3;3 3 5 -6] >> B2=[1;8;3;5] >> x2=inv(A1)*B2

4、已知

⎥⎥⎥⎥

⎦⎤⎢⎢⎢

⎢⎣⎡----=1323151122231592127

A (1)求矩阵A 的秩(rank)

(2)求矩阵A 的行列式(determinant) (3)求矩阵A 的逆(inverse) (4)求矩阵A 的特征值及特征向量(eigenvalue and eigenvector) >> A3=[7 2 1 -2;9 15 3 -2;-2 -2 11 5;1 3 2 13] >> r=rank(A3) >> b=inv(A3) >> a=det(A3) >> [V,D]=eig(A3)

5、

10

99

101010

22222++++==---=∑

Λn n y ,求y=?(运行format long g 命令后,查看y 的值)

m1=0; for m=-10:10 m1=m1+2^m; end m1 m1 = 2047.9990234375

6、求分段函数的值。

⎪⎩⎪⎨⎧≥--<≤+-<-+=51506506222

x x x x x x x x x y 用if 语句实现,算出下列表中x 对应的y 值。

x=input('enter x='); if x<0 y=x^2+x-6; elseif x>=0&&x<5 y=x^2-5*x+6; else y=x^2-x-1; end y 7、分别用if 和switch 语句实现,将百分制成绩转换为成绩等级A 、B 、C 、D 、E 。其中90~100分为A ,80~89分为B ,70~79分为C ,60~69分为D ,60分以下为E 。对超出百分制范围的成绩,给出错误提示信息。

if 结构程序: x=input('please enter score='); if x>=90&&x<=100 disp('A') elseif x<90&&x>=80 disp('B')

elseif x<80&&x>=70 disp('C') elseif x<70&&x>=60

disp('D') elseif x<60&&x>=0 disp('E') else disp('error') end switch 结构程序: x=input('please enter score='); switch fix(x/10) case{10,9} if x>100

disp('error') else disp('A')

end case{8} disp('B')

case{7} disp('C')

case{6} disp('D') case{0,1,2,3,4,5} disp('E') otherwise disp('error') end 8、思考题 设计程序,完成成两位数的加、减、乘、除四则运算,即产生两个两位随机整数,再输入一个运算符号,做相应的运算,并显示相应的

结果。

x=input('请输入运算符')

a=num2str(floor(rand(1)*90+10)); a

b=num2str(floor(rand(1)*90+10)); b

if x=='+' y=a+b; elseif x=='-' y=a-b; elseif x=='*' y=a*b; elseif x=='/' y=a/b; else

disp('error') end y

9、启动MATLAB 后,点击File|New|M-File ,启

动MATLAB 的程序编辑及调试器

(Editor/Debugger ),编辑以下程序,点击

File|Save 保存程序,注意文件名最好用英

文字符。点击Debug|Run 运行程序,在命令窗口查看运行结果,程序如有错误则改正。

注:数论中一个有趣的题目:任意一个正整数,若为偶数,则用2除之,若为奇数,则与3相乘再加上1。重复此过程,最终得到的结果为1。

n=input('请输入n 值:'); a=n;

while n>1

if rem(n,2)==0 n=n/2; else

n=3*n+1; end a=[a,n]; end a

10、根据2222213121116n x ++++=Λ,当n 分别取100、1000、10000时,求x 的值分别是多少? a=input('请输入数值') n=0;

for m=1:100

n=n+1/(m*m); end n=6*n; x=sqrt(n); x

11、编程求满足∑=>m

k k 1100002的最小m

值。

sum=0; m=2; a=1; while a for i=1:m sum=sum+2^i; if sum>10000 a=0; end end m=m+1; end m

12、思考题 已知y 和

t 的函数关系:

!

20/...!3/!2/1)(2032t t t t t y +++++=求下面表格中与t 对应的y 值 t=input('请输入t 值:') sum=0; for i=1:20

b=factorial(i); sum=sum+t^i/b;

end

sum=sum+1; sum

相关文档
最新文档