2016-06-09 50 views
0

我想通過使用ODEINT庫通過下面的示例來學習解決ODE問題。但是,當我輸出結果時,時間步剛剛跳過0; 1; 5.5; 25 ...有沒有辦法控制這個時間步使其增加「1」。謝謝!控制時間步驟使用odeint(升壓)

#include <iostream> 
#include <boost/array.hpp> 

#include <boost/numeric/odeint.hpp> 

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

const double sigma = 0.0018; 


typedef std::vector<double> state_type; 

void my_ode(const state_type &x , state_type &dxdt , int t) 
{ 
    dxdt[0] = sigma * x[0]*(1 - x[0]); 

} 

void write_ode(const state_type &x , const double t) 
{ 
    cout << t << '\t' << x[0] << endl; 
} 

int main(int argc, char **argv) 
{ 
    state_type x(1); // Initial condition, vector of 1 element (scalar problem) 
    x[0] = 0.001; 
    integrate(my_ode , x , 0.0 , 6000.0 , 1.0 , write_ode); 
} 

回答

0

是的,有一個簡單的方法可以做到這一點。本教程充滿了實例。

您需要定義一個特定的求解器。一個很好的選擇可能是一個密集的輸出求解器,它具有步長控制,並允許在任意時間步驟計算解。它可能會使用像

// not tested 
state_type x(1); 
x[0] = 0.001; 
auto rkd = runge_kutta_dopri5<state_type>{}; 
auto stepper = make_dense_output(1.0e-6, 1.0e-6, rkd); 
integrate_const(stepper, x, 0.0, 6000.0, 1.0, write_ode); 
+0

我測試了您的代碼,但沒有奏效。然而,這工作:state_type x = {0.001}; //初始條件 typedef dense_output_runge_kutta >> stepper_type; integrate_const(stepper_type(),ode_system,x,0.0,500.0,1.0); 還有一個問題:我們如何檢索時間「t」作爲全局變量?我想用它作爲其他函數的條件(例如,在my_ode()下)?謝謝 – hieu

+0

我不明白你對t是一個全局變量的問題。它是本地的,它會被價值頌揚,這意味着它可能不是全球性的。你想用它做什麼? – headmyshoulder

+0

我想用「t」在my_ode()下設置一個條件:like:if(t <450)then dxdt [0] = 0,否則dxdt [0] = sigma * x [0] *(1 - x [0]); 這與ODEINT的這個問題非常相似,但在python上:http://stackoverflow.com/questions/16520101/update-initial-condition-in-ode-solver-each-time-step/39029114#39029114 – hieu