1

I am trying to use solve_ivp() function to solve for an ODE with state-dependent args. The idea is to update the ODE at each time instants given the current state of the trajectory.

Given the ODE model as the following:

def model(t, y, arg1, arg2):
    '''
    Some Dynamics model that includes arg1 and arg 2
    '''
    return dy_dt

The arg1 and arg2 are calculated using some other function:

def gen_args(y):
    '''
    Calculate arg1 and arg2 based on the state y
    '''
    return arg1, arg2

Then I want to integrate over the ODE model:

scipy.integrate.solve_ivp(model, t_span, y0, method='RK45', t_eval=None, dense_output=False, events=None, vectorized=False)

What would be a nice way to implement this?

I have checked similar questions like here and here, But all the args are external constants.

Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
9Cookiee
  • 45
  • 7

2 Answers2

3

A simple solution is to call gen_args in model:

def model(t, y):
    arg1, arg2 = gen_args(y)
    dy_dt = ...
    return dy_dt
Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214
0

Adding on to @Warren Weckesser answer

It makes very less sense to include any state and (or) time-dependent arguments in the args parameter of 'solve_ivp'. They should be included in the 'fun' parameter of 'solve_ivp' itself. Including any state and (or) time-dependent arguments analogous to this answer inspired by @Warren Weckesser is not possible. The args parameter in

solve_ivp( fun, t_span, y0, method='RK45', t_eval=None, dense_output=False, events=None, vectorized=False, args=None, **options, )

can only take in a tuple of constant arguments (for example floats, integers, etc)

Tejas Shetty
  • 685
  • 6
  • 30