Danh mục

Lecture note Data visualization - Chapter 26

Số trang: 12      Loại file: pptx      Dung lượng: 139.86 KB      Lượt xem: 9      Lượt tải: 0    
Hoai.2512

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

This chapter presents the following content: Basic definitions, example of a database, typical DBMS functionality, main characteristics of the database approach, database users, workers behind the scene,...
Nội dung trích xuất từ tài liệu:
Lecture note Data visualization - Chapter 26Lecture26RecapSavingPlotsSummaryofChapter5IntroductionofChapter6FunctionMfilesUserdefinedfunctionsarestoredasMfilesandcanbe accessedbyMATLABiftheyareinthecurrentfolderor onMATLAB’ssearchpathSyntaxofFunctionMfileBothbuiltinMATLAB®functionsanduserdefined MATLABfunctionshavethesamestructureEachconsistsofaname,userprovidedinput,and calculatedoutput.Forexample:thefunction cos(x) isnamedcos takestheuserinputinsidetheparentheses(inthiscase,x) calculatesaresultContinued….UserdefinedfunctionsarecreatedinMfiles.Eachmust startwithafunctiondefinitionlinethatcontains: Thewordfunction Avariablethatdefinesthefunctionoutput Afunctionname AvariableusedfortheinputargumentForexample: functionoutput=my_function(x)isthefirstlineoftheuserdefinedfunctioncalledmy_functionContinued….Here’sanexampleofaverysimpleMATLABfunction thatcalculatesthevalueofaparticularpolynomial: functionoutput=poly(x) %Thisfunctioncalculatesthevalueofathirdorder %polynomial output=3*x.^3+5*x.^22*x+1;Thefunctionnameispoly,theinputargumentisx,and theoutputvariableisnamedoutputBeforethisfunctioncanbeused,itmustbesavedintothe currentfolderContinued….OncetheMfilehasbeensaved,thefunctionisavailable forusefromthecommandwindow,fromascriptMfile, orfromanotherfunctionYoucannotexecuteafunctionMfiledirectlyfromthe Mfileitself.Thismakessense,sincetheinputparameters havenotbeendefineduntilyoucallthefunctionfromthe commandwindoworascriptMfileConsiderthepolyfunctionjustcreated.If,inthe commandwindow,wetype poly(4)thenMATLABrespondswith ans=265CommentsAswithanycomputerprogram,codeshouldbe commentedliberallysothatitiseasytofollowHowever,inaMATLABfunction,thecommentsonthe lineimmediatelyfollowingtheveryfirstlineservea specialroleTheselinesarereturnedwhenthehelpfunctionisqueried fromthecommandwindow Consider,forexample,thefollowingfunction: functionresults=f(x) %Thisfunctionconvertssecondstominutes FunctionswithMultipleInputsandOutputsContinued….Similarly,auserdefinedfunctioncouldbewrittento multiplytwovectorstogether: functionoutput=g(x,y) %Thisfunctionmultipliesxandytogether %xandymustbethesamesizematrices a=x.*y; output=a;Whenxandyaredefinedinthecommandwindowand thefunctiongiscalled,avectorofoutputvaluesis returned: x=1:5;Continued….Suchfunctionscanalsobecreatedthatreturnmorethan oneoutputvariableManyofthepredefinedMATLABfunctionsreturnmore thanoneresultForexample:maxreturnsboththemaximumvalueina matrixandtheelementnumberatwhichthemaximum occurs.Toachievethesameresultinauserdefined function,maketheoutputamatrixofanswersinsteadofa singlevariable,asin function[dist,vel,accel]=motion(t) %Thisfunctioncalculatesthedistance,velocity,andContinued….Remember,allvariablesinMATLABarematrices,soit’s importantintheprecedingexampletousethe.*operator, whichspecifieselementbyelementmultiplicationForexample:usingavectoroftimevaluesfrom0to30 inthemotionfunction time=0:10:30; [distance,velocity,acceleration]=motion(time) returnsthreevectorsofanswers: distance=083.33666.672250.00 velocity=025.00100.00225.00 acceleration=05.0010.0015.00

Tài liệu được xem nhiều: