Module: NumericalIntegration
- Defined in:
- lib/Numerical_Integration.rb
Instance Method Summary collapse
- #my_exp(x) ⇒ Object
-
#rectangle_method(function, a, b, n) ⇒ Object
Метод левых прямоугольников.
-
#simpsons_method(function, a, b, n) ⇒ Object
Формула Симпсона.
-
#trapezoidal_method(function, a, b, n) ⇒ Object
Метод трапеций.
Instance Method Details
#my_exp(x) ⇒ Object
53 54 55 |
# File 'lib/Numerical_Integration.rb', line 53 def my_exp(x) return Math.exp(x) end |
#rectangle_method(function, a, b, n) ⇒ Object
Метод левых прямоугольников
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/Numerical_Integration.rb', line 4 def rectangle_method(function, a, b, n) h = (b - a).to_f / n #Шаг sum = 0.0 (0..n).each do |i| x = a + i*h rectangle = function.call(x) sum += rectangle end sum *= h return sum end |
#simpsons_method(function, a, b, n) ⇒ Object
Формула Симпсона
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/Numerical_Integration.rb', line 29 def simpsons_method(function, a, b, n) h = (b - a).to_f / n x = Array.new fx = Array.new (0..n).each do |i| x.push(a + i*h) fx.push(function.call(x[i])) end result = 0.0 (0..n).each do |i| if i == 0 or i == n result += fx[i] elsif i % 2 != 0 result += 4* fx[i] else result += 2 * fx[i] end end result = result * (h / 3) return result end |
#trapezoidal_method(function, a, b, n) ⇒ Object
Метод трапеций
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/Numerical_Integration.rb', line 17 def trapezoidal_method(function, a, b, n) h = (b - a).to_f / n sum = 0.0 sum += (function.call(a) + function.call(b)) / 2.0 (1..(n-1)).each do |i| sum += function.call(a + i*h) end sum *= h return sum end |