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

David Winsemius dw|n@em|u@ @end|ng |rom comc@@t@net
Tue May 29 02:26:47 CEST 2018



> On May 28, 2018, at 5:02 PM, Kebebe Ergano via R-help <r-help using r-project.org> wrote:
> 
> 
> 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. 
> 

Homework is not on-topic for r-help. Nevertheless you might try using a small time increment if you are being asked to do numeric integration. You also might look up the many worked examples of compound interest.

David.


> 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
> 
> 
> inA<-5
> 
> A <- inA
> 
> t <- Tstart
> 
> #Step 3: Set up rate parameters
> 
> 
> A<-5
> 
> 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))))
> 
> val
> 
> #Step 5: Set up while loop we use to runintegration
> 
> 
>  while(t<=Tstop){
> 
>  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)
> 
> value
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list