利用c++ boost库求解一阶微分方程计算船舶运动

本文介绍了一种基于Eigen和Boost库的船舶运动模拟方法,通过定义船舶类,实现船舶在风力、水流和控制力作用下的三维运动。利用Runge-Kutta-Dopri5算法求解船舶动力学方程,实现船舶状态的实时更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

船舶类的头文件 vessel.h

#pragma once
#include"Eigen/Dense"
#include"boost/numeric/odeint/stepper/runge_kutta_dopri5.hpp"

class Vessel {
public:
	static const size_t dim = 3;
	typedef Eigen::Vector3d state_type;
	struct VesselMotion {
		Eigen::Vector3d F;
		Eigen::Matrix3d D;
		Eigen::Matrix3d M_inv;
		VesselMotion(const Eigen::Matrix3d &M_inv, const Eigen::Matrix3d &D, Eigen::Vector3d &F) : M_inv(M_inv), D(D), F(F) {}
		void operator()(const state_type &x, state_type &dxdt, const double t) const {
			dxdt = M_inv * (-D * x + F);
		}
	};

	Vessel();
	void Move(Eigen::Vector3d &tauWind, Eigen::Vector3d &tauCurrent, Eigen::Vector3d &tauControl);

	Eigen::Vector3d eta;
	Eigen::Vector3d nu;
	Eigen::Vector3d nu_;
	Eigen::Matrix3d M;
	Eigen::Matrix3d D;
};

船舶类的成员函数 vessel.cpp

#include"vessel.h"

Vessel::Vessel() {
	M << 4, 0, 0,
		0, 4, 0,
		0, 0, 4;
	D << 0, 0, 0,
		0, 0, 0,
		0, 0, 0;
	eta << 0, 0, 0;
	nu << 0, 0, 0;
	nu_ << nu;
};

void Vessel::Move(Eigen::Vector3d &tauWind, Eigen::Vector3d &tauCurrent, Eigen::Vector3d &tauControl) {
	Eigen::Vector3d F = tauWind + tauCurrent + tauControl;
	const int steps = 10;
	const double dt = 0.1;
	state_type x = { this->nu[0], this->nu[1], this->nu[2] };
	boost::numeric::odeint::runge_kutta_dopri5<state_type> stepper;
	for (int step = 0; step < steps; ++step) {
		stepper.do_step(VesselMotion(this->M.inverse(), this->D, F), x, step*dt, dt);
	}
	this->nu_ << x[0], x[1], x[2];

	this->eta << (steps * dt / 2 * (this->nu + this->nu_));
	this->nu << this->nu_;
}

编写主程序求解 main.cpp

#include"vessel.h"

int main() {
	Vessel vessel;
	Eigen::Vector3d tauWind;
	Eigen::Vector3d tauCurrent;
	Eigen::Vector3d tauControl;
	tauWind << 1, 1, 1;
	tauCurrent << 1, 1, 1;
	tauControl << 4, 4, 4;
	vessel.Move(tauWind, tauCurrent, tauControl);
	
	system("pause");
	return 0;
}

其中,Eigen库和boost库在visual studio 2017 中的添加参见

Eigen库在VS2017下的配置与使用_Kerwines的博客-优快云博客_vs配置eigen

VS配置boost库_今天也要debug的博客-优快云博客_vs安装boost库

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值