MATLAB 上机 习题及答案.docx

上传人:b****4 文档编号:24229148 上传时间:2023-05-25 格式:DOCX 页数:40 大小:324.73KB
下载 相关 举报
MATLAB 上机 习题及答案.docx_第1页
第1页 / 共40页
MATLAB 上机 习题及答案.docx_第2页
第2页 / 共40页
MATLAB 上机 习题及答案.docx_第3页
第3页 / 共40页
MATLAB 上机 习题及答案.docx_第4页
第4页 / 共40页
MATLAB 上机 习题及答案.docx_第5页
第5页 / 共40页
点击查看更多>>
下载资源
资源描述

MATLAB 上机 习题及答案.docx

《MATLAB 上机 习题及答案.docx》由会员分享,可在线阅读,更多相关《MATLAB 上机 习题及答案.docx(40页珍藏版)》请在冰豆网上搜索。

MATLAB 上机 习题及答案.docx

MATLAB上机习题及答案

 

15、今有多项式P1(x)=x4-2x+1,P2(x)=x2+4x-0.5,要求先求得P(x)=P1(x)+P2(x),然后计算xi=0.2*i各点上的P(xi)(i=0,1,2,…,5)值。

p1=[1.00.00.0-2.01.0];

>>p2=[0.00.01.04.0-0.5];

>>p1x=poly2sym(p1);p2x=poly2sym(p2);

>>p=p1x+p2x

p=

x^4+2*x+1/2+x^2

>>x=0:

5;

>>x.^4+2*x+1/2+x.^2

ans=

0.50004.500024.500096.5000280.5000660.5000

 

1、试个MATLAB的工作空间中建立以下2个矩阵:

A=[12]

,求出矩阵A和B的乘积,并将结果赋给变量C。

>>A=[12]

A=

12

>>B=[12

34]

B=

12

34

>>C=A*B

C=

710

 

2、利用MATLAB提供的帮助信息,了解inv命令的调用格式,并作简要说明。

helpinv

INVMatrixinverse.

INV(X)istheinverseofthesquarematrixX.

AwarningmessageisprintedifXisbadlyscaledor

nearlysingular.

SeealsoSLASH,PINV,COND,CONDEST,LSQNONNEG,LSCOV.

Overloadedmethods

helpgf/inv.m

helpzpk/inv.m

helptf/inv.m

helpss/inv.m

helplti/inv.m

helpfrd/inv.m

helpsym/inv.m

helpidmodel/inv.m

 

3、使用help命令查询函数plot的功能以及调用方法,然后利用plot命令绘制函数y=sin(x)的图形,其中

helpplot

PLOTLinearplot.

PLOT(X,Y)plotsvectorYversusvectorX.IfXorYisamatrix,

thenthevectorisplottedversustherowsorcolumnsofthematrix,

whicheverlineup.IfXisascalarandYisavector,length(Y)

disconnectedpointsareplotted.

PLOT(Y)plotsthecolumnsofYversustheirindex.

IfYiscomplex,PLOT(Y)isequivalenttoPLOT(real(Y),imag(Y)).

InallotherusesofPLOT,theimaginarypartisignored.

Variouslinetypes,plotsymbolsandcolorsmaybeobtainedwith

PLOT(X,Y,S)whereSisacharacterstringmadefromoneelement

fromanyorallthefollowing3columns:

bblue.point-solid

ggreenocircle:

dotted

rredxx-mark-.dashdot

ccyan+plus--dashed

mmagenta*star

yyellowssquare

kblackddiamond

vtriangle(down)

^triangle(up)

>triangle(right)

ppentagram

hhexagram

