微分方程 c++和matlab求解

1.matlab ode45与c++odeint求解对比

方程用最基础的一阶常微分方程 d y d t = t , y t = 0 = 0 \frac{dy}{dt}=t,y_{t=0}=0 dtdy=t,yt=0=0
下面是matlab和c++的代码求解过程,均设置步长为0.1,求解从t=0~t=2的数值解
首先是matlab

clc; %清除命令行
clear; %清除工作区

odefun=@(t,y)t;
[t,y]=ode45(odefun,0:0.1:2,0);

然后是c++的代码

#include <iostream>
#include <math.h>
#include <boost/numeric/odeint.hpp>
#include <vector>

typedef std::vector<double> state_type;

using namespace std;
using namespace boost::numeric::odeint;

void equation1(const state_type& x, state_type& dxdt, double t)
{
    dxdt[0] = t;
}
//常微分方程1输出
void observer1(const state_type& x, const double t)
{
     cout << " t = " << t << '\t' << x[0] << endl;
}
int main(int argc, char** argv)
{
    state_type x0 = { 0 };
    integrate_const(runge_kutta4<state_type>(), equation1, x0, 0.0, 2.0, 0.1, observer1);
}

两个求解结果如下:

matlab与C++求解结果

可见两者的求解结果相同

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值