Search code examples
c++boostodeint

Solving a system of ODEs backwards in time in c++


Unfortunately, I have noticed that BOOST's odeint cannot solve a system of ODEs backward in time i.e. when I change the conditions so that

typedef std::vector< double > state_type;

void ode_function(const state_type &x, state_type &dxdt, const double     
{
dxdt[0] = x[0];
}

using namespace std;
using namespace boost::numeric::odeint;

state_type x(1);
x[0] = std::exp(1); 


runge_kutta4< state_type > stepper;
integrate_const(stepper, ode_function, x, 1., 0., 0.01);

cout << x[0] << endl;

This in fact does nothing and returns the initial condition unchanged. In the case of this simple example it is possible to solve this by changing variables s=-t. However, I am not sure if this trick works well for any system of ODEs. When I use it in my program I am not sure if it gives the correct results. Therefore, does anyone know any c++ library that allows backward time integration?


Solution

  • in odeint if you want to integrate backward in time you also have to use a negative step size. in your case you should be using -0.01 as last parameter in the integrate_const function.