Module: Integrators

Defined in:
lib/linmeric/Integrators.rb

Overview

This module provides some method to integrate a function

Author

Massimiliano Dal Mas ([email protected])

License

Distributed under MIT license

Class Method Summary collapse

Class Method Details

.boole(a, b, n) ⇒ Object

Implementation of Boole’s method

  • argument: left range value

  • argument: right range value

  • argument: number of integration points

  • returns: result of the operation (Numeric)



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

def self.boole(a,b,n)
  h = (b - a) / (4 * n.to_f)
  part = 0
  for i in 0..n-1
    part +=  7 * yield(a + (h * 4 * i)) +
            32 * yield(a + (h * (4 * i + 1))) +
            12 * yield(a + (h * (4 * i + 2))) +
            32 * yield(a + (h * (4 * i + 3))) +
             7 * yield(a + (h * (4 * i + 4)))
  end
  return (2 * h) / 45.0 * part
end

.midpoint(a, b, n) ⇒ Object

Implementation of midpoint method

  • argument: left range value

  • argument: right range value

  • argument: number of integration points

  • returns: result of the operation (Numeric)



38
39
40
41
42
43
44
45
46
# File 'lib/linmeric/Integrators.rb', line 38

def self.midpoint(a,b,n)
  parz = 0
  h = (b - a) / n.to_f
  for i in 0...n
    f1 = yield(a + (i + 0.5) * h)
    parz += f1
  end
  return h * parz
end

.rettdx(a, b, n) ⇒ Object

Implementation of right rectangles method

  • argument: left range value

  • argument: right range value

  • argument: number of integration points

  • returns: result of the operation (Numeric)



94
95
96
97
98
99
100
101
# File 'lib/linmeric/Integrators.rb', line 94

def self.rettdx(a,b,n)
  h = (b - a) / n.to_f
  area = 0
  for i in 1..n
    area += yield(a + (h * i)) * h
  end
  return area
end

.rettsx(a, b, n) ⇒ Object

Implementation of left rectangles method

  • argument: left range value

  • argument: right range value

  • argument: number of integration points

  • returns: result of the operation (Numeric)



79
80
81
82
83
84
85
86
# File 'lib/linmeric/Integrators.rb', line 79

def self.rettsx(a,b,n)
  h = (b - a) / n.to_f
  area = 0
  for i in 0..n-1
    area += yield(a + (h * i)) * h
  end
  return area
end

.simpson(a, b, n) ⇒ Object

Implementation of Simpson’s method

  • argument: left range value

  • argument: right range value

  • argument: number of integration points

  • returns: result of the operation (Numeric)



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/linmeric/Integrators.rb', line 54

def self.simpson(a,b,n)
  hs = (b - a) / (n * 2.0)
  intSA = 0
  intSB = 0
  for i in 1..n-1
    xa = a + (2 * i) * hs
    intSA += yield(xa)
  end
  
  for i in 0..n-1
    xb = a + (2 * i + 1) * hs
    intSB += yield(xb)
  end
  fa = yield(a)
  fb = yield(b)
  intS = hs / 3.0 * (fa + fb + 2 * intSA + 4 * intSB)
  return intS
end

.trapezi(a, b, n) ⇒ Object

Implementation of trapezes method

  • argument: left range value

  • argument: right range value

  • argument: number of integration points

  • returns: result of the operation (Numeric)



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/linmeric/Integrators.rb', line 17

def self.trapezi(a,b,n)
    h = (b - a) / n.to_f
    int_parz = 0
    for i in 0...n
       # Calculating first addend = f(x_i)
       f1 = yield(a + i * h)
       # calculating second addend = f(X_{i+1})
       f2 = yield(a + (i + 1) * h)
       # Calculating trapeze area
       trap = 0.5 * h * (f1 + f2)
       int_parz += trap
    end
    return int_parz
end