simple1d.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* Boost libs/numeric/odeint/examples/simple1d.cpp
  2. Copyright 2012-2013 Mario Mulansky
  3. Copyright 2012 Karsten Ahnert
  4. example for a simple one-dimensional 1st order ODE
  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. /* we solve the simple ODE x' = 3/(2t^2) + x/(2t)
  14. * with initial condition x(1) = 0.
  15. * Analytic solution is x(t) = sqrt(t) - 1/t
  16. */
  17. void rhs( const double x , double &dxdt , const double t )
  18. {
  19. dxdt = 3.0/(2.0*t*t) + x/(2.0*t);
  20. }
  21. void write_cout( const double &x , const double t )
  22. {
  23. cout << t << '\t' << x << endl;
  24. }
  25. // state_type = double
  26. typedef runge_kutta_dopri5< double > stepper_type;
  27. int main()
  28. {
  29. double x = 0.0; //initial value x(1) = 0
  30. // use dopri5 with stepsize control and allowed errors 10^-12, integrate t=1...10
  31. integrate_adaptive( make_controlled( 1E-12 , 1E-12 , stepper_type() ) , rhs , x , 1.0 , 10.0 , 0.1 , write_cout );
  32. }