How-to: solve a simple ODE#
Note
You can also see Very first steps with Lorenz system tutorial.
We would like to solve the following equation
We first define the function \(f:(t,y)\mapsto f(t, y) = -y\), in ponio we define this function as f(double t, double y, double& dy), where the current state y can be capture by value, reference or constant reference.
19 auto f = []( double /* t */, double y, double& dy )
20 {
21 dy = -y;
22 };
We also define initial condition and time step:
24 double const y0 = 1.0;
25 double const dt = 0.1;
Next we solve this equation with explicit Euler method defines in ponio as ponio::runge_kutta::euler.
27 ponio::solve( f, ponio::runge_kutta::euler(), y0, { 0., 2.0 }, dt, "how_to_solve_exp.txt"_fobs );
In this line we call ponio::solve() function, with a observer::file_observer to store the output in how_to_solve_exp.txt text file.
Note
To use the _fobs literal you need to using the correct namespace
using namespace ponio::observer; // to use _fobs litteral
See also
The full example can be found in how_to_solve_exp.cpp.