简体   繁体   中英

R-package deSolve: Error in checkFunc

I'm fairly new with R and I'm trying to solve a system of differential equations with de deSolve package.

I have a total of 10000 timesteps and every 100 timesteps i have to change the values of the variables. The code i tried gives me following error:

Error in checkFunc(Func2, times, y, rho) : 
  The number of derivatives returned by func() (2) must equal the length of the initial conditions vector (3) 

I'm not quite sure where the error comes from. Here is my code so far:

library(deSolve)

#--------variables and parameter--------------------------


parameters <- c(up = 0.0001, hm = 0.1, hp = 0,889, mm = 0.1 , nt = 100)
yini <- c(u = 1, m = 0.001, h = 0.001)
times <- seq(0,100, by=0.1)
out<- NULL

#--------functions---------------------------------------
DiffU <- function(t, yini, parameters){
  with(as.list(c(yini, parameters)),{

    #functions
    du <- -(up*u) + hm*h + (1/2*nt)*h
    dm <- hp*h - mm*m - (1/nt)*m

    # return functions
    list(c(du, dm))
  })
}

#---------------------------------------------
repeat{
  out<- rbind(out, ode(yini, times, DiffU, parameters))
  yini<-c(u = u+0.5*h, m = 0.001, h = m+0.5*h)
  x<- x+1
  if (x==100){
    break
  }
}

Do you have any suggestions to get rid of the error or to improve the code? Thank you very much!

Your problem is, that you have two derivatives 'du' and 'dm', but three values for yini ('u', 'm' and 'h'). This causes the error.

One solution is (1) add a third derivative or (2) remove 'h' from yini and add it to parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM