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

船舶类的头文件 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的博客-CSDN博客_vs配置eigen

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

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
一阶时滞方程是指方程的解在当前时间点的值与之前某一时间点的值存在一定的延迟关系。求解它可以使用数值方法来逼近解。 以下是一阶时滞方程求解c的源代码示例: ```Python import numpy as np from scipy.integrate import solve_ivp def delay_func(t, y, c, tau): # 定义一阶时滞方程的函数,c为待求解的常数,tau为时间延迟 return -c * y(t-tau) def solve_delay_equation(c, tau): # 求解一阶时滞方程的函数 t_start = 0 # 初始时间 t_end = 10 # 结束时间 t_span = (t_start, t_end) # 时间范围 y_initial = [1] # 初始条件 # 使用solve_ivp函数求解微分方程 sol = solve_ivp(delay_func, t_span, y_initial, args=(c, tau)) return sol.t, sol.y[0] # 输入待求解的c和时间延迟tau c = 0.5 tau = 2.0 # 求解一阶时滞方程 t, y = solve_delay_equation(c, tau) # 打印结果 print(f"c的解为{c}") print(f"时间范围为{t[0]}到{t[-1]}") print(f"y的值为{y}") ``` 这段代码通过调用`solve_ivp`函数来求解一阶时滞方程。其中`delay_func`定义了一阶时滞方程的函数形式,`solve_delay_equation`函数用于求解方程,最后通过打印结果展示出c的解以及求解的时间范围和对应的y值。 请注意,以上示例代码是使用Python语言编写,需要安装NumPy和SciPy来支持数值计算求解微分方程。若使用其他的编程语言,可以采用类似的数值方法对一阶时滞方程进行求解,但具体的代码细节会有所不同。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值