This chapter describes functions for solving ordinary differential equation (ODE) initial value problems. GSL Shell gives you access to a variety of methods provided by the GSL library, such as Runge-Kutta and Bulirsch-Stoer routines with an high-level interface and an adaptive step-size control. The interface can be used to easily achieve the desired solution, with full access to any intermediate steps.
In GSL Shell an ODE system is integrated by using an ode solver object. This kind of objects store internally the state of the solver and you can advance the solution step-by-step until, eventually, the desired value of t is reached.
An ODE solver allows users to obtain a numerical solution of an Ordinary Differential Equation (ODE) system. The ODE solver lets you solve the general n-dimensional first-order system,
for . The stepping functions rely on the
vector of derivatives
and the Jacobian matrix,
Here an examples about the usage of an ODE solver for real numbers:
mu = 10
-- define the ODE function
function odef(t,y,f)
f:set(1,1, y[2])
f:set(2,1, -y[1] - mu*y[2]*(y[1]*y[1]-1))
end
-- create the ODE solver
s = ode {f = odef, n= 2, eps_abs= 1e-6}
-- we define initial values
t0, t1 = 0, 100
y0 = vector {1,0}
-- the ODE solver is iterated tiil the time t1 is reached
for t, y in s:iter(t0, y0, t1) do
print(t, y:row_print())
end
and here an example with complex numbers:
m = cmatrix {{4i, 0},{-0.3, 3i}}
function myf(t, y, f)
set(f, cmul(m, y))
end
function mydf(t, y, dfdy, dfdt)
set(dfdy, m)
null(dfdt)
end
s = code {f= myf, df= mydf, n= 2, method='bsimp'}
t0, t1 = 0, 5
y0 = cvector {1,0}
for t, y in s:iter(t0, y0, t1, 0.05) do
print(t, y:row_print())
end
Solver of ODE system.
Create a new solver for an ODE system. The spec should be a table containing the following fields:
The low-level integration method used. Can be choosed between:
Provides an iterators that can be used in a for loop. The iterators returns the couple (t, y) at each step and terminate when t1 is reached.
The method iter() is defined with the following function:
function ode_iter(s, t0, y0, t1, tstep)
s:set(t0, y0)
return function()
local t, y = s.t, s.y
if t < t1 then
s:evolve(t1, tstep)
return t, y
end
end
end