Skip to content Skip to sidebar Skip to footer

How To Update Parameter And Pass To Solve_ivp In Python

In my ODE function I need to iteratively solve an equation for a parameter until convergence at each time step. I'd like to pass the latest parameter value to be used as the initia

Solution 1:

It's not clear what you're after: do you want to obtain a solution for an ODE at a series of parameter values (i.e. for each value of the parameter you solve the full ODE) or you are changing the parameter along with the ODE iterations (IOW, you want inner or outer iterations).

If the former, then just do a for loop over the parameters. If the latter, it's likely easier and cleaner to use solver classes which implement specific solvers (DOPRI, Radau, RK, BDF etc), which solve_ivp delegates the work to. They offer a step method, which performs a single step. So that you can adjust you parameters, control convergence etc on a way that's most relevant to this particular case.

Solution 2:

I think what you are looking for is something in the following form:

classtest:
    a = 1e-8deff(self, t, y):
        ## do iter on self.areturnself.a*y
        
t = test()
# solve_ivp(t.f, .....)

This way you can always use the last value of a, since it is part of your instance of the test class. This is not exactly what you are asking for, since this will call the iteration each time solve_ivp evaluates f, which will be multiple times per timestep. However, I think this is the closest you can get, since solve_ivp does not appear to have a callback function to invoke after each timestep

Post a Comment for "How To Update Parameter And Pass To Solve_ivp In Python"