integrate_times.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* Boost libs/numeric/odeint/examples/integrate_times.cpp
  2. Copyright 2009-2014 Karsten Ahnert
  3. Copyright 2009-2014 Mario Mulansky
  4. example for the use of integrate_times
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE_1_0.txt or
  7. copy at http://www.boost.org/LICENSE_1_0.txt)
  8. */
  9. #include <iostream>
  10. #include <boost/numeric/odeint.hpp>
  11. using namespace std;
  12. using namespace boost::numeric::odeint;
  13. /*
  14. * simple 1D ODE
  15. */
  16. void rhs( const double x , double &dxdt , const double t )
  17. {
  18. dxdt = 3.0/(2.0*t*t) + x/(2.0*t);
  19. }
  20. void write_cout( const double &x , const double t )
  21. {
  22. cout << t << '\t' << x << endl;
  23. }
  24. // state_type = double
  25. typedef runge_kutta_dopri5< double > stepper_type;
  26. const double dt = 0.1;
  27. int main()
  28. {
  29. // create a vector with observation time points
  30. std::vector<double> times( 91 );
  31. for( size_t i=0 ; i<times.size() ; ++i )
  32. times[i] = 1.0 + dt*i;
  33. double x = 0.0; //initial value x(1) = 0
  34. // we can provide the observation time as a boost range (i.e. the vector)
  35. integrate_times( make_controlled( 1E-12 , 1E-12 , stepper_type() ) , rhs ,
  36. x , times , dt , write_cout );
  37. // or as two iterators
  38. //integrate_times( make_controlled( 1E-12 , 1E-12 , stepper_type() ) , rhs ,
  39. // x , times.begin() , times.end() , dt , write_cout );
  40. }