Class: Minimization::Bisection

Inherits:
Unidimensional show all
Defined in:
lib/minimization.rb

Overview

Bisection Method for Minimization.

See Unidimensional for methods.

Usage.

require 'minimization'
min=Minimization::Bisection.new(1,2  , proc {|x| (x)**3-(x)-2}
min.iterate
min.x_minimum
min.f_minimum
min.log

Source:

* R.L. Burden, J. Faires: Numerical Analysis

Constant Summary

Constants inherited from Unidimensional

Unidimensional::EPSILON, Unidimensional::MAX_ITERATIONS

Instance Attribute Summary

Attributes inherited from Unidimensional

#epsilon, #expected, #f_minimum, #iterations, #log, #log_header, #x_minimum

Instance Method Summary collapse

Methods inherited from Unidimensional

#f, #initialize, #log_summary, minimize

Constructor Details

This class inherits a constructor from Minimization::Unidimensional

Instance Method Details

#iterateObject


459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/minimization.rb', line 459

def iterate()
  ax = @lower
  cx = @upper
  k = 0;
  while (ax-cx).abs > @epsilon and k<@max_iteration
    bx = (ax + cx).quo(2);
    fa = f(ax);
    fb = f(bx);
    fc = f(cx);
    if (fa*fb <0)
      cx = bx;
    else (fb*fc <0)
      ax = bx;
    end
    k +=1;
    @log << [k, ax.to_f, cx.to_f, f(ax).to_f, f(cx).to_f, (ax-cx).abs.to_f, (f(ax)-f(cx)).abs.to_f]
  end

  if (fa<fc)
    @x_minimum,@f_minimum = ax.to_f, f(ax).to_f;
  else
    @x_minimum,@f_minimum = cx.to_f, f(cx).to_f;
  end

end