【问题标题】:ode solver with fixed step size具有固定步长的 ode 求解器
【发布时间】:2015-05-18 03:54:51
【问题描述】:

我想知道odeint 中的步长是否固定。在stepper

基本的步进器概念。遵循此步进器的基本步进器 概念能够执行解决方案 x(t) 的单个步骤 使用给定步长 dt 获得 x(t+dt) 的 ODE。

在我下面的代码中,

#include <iostream>
#include <boost/numeric/odeint.hpp>

using namespace std;
using namespace boost::numeric::odeint;

/* The type of container used to hold the state vector */
typedef std::vector< double > state_type;

const double gam = 0.15;


void sys( const state_type &x , state_type &dx ,  double t )
{
    dx[0] =  x[1];
    dx[1] = -x[0] - gam*x[1];

    static int count(0);

    cout << "count in sys: " << count << endl;

    ++count;
}

int main(int argc, char **argv)
{
    const double dt = 0.1;

    runge_kutta_dopri5<state_type> stepper;
    state_type x(2);
    // initial values
    x[0] = 1.0;
    x[1] = 0.0;



   int count(0);
   double t = 0.0;

   for ( size_t i(0); i < 100; ++i, t+=dt ){

       stepper.do_step(sys , x , t, dt );
       cout << "count in main: " << count << endl;
       ++count;
   }

    return 0;
}

在上面的代码中,我有两个计数器,一个在sys 函数内部,它传递给do_step 用于解决ode,另一个计数器在main 函数内部。输出如下所示

count in sys: 598 
count in sys: 599 
count in sys: 600 
count in main: 99 
Press any key to continue . . .

这是否意味着步长不固定,因为sysmain 中被多次调用?

【问题讨论】:

    标签: c++ odeint


    【解决方案1】:

    步进器的步长是固定的。每一步调用系统函数6次。具体来说,它执行 6 个欧拉步骤,每个步骤具有不同的步长,并进行某种平均以提高解决方案的准确性。

    【讨论】:

    • sys函数内,我想从txt读取数据。有没有安全的方法来检索sys 内每个步长的数据?到目前为止,我正在使用全局变量来解决这个问题。
    • 您可以传递一个带有适当operator() 的类以及打开和阅读txt 文件所需的所有信息。但请注意,在每个步骤中都会调用 sys 多次,并且每个中间步骤的时间不仅仅是 t。也许您需要插入 txt 中的数据,以获取正确的时间。否则,您也可以使用 adams bashforth moulton stepper。它在每个步骤中只调用一次 sys 函数。
    • 当我使用adams_bashforth_moulton&lt;1,state_type&gt; stepper; 时,sys 被调用了两次。任何建议如何强制它被调用一次?
    • 在第一次迭代中,它被多次调用,因为它需要创建一个包含 ode 历史的缓冲区。但是经过几个步骤后,它应该每一步只调用一次 sys 。你能检查一下吗?
    • 能否请您详细说明如何检查?
    猜你喜欢
    • 2012-08-05
    • 1970-01-01
    • 2017-08-19
    • 2017-09-11
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    相关资源
    最近更新 更多