Vector Fields and Trajectories!

In this notebook, we're going to learn how to make vector fields and combine them with trajectory curves.  We will use the rabbits and foxes example from Section 8.1 in Calculus in Context to illustrate this process. Notice the following things about the VectorPlot command:
    • The first argument is a list of derivatives (in this case, {Rprime, Fprime}).
    • The next two arguments specify the x-range and y-range.
    • PlotRange is necessary because otherwise we get bunches of weird dots and most arrowheads will have no arrowbodies.

Clear[R]
Clear[F]
VectorPlot[{0.1` R (1-(R/10000))-0.005` R F,0.00004` R F-0.04` F},{R,0,3000},{F,0,30}, PlotRange->{{0,3000},{0,30}}]

Notice that you can do this with pretty much any derivatives (try it!).
    Now, we already know how to plot a trajectory---we just do it by using a modification of our Euler's Method code to rabbits and foxes, and instead of doing t vs. R and t vs. F, we do R vs. F.  Like so:

Clear[t];
Clear[tinitial];
Clear[tfinal];
Clear[numberofsteps];
Clear[deltat];
Clear[datapoints];
datapoints={};
R=1000;
F=7;
tinitial=0;
tfinal=300;
t=tinitial;
numberofsteps=3000;
deltat=((tfinal-tinitial)/numberofsteps);
For[k=1,k≤numberofsteps,Rprime=0.1` R (1-(R/10000))-0.005` R F;Fprime=0.00004` R F-0.04` F;deltaR=Rprime deltat;deltaF=Fprime deltat;t=t+deltat;R=R+deltaR;F=F+deltaF;datapoints=Append[datapoints,{t,R,F}];k++]
rabbitsfoxes=Table[{datapoints[[i,2]],datapoints[[i,3]]},{i,1,Length[datapoints]}];
ListPlot[rabbitsfoxes,Joined->True]

It's hard to figure out the values at various points, so we have a couple of ways of dealing with that.

ListPlot[rabbitsfoxes,Joined->True, GridLines->Automatic]

ListPlot[Tooltip[rabbitsfoxes],Joined->True]

Oh, wanna show those two plots on the same axes?  Let's go for it.

Clear[R]
Clear[F]
Show[ListPlot[rabbitsfoxes,Joined->True],VectorPlot[{0.1` R (1-(R/10000))-0.005` R F,0.00004` R F-0.04` F},{R,0,3000},{F,0,30}]]

Notice that we had to clear R and F because they'd just been defined when we ran our Euler's Method code.

We can do even better, though:  We can show the evolution of the trajectory through the vector field over time.

Clear[R]
Clear[F]
Animate[Show[ListPlot[Table[{datapoints[[i,2]],datapoints[[i,3]]},{i,1,k}],Joined->True],VectorPlot[{0.1` R (1-(R/10000))-0.005` R F,0.00004` R F-0.04` F},{R,0,3000},{F,0,30}]],{k,1,Length[datapoints],1}]

Better, yet:  We can Manipulate the evolution of the trajectory through the vector field over time.

Clear[R]
Clear[F]
Manipulate[Show[ListPlot[Table[{datapoints[[i,2]],datapoints[[i,3]]},{i,1,k}],Joined->True],VectorPlot[{0.1` R (1-(R/10000))-0.005` R F,0.00004` R F-0.04` F},{R,0,3000},{F,0,30}]],{k,1,Length[datapoints],1}]

Still not enough for you?  Oh, ho, ho, let's fiddle with the parameters!

RabbitsFoxes[tfinal_,numberofsteps_,rab_,fox_,a_,b_,c_,d_, e_] := Block[{t,tinitial,deltat, datapointsrf,rprime, fprime,deltar,deltaf, foxes, rabbits,r, F,u,v},
datapointsrf = {};
tinitial=0;
t = tinitial;
r =rab;
F = fox;
deltat = (tfinal- tinitial)/numberofsteps;
For[k=1,k≤numberofsteps,
rprime =a r (1-(r/b))-c r F; 
fprime = d r F-e F; 
deltar = rprime*deltat; 
deltaf = fprime*deltat; 
t=t+deltat; 
r = r+deltar; 
F = F + deltaf; 
datapointsrf =Append[datapointsrf, {r,F}];
 k++];
Clear[r];
Clear[F];
Show[ListPlot[datapointsrf, Joined -> True],VectorPlot[{a r (1-(r/b))-c r F,d r F-e F},{r,0,Max[Transpose[datapointsrf][[1]]]},{F,0,Max[Transpose[datapointsrf][[2]]]}]]]

RabbitsFoxes[300,3000,1000,7,.1,10000,.005,.00004, .04]

You know what you can do next.... Manipulate over time, over parameters... go for it.