3#include "math/eigen_interface.h"
34template <
int X,
int U>
35using WithInputDerivative =
36 std::function<Eigen::Vector<double, X>(
const Eigen::Vector<double, X> &,
const Eigen::Vector<double, U> &)>;
39using WithoutInputDerivative = std::function<Eigen::Vector<double, X>(
const Eigen::Vector<double, X> &)>;
42using TimeVariantDerivative = std::function<Eigen::Vector<double, Y>(
const double &,
const Eigen::Vector<double, Y> &)>;
57template <
int X,
int U>
58Eigen::Vector<double, X> euler_with_input(
59 const WithInputDerivative<X, U> &f,
const Eigen::Vector<double, X> &x,
const Eigen::Vector<double, U> &u,
62 Eigen::Vector<double, X> k1 = f(x, u);
80Eigen::Vector<double, X>
81euler_without_input(
const WithoutInputDerivative<X> &f,
const Eigen::Vector<double, X> &x,
const double &h) {
82 Eigen::Vector<double, X> k1 = f(x);
101Eigen::Vector<double, Y> euler_time_variant(
102 const TimeVariantDerivative<Y> &f,
const double &t,
const Eigen::Vector<double, Y> &y,
const double &h
104 Eigen::Vector<double, Y> k1 = f(t, y);
123template <
int X,
int U>
124Eigen::Vector<double, X> RK2_with_input(
125 const WithInputDerivative<X, U> &f,
const Eigen::Vector<double, X> &x,
const Eigen::Vector<double, U> &u,
128 Eigen::Vector<double, X> k1 = f(x, u);
129 Eigen::Vector<double, X> k2 = f(x + h * 0.5 * k1, u);
148Eigen::Vector<double, X>
149RK2_without_input(
const WithoutInputDerivative<X> &f,
const Eigen::Vector<double, X> &x,
const double &h) {
150 Eigen::Vector<double, X> k1 = f(x);
151 Eigen::Vector<double, X> k2 = f(x + h * 0.5 * k1);
171Eigen::Vector<double, Y> RK2_time_variant(
172 const TimeVariantDerivative<Y> &f,
const double &t,
const Eigen::Vector<double, Y> &y,
const double &h
174 Eigen::Vector<double, Y> k1 = f(t, y);
175 Eigen::Vector<double, Y> k2 = f(t + h * 0.5, y + h * 0.5 * k1);
196template <
int X,
int U>
197Eigen::Vector<double, X> RK4_with_input(
198 const WithInputDerivative<X, U> &f,
const Eigen::Vector<double, X> &x,
const Eigen::Vector<double, U> &u,
201 Eigen::Vector<double, X> k1 = f(x, u);
202 Eigen::Vector<double, X> k2 = f(x + h * 0.5 * k1, u);
203 Eigen::Vector<double, X> k3 = f(x + h * 0.5 * k2, u);
204 Eigen::Vector<double, X> k4 = f(x + h * k3, u);
206 return x + h / 6.0 * (k1 + 2.0 * k2 + 2.0 * k3 + k4);
225Eigen::Vector<double, X>
226RK4_without_input(
const WithoutInputDerivative<X> &f,
const Eigen::Vector<double, X> &x,
const double &h) {
227 Eigen::Vector<double, X> k1 = f(x);
228 Eigen::Vector<double, X> k2 = f(x + h * 0.5 * k1);
229 Eigen::Vector<double, X> k3 = f(x + h * 0.5 * k2);
230 Eigen::Vector<double, X> k4 = f(x + h * k3);
232 return x + h / 6.0 * (k1 + 2.0 * k2 + 2.0 * k3 + k4);
252Eigen::Vector<double, Y> RK4_time_variant(
253 const TimeVariantDerivative<Y> &f,
const double &t,
const Eigen::Vector<double, Y> &y,
const double &h
255 Eigen::Vector<double, Y> k1 = f(t, y);
256 Eigen::Vector<double, Y> k2 = f(t + h * 0.5, y + h * 0.5 * k1);
257 Eigen::Vector<double, Y> k3 = f(t + h * 0.5, y + h * 0.5 * k2);
258 Eigen::Vector<double, Y> k4 = f(t + h, y + h * k3);
260 return y + h / 6.0 * (k1 + 2.0 * k2 + 2.0 * k3 + k4);