class Silicium::NumericalIntegration

A class providing numerical integration methods

Public Class Methods

left_rect_integration(left_p, right_p, eps = 0.0001, &block) click to toggle source

Left Rectangle Method and Right Rectangle Method

# File lib/numerical_integration.rb, line 48
def self.left_rect_integration(left_p, right_p, eps = 0.0001, &block)
  splits = 1
  res1 = left_rect_integration_n(left_p, right_p, 1, &block)
  res2 = left_rect_integration_n(left_p, right_p, 5, &block)
  while (res1 - res2).abs > eps
    res1 = left_rect_integration_n(left_p, right_p, splits, &block)
    splits *= 5
    res2 = left_rect_integration_n(left_p, right_p, splits, &block)
  end
  (res1 + res2) / 2.0
end
left_rect_integration_n(left_p, right_p, splits, &block) click to toggle source

Left Rectangle Auxiliary Method and Right Rectangle Auxiliary Method

# File lib/numerical_integration.rb, line 61
def self.left_rect_integration_n(left_p, right_p, splits, &block)
  dx = (right_p - left_p) / splits.to_f
  result = 0
  i = 0
  while i < splits
    result += block.call(left_p + i * dx)
    i += 1
  end
  result * dx
end
middle_rectangles(a, b, eps = 0.0001, &block) click to toggle source

Middle Rectangles Method with specified accuracy

# File lib/numerical_integration.rb, line 86
def self.middle_rectangles(a, b, eps = 0.0001, &block)
  wrapper_method([a, b], eps, :middle_rectangles_with_a_segment, &block)
end
middle_rectangles_with_a_segment(a, b, n, &block) click to toggle source

Middle Rectangles Method with a segment

# File lib/numerical_integration.rb, line 74
def self.middle_rectangles_with_a_segment(a, b, n, &block)
  dx = (b - a) / n.to_f
  result = 0
  i = 0
  n.times do
    result += block.call(a + dx * (i + 1 / 2)) * dx
    i += 1
  end
  result
end
simpson_integration(a, b, eps = 0.0001, &block) click to toggle source

Simpson integration with specified accuracy

# File lib/numerical_integration.rb, line 43
def self.simpson_integration(a, b, eps = 0.0001, &block)
  wrapper_method([a, b], eps, :simpson_integration_with_a_segment, &block)
end
simpson_integration_with_a_segment(a, b, n, &block) click to toggle source

Simpson integration with a segment

# File lib/numerical_integration.rb, line 30
def self.simpson_integration_with_a_segment(a, b, n, &block)
  dx = (b - a) / n.to_f
  result = 0
  i = 0
  while i < n
    result += (block.call(a + i * dx) + 4 * block.call(((a + i * dx) +
        (a + (i + 1) * dx)) / 2.0) + block.call(a + (i + 1) * dx)) / 6.0 * dx
    i += 1
  end
  result
end
three_eights_integration(a, b, eps = 0.0001, &block) click to toggle source

Computes integral from a to b of block with accuracy eps

# File lib/numerical_integration.rb, line 10
def self.three_eights_integration(a, b, eps = 0.0001, &block)
  wrapper_method([a, b], eps, :three_eights_integration_n, &block)
end
three_eights_integration_n(a, b, n, &block) click to toggle source

Computes integral from a to b of block with n segmentations

# File lib/numerical_integration.rb, line 15
def self.three_eights_integration_n(a, b, n, &block)
  dx = (b - a) / n.to_f
  result = 0
  x = a
  n.times do
    result +=
      (block.call(x) + 3 * block.call((2 * x + x + dx) / 3.0) +
          3 * block.call((x + 2 * (x + dx)) / 3.0) + block.call(x + dx)) / 8.0 * dx
    x += dx
  end
  result
end
trapezoid(a, b, eps = 0.0001, &block) click to toggle source

Trapezoid Method with specified accuracy

# File lib/numerical_integration.rb, line 105
def self.trapezoid(a, b, eps = 0.0001, &block)
  wrapper_method([a, b], eps, :trapezoid_with_a_segment ,&block)
end
trapezoid_with_a_segment(a, b, n, &block) click to toggle source

Trapezoid Method with a segment

# File lib/numerical_integration.rb, line 92
def self.trapezoid_with_a_segment(a, b, n, &block)
  dx = (b - a) / n.to_f
  result = 0
  i = 1
  (n - 1).times do
    result += block.call(a + dx * i)
    i += 1
  end
  result += (block.call(a) + block.call(b)) / 2.0
  result * dx
end