Variable odeint bound

I use odeint to model a system in which there are several variables that should not go less than zero.

Is there a way to bind a variable in odeint to a specific range?

+2
source share
2 answers

In odeint there is no such possibility. And I think that there are no algorithms that could do this. You must somehow encode the binding in your ODE.

If you want to find a binding during the evolution of your system, use a loop like

while( t < tmax ) { stepper.do_step( ode , x , t , dt ); t += dt; if( check_bound( x , t ) ) break; } 

Two side nodes, maybe this relates to your problem:

  • There are special algorithms for ODEs with conservation laws, where the algorithm ensures that the conservation law is fulfilled, for example, symplectic solvers.

  • If you are bound in some way encoded in your ODE, and the boundary is somehow reached, you should shorten the solution step.

+1
source

What you need is sometimes called a β€œsaturation” constraint, a common problem in dynamic system modeling. You can easily code it inside your equation:

 void YourEquation::operator() (const state_type &x, state_type &dxdt, const time_type t) { // suppose that x[0] is the variable that should always be greater/equal 0 double x0 = x[0]; // or whatever data type you use dxdt[0] = .... // part of your equation here if (x0 <= 0 && dxdt[0] < 0) { x0 = 0; dxdt[0] = 0 } // the rest of the system equations, use x0 instead of x[0] if necessary, actually it depends on the situation and physical interpretation dxdt[1] = .... dxdt[2] = .... ... } 
0
source

All Articles