16.12.2012 Views

Computer Algebra Recipes

Computer Algebra Recipes

Computer Algebra Recipes

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2.3. NUMERICAL SOLUTION OF ODES 93<br />

round-o® error mentioned earlier. To compare the speed of di®erent numerical<br />

schemes or algorithms, the computer CPU time at which the execution of the<br />

algorithm begins is recorded using the time command. The CPU time at which<br />

the execution is ¯nished will also be recorded, the CPU time for the algorithm<br />

then being the di®erence. The CPU time will depend on the computer being<br />

used, the times quoted here being for a 3-GHz PC.<br />

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

Functional operators are formed to calculate R and F for speci¯ed x and y.<br />

> R:=(x,y)->a*x-b*x*y: F:=(x,y)->-c*y+d*x*y:<br />

A do loop is used to iterate the di®erence equations (2.16) and the time.<br />

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

> r[n+1]:=r[n]+h*R(r[n],f[n]);<br />

> f[n+1]:=f[n]+h*F(r[n],f[n]);<br />

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

A 3-dimensional plotting point is formed for the nth time step.<br />

> pnt[n]:=[t[n],r[n],f[n]];<br />

> end do:<br />

On ending the loop, the temporal evolution of the solution will be observed by<br />

creating a three-dimensional plot using a sequence of the plotting points from<br />

n =0ton = N and the pointplot3d command.<br />

> pointplot3d([seq(pnt[n],n=0..N)],symbol=cross,color=red,<br />

axes=normal,labels=["t","r","f"],tickmarks=[2,3,3]);<br />

20<br />

t<br />

600<br />

f<br />

400<br />

200<br />

0 0<br />

200<br />

r<br />

400<br />

Figure 2.22: Forward Euler solution of the rabbits{foxes equations.

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

Saved successfully!

Ooh no, something went wrong!