Search code examples
pythonrscipyoderunge-kutta

What is the R equivalent of the solve_ivp() function in Python?


I am translating some code from Python to R, and am finding it hard to find the corresponding functions in each. In this particular case, the code I'm having trouble with is:

x_sol_best = solve_ivp(
                    fun=model_covid,
                    y0=x_0_cases,
                    t_span=[t_predictions[0], t_predictions[-1]],
                    t_eval=t_predictions,
                    args=tuple(optimal_params),
                ).y

From the scipy.integrate.solve_ivp documentation, I see that the default integration method used in this function is : ‘RK45’ (default): Explicit Runge-Kutta method of order 5(4)

What packages / functions in R would be equivalent to this?

From the R documentation of the ode function in R, I see that there are a number of RK 4(5) methods available (pasted below) - but the Python documentation states that RK45 is order 5(4)...

Can anyone offer any clarification? TIA

"rk45ck"    |   Runge-Kutta Cash-Karp, order 4(5)
"rk45f" |   Runge-Kutta-Fehlberg, order 4(5); Octave: ode45, pair=1
"rk45e" |   Runge-Kutta-England, order 4(5)
"rk45dp6"   |   Dormand-Prince, order 4(5), local order 6
"rk45dp7", "ode45"  |   Dormand-Prince 4(5), local order 7

Solution

  • According to the documentation, the default solver in solve_ivp() is Dormand-Prince. This called ode45 in the ode() function of the deSolve package.

    x_sol_best = deSolve::ode(
                        y = x_0_cases,
                        times = t_predictions,
                        func = model_covid,
                        parms = c(...), # vector of parameter values
                        method = "ode45"
                    )[ , -1] # drop the t column