Sha256: dec552ee8c1b08b463ebc214b13a190a7fe3d0e3e1e9d4b9b51fae0a832ca3b7

Contents?: true

Size: 859 Bytes

Versions: 16

Compression:

Stored size: 859 Bytes

Contents

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

f = Proc.new { |x, params| 
  a = params[0]
  b = params[1]
  c = params[2]
  (a*x + b)*x + c
}

df = Proc.new { |x, params| 
  a = params[0]
  b = params[1]
  2.0*a*x + b
}

expected = sqrt(5.0)
x = 5.0

function_fdf = GSL::Function_fdf.alloc(f, df)
function_fdf.set_params([1.0, 0, -5])

solver = GSL::Root::FdfSolver.alloc(GSL::Root::FdfSolver::NEWTON)
puts "using #{solver.name} method"

solver.set(function_fdf, x)

printf("%-5s %10s %10s %10s\n",
          "iter", "root", "err", "err(est)")
iter = 0
status = nil
while status != GSL::SUCCESS
  iter += 1
  status = solver.iterate
  x0 = x
  x = solver.root
  status = GSL::Root::test_delta(x, x0, 0, 1e-3)

  if status == GSL::SUCCESS
    printf("Converged:\n")
  end
  printf("%5d %10.7f %+10.7f %10.7f\n",
         iter, x, x - expected, x - x0)

end

Version data entries

16 entries across 16 versions & 4 rubygems

Version Path
rb-gsl-1.16.0.4 examples/roots/newton.rb
rb-gsl-1.16.0.3 examples/roots/newton.rb
rb-gsl-1.16.0.3.rc1 examples/roots/newton.rb
gsl-nmatrix-1.15.3.2 examples/roots/newton.rb
gsl-nmatrix-1.15.3.1 examples/roots/newton.rb
rb-gsl-1.16.0.2 examples/roots/newton.rb
rb-gsl-1.16.0.1 examples/roots/newton.rb
rb-gsl-1.16.0 examples/roots/newton.rb
rb-gsl-1.15.3.2 examples/roots/newton.rb
rb-gsl-1.15.3.1 examples/roots/newton.rb
blackwinter-gsl-1.15.3.2 examples/roots/newton.rb
gsl-nmatrix-1.15.3.0 examples/roots/newton.rb
gsl-1.15.3 examples/roots/newton.rb
gsl-1.14.7 examples/roots/newton.rb
gsl-1.14.6 examples/roots/newton.rb
gsl-1.14.5 examples/roots/newton.rb