StepEuler {smfsb} | R Documentation |
Create a function for advancing the state of an ODE model by using a simple Euler integration method
Description
This function creates a function for advancing the state of an ODE model
using a simple Euler integration method. The resulting function
(closure) can be used in conjunction with other functions (such as
simTs
) for simulating realisations of ODE models. This
function is intended to be pedagogic. See StepODE
for a
more accurate integration function.
Usage
StepEuler(RHSfun,dt=0.01)
Arguments
RHSfun |
A function representing the RHS of the ODE
model. |
dt |
Time step to be used by the simple Euler integration method. Defaults to 0.01. |
Value
An R function which can be used to advance the state of the ODE model RHSfun
by using an Euler method with step size dt
. The function closure has interface function(x0,t0,deltat,...)
, where t0
and x0
represent the initial time and state, and deltat
represents the amount of time by which the process should be advanced. The function closure returns a vector representing the simulated state of the system at the new time.
See Also
StepEulerSPN
, StepODE
, simTs
, simSample
Examples
# Build a RHS for the Lotka-Volterra system
LVrhs <- function(x,t,th=c(c1=1,c2=0.005,c3=0.6))
{
with(as.list(c(x,th)),{
c( c1*x1 - c2*x1*x2 ,
c2*x1*x2 - c3*x2 )
})
}
# create a stepping function
stepLV = StepEuler(LVrhs)
# step the function
print(stepLV(c(x1=50,x2=100),0,1))
# integrate the process and plot it
out = simTs(c(x1=50,x2=100),0,20,0.1,stepLV)
plot(out,plot.type="single",lty=1:2)