-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation of model1-4 v(t).R
159 lines (126 loc) · 4.96 KB
/
simulation of model1-4 v(t).R
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# This is to check that my solutions to the ODEs for models 1,2,3 are correct
# 20 Oct 2011
# E(N(t)) is expected number of individuals left in the stream
# V(t) is the variance in the number left
# ODEs for dE(N)/dt and dV/dt:
# dE(N)/dt = f(E(N))
# dV/dt = -2f'(N)V + f(N)
# from Carl Boettiger
# model1 and my analytical solutions:
# dN/dt = -mN
# N(t) = N0*exp(-mt)
# dV/dt = 2mV(t) -mN(t)
# V(t) = exp(2mt) * (V0 + N0 * (exp(-3 * m * t) - 1) / 3)
# model2 and my analytical solutions:
# dN/dt = -mN + bFN
# N(t) = N0*exp(t(bF-m))
# dV/dt = -2(bF - m)V(t) - mN(t) + bfN(t)
# V(t) = exp(2t(m-bF)) * (V0 + N0(exp(3t(bf-m))-1)/3)
# model3 and my analytical solutions:
# dN/dt = -mN / (c + F)
# N(t) = N0*exp(-mt / (c + F))
# dV/dt = 2mV(t) / (c + F) - mN(t) / (c + F)
# V(t) = exp(2mt / (c + F)) * (V0 + N0 * (exp(-3*m*t/(c +F)) - 1)/3)
# model4, analytical solution for N, see odt file for analytical solution of V:
# dN/dt = -mN - aN^2
# N(t) = m / ((m/N0 + a) * exp(t * m) - a)
# dV/dt = (2*m + 4 * a * N) * V - m * N - a * N^2
# original, incorrect solution:
# V(t) = (a * N0 - exp(t*m)*(a*N0 + m))^4 * exp(- 2 * t * m) *
# (k - m*N0 * (a^2*N0^2 - 5*a*N0*exp(t*m)*(a*N0 + m) + 10 * exp(2*t*m) * (a*N0 + m)^2) /
# 30*(a*N0 + m)^2 * (exp(t*m)*(a*N0 + m) - a*N0)^5)
# initial number and parameters:
N0 = 113
V0 = N0/3 # not sure what V0 should be!
k = 100 #V0 + N0/3 # no idea what k from model4 should be
m = 0.15 # emigration rate
t = seq(0,10, by=0.1) # timesteps
f = 1 # food abundance, constant
b = 0 # all models should be identical to model1 when b=0, c=0, a=0
c = 0
a = 0
C = 100
# analytical solutions for N(t) and V(t) for all models:
Nt1 = N0 * exp(-m * t)
Vt1 = exp(2*m*t) * (N0*exp(-3*m*t)/3 + (V0 - N0/3))
Nt2 = N0 * exp(t*(b*f - m))
Vt2 = exp(2*t*(m-b*f)) * (V0 + N0 * (exp(3 * t * (b*f - m)) - 1)/3)
Nt3 = N0 * exp(-m * t / (c + f))
Vt3 = exp(2 * m * t / (c + f)) * (V0 + N0 * (exp(-3*m*t/(c + f)) - 1)/3)
Nt4 = m / ((m/N0 + a) * exp(t * m) - a)
Vt4 = ( ( ( (-(m^2) / a) * (1-4*C*exp(m*t)) ) / (12 * C^2 * m * (C * exp(m*t) -1)^4) ) -
( ( ((2 * m^2) / a) * (1-5*C*exp(m*t)) ) / (20 * C^2 * m * (C * exp(m*t) -1)^5) )
+ C ) /
( exp(2 * t * m) / ((C * exp(t * m) -1)^4) )
# OLD, WRONG: Vt4 = (a * N0 - exp(t*m)*(a*N0 + m))^4 * exp(- 2 * t * m) *
#(k - m*N0 * (a^2*N0^2 - 5*a*N0*exp(t*m)*(a*N0 + m) + 10 * exp(2*t*m) * (a*N0 + m)^2) /
#30*(a*N0 + m)^2 * (exp(t*m)*(a*N0 + m) - a*N0)^5)
# plot and compare all analytical results
par(mfrow=c(1,2))
plot(Nt1 ~ t, type='l', lty=2) # model1 = black
points(Nt2 ~ t, type='l', lty=3, col=2) # model2 = red
points(Nt3 ~ t, type='l', lty=4, col=4) # model3 = blue
points(Nt4 ~ t, type='l', lty=5, col=6, lwd=5) # model4 = purple
plot(Vt1 ~ t, type='l', lty=2) # model1 = black
points(Vt2 ~ t, type='l', lty=3, col=2) # model2 = red
points(Vt3 ~ t, type='l', lty=4, col=4) # model3 = blue
points(Vt4 ~ t, type='l', lty=5, col=6, lwd=5) # model4 = purple
# Numerically simulate ODEs to test analytical solutions:
require(deSolve)
parms = c(N0 = N0, V0 = V0, m = m, f=f, c=c, b=b)
start = c(N = N0, V = V0)
model1 = function(t, x, parms) {
with( as.list( c(parms, x)), {
dn.dt = -m*N # dN/dt
dv.dt = 2 * m * V - m * N # dV/dt
res = c(dn.dt, dv.dt)
list(res)
})
}
out.model1 = as.data.frame(lsoda(start, times=t, model1, parms))
model2 = function(t, x, parms) {
with( as.list( c(parms, x)), {
dn.dt = -m*N + b*f*N # dN/dt
dv.dt = -2*(b*f - m)*V - m*N + b*f*N # dV/dt
res = c(dn.dt, dv.dt)
list(res)
})
}
out.model2 = as.data.frame(lsoda(start, times=t, model2, parms))
model3 = function(t, x, parms) {
with( as.list( c(parms, x)), {
dn.dt = -m*N / (c + f) # dN/dt
dv.dt = 2*m*V / (c + f) - m*N / (c + f) # dV/dt
res = c(dn.dt, dv.dt)
list(res)
})
}
out.model3 = as.data.frame(lsoda(start, times=t, model3, parms))
model4 = function(t, x, parms) {
with( as.list( c(parms, x)), {
dn.dt = -m*N - a*N^2 # dN/dt
dv.dt = -2 * (- m - 2 * a * N) * V + (- m*N - a * N^2) # dV/dt
res = c(dn.dt, dv.dt)
list(res)
})
}
out.model4 = as.data.frame(lsoda(start, times=t, model4, parms))
# graphically compare numerical and analytical results
par(mfrow=c(4,2))
plot(Nt1 ~ t, type='l', lty=2, col='blue', main='Blue = Analytical, Red = Numerical')
points(out.model1$N ~ t, type='l', lty=4, col='red')
plot(Vt1 ~ t, type='l', lty=2, col='blue')
points(out.model1$V ~ t, type='l', lty=4, col='red')
plot(Nt2 ~ t, type='l', lty=2, col='blue')
points(out.model2$N ~ t, type='l', lty=4, col='red')
plot(Vt2 ~ t, type='l', lty=2, col='blue')
points(out.model2$V ~ t, type='l', lty=4, col='red')
plot(Nt3 ~ t, type='l', lty=2, col='blue')
points(out.model3$N ~ t, type='l', lty=4, col='red')
plot(Vt3 ~ t, type='l', lty=2, col='blue')
points(out.model3$V ~ t, type='l', lty=4, col='red')
plot(Nt4 ~ t, type='l', lty=2, col='blue')
points(out.model4$N ~ t, type='l', lty=4, col='red')
#plot(Vt4 ~ t, type='l', lty=2, col='blue',
#ylim=c(min(c(Vt4,out.model4$V)), max(c(Vt4,out.model4$V))))
plot(out.model4$V ~ t, type='l', lty=4, col='red')