抱歉,如果这是一个简单的问题 - 但是否存在对 odeint 中状态变量的演变进行下采样的“最佳实践”?
下面,我复制了一个很好的例子来构建一个“观察者”来记录本文 (http://www.codeproject.com/Articles/268589/odeint-v2-Solving-ordinary-differential-equations) 中提供的状态变量
struct streaming_observer
{
std::ostream &m_out;
streaming_observer( std::ostream &out ) : m_out( out ) {}
void operator()( const state_type &x , double t ) const
{
m_out << t;
for( size_t i=0 ; i < x.size() ; ++i )
m_out << "\t" << x[i];
m_out << "\n";
}
};
// ...
integrate_const( runge_kutta4< state_type >() , lorenz , x , 0.0 , 10.0 , dt , streaming_observer( std::cout ) );
您将如何更改观察者以仅每 10 步记录一次状态(例如)。我想知道是否有比放入 if 语句更优雅的解决方案:
struct streaming_observer
{
std::ostream &m_out;
int count;
streaming_observer( std::ostream &out ) : m_out( out ) {count = 10;}
void operator()( const state_type &x , double t ) const
{
if( count == 10 ) {
count = 1;
m_out << t;
for( size_t i=0 ; i < x.size() ; ++i )
m_out << "\t" << x[i];
m_out << "\n";
}
else {
count++;
}
}
};
最佳答案
我遇到了同样的问题,并且完全按照你的方式解决了它。但是,您也可以考虑使用带步长控制的步进器,然后将 integrate_const 与 dt 一起使用,以便在所需的时间间隔调用观察器。当您使用带步长控制的步进器时(甚至更好:像 dopri5 这样的密集输出),integrate_const 会根据您的误差容限调整步长,但随后确保观察器在时间 t0 + n*dt 被调用。
关于c++ - odeint (c++) - 下采样观察,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20360972/