我在 Mint 12 上运行 g++ 4.7,boost 1.55。我正在尝试使用 odeint 解决一个简单的 2d ode 系统——按照此处的 1d 示例:1d .第一个示例在原始版本和答案的修改版本中都可以正常编译。现在,如果我想要一个 2d 系统并且我使用 double[2] 东西不起作用:
#include <iostream>
#include <boost/numeric/odeint.hpp>
using namespace std;
using namespace boost::numeric::odeint;
void rhs( const double *x, double *dxdt, const double t )
{
dxdt[0] = 3.0/(2.0*t*t) + x[0]/(2.0*t);
dxdt[1] = 3.0/(2.0*t*t) + x[1]/(2.0*t);
}
void write_cout( double *x, const double t )
{
cout << t << '\t' << x[0] << '\t' << 2*x[1] << endl;
}
typedef runge_kutta_cash_karp54< double[2] > stepper_type;
int main()
{
double x[2] = {0.0,0.0};
integrate_adaptive( make_controlled( 1E-12, 1E-12, stepper_type() ), rhs, x, 1.0, 10.0, 0.1, write_cout );
}
错误信息一团糟,但结尾是:
/usr/include/boost/numeric/odeint/algebra/range_algebra.hpp:129:47: error: function returning an array
数组 double[2] 是问题所在吗?我应该如何解决它?也许使用 vector ?顺便说一句,我尝试同时使用这两种
typedef runge_kutta_cash_karp54< double > stepper_type;
typedef runge_kutta_cash_karp54< double , double , double , double , vector_space_algebra > stepper_type;
如 1d 答案中所建议,但无济于事。我还应该提到,在具有较旧 boost 的旧机器上(不记得是哪个版本),所有编译都没有问题。感谢您的任何建议!
最佳答案
使用 std::array< double ,2 >
#include <array>
typedef std::array< double , 2 > state_type;
void rhs( state_type const &x, state_type &dxdt, const double t )
{
dxdt[0] = 3.0/(2.0*t*t) + x[0]/(2.0*t);
dxdt[1] = 3.0/(2.0*t*t) + x[1]/(2.0*t);
}
void write_cout( state_type const& x, const double t )
{
cout << t << '\t' << x[0] << '\t' << 2*x[1] << endl;
}
typedef runge_kutta_cash_karp54< state_type > stepper_type;
关于c++ - 带有 odeint 的简单二维系统(使用数组)无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22531696/