c++ - 具有固定步长的 ode 求解器

标签 c++ odeint

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

The basic stepper concept. A basic stepper following this Stepper concept is able to perform a single step of the solution x(t) of an ODE to obtain x(t+dt) using a given step size 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;
}

在上面的代码中,我有两个计数器,一个在 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 中被调用了不止一次?

最佳答案

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

关于c++ - 具有固定步长的 ode 求解器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30294862/

相关文章:

c++ - 如何可靠地确定文件是否已在 Microsoft Excel 中打开?

python - 如何监控SciPy.odeint的进程?

c++ - 如何在 Linux 中安装仅 header (odeint) 库?

python - Rs deSolve 和 Python odeint 之间的差异

C++ 在派生类构造函数之前期望主表达式

在 Eclipse 中进行 C++ 开发,找不到编译器

c++ - 方法不可访问

c++ - 如何修复来自 getMemberNames() 的 JsonCPP 错误?

c++ - 如何使用 C++ Boost odeint 库求解这个常微分方程

c++ - 限制 boost::odeint 集成中的步骤数