我正在使用odeint来模拟一个系统,其中有几个变量不应小于零。
是否有合适的方法将odeint中的变量绑定到特定范围?
答案 0 :(得分:1)
在odeint中没有这种可能性。我想没有算法可以做到这一点。您必须以某种方式编码ODE中的边界。
如果您只想在系统发展过程中找到一个限制,请使用类似
的循环while( t < tmax )
{
stepper.do_step( ode , x , t , dt );
t += dt;
if( check_bound( x , t ) ) break;
}
两个侧节点,也许这就是您的问题:
对于具有守恒定律的特殊算法,其中算法确保守恒定律成立,例如参见辛算子。
如果绑定已经在ODE中以某种方式编码并且无论如何都达到了界限,则必须缩短解算器的步长。
答案 1 :(得分:1)
您所需要的有时称为“饱和度”约束,这是动态系统建模中的常见问题。您可以在等式中轻松编写代码:
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] = ....
...
}