module
GSL::ODE
Overview
This module implements Ordinary Differential Equations solving (initial value problems)
Usage examples:
For systems without Jacobian information:
class TestODE < GSL::ODE::System
def initialize(@k : Float64)
super(2) # dimension of system
end
# Override `function` with your system (evaluate dy/dt = f(t, y))
def function(t, y, dydt)
dydt[0] = y[1]
dydt[1] = -@k * y[0]
end
end
# Evolve system using adaptive step, returns two arrays
y0 = [1, 0]
y, t = ode.evolve(y0, 0, Math::PI/2)
# Evolve system using given time points, return array of y states at this points
y0 = [0, 10]
results = ode.evolve(y0, [0, Math::PI/2, Math::PI, 3*Math::PI/2, Math::PI*2])
# Evolve system using fixed time step, result same as previuos
y0 = [0, 10]
results = ode.evolve(y0, 0, Math::PI*2, Math::PI/2)
# Yielding versions of all above are available
y0 = [1, 0]
ode.evolve(y0, 0, Math::PI/2) do |y, t|
puts "time=#{t}: state #{y}"
end
When Jacobian information is available, use JacobianSystem:
class TestODEJacobian < GSL::ODE::JacobianSystem
def initialize(@mu : Float64)
super(2) # dimension of system
end
# evaluate dy/dt = f(t, y) here
def function(t, y, dydt)
dydt[0] = y[1]
dydt[1] = -y[0] - @mu * y[1]*(y[0]*y[0] - 1)
end
# evaluate Jacobian ∂f/∂y and ∂f/∂t here
def jacobian(t, y, dfdy, dfdt)
dfdy[0] = 0
dfdy[1] = 1.0
dfdy[2] = -2.0*@mu*y[0]*y[1] - 1.0
dfdy[3] = -@mu*(y[0]*y[0] - 1.0)
dfdt[0] = 0.0
dfdt[1] = 0.0
end
end