• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Boost libs/numeric/odeint/examples/simple1d.cpp
2 
3  Copyright 2012-2013 Mario Mulansky
4  Copyright 2012 Karsten Ahnert
5 
6  example for a simple one-dimensional 1st order ODE
7 
8  Distributed under the Boost Software License, Version 1.0.
9 (See accompanying file LICENSE_1_0.txt or
10  copy at http://www.boost.org/LICENSE_1_0.txt)
11  */
12 
13 
14 #include <iostream>
15 #include <boost/numeric/odeint.hpp>
16 
17 using namespace std;
18 using namespace boost::numeric::odeint;
19 
20 
21 /* we solve the simple ODE x' = 3/(2t^2) + x/(2t)
22  * with initial condition x(1) = 0.
23  * Analytic solution is x(t) = sqrt(t) - 1/t
24  */
25 
rhs(const double x,double & dxdt,const double t)26 void rhs( const double x , double &dxdt , const double t )
27 {
28     dxdt = 3.0/(2.0*t*t) + x/(2.0*t);
29 }
30 
write_cout(const double & x,const double t)31 void write_cout( const double &x , const double t )
32 {
33     cout << t << '\t' << x << endl;
34 }
35 
36 // state_type = double
37 typedef runge_kutta_dopri5< double > stepper_type;
38 
main()39 int main()
40 {
41     double x = 0.0; //initial value x(1) = 0
42     // use dopri5 with stepsize control and allowed errors 10^-12, integrate t=1...10
43     integrate_adaptive( make_controlled( 1E-12 , 1E-12 , stepper_type() ) , rhs , x , 1.0 , 10.0 , 0.1 , write_cout );
44 }
45