增强为ode集成提供奇怪的结果

时间:2019-04-24 18:50:51

标签: c++ boost ode

我运行了以下代码,用于解决一个简单的微分方程。结果似乎取决于步骤。对于ts​​teps = 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);
    }
}

0 个答案:

没有答案