bulirsch_stoer.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * bulirsch_stoer.cpp
  3. *
  4. * Copyright 2011-2013 Mario Mulansky
  5. * Copyright 2011-2012 Karsten Ahnert
  6. *
  7. * Distributed under the Boost Software License, Version 1.0.
  8. * (See accompanying file LICENSE_1_0.txt or
  9. * copy at http://www.boost.org/LICENSE_1_0.txt)
  10. */
  11. #include <iostream>
  12. #include <fstream>
  13. #define _USE_MATH_DEFINES
  14. #include <cmath>
  15. #include <boost/array.hpp>
  16. #include <boost/ref.hpp>
  17. #include <boost/numeric/odeint/config.hpp>
  18. #include <boost/numeric/odeint.hpp>
  19. #include <boost/numeric/odeint/stepper/bulirsch_stoer.hpp>
  20. #include <boost/numeric/odeint/stepper/bulirsch_stoer_dense_out.hpp>
  21. using namespace std;
  22. using namespace boost::numeric::odeint;
  23. typedef boost::array< double , 1 > state_type;
  24. /*
  25. * x' = ( - x*sin t + 2 tan x ) y
  26. * with x( pi/6 ) = 2/sqrt(3) the analytic solution is 1/cos t
  27. */
  28. void rhs( const state_type &x , state_type &dxdt , const double t )
  29. {
  30. dxdt[0] = ( - x[0] * sin( t ) + 2.0 * tan( t ) ) * x[0];
  31. }
  32. void rhs2( const state_type &x , state_type &dxdt , const double t )
  33. {
  34. dxdt[0] = sin(t);
  35. }
  36. ofstream out;
  37. void write_out( const state_type &x , const double t )
  38. {
  39. out << t << '\t' << x[0] << endl;
  40. }
  41. int main()
  42. {
  43. bulirsch_stoer_dense_out< state_type > stepper( 1E-8 , 0.0 , 0.0 , 0.0 );
  44. bulirsch_stoer< state_type > stepper2( 1E-8 , 0.0 , 0.0 , 0.0 );
  45. state_type x = {{ 2.0 / sqrt(3.0) }};
  46. double t = M_PI/6.0;
  47. //double t = 0.0;
  48. double dt = 0.01;
  49. double t_end = M_PI/2.0 - 0.1;
  50. //double t_end = 100.0;
  51. out.open( "bs.dat" );
  52. out.precision(16);
  53. integrate_const( stepper , rhs , x , t , t_end , dt , write_out );
  54. out.close();
  55. x[0] = 2.0 / sqrt(3.0);
  56. out.open( "bs2.dat" );
  57. out.precision(16);
  58. integrate_adaptive( stepper , rhs , x , t , t_end , dt , write_out );
  59. out.close();
  60. x[0] = 2.0 / sqrt(3.0);
  61. out.open( "bs3.dat" );
  62. out.precision(16);
  63. integrate_adaptive( stepper2 , rhs , x , t , t_end , dt , write_out );
  64. out.close();
  65. typedef runge_kutta_dopri5< state_type > dopri5_type;
  66. typedef controlled_runge_kutta< dopri5_type > controlled_dopri5_type;
  67. typedef dense_output_runge_kutta< controlled_dopri5_type > dense_output_dopri5_type;
  68. dense_output_dopri5_type dopri5 = make_dense_output( 1E-9 , 1E-9 , dopri5_type() );
  69. x[0] = 2.0 / sqrt(3.0);
  70. out.open( "bs4.dat" );
  71. out.precision(16);
  72. integrate_adaptive( dopri5 , rhs , x , t , t_end , dt , write_out );
  73. out.close();
  74. }