matlab中ode45函数编写.docx

上传人:b****8 文档编号:10242933 上传时间:2023-02-09 格式:DOCX 页数:20 大小:20.53KB
下载 相关 举报
matlab中ode45函数编写.docx_第1页
第1页 / 共20页
matlab中ode45函数编写.docx_第2页
第2页 / 共20页
matlab中ode45函数编写.docx_第3页
第3页 / 共20页
matlab中ode45函数编写.docx_第4页
第4页 / 共20页
matlab中ode45函数编写.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

matlab中ode45函数编写.docx

《matlab中ode45函数编写.docx》由会员分享,可在线阅读,更多相关《matlab中ode45函数编写.docx(20页珍藏版)》请在冰豆网上搜索。

matlab中ode45函数编写.docx

matlab中ode45函数编写

functionvarargout=ode45(ode,tspan,y0,options,varargin)

%ODE45Solvenon-stiffdifferentialequations,mediumordermethod.

%[TOUT,YOUT]=ODE45(ODEFUN,TSPAN,Y0)withTSPAN=[T0TFINAL]integrates%thesystemofdifferentialequationsy'=f(t,y)fromtimeT0toTFINAL%withinitialconditionsY0.ODEFUNisafunctionhandle.ForascalarT

%andavectorY,ODEFUN(T,Y)mustreturnacolumnvectorcorresponding%tof(t,y).EachrowinthesolutionarrayYOUTcorrespondstoatime%returnedinthecolumnvectorTOUT.Toobtainsolutionsatspecific%timesT0,T1,...,TFINAL(allincreasingoralldecreasing),useTSPAN=%[T0T1...TFINAL].

%

%[TOUT,YOUT]=ODE45(ODEFUN,TSPAN,Y0,options)solvesasabovewithdefault

%integrationpropertiesreplacedbyvaluesinoptions,anargumentcreated%withtheODESETfunction.SeeODESETfordetails.Commonlyusedoptions%arescalarrelativeerrortolerance'RelTol'(1e-3bydefault)andvector%ofabsoluteerrortolerances'AbsTol'(allcomponents1e-6bydefault).%Ifcertaincomponentsofthesolutionmustbenon-negative,use

%ODESETtosetthe'NonNegative'propertytotheindicesofthese

%components.

%

%ODE45cansolveproblemsM(t,y)*y'=f(t,y)withmassmatrixMthatis%nonsingular.UseODESETtosetthe'Mass'propertytoafunctionhandle%MASSifMASS(T,Y)returnsthevalueofthemassmatrix.Ifthemassmatrix%isconstant,thematrixcanbeusedasthevalueofthe'Mass'option.If

%themassmatrixdoesnotdependonthestatevariableYandthefunction%MASSistobecalledwithoneinputargumentT,set'MStateDependence'to

%'none'.ODE15SandODE23Tcansolveproblemswithsingularmassmatrices.%

%[TOUT,YOUT,TE,YE,IE]=ODE45(ODEFUN,TSPAN,Y0,OPTIONS)withthe'Events'%propertyinOPTIONSsettoafunctionhandleEVENTS,solvesasabove%whilealsofindingwherefunctionsof(T,Y),calledeventfunctions,%arezero.Foreachfunctionyouspecifywhethertheintegrationis

%toterminateatazeroandwhetherthedirectionofthezerocrossing%matters.ThesearethethreecolumnvectorsreturnedbyEVENTS:

%[VALUE,ISTERminAL,DIRECTION]=EVENTS(T,Y).FortheI-theventfunction:

%VALUE(I)isthevalueofthefunction,ISTERminAL(I)=1iftheintegration%istoterminateatazeroofthiseventfunctionand0otherwise.

%DIRECTION(I)=0ifallzerosaretobecomputed(thedefault),+1ifonly%zeroswheretheeventfunctionisincreasing,and-1ifonlyzeroswhere%theeventfunctionisdecreasing.outputTEisacolumnvectoroftimes%atwhicheventsoccur.RowsofYEarethecorrespondingsolutions,and%indicesinvectorIEspecifywhicheventoccurred.

%

%SOL=ODE45(ODEFUN,[T0TFINAL],Y0...)returnsastructurethatcanbe%usedwithDEVALtoevaluatethesolutionoritsfirstderivativeat

%anypointbetweenT0andTFINAL.ThestepschosenbyODE45arereturned%inarowvectorSOL.x.ForeachI,thecolumnSOL.y(:

I)contains

