Maple Tools for Differenial Equaions A. J. Meir Copyrigh (C) A. J. Meir. All righs reserved. This workshee is for educaional use only. No par of his publicaion may be reproduced or ransmied for profi in any form or by any means, elecronic or mechanical, including phoocopy, recording, or any informaion sorage and rerieval sysem wihou prior wrien permission from he auhor. No for profi disribuion of he sofware is allowed wihou prior wrien permission, providing ha he workshee is no modified in any way and full credi o he auhor is acknowledged. (This documen is largely based on a similar documen originally wrien by P. G. Schmid.) Maple will find symbolic soluions (ha is, closed-form soluions or soluion formulas) for many ypes of d.e.s and i.v.p.s, including separable and linear firs-order equaions. Moreover, Maple can compue and graph numerical soluions (ha is, approximae soluions) for virually all i.v.p.s of pracical ineres. 1. Finding and Ploing Symbolic Soluions. To find a general soluion for a given d.e., use Maple's dsolve command, whose general forma is dsolve(de,unknown_funcion). The following command, for example, finds a general soluion for he d.e. x' = Kk x (he radioacivedecay equaion), where he unknown funcion is x = x and k is a parameer: dsolve(d(x)()=-k*,); x = _C1 e Kk (1) Noe ha inside he dsolve command he derivaive, x', should be wrien as D x and ha he independen variable,, mus be carried along (so ha he d.e. becomes D x = Kk x insead of x' = Kk x ). In Maple's response, _C1 denoes an arbirary consan. (In general, Maple renders arbirary consans as _C1, _C, _C3, and so forh.) In sandard calculus noaion, we would wrie he soluion formula as x = c e Kk. The nex command produces a general soluion for he d.e. x'' + a x = 0 (he elasic-spring equaion wih a = k ), where he unknown funcion is again x = x and a is a parameer: m dsolve(d(d(x))()+a^*,); x = _C1 sin a C_C cos a () Noe ha he second derivaive, x'', should be wrien as D D x and ha he independen variable mus be carried along, jus as in he previous example. Wih a minor modificaion, we can use he dsolve command o find symbolic soluions of i.v.p.s; he modified forma is dsolve({de,iniial_condiion(s)},unknown_funcion). Noe he "curly braces" around he d.e. and iniial condiion(s). For example, le's solve he firs-order i. v.p. x' = Kk x, x 0 = x0 : dsolve({d(x)()=-k*,x(0)=x0},); x = x0 e Kk (3) Nex, we solve he second-order i.v.p. x'' + a x = 0, x 0 = x0, x'(0) = v0 : 1
dsolve({d(d(x))()+a^*=0,x(0)=x0,d(x)(0)=v0},); v0 sin a x = Cx0 cos a a () In order o plo a soluion generaed by he dsolve command, we firs have o give i a name, say, s. The following command ses s equal o he righ-hand side of he immediaely preceding soluion formula: s:=rhs(%); s := v0 sin a a Cx0 cos a (5) Noe: The percen (%) is jus a placeholder for he las oupu generaed before execuion of he presen command (which, in his case, happens o be he soluion formula produced by he preceding dsolve command). Insead of using he percen, we could have given a name (say, sol ) o he soluion formula and used ha name in he rhs command: sol:=dsolve({d(d(x))()+a^*=0,x(0)=x0,d(x)(0)=v0},); v0 sin a sol := x = Cx0 cos a a (6) s:=rhs(sol); s := v0 sin a a Cx0 cos a (7) If we now specify he value of he parameer a and he iniial values x0 and v0, we can plo s jus like any oher funcion. Se, for example, a = 1, x0 =, and v0 = K3 : a:=1; x0:=; v0:=-3; a := 1 x0 := v0 := K3 (8) In his case, he soluion of he i.v.p. is s; K3 sin C cos (9) and a graph can be generaed wih he usual plo command: plo(s,=-10..0);
3 1 K10 0 10 0 K1 K K3 To obain he soluion and is graph for oher values of he parameer a or oher iniial values x0 and v0, jus re-execue he las hree commands, changing only he firs one as needed. For furher deails on ploing, especially on simulaneous ploing of several graphs, see he handou "More on Basic Maple Commands".. Ploing Direcion Fields and Numerical Soluions. The basic Maple command for he graphical represenaion of direcion fields and approximae soluion curves is DEplo ; i is par of a package of rouines called DEools, which mus be explicily loaded wih he command wih(deools): To plo a direcion field for a firs-order d.e. of he form x' = f, x, in he recangle = a..b, x = c..d, use he DEplo command in he form DEplo(D(x)()=f(,),,=a..b,x=c..d). The following command, for example, generaes a direcion field for x' = x in he recangle = K3..3, x =..6 : DEplo(D(x)()=^,,=-3..3,x=-6..6); 3
6 K3 K K1 0 1 3 K K The defaul number of grid poins used is 0 in eiher coordinae direcion. You can change ha, if needed, by adding he opion dirgrid=[m,n] inside he DEplo command, where m and n denoe he desired number of grid poins in he horizonal and verical direcion, respecively. Example: DEplo(D(x)()=^,,=-3..3,x=-6..6,dirgrid=[15,30]);
6 K3 K K1 0 1 3 K K In addiion o a direcion field, Maple will compue and display one or more approximae soluion curves of a given d.e. if suiable iniial condiions are specified in he form {[x(0)=x0],[x(1)=x1],[x()=x],...}, immediaely following he range specificaions in he DEplo command. Noe ha he se of iniial condiions is enclosed in curly braces, while each individual condiion appears in brackes. Boh, brackes and braces, mus be presen even if only a single iniial condiion is given. For example, he following command produces a direcion field for x' = x, as before, along wih he (approximae) graph of a paricular soluion, saisfying he iniial condiion x 0 =1 : DEplo(D(x)()=^,,=-3..3,x=-6..6,{[x(0)=1]}); 5
6 K3 K K1 0 1 3 K K Now we will add a second soluion curve, saisfying he iniial condiion x 0 = K1 : DEplo(D(x)()=^,,=-3..3,x=-6..6,{[x(0)=1],[x(0)=-1]}) ; 6 K3 K K1 0 1 3 K K 6
In general, he given iniial poins do no have o lie inside he plo window. If iniial condiions are given, specificaion of he verical range (x-range) is opional; if omied, DEplo will choose he x-range auomaically, depending on where he soluion curves "go". This is someimes useful, bu no always. Le's ry i wih he above example (same command as before, bu wihou x-range specificaion): DEplo(D(x)()=^,,=-3..3,x=-6..6,{[x(0)=1],[x(0)=-1]}) ; 6 K3 K K1 0 1 3 K K Can you explain he poor oucome? (Hin: The exac soluion wih iniial condiion x 0 = 1 has a verical asympoe a = 1.) If only he soluion curves are of ineres, display of he direcion field arrows can be suppressed by adding he opion arrows=none inside he DEplo command, following he specificaion of iniial condiions. Example: DEplo(D(x)()=^,,=-3..3,x=-6..6,{[x(0)=1],[x(0)=-1]}, arrows=none); 7
6 K3 K K1 0 1 3 K K Many furher opions are available. In fac, mos of he opions available wih he basic plo command will also work wih DEplo. Very useful for prining purposes is he opion linecolor=black, which causes he soluion curves o be drawn in black raher han yellow (he defaul, which looks piiful on a black-and-whie prinou). The color and appearance of he direcion field arrows can be cusomized, oo, bu his will hardly ever be necessary. For deails, consul he relevan help pages (by yping?deplo or?plo,opions). Noe: pions may be added o he DEplo command in any order, bu mus be preceded by he range specificaion(s) and iniial condiion(s) (if any). Example: DEplo(D(x)()=^,,=-3..3,x=-6..6,{[x(0)=1],[x(0)=-1]}, dirgrid=[10,10],linecolor=black,color=green,ile=`dx/d=x^`); 8
dx/d=x^ 6 K3 K K1 0 1 3 K K The defaul mehod ha DEplo uses o compue approximae soluions of he given d.e. is "rk", a socalled h order Runge-Kua mehod, much more sophisicaed han he classical Euler mehod. You can force Maple o use he Euler mehod by specifying he opion mehod=classical. Boh "rk" and "Euler" employ a fixed sepsize; he defaul sepsize is (b Ka)/0, where a..b is he horizonal range (-range) of he plo. Frequenly, i will be necessary o reduce he sepsize, for improved numerical accuracy. This can be done by specifying he opion sepsize=h, where h denoes he desired sepsize, smaller han (b Ka)/0. Le's ry he following example, where we approximae he same soluions of x' = x as before, bu wih a larger verical range (K10..10) and using he classical Euler mehod (insead of "rk"): DEplo(D(x)()=^,,=-3..3,x=-9..9,{[x(0)=1],[x(0)=-1]}, mehod=classical,linecolor=black); 9
8 6 K3 K K1 0 1 3 K K K8 The horizonal par of he lower soluion curve is clearly erroneous -- i cerainly doesn' mach he direcion field! The (defaul) sepsize used in he plo is.3 (namely, (b Ka)/0 wih b = 3 and a = K3 ), and apparenly his is no accurae enough. Reducing he sepsize should remedy he problem: DEplo(D(x)()=^,,=-3..3,x=-9..9,{[x(0)=1],[x(0)=-1]}, mehod=classical,sepsize=.1,linecolor=black); 10
8 6 K3 K K1 0 1 3 K K K8 To accuraely rack a rapidly growing or highly oscillaory soluion, i may be necessary o employ a very small sepsize and compue millions of daa poins. No only will his consume a lo of ime -- soring millions of daa poins may also ax Maple's memory. Forunaely, i is no nessary o sore all he compued daa poins (he number of poins used in creaing he plo is anyway limied by he resoluion of compuer screen and laser priner). Wih he opion ieraions=n, you can insruc Maple o sore and plo only every nh of he compued daa poins. This opion, if applied judiciously, can save a lo of ime and memory, wih no loss of numerical accuracy. As a drasic example, we will compue and display an approximae soluion of he i.v.p. dx =1Ke x, x 0 =0, d on he inerval = 0..15, using "rk" (he defaul mehod) wih a sepsize of.001 and 100 ieraions beween ploed poins. (Thus, Maple will compue 15,000 daa poins, bu sore and plo only 150.) DEplo(D(x)()=1-exp()*^,,=0..15,{[x(0)=0]}, sepsize=.001,ieraions=100,arrows=none,linecolor=black); 11
0.6 0.5 0. 0.3 0. 0.1 0 0 5 10 15 Generaing his plo sill akes quie some ime, bu wihou he opion ieraions=100, i would ake abou en imes as long, wihou any visible improvemen! n he oher hand, if you ried o speed hings up by using a sepsize significanly greaer han.001, he plo would no be reliable. Check i ou! Beyond "rk" (he defaul mehod) and "Euler" ( mehod=classical ), you have a choice of several, much more sophisicaed d.e. solvers. ne of hem is "rkf5", a so-called h/5h order Runge-Kua-Fehlberg mehod, which can be invoked by specifying mehod=rkf5 in he DEplo command. While "rk" and "Euler" use a fixed sepsize, "rkf5" employs a variable sepsize ha's auomaically adjused in accordance wih he "siffness" of he problem. As an adapive (self-correcing) mehod, "rkf5" is more reliable han "rk" or "Euler", bu usually slower. Bu in "siff" problems (like he previous example), "rkf5" offers a double advanage: You don' have o worry abou he sepsize, and i is faser han "rk" or "Euler"! (In general, conrolling and adjusing he sepsize as needed is more efficien han using a very small sepsize hroughou.) Since "rkf5" conrols he (numerical) sepsize auomaically, specifying a sepsize when using his mehod has no effec on he numerical accuracy of he procedure; i does, however, affec he number of poins ploed and, hereby, he apparen smoohness of he graph. More precisely, in conjuncion wih "rkf5", he specified sepsize becomes simply he horizonal disance beween consecuive poins ploed. For example, if we solve he previous problem wih "rkf5", we should specify a sepsize of.1 o obain he same number of poins ploed as before, when we used "rk" wih a (numerical) sepsize of.001 and 100 ieraions beween poins ploed. Le's ry i: DEplo(D(x)()=1-exp()*^,,=0..15,{[x(0)=0]}, mehod=rkf5,sepsize=.1,arrows=none,linecolor=black); 1
0.6 0.5 0. 0.3 0. 0.1 0 0 5 10 15 DEplo will no only solve firs-order, bu also higher-order i.v.p.s (alhough no direcion fields can be generaed for he laer). The following command, for example, produces an approximae soluion of he i.v.p. x'' + x = 0, x 0 =, x'(0) = -3, whose symbolic soluion is cos K3 sin : DEplo(D(D(x))()+=0,,=-10..0,{[x(0)=,D(x)(0)=-3]}, sepsize=.,linecolor=black); 13
3 1 K10 0 10 0 K1 K K3 Noe ha he wo iniial condiions appear inside a single pair of brackes (because hey deermine only one soluion curve). The sepsize specificaion was necessary, due o he oscillaory naure of he graph and he lengh of he plo inerval; he defaul sepsize would have been 30/0 = 1.5! (Check ou wha you would ge wihou sepsize specificaion.) Basically, you have he same opions as in he firs-order case (excep for hose relaing o direcion fields, of course), and jus as in he firs-order case, several approximae soluions can be generaed simulaneously, by specifying several ses of iniial daa in he form {[x(0)=x0,d(x)(0)=v0],[x(1)=x1,d(x)(1)=v1],[x(3)=x3,d(x)(3)=v3],...}. Noe ha, in analogy o he firs-order case, he enire se of iniial condiions is enclosed in curly braces, while he iniial condiions for each individual soluion curve appear in brackes. Example: DEplo(D(D(x))()+=0,,=-10..0,{[x(0)=,D(x)(0)=-3],[x (0)=-3,D(x)(0)=]},linecolor=black); 1
3 1 K10 0 10 0 K1 K K3 Prey lousy! bviously, we should reduce he sepsize or use a more flexible solver. Le's do boh and also add some perks: DEplo(D(D(x))()+=0,,=-10..0,{[x(0)=,D(x)(0)=-3],[x (0)=-3,D(x)(0)=]}, mehod=rkf5,sepsize=.,linecolor=[blue,green], ile=`x(0)=, x'(0)=-3 (blue); x(0)=-3, x'(0)= (green)`, ilefon=[times,rman,1]); 15
x(0)=, x'(0)=-3 (blue); x(0)=-3, x'(0)= (green) 3 1 K10 0 10 0 K1 K K3 16