Dynamic Systems
A dynamic systems changes over time, a function relates to time.
Exponential growth
A simple example is that of exponential growth, applicable to bacterial growth in a test tube.
Assumption:
Birth rate:
Death rate:
Growth rate:
Population size:
Because both birth rate and death rate are fixed, so growth rate is also fixed.
Solution:
Here we suppose .
t = seq(0, 10, 0.01)
r1 = 0.5
r2 = -1
n0 = 1
np = n0*exp(r1*t)
nn = n0*exp(r2*t)
plot(t,np,type = 'l',xlab = 'time', ylab = 'n', ylim = c(0, 10))
lines(t,nn,col = 'red')
legend("topleft", c("r>0","r<0"), lty=c(1, 1), col=c("red", "black"))
Conclusion:
So when r>0, n increases, otherwise, decreases.
Differential calculus in R
library(deSolve)
expGrowth = function(t, n, r){
dndt = r*n
return(list(dndt)) # the output has to be a list
}
timevec = seq(0, 20, 0.1)
r =0.1
n0 = 1
# call the ode function to solve the differential equation
out = ode(y = n0, func = expGrowth, times = timevec, parms = r)
plot(out, main = "Exponential growth")
Logistic growth
Population can not go on forever. The per capita growth rate r will decrease with population size n.
Assumption:
Logistic equation:
Using R:
par(mfrow=c(1,2))
n = seq(0, 10, 0.1)
K = 8
r0 = 1
r = r0*(1-n/K)
plot(n, r, type = 'l', col= 'blue')
abline(h = c(0))
plot(n, r*n, type = 'l', col= 'blue', ylab = 'dn/dt')
abline(h = c(0))
par(mfrow=c(1,1))
# defferential
# More than one parameter: use a list
library(deSolve)
logisticGrowth = function(t, n, P){
dndt = P$r0 * n * (1- n/P$K)
return(list(dndt))
}
timeVec = seq(0, 20, 0.1)
P = list(r0 = 1, K = 100)
n0 = 1
out = ode(y = n0, func = logisticGrowth, times = timeVec, parms = P)
plot(out, main = 'Logistic growth')
Stability:
According to previous conclusion and the equation 5, when n>K, N increases, n<K, N decreases and when n=K, the population size is stable.
And from mathematic aspect:
The derivative of population size N can show the growth trend, according the negative and positive.
Conclusion:
An equilibrium point is K, which means the system will be stable when started approaching K.
General condition for stability:
- Find the equilibrium point:
- Whether the equilibrium n* is stable:
- Return time:
- The population go extinct:
Exercise
…
Multidimensional systems
The Lotka-Volterra competition model
= competition coefficient = How much population 2 competes with population 1.
Competition coefficient are usually, but not always, below 1.
Isoclines
Calculate the system equilibrium
Lotka-Volterra predator-prey model
Assumption:
rn: Prey is exponential growth
a: Prey caught per time unit
c: Converting prey to predators
: Background mortality of predators
When p = r/a, the population size of prey is stable. When predator(p>r/a) increases, prey decreases, vice versa. So the stability of prey is decided by the number of predator.
Stability
Jacobean matrix:
If all eigenvalues(特征值) of the Jacobean matrix have a negative real part, the equilibrium is stable.
**Equilibrium: **
Jacobean:
Eigencalues of 2*2 matrices:
Exercise
…
Stochastic systems
A stochastic system has an uncertain outcome, it can not be perfectly predicted beforehand. A single ‘run’ of a stochastic system is called a realization.
Stochastic models
A random walk
n = 1000 # the length of the walk
x = seq(0, n)
# get an empty plot
plot(NA, type = 'n', xlim = c(0, n), ylim = c(-2*sqrt(n), 2*sqrt(n)), xlab = 'steps', ylab = 'distance')
for(iter in 1:100){ # repeat 100 times
# walk randomly
for(i in 1:(n-1)){
x[i+1] = x[i] + rnorm(1)
}
lines(x, col = 'black')
}
A queue
A model of a standard queue. Every minutes, a new customer is added to the queue with probability Pin. Also every minutes, a customer is finished with probability pout and leaves the queue.
runQ <- function(Pin, Pout, T_max) {
q_length <- 0 # number of people in line
q_stored <- rep(0,T_max)
for (t_q in 1:T_max) {
# every minute a person is added to the q
# with probability Pin
if(runif(1) < Pin){
# add a person to the queue
q_length <- q_length + 1
}
# every minute a person leaves
# with probability Pout
if(runif(1) < Pout ) {
if(q_length>0) {
# remove a person from the queue
q_length <- q_length - 1
}
}
q_stored[t_q] <- q_length
}
plot(1:T_max, q_stored, xlab='time (min)', ylab='q length', type='l')
}
runQ(0.6, 0.5, 100)
The Moran process
The Moran process is a model in population genetics. It is a model of the spread of an allele in a population. A population has N individuals of type A or B. Each time-step, one random individual is chosen to die. It is replaced by the copy of another randomly chosen individual. The process continues until A or B is fixed.
runMoran <- function(tmax, N) {
# N is total population size
# a function runMoran that simulates
# the Moran process tmax time-steps,
# population vector
# each element represents one individual
# 0 : B, wild type
# 1 : A, mutant type
population <- rep(0,N)
# starting with a single copy of A
population[1] <- 1
for (t in 1:tmax) {
#Each time-step,
# one random individual is chosen to die.
# #It is replaced by the copy
# # of another randomly chosen individual.
# a vector of two random individuals:
dead_lucky <- sample.int(N,2)
dead <- dead_lucky[1]
lucky <- dead_lucky[2]
population[dead] <- population[lucky]
# if B fixed, stop:
if(sum(population)==0 ) {
break
}
# if A fixed, stop:
if(sum(population)==N) {
break
}
}
# and returns the final number of A:s.
return(sum(population))
}
Discrete time systems
conclusion
Equilibrium:
Stability:
The Ricker model
来源:CSDN
作者:Chix1996
链接:https://blog.csdn.net/qq_43063824/article/details/103786805