实验一matlab运算基础与矩阵处理.docx

上传人:b****7 文档编号:10257421 上传时间:2023-02-09 格式:DOCX 页数:20 大小:107.35KB
下载 相关 举报
实验一matlab运算基础与矩阵处理.docx_第1页
第1页 / 共20页
实验一matlab运算基础与矩阵处理.docx_第2页
第2页 / 共20页
实验一matlab运算基础与矩阵处理.docx_第3页
第3页 / 共20页
实验一matlab运算基础与矩阵处理.docx_第4页
第4页 / 共20页
实验一matlab运算基础与矩阵处理.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

实验一matlab运算基础与矩阵处理.docx

《实验一matlab运算基础与矩阵处理.docx》由会员分享,可在线阅读,更多相关《实验一matlab运算基础与矩阵处理.docx(20页珍藏版)》请在冰豆网上搜索。

实验一matlab运算基础与矩阵处理.docx

实验一matlab运算基础与矩阵处理

实验报告

MATLAB软件与应用

 

姓名:

钟翠丽

学号:

201030770135

 

学院名称

理学院

专业班级

10应数1班

提交日期

2012年月

评阅人____________

评阅分数____________

 

实验一matlab运算基础与矩阵处理

一、实验目的

1.熟悉启动和退出MATLAB软件的方法。

2.熟悉MATLAB软件的运行环境。

3.熟悉MATLAB的基本操作。

4.掌握建立矩阵的方法。

5.掌握MATLAB各种表达式的书写规则以及常用函数的使用。

6.能用matlab进行基本的数组、矩阵运算。

7.能用矩阵求逆法解线性方程组。

二、实验原理

1.Matlab的启动

matlab系统的启动有三种常见方法:

1)使用Windows“开始”菜单。

2)运行matlab系统启动程序matlab.exe。

3)利用快捷方式。

2.Matlab系统的退出

要退出matlab系统,也有三种常见方法:

1)在matlab主窗口File菜单中选择Exitmatlab命令。

2)在matlab命令窗口输入Exit或Quit命令。

3)单击matlab主窗口的“关闭”按钮。

3.Matlab帮助窗口

进入帮助窗口可以通过以下三种方法:

1)单击matlab主窗口工具栏中的help按钮。

2)在命令窗口中输入helpwin、helpdesk或doc。

3)选择help菜单中的“matlabhelp”选项。

4.Matlab帮助命令

1)help命令

在matlab命令窗口直接输入help命令将会显示当前帮助系统中所包含的所有项目,即搜索路径中所有的目录名称。

同样,可以通过help加函数名来显示该函数的帮助说明。

2)lookfor命令

help命令只搜索出那些关键字完全匹配的结果,lookfor命令对搜索范围内的m文件进行关键字搜索,条件比较宽松。

3)模糊查询

用户只要输入命令的前几个字母,然后按tab键,系统就会列出所有以这几个字母开头的命令。

5.赋值语句

1)变量=表达式

2)表达式

6.矩阵的建立

1)直接输入法:

将矩阵的元素用方括号括起来,按矩阵行的顺序输入各元素,同一行的各元素之间用空格或逗号分开,不同行的元素之间用分号分开。

2)利用M文件建立矩阵

3)利用冒号表达式建立一个向量e1:

e2:

e3

4)利用linspace函数产生行向量linspace(a,b,n).

7.通用的特殊矩阵

常用的产生通用特殊矩阵的函数有:

zeros:

产生全0矩阵;

ones:

产生全1矩阵

eye:

产生单位矩阵;

rand:

产生0-1间均匀分布的随机矩阵;

randn:

产生均值为0,方差为1的标准正态分布的随机矩阵。

8.矩阵运算

1)矩阵加减运算:

假定有两个矩阵A和B,则可以由A+B和A-B实现矩阵的加减运算。

2)假定有两个矩阵A和B,若A为m×n矩阵,B为n×p矩阵,则C=A*B为m×p矩阵。

3)如果A矩阵是非奇异方阵,A\B等效与A的逆左乘B矩阵,inv(A)*B,而B/A等效于A矩阵的逆右乘B矩阵,也就是B*inv(A).

4)矩阵的乘方。

一个矩阵的乘方运算可以表示成A^x,要求A为方阵,x为标量。

9.矩阵点运算

在matlab中,有一种特殊的运算,因为其运算苻是在有关算术运算苻前面加点,因此叫点运算。

点运算符有.*,./,.\和.^。

两个矩阵进行点运算是指它们的对应元素进行相关运算,要求两矩阵的维参数相同。

三、实验内容

要求:

