Skip to content

Instantly share code, notes, and snippets.

View amkisko's full-sized avatar
😺
Hymyillen suora selkä!

Andrei Makarov amkisko

😺
Hymyillen suora selkä!
View GitHub Profile
@simplay
simplay / Ruby_Lambda_Fun_Numerical _Integration_Trapezoid.rb
Last active April 22, 2022 15:23
# Numerical Integraion over real domain of real 1d valued functions using Newton-Cotes approximation. # NB1: Error of approximation in O(f'' * (b-a)^3 ) # NB2: In practice quite usable for many common functions, like affine, trigonomatric function
# D subset |R Domain over which we integrate - sample D = [1,10]
D = (1..10).to_a
# f:|R->|R function - sample x |-> f(x) := x^2
f = lambda {|x| x*x}
# summation of function values over our domain
summator = lambda {|function, domain| domain.inject(0){|x,y| x += function.call(y)} }
# Integrations Driver (for real valued 1d functions only) - assumption: integration steps equidistant
@gysel
gysel / integrate.rb
Created August 4, 2012 12:45
Simple Integral implementation in Ruby
require 'bigdecimal'
def integrate (a, b, n)
a = a.to_f; b = b.to_f; n = n.to_i
result = 0.0
dx = (b - a) / n
dx_half = dx / 2
for i in 0 ... n
result += yield a + (i * dx)