Reputation: 497
I want to solve a system of ODEs with scipy's solve_ivp and I need a negative first_step in order to evolve the solution, but
Nsol = solve_ivp(derivs, (N , Nend), ydoub, method='RK45', t_eval=None, dense_output=False, events=None, vectorized=False, first_step=-1e-8)
returns
ValueError: `first_step` must be positive.
Any ideas how to resolve this or find a way around it?
EDIT: here is the code which yields this:
import numpy
from scipy.integrate import solve_ivp
w = numpy.array ( [ 0.00000000e+00, 1.00000000e+00, 3.17214587e-01,
-3.41988549e-01, -1.50137165e-05, -2.48117074e-02,
1.17624224e-03, -1.27149037e-04] )
def derivs2 (t, w):
dydN = numpy.zeros(2 , dtype=float , order='C')
dydN[0] = 0.0
dydN[1] = y[1] * y[2]
dydN[2] = y[2] * ( y[3] + 2.0 * y[2] )
dydN[3] = 2.0 * y[4] - 5.0 * y[2] * y[3] - 12.0 * y[2] * y[2]
for i in range (4 , NEQS-1):
dydN[i] = ( 0.5 * (i-3) * y[3] + (i-4) * y[2] ) * y[i] + y[i+1]
dydN[NEQS-1] = ( 0.5 * (NEQS-4) * y[3] + (NEQS-5) * y[2] ) * y[NEQS-1]
return dydN
Nsol = solve_ivp(derivs, (1000.0 , 0.0), w, method='RK45', t_eval=None, dense_output=False, events=None, vectorized=False , first_step=-1e-6)
Upvotes: -1
Views: 3579
Reputation: 595
Your code has many problems (derivs
vs. derivs2
and w
vs y
, etc.) and will not run.
first_step
is actually the magnitude of the first step, this is not described clearly, or at all, in the documentation. Change first_step=1e-6
and this should work.
from scipy.integrate import solve_ivp
def fun(t, y):
return y
try:
sol = solve_ivp(fun, (1000, 0), [1], first_step=-1e-6)
except ValueError:
print(f"fails backwards")
sol = solve_ivp(fun, (1000, 0), [0], first_step=1e-6)
print(sol.t)
print("First step = {}".format(sol.t[1]-sol.t[0]))
result:
fails backwards
[1000. 999.999999 999.999989 999.999889 999.998889
999.988889 999.888889 998.888889 988.88888903 888.88888928
0. ]
First step = -9.999999974752427e-07
Upvotes: 2