Learn R Programming

simecol (version 0.5-5)

iteration: Discrete Simulation

Description

Solver function to simulate discrete ecological (or other) dynamic models. It is normally called indirectly from sim.

Usage

iteration(y, times=FALSE, func=FALSE, parms=FALSE, animate = FALSE, ...)

Arguments

y
the initial values for the system. If y has a name attribute, the names will be used to label the output matrix.
times
times at which explicit estimates for y are desired. The first value in times must be the initial time.
func
a user-supplied function that computes the values of the next time step (not the derivatives !!!) in the system (the model defininition) at time t. The user-supplied function func must be called as:
parms
vector or list holding the parameters used in func that should be modifiable without rewriting the function.
animate
Animation during the simulation (if available for the specified class.
...
Optional arguments passed to the plot function if animate=TRUE.

Value

  • A list of the model outputs (states ...) for each timestep.

Details

The solver method iteration is used to simulate discrete event models. In contrast to differential equation solvers, the main function of the model must not return the first derivative but instead and explicitly the new state at the specified times. The actual value of time is available in the main function as time and the current increment as parms$DELTAT or parms["DELTAT"] depending on the data type of parms. Normally, this function is run indirectly from sim.

See Also

sim, parms, lsoda, rk4, euler.

Examples

Run this code
data(conway)
## plot after simulation:
plot(sim(conway), delay=100)

## plot during simulation
sim(conway, animate=TRUE, delay=100)

##### discrete version of logistic growth equation
dlogist <- new("odeModel",
main = function (time, init, parms, ...) {
  x <- init
  p <- parms
  x[1] <- x[1] + p["r"] * x[1] * (1 - x[1] / p["K"])
  #       ^^^^ important !!! new value, not derivative
  list(c(x))
},
parms  = c(r=0.1, K=10),
times  = seq(0, 100, 1),
init   = c(population=0.1),
solver = "iteration" #!!!
)

plot(sim(dlogist))

parms(dlogist)["r"] <- 2
plot(sim(dlogist))

Run the code above in your browser using DataLab