You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -13,17 +13,17 @@ public class RiemannIntegration {
13
13
* @param deltaX This is essentially the change in each rectangle. You ideally want a very small positive values. If you want an extremely high accuracy, use {@code Double.MIN_DOUBLE}, but be warned: this will take an extremely long time.
14
14
* @exception IllegalArgumentException when you pass a negative value.
15
15
*/
16
-
publicRiemannIntegration(finaldoubledeltaX) {
16
+
publicRiemannIntegration(finaldoubledeltaX) {
17
17
if (deltaX <= 0) {
18
-
thrownewIllegalArgumentException("Accuracy must be a positive number. " + deltaX + " was passed instead.");
18
+
thrownewIllegalArgumentException("Accuracy must be a positive number. " + deltaX + " was passed instead.");
19
19
}
20
20
this.deltaX = deltaX;
21
21
}
22
22
23
23
/**
24
24
* Creating the integration class. This will have good accuracy, but will take a few seconds to calculate complicated integrals.
25
25
*/
26
-
publicRiemannIntegration() {
26
+
publicRiemannIntegration() {
27
27
this(0.000000001);
28
28
}
29
29
@@ -35,12 +35,12 @@ public RiemannIntegration () {
35
35
* @param upperBoundary The upper bound of where your intetgration will end. Conventionally, this is the {@code a} value.
36
36
* @return The area under the curve between the given bounds.
for (doublex = lowerBoundary; x < upperBoundary; x += deltaX) {
43
-
value += this.deltaX * function.apply(x);
43
+
value += this.deltaX * function.apply(x);
44
44
x += deltaX;
45
45
}
46
46
break;
@@ -49,22 +49,22 @@ public double integrate(final Function<Double, Double> function, final RiemannAp
49
49
doublex = lowerBoundary;
50
50
while (x < upperBoundary) {
51
51
x += deltaX;
52
-
value += this.deltaX * function.apply(x);
52
+
value += this.deltaX * function.apply(x);
53
53
}
54
54
break;
55
55
}
56
56
caseTRAPEZOIDAL_RIEMANN_SUM: {
57
-
value += function.apply(lowerBoundary) * deltaX;
57
+
value += function.apply(lowerBoundary) * deltaX;
58
58
for (doublex = lowerBoundary + deltaX; x < upperBoundary; x += deltaX) {
59
-
value += function.apply(x) * deltaX * 2;
59
+
value += function.apply(x) * deltaX * 2;
60
60
}
61
-
value += function.apply(upperBoundary) * deltaX;
61
+
value += function.apply(upperBoundary) * deltaX;
62
62
value /= 2;
63
63
break;
64
64
}
65
65
caseMIDPOINT_RIEMANN_SUM: {
66
66
for (doublex = lowerBoundary + deltaX / 2; x < upperBoundary; x += deltaX) {
67
-
value += deltaX * function.apply(x);
67
+
value += deltaX * function.apply(x);
68
68
}
69
69
break;
70
70
}
@@ -79,19 +79,18 @@ public enum RiemannApproximationMethod {
79
79
TRAPEZOIDAL_RIEMANN_SUM
80
80
}
81
81
82
-
publicstaticvoidmain(String[] args) {
83
-
example();
82
+
publicstaticvoidmain(String[] args) {
83
+
example();
84
84
}
85
85
86
-
87
86
/**
88
87
* Feel free to look at how the implementation of this method to see how it works.
89
88
*/
90
89
publicstaticfinalvoidexample() {
91
-
finalFunction<Double, Double> xSquaredFunction = x -> Math.pow(x, 2); // Creates the function f(x) = x^2
90
+
finalFunction < Double, Double> xSquaredFunction = x -> Math.pow(x, 2); // Creates the function f(x) = x^2
92
91
finalRiemannApproximationMethodriemannApproximationMethod = RiemannApproximationMethod.TRAPEZOIDAL_RIEMANN_SUM; // Chooses the Trapezoidal method for approximating the integral.
finaldoubleresult = riemannIntegration.integrate(xSquaredFunction, riemannApproximationMethod, 0, 1); // The integral of x^2 from x = 1 to x = 2 is 1/3.
finaldoubleresult = riemannIntegration.integrate(xSquaredFunction, riemannApproximationMethod, 0, 1); // The integral of x^2 from x = 1 to x = 2 is 1/3.
0 commit comments