#!/usr/bin/env ruby
require("gsl")

proc = Proc.new { |x, params, f|
  a = params[0]
  b = params[1]
  x0 = x[0]
  x1 = x[1]
  f[0] = a*(1 - x0)
  f[1] = b*(x1 - x0*x0)
}

params = [1.0, 10.0]
f = MultiRoot::Function.new(proc, 2, params)

#fsolver = MultiRoot::FSolver.new("hybrids", 2)
#fsolver = MultiRoot::FSolver.new("hybrid", 2)
#fsolver = MultiRoot::FSolver.new("dnewton", 2)
fsolver = MultiRoot::FSolver.new("broyden", 2)

x = Vector.new(-10.0, -5.0)
fsolver.set(f, x)
#p fsolver.name
#p fsolver.x

iter = 0
begin
  iter += 1
  status = fsolver.iterate
  root = fsolver.root
  f = fsolver.f
  printf("iter = %3u x = % .3f % .3f f(x) = % .3e % .3e\n",
          iter, root[0], root[1], f[0], f[1])
  status = fsolver.test_residual(1e-7)
end while status == GSL::CONTINUE and iter < 1000
