16.12.2012 Views

Computer Algebra Recipes

Computer Algebra Recipes

Computer Algebra Recipes

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

98 CHAPTER 2. PHASE-PLANE ANALYSIS<br />

After loading the plots package, specifying the number of digits, and recording<br />

the beginning time,<br />

> restart: with(plots): Digits:=10: begin:=time():<br />

the parameter values are entered and N = 1000 steps of size h =0:05 are<br />

considered.<br />

> t[0]:=0: x[0]:=3: y[0]:=3: a:=0.05: b:=0.5: N:=1000: h:=0.05:<br />

The functional, or arrow, operator is used to produce the right-hand side of<br />

each ODE. Whatever forms are inserted for x and y in the entries X(x,y) and<br />

Y(x,y) will be operated on as indicated on the right-hand side of the arrow.<br />

> X:=(x,y)-> -x+a*y+x^2*y;<br />

X := (x; y) !¡x + ay+ x2 y<br />

> Y:=(x,y)->b-a*y-x^2*y;<br />

Y := (x; y) ! b ¡ ay¡ x2 y<br />

Similarly, operators are introduced to perform the four function evaluations,<br />

where f will be taken to be X and Y . Note that X and Y do not depend<br />

explicitly on time here.<br />

> k1:=f->h*f(x[n],y[n]):<br />

> k2:=f->h*f(x[n]+K1[n]/2,y[n]+L1[n]/2):<br />

> k3:=f->h*f(x[n]+K2[n]/2,y[n]+L2[n]/2):<br />

> k4:=f->h*f(x[n]+K3[n],y[n]+L3[n]):<br />

Using the above operators, the fourth-order Runge{Kutta algorithm is iterated<br />

from n =0toN = 1000 and a plotting point produced at each step.<br />

> for n from 0 to N do<br />

> K1[n]:=k1(X); L1[n]:=k1(Y);<br />

> K2[n]:=k2(X); L2[n]:=k2(Y);<br />

> K3[n]:=k3(X); L3[n]:=k3(Y);<br />

> K4[n]:=k4(X); L4[n]:=k4(Y);<br />

> x[n+1]:=x[n]+(K1[n]+2*K2[n]+2*K3[n]+K4[n])/6;<br />

> y[n+1]:=y[n]+(L1[n]+2*L2[n]+2*L3[n]+L4[n])/6;<br />

> t[n+1]:=t[n]+h;<br />

> pnt[n]:=[t[n],x[n],y[n]];<br />

> end do:<br />

Using the spacecurve command, the sequence of points is plotted as a solid<br />

blue line with normal axes. The tickmarks are controlled, axis labels added,<br />

and a particular orientation of the 3-dimensional viewing box chosen.<br />

> spacecurve([seq(pnt[n],n=0..N)],color=blue,axes=normal,<br />

tickmarks=[2,2,2],labels=["t","x","y"],<br />

orientation=[23,71]);<br />

The resulting picture is shown in Figure 2.24. It may be rotated on the computer<br />

screen by dragging with the mouse.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!