%thesolutionatSOL.x(I).Ifeventsweredetected,SOL.xeisarowvector%ofpointsatwhicheventsoccurred.ColumnsofSOL.yearethecorresponding

%solutions,andindicesinvectorSOL.iespecifywhicheventoccurred.%

%Example

%[t,y]=ode45(@vdp1,[020],[20]);

%plot(t,y(:

1));

%solvesthesystemy'=vdp1(t,y),usingthedefaultrelativeerror%tolerance1e-3andthedefaultabsolutetoleranceof1e-6foreach%component,andplotsthefirstcomponentofthesolution.

%

%ClasssupportforinputsTSPAN,Y0,andtheresultofODEFUN(T,Y):

%float:

double,single

%

%Seealso

%otherODEsolvers:

ODE23,ODE113,ODE15S,ODE23S,ODE23T,ODE23TB%implicitODEs:

ODE15I

%optionshandling:

ODESET,ODEGET

%outputfunctions:

ODEPLOT,ODEPHAS2,ODEPHAS3,ODEPRINT

%evaluatingsolution:

DEVAL

%ODEexamples:

RIGIDODE,BALLODE,ORBITODE

%functionhandles:

function_HANDLE

%

%NOTE:

%TheinterpretationofthefirstinputargumentoftheODEsolversand%somepropertiesavailablethroughODESEThavechangedinMATLAB6.0.%Althoughwestillsupportthev5syntax,anynewfunctionalityis%availableonlywiththenewsyntax.Toseethev5help,typein

%thecommandline

%moreon,typeode45,moreoff

%NOTE:

%Thisportiondescribesthev5syntaxofODE45.

%

%[T,Y]=ODE45('F',TSPAN,Y0)withTSPAN=[T0TFINAL]integratesthe

%systemofdifferentialequationsy'=F(t,y)fromtimeT0toTFINALwith%initialconditionsY0.'F'isastringcontainingthenameofanODE%file.FunctionF(T,Y)mustreturnacolumnvector.Eachrowin

%solutionarrayYcorrespondstoatimereturnedincolumnvectorT.To%obtainsolutionsatspecifictimesT0,T1,...,TFINAL(allincreasing%oralldecreasing),useTSPAN=[T0T1...TFINAL].

%

%[T,Y]=ODE45('F',TSPAN,Y0,options)solvesasabovewithdefault

%integrationparametersreplacedbyvaluesinoptions,anargument

%createdwiththeODESETfunction.SeeODESETfordetails.Commonly

%usedoptionsarescalarrelativeerrortolerance'RelTol'(1e-3by

%default)andvectorofabsoluteerrortolerances'AbsTol'(all

%components1e-6bydefault).

%

%[T,Y]=ODE45('F',TSPAN,Y0,OPTIONS,P1,P2,...)passestheadditional

%parametersP1,P2,...totheODEfileasF(T,Y,FLAG,P1,P2,...)(see

%ODEFILE).UseOPTIONS=[]asaplaceholderifnooptionsareset.%

%ItispossibletospecifyTSPAN,Y0andOPTIONSintheODEfile(see%ODEFILE).IfTSPANorY0isempty,thenODE45callstheODEfile

%[TSPAN,Y0,OPTIONS]=F([],[],'init')toobtainanyvaluesnotsupplied%intheODE45argumentlist.Emptyargumentsattheendofthecalllist%maybeomitted,e.g.ODE45('F').

%

%ODE45cansolveproblemsM(t,y)*y'=F(t,y)withamassmatrixMthatis

%nonsingular.UseODESETtosetMassto'M','M(t)',or'M(t,y)'ifthe

%ODEfileiscodedsothatF(T,Y,'mass')returnsaconstant,

%time-dependent,ortime-andstate-dependentmassmatrix,respectively.%ThedefaultvalueofMassis'none'.ODE15SandODE23Tcansolveproblems%withsingularmassmatrices.

%

%[T,Y,TE,YE,IE]=ODE45('F',TSPAN,Y0,options)withtheEventspropertyin

%optionssetto'on',solvesasabovewhilealsolocatingzerocrossings%ofaneventfunctiondefinedintheODEfile.TheODEfilemustbe

%codedsothatF(T,Y,'events')returnsappropriateinformation.See

