Sha256: 22f670e160f395f73f3e26b9797f5918622989cfb7b84fd058c554b5653ca07c

Contents?: true

Size: 532 Bytes

Versions: 1

Compression:

Stored size: 532 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
      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) ) rescue binding.pry
      end
      ( h * sum ) / 6.0
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dydx-0.1.0 lib/dydx/integrand.rb