欢迎来到天天文库
浏览记录
ID:39971917
大小:420.50 KB
页数:40页
时间:2019-07-16
《matlab预定义函数》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Matlab与科学计算计算机学院刘咏梅Email:liuyongmei@hrbeu.edu.cn1第四章MATLAB编程(预定义函数)IntroductiontoMATLABMATLABBasicsofNumericalComputingMATLABProgramming(branchingandLoops)MATLABProgramming(PredefinedFunctions)MATLABGraphicandImageProcessingScientificComputingusingMATLA
2、B2本章学习的目标:IntroductiontoMatlabFunctionsbuiltinfunctionsanduserdefinedfunctionsOptionalArgumentsSharingDataUsingGlobalMemoryPreservingDataBetweenCallstoaFunctionFunctionFunctionsSubfunctions3FunctionsFunctionsaremorecomplexthanscriptsFunctionshavetheirow
3、nlocalvariablesFunctionsreturnoutputasspecified,andcanacceptinputasspecified4Whyweusefunctions?IndependentTestingofSub-Tasks:Eachtaskisanindependentunitthatcanbetestedseparately(unittesting).ReusableCode:Withinthesameprogram(thecodeisnotrepeated).Itcanb
4、eusedinanotherprogram.Isolationfromunintendedsideeffects:Isolationoffunctionvariablesfromthemainprogramvariables.Theinputandtheoutputvariablesarespecifiedclearly.5SyntaxofMatlabFunctionsUserdefinedfunctionsbehavejustlikebuiltinfunctions.Allfunctionshave
5、asimilarsyntax,whethertheyarebuilt-infunctionsoruser-definedfunctions.NameInputOutputA=cos(x)6Howtocallfunctions?Thefunctionscanbecalleddirectlyinthecommandwindow,orbyanyotherprogramorfunction.Veryimportant:iftheinputvariablesarechangedinthefunctionsthe
6、ywillnotchangeinthemainprogram(PASS-BY-VALUESCHEME)7BuiltinFunctionsBuiltinfunctionsallowustoreusecomputercodeforcalculationsthatareperformedfrequently.Forexample,supposetherewerenobuiltinsinefunctioninMATLABandyouwilldoalotoftrigonometriccalculationsin
7、volvingthesineofanangle.We’vealreadybecomefamiliarwithanumberofMATLABbuiltinfunctionslikesin(x)orplot(x,y)forexample.8SomeexamplesofBuiltinFunctionsExponential(指数)exp(x)exponentialofx(ex)sqrt(x)squarerootofxLogarithmic(对数)log(x)naturallogarithm(lnx)log1
8、0(x)log2(x)9SomeexamplesofBuiltinFunctionsNumeric(数值)fix(x)roundtonearestintegertowards0round(x)roundtonearestintegersign(x)+1,0or-1rem(x,y)Findsremainderofx/yComplex(复数)abs(x)absolutevalueangle(x)phaseangleincomplexplaneconj(x)c
此文档下载收益归作者所有