%ODEFILEfordetails.outputTEisacolumnvectoroftimesatwhich%eventsoccur,rowsofYEarethecorrespondingsolutions,andindicesin

%vectorIEspecifywhicheventoccurred.

%

%SeealsoODEFILE

%ODE45isanimplementationoftheexplicitRunge-Kutta(4,5)pairof%DormandandPrincecalledvariouslyRK5(4)7FM,DOPRI5,DP(4,5)andDP54.%Itusesa"free"interpolantoforder4communicatedprivatelyby

%DormandandPrince.Localextrapolationisdone.

%DetailsaretobefoundinTheMATLABODESuite,L.F.Shampineand

%M.W.Reichelt,SIAMJournalonScientificComputing,18-1,1997.

%MarkW.ReicheltandLawrenceF.Shampine,6-14-94

%Copyright1984-2009TheMathWorks,Inc.

%$Revision:

5.74.4.10$$Date:

2009/04/2103:

24:

15$

solver_name='ode45';

%Checkinputs

ifnargin<4

options=[];

ifnargin<3

y0=[];

ifnargin<2

tspan=[];

ifnargin<1

error('MATLAB:

ode45:

NotEnoughInputs',...

'Notenoughinputarguments.SeeODE45.');

end

end

end

end

%Stats

nsteps=0;

nfailed=0;

nfevals=0;

%output

FcnHandlesUsed=isa(ode,'function_handle');

output_sol=(FcnHandlesUsed&&(nargout==1));%sol=odeXX(...)output_ty=(~output_sol&&(nargout>0));%[t,y,...]=odeXX(...)

%Theremightbenooutputrequested...

sol=[];f3d=[];

ifoutput_sol

sol.solver=solver_name;

sol.extdata.odefun=ode;

sol.extdata.options=options;

sol.extdata.varargin=varargin;

end

%Handlesolverarguments

[neq,tspan,ntspan,next,t0,tfinal,tdir,y0,f0,odeArgs,odeFcn,...options,threshold,rtol,normcontrol,normy,hmax,htry,htspan,dataType]=...

odearguments(FcnHandlesUsed,solver_name,ode,tspan,y0,options,varargin);

nfevals=nfevals+1;

%Handletheoutput

ifnargout>0

outputFcn=odeget(options,'OutputFcn',[],'fast');

else

outputFcn=odeget(options,'OutputFcn',@odeplot,'fast');

end

outputArgs={};

ifisempty(outputFcn)

haveOutputFcn=false;

else

haveOutputFcn=true;

outputs=odeget(options,'OutputSel',1:

neq,'fast');

ifisa(outputFcn,'function_handle')

%WithMATLAB6syntaxpassadditionalinputargumentstooutputFcn.outputArgs=varargin;

end

end

refine=max(1,odeget(options,'refine',4,'fast'));

ifntspan>2

outputAt='RequestedPoints';%outputonlyattspanpoints

elseifrefine<=1

outputAt='SolverSteps';%computedpoints,norefinementelse

outputAt='RefinedSteps';%computedpoints,withrefinementS=(1:

refine-1)/refine;

end

printstats=strcmp(odeget(options,'Stats','off','fast'),'on');

%Handletheeventfunction

[haveEventFcn,eventFcn,eventArgs,valt,teout,yeout,ieout]=...

odeevents(FcnHandlesUsed,odeFcn,t0,y0,options,varargin);

%Handlethemassmatrix

[Mtype,M,Mfun]=odemass(FcnHandlesUsed,odeFcn,t0,y0,options,varargin);ifMtype>0%non-trivialmassmatrix

Msingular=odeget(options,'MassSingular','no','fast');

ifstrcmp(Msingular,'maybe')

warning('MATLAB:

ode45:

MassSingularAssumedNo',['ODE45assumes'...

'MassSingularis''no''.SeeODE15SorODE23T.']);

elseifstrcmp(Msingular,'yes')

error('MATLAB:

ode45:

MassSingularYes',...

['MassSingularcannotbe''yes''forthissolver.SeeODE15S'...

'orODE23T.']);

end

%IncorporatethemassmatrixintoodeFcnandodeArgs.

[odeFcn,odeArgs]=

odemassexplicit(FcnHandlesUsed,Mtype,odeFcn,odeArgs,Mfun,M);

f0=feval(odeFcn,t0,y0,odeArgs{:

});

nfevals

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

当前位置:首页 > 高等教育 > 文学

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

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