[R-sig-dyn-mod] ode with variable dependent on results from previous time step

Thomas Petzoldt thomas.petzoldt at tu-dresden.de
Mon Feb 27 15:13:04 CET 2017


Hi,

I don't yet completely understand what you want, but maybe the following 
can help you a little bit.

1) your code is not completely correct, as parms$c does not contain the 
interaction matrix. Add a print to your function to see this:

     c <- params$c
     print(c)


2) using for-loops in a model function is quite unusual and, together 
with calculating sums a strong indication to me, that you should 
consider matrix multiplication, see page 124 vs. 123 in our tutorial:

http://desolve.r-forge.r-project.org/user2014/tutorial.pdf#124

That example shows a multi-species Lotka-Volterra model. Could it be 
possible that you want something like that?

3) I wonder also, why you use c*P[i] and not c[i,j] and why all 
interactions are positive.

4) Finally: in ODE models, "time steps" are infinitesimally small (i.e. 
close to zero). This means that interaction happens instantaneously or 
with other words: all variables are a result from previous time. Time is 
continuous, so time "step" and "step before" do not make much sense -- 
except the technical fact that the solver approximates continuous time 
with finite steps; or "time delay" for delay differential equations, but 
this would be another story.

Regards,

Thomas P.


Am 27.02.2017 um 05:02 schrieb Guadalupe Peralta:
> funcPA <- function (t, y, params, nsp) {
>   with(as.list(c(y,params)), {
>     nP <- nsp[1]
>     nA <- nsp[2]
>     ntot <- nP + nA
>     P <- y[1:nP]
>     A <- y[(nP + 1):ntot]
>     dP <- array(0, nP)
>     dA <- array(0, nA)
>     c <- params$c
>     for (i in 1:nP) {
>       for (j in 1:nA){
>         dP[i] <- P[i] + sum(c*A[j]*P[i])
>         dP
>         dA[j] <- A[j] + sum(c*P[i]*A[j])
>         dA }}
>     res <-list(c(dP, dA))
>   })
> }
>
> nsp <- c(nP=3, nA=2)
> c <- list()
> c[[1]] <- matrix(c(0.1,0.11,0.12,0,0,0.1),nrow=3, ncol=2)
> y <- c(P1=2, P2=1, P3=2, A1=1, A2=2)
>
> ode(y=y, times=seq(0,1,by=0.1), func=funcPA, nsp=nsp, parms=c)



More information about the R-sig-dynamic-models mailing list