首页 > 解决方案 > Boost 为 ode 集成提供了奇怪的结果

问题描述

我运行了以下代码,它解决了一个简单的微分方程。结果似乎取决于 tsteps。对于 tsteps = 100,我得到的结果是 9.688438503116524e-15,但对于 tsteps = 1000,答案是 7.124585369895499e-218 更接近预期结果。

#include <iostream>
#include <iomanip>
#include <boost/numeric/odeint.hpp> // odeint function definitions
using namespace std;
using namespace boost::numeric::odeint;
typedef std::vector< double> state_type;
int tsize = 1;
void my_observer( const state_type &x, const double t );
void initarrays(state_type &x)
{
    x[0]=1.0e0;
}
void my_system( const state_type &x , state_type &dxdt , const double t )
{
    dxdt[0]=-x[0];
}
void my_observer( const state_type &x, const double t )
{
    std::cout<<t<<" ";
    for(int i=0;i<tsize;i++)
    {
    std::cout<<x[i]<<" ";
    }
    std::cout<<std::endl;
}
int main()
{
    std::cout.setf ( std::ios::scientific, std::ios::floatfield );
    std::cout.precision(15);
    int size=tsize;
    state_type x0(size);
    double err_abs = 1.0e-12;
    double err_rel = 1.0e-12;
    double a_x = 1.0;
    double a_dxdt = 1.0;
    initarrays(x0);
    double t0 = 0.0e0;
    int tsteps = 1000;
    double t1 = 500.0e0;
    double dt = (t1-t0)/((double)tsteps);
    typedef runge_kutta_fehlberg78< state_type > solver;
    typedef controlled_runge_kutta< solver > controller;
    my_observer(x0,t0);
    for(int ts=0;ts<tsteps;ts++)
    {
        integrate_adaptive( make_controlled( err_abs , err_rel , solver() ), my_system, x0 , t0+ts*dt , t0+(1+ts)*dt , dt);
        my_observer(x0,t0+(1+ts)*dt);
    }
}

标签: c++boostode

解决方案


推荐阅读