-
Notifications
You must be signed in to change notification settings - Fork 1
/
meantime.m
102 lines (73 loc) · 2.79 KB
/
meantime.m
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
%meantime simulates the model over multiple trials and records the time of
%extinction of each, if birds survive and return to equilibrium the time
%will be infinite and hence a maximum constraint is set, currently it is
%100 years. This will output the averages times of each bird populations
%survival given a beta_R.
clear all
close all
m=30; % number of beta_R tested
n=50; % number of trials for each beta_R
N = 1000; % number of nests available
outcome = zeros(m,n);
X_outcome = [];
f1 = figure;
meanval = zeros(m);
for i = 1:m
for j = 1:n % Number of trials for each rate
% parameters
b_born = 0.6; % beta_B
b_death = 2/7; % 1/expected life (3.5 years)
r_born = 0.1*i; % beta_R
r_death = 0.5; % 1/expected life (2 years)
% initial conditions.
X = [500; 10]; % X(1) is bird pop, X(2) is rat pop
t = 0;
a = zeros(4,1);
% initialise
X_out = X;
t_out = 0;
T = 50; % maximum time allowed for the simulation
while X_out(1,end) > 0
% step 1. Calculate the rates of each event given the current state.
a(1) = r_born*X(1)*X(2)/N; % rate at which rat eats bird
a(2) = b_born*X(1)*(N-X(1))/N; % rate at which bird born
a(3) = r_death*X(2); % rate at which rat dies
a(4) = b_death*X(1); % rate at which bird dies
if t > T % if time restriction is broken break the loop
break
end
a0 = a(1)+a(2)+a(3)+a(4); % total rate of events
% step 2. Calculate the time to the next event.
t = t - log(rand)/a0;
% step 3. Update the state.
r = rand*a0;
if r < a(1)
% rat eats bird
X(1) = X(1) - 1;
X(2) = X(2) + 6;
elseif r < a(1)+ a(2)
% bird is born
X(1) = X(1) + 1;
elseif r < a(1)+a(2)+a(3)
% rat dies
X(2) = X(2) - 1;
else
% bird dies
X(1) = X(1) - 1;
end
% record the time and state after each jump
X_out = [X_out, X];
t_out = [t_out, t];
end
outcome(i,j) = t;
end
end
for k = 1:30
meanval(k) = mean(outcome(k,:)); % averaging the times of each trial
end
rates=0.1:0.1:3;
hold on
plot(rates,meanval)
xlabel('\beta_{R}')
ylabel('time (years)')
hold off