How-to: solve an ODE with an implicit method#

Note

You can also see Very first steps with Lorenz system tutorial.

We would like to solve the following equation

\[\dot{y} = -y,\qquad y(0) = 1\]

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). We also need to define the Jacobian function of this function \(J_f:(t,y)\mapsto \partial_y f(t,y) = -1\), in ponio we define this function as df(double t, double y)->double. We store this two functions in a ponio::implicit_problem class:

20    auto pb = ponio::make_implicit_problem(
21        []( double /* t */, double y, double& dy )
22        {
23            dy = -y;
24        },
25        []( double /* t */, double /* y */ )
26        {
27            return -1.;
28        } );

We also define initial condition and time step:

30    double const y0 = 1.0;
31    double const dt = 0.1;

Next we solve this equation with explicit Euler method defines in ponio as ponio::runge_kutta::euler.

33    ponio::solve( pb, ponio::runge_kutta::backward_euler(), y0, { 0., 2.0 }, dt, "how_to_solve_impl_exp.txt"_fobs );

In this line we call ponio::solve() function, with a observer::file_observer to store the output in how_to_solve_impl_exp.txt text file.

See also

The full example can be found in how_to_solve_impl_exp.cpp.