From 6c14d8de4db8320cdd497f4b426acc3ffbe6f47e Mon Sep 17 00:00:00 2001 From: il798li <92280179+il798li@users.noreply.github.com> Date: Sun, 6 Apr 2025 14:03:55 -0700 Subject: [PATCH 1/9] Create RiemannIntegration.java This class approximates integrals by using 4 of Riemann's approximation method. --- .../maths/RiemannIntegration.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/main/java/com/thealgorithms/maths/RiemannIntegration.java diff --git a/src/main/java/com/thealgorithms/maths/RiemannIntegration.java b/src/main/java/com/thealgorithms/maths/RiemannIntegration.java new file mode 100644 index 000000000000..cc900f2ca43c --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/RiemannIntegration.java @@ -0,0 +1,97 @@ +package com.thealgorithms.maths; +import java.util.function.Function; + +/** + * @author https://github.com/il798li/ + * For more information on Riemann's approximation methods for integrals, visit {@link https://en.wikipedia.org/wiki/Riemann_sum this website} + */ +public class RiemannIntegration { + private final double deltaX; + + /** + * Creating the integration class. + * @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. + * @exception IllegalArgumentException when you pass a negative value. + */ + public RiemannIntegration (final double deltaX) { + if (deltaX <= 0) { + throw new IllegalArgumentException ("Accuracy must be a positive number. " + deltaX + " was passed instead."); + } + this.deltaX = deltaX; + } + + /** + * Creating the integration class. This will have good accuracy, but will take a few seconds to calculate complicated integrals. + */ + public RiemannIntegration () { + this(0.000000001); + } + + /** + * Integrates a function. + * @param function You will need to define this function, using {@code Function function = x -> {...}}. + * @param riemannApproximationMethod Each sub-interval can use different shapes to approximate the integral. It is recommended to use Trapezoidal sum. + * @param lowerBoundary The lower bound of where your integration will start. Conventionally, this is the {@code a} value. + * @param upperBoundary The upper bound of where your intetgration will end. Conventionally, this is the {@code a} value. + * @return The area under the curve between the given bounds. + */ + public double integrate(final Function function, final RiemannApproximationMethod riemannApproximationMethod, final double lowerBoundary, final double upperBoundary) { + double value = 0; + switch (riemannApproximationMethod) { + case LEFT_RIEMANN_SUM: { + for (double x = lowerBoundary; x < upperBoundary; x += deltaX) { + value += this.deltaX * function.apply (x); + x += deltaX; + } + break; + } + case RIGHT_RIEMANN_SUM: { + double x = lowerBoundary; + while (x < upperBoundary) { + x += deltaX; + value += this.deltaX * function.apply (x); + } + break; + } + case TRAPEZOIDAL_RIEMANN_SUM: { + value += function.apply (lowerBoundary) * deltaX; + for (double x = lowerBoundary + deltaX; x < upperBoundary; x += deltaX) { + value += function.apply (x) * deltaX * 2; + } + value += function.apply (upperBoundary) * deltaX; + value /= 2; + break; + } + case MIDPOINT_RIEMANN_SUM: { + for (double x = lowerBoundary + deltaX / 2; x < upperBoundary; x += deltaX) { + value += deltaX * function.apply (x); + } + break; + } + } + return value; + } + + public enum RiemannApproximationMethod { + LEFT_RIEMANN_SUM, + RIGHT_RIEMANN_SUM, + MIDPOINT_RIEMANN_SUM, + TRAPEZOIDAL_RIEMANN_SUM + } + + public static void main (String[] args) { + example (); + } + + + /** + * Feel free to look at how the implementation of this method to see how it works. + */ + public static final void example() { + final Function xSquaredFunction = x -> Math.pow(x, 2); // Creates the function f(x) = x^2 + final RiemannApproximationMethod riemannApproximationMethod = RiemannApproximationMethod.TRAPEZOIDAL_RIEMANN_SUM; // Chooses the Trapezoidal method for approximating the integral. + final RiemannIntegration riemannIntegration = new RiemannIntegration (); + final double result = riemannIntegration.integrate (xSquaredFunction, riemannApproximationMethod, 0, 1); // The integral of x^2 from x = 1 to x = 2 is 1/3. + System.out.println (result); + } +} From a36c389c60703900e1ef6aa43e2b67fe115b5003 Mon Sep 17 00:00:00 2001 From: il798li <92280179+il798li@users.noreply.github.com> Date: Sun, 6 Apr 2025 14:15:46 -0700 Subject: [PATCH 2/9] Update RiemannIntegration.java used clang-format to format my java code --- .../maths/RiemannIntegration.java | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/RiemannIntegration.java b/src/main/java/com/thealgorithms/maths/RiemannIntegration.java index cc900f2ca43c..397dea2f6bac 100644 --- a/src/main/java/com/thealgorithms/maths/RiemannIntegration.java +++ b/src/main/java/com/thealgorithms/maths/RiemannIntegration.java @@ -3,7 +3,7 @@ /** * @author https://github.com/il798li/ - * For more information on Riemann's approximation methods for integrals, visit {@link https://en.wikipedia.org/wiki/Riemann_sum this website} + * @Info https://math.libretexts.org/Bookshelves/Calculus/Calculus_3e_(Apex)/05%3A_Integration/5.03%3A_Riemann_Sums */ public class RiemannIntegration { private final double deltaX; @@ -13,9 +13,9 @@ public class RiemannIntegration { * @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. * @exception IllegalArgumentException when you pass a negative value. */ - public RiemannIntegration (final double deltaX) { + public RiemannIntegration(final double deltaX) { if (deltaX <= 0) { - throw new IllegalArgumentException ("Accuracy must be a positive number. " + deltaX + " was passed instead."); + throw new IllegalArgumentException("Accuracy must be a positive number. " + deltaX + " was passed instead."); } this.deltaX = deltaX; } @@ -23,7 +23,7 @@ public RiemannIntegration (final double deltaX) { /** * Creating the integration class. This will have good accuracy, but will take a few seconds to calculate complicated integrals. */ - public RiemannIntegration () { + public RiemannIntegration() { this(0.000000001); } @@ -35,12 +35,12 @@ public RiemannIntegration () { * @param upperBoundary The upper bound of where your intetgration will end. Conventionally, this is the {@code a} value. * @return The area under the curve between the given bounds. */ - public double integrate(final Function function, final RiemannApproximationMethod riemannApproximationMethod, final double lowerBoundary, final double upperBoundary) { + public double integrate(final Function < Double, Double > function, final RiemannApproximationMethod riemannApproximationMethod, final double lowerBoundary, final double upperBoundary) { double value = 0; switch (riemannApproximationMethod) { case LEFT_RIEMANN_SUM: { for (double x = lowerBoundary; x < upperBoundary; x += deltaX) { - value += this.deltaX * function.apply (x); + value += this.deltaX * function.apply(x); x += deltaX; } break; @@ -49,22 +49,22 @@ public double integrate(final Function function, final RiemannAp double x = lowerBoundary; while (x < upperBoundary) { x += deltaX; - value += this.deltaX * function.apply (x); + value += this.deltaX * function.apply(x); } break; } case TRAPEZOIDAL_RIEMANN_SUM: { - value += function.apply (lowerBoundary) * deltaX; + value += function.apply(lowerBoundary) * deltaX; for (double x = lowerBoundary + deltaX; x < upperBoundary; x += deltaX) { - value += function.apply (x) * deltaX * 2; + value += function.apply(x) * deltaX * 2; } - value += function.apply (upperBoundary) * deltaX; + value += function.apply(upperBoundary) * deltaX; value /= 2; break; } case MIDPOINT_RIEMANN_SUM: { for (double x = lowerBoundary + deltaX / 2; x < upperBoundary; x += deltaX) { - value += deltaX * function.apply (x); + value += deltaX * function.apply(x); } break; } @@ -79,19 +79,18 @@ public enum RiemannApproximationMethod { TRAPEZOIDAL_RIEMANN_SUM } - public static void main (String[] args) { - example (); + public static void main(String[] args) { + example(); } - /** * Feel free to look at how the implementation of this method to see how it works. */ public static final void example() { - final Function xSquaredFunction = x -> Math.pow(x, 2); // Creates the function f(x) = x^2 + final Function < Double, Double > xSquaredFunction = x -> Math.pow(x, 2); // Creates the function f(x) = x^2 final RiemannApproximationMethod riemannApproximationMethod = RiemannApproximationMethod.TRAPEZOIDAL_RIEMANN_SUM; // Chooses the Trapezoidal method for approximating the integral. - final RiemannIntegration riemannIntegration = new RiemannIntegration (); - final double result = riemannIntegration.integrate (xSquaredFunction, riemannApproximationMethod, 0, 1); // The integral of x^2 from x = 1 to x = 2 is 1/3. - System.out.println (result); + final RiemannIntegration riemannIntegration = new RiemannIntegration(); + final double result = riemannIntegration.integrate(xSquaredFunction, riemannApproximationMethod, 0, 1); // The integral of x^2 from x = 1 to x = 2 is 1/3. + System.out.println(result); } } From bc6633a37988bff4517f53e022de6623c5eeb985 Mon Sep 17 00:00:00 2001 From: il798li <92280179+il798li@users.noreply.github.com> Date: Sun, 6 Apr 2025 17:18:17 -0700 Subject: [PATCH 3/9] Update RiemannIntegration.java Added four separate methods for each form of Riemann sumse --- .../maths/RiemannIntegration.java | 94 +++++++------------ 1 file changed, 33 insertions(+), 61 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/RiemannIntegration.java b/src/main/java/com/thealgorithms/maths/RiemannIntegration.java index 397dea2f6bac..e7e53e4e7e06 100644 --- a/src/main/java/com/thealgorithms/maths/RiemannIntegration.java +++ b/src/main/java/com/thealgorithms/maths/RiemannIntegration.java @@ -6,77 +6,49 @@ * @Info https://math.libretexts.org/Bookshelves/Calculus/Calculus_3e_(Apex)/05%3A_Integration/5.03%3A_Riemann_Sums */ public class RiemannIntegration { - private final double deltaX; - /** - * Creating the integration class. - * @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. - * @exception IllegalArgumentException when you pass a negative value. - */ - public RiemannIntegration(final double deltaX) { - if (deltaX <= 0) { - throw new IllegalArgumentException("Accuracy must be a positive number. " + deltaX + " was passed instead."); - } - this.deltaX = deltaX; + private static double calculateDeltaX (final double accuracy) { + return Math.pow(10, -accuracy); } - /** - * Creating the integration class. This will have good accuracy, but will take a few seconds to calculate complicated integrals. - */ - public RiemannIntegration() { - this(0.000000001); + public double leftRiemannSum(final Function function, final double lowerBoundary, final double upperBoundary, final double accuracy) { + final double deltaX = calculateDeltaX (accuracy); + double value = 0; + for (double x = lowerBoundary; x < upperBoundary; x += deltaX) { + value += deltaX * function.apply(x); + } + return value; } - /** - * Integrates a function. - * @param function You will need to define this function, using {@code Function function = x -> {...}}. - * @param riemannApproximationMethod Each sub-interval can use different shapes to approximate the integral. It is recommended to use Trapezoidal sum. - * @param lowerBoundary The lower bound of where your integration will start. Conventionally, this is the {@code a} value. - * @param upperBoundary The upper bound of where your intetgration will end. Conventionally, this is the {@code a} value. - * @return The area under the curve between the given bounds. - */ - public double integrate(final Function < Double, Double > function, final RiemannApproximationMethod riemannApproximationMethod, final double lowerBoundary, final double upperBoundary) { + public double rightRiemannSum(final Function function, final double lowerBoundary, final double upperBoundary, final double accuracy) { + final double deltaX = calculateDeltaX (accuracy); + double x = lowerBoundary; double value = 0; - switch (riemannApproximationMethod) { - case LEFT_RIEMANN_SUM: { - for (double x = lowerBoundary; x < upperBoundary; x += deltaX) { - value += this.deltaX * function.apply(x); - x += deltaX; - } - break; - } - case RIGHT_RIEMANN_SUM: { - double x = lowerBoundary; - while (x < upperBoundary) { - x += deltaX; - value += this.deltaX * function.apply(x); - } - break; - } - case TRAPEZOIDAL_RIEMANN_SUM: { - value += function.apply(lowerBoundary) * deltaX; - for (double x = lowerBoundary + deltaX; x < upperBoundary; x += deltaX) { - value += function.apply(x) * deltaX * 2; - } - value += function.apply(upperBoundary) * deltaX; - value /= 2; - break; - } - case MIDPOINT_RIEMANN_SUM: { - for (double x = lowerBoundary + deltaX / 2; x < upperBoundary; x += deltaX) { - value += deltaX * function.apply(x); - } - break; - } + while (x < upperBoundary) { + x += deltaX; + value += deltaX + function.apply(x); + } + return value; + } + + public double midpointRiemannSum(final Function function, final double lowerBoundary, final double upperBoundary, final double accuracy) { + final double deltaX = calculateDeltaX (accuracy); + double value = 0.0; + for (double x = lowerBoundary + accuracy / 2.0; x < upperBoundary; x += accuracy) { + value += accuracy * function.apply(x); } return value; } - public enum RiemannApproximationMethod { - LEFT_RIEMANN_SUM, - RIGHT_RIEMANN_SUM, - MIDPOINT_RIEMANN_SUM, - TRAPEZOIDAL_RIEMANN_SUM + public double trapezoidalRiemannSum(final Function function, final double lowerBoundary, final double upperBoundary, final double accuracy) { + final double deltaX = calculateDeltaX (accuracy); + double value = function.apply(lowerBoundary) * deltaX; + for (double x = lowerBoundary + deltaX; x < upperBoundary; x += deltaX) { + value += function.apply(x) * deltaX * 2; + } + value += function.apply(upperBoundary) * deltaX; + value /= 2; + return value; } public static void main(String[] args) { From 30c3bf5041dc533322dd4e4052dc246223ee7c94 Mon Sep 17 00:00:00 2001 From: il798li <92280179+il798li@users.noreply.github.com> Date: Sun, 6 Apr 2025 17:23:54 -0700 Subject: [PATCH 4/9] Update RiemannIntegration.java added comments explaining how the code works --- .../maths/RiemannIntegration.java | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/RiemannIntegration.java b/src/main/java/com/thealgorithms/maths/RiemannIntegration.java index e7e53e4e7e06..63a047760e24 100644 --- a/src/main/java/com/thealgorithms/maths/RiemannIntegration.java +++ b/src/main/java/com/thealgorithms/maths/RiemannIntegration.java @@ -11,6 +11,13 @@ private static double calculateDeltaX (final double accuracy) { return Math.pow(10, -accuracy); } + /** + * @param function A function that takes in an x value and outputs a {@code y} value. + * @param lowerBoundary The lower boundary for integration, conventionally the {@code a} value. + * @param upperBoundary The upper boundary for integration, conventionally the {@code b} value. + * @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)}. + * @return The approximate value of the definite integral, calculated using the left Riemann Sum. + */ public double leftRiemannSum(final Function function, final double lowerBoundary, final double upperBoundary, final double accuracy) { final double deltaX = calculateDeltaX (accuracy); double value = 0; @@ -19,7 +26,13 @@ public double leftRiemannSum(final Function function, final doub } return value; } - + /** + * @param function A function that takes in an x value and outputs a {@code y} value. + * @param lowerBoundary The lower boundary for integration, conventionally the {@code a} value. + * @param upperBoundary The upper boundary for integration, conventionally the {@code b} value. + * @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)}. + * @return The approximate value of the definite integral, calculated using the right Riemann Sum. + */ public double rightRiemannSum(final Function function, final double lowerBoundary, final double upperBoundary, final double accuracy) { final double deltaX = calculateDeltaX (accuracy); double x = lowerBoundary; @@ -30,7 +43,13 @@ public double rightRiemannSum(final Function function, final dou } return value; } - + /** + * @param function A function that takes in an x value and outputs a {@code y} value. + * @param lowerBoundary The lower boundary for integration, conventionally the {@code a} value. + * @param upperBoundary The upper boundary for integration, conventionally the {@code b} value. + * @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)}. + * @return The approximate value of the definite integral, calculated using the midpoint Riemann Sum. + */ public double midpointRiemannSum(final Function function, final double lowerBoundary, final double upperBoundary, final double accuracy) { final double deltaX = calculateDeltaX (accuracy); double value = 0.0; @@ -40,6 +59,13 @@ public double midpointRiemannSum(final Function function, final return value; } + /** + * @param function A function that takes in an x value and outputs a {@code y} value. + * @param lowerBoundary The lower boundary for integration, conventionally the {@code a} value. + * @param upperBoundary The upper boundary for integration, conventionally the {@code b} value. + * @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)}. + * @return The approximate value of the definite integral, calculated using the trapezoidal Riemann Sum. + */ public double trapezoidalRiemannSum(final Function function, final double lowerBoundary, final double upperBoundary, final double accuracy) { final double deltaX = calculateDeltaX (accuracy); double value = function.apply(lowerBoundary) * deltaX; From 1bbe2924690a8dd68c64d6d0e2f093968303706d Mon Sep 17 00:00:00 2001 From: il798li <92280179+il798li@users.noreply.github.com> Date: Sun, 6 Apr 2025 17:25:02 -0700 Subject: [PATCH 5/9] Update RiemannIntegration.java made all the riemann integration methods static --- .../java/com/thealgorithms/maths/RiemannIntegration.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/RiemannIntegration.java b/src/main/java/com/thealgorithms/maths/RiemannIntegration.java index 63a047760e24..6ad0c09e5123 100644 --- a/src/main/java/com/thealgorithms/maths/RiemannIntegration.java +++ b/src/main/java/com/thealgorithms/maths/RiemannIntegration.java @@ -18,7 +18,7 @@ private static double calculateDeltaX (final double accuracy) { * @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)}. * @return The approximate value of the definite integral, calculated using the left Riemann Sum. */ - public double leftRiemannSum(final Function function, final double lowerBoundary, final double upperBoundary, final double accuracy) { + public static double leftRiemannSum(final Function function, final double lowerBoundary, final double upperBoundary, final double accuracy) { final double deltaX = calculateDeltaX (accuracy); double value = 0; for (double x = lowerBoundary; x < upperBoundary; x += deltaX) { @@ -33,7 +33,7 @@ public double leftRiemannSum(final Function function, final doub * @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)}. * @return The approximate value of the definite integral, calculated using the right Riemann Sum. */ - public double rightRiemannSum(final Function function, final double lowerBoundary, final double upperBoundary, final double accuracy) { + public static double rightRiemannSum(final Function function, final double lowerBoundary, final double upperBoundary, final double accuracy) { final double deltaX = calculateDeltaX (accuracy); double x = lowerBoundary; double value = 0; @@ -50,7 +50,7 @@ public double rightRiemannSum(final Function function, final dou * @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)}. * @return The approximate value of the definite integral, calculated using the midpoint Riemann Sum. */ - public double midpointRiemannSum(final Function function, final double lowerBoundary, final double upperBoundary, final double accuracy) { + public static double midpointRiemannSum(final Function function, final double lowerBoundary, final double upperBoundary, final double accuracy) { final double deltaX = calculateDeltaX (accuracy); double value = 0.0; for (double x = lowerBoundary + accuracy / 2.0; x < upperBoundary; x += accuracy) { @@ -66,7 +66,7 @@ public double midpointRiemannSum(final Function function, final * @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)}. * @return The approximate value of the definite integral, calculated using the trapezoidal Riemann Sum. */ - public double trapezoidalRiemannSum(final Function function, final double lowerBoundary, final double upperBoundary, final double accuracy) { + public static double trapezoidalRiemannSum(final Function function, final double lowerBoundary, final double upperBoundary, final double accuracy) { final double deltaX = calculateDeltaX (accuracy); double value = function.apply(lowerBoundary) * deltaX; for (double x = lowerBoundary + deltaX; x < upperBoundary; x += deltaX) { From 24b9a5e5dc71279194fd59253e3cf0a1e3bc5678 Mon Sep 17 00:00:00 2001 From: il798li <92280179+il798li@users.noreply.github.com> Date: Sun, 6 Apr 2025 17:27:36 -0700 Subject: [PATCH 6/9] Update RiemannIntegration.java added an example usage of the riemann integration --- .../java/com/thealgorithms/maths/RiemannIntegration.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/RiemannIntegration.java b/src/main/java/com/thealgorithms/maths/RiemannIntegration.java index 6ad0c09e5123..8e846a3855d9 100644 --- a/src/main/java/com/thealgorithms/maths/RiemannIntegration.java +++ b/src/main/java/com/thealgorithms/maths/RiemannIntegration.java @@ -86,9 +86,7 @@ public static void main(String[] args) { */ public static final void example() { final Function < Double, Double > xSquaredFunction = x -> Math.pow(x, 2); // Creates the function f(x) = x^2 - final RiemannApproximationMethod riemannApproximationMethod = RiemannApproximationMethod.TRAPEZOIDAL_RIEMANN_SUM; // Chooses the Trapezoidal method for approximating the integral. - final RiemannIntegration riemannIntegration = new RiemannIntegration(); - final double result = riemannIntegration.integrate(xSquaredFunction, riemannApproximationMethod, 0, 1); // The integral of x^2 from x = 1 to x = 2 is 1/3. - System.out.println(result); + final double result = RiemannIntegration.trapezoidalRiemannSum (xSquaredFunction, 0, 1, 9); // I find that an accuracy between 7 - 10 (inclusive) works best. + System.out.println ("Integral of y = x^2 from x = 0 to x = 1: " + result); } } From 36ae92c3bfe6e25eb6f2a7d7ad4df78cd5db77ca Mon Sep 17 00:00:00 2001 From: il798li <92280179+il798li@users.noreply.github.com> Date: Sun, 6 Apr 2025 17:44:42 -0700 Subject: [PATCH 7/9] Update RiemannIntegration.java fixed some of the methods' bugs --- .../java/com/thealgorithms/maths/RiemannIntegration.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/RiemannIntegration.java b/src/main/java/com/thealgorithms/maths/RiemannIntegration.java index 8e846a3855d9..7d6853a9dab1 100644 --- a/src/main/java/com/thealgorithms/maths/RiemannIntegration.java +++ b/src/main/java/com/thealgorithms/maths/RiemannIntegration.java @@ -37,9 +37,9 @@ public static double rightRiemannSum(final Function function, fi final double deltaX = calculateDeltaX (accuracy); double x = lowerBoundary; double value = 0; - while (x < upperBoundary) { + while (x < upperBoundary - deltaX) { x += deltaX; - value += deltaX + function.apply(x); + value += deltaX * function.apply(x); } return value; } @@ -53,8 +53,8 @@ public static double rightRiemannSum(final Function function, fi public static double midpointRiemannSum(final Function function, final double lowerBoundary, final double upperBoundary, final double accuracy) { final double deltaX = calculateDeltaX (accuracy); double value = 0.0; - for (double x = lowerBoundary + accuracy / 2.0; x < upperBoundary; x += accuracy) { - value += accuracy * function.apply(x); + for (double x = lowerBoundary + deltaX / 2.0; x < upperBoundary; x += deltaX) { + value += deltaX * function.apply(x); } return value; } From 49ae0ea6ee884e4887a8babb2ceb5514897110ce Mon Sep 17 00:00:00 2001 From: il798li <92280179+il798li@users.noreply.github.com> Date: Sun, 6 Apr 2025 18:07:59 -0700 Subject: [PATCH 8/9] Update RiemannIntegration.java i'll deal with formatting later --- src/main/java/com/thealgorithms/maths/RiemannIntegration.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/RiemannIntegration.java b/src/main/java/com/thealgorithms/maths/RiemannIntegration.java index 7d6853a9dab1..429d8c6c653b 100644 --- a/src/main/java/com/thealgorithms/maths/RiemannIntegration.java +++ b/src/main/java/com/thealgorithms/maths/RiemannIntegration.java @@ -86,7 +86,7 @@ public static void main(String[] args) { */ public static final void example() { final Function < Double, Double > xSquaredFunction = x -> Math.pow(x, 2); // Creates the function f(x) = x^2 - final double result = RiemannIntegration.trapezoidalRiemannSum (xSquaredFunction, 0, 1, 9); // I find that an accuracy between 7 - 10 (inclusive) works best. + double result = RiemannIntegration.trapezoidalRiemannSum (xSquaredFunction, 0, 1, 9); // I find that an accuracy between 7 - 10 (inclusive) works best. System.out.println ("Integral of y = x^2 from x = 0 to x = 1: " + result); } } From 5b52fa3e524f94468e589bec40032b3c59a4aa62 Mon Sep 17 00:00:00 2001 From: il798li <92280179+il798li@users.noreply.github.com> Date: Wed, 23 Apr 2025 11:32:20 -0700 Subject: [PATCH 9/9] Update RiemannIntegration.java used interval notation in a comment to cater to math language --- src/main/java/com/thealgorithms/maths/RiemannIntegration.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/RiemannIntegration.java b/src/main/java/com/thealgorithms/maths/RiemannIntegration.java index 429d8c6c653b..777e6046129d 100644 --- a/src/main/java/com/thealgorithms/maths/RiemannIntegration.java +++ b/src/main/java/com/thealgorithms/maths/RiemannIntegration.java @@ -87,6 +87,6 @@ public static void main(String[] args) { public static final void example() { final Function < Double, Double > xSquaredFunction = x -> Math.pow(x, 2); // Creates the function f(x) = x^2 double result = RiemannIntegration.trapezoidalRiemannSum (xSquaredFunction, 0, 1, 9); // I find that an accuracy between 7 - 10 (inclusive) works best. - System.out.println ("Integral of y = x^2 from x = 0 to x = 1: " + result); + System.out.println ("Integral of y = x^2 on x E [0, 1]:" + result); } }