1、导入库函数
2、添加投文件#include <boost/numeric/odeint.hpp>
给定初始状态 ,集成函数从初始时间
到结束时间
不断地调用给定的 ,计算微分方程在不同时刻的解,用户还可以提供 以分析某个时刻的状态值。
integrate_const(stepper, system, x0, t0, t1, dt, observer)
3、代码实现
一个简单二阶微分方程:
define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <cmath>
#include <boost/numeric/odeint.hpp>
using namespace std;
using namespace boost::numeric::odeint;
typedef std::vector<double> state_type;// 定义状态变量的类型
// 要求解的微分方程
void pendulum(const state_type &x, state_type &dxdt, double t)
{
dxdt[0] = x[1];
dxdt[1] = -x[1] - sin(x[0]);
}
// Observer打印状态值
void write_pendulum(const state_type &x, const double t)
{
cout << t << '\t' << x[0] << '\t' << x[1] << endl;
}
int main()
{
int main(int argc, char **argv)
{
// 初始条件,二维向量
state_type x = {0.10 , 0.00};
// 求解方法为runge_kutta4
integrate_const(runge_kutta4<state_type>(), pendulum, x , 0.0 , 5.0 , 0.01 , write_pendulum);
}
return 0;
}