微分方程的求解
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);
}
两个求解结果如下:
可见两者的求解结果相同