rkMethod()          # returns the names of all available methods
rkMethod("rk45dp7") # parameters of the Dormand-Prince 5(4) method
rkMethod("ode45")   # an alias for the same method
func <- function(t, x, parms) {
  with(as.list(c(parms, x)),{
    dP  <- a * P        - b * K * P
    dK  <- b * P * K  - c * K
    res <- c(dP, dK)
    list(res)
  })
}
times  <- seq(0, 20, length = 21)
parms  <- c(a = 0.1, b = 0.1, c = 0.1)
x <- c(P=2, K=1)
ode(x, times, func, parms, method = rkMethod("rk4"))
ode(x, times, func, parms, method = "ode45")
## disable polynomial interpolation (dense output)
## and fall back to linear approximation
ode(x, times, func, parms, method = rkMethod("rk45dp7", d = NULL))
## define and use a new rk method
ode(x, times, func, parms, 
  method = rkMethod(ID = "midpoint",
    varstep = FALSE,
    #A       = matrix(c(0, 0, 1/2, 0), nrow=2, byrow=TRUE), 
    # or simply, because this A is nonzero only in the subdiagonal
    A      = c(0, 1/2),
    b1      = c(0, 1),
    c       = c(0, 1/2),
    stage   = 2,
    Qerr    = 1
  )
)Run the code above in your browser using DataLab