|
| 1 | +package Maths; |
| 2 | + |
| 3 | +import java.util.TreeMap; |
| 4 | + |
| 5 | +public class SimpsonIntegration{ |
| 6 | + |
| 7 | + /* |
| 8 | + * Calculate definite integrals by using Composite Simpson's rule. |
| 9 | + * Wiki: https://en.wikipedia.org/wiki/Simpson%27s_rule#Composite_Simpson's_rule |
| 10 | + * Given f a function and an even number N of intervals that divide the integration interval e.g. [a, b], |
| 11 | + * we calculate the step h = (b-a)/N and create a table that contains all the x points of |
| 12 | + * the real axis xi = x0 + i*h and the value f(xi) that corresponds to these xi. |
| 13 | + * |
| 14 | + * To evaluate the integral i use the formula below: |
| 15 | + * I = h/3 * {f(x0) + 4*f(x1) + 2*f(x2) + 4*f(x3) + ... + 2*f(xN-2) + 4*f(xN-1) + f(xN)} |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | + public static void main(String[] args) { |
| 20 | + SimpsonIntegration integration = new SimpsonIntegration(); |
| 21 | + |
| 22 | + // Give random data for the example purposes |
| 23 | + int N = 16; |
| 24 | + double a = 1; |
| 25 | + double b = 3; |
| 26 | + |
| 27 | + // Check so that N is even |
| 28 | + if(N%2 != 0){ |
| 29 | + System.out.println("N must be even number for Simpsons method. Aborted"); |
| 30 | + System.exit(1); |
| 31 | + } |
| 32 | + |
| 33 | + // Calculate step h and evaluate the integral |
| 34 | + double h = (b-a) / (double) N; |
| 35 | + double integralEvaluation = integration.simpsonsMethod(N, h, a); |
| 36 | + System.out.println("The integral is equal to: " + integralEvaluation); |
| 37 | + } |
| 38 | + |
| 39 | + /* |
| 40 | + * @param N: Number of intervals (must be even number N=2*k) |
| 41 | + * @param h: Step h = (b-a)/N |
| 42 | + * @param a: Starting point of the interval |
| 43 | + * @param b: Ending point of the interval |
| 44 | + * |
| 45 | + * The interpolation points xi = x0 + i*h are stored the treeMap data |
| 46 | + * |
| 47 | + * @return result of the integral evaluation |
| 48 | + */ |
| 49 | + public double simpsonsMethod(int N, double h, double a){ |
| 50 | + TreeMap<Integer, Double> data = new TreeMap<>(); // Key: i, Value: f(xi) |
| 51 | + double temp; |
| 52 | + double xi = a; // Initialize the variable xi = x0 + 0*h |
| 53 | + |
| 54 | + // Create the table of xi and yi points |
| 55 | + for(int i=0; i<=N; i++){ |
| 56 | + temp = f(xi); // Get the value of the function at that point |
| 57 | + data.put(i, temp); |
| 58 | + xi += h; // Increase the xi to the next point |
| 59 | + } |
| 60 | + |
| 61 | + // Apply the formula |
| 62 | + double integralEvaluation = 0; |
| 63 | + for(int i=0; i<data.size(); i++){ |
| 64 | + if(i == 0 || i == data.size()-1) { |
| 65 | + integralEvaluation += data.get(i); |
| 66 | + System.out.println("Multiply f(x" + i + ") by 1"); |
| 67 | + } |
| 68 | + else if(i%2 == 1) { |
| 69 | + integralEvaluation += (double) 4 * data.get(i); |
| 70 | + System.out.println("Multiply f(x" + i + ") by 4"); |
| 71 | + } |
| 72 | + else { |
| 73 | + integralEvaluation += (double) 2 * data.get(i); |
| 74 | + System.out.println("Multiply f(x" + i + ") by 2"); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Multiply by h/3 |
| 79 | + integralEvaluation = h/3 * integralEvaluation; |
| 80 | + |
| 81 | + // Return the result |
| 82 | + return integralEvaluation; |
| 83 | + } |
| 84 | + |
| 85 | + // Sample function f |
| 86 | + // Function f(x) = e^(-x) * (4 - x^2) |
| 87 | + public double f(double x){ |
| 88 | + return Math.exp(-x) * (4 - Math.pow(x, 2)); |
| 89 | +// return Math.sqrt(x); |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments