2016-06-09 54 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