6#ifndef THEORETICA_ODE_H
7#define THEORETICA_ODE_H
23 template<
typename Vector = vec<real>>
50#ifndef THEORETICA_NO_PRINT
53 inline std::string
to_string(
const std::string& separator =
" ")
const {
55 if (
t.size() !=
x.size()) {
60 std::stringstream res;
62 for (
unsigned int i = 0; i <
t.size(); ++i) {
64 res <<
t[i] << separator;
66 for (
unsigned int j = 0; j <
x[i].size(); ++j) {
70 if(j !=
x[i].size() - 1)
82 inline operator std::string() {
123 template<
typename Vector>
140 template<
typename Vector,
typename OdeFunction = ode_function<Vector>>
143 return x + h * f(t, x);
156 template<
typename Vector,
typename OdeFunction = ode_function<Vector>>
159 return x + h * f(t + h / 2.0, x + f(t, x) * h / 2.0);
172 template<
typename Vector,
typename OdeFunction = ode_function<Vector>>
175 const Vector k1 = f(t, x);
177 return x + (k1 + f(t + h, x + k1 * h)) * (h / 2.0);
191 template<
typename Vector,
typename OdeFunction = ode_function<Vector>>
194 const Vector k1 = f(t, x);
195 const Vector k2 = f(t + (h / 2.0), x + k1 * (h / 2.0));
211 template<
typename Vector,
typename OdeFunction = ode_function<Vector>>
214 const real half = h / 2.0;
216 const Vector k1 = f(t, x);
217 const Vector k2 = f(t + half, x + k1 * half);
218 const Vector k3 = f(t + half, x + k2 * half);
219 const Vector k4 = f(t + h, x + k3 * h);
221 return x + (k1 + k2 * 2.0 + k3 * 2.0 + k4) * (h / 6.0);
235 template<
typename Vector,
typename OdeFunction = ode_function<Vector>>
238 const Vector k1 = f(t, x);
239 const Vector k2 = f(t + (h / 3.0), x + k1 * (h / 3.0));
240 const Vector k3 = f(t + (h * 2.0 / 3.0), x + h * (-k1 / 3.0 + k2));
241 const Vector k4 = f(t + h, x + h * (k1 - k2 + k3));
243 return x + (k1 + 3.0 * k2 + 3.0 * k3 + k4) * (h / 8.0);
269 typename StepFunction
271 inline ode_solution_t<Vector>
273 OdeFunction f,
const Vector& x0,
real t0,
real tf,
274 StepFunction step,
real stepsize = 0.001) {
282 const unsigned int steps =
floor((tf - t0) / stepsize);
283 unsigned int total_steps = steps;
294 for (i = 1; i <= steps; ++i) {
295 solution.x[i] = step(f, solution.x[i - 1], solution.t[i - 1], stepsize);
296 solution.t[i] = solution.t[i - 1] + stepsize;
302 if (steps != total_steps) {
303 solution.x[i] = step(
304 f, solution.x[i - 1], solution.t[i - 1], tf - solution.t[i - 1]);
327 OdeFunction f,
const Vector& x0,
349 OdeFunction f,
const Vector& x0,
371 OdeFunction f,
const Vector& x0,
394 OdeFunction f,
const Vector& x0,
417 OdeFunction f,
const Vector& x0,
439 OdeFunction f,
const Vector& x0,
Linear algebra type definitions.
A statically allocated N-dimensional vector with elements of the given type.
Definition vec.h:92
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compilation op...
Definition error.h:219
Numerical methods for ordinary differential equations.
Vector step_euler(OdeFunction f, const Vector &x, real t, real h=0.0001)
Compute one step of Euler's method for ordinary differential equations.
Definition ode.h:141
Vector step_k38(OdeFunction f, const Vector &x, real t, real h=0.001)
Compute one step of Kutta's 3/8 rule method for ordinary differential equations.
Definition ode.h:236
ode_solution_t< real > ode_solution1d
The solution of an ODE in 1 variable.
Definition ode.h:103
ode_solution_t< vec4 > ode_solution4d
The solution of an ODE in 4 variables.
Definition ode.h:115
Vector step_rk2(OdeFunction f, const Vector &x, real t, real h=0.001)
Compute one step of the Runge-Kutta method of 2nd order for ordinary differential equations.
Definition ode.h:192
ode_solution_t< Vector > solve_midpoint(OdeFunction f, const Vector &x0, real t0, real tf, real stepsize=0.0001)
Integrate an ordinary differential equation over a certain domain with the given initial conditions u...
Definition ode.h:348
std::function< Vector(real, const Vector &)> ode_function
A function representing a system of differential equations, taking as input the time (independent var...
Definition ode.h:124
ode_solution_t< Vector > solve_k38(OdeFunction f, const Vector &x0, real t0, real tf, real stepsize=0.0001)
Integrate an ordinary differential equation over a certain domain with the given initial conditions u...
Definition ode.h:438
ode_solution_t< Vector > solve_rk4(OdeFunction f, const Vector &x0, real t0, real tf, real stepsize=0.01)
Integrate an ordinary differential equation over a certain domain with the given initial conditions u...
Definition ode.h:416
Vector step_midpoint(OdeFunction f, const Vector &x, real t, real h=0.0001)
Compute one step of the midpoint method for ordinary differential equations.
Definition ode.h:157
ode_solution_t< Vector > solve_fixstep(OdeFunction f, const Vector &x0, real t0, real tf, StepFunction step, real stepsize=0.001)
Integrate an ordinary differential equation using any numerical algorithm with a constant step size,...
Definition ode.h:272
Vector step_rk4(OdeFunction f, const Vector &x, real t, real h=0.01)
Compute one step of the Runge-Kutta method of 4th order for ordinary differential equations.
Definition ode.h:212
ode_solution_t< Vector > solve_heun(OdeFunction f, const Vector &x0, real t0, real tf, real stepsize=0.0001)
Integrate an ordinary differential equation over a certain domain with the given initial conditions u...
Definition ode.h:370
ode_solution_t< vec3 > ode_solution3d
The solution of an ODE in 3 variables.
Definition ode.h:111
Vector step_heun(OdeFunction f, const Vector &x, real t, real h=0.001)
Compute one step of Heun's method for ordinary differential equations.
Definition ode.h:173
ode_solution_t< Vector > solve_rk2(OdeFunction f, const Vector &x0, real t0, real tf, real stepsize=0.0001)
Integrate an ordinary differential equation over a certain domain with the given initial conditions u...
Definition ode.h:393
ode_solution_t< vec2 > ode_solution2d
The solution of an ODE in 2 variables.
Definition ode.h:107
ode_solution_t< Vector > solve_euler(OdeFunction f, const Vector &x0, real t0, real tf, real stepsize=0.0001)
Integrate an ordinary differential equation over a certain domain with the given initial conditions u...
Definition ode.h:326
ode_solution_t< vec< real > > ode_solution
The solution of an ODE with any number of variables.
Definition ode.h:99
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:242
double real
A real number, defined as a floating point type.
Definition constants.h:207
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:74
@ InvalidArgument
Invalid argument.
Definition error.h:32
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:216
TH_CONSTEXPR int floor(real x)
Compute the floor of x, as the maximum integer number that is smaller than x.
Definition real_analysis.h:271
Data structure holding the numerical solution of a discretized ODE, where the vector represents the ...
Definition ode.h:24
friend std::ostream & operator<<(std::ostream &out, const ode_solution_t< Vector > &obj)
Stream the ODE solution in string representation to an output stream (std::ostream).
Definition ode.h:89
vec< real > t
Definition ode.h:27
ode_solution_t(size_t steps, const Vector &x0, real t0)
Prepare the structure for integration by specifying the number of total steps and the initial conditi...
Definition ode.h:40
ode_solution_t()
Default constructor.
Definition ode.h:34
std::string to_string(const std::string &separator=" ") const
Convert the ODE solution to string representation.
Definition ode.h:53
vec< Vector > x
Definition ode.h:30