Forexample,PLOT(X,Y,'c+:

')plotsacyandottedlinewithaplus

ateachdatapoint;PLOT(X,Y,'bd')plotsbluediamondateachdata

pointbutdoesnotdrawanyline.

PLOT(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...)combinestheplotsdefinedby

the(X,Y,S)triples,wheretheX'sandY'sarevectorsormatrices

andtheS'sarestrings.

Forexample,PLOT(X,Y,'y-',X,Y,'go')plotsthedatatwice,witha

solidyellowlineinterpolatinggreencirclesatthedatapoints.

ThePLOTcommand,ifnocolorisspecified,makesautomaticuseof

thecolorsspecifiedbytheaxesColorOrderproperty.Thedefault

ColorOrderislistedinthetableaboveforcolorsystemswherethe

defaultisblueforoneline,andformultiplelines,tocycle

throughthefirstsixcolorsinthetable.Formonochromesystems,

PLOTcyclesovertheaxesLineStyleOrderproperty.

PLOTreturnsacolumnvectorofhandlestoLINEobjects,one

handleperline.

TheX,Ypairs,orX,Y,Striples,canbefollowedby

parameter/valuepairstospecifyadditionalproperties

ofthelines.

SeealsoSEMILOGX,SEMILOGY,LOGLOG,PLOTYY,GRID,CLF,CLC,TITLE,

XLABEL,YLABEL,AXIS,AXES,HOLD,COLORDEF,LEGEND,SUBPLOT,STEM.

Overloadedmethods

helpcfit/plot.m

helpfints/plot.m

helpcgrules/Plot.m

helpxregtwostage/plot.m

helpxregtransient/plot.m

helpxregmodel/plot.m

helplocalmod/plot.m

helpsweepset/plot.m

helpmdevtestplan/plot.m

helpcgdatasetnode/plot.m

helpcgdatadisplay/plot.m

helpidmodel/plot.m

helpiddata/plot.m

helpntree/plot.m

helpdtree/plot.m

helpwvtree/plot.m

helprwvtree/plot.m

helpedwttree/plot.m

>>a=0:

0.01:

pi;

>>y=sin(a);

>>plot(y)

 

4、试用不同的方法建立数组A=[11.52.02.53.0],了解怎样访问数组A的第二个元素,然后将其更换为4.0。

A=[11.52.02.53.0]

A=

1.00001.50002.00002.50003.0000

>>A

(2)

ans=

1.5000

>>A

(2)=4.0

A=

1.00004.00002.00002.50003.0000

 

5、已知矩阵

,试用MATLAB提供的关系运算命令将B中所有大于2的元素全改为0。

>>B=[125

072

631]

B=

125

072

631

>>B(find(B>2))

ans=

6

7

3

5

>>B(find(B>2))=0

B=

120

002

001

 

6、已知矩阵

,试求矩阵A的左右翻转矩阵,上下翻转矩阵,然后在工作空间中利用size命令查看矩阵A的大小。

A=[123

456

789]

A=

123

456

789

>>fliplr(A)

ans=

321

654

987

>>flipud(A)

ans=

789

456

123

>>size(A)

ans=

33

7、已知矩阵

,试求其转置、逆、迹、特征值、特征向量和B对应的行列式的值。

B=[12

34]

B=

12

34

>>B'

ans=

13

24

>>inv(B)

ans=

-2.00001.0000

1.5000-0.5000

>>trace(B)

ans=

5

eig(B)

ans=

-0.3723

5.3723

>>det(B)

ans=

-2

8、分别建立一个

阶的单位阵、随机阵和魔方阵。

eye(3)

ans=

100

010

001

>>rand(3)

ans=

0.95010.48600.4565

0.23110.89130.0185

0.60680.76210.8214

>>magic(3)

ans=

816

357

492

9、已知多项式

试求两个多项式的和与乘积。

symsx

>>a=x^2+2*x-2

a=

x^2+2*x-2

>>b=x^3+x^2-3*x+1

b=

x^3+x^2-3*x+1

>>a+b

ans=

2*x^2-x-1+x^3

>>a*b

ans=

(x^2+2*x-2)*(x^3+x^2-3*x+1)

10、复数

表达,及计算

z1=3+4*i

z1=

3.0000+4.0000i

>>z2=1+2i

z2=

1.0000+2.0000i

>>z3=2*exp(pi/6*i)

z3=

1.7321+1.0000i

>>z=z1*z2/z3

z=

0.3349+5.5801i

11、产生1×5的均布随机数组,进行如下操作:

1)寻访数组的第三个元素;2)寻访数组的第一、二、五个元素组成的子数组;3)寻访前三个元素组成的子数组;

4)寻访除前2个元素外的全部其他元素。

A=rand(1,5)

A=

0.44470.61540.79190.92180.7382

>>A(3)

ans=

0.7919

>>A([125])

ans=

0.44470.61540.7382

>>A(1:

3)

ans=

0.44470.61540.7919

>>A(2:

end)

ans=

0.61540.79190.92180.7382

12、试用两种方法用MATLAB计算

1)>>symsx

>>x=solve('x^3=-8','x')

x=

[-2]

[1-i*3^(1/2)]

[1+i*3^(1/2)]

2)x=-8^(1/3)

x=

-2

13、求

的“商”及“余”多项式。

p1=conv([1,0,2],conv([1,4],[1,1]));

>>p2=[1011];

>>[q,r]=deconv(p1,p2)

q=

15

 

r=

00543

14、求方程x^4+7x^3+9x-20=0的全部根。

p=[1709-20];

>>roots(p)

ans=

-7.2254

-0.4286+1.5405i

-0.4286-1.5405i

1.0826

16、已知一线性方程组如下所示:

,试求其结果。

>>a=[3.01.0-1.0;1.02.04.0;-1.04.05.0]

a=

31-1

124

-145

>>b=[3.6;2.1;-1.4]

b=

3.6000

2.1000

-1.4000

>>x=a\b

x=

1.4818

-0.4606

0.3848

17、已知矩阵A=

914358

-891435

-5-8-9143

-3-5-8914

-4-3-5-891

-1-4-3-5-89

(1)此矩阵的秩;

(2)此矩阵的行列式的值;(3)此矩阵的迹;(4)此矩阵的特征多项式的系数及多项式的根;(6)此矩阵的逆阵;(7)此矩阵的特征值与特征向量

A=[914358

-891435

-5-8-9143

-3-5-8914

-4-3-5-891

-1-4-3-5-89]

A=

914358

-891435

-5-8-9143

-3-5-8914

-4-3-5-891

-1-4-3-5-89

(1)>>rank(A)

ans=

6

(2)>>det(A)

ans=

595024

(3)>>trace(A)

ans=

36

(4)>>poly(A)

ans=

1.0e+005*

Columns1through6

0.0000-0.00040.0060-0.07600.7135-3.5599

Column7

5.9502

>>B=poly(A)

B=

1.0e+005*

Columns1through6

0.0000-0.00040.0060-0.07600.7135-3.5599

Column7

5.9502

>>roots(B)

ans=

-0.1622+12.0911i

-0.1622-12.0911i

14.5379

9.3652+1.9707i

9.3652-1.9707i

3.0561

(6)>>inv(A)

ans=

0.0038-0.0939-0.35100.25920.18330.0302

-0.0571-0.0215-0.48830.33880.25920.0461

0.08640.09560.5906-0.4883-0.3510-0.0708

0.01890.02160.0956-0.0215-0.0939-0.0407

0.04170.01890.0864-0.05710.0038-0.0515

0.05150.04070.0708-0.0461-0.03020.0430

(7)>>[vd]=eig(A)

v=

Columns1through4

0.2562+0.3857i0.2562-0.3857i-0.46910.0200+0.1849i

0.0680+0.4650i0.0680-0.4650i0.2202-0.8084

-0.4968-0.49680.02840.3485-0.0625i

-0.2618+0.0993i-0.2618-0.0993i-0.31180.0639-0.1206i

-0.3238-0.0038i-0.3238+0.0038i0.53850.2438+0.1483i

-0.3360-0.1475i-0.3360+0.1475i-0.5861-0.2942-0.0029i

Columns5through6

0.0200-0.1849i0.3706

-0.80840.6762

0.3485+0.0625i-0.6252

0.0639+0.1206i-0.0948

0.2438-0.1483i-0.0679

-0.2942+0.0029i0.0307

 

d=

Columns1through4

-0.1622+12.0911i000

0-0.1622-12.0911i00

0014.53790

0009.3652+1.9707i

0000

0000

Columns5through6

00

00

00

00

9.3652-1.9707i0

