mjg211 / phaseR

Development version of phaseR, an R package for phase plane analysis of one- and two-dimensional autonomous ODE systems
https://doi.org/10.32614/RJ-2014-023
Other
15 stars 3 forks source link

Method "ode45" not adapted for some models trajectories #8

Closed vcarret closed 4 years ago

vcarret commented 4 years ago

https://github.com/mjg211/phaseR/blob/4f58357b7c285877eb0266ec70a5608bb49f58b5/R/trajectory.R#L209

First of thank you for your time-saving package. I give a reproducible exemple below: for some initial conditions on this model, method "ode45" computing time for the trajectory is exponentially longer than method "euler". I am not sure whether the choice of "ode45" was guided by some particular reason? Maybe you could add the possibility to provide a method in the call to trajectory?

library(tictoc)
library(deSolve)

params <- c(s = 0.35, gamma = 2.5, n = 1.5, alpha = 3, beta = 2)

model <- function(t, y, parameters){
  with(as.list(parameters),{
    g <- y[1] 
    gw <- y[2]
    dg <- gamma*s*(1/gw-1/g)
    dgw <- -gw^2/s*(alpha*s*(1/g-1/gw) - beta*(g-n))

    list(c(dg,dgw))
  })
}
yini <- c(g = 0.27, gw = 1)
times <- seq(0, 5, by = 0.1)
tic()
out <- ode(yini, times, model, params, method = "ode45")
toc()
24.501 sec elapsed

tic()
out <- ode(yini, times, model, params, method = "euler")
toc()
0.003 sec elapsed
mjg211 commented 4 years ago

Very sensible change 👍Have added 'method' as an option where relevant in 2.2

vcarret commented 4 years ago

Very sensible change Have added 'method' as an option where relevant in 2.2

Did you not change it on purpose in trajectory.r ?

https://github.com/mjg211/phaseR/blob/b940952b8032b94ee2b35c71d2b6546f2740f2ed/R/trajectory.R#L209

This line was actually the one causing me problems when I drew some trajectories

mjg211 commented 4 years ago

An oversight - should be fixed now