实验报告需要写出程序代码,实验步骤以及实验结果。

1.练习下面指令,写出每个指令的作用。

cd,clear,dir,path,help,who,whos,save,load。

解:

cd:

将用户目录设置成当前目录;

Clear:

用于删除MATLAB工作空间中的变量;

Dir:

列出一个目录或子目录中的所有文件;

Path:

可以把用户目录临时纳入搜索路径;

Help:

查询函数语法的最基本方法,查询信息直接显示在命令窗口;

Who:

用于显示在MATLAB工作空间中驻留的变量名清单;

Whos:

用于显示在MATLAB工作空间中驻留的变量名清单,还给出它们的维数、所占字节以及变量的类型;

Save:

可以生成MAT文件;

Load:

可以装入MAT文件。

 

2.建立自己的工作目录MYBIN和MYDATA,并将它们分别加到搜索路径的前面或者后面。

解:

(1)在桌面上新建一个文件夹,命名为MYBIN或MYDATA,建立工作目录;

(2)使用path命令把工作目录临时纳入搜索路径。

即在命令窗口输入命令:

path(path,’C:

\DocumentsandSettings\maths1\桌面\MYBIN‘)

path(path,’C:

\DocumentsandSettings\maths1\桌面\MYDATA')。

 

3.求

的算术运算结果。

解:

x=(12+2*(7-4))/3^2

x=

2

 

4.求出下列表达式的值,然后显示matlab工作空间的使用情况并保存全部变量:

 

解:

(1)z1=2*sin((85/180)*pi)/(1+exp

(2))

z1=

0.2375

(2)x1=[21+2*i;-0.455];

z2=0.5*log(x1+sqrt(1+x1^2))

z2=

0.7114-0.0253i0.8968+0.3658i

0.2139+0.9343i1.1541-0.0044i

matlab工作空间的使用情况:

保存全部变量:

 

5.利用MATLAB的帮助功能分别查询inv、plot、max、round函数的功能和用法。

解:

(1)>>helpinv

INVMatrixinverse.

INV(X)istheinverseofthesquarematrixX.

AwarningmessageisprintedifXisbadlyscaledor

nearlysingular.

Inv

Matrixinverse

Syntax

Y=inv(X)

Description

Y=inv(X)returnstheinverseofthesquarematrixX.AwarningmessageisprintedifXisbadlyscaledornearlysingular.

Inpractice,itisseldomnecessarytoformtheexplicitinverseofamatrix.Afrequentmisuseofinvariseswhensolvingthesystemoflinearequations

.Onewaytosolvethisiswithx=inv(A)*b.Abetterway,frombothanexecutiontimeandnumericalaccuracystandpoint,istousethematrixdivisionoperatorx=A\b.ThisproducesthesolutionusingGaussianelimination,withoutformingtheinverse.See\and/forfurtherinformation.

Examples

Hereisanexampledemonstratingthedifferencebetweensolvingalinearsystembyinvertingthematrixwithinv(A)*bandsolvingitdirectlywithA\b.ArandommatrixAoforder500isconstructedsothatitsconditionnumber,cond(A),is1.e10,anditsnorm,norm(A),is1.Theexactsolutionxisarandomvectoroflength500andtheright-handsideisb=A*x.Thusthesystemoflinearequationsisbadlyconditioned,butconsistent.

Ona300MHz,laptopcomputerthestatements

n=500;

Q=orth(randn(n,n));

d=logspace(0,-10,n);

A=Q*diag(d)*Q';

x=randn(n,1);

b=A*x;

tic,y=inv(A)*b;toc

err=norm(y-x)

res=norm(A*y-b)

produce

elapsed_time=

1.4320

err=

7.3260e-006

res=

4.7511e-007

whilethestatements

tic,z=A\b,toc

err=norm(z-x)

res=norm(A*z-b)

produce

elapsed_time=

0.6410

err=

7.1209e-006

res=

4.4509e-015

Ittakesalmosttwoandonehalftimesaslongtocomputethesolutionwithy=inv(A)*baswithz=A\b.Bothproducecomputedsolutionswithaboutthesameerror,1.e-6,reflectingtheconditionnumberofthematrix.Butthesizeoftheresiduals,obtainedbypluggingthecomputedsolutionbackintotheoriginalequations,differsbyseveralordersofmagnitude.Thedirectsolutionproducesresidualsontheorderofthemachineaccuracy,eventhoughthesystemisbadlyconditioned.

Thebehaviorofthisexampleistypical.UsingA\binsteadofinv(A)*bistwotothreetimesasfastandproducesresidualsontheorderofmachineaccuracy,relativetothemagnitudeofthedata.

Algorithm

InputsofTypeDouble

Forinputsoftypedouble,invusesthefollowingLAPACKroutinestocomputethematrixinverse:

Matrix

Routine

Real

DLANGE,DGETRF,DGECON,DGETRI

Complex

ZLANGE,ZGETRF,ZGECON,ZGETRI

InputsofTypeSingle

Forinputsoftypesingle,invusesthefollowingLAPACKroutinestocomputethematrixinverse:

Matrix

Routine

Real

SLANGE,SGETRF,SGECON,SGETRI

Complex

CLANGE,CGETRF,CGECON,CGETRI

 

(2)>>helpplot

PLOTLinearplot.

PLOT(X,Y)plotsvectorYversusvectorX.IfXorYisamatrix,

thenthevectorisplottedversustherowsorcolumnsofthematrix,

whicheverlineup.IfXisascalarandYisavector,disconnected

lineobjectsarecreatedandplottedasdiscretepointsverticallyat

X.

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(none)noline

yyellowssquare

kblackddiamond

wwhitevtriangle(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.

Ifyoudonotspecifyamarkertype,PLOTusesnomarker.

Ifyoudonotspecifyalinestyle,PLOTusesasolidline.

PLOT(AX,...)plotsintotheaxeswithhandleAX.

PLOTreturnsacolumnvectorofhandlestolineseriesobjects,one

handleperplottedline.

TheX,Ypairs,orX,Y,Striples,canbefollowedby

parameter/valuepairstospecifyadditionalproperties

ofthelines.Forexample,PLOT(X,Y,'LineWidth',2,'Color',[.600])

willcreateaplotwithadarkredlinewidthof2points.

Example

x=-pi:

pi/10:

pi;

y=tan(sin(x))-sin(tan(x));

plot(x,y,'--rs','LineWidth',2,...

'MarkerEdgeColor','k',...

'MarkerFaceColor','g',...

'MarkerSize',10)

 

(3)>>helpmax

MAXLargestcomponent.

Forvectors,MAX(X)isthelargestelementinX.Formatrices,

MAX(X)isarowvectorcontainingthemaximumelementfromeach

column.ForN-Darrays,MAX(X)operatesalongthefirst

non-singletondimension.

[Y,I]=MAX(X)returnstheindicesofthemaximumvaluesinvectorI.

Ifthevaluesalongthefirstnon-singletondimensioncontainmore

thanonemaximalelement,theindexofthefirstoneisreturned.

MAX(X,Y)returnsanarraythesamesizeasXandYwiththe

largestelementstakenfromXorY.Eitheronecanbeascalar.

[Y,I]=MAX(X,[],DIM)operatesalongthedimensionDIM.

WhenXiscomplex,themaximumiscomputedusingthemagnitude

MAX(ABS(X)).Inthecaseofequalmagnitudeelements,thenthephase

angleMAX(ANGLE(X))isused.

NaN'sareignoredwhencomputingthemaximum.WhenallelementsinX

areNaN's,thenthefirstoneisreturnedasthemaximum.

Example:

IfX=[284thenmax(X,[],1)is[789],

739]

max(X,[],2)is[8andmax(X,5)is[585

9],759].

 

(4)>>helpround

ROUNDRoundtowardsnearestinteger.

ROUND(X)roundstheelementsofXtothenearestintegers.

round

Roundtonearestinteger

Syntax

Y=round(X)

Description

Y=round(X)roundstheelementsofXtothenearestintegers.ForcomplexX,theimaginaryandrealpartsareroundedindependently.

Examples

a=[-1.9,-0.2,3.4,5.6,7.0,2.4+3.6i]

a=

Columns1through4

-1.9000-0.20003.40005.6000

Columns5through6

7.00002.4000+3.6000i

round(a)

ans=

Columns1through4

-2.000003.00006.0000

Columns5through6

7.00002.0000+4.0000i

 

6.写出完成下列操作的命令:

(1)建立3阶单位矩阵;

(2)建立5*6随机矩阵A,其元素为[100,200]范围内的随机整数;

(3)产生均值为1,方差为0.2的500个正态分布的随机数;

(4)产生和A同样大小的幺矩阵;

(5)将矩阵A的对角线元素加30;

(6)从矩阵A提取对角线元素,并以这些元素构成对角阵B。

解:

(1)B=eye(3)

B=

100

010

001.

(2)x2=100+(200-100)*rand(5,6);

>>A=fix(x2)

A=

181109115114165175

190127197142103174

1121541951911841

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

当前位置:首页 > 农林牧渔 > 水产渔业

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

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