-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpsonIntegrator.java
More file actions
70 lines (58 loc) · 1.86 KB
/
Copy pathSimpsonIntegrator.java
File metadata and controls
70 lines (58 loc) · 1.86 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* An application of the Composite Simpson's Rule for approximating functions.
*/
public class SimpsonIntegrator {
private double a;
private double b;
private double n; /* number of evaluations */
/* checks if parameters are valid */
public boolean checkConditions() {
if (n % 2 != 0) {
n++;
System.out.print("The provided (n) value was not even, (n) has " +
"been incremented");
}
if (a > b) {
System.out.print("Invalid parameters. (a) must be less than (b). " +
"Please check and recompile.");
return false;
}
if (n < 0) {
System.out.print("Invalid parameters. (n) must be greater "+
"than (0). Please check and recompile.");
return false;
}
return true;
}
/**
* Composite Simpson's Rule.
*
* @param a : lower bound
* @param b : upper bound
* @param n : number of evaluations
* @param func : user defined function class
*/
public double integrate(double a, double b, double n, Function func) {
this.a = a;
this.b = b;
this.n = n;
double h = (b - a) / this.n;
double dx = (a + h);
double x = (a + dx);
double t1 = func.f(a);
double t4 = func.f(b);
double t3 = func.f(x);
double t2 = 0;
if (!checkConditions()) {
System.out.println("Invalid parameters. Recompile.");
System.exit(1);
}
for (int j = 1; j <= (n - 2)/2; j++) {
x += dx;
t2 += func.f(x);
x += dx;
t3 += func.f(x);
}
return (dx / 3) * (t1 + (2 * t2) + (4 * t3) + t4);
}
}