odes mat lab

Upload: breytner-joseph-fernandez-mesa

Post on 02-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Odes Mat Lab

    1/7

    Using Matlab for First Order ODEs

    Contents

    @-functionsDirection fieldsNumerical solution of initial value problems

    Plotting the solutionCombining direction field and solution curvesFinding numerical values at given t values

    Symbolic solution of ODEsFinding the general solutionSolving initial value problems

    Plotting the solutionFinding numerical values at given t values

    Symbolic solutions Dealing !ith solutions in implicit form

    @-functions

    "ou can define a function in #atlab using the @-synta$

    g % @&$' sin&$'($

    defines the function g&$' % sin&$')$* "ou can then

    evaluate the functionfor a given $-valueg&+*,'

    plot the graph of the functionover an intervalezplot&g.+/+0'

    find a zero of the functionnear an initial guessfzero&g,'

    "ou can also define @-functions of several variables

    1 % @(x,y)$23 4 y23 - 3(&$2/4y2/' 4 3

    defines the function 1&$y' % $34 y3- 3&$/4 y/' 4 3 of t!o variables* "ou canthen

    evaluate the functionfor given values of $y1&5/'

    plot the graph of the functionas a surface over a rectangle in the $yplane

    ezsurf&1.-//-//0'

    Clic6 on in the figure toolbar then you can rotate the graph bydragging !ith the mouse*

    http://terpconnect.umd.edu/~petersd/246/matlabode.html#L584http://terpconnect.umd.edu/~petersd/246/matlabode.html#L585http://terpconnect.umd.edu/~petersd/246/matlabode.html#L586http://terpconnect.umd.edu/~petersd/246/matlabode.html#L588http://terpconnect.umd.edu/~petersd/246/matlabode.html#L589http://terpconnect.umd.edu/~petersd/246/matlabode.html#L590http://terpconnect.umd.edu/~petersd/246/matlabode.html#L591http://terpconnect.umd.edu/~petersd/246/matlabode.html#L592http://terpconnect.umd.edu/~petersd/246/matlabode.html#L593http://terpconnect.umd.edu/~petersd/246/matlabode.html#L594http://terpconnect.umd.edu/~petersd/246/matlabode.html#L595http://terpconnect.umd.edu/~petersd/246/matlabode.html#implicithttp://terpconnect.umd.edu/~petersd/246/matlabode.html#L585http://terpconnect.umd.edu/~petersd/246/matlabode.html#L586http://terpconnect.umd.edu/~petersd/246/matlabode.html#L588http://terpconnect.umd.edu/~petersd/246/matlabode.html#L589http://terpconnect.umd.edu/~petersd/246/matlabode.html#L590http://terpconnect.umd.edu/~petersd/246/matlabode.html#L591http://terpconnect.umd.edu/~petersd/246/matlabode.html#L592http://terpconnect.umd.edu/~petersd/246/matlabode.html#L593http://terpconnect.umd.edu/~petersd/246/matlabode.html#L594http://terpconnect.umd.edu/~petersd/246/matlabode.html#L595http://terpconnect.umd.edu/~petersd/246/matlabode.html#implicithttp://terpconnect.umd.edu/~petersd/246/matlabode.html#L584
  • 8/10/2019 Odes Mat Lab

    2/7

    plot the curves where G(x,y)=0in a rectangle in the $y planeezplot&1.-//-//0'

    make a contour plot of the functionfor a rectangle in the $y planeezcontour&1.-//-//0'7 colorbar

    Direction FieldsFirst do!nload the file dirfield*mand put it in the same directory as your otherm-files for the home!or6*

    Define an @-function f of t!o variables t y corresponding to the right handside of the differential e8uation y9&t' % f&ty&t''* E*g* for the differential

    e8uation y9&t' % t y/define

    f % @&ty' t(y2/

    "ou must use @&ty'*** even if t or y does not occur in your formula*

    E*g* for the ODE y9%y/you !ould use f%@&ty'y2/

    :o plot the direction field for t going from t+ to t5 !ith a spacing of dt and ygoing from y+ to y5 !ith a spacing of dy use dirfield&ft+dtt5y+dyy5'* E*g*for t and y bet!een -/ and / !ith a spacing of +*/ type

    dirfield&f-/+*//-/+*//'

    Solving an initial value problem numerically

    First define the @-function f corresponding to the right hand side of thedifferential e8uation y9&t' % f&ty&t''* E*g* for the differential e8uation y9&t'

    % t y/define

    f % @&ty' t(y2/

    :o plot the numerical solution of an initial value problem For the initialcondition y&t+'%y+ you can plot the solution for t going from t+ to t5

    http://terpconnect.umd.edu/~petersd/246/dirfield.mhttp://terpconnect.umd.edu/~petersd/246/dirfield.m
  • 8/10/2019 Odes Mat Lab

    3/7

    using ode3;&f.t+t50y+'*

    E$ample :o plot the solution of the initial value problem y9&t' % t y/ y&-/'%5 inthe interval .-//0 use

    .tsys0 % ode3;&f.-//05'

    plot&tsys9o-9'

    :he circles mar6 the values !hich !ere actually computed &the points arechosen by #atlab to optimi

  • 8/10/2019 Odes Mat Lab

    4/7

    :o obtain numerical values of the solution at certain t values "ou can specifya vector tv of t values and use .tsys0 % ode3;&gtvy+'* :he first element of the

    vector tv is the initial t value7 the vector tv must have at least , elements* E*g*to obtain the solution !ith the initial condition y&-/'%5 at t % -/ -5*; *** 5*; /and display the results as a table !ith t!o columns use

    .tsys0%ode3;&f-/+*;/5'7

    .tsys0

    :o obtain the numerical value of the solution at the final t-value use ys&end' *

    "t may happen that the solution does not exist on the whole interval#

    f % @&ty' t(y2/.tsys0 % ode3;&f.+/0/'7

    ?n this case ode45prints a !arning Failure at t=... to sho! !here it

    stopped*

    Note that in some cases ode$%sperforms better than ode3;* :his happensfor so-called stiff problems* ode5;s is also better at detecting !here asolution stops to e$ist if the slope becomes infinite*

    Solving a differential equation symbolically"ou have to specify the differential e8uation in a string using Dy for y9&t'

    and y for y&t' E*g* for the differential e8uation y9&t' % t y/type

    sol % dsolve&9Dy%t(y2/99t9'

    :he last argument 9t9 is the name of the independent variable* Do nottype y&t' instead of y*

    ?f #atlab can9t find a solution it !ill return an empty symbol* ?f #atlab finds

    several solutions it returns a vector of solutions*

    Aere there are t!o solutions and #atlab returns a vector sol!ith t!o

  • 8/10/2019 Odes Mat Lab

    5/7

    components sol(1)is 0and sol(2)is -1/(t^2/2 + C3)!ith an arbitrary

    constant C3*

    :he solution !ill contain a constant C, &or C3C; etc*'* "ou can substitutevalues for the constant using subs&sol9C,9value'* E*g* to set C, in sol&/' to ;use

    subs&sol&/'9C,9;'

    :o solve an initial value problem additionally specify an initial condition

    sol % dsolve&9Dy%t(y2/99y&-/'%599t9'

    :o plot the solution use e

  • 8/10/2019 Odes Mat Lab

    6/7

    ootOf&G>H2; - ;(G>H - &;(t2/'/ 4 ;/ G>H':his means that the solution in implicit form is

    y;- ;y - ;t// 4 ;/ % +2.dsolve returns the ans!er empty sym !?n this case #atlab !as unable to find the solution in implicit form* ?n

    older versions &e*g* #atlab /+5+b' this can even happen !hen it easyto find by hand the solution in implicit form* ?n some cases omitting theinitial condition helpsFor E$ample 5 ne!er #atlab versions &/+55b /+5/b' return .emptysym0* ?n this case using dsolve&9Dy%t&y23-5'99t9' gives the implicitsolution !ith a constant* Ie can then find the value of the constantusing the initial condition*

    lotting the solution of "* in implicit form#?f the solution in implicit formis expression%+ use

    ezplot(expression,[ tmintmaxyminymin])

    to plot the solution y&t' for tmin t J t ma$ ymin y J y ma$*E*g* for E$ample 5 !e can plot the initial point together !ith the solutioncurve by

    $old o!% plot(1&0&'o')%

    eplot('^5 - 5* + 5/2 - 5*t^2/2'&-2 2 -2 2,)% rid o!% $old

    o##

    Ie see from the graph that the interval where the solution existsis roughly&-5*K 5*K'*

    lotting the general solution in implicit form#?f the general solution inimplicit form is expression%C !ith C arbitrary use

    ezcontour(expression, tmin

    tmax

    ymin

    ymin

    !)

    E*g* for E$ample 5 !e can plot the general solution bye

  • 8/10/2019 Odes Mat Lab

    7/7

    t%5*;7 fzero&@&y'y2;-;(y4;/-;(t2//-+*;'!hich returns the ans!er y%-+*K3L>5H*