Sha256: a1d1aa235ded6f25308f0d21803bfee82d0ead8bdfb2698cea40da141f1d1e99

Contents?: true

Size: 957 Bytes

Versions: 6

Compression:

Stored size: 957 Bytes

Contents

#!/usr/bin/env ruby
require("gsl")
solver = GSL::Root::FSolver.alloc(GSL::Root::FSolver::BISECTION)
f = GSL::Function.alloc { |x, params|
  a = params[0]; b = params[1]; c = params[2]
  (a*x + b)*x + c
}
f.set_params(1, 0, -5)
expected = Math::sqrt(5.0)
printf("%5s [%9s, %9s] %9s %10s %9s\n", "iter",
       "lower", "upper", "root", "err", "err(est)")
solver.set(f, 0.0, 5.0)
iter = 0; status = nil
IO.popen("graph -T X -C -g 3 -X Iterations -Y 'Root'\
         --toggle-rotate-y-label\
         -L 'x^2 - 5 = 0, #{solver.name}' -I e -S 4", "w") do |io|
  while status != GSL::SUCCESS
    iter += 1
    status = solver.iterate
    r = solver.root
    xl = solver.x_lower; xu = solver.x_upper
    status = solver.test_interval(0, 0.001)
    if status == GSL::SUCCESS; printf("Converged:\n"); end
    printf("%5d [%.7f, %.7f] %.7f %+.7f %.7f\n",
           iter, xl, xu, r, r - expected, xu - xl)
    io.printf("%d %e %e\n", iter, r, (xu-xl)/2)
  end
end


Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
gsl-2.1.0.3 examples/roots/demo.rb
gsl-2.1.0.2 examples/roots/demo.rb
gsl-2.1.0.1 examples/roots/demo.rb
gsl-2.1.0 examples/roots/demo.rb
gsl-1.16.0.6 examples/roots/demo.rb
rb-gsl-1.16.0.5 examples/roots/demo.rb