2015-05-18 31 views
0

我想知道odeint中的步長是否是固定的。在stepper固定步長的頌歌解算器

基本步進概念。遵循該步進概念的基本步進器能夠執行ODE的解x(t)的單個步驟以使用給定步長dt來獲得x(t + dt)。

在我下面的代碼,

#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; 
} 

在上面的代碼,我有兩個櫃檯,其中一個被傳遞到do_step解決odemain函數內的另一個櫃檯sys函數內。輸出如下

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

這是否意味着在步長是不固定的,因爲sys被稱爲比main的一次?

回答

2

步進大小是固定的。每步調用系統功能6次。詳細地說,它執行6個具有不同步長的歐拉步驟,並進行某種平均以提高解決方案的準確性。

+0

裏面'sys'函數,我想從'txt'讀取數據。是否有任何安全的方法來檢索'sys'中每個步長的數據?到目前爲止,我正在使用全局變量來解決這個問題。 – CroCo

+0

你可以通過一個合適的'operator()'類來傳遞一個類,並且你需要打開和讀取txt文件的所有信息。但請注意,在每個步驟中系統都會被調用多次,並且每個中間步驟的時間並不是簡單的「t」。也許你需要插入來自txt的數據,只是爲了讓時間正確。否則,你也可以使用adams bashforth moulton步進器。它在每個步驟中只調用一次sys函數。 – headmyshoulder

+0

當我使用'adams_bashforth_moulton <1,state_type>步進器;'時,'sys'被調用兩次。任何建議如何強制它被調用一次? – CroCo