我直观地与循环作斗争。我有一个简单的消费者资源模型,我想遍历资源增长率的值g
以获得最终状态值,然后绘制平衡作为参数值的函数。这是我到目前为止所拥有的:
param.values = seq(from = 1, to = 10, by = 1)
variable = rep(0,length(param.values))
for (i in 1:length(param.values)){
state <- c(r = 1, n = 1)
parameters = c(g = variable[i],# resource growth rate
d = 0.5, # n mortality rate
k = 5, # r carrying capacity
c = 1, # consumption rate of n on r
e = 1, # conversion efficiency for n on r
h = 1 # handling time n on r
)
function1 <- function(times, state, parameters) {
with(as.list(c(state, parameters)),{
# rate of change
dr = variable[i]*r*(1 - (r/k)) - (c*n*r/(1+(h*c*r)))
dn = (e*c*n*r/(1+(h*c*r)))- n*d
# return the rate of change
list(c(dr, dn))
})
}
times <- seq(0, 100, by = 1)
out <- ode(y = state, times = times, func = function1, parms = parameters)
sol <- out[101, 2:3] # trying to get last equilibrium value to plot against param values...
print(sol[i])
}
plot(sol[,1] ~ param.values)
plot(sol[,2] ~ param.values)
我想我一直在思考 ode 函数 - 在此之后我应该在哪里建立索引i
?我希望这是有道理的。