不能在微分方程

时间:2017-05-16 12:00:28

标签: r ode differential-equations

大家好我一直在努力让这个ODE工作但我不断遇到这个错误:eval中的错误(expr,envir,enclos):找不到对象'j'

我的代码在下面,似乎是我在ODE中的if语句的问题

parameters <- c(
                a = 0.032,
                b = (9 / 140),
                c = (5 / 1400),
                d = (95 / 700),
                k = 1 / 140,
                i = 0.25,
                # r = 0.2,
                n = 6000000,
                x = 0.3 ,
                t = 1 / 180,        # important in looking at the shape
                u = 1 / 180,        # important in looking at the shape
                v = 1 / 360,        # important in looking at the shape
                p = 10,
                s = 10000,
                g = 100

                # e = .4,
                #h = 1000
 )

 state <- c(
            S = 5989900,
            E = 100,
            I = 100,
            Q = 100,
            D = 100,
            B = 100,
            C = 100,
            Y = 100,
            H = 1000,
            R = 1000,
            J = 1000,
            h = 100,
            e = 0.1,
            r = 0.1
 )


 # set up the equations

equation <- (function(t, state, parameters)
  with(as.list(c(state, parameters)), {
    # rate of change

    dS <- (-(a * S * I) / n) - (((1 / r) * S * D) / n)
    dE <- (a * S * I) / n + (((1 / r) * S * D) / n) - i * E
    if (h > Q)
      j = 1
    else if (h < Q) 
      j = 0
    dI <- i * (j) * E - (e) * I - c * I - d * I
    dQ <- (j) * (e) * I - b * Q - k * Q
    dD <- d * I - r * D
    dB <- b * Q + r * D
    dC <- c * I + k * Q

    dY <- p * (b * Q + r * D)
    dR <- (1 - x) * (p * (b * Q + r * D))  - t * (R)
    de <- t * (s / R)
    dJ <- (x) * (p * (b * Q + r * D))  - v * (J)
    dr <- v * (s / J)
    dH <- (x) * (p * (b * Q + r * D)) - u * (H)
    dh <- u * (H / g)


    # return the rate of change
    list(c(dS, dE, dI, dQ, dD, dB, dC, dY, dR, de, dJ, dr, dH, dh))
  }))
#

# solve the equations for certain starting parameters


library(deSolve)
times <- seq(0, 200, by = 1)

out <-
  ode(y = state,
    times = times,
    func = equation,
    parms = parameters
  )
# , method = "vode"
head(out)
tail(out)

# graph the results

par(oma = c(0, 0, 3, 0))
plot(out, xlab = "Time", ylab = "People")
#plot(out[, "X"], out[, "Z"], pch = ".")
mtext(outer = TRUE, side = 3, "Ebola Model",cex = 1.5
)

任何帮助都会很棒!

1 个答案:

答案 0 :(得分:2)

如果不会创建h==Q变量j。 在给定示例中,h等于Q。 您应该在else语句之前添加j语句或将基值分配给if

像这样:

j = 0

if (h > Q){
  j = 1
}
else if (h < Q) {
  j = 0
}

if (h > Q){
  j = 1
}else if (h < Q) {
  j = 0
}else{
j = 0
}