package owl-ode

  1. Overview
  2. Docs
On This Page
  1. Example of use
Legend:
Library
Module
Module type
Parameter
Class
Class type

Tutorial

Owl_ode is a lightweight package for solving ordinary differential equations. Built on top of Owl’s numerical library, Owl_ode was designed with extensibility and ease of use in mind and includes a number of classic ode solvers (e.g. Euler and Runge-Kutta, in both adaptive and fixed-step variants) and symplectic sovlers (e.g. Leapfrog), with more to come.

This library provides a collection of solvers for the initial value problem for ordinary differential equation systems.

You can jump to the interface of the Owl_ode.Ode.odelib.

Example of use

Let's solve the linear initial value problem \(\dot{y} = Ay\), with \(y(t_0) = y_0\). Say that A is the matrix ((1;-1); (2;3)), and the initial conditions are given by y(0) = (-1;1).

We begin by defining a function \(f(y, t)\) that corresponds to the RHS of the differential equation

let f y t = 
    let a = [|[|1.; -1.|];
            [|2.; -3.|]|]
        |> Owl.Mat.of_arrays
in
Owl.Mat.(a *@ y)

and the initial condition \(y0\)

let y0 = Mat.of_array [|-1.; 1.|] 2 1

Before being able to actually call the integrating function, we need to define the time specification for the problem at hand

let tspec = Owl_ode.Types.(T1 {t0 = 0.; duration = 2.; dt=1E-3})

This in particular allows us to specify also that \(t_0=0\). Here, we construct a record using the constructor Owl_ode.Types.T1, which includes the start time \(t_0\), the time duration for the numerical solution, and a step size dt.

Finally we can call

let ts, ys = Owl_ode.(
    odeint Native.D.rk4 f y0 tspec ()
)

to get an array with the approximate value of the vector y at the times ts. As you can see from the snippet above, you have to specify the algorithm used for the integration by providing its module in the function call. Here, we integrated the dynamical system with Owl_ode.Native.D.rk4, a fixed-step double-precision Runge-Kutta solver. In Owl_ode, we provide a number of ocaml-based double-precision solvers in the Owl_ode.Native.D modeuoe and single-precision ones in Owl_ode.Native.S. Additional integrators are provided by external and third party libraries.

The solution can be easily plotted using Owl_plplot or any other owl-compatible plotting library, for example

let open Owl_plplot in
let h = Plot.create "myplot.png" in
Plot.plot ~h ~spec:[ RGB (0,0,255); LineStyle 1 ] ts (Mat.col ys 0);
Plot.output h;

You can refer to the examples in the source repository for more complex examples.

  • version 0.1
  • author Marcello Seri
  • author Ta-Chu Kao
OCaml

Innovation. Community. Security.