Christopher Turnbull
Christopher Turnbull

Reputation: 1005

Adjusting scipy.integrate.ode to error tolerances

I have just read Using adaptive time step for scipy.integrate.ode when solving ODE systems .

My code below works fine but the results it produces when solving more complicated equations rather than the one I have provided in the example below, the differential equations seem inaccurate. Is there a way to change this code so that it automatically adapts the time-step according to speci ed absolute and relative error tolerances? eg. 10^-8?

from scipy.integrate import ode

initials = [0.5,0.2] 
integration_range = (0, 30)

def f(t,X):
    x,y = X[0],X[1]

    dxdt = x**2 + y
    dydt = y**2 + x
    return [dxdt,dydt]

X_solutions = []
t_solutions = []
def solution_getter(t,X): 
   t_solutions.append(t)
   X_solutions.append(X.copy()) 


backend = "dopri5"
ode_solver = ode(f).set_integrator(backend)
ode_solver.set_solout(solution_getter)
ode_solver.set_initial_value(y=initials, t=0)

ode_solver.integrate(integration_range[1])

Upvotes: 1

Views: 1544

Answers (1)

Lutz Lehmann
Lutz Lehmann

Reputation: 25992

You could set the values of rtol and atol in the set_integrator call, see https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.ode.html.

The default values provide a medium accuracy that is good enough for graphics, but may not be enough for other purposes.

Upvotes: 2

Related Questions