03.0561

>>

习题二

1、编制一个函数,使得该函数能对输入的两个数值进行比较,并返回其中的最小值。

functionm=min(a,b)

if(a

m=a;

else

m=b;

end

>>min(2,1)

ans=

1

2、试编一个m程序,将一维数组x中的N个数按颠倒的次序重新存储。

如N=5,原来x为:

x=[13579]而经过颠倒处理后x中数据的次序应该为:

x=[97531]

functionb=fun1(x)

N=length(x)

fori=1:

N

b(i)=x(N-i+1);

end

fun1([13579])

N=

5

 

ans=

97531

>>

3、编制一个m程序,计算阶乘n!

=1×2×3×…×n

functionm=fun2(n)

m=1;

fori=1:

n

m=m*i;

end

fun2(3)

ans=

6

>>

4、利用循环语句进行程序设计:

假设定义m×n的矩阵A。

判断矩阵A的第1列元素是否为0,若全为0,则从矩阵A中删除第1列

functionb=fun3(A)

[m,n]=size(A)

fori=1:

m

ifA(i,1)==0

YC=0;

continue;

else

YC=1;

break;

end

end

ifYC==0A(:

1)=[];

b=A;

elseb=A;

end

5、利用循环语句进行程序设计:

在区间[-2,-0.75]内,步长为0.25,对函数y=f(x)=1+1/x求值,并列表。

将所得x值和y值分别存入向量r和s中。

forr=-2:

0.25:

-0.75

x=r

y=1+1/x;

s=y

end

6、编程计算

k=0;

fori=0:

63

k=k+2^i;

end

k=

1.8447e+019

习题三

1、用subplot命令在同一图形输出窗口中绘制以下4个函数的图形:

x=-3:

0.01:

3;

y1=x;y2=x.*sin(x);y3=x.^2;y4=tan(x);

subplot(2,2,1),plot(x,y1);axis([0,3,-1,3])

subplot(2,2,2),plot(x,y2);axis([-1,1,-pi,pi])

subplot(2,2,3),plot(x,y3);axis([0,1.5,-1,3])

subplot(2,2,4),plot(x,y4);axis([0,1.3,-1,3])

2、绘制曲线

区间上的阶梯图。

x=0:

0.1:

5*pi;

y=exp(-0.2*x).*sin(x);

stairs(y);

3、试绘制以极坐标形式表示的图形:

,其中

的范围为

x=0:

0.01*pi:

8*pi;

y=cos(5/4*x)+1/3;

polar(x,y);

4、画出衰减振荡曲线

及其它的包络线

的取值范围是

x=0:

0.01*pi:

4*pi;

y=exp(-x/3).*sin(3*x);

y0=exp(-x/3);

plot(y);

holdon

plot(y0);

holdoff;

5、画出

所表示的三维曲面。

的取值范围是

x=-8:

0.5:

8;

y=x;

[X,Y]=meshgrid(x,y);

Z=sin((X.^2+Y.^2).^1/2)./(X.^2+Y.^2)^1/2;

surf(X,Y,Z);

6、在[02π]范围内绘制二维曲线图y=sin(x)*cos(5x)。

x=0:

0.01:

2*pi;

y=sin(x).*cos(5*x);

plot(y);

7、在[02π]范围内绘制以Y轴为对数的二维曲线图。

y=|1000sin(4x)|+1

x=0:

0.01:

2*pi;

y=abs(1000*sin(4*x))+1;

semilogy(x,y);

8、绘制z=sin(x)*cos(y)的三维网格和三维曲面图,x,y变化范围均为[02π]。

x=0:

0.1:

2*pi;

y=x;

[X,Y]=meshgrid(x,y);

Z=sin(X).*cos(Y);

subplot(2,1,1);

mesh(X,Y,Z);

subplot(2,1,2);

surf(X,Y,Z);

9、用简短的MATLAB命令在一个图上绘制在0≤x≤7范围内的sin(2x)、

三条曲线,并将其一一标明。

x=0:

0.01:

7;

y1=sin(2*x);

y2=sin(x.^2);

y3=

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 党团建设

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1