Bound an odeint variable

后端 未结 2 1541
慢半拍i
慢半拍i 2021-01-14 03:15

I\'m using odeint to simulate a system wherein there are several variables which should not go less than zero.

Is there an appropriate way to bound a variable in ode

2条回答
  •  清酒与你
    2021-01-14 03:36

    What you need is sometimes called a "saturation" constraint, a common issue in dynamic systems 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] = ....
        ...
    }
    

提交回复
热议问题