Sie sind auf Seite 1von 6

4/2/13

Using Matlab for First Order ODEs

UsingMatlabforFirstOrderODEs
Contents
@functions Directionfields Numericalsolutionofinitialvalueproblems Plottingthesolution Combiningdirectionfieldandsolutioncurves Findingnumericalvaluesatgiventvalues SymbolicsolutionofODEs Findingthegeneralsolution Solvinginitialvalueproblems Plottingthesolution Findingnumericalvaluesatgiventvalues Symbolicsolutions:Dealingwithsolutionsinimplicitform

@functions
YoucandefineafunctioninMatlabusingthe@syntax:
g=@ ( x )s i n ( x ) * x

definesthefunctiong(x)=sin(x)x.Youcanthen evaluatethefunctionforagivenxvalue:
g ( 0 . 3 )

plotthegraphofthefunctionoveraninterval:
e z p l o t ( g , [ 0 , 2 0 ] )

findazeroofthefunctionnearaninitialguess:
f z e r o ( g , 3 )

Youcanalsodefine@functionsofseveralvariables:
G=@ ( x , y )x ^ 4+y ^ 4-4 * ( x ^ 2 + y ^ 2 )+4

definesthefunctionG(x,y)=x4+y44(x2+y2)+4oftwovariables.Youcanthen evaluatethefunctionforgivenvaluesofx,y:
G ( 1 , 2 )

plotthegraphofthefunctionasasurfaceoverarectangleinthex,yplane: e z s u r f ( G , [ 2 , 2 , 2 , 2 ] ) Clickon inthefiguretoolbar,thenyoucanrotatethegraphbydraggingwiththe mouse. plotthecurveswhereG(x,y)=0inarectangleinthex,yplane:


e z p l o t ( G , [ 2 , 2 , 2 , 2 ] )

makeacontourplotofthefunctionforarectangleinthex,yplane:
terpconnect.umd.edu/~petersd/246/matlabode.html 1/6

4/2/13

Using Matlab for First Order ODEs

e z c o n t o u r ( G , [ 2 , 2 , 2 , 2 ] ) ;c o l o r b a r

DirectionFields
Firstdownloadthefiled i r f i e l d . m andputitinthesamedirectoryasyourothermfilesforthe homework. Definean@functionf oftwovariablest ,y correspondingtotherighthandsideofthe differentialequationy'(t)=f(t,y(t)).E.g.,forthedifferentialequationy'(t)=ty2define
f=@ ( t , y )t * y ^ 2

Youmustuse@ ( t , y ) . . . ,evenift ory doesnotoccurinyourformula. 2 E.g.,fortheODEy'=y youwouldusef = @ ( t , y ) y ^ 2 Toplotthedirectionfieldfortgoingfromt0tot1withaspacingofdtandygoingfromy0toy1 withaspacingofdyused i r f i e l d ( f , t 0 : d t : t 1 , y 0 : d y : y 1 ) .E.g.,fort andy between2and2 withaspacingof0.2type
d i r f i e l d ( f , 2 : 0 . 2 : 2 , 2 : 0 . 2 : 2 )

Solvinganinitialvalueproblemnumerically
Firstdefinethe@functionf correspondingtotherighthandsideofthedifferentialequation y'(t)=f(t,y(t)).E.g.,forthedifferentialequationy'(t)=ty2define
f=@ ( t , y )t * y ^ 2

Toplotthenumericalsolutionofaninitialvalueproblem:Fortheinitialconditiony(t0)=y0 youcanplotthesolutionfortgoingfromt0tot1usingo d e 4 5 ( f , [ t 0 , t 1 ] , y 0 ) . Example:Toplotthesolutionoftheinitialvalueproblemy'(t)=ty2,y(2)=1intheinterval[2,2] use


[ t s , y s ]=o d e 4 5 ( f , [ 2 , 2 ] , 1 ) p l o t ( t s , y s , ' o ' )
terpconnect.umd.edu/~petersd/246/matlabode.html 2/6

4/2/13

Using Matlab for First Order ODEs

Thecirclesmarkthevalueswhichwereactuallycomputed(thepointsarechosenbyMatlabto optimizeaccuracyandefficiency).Thevectorst s andy s containthecoordinatesofthesepoints, toseethemasatabletype[ t s , y s ] Youcanplotthesolutionwithoutthecirclesusingp l o t ( t s , y s ) . Tocombineplotsofthedirectionfieldandseveralsolutioncurvesusethecommandsh o l d o n andh o l do f f :Afterobtainingthefirstplottypeh o l do n ,thenallsubsequentcommandsplot inthesamewindow.Afterthelastplotcommandtypeh o l do f f . Example:Plotthedirectionfieldandthe13solutioncurveswiththeinitialconditionsy(2)=0.4, 0.2,...,1.8,2:
d i r f i e l d ( f , 2 : 0 . 2 : 2 , 2 : 0 . 2 : 2 ) h o l do n f o ry 0 = 0 . 4 : 0 . 2 : 2 [ t s , y s ]=o d e 4 5 ( f , [ 2 , 2 ] , y 0 ) ;p l o t ( t s , y s ) e n d h o l do f f

