加入VIP
  • 专属下载特权
  • 现金文档折扣购买
  • VIP免费专区
  • 千万文档免费下载

上传资料

关闭

关闭

关闭

封号提示

内容

首页 matlab解微分方程

matlab解微分方程.pdf

matlab解微分方程

冷血
2011-12-09 0人阅读 举报 0 0 暂无简介

简介:本文档为《matlab解微分方程pdf》,可适用于IT/计算机领域

AMATMathematicalMethodsforDifferentialEquationsNumericalMethodsandTechniquesusingMATLABc©DrGNMercerPrefaceThesenotescomprisethefirsthalfofthenumericalpartofthecourseAMATMathematicalMethodsforDifferentialEquationsTheyarenotacompletesetofnotesExtramaterialandexamplesmayalsobepresentedinthelecturesandtutorialsUsingtheelectronicversionofthesenotesThesenotesarehyperlinkedAllgreentextisalinktosomewhereelsewithinthisdocumentForexamplethecontentspagelinkstotheappropriatepageinthetext,thenumbersintheindexlinktothepagereference,thewordIndexintheheaderofmostpageslinkstotheindexandthepagenumbersintheheaderoneachpagelinkbacktothecontentspageTherearealsosomeinternallinkedwordsthattakeyoutotherelevanttextLinkstoexternalwebpagesareredincolourProvidedyourPDFreader(egAdobeAcrobatReader)issetupcorrectlytheselinksshouldopentheappropriatepageinyourwebbrowser(egMicrosoftInternetExplorer,Netscape,Mozilla)MATLABcodeTheMATLABcodesusedinthesenotesareavailableatthecoursewebsitehttp:wwwmaadfaeduauTeachingThirdYearmmdehtmlManyofthecodesarealsolinkeddirectlyfromwithinthetexttotheappropriatewebpageMATLABcodeinthetextisusuallyshowninpurpleandtheresultingoutputinblueIfyouhaveanydifficultieswithanypartofthecoursedonothesitateincontactingmetosortthemoutGeoffMercerRoomGScienceSouthSchoolofPhysical,EnvironmentalandMathematicalSciencesPhone:Email:gmerceradfaeduauiiIndexContentsIntroductiontoMATLABAccesstoMATLABUsingMATLABAfewsimpleexamplesRunningMATLABscriptsSendingoutputtoafileMATLABhelpfacilityandlookforcommandFunctionsinMATLABFunctionsinMATLAB–vectorinputPassingfunctionnamesAnIntroductiontosolvingDEsnumericallySomeElementaryMATLABCommandsInitialValueProblemsIntroductionNumericalapproachEuler’smethodGraphicallyErrorsandorderofthemethodAdjustingthestepsizeImprovementstoEuler’smethodmidpointmethodSecondorderRungeKuttamethodFourthorderRungeKuttamethododeProjectileexamplePhasePlaneAnalysisIntroductionQuiverplotsPredatorPreymodelCompetitionmodelSIRdiseasemodelBoundaryValueProblemsIntroductionShootingmethodShootingmethodusingguessesMATLABfindingzerosShootingmethodusingfzeroRelaxationmethodsFinitedifferencesUsingfinitedifferenestosolveaBVPCommentsonsolvingmatrixequationsFiniteelementsSymbolicformofaBVPFiniteelementtheoryFiniteelementexampleFiniteelementsummaryDifferentialeigenvalueequationsivCONTENTSIndexIntroductiontoMATLABMATLAB(MATrixLABoratory)isaverypowerfulpackagedesignedfornumericalanalysis,matrixcalculationsandvisualisation(aswellasmanyotherapplications)OneofMATLABsstrengthsisthatitcanbeusedasasimple‘calculator’andalsoiseasytoprogrammorecomplicatedtasksManymathematicalalgorithmsarealreadybuiltintoMATLABandmanymoreareeasilyaddedAccesstoMATLABUniversityCollegehasalimiteduserlicenceforthefullversionofMATLAB,thisisavailableviaoctarineAlternativelyyoucanpurchaseMATLAB(fromtheCoopBookshop),thisisastudentreleaseThemostrecentversionofMATLABis,ReleaseOlderversionofMATLABhadastudenteditionwhichismorethanadequateforthiscourse(andisoftenallthatiseverneededformanyapplications)UsingMATLABMATLABisaninteractiveprogramsoyoucanentercommandsatthepromptorreadinfileswithlinesofMATLABcommandsForrelativelysimpletasksitisacceptabletoentercommandsatthepromptbutformorecomplicatedtasksitisbettertostorethecommandsinafileandthenreadinthefileThismakesiteasytochangethecommandsandtostorethemInfactallinbuiltMATLABcommandsarewrittenasfilesofcommandsForthisreasontherearevastnumbersofMATLABfiles(calledMfiles)writtenforvariouspurposesavailableontheinternetForthiscourseyoushoulddoyourworkusingMfilesasthesewillbehandedupaspartofyourassignmentsalongwiththerelevantoutputAllMfilesgiveninthenoteswillbeavailablefromthecoursewebpageathttp:wwwmaadfaeduauTeachingThirdYearmmdehtmlManyoftheassignmentquestionswillbeamodificationofthesefilesINTRODUCTIONTOMATLABIndexAfewsimpleexamplesMatrixManipulationEntera×matrixfinditsinverse,andeigenvaluesandeigenvectorsCheckthatitsinversetimesitselfistheidentityMATLABcodeanythingafterasignisacommentA=,,enterthexmatrixandcallitAB=inv(A)calculatetheinverseofAandcallitBeig(A)calculatetheeigenvaluesofAV,D=eig(A)calctheeigenvectors(columnsofmatrixV)diagonalmatrixD(eigenvaluesondiagonal)check=B*AIfthesecommandsarestoredinanMfilecalledexamplemthentorunittypeexample(withoutthem)fromwithinMATLABTheMATLABoutputfromthesecommandsisthefollowingA=B=ans=V=D=check=IndexAFEWSIMPLEEXAMPLESPlottingMATLABhasverygoodgraphicscapabilityTheeasiestmethodforsimpleplotsistodefineavector(forexamplex)andthenperformafunctiononeachelementofthatvector(egy(x))andthenploty(x)versusxDefiningavectorisstraightforwardTheMATLABcommandx=a:b:cdefinesxtobeavectorofvaluesstartingataincreasinginstepsofbtocForexamplex=::resultsinxbeingthevector−,−,,,ExamplePloty=xsin(xpi)MATLABcodex=::defineavectorxwithvaluesfromtoinstepsofthesemicolonsuppressestheoutputy=x*xsin(x*pi)calculatey=x*xsin(x*pi),notethe*thisisbecausexisavectorsoweneedtomultiple’componentbycomponent’plot(x,y)printdepsexamplesendtheplotoutputtoapostscriptfilecalledexampleeps−−INTRODUCTIONTOMATLABIndexPlottingdataItisveryeasytoreaddatainfromafile,manipulateitandplotitThecommandloadisusedtoloaddatafromanexternalfileForexamplereaddatafromafilecalleddatafile,calculatethelineofbestfittothedataandaddsometitles,legendandtexttotheplotMATLABcodereaddatafromthefiledatafileThisputsitintoavariablematrixcalleddatafilebydefaultloaddatafilex=datafile(:,)putfirstcolumnintovariablexy=datafile(:,)putsecondcolumnintovariableyFitalinearregressionlinetothedatat=ones(size(x))xcoeffofregn=tystraightline=ones(size(x))x*coeffofregnPlotthedataandlineandaddtitlesandlabelsplot(x,y,’ro’,x,straightline,’b’)title(’dataandlineofbestfit’)xlabel(’x’)ylabel(’y’)addsometexttotheplotandalegend(thenumberiswheretoputthelegend,isthetopleftcorner)text(,,’Thisistext’)legend(’data’,’lineofbestfit’,)dataandlineofbestfitxyThisistextdatalineofbestfitIndexAFEWSIMPLEEXAMPLESDPlottingMATLABhasverygoodDgraphicscapabilityTheeasiestmethodisdodefinethedomainusingthemeshgridcommand,thendefinethefunctionandthenplotitusingmeshorsurfcommandsExamplePlotz=x−yMATLABcodex,y=meshgrid(::,::)definethemesh(x,y)thedomainofplottingxfromtoinstepsofyfromtoinstepsofz=*x*x*yˆcalculatez=xˆyˆnotethe*andˆthisisbecausexandyarevectorssoweneedtomultiple’componentbycomponent’mesh(x,y,z)drawthesurfaceasameshorusesurf(x,y,z)todrawitasasurfaceprintdepscexampleasendtheplotoutputtoacolourpostscriptfile−−−−INTRODUCTIONTOMATLABIndexSolvingLinearEquationsTheoriginalreasonfordevelopingMATLABwastoperformmatrixcalculationsDuetothisithasmanyconvenientwaysofdealingwithmatricesForexampletosolvealinearequationsystemAx=bthereisacommandknownas’backslashdivide’MATLABcodeA=,,,,,,definethematrixAb=,,’notethe’totaketransposeorcoulddob=x=Absolveforxx=ExtractingSegmentsofMatricesOftenyoumaywanttoextractsectionsofamatrixForexampleallofthefirstroworthesecondcolumnMATLABcodeA=,,,,,,c=A(,:)extractallofthefirstrowofAd=A(:,)extractallofthesecondcolumnofAc=d=diaryoffIndexRUNNINGMATLABSCRIPTSRunningMATLABscriptsOneoftheeasiestandmosttransportablewaystouseMATLABistoenteryourMATLABcommandsintoafileandthenrunthatfileThesetypesoffilesarecalledMfilesandhavetheextensionmaftertheirnameTorunanMfilefromMATLABmakesureyouareinthedirectorywherethefileislocatedandthenjusttypethenameofthefilewithouttheextension(iewithoutthem)MATLABwillthenprocessallthecommandsthatareinthatfileExampleCreateafilecalledexamplemInthatfileputthefollowinglinesformatcompactthisoutputsinacompactformA=,,thisentersthexmatrixAC=AˆthisfindsthetenthpowerofAeig(C)thisfindstheeigenvaluesofCFromwithinMATLABtypeexamplethatwillrunthecommandsinexamplemThefollowingwillappearonthescreenA=C=ans=SendingoutputtoafileOftenyouwanttokeeptheoutputofaMATLABcommandThediarycommandsdoesthatforyouBytypingdiaryexampleoutallsubsequentoutputwillsenttothefileexampleoutaswellastothescreenToturnthediarycommandoffusediaryoffINTRODUCTIONTOMATLABIndexMATLABhelpfacilityandlookforcommandMATLABhasacomprehensivehelpfacilityItisaccessedbytypinghelpfollowedbythenameofthecommandyourequirehelponForexamplehelpdiarywillreturnthefollowingDIARYSavetextofMATLABsessionDIARYfilenamecausesacopyofallsubsequentcommandwindowinputandmostoftheresultingcommandwindowoutputtobeappendedtothenamedfileIfnofileisspecified,thefile’diary’isusedDIARYOFFsuspendsitDIARYONturnsitbackonDIARY,byitself,togglesthediarystateUsethefunctionalformofDIARY,suchasDIARY(’file’),whenthefilenameisstoredinastringNotethatthehelpfacilityusescapitalsforthecommandname,capitalsshouldNOTbeusedwhenactuallyusingthecommandsIfyoudon’tknowthenameofthefunctionthenuselookforkeywordThiswillfindalloccurrencesoffunctionsorcommandswiththespecifiedkeywordForexamplelookfordifferentiatereturnsallfunctionsthatmentiondifferentiatelookfordifferentiatePOLYDERDifferentiatepolynomialFNDERDifferentiateafunctionDIFFDifferentiateIndexFUNCTIONSINMATLABFunctionsinMATLABSometimesyouwillwanttowriteyourownfunctioninMATLABForexampleanMfilethattakessomekindofinputandreturnssomecalculationsonthatinputForexamplewriteafunctionthatcalculatesf(x)=xxCreateafilecalledcalcfmthatlookslikethefollowingMATLABcodefunctionf=calcf(x)thisfileisnamedcalcfmthisfunctioncalculatesf(x)andreturnstheanswersinthevariablesff=xˆ*xˆreturnFromwithinMATLABtypecalcf()tocalculatef()thisresultsinthefollowingoutputcalcf()ans=INTRODUCTIONTOMATLABIndexFunctionsinMATLAB–vectorinputSinceMATLABdealswithvectorsofnumbersitismoreusefultowritefunctionssothattheycanhandlevectorsratherthananindividualnumberTheonlychangeneededtoachievethisistoensurethatthecalculationswithinthefunctionaredoneonanelementbyelementbasisbyusingthe*notationinsteadofthe*notation(andequivalentlyˆ,etc)Forexamplewriteafunctionthatcalculatesf(x)=xxforaninputvectorxThatis,itshouldcalculatef(x)foreachvalueofavectorxandreturntheanswerinavectorMATLABcodefunctionf=calcfvec(x)thisfileisnamedcalcfvecmthisfunctioncalculatesf(x)foravectorxandreturnstheanswersinthevariableff=xˆ*xˆreturnFromwithinMATLABtypey=::calcfvec(y)tocalculatef(y)thisresultsinthefollowingoutputy=::y=calcfvec(y)ans=IndexPASSINGFUNCTIONNAMESPassingfunctionnamesOfteninMATLAByouneedtopassthenameofafunctiontoanotherfunctionForexampleifyouwanttonumericallycalculatetheintegralofafunctionMATLABhasaninbuiltfunctionthatdoesthisbutsomehowyouneedtotellitwhatfunctiontointegrateHenceyouneedtopassthefunctionsnameMATLABhastwoinbuiltfunctionsforcalculatingintegralsA,count=quad(’funcname’,a,b,tol,trace)A,count=quad(’funcname’,a,b,tol,trace)quadusesSimpson’srule,quadusesadegreepolynomialtolisthetolerancefortheconvergenceusingacomparisonbetweensuccessiveapproximationstraceisanadditionalfeatureforplottingatraceoftheintegrandusedTheanswerisreturnedinAandcountisacountofhowmanyfunctionevaluationshavebeenperformedfuncnameisthenameofthefunctiontobepassedForexampleifintegratingthefunctiondefinedintheMfilequadexamplefuncmthenusequadexamplefuncwhereeveryouseefuncnameImportantNote:Thefunctionreferencedby’funcname’mustbewrittentotakeavectorinputandgiveavectoroutputExampleCalculatef(x)=√pi∫xe−tdtForx=Thisfunctionisknownastheerrorfunction(erf(x)inMATLAB)MATLABcodequadexamplemformatlonguselotsofdecimalplacesthe’exact’answerusingMATLABserffunctionerrorfunc=erf()numericallyintegrateusingSimpsonsrule(quad)integral=quad(’quadexamplefunc’,,,e)nowdoitagainusinganthorderpolynomial(quadl)integral=quadl(’quadexamplefunc’,,)countthenumberoffunctioncallsusingthetraceswitchtrace=integral,count=quad(’quadexamplefunc’,,,e,trace)MATLABcodefunctionf=quadexamplefunc(x)quadexamplefuncmthefunctiontobeintegratedf=(sqrt(pi))*exp(xˆ)returnINTRODUCTIONTOMATLABIndex>>quadexampleerrorfunc=integral=integral=eeeeeeeintegral=count=diaryoffIndexANINTRODUCTIONTOSOLVINGDESNUMERICALLYAnIntroductiontosolvingDEsnumericallyMATLABhasmanywaysofnumericallysolvingDifferentialEquationsHerewewillpresentonemethod,thedetailsofthemethodandthemathematicsbehinditwillbeexplainedinlaterlecturesThiswillgiveyouenoughnowtobeabletonumericallysolvemanyoftheDEsyouwillencounterearlyinthiscourseConsiderafirstorderODEdydt=f(t,y)subjecttosomeinitialcondition(attimet=)y()=yTheMATLABfunctionodewillsolvethesekindofproblemsHereisanexcerptfromthehelponodeODESolvenonstiffdifferentialequations,mediumordermethodT,Y=ODE(ODEFUN,TSPAN,Y)withTSPAN=TTFINALintegratesthesystemofdifferentialequationsy’=f(t,y)fromtimeTtoTFINALwithinitialconditionsYFunctionODEFUN(T,Y)mustreturnacolumnvectorcorrespondingtof(t,y)EachrowinthesolutionarrayYcorrespondstoatimereturnedinthecolumnvectorTToobtainsolutionsatspecifictimesT,T,,TFINAL(allincreasingoralldecreasing),useTSPAN=TTTFINALExamplet,y=ode(vdp,,)plot(t,y(:,))solvesthesystemy’=vdp(t,y),usingthedefaultrelativeerrortoleranceeandthedefaultabsolutetoleranceofeforeachcomponent,andplotsthefirstcomponentofthesolutionTherearethingsyouneedtodefinetouseMATLABtosolvethistypeofDETherangeoftyouwishtocalculatethesolutionover(TSPAN=T,TFINAL)Theinitialconditionony(Y)AnMfilewiththefunctionf(t,y)(ODEFUN)Notestisreturnedasavectorofallthetimeswherethesolutionwasdet

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续阅读或者下载,敬请购买!

文档小程序码

使用微信“扫一扫”扫码寻找文档

1

打开微信

2

扫描小程序码

3

发布寻找信息

4

等待寻找结果

我知道了
评分:

/18

matlab解微分方程

仅供在线阅读

VIP

在线
客服

免费
邮箱

爱问共享资料服务号

扫描关注领取更多福利