For a second-order ODE (dopri5 method in python) the code below always results in an error: C:\Users\MY\Anaconda3\lib\site-packages\scipy\integrate\_ode.py:1019: UserWarning: dopri5: larger nmax is needed
self.messages.get(idid, 'Unexpected idid=%s' % idid)). I've changed the parameters but nothing seems to help. Even setting nsteps=100000 doesn't work. Is there any other way to solve this instead of just increasingnsteps?
from scipy.integrate import ode
import numpy as np
def fun(t, y):
return np.array([y[1], -3/t*y[1] + 7/(t**6)*y[0]])
yinit = np.array([0.01, 0.2])
dt = 0.01
t_stop = 2
solver = ode(fun).set_integrator('dopri5', nsteps=100000).set_initial_value(yinit)
solver.t = 0.001
t_RK4_sci = [0]
x_RK4_sci = [yinit]
while solver.successful() and solver.t < t_stop:
solver.integrate(solver.t+dt, step=True)
t_RK4_sci.append(solver.t)
x_RK4_sci.append(solver.y)
t_RK4_sci = np.array(t_RK4_sci)
x_RK4_sci = np.array(x_RK4_sci)
Put a
as first line in
funto see that your solution is rapidly exploding while employing very small step sizes. The last lines of that log areTo see the mathematical side of it observe that the initial Lipschitz constant is somewhere at
L=1e+18.Useful step sizes for numerical integration have to observe
L*dt < 10, probably a smaller upper bound, to stay inside the region of A-stability for explicit methods.The magnification rate from local to global error is
(exp(L*T)-1), whereTis the length of the integration interval. This means that meaningful results can only be expected for, optimistically,L*T < 50, which givesT<5e-17.Under those theoretical restrictions, the dopri5 integrator proves as quite robust in practice since it bails out only at
T=2.5e-7.Perturbation to the Euler form
gives initial growth in the range of
and since the largest double is around
10^300the numerical range gets exceeded at aroundwhich is the closest to where the numerical integration bails out and thus probably the true reason. (Better bounds give
10^(308/2.6e6)=1.00027280498.)Summary
Not only has this differential equation an exceedingly large Lipschitz constant and is thus ill-behaved or stiff, also the exact solution, as far as the approximation by an Euler equation can tell, grows so fast to exceed the
doublerange at the time where the numerical integration breaks down. That is, even a better method like an implicit integrator will not give better results.