Toobtainnumericalvaluesofthesolutionatcertaintvalues:Youcanspecifyavectort v of tvaluesanduse[ t s , y s ]=o d e 4 5 ( g , t v , y 0 ) .Thefirstelementofthevectort v istheinitialt valuethevectort v musthaveatleast3elements.E.g.,toobtainthesolutionwiththeinitial


terpconnect.umd.edu/~petersd/246/matlabode.html 3/6

4/2/13

Using Matlab for First Order ODEs

conditiony(2)=1att=2,1.5,...,1.5,2anddisplaytheresultsasatablewithtwocolumns,use
[ t s , y s ] = o d e 4 5 ( f , 2 : 0 . 5 : 2 , 1 ) ; [ t s , y s ]

Toobtainthenumericalvalueofthesolutionatthefinaltvalueusey s ( e n d ) . Itmayhappenthatthesolutiondoesnotexistonthewholeinterval:
f=@ ( t , y )t * y ^ 2 [ t s , y s ]=o d e 4 5 ( f , [ 0 , 2 ] , 2 ) ;

Inthiscaseo d e 4 5 printsawarning"F a i l u r ea tt = . . . "toshowwhereitstopped. Notethatinsomecaseso d e 1 5 s performsbetterthano d e 4 5 .Thishappensforsocalledstiff problems.o d e 1 5 s isalsobetteratdetectingwhereasolutionstopstoexistiftheslopebecomes infinite.

Solvingadifferentialequationsymbolically
Youhavetospecifythedifferentialequationinastring,usingD y fory'(t)andy fory(t):E.g.,forthe 2 differentialequationy'(t)=ty type
s o l=d s o l v e ( ' D y = t * y ^ 2 ' , ' t ' )

Thelastargument' t ' isthenameoftheindependentvariable.Donottypey ( t ) insteadofy . IfMatlabcan'tfindasolutionitwillreturnanemptysymbol.IfMatlabfindsseveralsolutionsit returnsavectorofsolutions. HeretherearetwosolutionsandMatlabreturnsavectors o l withtwocomponents:s o l ( 1 ) is0 ands o l ( 2 ) is1 / ( t ^ 2 / 2+C 3 ) withanarbitraryconstantC 3 . ThesolutionwillcontainaconstantC 3 (orC 4 ,C 5 etc.).Youcansubstitutevaluesfortheconstant usings u b s ( s o l , ' C 3 ' , v a l u e ) .E.g.,tosetC 3 ins o l ( 2 ) to5use
s u b s ( s o l ( 2 ) , ' C 3 ' , 5 )

Tosolveaninitialvalueproblemadditionallyspecifyaninitialcondition:
s o l=d s o l v e ( ' D y = t * y ^ 2 ' , ' y ( 2 ) = 1 ' , ' t ' )

Toplotthesolutionusee z p l o t ( s o l , [ t 0 , t 1 ] ) .Hereisanexampleforplottingthesolution curvewiththeinitialconditionsy(2)=0.4:


s o l=d s o l v e ( ' D y = t * y ^ 2 ' , ' y ( 2 ) = 0 . 4 ' , ' t ' ) e z p l o t (s o l,[ 22 ] )

Toobtainnumericalvaluesatoneormoretvaluesuses u b s ( s o l , ' t ' , t v a l ) andd o u b l e (or v p a formoredigits):


s o l=d s o l v e ( ' D y = t * y ^ 2 ' , ' y ( 2 ) = 1 ' , ' t ' )

terpconnect.umd.edu/~petersd/246/matlabode.html

4/6

4/2/13

Using Matlab for First Order ODEs

Thisgivesanumericalvalueofthesolutionatt=0.5:
d o u b l e (s u b s ( s o l , ' t ' , 0 . 5 ))

Thiscomputesnumericalvaluesofthesolutionatt=2,1.5,...,2anddisplaystheresultasa tablewithtwocolumns:
t v a l=( 2 : 0 . 5 : 2 ) ' ;%c o l u m nv e c t o rw i t ht v a l u e s y v a l=d o u b l e (s u b s ( s o l , ' t ' , t v a l )) %c o l u m nv e c t o rw i t hy v a l u e s [ t v a l , y v a l ]%d i s p l a y2c o l u m n st o g e t h e r

