Class: Silicium::NumericalIntegration

Inherits:
Object
  • Object
show all
Defined in:
lib/numerical_integration.rb

Overview

A class providing numerical integration methods

Class Method Summary collapse

Class Method Details

.left_rect_integration(a, b, eps = 0.0001, &block) ⇒ Object

Computes integral by the Left Rectangles method from a to b of block with accuracy eps



58
59
60
# File 'lib/numerical_integration.rb', line 58

def self.left_rect_integration(a, b, eps = 0.0001, &block)
  wrapper_method([a, b], eps, :left_rect_integration_n, &block)
end

.left_rect_integration_n(a, b, n, &block) ⇒ Object

Computes integral by the Left Rectangles method from a to b of block with n segmentations



65
66
67
68
# File 'lib/numerical_integration.rb', line 65

def self.left_rect_integration_n(a, b, n, &block)
  dx = (b - a) / n.to_f
  amount_calculation(a, [0, n], dx, &block)
end

.middle_rectangles(a, b, eps = 0.0001, &block) ⇒ Object

Computes integral by the Middle Rectangles method from a to b of block with accuracy eps



102
103
104
# File 'lib/numerical_integration.rb', line 102

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) ⇒ Object

Computes integral by the Middle Rectangles method from a to b of block with n segmentations



88
89
90
91
92
93
94
95
96
97
# File 'lib/numerical_integration.rb', line 88

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

.right_rect_integration(a, b, eps = 0.0001, &block) ⇒ Object

Computes integral by the Right Rectangles method from a to b of block with accuracy eps



73
74
75
# File 'lib/numerical_integration.rb', line 73

def self.right_rect_integration(a, b, eps = 0.0001, &block)
  wrapper_method([a, b], eps, :right_rect_integration_n, &block)
end

.right_rect_integration_n(a, b, n, &block) ⇒ Object

Computes integral by the Right Rectangles method from a to b of block with n segmentations



80
81
82
83
# File 'lib/numerical_integration.rb', line 80

def self.right_rect_integration_n(a, b, n, &block)
  dx = (b - a) / n.to_f
  amount_calculation(a, [1, n + 1], dx, &block)
end

.simpson_integration(a, b, eps = 0.0001, &block) ⇒ Object

Computes integral by the Simpson’s rule from a to b of block with accuracy eps



51
52
53
# File 'lib/numerical_integration.rb', line 51

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) ⇒ Object

Computes integral by the Simpson’s rule from a to b of block with n segmentations



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/numerical_integration.rb', line 36

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) ⇒ Object

Computes integral by the 3/8 rule from a to b of block with accuracy eps



13
14
15
# File 'lib/numerical_integration.rb', line 13

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) ⇒ Object

Computes integral by the 3/8 rule from a to b of block with n segmentations



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/numerical_integration.rb', line 20

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) ⇒ Object

Computes integral by the Trapezoid method from a to b of block with accuracy eps



124
125
126
# File 'lib/numerical_integration.rb', line 124

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) ⇒ Object

Computes integral by the Trapezoid method from a to b of block with n segmentations



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/numerical_integration.rb', line 109

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