6#ifndef THEORETICA_ODE_H
7#define THEORETICA_ODE_H
9#include "../algebra/vec.h"
22 template<
typename Vector = vec<real>>
49#ifndef THEORETICA_NO_PRINT
54 if (
t.
size() !=
x.size()) {
59 std::stringstream
res;
61 for (
unsigned int i = 0; i <
t.
size(); ++i) {
65 for (
unsigned int j = 0;
j <
x[i].size(); ++
j) {
69 if(
j !=
x[i].size() - 1)
81 inline operator std::string() {
90 return out <<
obj.to_string();
122 template<
typename Vector>
139 template<
typename Vector,
typename OdeFunction = ode_function<Vector>>
142 return x +
h * f(t, x);
155 template<
typename Vector,
typename OdeFunction = ode_function<Vector>>
158 return x +
h * f(t +
h / 2.0, x + f(t, x) *
h / 2.0);
171 template<
typename Vector,
typename OdeFunction = ode_function<Vector>>
176 return x + (
k1 + f(t +
h, x +
k1 *
h)) * (
h / 2.0);
190 template<
typename Vector,
typename OdeFunction = ode_function<Vector>>
210 template<
typename Vector,
typename OdeFunction = ode_function<Vector>>
220 return x + (
k1 +
k2 * 2.0 +
k3 * 2.0 +
k4) * (
h / 6.0);
234 template<
typename Vector,
typename OdeFunction = ode_function<Vector>>
239 const Vector k3 = f(t + (
h * 2.0 / 3.0), x +
h * (-
k1 / 3.0 +
k2));
242 return x + (
k1 + 3.0 *
k2 + 3.0 *
k3 +
k4) * (
h / 8.0);
293 for (i = 1; i <=
steps; ++i) {
A statically allocated N-dimensional vector with elements of the given type.
Definition vec.h:92
void resize(size_t n) const
Compatibility function to allow for allocation or resizing of dynamic vectors.
Definition vec.h:459
TH_CONSTEXPR unsigned int size() const
Returns the size of the vector (N)
Definition vec.h:449
#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
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:140
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:235
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:191
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:123
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:347
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:437
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:415
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:156
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:271
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:211
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:369
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:172
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:392
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:325
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
double real
A real number, defined as a floating point type.
Definition constants.h:207
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:242
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:74
@ InvalidArgument
Invalid argument.
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:23
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:88
vec< real > t
A vector of the time values (independent variable).
Definition ode.h:26
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:39
ode_solution_t()
Default constructor.
Definition ode.h:33
std::string to_string(const std::string &separator=" ") const
Convert the ODE solution to string representation.
Definition ode.h:52
vec< Vector > x
A vector of the phase space values (solution).
Definition ode.h:29