Search code examples
python-3.xscipyode

RK45 ode solver python3


How to use this function please?

I have a function that return list of right-hand side differential equations right(t, x0).

import scipy.integrate as ode
t, r =ode.RK45(right(t, x0), t0, tmax, dt)

The error:

TypeError: 'list' object is not callable

What should be the first argument please?


Solution

  • You pass the function as object, not a function value, thus

    solver = ode.RK45(right)
    solver.set_initial(y0,t0)
    

    and then in a loop over solver.integrate you compute the desired integration steps, see the examples in the documentation.

    If you have any recent version of scipy, then use the newer solve_ivp interface, where you provide everything in one call

    solution = solve_ivp(right, [t0, tmax], y0, method = "RK45", t_eval = np.arange(t0,tmax,dt))
    

    and find the solution components in solution.t and solution.y, where the latter is a tuple of the time series of each state component.