Skip to content

Commit 30c3bf5

Browse files
committed
Update RiemannIntegration.java
added comments explaining how the code works
1 parent bc6633a commit 30c3bf5

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/main/java/com/thealgorithms/maths/RiemannIntegration.java

+28-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ private static double calculateDeltaX (final double accuracy) {
1111
return Math.pow(10, -accuracy);
1212
}
1313

14+
/**
15+
* @param function A function that takes in an x value and outputs a {@code y} value.
16+
* @param lowerBoundary The lower boundary for integration, conventionally the {@code a} value.
17+
* @param upperBoundary The upper boundary for integration, conventionally the {@code b} value.
18+
* @param accuracy The accuracy of the integration. It is recommended to keep this less than 10. Each sub-interval will have a width of {@code 10^(-accuracy)}.
19+
* @return The approximate value of the definite integral, calculated using the left Riemann Sum.
20+
*/
1421
public double leftRiemannSum(final Function<Double, Double> function, final double lowerBoundary, final double upperBoundary, final double accuracy) {
1522
final double deltaX = calculateDeltaX (accuracy);
1623
double value = 0;
@@ -19,7 +26,13 @@ public double leftRiemannSum(final Function<Double, Double> function, final doub
1926
}
2027
return value;
2128
}
22-
29+
/**
30+
* @param function A function that takes in an x value and outputs a {@code y} value.
31+
* @param lowerBoundary The lower boundary for integration, conventionally the {@code a} value.
32+
* @param upperBoundary The upper boundary for integration, conventionally the {@code b} value.
33+
* @param accuracy The accuracy of the integration. It is recommended to keep this less than 10. Each sub-interval will have a width of {@code 10^(-accuracy)}.
34+
* @return The approximate value of the definite integral, calculated using the right Riemann Sum.
35+
*/
2336
public double rightRiemannSum(final Function<Double, Double> function, final double lowerBoundary, final double upperBoundary, final double accuracy) {
2437
final double deltaX = calculateDeltaX (accuracy);
2538
double x = lowerBoundary;
@@ -30,7 +43,13 @@ public double rightRiemannSum(final Function<Double, Double> function, final dou
3043
}
3144
return value;
3245
}
33-
46+
/**
47+
* @param function A function that takes in an x value and outputs a {@code y} value.
48+
* @param lowerBoundary The lower boundary for integration, conventionally the {@code a} value.
49+
* @param upperBoundary The upper boundary for integration, conventionally the {@code b} value.
50+
* @param accuracy The accuracy of the integration. It is recommended to keep this less than 10. Each sub-interval will have a width of {@code 10^(-accuracy)}.
51+
* @return The approximate value of the definite integral, calculated using the midpoint Riemann Sum.
52+
*/
3453
public double midpointRiemannSum(final Function<Double, Double> function, final double lowerBoundary, final double upperBoundary, final double accuracy) {
3554
final double deltaX = calculateDeltaX (accuracy);
3655
double value = 0.0;
@@ -40,6 +59,13 @@ public double midpointRiemannSum(final Function<Double, Double> function, final
4059
return value;
4160
}
4261

62+
/**
63+
* @param function A function that takes in an x value and outputs a {@code y} value.
64+
* @param lowerBoundary The lower boundary for integration, conventionally the {@code a} value.
65+
* @param upperBoundary The upper boundary for integration, conventionally the {@code b} value.
66+
* @param accuracy The accuracy of the integration. It is recommended to keep this less than 10. Each sub-interval will have a width of {@code 10^(-accuracy)}.
67+
* @return The approximate value of the definite integral, calculated using the trapezoidal Riemann Sum.
68+
*/
4369
public double trapezoidalRiemannSum(final Function<Double, Double> function, final double lowerBoundary, final double upperBoundary, final double accuracy) {
4470
final double deltaX = calculateDeltaX (accuracy);
4571
double value = function.apply(lowerBoundary) * deltaX;

0 commit comments

Comments
 (0)