In [1]:
start_time = 0;
end_time = 100;

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

# The number of zebras when the simulation is started
initial_zebras = 30000;

# The number of lions when the simulation is started
initial_lions = 15;

# The number of zebras that must be killed for a lion to be born. (Zebras / Lion)

zebras_per_lion = 1000;

# The chance a zebra will die when a zebra lion cross paths
fighting_chance = 0.50;

meeting_chance = 0.02;

zebra_growth_rate = 0.20;

lion_death_rate = 0.10;

# Initialization

zebras_over_time = fill(0.0, end_step+1);
lions_over_time = fill(0.0, end_step+1);
model_time = fill(0.0, end_step+1);

zebras = initial_zebras;
lions = initial_lions;

zebras_over_time[1]	= zebras;
lions_over_time[1] = lions;

In [2]:
# Run the model

for sim_step = 1:end_step

	sim_time = start_time + sim_step * time_step;
	model_time[sim_step] = sim_time;

	# First we must calculate our flows (our rates)
	zebra_births = zebras * zebra_growth_rate;
	zebra_deaths  = min(zebras, meeting_chance*fighting_chance*zebras*lions);

	lion_births = 1/zebras_per_lion * zebra_deaths;
	lion_deaths = lions * lion_death_rate;

	# Update the stock levels
	zebras = zebras + zebra_births - zebra_deaths;
	lions = lions + lion_births - lion_deaths

	# Stock values always update in the next time step
	zebras_over_time[sim_step+1] = zebras;
	lions_over_time[sim_step+1] = lions;
end

In [16]:
using Gadfly
using DataFrames

In [39]:
d = DataFrame(ID = 1:size(d, 1), T = model_time, L=lions_over_time, Z=zebras_over_time);
d[:ID] = 1:size(d, 1)
[head(d),tail(d)]


Out[39]:
TLZid
10.2515.030000.01
20.518.031500.02
30.7521.8732130.03
41.026.70983131529.1694
51.2532.4602356556043929413.615044395615
61.538.76194084828707525748.609295031616
799.00.020858287929950391.7362620572629801e-6396
899.250.0187724591373175062.0831523141764537e-6397
999.50.0168952132239768152.4993917180947974e-6398
1099.750.0152056919020014112.9988477841536824e-6399
11100.00.0136851227122572663.59816134542975e-6400
120.00.0123166104415239534.317301201720193e-6401

In [38]:
ds = stack(d, [:Z, :L], :T);
[head(ds),tail(ds)]


Out[38]:
variablevalueT
1Z30000.00.25
2Z31500.00.5
3Z32130.00.75
4Z31529.1691.0
5Z29413.615044395611.25
6Z25748.609295031611.5
7L0.0208582879299503999.0
8L0.01877245913731750699.25
9L0.01689521322397681599.5
10L0.01520569190200141199.75
11L0.013685122712257266100.0
12L0.0123166104415239530.0

In [ ]: