class ChebyRuby::SimpsonsIntegration

This class is for the composite Simpson's rule. The Composite Simpson's rule involves summing Simpson's rule being calculated over a fine mesh.

@attr func [UnivariateFunction] the function over which to integrate.

Attributes

func[RW]

Public Class Methods

new(func) click to toggle source

Basic constructor for SimpsonsIntegration. It can be implemented in two ways. It can either have a UnivariateFunction passed to it or a proc.

@param [UnivariateFunction||Proc] func the function to implement

# File lib/chebyruby/simpsons_integration.rb, line 16
def initialize(func)
  if func.is_a? Proc
    @func = UnivariateFunction.new(&func)
  else
    @func = func
  end
end

Public Instance Methods

accuracy_comparison(a, b, method = 'Trapezoid') click to toggle source

This method compares the accuate of Simpsons to other methods.

@param [Numeric] a the lower bounds of the integral @param [Numeric] b the upper bounds of the integral @param [String] method the method to compare to @return [Float] the absolute difference between the two methods.

# File lib/chebyruby/simpsons_integration.rb, line 46
def accuracy_comparison(a, b, method = 'Trapezoid')
  super.accuracy_comparison(a, b, method, 'Simpsons')
end
integrate(a, b, iterations = 32) click to toggle source

This method integrates the function parameter using the composite simpsons rule.

@param [Numeric] a the lower bounds of the integral @param [Numeric] b the upper bounds of the integral @param [Integer] iterations the number of iterations to be used for integration.

# File lib/chebyruby/simpsons_integration.rb, line 30
def integrate(a, b, iterations = 32)
  h = (b - a) / iterations.to_f
  mesh = (a..b).step(h).to_a[1...-1]
  even = mesh.each_with_index.select{|i, j| j.odd?}.map(&:first)
  odd = mesh.each_with_index.select{|i, j| j.even?}.map(&:first)
  summand = func.value(a) + func.value(b) +
      2 * even.map(&func.func).inject(:+) + 4 * odd.map(&func.func).inject(:+)
  (h / 3.0) * summand
end