[R] Difficulty in writing R code for one pool dynamic model

Kebebe Ergano kebeberg @ending from y@hoo@com
Tue May 29 02:02:49 CEST 2018

Hi everyone,

I was trying to mode the following exercise using R.

The question: Set up a one pool model using numericintegration. The model will run from time 1 to time 30 using a time step of 1.The pool (A) will be fed by flux "inA" at a rate of 5 units per hour anddrained by flux "outA" at a rate of 20% per hour. At time 0, A has 5units. At time 30, what is the pool size of A (rounded to 1 decimal)? In theanswer sheet provide the value of pool could be (23.8, 24.8, 25.0 or 24.7). Thevalue I got does not correspond to any of the choices given above.  I don't know where I got it wrong. 

Below is the Ro code I tried to estimate thepool size of A at time 30. 

#Step 1: Specify time 

Tstart <- 1

Tstop <- 30

Tstep <- 1

#Step 2: Set variables corresponding to pools


A <- inA

t <- Tstart

#Step 3: Set up rate parameters


kinA <- 5

koutA <- 0.2

#Step 4: Write arrays that track pool size overtime

val <- array(1, dim=c(1, length(seq(Tstart,Tstop, by=Tstep))))


#Step 5: Set up while loop we use to runintegration


 if(t==Tstart) A <- inA else A <- val[1, t-1]

  # Setup rates

  inA<- kinA*A

  outA<- A*koutA

  A <-A+inA-outA

 val[1,t] <- A

  t <-t+1


# Step 6: Print the output as array

value <- data.frame(val)

# Transpose 'value' using t

value <- t(value)


	[[alternative HTML version deleted]]

More information about the R-help mailing list