In [34]:
# This code and all tutorial code can be found on https://github.com/forio/julia-tutorials

#Time parameters
start_time = 0
end_time = 10000

#How much time passes between each successive calculation
time_step = 1/4 # years
end_step = int(((end_time-start_time)/time_step))

initial_rabbits = 30000 #The number of rabbits when the simulation is started. (Rabbits)
initial_foxes = 15 #The number of foxes when the simulation is started (Foxes)
rabbits_killed_per_fox_birth = 1000 #The number of rabbits that must be killed for a fox to be born. (Rabbits/Fox)
chance_a_rabbit_will_die_during_a_meeting = 0.50 #The chance a rabbit will die when a rabbit fox cross paths. (dmnl)
chance_of_rabbit_and_fox_meeting = 0.02 #The chance that a rabbit and fox will cross paths. (dmnl)
rabbit_growth_rate = 0.20 # The percent of the rabbit population that will be born this time step. (1/Year)
fox_death_rate = 0.10 #The percent of the fox population that will die this time step from old age. (1/Year)

rabbits_over_time = fill(0.0, end_step+1)
foxes_over_time = fill(0.0, end_step+1)
model_time = fill(0.0, end_step+1)

rabbits = initial_rabbits
foxes = initial_foxes

rabbits_over_time[1] = rabbits
foxes_over_time[1] = foxes


Out[34]:
15

In [35]:
#Run the model
for sim_step = 1:end_step
    # Get the time from the step
    sim_time = start_time + sim_step * time_step
    model_time[sim_step] = sim_time

    #first we must calculate our flows (our rates)    
    rabbit_births = rabbits * rabbit_growth_rate       
    rabbits_eaten = min(rabbits, chance_a_rabbit_will_die_during_a_meeting * chance_of_rabbit_and_fox_meeting * foxes * rabbits)
    
    fox_births = 1/rabbits_killed_per_fox_birth * rabbits_eaten 
    fox_deaths = foxes * fox_death_rate

    #then we update our stocks
    foxes = foxes + fox_births - fox_deaths
    rabbits = rabbits + rabbit_births - rabbits_eaten
    
    #stock values always update in the next time step
    rabbits_over_time[sim_step+1] = rabbits
    foxes_over_time[sim_step+1] = foxes
end

In [30]:
using PyPlot
plot(model_time, rabbits_over_time/1000, color="green", linewidth=1.5, linestyle="-")
plot(model_time, foxes_over_time, color="red", linewidth=1.5, linestyle="-")
ylabel("Population")
xlabel("Time")
title("Rabbits vs. Foxes")


Out[30]:
PyObject <matplotlib.text.Text object at 0xa599850>

In [49]:
transform=fftshift(fft(foxes_over_time))
dt = time_step;
Fs = 1/dt;
df = Fs/length(transform);
freq = -Fs/2+df:df:Fs/2;

plot(freq, transform)


Out[49]:
1-element Array{Any,1}:
 PyObject <matplotlib.lines.Line2D object at 0x83df390>

In [38]:


In [ ]: