From 30cf24c84661ac409ca48fb548f57d7affdd4cb6 Mon Sep 17 00:00:00 2001 From: lane Date: Tue, 20 Feb 2018 17:39:45 -0700 Subject: [PATCH 1/3] added quadrature trapezoidal rule to Maths --- Maths/TrapezoidalRule.py | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Maths/TrapezoidalRule.py diff --git a/Maths/TrapezoidalRule.py b/Maths/TrapezoidalRule.py new file mode 100644 index 000000000..f926ed256 --- /dev/null +++ b/Maths/TrapezoidalRule.py @@ -0,0 +1,45 @@ +''' +Numerical integration or quadrature for a smooth function f with known values at x_i + +This method is the classical approch of suming 'Equally Spaced Abscissas' + +method 1: +"extended trapezoidal rule" + +''' + +def method_1(boundary, steps): +# "extended trapezoidal rule" +# int(f) = dx/2 * (f1 + 2f2 + ... + fn) + h = (boundary[1] - boundary[0]) / steps + a = boundary[0] + b = boundary[1] + x_i = makePoints(a,b,h) + y = 0.0 + y += (h/2.0)*f(a) + for i in x_i: + #print(i) + y += h*f(i) + y += (h/2.0)*f(b) + return y + +def makePoints(a,b,h): + x = a + h + while x < (b-h): + yield x + x = x + h + +def f(x): + y = (x-0)*(x-0) + return y + +def main(): + a = 0.0 #Lower bound of integration + b = 1.0 #Upper bound of integration + steps = 10.0 #define number of steps or resolution + boundary = [a, b] #define boundary of integration + y = method_1(boundary, steps) + print 'y = {0}'.format(y) + +if __name__ == '__main__': + main() From 537909df64a55df4b52f7b94d6cbb679a2550558 Mon Sep 17 00:00:00 2001 From: lane Date: Tue, 20 Feb 2018 17:48:22 -0700 Subject: [PATCH 2/3] added quadrature trapezoidal rule to Maths --- Maths/TrapezoidalRule.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/TrapezoidalRule.py b/Maths/TrapezoidalRule.py index f926ed256..2ad857390 100644 --- a/Maths/TrapezoidalRule.py +++ b/Maths/TrapezoidalRule.py @@ -29,7 +29,7 @@ def makePoints(a,b,h): yield x x = x + h -def f(x): +def f(x): #enter your function here y = (x-0)*(x-0) return y From 698faa9ca4bdb598aee3d2029b3c21d81fe0cc64 Mon Sep 17 00:00:00 2001 From: lane Date: Thu, 22 Feb 2018 08:40:00 -0700 Subject: [PATCH 3/3] Added Simpson Rule to Maths --- Maths/SimpsonRule.py | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Maths/SimpsonRule.py diff --git a/Maths/SimpsonRule.py b/Maths/SimpsonRule.py new file mode 100644 index 000000000..51b5ed1e4 --- /dev/null +++ b/Maths/SimpsonRule.py @@ -0,0 +1,47 @@ + +''' +Numerical integration or quadrature for a smooth function f with known values at x_i + +This method is the classical approch of suming 'Equally Spaced Abscissas' + +method 2: +"Simpson Rule" + +''' + +def method_2(boundary, steps): +# "Simpson Rule" +# int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn) + h = (boundary[1] - boundary[0]) / steps + a = boundary[0] + b = boundary[1] + x_i = makePoints(a,b,h) + y = 0.0 + y += (h/3.0)*f(a) + cnt = 2 + for i in x_i: + y += (h/3)*(4-2*(cnt%2))*f(i) + cnt += 1 + y += (h/3.0)*f(b) + return y + +def makePoints(a,b,h): + x = a + h + while x < (b-h): + yield x + x = x + h + +def f(x): #enter your function here + y = (x-0)*(x-0) + return y + +def main(): + a = 0.0 #Lower bound of integration + b = 1.0 #Upper bound of integration + steps = 10.0 #define number of steps or resolution + boundary = [a, b] #define boundary of integration + y = method_2(boundary, steps) + print 'y = {0}'.format(y) + +if __name__ == '__main__': + main()