0

I get this error The number of derivatives returned by func() (1) must equal the length of the initial conditions vector (3)

Can some kindly suggest how resolve ?

Thanks

library(deSolve)

# Define the ODE system
diffeq <- function(t, y, params) {
  with(as.list(c(y, params)), {
    dCc <- -k12 * c_central + k21  * c_peripheral - k10 * c_central
    dCp <- k12  * c_central - k21 * c_peripheral
    dy <- -alpha * exp(-beta * y) * y + (c_central - gamma) * (c_central - gamma)
    dw <- g * (t - delay) - kw * w
    dg <- ks * kw * w0 * (w/W0)^(-param) * exp(-u * y) - (ks + v * y) * g
    return(list(dCc, dCp,dy, dw, dg))
  })
}

# Set the initial conditions and parameter values
y0 <- 0
g0 <- 0
w0 <- 100
c_central0 <- 500/5
c_peripheral0 <- 0
params <- c(alpha = 1, beta = 2, gamma = 3, kw = 0.1, k12 = 1.2, 
            k21 = 1.5, k10 = 0.04,ks = 0.01, delay = 0.5, 
            W0 = 100, param = 2, u = 1, v = 2,c_central = c_central0, c_peripheral = c_peripheral0)

# Solve the differential equations using an ODE solver
result <- ode(y = c(y = y0, w = w0, g = g0), times = seq(0, 40, by = 0.1), func = diffeq, parms = params)

# Plot the solution
plot(result[, "time"], result[, "y"], type = "l", xlab = "Time (t)", ylab = "y(t)")
lines(result[, "time"], result[, "w"], col = "red")
lines(result[, "time"], result[, "g"], col = "blue")
tpetzoldt
  • 5,338
  • 2
  • 12
  • 29
Ravua1992
  • 21
  • 4

1 Answers1

0

The code contained essentially two mistakes:

  1. The return value of the model function must be a list, whose first element is a vector containing the derivatives. This means that the derivatives need to be enclosed in c().
  2. The initial state in the ode-call must have the same number of elements as the return value. In the example, c_central and c_peripheral are states, not parameters.

A technically working solution is then shown below. But, I assume that the term (t - delay) may be another mistake as it indicates that the original intention may be a delay differential equation.

library("deSolve")

# Define the ODE system
diffeq <- function(t, y, params) {
  with(as.list(c(y, params)), {
    dCc <- -k12 * c_central + k21  * c_peripheral - k10 * c_central
    dCp <- k12  * c_central - k21 * c_peripheral
    dy <- -alpha * exp(-beta * y) * y + (c_central - gamma) * (c_central - gamma)
    dw <- g * (t - delay) - kw * w
    dg <- ks * kw * w0 * (w/W0)^(-param) * exp(-u * y) - (ks + v * y) * g
    return(list(c(dCc, dCp,dy, dw, dg)))
  })
}

# Set the initial conditions and parameter values
y0 <- 0
g0 <- 0
w0 <- 100
c_central0 <- 500/5
c_peripheral0 <- 0
params <- c(alpha = 1, beta = 2, gamma = 3, kw = 0.1, k12 = 1.2, 
            k21 = 1.5, k10 = 0.04,ks = 0.01, delay = 0.5, 
            W0 = 100, param = 2, u = 1, v = 2)

# Solve the differential equations using an ODE solver
result <- ode(y = c(c_central = c_central0, c_peripheral = c_peripheral0, 
                    y = y0, w = w0, g = g0), 
              times = seq(0, 40, by = 0.1), func = diffeq, parms = params,
              atol = 1e-8)

# Plot the solution
plot(result)
tpetzoldt
  • 5,338
  • 2
  • 12
  • 29