forked from indy256/codelibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpsonIntegration.java
More file actions
25 lines (21 loc) · 835 Bytes
/
SimpsonIntegration.java
File metadata and controls
25 lines (21 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package numeric;
import java.util.function.DoubleFunction;
public class SimpsonIntegration {
public static double integrate(DoubleFunction<Double> f, double a, double b) {
double eps = 1e-10;
double m = (a + b) / 2;
double am = simpsonIntegration(f, a, m);
double mb = simpsonIntegration(f, m, b);
double ab = simpsonIntegration(f, a, b);
if (Math.abs(am + mb - ab) < eps)
return ab;
return integrate(f, a, m) + integrate(f, m, b);
}
static double simpsonIntegration(DoubleFunction<Double> f, double a, double b) {
return (f.apply(a) + 4 * f.apply((a + b) / 2) + f.apply(b)) * (b - a) / 6;
}
// Usage example
public static void main(String[] args) {
System.out.println(integrate(x -> Math.sin(x), 0, Math.PI / 2));
}
}