版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
IntroductiontoMATLAB
MATLAB(shortforMatrixLaboratory)isaspecial-purposecomputerprogram
optimizedtoperformengineeringandscientificcalculations.Itstartedlifehasa
programdesignedtoperformmatrixmathematics,butovertheyearsithasgrown
intoaflexiblecomputingsystemcapableofsolvingessentiallyanytechnical
problem.
TheMATLABprogramimplementstheMATLABprogramminglanguageand
providesanextensivelibraryofpredefinedfunctionsthatmaketechnical
programmingtaskseasierandmoreefficient.ThisbookintroducestheMATLAB
languageandshowshowtouseittosolvetypicaltechnicalproblems.
MATLABisahugeprogram,withanincrediblyrichvarietyoffunctions.Even
thebasicversionofMATLABwithoutanytoolkitsismuchricherthanother
technicalprogramminglanguages.Therearemorethan1000functionsinthe
basicMATLABproductalone,andthetoolkitsextendthiscapabilitywithmany
morefunctionsinvariousspecialties.Thisbookmakesnoattempttointroducethe
usertoallofMALTLAB'sowntoolstolocatethecorrectfunctionforaspecific
purposefromtheenormouschoiceavailable.
AdvantagesofMATLAB
MATLABhasmanyadvantagescomparedwithconventionalcomputer
languagesfortechnicalproblemsolving.Amongthemarethefollowing:
1.EaseofUse
MATLABisaninterpretedlanguage,likemanyversionsofBasic.LikeBasic,itis
veryeasytouse.Theprogramcanbeusedasascratchpadtoevaluate
expressionstypedatthecommandline,oritcanbeusedtoexecutelarge
prewrittenprograms.Programsmaybeeasilywrittenandmodifiedwiththe
built-inintegrateddevelopmentenvironment,anddebuggedwiththeMATLAB
debugger.Becausethelanguageissoeasytouse,itisidealforeducationaluse,
andfortherapidprototypingofnewprograms.
Manyprogramdevelopmenttoolsareprovidedtomaketheprogrameasyto
use.aworkspacebrowser,andextensivedemos.
2.Platformindependence
MATLABissupportedonmanydifferentcomputersystems,providingalarge
measureofplatformindependence.Atthetimeofthiswriting,thelanguageis
supportedonwindows9x/NT/2000andmanydifferentversionsofUNIX.
Programswrittenonanyplatformwillrunonalloftheotherplatforms,anddata
fileswrittenonanyplatformmaybereadtransparentlyonanyotherplatforms,AS
dresult,
ProgramswritteninMATLABcanmigratetonewplatformswhentheneedsof
theuserchange.
3.PredefinedFunctions
MATLABcomescompletewithanextensivelibraryofpredefinedfunctions
thatprovidetestedandprepackagedsolutionstomanybasictechnicaltasks.For
example,supposethatyouarewritingaprogramthatmustcalculatethestatistics
associatedwithaninputdataset.Inmostlanguages,youwouldneedtowriteyour
ownsubroutinesorfunctionstoimplementcalculationssuchasthearithmetic
mean,standarddeviation,median,andsoforth.Theseandhundredsofother
functionsarebuiltrightintotheMATLABlanguage,makingyourjobmucheasier.
InadditiontothelargelibraryoffunctionsbuiltintothebasicMATLAB
language,manyspecial-purposetoolboxesareavailabletohelpsolvecomplex
problemsinspecificareas.Forexample,ausercanbuystandardtoolboxesto
solveproblemsinSignalProcessing,ControlSystems,Communications,Image
Processing,andNeuralNetworks,amongmanyothers.Thereisalsoanextensive
collectionoffreeuser-contributedMATLABprogramsthataresharedthroughthe
MATLABWebsite.
4.Device-IndependentPlotting
Unlikemostothercomputerlanguages,MATLABhasmanyintegralplotting
andimagingcommands.Theplotsandimagescanbedisplayedonanygraphical
outputdevicesupportedbythecomputeronwhichMATLABisrunning.This
capabilitymakesMATLABanoutstandingtoolforvisualizingtechnicaldata.
5.GraphicalUserInterface
MATLABincludestoolsthatallowaprogrammertointeractivelyconstructa
graphicaluserinterface(GUI)forhisorherprogram.Withthiscapability,the
programmercandesignsophisticateddataanalysisprogramsthatcanbe
operatedbyrelativelyinexperiencedusers.
6.MATLABCompiler
MATLAB'sflexibilityandplatformindependenceisachievedbycompiling
MATLABprogramsintoadevice-independencep-code,andtheninterpretingthe
p-codeinstructionsatruntime.ThisapproachissimilartothatusedbyMicrosoft
isVisualBasiclanguage.Unfortunately,theresultingprogramscansometimes
executeslowlybecausetheMATLABcodeisinterpretedratherthancompiled.We
willpointoutfeaturesthattendtoslowprogramexecutionwhenweencounter
them.
AseparateMATLABcompilerisavailable.ThiscompilercancompileaMATLAB
programintoatrueexecutablethatrunsfasterthantheinterpretedcode.Itisa
greatwaytoconvertaprototypeMATLABprogramintoanexecutablesuitablefor
saleanddistributiontousers.
DisadvantagesofMATLAB
MATLABhastwoprincipaldisadvantages.Thefirstisthatitisaninterpreted
language,andthereforecanexecutemoreslowlythancompiledlanguages.This
problemcanbemitigatedbyproperlystructuringtheMATLABprogramandby
theuseoftheMATLABcompilertocompilethefinalMATLABprogrambefore
distributionandgeneraluse.
Theseconddisadvantageiscost:AfullcopyofMATLABis5to10timesmore
expensivethanaconventionalCorFortrancompiler.Thisrelativelyhighcostis
morethanoffsetbythereducedtimerequiredforanengineerorscientistto
createaworkingprogram,soMATLABiscost-effectiveforbusinesses.However,it
istooexpensiveformostindividualstoconsiderpurchasing.Fortunately,thereis
alsoaninexpensiveStudentEditionofMATLAB,whichisagreattoolforstudents
wantingtolearnthelanguage.TheStudentEditionofMATLABisessentially
identicaltothefulledition.
Withtheintroductionofbranchesandloops,ourprogramsaregoingto
becomemorecomplex,anditwillgeteasiertomakemistakes.Tohelpavoid
programmingerrors,wewillintroduceaformalprogramdesignprocedurebased
onthetechniqueknownastop-downdesign.Wewillalsointroduceacommon
algorithmdevelopmenttoolknownaspseudocode.
IntroductionToTop-DownDesignTechniques
Supposethatyouareanengineerworkinginindustry,andthatyouneedto
writeaprogramtosolvesomeproblem.Howdoyoubegin?
Whengivenanewproblem,thereisanaturaltendencytositdownata
keyboardandstartprogrammingwithout“wasting“alotoftimethinkingabout
theproblemfirst.Itisoftenpossibletogetawaywiththis“onthefly“approach
toprogrammingforverysmallproblems,suchasmanyoftheexamplesinthis
book.Intherealworld,however,problemsarelarger;andaprogrammer
attemptingthisapproachwillbecomehopelesslyboggeddown.Forlarger
problems,itpaystocompletelythinkouttheproblemandtheapproachyouare
goingtotaketoitbeforewritingasinglelineofcode.
Weintroduceaformalprogramdesignprocessinthissection,andthenapply
thatprocesstoeverymajorapplicationdevelopedintheremainderofthebook.
Forsomeofthesimpleexamplesthatwewillbedoing,thedesignprocesswill
seemlikeoverkill;however,astheproblemsthatwesolvegetlargerandlarger,the
processbecomesmoreandmoreessentialtosuccessfulprogramming.
WhenIwasanundergraduate,oneofmyprofessorswasfondofsaying,
“programmingiseasy.Itisknowingwhattoprogramthatishard."hispointwas
forcefullydrivenhometomeafterIleftuniversityandbeganworkinginindustry
onlargerscalesoftwareprojects.Ifoundthatthemostdifficultportofmyjobwas
tounderstandtheproblemIwastryingtosolve.OnceIreallyunderstoodthe
problem,itbecameeasytobreaktheproblemapartintosmaller,moreeasily
manageablepieceswithwell-definedfunctions,andthentotacklethosepieces
oneatatime.
Top-downdesignistheprocessofstartingwithalargetaskandbreakingit
downintosmaller,moreeasilyunderstandablepieces,whichperformaportionof
thedesiredtask.Eachsubtaskmayinturnbesubdividedintosmallersubtasksif
necessary.Oncetheprogramisdividedintosmallpieces,eachpiececanbecoded
andtestedindependently.Wedonotattempttocombinethesubtasksintoa
completetaskuntileachofthesubtaskshasbeenverifiedtoworkproperlyby
itself.
Theconceptoftop-downdesignisthebasisofourformalprogramdesign
process.Wewillnowintroducethedetailsoftheprocess,whichisillustratedin
figure1thestepsinvolvedare:
1.Clearlystatetheproblemthatyouaretryingtosolve.
Programsareusuallywrittentofillsomeperceivedneed,butthatneedmay
notbearticulatedclearlybythepersonrequestingtheprogram.Forexample,a
usermayaskforaprogramtosolveasystemofsimultaneouslinearequations.
Thisrequestisnotclearenoughtoallowaprogrammertodesignaprogramto
meettheneed;heorshemustfirstknowmuchmoreabouttheproblemtobe
solved.Isthesystemofequationstobesolvedrealorcomplex?Whatisthe
maximumnumberofequationsandunknownthattheprogrammusthandle?Are
thereanysymmetryintheequationsthatmightbeexploitedtomakethetask
easier?Theprogramdesignerwillhavetotalkwiththeuserrequestingthe
program,andthetwoofthemwillhavecomeupwithaclearstatementofexactly
whattheyaretryingtoaccomplish.Aclearstatementoftheproblemwillprevent
misunderstandings,anditwillalsohelptheprogramdesignertoproperly
organizehisorherthoughts.Intheexamplewev/eredescribing,aproper
statementoftheproblemmighthavebeen:
FgS
Figure1
Designandwriteaprogramtosolveasystemofsimultaneouslinear
equationshavingrealcoefficientsandwithupto20equationsin20unknowns.
2.Definetheinputsrequiredbytheprogramandtheoutputstobeproduced
bytheprogram.
Theinputstotheprogramandtheoutputsproducedbytheprogrammustbe
specifiedsothatthenewprogramwillproperlyfitintotheoverallprocessing
scheme.Inthisexample,thecoefficientsoftheequationstobesolvedare
probablyinsomepre-existingorder,andournewprogramneedstobeableto
readtheminthatorder.Andournewprogramneedstobeabletoreadthemin
thatorder.Similarly,itneedstoproducetheanswersrequiredbytheprograms
thatmayfollowitintheoverallprocessingscheme,andtowriteoutthoseanswers
intheformatneededbytheprogramsfollowingit.
3.Designthealgorithmthatyouintendtoimplementintheprogram.
Analgorithmisastep-by-stepprocedureforfindingthesolutiontoaproblem.
Itisatthisstageintheprocessthattop-downdesigntechniquescomeintoplay.
Thedesignerlooksforlogicaldivisionswithintheproblem,anddividesitupinto
subtasksalongthoselines.Thisprocessiscalleddecomposition.Ifthesubtasks
arelarge,thedesignercanbreakthemupintoevensmallersub-tasks.Thisprocess
continuesuntiltheproblemhasbeendividedintorranysmallpieces,eachof
whichdoesasimple,clearlyunderstandablejob.
Aftertheproblemhasbeendecomposedintosmallpieces,eachpieceis
furtherrefinedthroughaprocesscalledstepwiserefinement.Instepwise
refinement,adesignerstartswithageneraldescriptionofwhatthepieceofcode
shoulddo,andthendefinesthefunctionsofthepieceingreaterandgreaterdetail
untiltheyarespecificenoughtobeturnedintoMATLABstatements.Stepwise
refinementisusuallydonewithpseudocode,whichwillbedescribedinthe
nextsection.
Itisoftenhelpfultosolveasimpleexampleoftheproblembyhandduringthe
algorithmdevelopmentprecess.Ifthedesignerunderstandsthestepsthatheor
shewentthroughinsolvingtheproblembyhand,thenheorshewillbebetter
abletoapplydecompositionandstepwiserefinementtotheproblem.
4.TurnthealgorithmintoMATLABstatements.
Ifthedecompositionandrefinementprocesswascarriedoutproperly,this
stepwillbeverysimple.Alltheprogrammerwillhavetodoistoreplacepseudo
codewiththecorrespondingMATLABstatementsonaone-for-onebasis.
5.TesttheresultingMATLABprogram
ThisstepistherealIdler.Thecomponentsoftheprogrammustfirstbetested
individually,ifpossible,andthentheprogramasawholemustbetested.When
testingaprogram,wemustverifythatitworkscorrectlyforalllegalinputdatasets.
Itisverycommonforaprogramtobewritten,testedwithsomestandarddataset,
andreleasedforuse,onlytofindthatitproducesthewronganswers(orcrashes)
withadifferentinputdataset.Ifthealgorithmimplementedinaprogramincludes
differentbranches,wemusttestallofthepossiblebranchestoconfirmthatthe
programoperatescorrectlyundereverypossiblecircumstance.
Largeprogramstypicallygothroughaseriesoftestsbeforetheyarereleased
forgeneraluse(seeFigure2).Thefirststageoftestingissometimescalledunit
testing.Duringunittesting,theindividualsubtasksoftheprogramaretested
separatelytoconfirmthattheyworkcorrectly.Aftertheunittestingiscompleted,
theprogramgoesthroughaseriesofbuilds,duringwhichtheindividualsubtasks
arecombinedtoproducethefinalprogram.Thefirstbuildoftheprogram
typicallyincludesonlyafewofthesubtasks.Itisusedtochecktheinteractions
amongthosesubtasksandthefunctionsperformedbythecombinationsofthe
subtasks.Insuccessivebuilds,moreandmoresubtasksareadded,untiltheentire
programiscomplete.Testingisperformedoneachbuild,andanyerrors(bugs)
detectedarecorrectedbeforemovingontothenextbuild.
Figure2
MATLAB介绍
MATLAB(矩阵实验室的简称)是一种专业的计算机程序,用于工程科学的矩阵数学运
算。但在以后的几年内,它逐渐发展为一种极其灵活的计算体系,用于解决各种重要的技
术问题。
MATLAB程序执行MATLAB语言,并提供了一个极其广泛的预定义函数库,这样就
使得技术,作变得简单高效。本书将介绍MATLAB语言,并向大家展示如何运用它去解决
经典的技术问题。
MATLAB是一个庞大的程序,拥有难以置信的各种丰富的函数;即使基本版本的
MATLAB语言拥有的函数也比其他的工程编程语言要丰富的多。基本的MATLAB语言已
经拥有了超过1000多个函数,而它的工具包带有更多的函数,由此扩展了它在许多专业
领域的能力。本书无意将MATLAB的所有函数介绍给大家,而是让大家掌握编写调试和优
化程序的基本功,还有一些重要函数的子集。所以从大量可利用的函数中筛选出你所需要
的函数就显得尤为重要。
MATLAB的优点
MATLAB语言相对于传统的科技编程语言有诸多的优点。主要包括:
1.易用性
MATLAB是种解释型语言,就像各种版本的BASIC。和BASIC一样,它简单易用程
序可用作便笺簿求打在命令行处表达式的值,也可执行预先写好的大型程序。在MATLAB
集成开发环境下,程序可以方便的编写,修改和调试。这是因为这种语言极易使用,对于
教育应用和快速建立新程序的原型,它是一个理想的工具。许多的编程工具使得MATLAB
十分简单易用。这些工具包括:一个集成的编译/调试器,在线文件手册,工作台和扩展范
例。
2.平台独立性
MATLAB支持许多的操作系统,提供了大量的平台独立的措施。在本书编写的时侯,
windows98/2000/NT和许多版本的UNIX系统都支持它。在一个平台上编写的程序,在
其它平台上一样可以正常运行,在一个平台上编写的数据文件在其它平台上一样可以编译。
因此用户可以根据需要把MATLAB编写的程序移植到新平台。
3.预定义函数
MATLAB带有一个极大的预定义函数库,它提供了许多已测试和打包过的基本工程问
题的函数。例如,假设你正在编写一个程序,这个程序要求你必须计算与输入有关的统计
量。在许多的语言中,你需要写出你所编数组的下标和执行计算所需要的函数,这些函数
包括其数学意义,中值,标准误差等。像这样成百上千的函数已经在MATLAB中编写好,
所以让编程变得更加简单。
除了植入MATLAB基本语言中的大量函数,还有订多专用工具箱,以帮助用户解次在
具体领域的复杂问题。例如,用户可以购买标准的工具箱以解决在信号处理,控制系统,
通信,图象处理,神经网络和其他许多领域的问题。
4.机制独立的画图
与其他语言不同,MATLAB有许多的画图和图象处理命令。当MATLAB运行时,这
些标绘图和图片将会出现在这台电脑的图像输出设备中。此功能使得MATLAB成为一个形
象化技术数据的卓越工具。
5.用户图形界面
MATLAB允许程序员为他们的程序建立一个交互式的用户图形界面。利用MATLAB
的这种功能,程序员可以设计出相对于无经验的用户可以操作的复杂的数据分析程序。
6.MATLAB编译器
MATLAB的灵活性和平台独立性是通过将MATLAB代码编译成设备独立的P代码,
然后在运行时解释P代码来实现的。这种方法与微软的V3相类似。不幸的是,由于
MATLAB是解释性语言,而不是编译型语言,产生的程序执行速度慢。当我们遇到执彳亍速
度慢的程序时,我们将会指出其这一特性。
MATLAB的缺点
MATLAB有两个基本的缺点。
第一,它是解释型语言,其执行速度要比编译型语言慢得多。这个问题可以通过合理
的MATLAB结构得到缓解,也可以在发行广泛使用前编译出MATLAB程序。
第二,他的费用较高。一个完全版MATLAB编译器的大小是一个C语言或Fortan语
言编译器的5至吐0倍。但MATLAB能够节省大量的时间在科技编程方面,故MATLAB在
商业编程过程中是节省成本的。尽管如此,相对于大多数考虑购买的人还是很昂贵的。幸
运的是,它有一个价格便官的学牛专用版本,对学牛来说它是学习MATLAB语言的一个重
要工具。学生版的MATLAB和完全版的MATLAB是基本一致的。随着选择和循环介绍,我
们的程序也将变得复杂,对于解决问题来说,将会变得简单。为了帮助大家避免在编程过
程中出现大量的错误,我们将向大家介绍正式的编程步骤,即自上而下的编程方法。我们
也会向大家介绍一些普通的算法开发工具即伪代码。
自上而下的编程方法简介
假设你是在工厂工作的工程师,为了解决某些问题,你要编写一个程序。你如何开始
呢?当遇到一个新问题时,我们的心里会自然而然的产生这样的想法:马上坐在计算机前,
开始编程,而不用浪费大量的时间思考我们所要解决的问题是什么?用这种不切实际的想
法来编一些非常小的程序可能会成功。但在现实中,问题可能会非常的大,程序员再用这
种方法编程将会陷入困境。对于一个大的程序来说,在编写代码之前你要通盘的思考你所
要面临的问题和解决的方法。在本节中,我们将向大家介绍正式的编程设计步骤,然后应
用这个步骤来编写本书所有的大的应用程序。对于我们所遇到一些简单的例子来说,这个
步骤好像有些画蛇添足。但是当我们解决的问题变得越来越大的时侯,这个步骤将会变得
异常重要。当我还没有毕业的时侯,一个教授喜欢说:"编程很简单,因为我知道在编程的
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 互联网平台兼职劳动合同范本解析
- 网络直播合作协议书范本下载
- 中学思想政治教育调查报告(2篇)
- 校舍安全的自查报告(3篇)
- 农业局卫生应急工作计划范文(2篇)
- 咳嗽咳痰护理中的多学科合作
- 2026六年级数学上册 扇形统计图思维训练
- 外科护理人力资源管理
- 护理不良事件的持续改进方法
- 2026六年级道德与法治上册 自我保护方法
- JC/T2041-2020 聚氨酯灌浆材料
- DLT1263-2013 12kV~40.5kV 电缆分接箱技术条件
- 《无人机载荷与行业应用》 课件全套 第1-6章 无人机任务载荷系统概述- 未来展望与挑战
- 2022年河北雄安新区容西片区综合执法辅助人员招聘考试真题
- 周围血管与淋巴管疾病第九版课件
- 付款计划及承诺协议书
- 王君《我的叔叔于勒》课堂教学实录
- CTQ品质管控计划表格教学课件
- 沙库巴曲缬沙坦钠说明书(诺欣妥)说明书2017
- 卓越绩效管理模式的解读课件
- 疫苗及其制备技术课件
评论
0/150
提交评论