Threads
java ( 177186 ) - java ( 177187 ) stack: com.thealgorithms.maths.SimpsonIntegration.main(SimpsonIntegration.java:18)
package com.thealgorithms.maths;
import java.util.TreeMap;
public class SimpsonIntegration {
/*
* 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.
*
* 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 equal 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 evaluation
*/
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 < data.size(); i++) {
if (i == 0 || i == data.size() - 1) {
integralEvaluation += data.get(i);
System.out.println("Multiply f(x" + i + ") by 1");
} else if (i % 2 != 0) {
integralEvaluation += (double) 4 * data.get(i);
System.out.println("Multiply f(x" + i + ") by 4");
} else {
integralEvaluation += (double) 2 * data.get(i);
System.out.println("Multiply f(x" + i + ") by 2");
}
}
// Multiply by h/3
integralEvaluation = h / 3 * integralEvaluation;
// Return the result
return integralEvaluation;
}
// Sample function f
// 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);
}
}
Variables All
No. | From | Name | Value |
---|---|---|---|
1 | 18 | args | [Ljava.lang.String;@7852e922 |
END | 0 | 0 | 0 |
Process Filter | Thread Filter |
---|---|
177186 java | 177187 java |
No. | PN | PID | TID | TN | Message |
---|---|---|---|---|---|
1 | java | 177186 | 177187 | java | Multiply f(x0) by 1 |
2 | java | 177186 | 177187 | java | Multiply f(x0) by 2 |
3 | java | 177186 | 177187 | java | Multiply f(x1) by 4 |
4 | java | 177186 | 177187 | java | Multiply f(x2) by 2 |
5 | java | 177186 | 177187 | java | Multiply f(x3) by 4 |
6 | java | 177186 | 177187 | java | Multiply f(x4) by 2 |
7 | java | 177186 | 177187 | java | Multiply f(x5) by 4 |
8 | java | 177186 | 177187 | java | Multiply f(x6) by 2 |
9 | java | 177186 | 177187 | java | Multiply f(x7) by 4 |
10 | java | 177186 | 177187 | java | Multiply f(x8) by 2 |
11 | java | 177186 | 177187 | java | Multiply f(x9) by 4 |
12 | java | 177186 | 177187 | java | Multiply f(x10) by 2 |
13 | java | 177186 | 177187 | java | Multiply f(x11) by 4 |
14 | java | 177186 | 177187 | java | Multiply f(x12) by 2 |
15 | java | 177186 | 177187 | java | Multiply f(x13) by 4 |
16 | java | 177186 | 177187 | java | Multiply f(x14) by 2 |
17 | java | 177186 | 177187 | java | Multiply f(x15) by 4 |
18 | java | 177186 | 177187 | java | Multiply f(x16) by 1 |
19 | java | 177186 | 177187 | java | Multiply f(x16) by 2 |
20 | java | 177186 | 177187 | java | The integral is equal to: 0.3505787273808654 |
END | 0 | 0 | 0 | 0 | 0 |
×
Functions and Shortcuts
No. | Function | Shortcuts | Description |
---|---|---|---|
1 | GB | Alt + LEFT, Alt + A | Go Backward |
2 | GF | Alt + RIGHT, Alt + D | Go Foreward |
3 | PPE | Alt + UP, Alt + W | Previous Process End |
4 | NPS | Alt + DOWN, Alt + S | Next Process Start |
5 | PB | Ctrl + LEFT, Ctrl + A | current Process Backward |
6 | PF | Ctrl + RIGHT, Ctrl + D | current Process Foreward |
7 | PPTE | Ctrl + UP, Ctrl + W | go to current Process's Previous Thread's End |
8 | PNTS | Ctrl + DOWN, Ctrl + S | go to current Process's Next Thread's Start |
9 | TB | LEFT, A | current Thread Backward |
10 | TF | RIGHT, D | current Thread Foreward |
11 | LU | UP, W | go Line Up of current code block in current thread |
12 | LD | DOWN, S | go Line Down of current code block in current thread |
13 | LP | Shift + UP, Shift + W | go to the occurrence of current line in Previous Loop |
14 | LD | Shift + DOWN, Shift + S | go to the occurrence of current line in Next Loop |
15 | BS | Home | go to code Block Start |
16 | BE | End | go to code Block End |
Project: | Alg-Java |
Update: | 20240824 |
Commit: | a7cd97d7 |
Source Code: | maths.SimpsonIntegration |
BuildTool: | Java17 |
Compiler: | Java17 |
Runtime: | Openjdk17 |
System: | MySystemD |
Kernel: | Linux5.10.211 |
Cpu: | Intel:Corei7-7700K |
Machine: | AwesomeMachine |