Вывод ODEINT в текстовый файл вместо консоли

Есть ли способ записать результаты t и x этого примера в текстовый файл вместо консоли. Спасибо!

Это пример, который я скопировал с сайта Odeint.

#include <iostream>
#include <boost/numeric/odeint.hpp>using namespace std;
using namespace boost::numeric::odeint;/* we solve the simple ODE x' = 3/(2t^2) + x/(2t)
* with initial condition x(1) = 0.
* Analytic solution is x(t) = sqrt(t) - 1/t
*/

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;

int main()
{
double x = 0.0;
integrate_adaptive( make_controlled( 1E-12 , 1E-12 , stepper_type() ) ,
rhs , x , 1.0 , 10.0 , 0.1 , write_cout );
}

-1

Решение

вы можете просто передать вывод этого примера в текстовый файл

$ ./lorenz > data.txt

В противном случае вы можете использовать потоки C ++ для записи вывода непосредственно в файл, например, там описано: http://www.cplusplus.com/doc/tutorial/files/

2

Другие решения

просто замените cout объектом ofstream.

#include <iostream>
#include <fstream>
#include <boost/numeric/odeint.hpp>using namespace std;
using namespace boost::numeric::odeint;

ofstream data("data.txt");
/* we solve the simple ODE x' = 3/(2t^2) + x/(2t)
* with initial condition x(1) = 0.
* Analytic solution is x(t) = sqrt(t) - 1/t
*/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)
{
data << t << '\t' << x << endl;
}

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

int main()
{
double x = 0.0;
integrate_adaptive(make_controlled(1E-12, 1E-12, stepper_type()),
rhs, x, 1.0, 10.0, 0.1, write_cout);
}
1

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector