lib/more_math/newton_bisection.rb
# Return a bracket around a root, starting from the initial +range+. The
# method returns nil, if no such bracket around a root could be found after
# +n+ tries with  the scaling +factor+.
Return a bracket around a root, starting from the initial +range+. The
method returns nil, if no such bracket around a root could be found after
+n+ tries with  the scaling +factor+.
Return a bracket around a root starting from the initial + range + . The method returns nil if no such bracket around a root could be found after + n + tries with the scaling + factor + .
def bracket(range = -1..1, n = 50, factor =  1.6)
      x1, x2 = range.first.to_f, range.last.to_f
      x1 >= x2 and raise ArgumentError, "bad initial range #{range}"
      f1, f2 = @function[x1], @function[x2]
      n.times do
        f1 * f2 < 0 and return x1..x2
        if f1.abs < f2.abs
          f1 = @function[x1 += factor * (x1 - x2)]
        else
          f2 = @function[x2 += factor * (x2 - x1)]
        end
      end
      return
    end
def bracket ( range = - 1 .. 1 , n = 50 , factor = 1.6 ) x1 , x2 = range . first . to_f , range . last . to_f x1 >= x2 and raise ArgumentError , "bad initial range #{range}" f1 , f2 = @function [ x1 ] , @function [ x2 ] n . times do f1 * f2 < 0 and return x1 .. x2 if f1 . abs < f2 . abs f1 = @function [ x1 += factor * ( x1 - x2 ) ] else f2 = @function [ x2 += factor * ( x2 - x1 ) ] end end return end
Return a bracket around a root, starting from the initial +range+.
Return a bracket around a root starting from the initial + range + .
range
<not_specific>
n
<not_specific>
factor
<not_specific>