简体   繁体   中英

differential equations function different depending on time (in R)

I would like to make a differential equation in which a peak in growth of juveniles exists during a specific period of the year. During the rest of the year, there is a background birth rate. I would like to model this with a function in R, but I got stuck. The juveniles become young and the young become adults (which can reproduce new juveniles)

I thought it would be possible using a function in a function, but I get an error...

I now got:

model = function(t, state, parameters) {
with(as.list(c(state, parameters)), {
dJ = function(t, dJ1, dJ2) {
if (t<213) {dJ = rho*A - a*J*A - c*J - d*J*(1+(J/K1))} else {
dJ = r*A - a*J*A - c*J - d*J*(1+(J/K1))}
}
dY = c*J - e*Y * f*Y*(1+Y/K2) - k*Y*A
dA = e*Y - m*A(1+A/K3) 
return(list(c(dJ, dY, dA)))
})
}

run()

But... this does not work. I got the error "Error in lsods(y, times, func, parms, ...): REAL() can only be applied to a 'numeric, not al 'list'"

If you have any ideas how i can create a model like this, please let me know!

Never mind, I already got it! Actually, I was really close! I will post the answer below, for the case that someone else also struggles with it!

The solution that works is:

model = function(t, state, parameters) {
with(as.list(c(state, parameters)), {
dJ = ifelse(t<213, rho*A - a*J*A - c*J - d*J*(1+(J/K1)),
ifelse(t<305, r*A - a*J*A - c*J - d*J*(1+J/K1)),
rho*A - a*J*A - c*J - d*J*(1+(J/K1))))
dY = c*J - e*Y - f*Y*(1+Y/K2)) - k*Y*A
dA = e*Y - m*A*1+(A/K3))

here, rho and r have different values

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