-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdSolveWorkshop.R
More file actions
48 lines (40 loc) · 960 Bytes
/
dSolveWorkshop.R
File metadata and controls
48 lines (40 loc) · 960 Bytes
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
#Analyze the trajectory of a beneficial allele among active and dormant populations
#dPN/dt=s*p*(1-p) - (mNM * PN) + (mMN * PM)
#dPM/dt= (mNM * PN) - (mMN * PM)
#Clear working environment
rm(list=ls())
#Open deSolve library
library(deSolve)
s = 0.01
mNM = 0.001
mMN = 0.001
N = 100000
#Define system of differential equations
evolution <- function(t,y,params){
pN = y[1]; pM = y[2];
with(
as.list(params),
{
dpN <- s*pN*(1-pN) - (mNM * pN) + (mMN * pM)
dpM <- (mNM * pN) - (mMN * pM)
res <- c(dpN,dpM)
list(res)
}
)
}
#Pick times vector to integrate along
times=seq(from=0,to=100,by=1)
#Simulate
evolution_out=rk(
#Vector of initial conditions
c(pN=1/N,pM=0),
#Time vector to use
times,
#Differential equation system to use
evolution,
#Vector of parameters to use
c(s=s,mNM=mNM,mMN=mMN),
#Method to use
method="rk45dp7")
#Plot the data lazily and let R do all the work
plot(evolution_out)