#!/usr/bin/env ruby
# An oscillator
#   m: mass
#   k: spring constant
#   b: resist
#   f: external force

require("gsl") 

dim = 2
m = 1.0
b = 1.0
f = 1.0
k = 10.0

# x[0]: displacement, x[1]: velocity
func = Proc.new { |t, x, dxdt|
  dxdt[0] = x[1]
  dxdt[1] = (f - b*x[1] - k*x[0])/m
}

gos = Odeiv::Solver.new(Odeiv::Step::RKF45, [1e-6, 0.0], func, dim)

t = 0.0; tend = 10.0
h = 1e-6
x = Vector.new([0.0, 0.0])

GSL::ieee_env_setup()

while t < tend
  status = gos.apply(t, tend, h, x)

  break if status != GSL::SUCCESS

  printf("%.5e %.5e %.5e %.5e\n", t, x[0], x[1], h)
end

