Sha256: a9ab5b1e7e3afe819eae212af0ef3963ccb86ab4009d6997c14af2e5d61c6569

Contents?: true

Size: 645 Bytes

Versions: 6

Compression:

Stored size: 645 Bytes

Contents

module Dydx
  class Integrand
    attr_accessor :function, :var
    def initialize(function, var)
      @function = function
      @var = var
    end

    def [](a, b, n = 100)
      f = function
      a, b = [a, b].map(&:to_f)
      raise ArgumentError, 'b should be greater than a' if a > b
      # HOT FIX: should implement Infinity class
      a = - 1000 if a == - Float::INFINITY
      b = 1000 if b == Float::INFINITY

      h = (b - a) / n
      sum = 0.0
      xi = ->(i){ a + h * i }
      n.to_i.times do |i|
        sum += ( f(xi.(i)) + 4.0 * f(xi.(i) + h / 2.0 ) + f(xi.(i) + h) )
      end
      ( h * sum ) / 6.0
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dydx-0.1.3 lib/dydx/integrand.rb
dydx-0.1.29 lib/dydx/integrand.rb
dydx-0.1.28 lib/dydx/integrand.rb
dydx-0.1.25 lib/dydx/integrand.rb
dydx-0.1.2 lib/dydx/integrand.rb
dydx-0.1.1 lib/dydx/integrand.rb