Beruflich Dokumente
Kultur Dokumente
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 )
makeacontourplotofthefunctionforarectangleinthex,yplane:
terpconnect.umd.edu/~petersd/246/matlabode.html 1/6
4/2/13
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
4/2/13
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
4/2/13
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 ) ;
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 ' )
terpconnect.umd.edu/~petersd/246/matlabode.html
4/6
4/2/13
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
toobtaincontourcurvesfor50valuesofC.
terpconnect.umd.edu/~petersd/246/matlabode.html
6/6