UserPreferences

PopulationGrowthExampleCode


  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
from Tkinter import *

MAXTIME = 50
INIT_GROWTHRATE = 0.3
MAXY = 500
CARRYING_CAPACITY = 200

# --------------------------------------------
# GRAPHICS STUFF
win = Tk()
can = Canvas(win, width = 600, height = 400, background = "white")
yax = can.create_line(20,20,20,380, width=2,fill='black')
xax = can.create_line(20,380,580,380, width=2,fill='black')
for i in range(0,MAXY,100):
        can.create_text(20,380-((float(i)/MAXY)*380),text=str(i))
for i in range(0,MAXTIME,50):
        can.create_text(20+(float(i)/float(MAXTIME))*580, 390,text=str(i))
can.pack()

def graph_it(prev,t, v):
        x = ((float(t) / float(MAXTIME)) * 580) + 20
        y = 380 - ((float(v) / MAXY)*380 + 20)
        can.create_rectangle(x-1,y-1,x+2,y+2,width=0,fill='blue')
        can.create_line(prev[0],prev[1],x,y,width=1,fill='blue')
        can.pack()
        return [x,y]

# GRAPHICS STUFF
# --------------------------------------------

def growthrate(p):
        return (-INIT_GROWTHRATE/CARRYING_CAPACITY)*popsize + INIT_GROWTHRATE

popsize = 5.0
prev = [20,380 - ((popsize / MAXY)*380 + 20)]
for t in range(0,MAXTIME):
        popsize += popsize * growthrate(popsize)
        prev = graph_it(prev,t, popsize)

win.mainloop()