summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/numeric/odeint/examples/integrate_times.cpp
blob: 9e6b58104f750598d57257b7393756a754bdf20e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* Boost libs/numeric/odeint/examples/integrate_times.cpp

 Copyright 2009-2014 Karsten Ahnert
 Copyright 2009-2014 Mario Mulansky

 example for the use of integrate_times

 Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or
 copy at http://www.boost.org/LICENSE_1_0.txt)
 */


#include <iostream>
#include <boost/numeric/odeint.hpp>

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


/*
 * simple 1D ODE
 */

void rhs( const double x , double &dxdt , const double t )
{
    dxdt = 3.0/(2.0*t*t) + x/(2.0*t);
}

void write_cout( const double &x , const double t )
{
    cout << t << '\t' << x << endl;
}

// state_type = double
typedef runge_kutta_dopri5< double > stepper_type;

const double dt = 0.1;

int main()
{
    // create a vector with observation time points
    std::vector<double> times( 91 );
    for( size_t i=0 ; i<times.size() ; ++i )
        times[i] = 1.0 + dt*i;

    double x = 0.0; //initial value x(1) = 0
    // we can provide the observation time as a boost range (i.e. the vector)
    integrate_times( make_controlled( 1E-12 , 1E-12 , stepper_type() ) , rhs ,
                     x , times , dt , write_cout );
    // or as two iterators
    //integrate_times( make_controlled( 1E-12 , 1E-12 , stepper_type() ) , rhs ,
    //                   x , times.begin() , times.end() , dt , write_cout );
}