R中的SIR模型:错误的y边界

时间:2014-07-17 17:52:24

标签: r

我使用了SIR model并设置了initial conditions at S= 600, I = 1 and R=0 但是,y值不超过 25 ,整个模型正确,但y轴错误
这是代码:

library ("deSolve")

parms <- c(a=0.05, b=0.1)
inits <- c(S=600, I=1, R=0)
dt <- seq(0,100,0.1) # from 0 to 100 in steps of 0.1


SIR <- function(t,x,parms){
with(as.list(c(parms,x)),{
dS <- -a * S *I
dI <- a * S * I - b * I
dR  <- b * I
der <- c(dS, dI, dR)  # what does it mean?
list(der) # the output must be returned
}) # end of 'with'

}



SIR.out <- data.frame(ode(c(S=S, I=I, R=R), func=SIR, parms = parms, times = dt, hmax=0.01))
matplot(SIR.out[,1], sqrt(SIR.out[,-1]), type="l",col=1, lty =1:3, ylab="sqrt(No. of     Individuals)",xlab = "Years")
legend("right", c("S", "I", "R"), lty=1:3, bty = "n")

任何人都可以帮助我吗? 提前谢谢,Zita

1 个答案:

答案 0 :(得分:0)

您的初始条件设置为c(S=S, I=I, R=R),这将从全局工作区中设置的变量中提取SIR的值(我们无法访问,并且可能与inits中的值不同。我怀疑如果你试试

SIR.out <- data.frame(ode(inits, func=SIR, 
                          parms = parms, 
                          times = dt, hmax=0.01))

(即用inits代替c(S=S,I=I,R=R))你会发现结果与你期望的更好一致。