22template <
typename F,
typename T>
23T
RK4(
F&& f, T x, units::second_t dt) {
24 const auto h = dt.value();
27 T
k2 = f(x +
h * 0.5 *
k1);
28 T
k3 = f(x +
h * 0.5 *
k2);
31 return x +
h / 6.0 * (
k1 + 2.0 *
k2 + 2.0 *
k3 + k4);
42template <
typename F,
typename T,
typename U>
43T
RK4(
F&& f, T x, U u, units::second_t dt) {
44 const auto h = dt.value();
47 T
k2 = f(x +
h * 0.5 *
k1, u);
48 T
k3 = f(x +
h * 0.5 *
k2, u);
49 T k4 = f(x +
h *
k3, u);
51 return x +
h / 6.0 * (
k1 + 2.0 *
k2 + 2.0 *
k3 + k4);
65template <
typename F,
typename T,
typename U>
66T
RKDP(
F&& f, T x, U u, units::second_t dt,
double maxError = 1
e-6) {
70 constexpr int kDim = 7;
73 constexpr double A[kDim - 1][kDim - 1]{
75 { 3.0 / 40.0, 9.0 / 40.0},
76 { 44.0 / 45.0, -56.0 / 15.0, 32.0 / 9.0},
77 {19372.0 / 6561.0, -25360.0 / 2187.0, 64448.0 / 6561.0, -212.0 / 729.0},
78 { 9017.0 / 3168.0, -355.0 / 33.0, 46732.0 / 5247.0, 49.0 / 176.0, -5103.0 / 18656.0},
79 { 35.0 / 384.0, 0.0, 500.0 / 1113.0, 125.0 / 192.0, -2187.0 / 6784.0, 11.0 / 84.0}};
82 constexpr std::array<double, kDim> b1{
83 35.0 / 384.0, 0.0, 500.0 / 1113.0, 125.0 / 192.0, -2187.0 / 6784.0,
85 constexpr std::array<double, kDim> b2{5179.0 / 57600.0, 0.0,
86 7571.0 / 16695.0, 393.0 / 640.0,
87 -92097.0 / 339200.0, 187.0 / 2100.0,
91 double truncationError;
93 double dtElapsed = 0.0;
94 double h = dt.value();
97 while (dtElapsed < dt.value()) {
104 T
k2 = f(x +
h * (A[0][0] *
k1), u);
105 T
k3 = f(x +
h * (A[1][0] *
k1 + A[1][1] *
k2), u);
106 T k4 = f(x +
h * (A[2][0] *
k1 + A[2][1] *
k2 + A[2][2] *
k3), u);
107 T k5 = f(x +
h * (A[3][0] *
k1 + A[3][1] *
k2 + A[3][2] *
k3 + A[3][3] * k4), u);
108 T k6 = f(x +
h * (A[4][0] *
k1 + A[4][1] *
k2 + A[4][2] *
k3 + A[4][3] * k4 + A[4][4] * k5), u);
113 newX = x +
h * (A[5][0] *
k1 + A[5][1] *
k2 + A[5][2] *
k3 +
114 A[5][3] * k4 + A[5][4] * k5 + A[5][5] * k6);
117 truncationError = (
h * ((b1[0] - b2[0]) *
k1 + (b1[1] - b2[1]) *
k2 +
118 (b1[2] - b2[2]) *
k3 + (b1[3] - b2[3]) * k4 +
119 (b1[4] - b2[4]) * k5 + (b1[5] - b2[5]) * k6 +
120 (b1[6] - b2[6]) * k7))
123 if (truncationError == 0.0) {
124 h = dt.value() - dtElapsed;
126 h *= 0.9 *
std::pow(maxError / truncationError, 1.0 / 5.0);
128 }
while (truncationError > maxError);
Definition: AprilTagPoseEstimator.h:15
T RK4(F &&f, T x, units::second_t dt)
Performs 4th order Runge-Kutta integration of dx/dt = f(x) for dt.
Definition: NumericalIntegration.h:23
T RKDP(F &&f, T x, U u, units::second_t dt, double maxError=1e-6)
Performs adaptive Dormand-Prince integration of dx/dt = f(x, u) for dt.
Definition: NumericalIntegration.h:66
static constexpr const unit_t< compound_unit< energy::joule, time::seconds > > h(6.626070040e-34)
Planck constant.
static constexpr const unit_t< compound_unit< charge::coulomb, inverse< substance::mol > > > F(N_A *e)
Faraday constant.
static constexpr const charge::coulomb_t e(1.6021766208e-19)
elementary charge.
auto pow(const UnitType &value) noexcept -> unit_t< typename units::detail::power_of_unit< power, typename units::traits::unit_t_traits< UnitType >::unit_type >::type, typename units::traits::unit_t_traits< UnitType >::underlying_type, linear_scale >
computes the value of value raised to the power
Definition: base.h:2806
UnitTypeLhs() min(const UnitTypeLhs &lhs, const UnitTypeRhs &rhs)
Definition: base.h:3409
static constexpr uint64_t k1
Definition: Hashing.h:175
static constexpr uint64_t k3
Definition: Hashing.h:177
static constexpr uint64_t k2
Definition: Hashing.h:176