【问题标题】:Control time step using odeint (Boost)使用 odeint (Boost) 控制时间步长
【发布时间】:2016-06-13 21:42:32
【问题描述】:

我正在尝试通过以下示例使用 ODEINT 库来学习解决 ODE 问题。但是,当我输出结果时,时间步长只是跳过 0; 1个; 5.5; 25...有没有办法控制这个时间步长,使它增加“1”。谢谢!

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

【问题讨论】:

    标签: c++ boost odeint


    【解决方案1】:

    是的,有一个简单的方法可以做到这一点。该教程充满了示例。

    您需要定义一个特定的求解器。一个不错的选择可能是密集输出求解器,它具有步长控制,可以在任意时间步长计算解。它可能被用作

    // 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);
    

    【讨论】:

    • 我测试了你的代码但没有用。但是,这行得通: state_type x = { 0.001 }; // 初始条件 typedef dense_output_runge_kutta >> stepper_type;积分常数(stepper_type(),ode_system,x,0.0,500.0,1.0);还有一个问题:我们如何将时间“t”检索为全局变量?我想将其用作其他功能的条件(例如在 my_ode() 下)?谢谢
    • 我不明白你关于 t 是一个全局变量的问题。它是本地的,它将在 ode 中按值传递,这意味着它可能不是全局的。你想用它做什么?
    • 我想用"t"在my_ode()下设置一个条件:like: if (tstackoverflow.com/questions/16520101/…
    • 我认为您提出的问题需要一个固定的步长集成。 integrate_const 合适。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    相关资源
    最近更新 更多