[R-sig-dyn-mod] a simulation using base R code

YA x|nx|813 @end|ng |rom 126@com
Sun Jul 12 09:56:00 CEST 2020


Dear list,


I am trying to simulate a system dynamics model with two state variables, stock and resource. I expect that as the resource draining out, the inflow of stock would decrease, then the increase of the stock would be slow down, but still keep increasing the outflow of the resource. So the system would eventually runing out of resource and the curve of the stock would getting flat. However, the simulation result is not consistent with my hypothesis. Could you give me some advice please? I used the base R code without any system dynamics package, is that ok? or do I need to use the function in the package to get the simulation results?


Here is a reproducible code:


stock=1
resource=5
flow.stock=stock*0.02+resource*0.01 # inflow
flow.resource=stock*(-0.05) # outflow
unit=1/8
resource=resource+flow.resource*resource
stock=stock+flow.stock*stock
for (i in 1:length(seq(1,10,by=unit))) stock[i+1]=stock[i]+flow.stock*stock[i]
for (i in 1:length(seq(1,10,by=unit))) resource[i+1]=resource[i]+flow.resource*resource[i]
stock=c(3,stock)
resource=c(5,resource)
dat=data.frame(id=1:length(stock),time=c(0,seq(1,10,by=unit),10+unit),stock,resource)
dat
plot(dat$stock)
lines(dat$stock)
lines(dat$resource)


Thank you very much.


Best regards,


YA
	[[alternative HTML version deleted]]



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