StepEuler: Create a function for advancing the state of an ODE model by...

View source: R/StepEuler.R

StepEulerR 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. RHSfun should have prototype RHSfun(x,t,...), with x representing current system state and t representing current system time. The value of the function should be a vector of the same dimension as x, representing the infinitesimal change in state.

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)

smfsb documentation built on Jan. 13, 2024, 3:02 a.m.