Last active
April 22, 2022 15:23
-
-
Save simplay/5593854 to your computer and use it in GitHub Desktop.
# 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
Integrator = lambda {|summator, function, domain, upper, a,b| dL = domain[0,upper-1]; dU = domain[1,upper]; h=(b-a)/(upper-1).to_f; (h/2)*(summer.call(function, dL) + summator.call(function, dU) )} | |
# example - integral_1^10 {x*x} dx | |
Integrator.call(summator, f, D, D.count, D.min, D.max) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment