From: <jd...@us...> - 2007-10-28 15:00:39
|
Revision: 4038 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=4038&view=rev Author: jdh2358 Date: 2007-10-28 08:00:36 -0700 (Sun, 28 Oct 2007) Log Message: ----------- added lotka example Modified Paths: -------------- trunk/py4science/examples/qsort.py Added Paths: ----------- trunk/py4science/examples/lotka_volterra.py trunk/py4science/examples/skel/lotka_volterra_skel.py Added: trunk/py4science/examples/lotka_volterra.py =================================================================== --- trunk/py4science/examples/lotka_volterra.py (rev 0) +++ trunk/py4science/examples/lotka_volterra.py 2007-10-28 15:00:36 UTC (rev 4038) @@ -0,0 +1,78 @@ +import numpy as n +import pylab as p +import scipy.integrate as integrate + +def dr(r, f): + return alpha*r - beta*r*f + +def df(r, f): + return gamma*r*f - delta*f + +def derivs(state, t): + """ + Map the state variable [rabbits, foxes] to the derivitives + [deltar, deltaf] at time t + """ + #print t, state + r, f = state # rabbits and foxes + deltar = dr(r, f) # change in rabbits + deltaf = df(r, f) # change in foxes + return deltar, deltaf + +alpha, delta = 1, .25 +beta, gamma = .2, .05 + +# the initial population of rabbits and foxes +r0 = 20 +f0 = 10 + +t = n.arange(0.0, 100, 0.1) + +y0 = [r0, f0] # the initial [rabbits, foxes] state vector +y = integrate.odeint(derivs, y0, t) +r = y[:,0] # extract the rabbits vector +f = y[:,1] # extract the foxes vector + +p.figure() +p.plot(t, r, label='rabbits') +p.plot(t, f, label='foxes') +p.xlabel('time (years)') +p.ylabel('population') +p.title('population trajectories') +p.grid() +p.legend() +p.savefig('lotka_volterra.png', dpi=150) +p.savefig('lotka_volterra.eps') + + +p.figure() +p.plot(r, f) +p.xlabel('rabbits') +p.ylabel('foxes') +p.title('phase plane') + + +# make a direction field plot with quiver +rmax = 1.1 * r.max() +fmax = 1.1 * f.max() +R, F = n.meshgrid(n.arange(-1, rmax), n.arange(-1, fmax)) +dR = dr(R, F) +dF = df(R, F) +p.quiver(R, F, dR, dF) + + +R, F = n.meshgrid(n.arange(-1, rmax, .1), n.arange(-1, fmax, .1)) +dR = dr(R, F) +dF = df(R, F) + +p.contour(R, F, dR, levels=[0], linewidths=3, colors='black') +p.contour(R, F, dF, levels=[0], linewidths=3, colors='black') +p.ylabel('foxes') +p.title('trajectory, direction field and null clines') + +p.savefig('lotka_volterra_pplane.png', dpi=150) +p.savefig('lotka_volterra_pplane.eps') + + +p.show() + Modified: trunk/py4science/examples/qsort.py =================================================================== --- trunk/py4science/examples/qsort.py 2007-10-27 12:36:39 UTC (rev 4037) +++ trunk/py4science/examples/qsort.py 2007-10-28 15:00:36 UTC (rev 4038) @@ -33,5 +33,7 @@ rseq = range(10) random.shuffle(rseq) sseq = qsort(rseq) + print tseq + print sseq self.assertEqual(tseq,sseq) main() Added: trunk/py4science/examples/skel/lotka_volterra_skel.py =================================================================== --- trunk/py4science/examples/skel/lotka_volterra_skel.py (rev 0) +++ trunk/py4science/examples/skel/lotka_volterra_skel.py 2007-10-28 15:00:36 UTC (rev 4038) @@ -0,0 +1,56 @@ +import numpy as n +import pylab as p +import scipy.integrate as integrate + +def dr(r, f): + 'return delta r' + XXX + +def df(r, f): + 'return delta f' + XXX +def derivs(state, t): + """ + Map the state variable [rabbits, foxes] to the derivitives + [deltar, deltaf] at time t + """ + XXX + +alpha, delta = 1, .25 +beta, gamma = .2, .05 + +# the initial population of rabbits and foxes +r0 = 20 +f0 = 10 + +t = XXX # pick a time vector (think about the time scales!) +y0 = [r0, f0] # the initial [rabbits, foxes] state vector +y = XXX # integrate derives over t starting at y0 +r = XXX # extract the rabbits vector +f = XXX # extract the foxes vector + + +# FIGURE 1: rabbits vs time and foxes vs time on the same plot with +# legend and xlabel, ylabel and title + +# FIGURE 2: the phase plane + +# plot r vs f and label the x and y axes +XXX + +# FIGURE 2 continued.... + +# use meshgrid to make a grid over R and F +# with a coarse 1 year sampling. evaluate dR and dF over the 2 s +# grids and make a quiver plot. See pylab.quiver and matplotlib +# examples/quiver_demo.py +XXX + +# FIGURE 2 continued... use contour to compute the null clines over +# dR (the rabbits null cline) and dF (the foxes null cline). You will +# need to do a finer meshgrid for accurate null clins and pass +# levels=[0] to contour +XXX + +p.show() + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |