Sha256: 4d053801209d581f529b3ae1595d4eb4a0731154645f6768a87fa5d42c957bcf

Contents?: true

Size: 857 Bytes

Versions: 6

Compression:

Stored size: 857 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

6 entries across 6 versions & 2 rubygems

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