Home > Blockchain >  SciPy - Values in `t_eval` are not properly sorted
SciPy - Values in `t_eval` are not properly sorted

Time:12-02

I keep running into a problem where my code keeps telling me that "Values in t_eval are not properly sorted." I have tried using sort functions and manually sorting (below). What am I not seeing? I'm not sure how to fix this problem. I'm almost certain it's something very simple that I'm overlooking, but I can't find it and the assignment is due in an hour.

def pff(t, y, p):
    if (2 < t) and (t < 4):
        z = 1
    else:
        z = 0
    
    dy = np.zeros(2)
    dy[0]= (p[0] * (y[1]   z)**p[3]) / (1   (y[1]   z)**p[3]) - (p[2] * y[0])
    dy[1]= (p[1] * (y[0]   z)**p[3]) / (1   (y[0]   z)**p[3]) - (p[2] * y[1])

alpha = 2
beta = 2
gamma = .5
n = 2
k = 1

p = [gamma, k, n, alpha, beta]
x_0 = np.zeros([0,])
t_span = [0, 20]

sol = integrate.solve_ivp(pff, t_span, x_0, 'RK45', p)

Traceback:

ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_36808/2811005318.py in <module>
      9 t_span = [0, 20]
     10 
---> 11 sol = integrate.solve_ivp(pff, t_span, x_0, 'RK45', p)

~\anaconda3\lib\site-packages\scipy\integrate\_ivp\ivp.py in solve_ivp(fun, t_span, y0, method, t_eval, dense_output, events, vectorized, args, **options)
    527         d = np.diff(t_eval)
    528         if tf > t0 and np.any(d <= 0) or tf < t0 and np.any(d >= 0):
--> 529             raise ValueError("Values in `t_eval` are not properly sorted.")
    530 
    531         if tf > t0:

ValueError: Values in `t_eval` are not properly sorted.

CodePudding user response:

From the solve_ivp docs

args tuple, optional
Additional arguments to pass to the user-defined functions. If given, the 
additional arguments are passed to all user-defined functions. So if, for 
example, fun has the signature fun(t, y, a, b, c), then jac (if given) and 
any event functions must have the same signature, and args must be a tuple 
of length 3.

where as t_eval is the one that's supposed to be sorted - if provided.

t_eval array_like or None, optional
Times at which to store the computed solution, must be sorted and lie 
within t_span. If None (default), use points selected by the solver.
  • Related