ntimes = 50
H = numeric(ntimes) # Host
P = numeric(ntimes) # Parasitoid
H = .4; P = 2.0 # initial values for populations H and P
for (i in 2:ntimes) {
H[i+1] = (8.7*H[i]*exp^(-0.4*P[i]))/(1+(4.7*H[i]))
P[i+1] = (5.5*H[i])*(1-exp^(-0.4*P[i]))
}
exp ^( - 0.4 * P [i])中的错误:二元运算符的非数字参数 The model equation I am tryinng to create 
无法弄清楚为什么我会收到此错误。这是Nicholson Bailey模型改编的模型。这是我使用的初始代码,Nicholson Bailey模型。
ntimes = 25
H = numeric(ntimes) # Host
P = numeric(ntimes) # Parasitoid
H = 350; P = 150 # initial values for populations H and P
a = .004; c = 1; r = 1.75 # the parameter values
for (i in 2:ntimes) {
H[i] = r * H[i-1] * exp(-a * P[i-1])
P[i] = c * H[i-1] * (1 - exp(-a * P[i-1]))
}
par(mfrow = c(1,2)) # 1 x 2 graphics window
plot(1:ntimes, H, type = "n", las = 1,
xlim = c(0,25), ylim = c(0,1100),
ylab = "Number of Individuals",
xlab = "Time",cex.lab = 1.5)
leg.txt = c("Host","Parasitoid")
legend("topright",leg.txt,lwd=2,lty = c(1,2))
lines(1:ntimes,H,lwd=2,lty=1)
lines(1:ntimes,P,lwd=2, lty=2)
plot(H, P, las = 2,
xlim = c(0,800), ylim = c(0,1100),
ylab = "Number of Parasitoids", xlab = "Number of Hosts",
cex.lab = 1.5,type = "l",lwd = 2)
答案 0 :(得分:0)
这似乎有效:
纠正代码中的小错别字:
ntimes = 50
H = numeric(ntimes) # Host
P = numeric(ntimes) # Parasitoid
H[1] = .4; P[1] = 2.0 # initial values for populations H and P
for (i in 1:(ntimes-1)) {
H[i+1] = (8.7*H[i]*exp(-0.4*P[i]))/(1+(4.7*H[i]))
P[i+1] = (5.5*H[i])*(1-exp(-0.4*P[i]))
}
绘图,删除旧的xlim / ylim值并切换到type="b"
,pch=16
:
plot(H, P, las = 1,
xlim=c(0,1), ylim=c(1,3),
ylab = "Number of Parasitoids", xlab = "Number of Hosts",
cex.lab = 1.5,type = "b", pch=16, lwd = 2)