Symbolicsolutions:Dealingwithsolutionsin implicitform
Oftend s o l v e says'E x p l i c i ts o l u t i o nc o u l dn o tb ef o u n d '.Butinmanycasesonecanstill obtainthesolutioninimplicitform,andusethistoplotthegraphofthesolution,ortoobtain numericalapproximations. Ifd s o l v e says'E x p l i c i ts o l u t i o nc o u l dn o tb ef o u n d 'therearetwopossibilities:(Notethat differentversionsofthesymbolictoolboxbehavedifferently) 1. d s o l v e returnstheanswerintheformR o o t O f ( e x p r e s s i o n , z ) ors o l v e ( e q u a t i o n , y ) 4 Example1:SolvetheIVPy'=t/(y 1),y(1)=0. d s o l v e ( ' D y = t / ( y ^ 4 1 ) , y ( 1 ) = 0 ' , ' t ' ) returnsinMatlabR2010b R o o t O f ( X 8 9 ^ 5-5 * X 8 9-( 5 * t ^ 2 ) / 2+5 / 2 ,X 8 9 ) Thismeansthatthesolutioninimplicitformis y55y5t2/2+5/2=0 2. d s o l v e returnstheanswer[ e m p t ys y m] InthiscaseMatlabwasunabletofindthesolutioninimplicitform.Inolderversions(e.g. MatlabR2010b)thiscanevenhappenwheniteasytofindbyhandthesolutioninimplicit form.Insomecasesomittingtheinitialconditionhelps: ForExample1newerMatlabversions(R2011b,R2012b)return[ e m p t ys y m ] .Inthiscase usingd s o l v e ( ' D y = t / ( y ^ 4 1 ) ' , ' t ' ) givestheimplicitsolutionwithaconstant.Wecanthen findthevalueoftheconstantusingtheinitialcondition. PlottingthesolutionofIVPinimplicitform:Ifthesolutioninimplicitformisexpression=0use e z p l o t ( e x p r e s s i o n , [t ) m i nt m a xy m i ny m i n] toplotthesolutiony(t)fortminttmax ,yminyymax . E.g.,forExample1wecanplottheinitialpointtogetherwiththesolutioncurveby
h o l do n ;p l o t ( 1 , 0 , ' o ' ) ; e z p l o t ( ' y ^ 5-5 * y+5 / 2-5 * t ^ 2 / 2 ' , [ 2222 ] ) ;g r i do n ;h o l do f f

terpconnect.umd.edu/~petersd/246/matlabode.html

5/6

4/2/13

Using Matlab for First Order ODEs

Weseefromthegraphthattheintervalwherethesolutionexistsisroughly(1.6,1.6). Plottingthegeneralsolutioninimplicitform:Ifthegeneralsolutioninimplicitformis expression=CwithCarbitrary,use e z c o n t o u r ( e x p r e s s i o n , [t ) m i nt m a xy m i ny m i n] E.g.,forExample1wecanplotthegeneralsolutionby


e z c o n t o u r ( ' y ^ 5-5 * y+5 / 2-5 * t ^ 2 / 2 ' , [ 2222 ] ) e z c o n t o u r plotscontoursfor9valuesofC.Ifyouwanttoseemorecontourcurves:Downloadthe filee z c o n t o u r c . m andputitinthesamedirectoryasyourothermfiles.Thenyoucanusee.g. e z c o n t o u r c ( ' y ^ 5-5 * y+5 / 2-5 * t ^ 2 / 2 ' , [ 2222 ] , 5 0 )

toobtaincontourcurvesfor50valuesofC.

Findingvaluesofthesolutioninimplicitform: ForExample1weobtainedthesolutioninimplicitformy55y+5/25t2/2=0. Wenowwanttofindy(1.5):Weplugt=1.5intotheequationandneedtosolvetheequationy5 5y+5/251.52/2=0fory.Fromthegraphabovewecanseethatthereareactually3solutions: near1.5,near0.5,andnear1.5.Thesolutionwewantistheonenear0.5. Tofindasolutionynear0.5use whichreturnstheanswery=0.647819. TobiasvonPetersdorff


t = 1 . 5 ;f z e r o ( @ ( y ) y ^ 5 5 * y + 5 / 2 5 * t ^ 2 / 2 , 0 . 5 )

terpconnect.umd.edu/~petersd/246/matlabode.html

6/6

Das könnte Ihnen auch gefallen