用C++的odeint库求解微分方程

1、导入库函数

2、添加投文件#include <boost/numeric/odeint.hpp>

给定初始状态  x0,集成函数从初始时间 t0 到结束时间 t1 不断地调用给定的 stepper,计算微分方程在不同时刻的解,用户还可以提供 observer以分析某个时刻的状态值。

integrate_const(stepper, system, x0, t0, t1, dt, observer)

3、代码实现

一个简单二阶微分方程:

\dot{x} = v

\dot{v} = -v +sin(x)

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;
}

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值