Description Usage Arguments Details Value See Also Examples

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

.

1 |

`y ` |
the initial values for the system. If |

`times ` |
times at which explicit estimates for |

`func ` |
a user-supplied function that computes the values of the
The return value of func should be a list, whose first element is a
vector containing the derivatives of |

`parms ` |
vector or list holding the parameters used in |

`animate` |
Animation during the simulation (if available for the specified class. |

`...` |
optional arguments passed to the |

The solver method `iteration`

is used to simulate discrete event
models. Normally, this function is run indirectly from
`sim`

.

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`

. It is element of a vector if `parms`

is a
vector and it is a list if `parms`

is a list.

If iteration is used for difference equations (see example
`dlogist`

below), it is mandatory to multiply the incremental
part with `DELTAT`

to ensure that variable time steps are
correctly respected and that the first row of the simulation outputs
contains the states at *t_0*.

The default `iteration`

method of class `simObj`

supports
the `observer`

mechanism. This means that a function stored in
slot `observer`

is called during each iteration step with the
return value of `main`

as its first argument. You can use this to
control the amount of data stored during each iteration step
(e.g. whole population or only mean values for individual based
models), to do run-time animation or to write log files.

As an alternative for models of class `odeModel`

, the
`iteration`

method of package deSolve may be used as a
user-defined solver function. This is slightly faster and the output
supports the extended plotting functions, but then no observers are
possible and no implicit `DELTAT`

variable.

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

`sim`

,
`observer`

,
`parms`

,
`lsoda`

, `rk4`

,
`euler`

, `iteration`

.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | ```
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
## Note: function main returns the *new value*, not the derivative
dlogist <- new("odeModel",
main = function (time, init, parms, ...) {
x <- init
with(as.list(parms), {
x <- x + r * x * (1 - x / K) * DELTAT
# ^^^ add to old value ^^^^^^ special parameter with time step
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))
## alternative with function that returns the derivative
## discrete steps are realized with the euler method
dlogist <- new("odeModel",
main = function (time, init, parms, ...) {
x <- init
with(as.list(parms), {
x <- r * x * (1 - x / K)
list(c(x))
})
},
parms = c(r=0.1, K=10),
times = seq(0, 100, 1),
init = c(population=0.1),
solver = "euler"
)
plot(sim(dlogist))
## second alternative: use of the "iteration" solver from
## package deSolve, that supports extended plotting functions
dlogist <- new("odeModel",
main = function (time, init, parms, ...) {
x <- init[1]
with(as.list(parms), {
x <- x + r * x * (1 - x / K)
# ^^^ add to old value
list(c(x))
})
},
parms = c(r=0.1, K=10),
times = seq(0, 100, 1),
init = c(population=0.1),
solver = function(y, times, func, parms, ...)
ode(y, times, func, parms, ..., method="iteration")
)
plot(sim(dlogist))
``` |

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.