c++ - Boost 为 ode 集成提供了奇怪的结果
问题描述
我运行了以下代码,它解决了一个简单的微分方程。结果似乎取决于 tsteps。对于 tsteps = 100,我得到的结果是 9.688438503116524e-15,但对于 tsteps = 1000,答案是 7.124585369895499e-218 更接近预期结果。
#include <iostream>
#include <iomanip>
#include <boost/numeric/odeint.hpp> // odeint function definitions
using namespace std;
using namespace boost::numeric::odeint;
typedef std::vector< double> state_type;
int tsize = 1;
void my_observer( const state_type &x, const double t );
void initarrays(state_type &x)
{
x[0]=1.0e0;
}
void my_system( const state_type &x , state_type &dxdt , const double t )
{
dxdt[0]=-x[0];
}
void my_observer( const state_type &x, const double t )
{
std::cout<<t<<" ";
for(int i=0;i<tsize;i++)
{
std::cout<<x[i]<<" ";
}
std::cout<<std::endl;
}
int main()
{
std::cout.setf ( std::ios::scientific, std::ios::floatfield );
std::cout.precision(15);
int size=tsize;
state_type x0(size);
double err_abs = 1.0e-12;
double err_rel = 1.0e-12;
double a_x = 1.0;
double a_dxdt = 1.0;
initarrays(x0);
double t0 = 0.0e0;
int tsteps = 1000;
double t1 = 500.0e0;
double dt = (t1-t0)/((double)tsteps);
typedef runge_kutta_fehlberg78< state_type > solver;
typedef controlled_runge_kutta< solver > controller;
my_observer(x0,t0);
for(int ts=0;ts<tsteps;ts++)
{
integrate_adaptive( make_controlled( err_abs , err_rel , solver() ), my_system, x0 , t0+ts*dt , t0+(1+ts)*dt , dt);
my_observer(x0,t0+(1+ts)*dt);
}
}
解决方案
推荐阅读
- python - 在 linux 上安装 cassandra-driver 以便我可以从 python 连接到 cassandra
- android - React Native:是否可以控制启动图像在应用打开时显示多长时间?
- python - 将 bool 从 feed_dict 传递给函数不起作用
- batch-file - delete 命令在 for 循环之前运行
- c# - 如何使用 Interop 与 C# 读取嵌入在 Word 文档中的 Excel?
- python - ValueError:检查输入时出错:预期dense_1_input的形状为(1、224、224)但得到的数组形状为(224、224、1)
- c# - Selenium 无法在 Firefox 驱动程序中加载特定创建的配置文件
- amp-html - amp-iframe JavaScript 实现
- python - 将文件从服务器传输到本地计算机
- python - 如何在 2 个浮点数之间插入运算符号并计算结果?