From ed6edb51ea2e6eed1832eed5bfac56360cde3d9c Mon Sep 17 00:00:00 2001 From: ggkogkou Date: Mon, 18 Oct 2021 20:59:05 +0300 Subject: [PATCH 1/3] Initial Commit --- Maths/SimpsonIntegration.java | 89 +++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 Maths/SimpsonIntegration.java diff --git a/Maths/SimpsonIntegration.java b/Maths/SimpsonIntegration.java new file mode 100644 index 000000000000..d2d134ebbb56 --- /dev/null +++ b/Maths/SimpsonIntegration.java @@ -0,0 +1,89 @@ +package Maths; + +import java.util.TreeMap; + +public class SimpsonIntegration{ + + /* + * Calculate definite integrals by using Simpson's method. + * Given f a function and an even number N of intervals that divide the integration interval e.g. [a, b], + * we calculate the step h = (b-a)/N and create a table that contains all the x points of + * the real axis xi = x0 + i*h and the value f(xi) that corresponds to these xi. + * + * To evaluate the integral i use the formula below: + * I = h/3 * {f(x0) + 4*f(x1) + 2*f(x2) + 4*f(x3) + ... + 2*f(xN-2) + 4*f(xN-1) + f(xN)} + */ + + public static void main(String[] args) { + SimpsonIntegration integration = new SimpsonIntegration(); + + // Give random data for the example purposes + int N = 16; + double a = 1; + double b = 3; + + // Check so that N is even + if(N%2 != 0){ + System.out.println("N must be even number for Simpsons method. Aborted"); + System.exit(1); + } + + // Calculate step h and evaluate the integral + double h = (b-a) / (double) N; + double integralEvaluation = integration.simpsonsMethod(N, h, a); + System.out.println("The integral is evaluated to: " + integralEvaluation); + } + + /* + * @param N: Number of intervals (must be even number N=2*k) + * @param h: Step h = (b-a)/N + * @param a: Starting point of the interval + * @param b: Ending point of the interval + * + * The interpolation points xi = x0 + i*h are stored the treeMap data + * + * @return result of the integral evauation + */ + public double simpsonsMethod(int N, double h, double a){ + TreeMap data = new TreeMap<>(); // Key: i, Value: f(xi) + double temp; + double xi = a; // Initialize the variable xi = x0 + 0*h + + // Create the table of xi and yi points + for(int i=0; i<=N; i++){ + temp = f(xi); // Get the value of the function at that point + data.put(i, temp); + xi += h; // Increase the xi to the next point + } + + // Apply the formula + double integralEvaluation = 0; + for(int i=0; i Date: Mon, 18 Oct 2021 21:05:04 +0300 Subject: [PATCH 2/3] Minor Improvements --- Maths/SimpsonIntegration.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Maths/SimpsonIntegration.java b/Maths/SimpsonIntegration.java index d2d134ebbb56..0ae5e60d826d 100644 --- a/Maths/SimpsonIntegration.java +++ b/Maths/SimpsonIntegration.java @@ -12,6 +12,7 @@ public class SimpsonIntegration{ * * To evaluate the integral i use the formula below: * I = h/3 * {f(x0) + 4*f(x1) + 2*f(x2) + 4*f(x3) + ... + 2*f(xN-2) + 4*f(xN-1) + f(xN)} + * */ public static void main(String[] args) { @@ -31,7 +32,7 @@ public static void main(String[] args) { // Calculate step h and evaluate the integral double h = (b-a) / (double) N; double integralEvaluation = integration.simpsonsMethod(N, h, a); - System.out.println("The integral is evaluated to: " + integralEvaluation); + System.out.println("The integral is equal to: " + integralEvaluation); } /* @@ -42,7 +43,7 @@ public static void main(String[] args) { * * The interpolation points xi = x0 + i*h are stored the treeMap data * - * @return result of the integral evauation + * @return result of the integral evaluation */ public double simpsonsMethod(int N, double h, double a){ TreeMap data = new TreeMap<>(); // Key: i, Value: f(xi) @@ -84,6 +85,7 @@ else if(i%2 == 1) { // Function f(x) = e^(-x) * (4 - x^2) public double f(double x){ return Math.exp(-x) * (4 - Math.pow(x, 2)); +// return Math.sqrt(x); } } From 59266985b6db6ee99872e26658f4ef6ce03e9749 Mon Sep 17 00:00:00 2001 From: ggkogkou Date: Mon, 18 Oct 2021 21:20:41 +0300 Subject: [PATCH 3/3] Wiki link added --- Maths/SimpsonIntegration.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Maths/SimpsonIntegration.java b/Maths/SimpsonIntegration.java index 0ae5e60d826d..87a7e4d26ab0 100644 --- a/Maths/SimpsonIntegration.java +++ b/Maths/SimpsonIntegration.java @@ -5,7 +5,8 @@ public class SimpsonIntegration{ /* - * Calculate definite integrals by using Simpson's method. + * Calculate definite integrals by using Composite Simpson's rule. + * Wiki: https://en.wikipedia.org/wiki/Simpson%27s_rule#Composite_Simpson's_rule * Given f a function and an even number N of intervals that divide the integration interval e.g. [a, b], * we calculate the step h = (b-a)/N and create a table that contains all the x points of * the real axis xi = x0 + i*h and the value f(xi) that corresponds to these xi.