|
6 | 6 | * @Info https://math.libretexts.org/Bookshelves/Calculus/Calculus_3e_(Apex)/05%3A_Integration/5.03%3A_Riemann_Sums
|
7 | 7 | */
|
8 | 8 | public class RiemannIntegration {
|
9 |
| - private final double deltaX; |
10 | 9 |
|
11 |
| - /** |
12 |
| - * Creating the integration class. |
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 |
| - * @exception IllegalArgumentException when you pass a negative value. |
15 |
| - */ |
16 |
| - public RiemannIntegration(final double deltaX) { |
17 |
| - if (deltaX <= 0) { |
18 |
| - throw new IllegalArgumentException("Accuracy must be a positive number. " + deltaX + " was passed instead."); |
19 |
| - } |
20 |
| - this.deltaX = deltaX; |
| 10 | + private static double calculateDeltaX (final double accuracy) { |
| 11 | + return Math.pow(10, -accuracy); |
21 | 12 | }
|
22 | 13 |
|
23 |
| - /** |
24 |
| - * Creating the integration class. This will have good accuracy, but will take a few seconds to calculate complicated integrals. |
25 |
| - */ |
26 |
| - public RiemannIntegration() { |
27 |
| - this(0.000000001); |
| 14 | + public double leftRiemannSum(final Function<Double, Double> function, final double lowerBoundary, final double upperBoundary, final double accuracy) { |
| 15 | + final double deltaX = calculateDeltaX (accuracy); |
| 16 | + double value = 0; |
| 17 | + for (double x = lowerBoundary; x < upperBoundary; x += deltaX) { |
| 18 | + value += deltaX * function.apply(x); |
| 19 | + } |
| 20 | + return value; |
28 | 21 | }
|
29 | 22 |
|
30 |
| - /** |
31 |
| - * Integrates a function. |
32 |
| - * @param function You will need to define this function, using {@code Function<Double, Double> function = x -> {...}}. |
33 |
| - * @param riemannApproximationMethod Each sub-interval can use different shapes to approximate the integral. It is recommended to use Trapezoidal sum. |
34 |
| - * @param lowerBoundary The lower bound of where your integration will start. Conventionally, this is the {@code a} value. |
35 |
| - * @param upperBoundary The upper bound of where your intetgration will end. Conventionally, this is the {@code a} value. |
36 |
| - * @return The area under the curve between the given bounds. |
37 |
| - */ |
38 |
| - public double integrate(final Function < Double, Double > function, final RiemannApproximationMethod riemannApproximationMethod, final double lowerBoundary, final double upperBoundary) { |
| 23 | + public double rightRiemannSum(final Function<Double, Double> function, final double lowerBoundary, final double upperBoundary, final double accuracy) { |
| 24 | + final double deltaX = calculateDeltaX (accuracy); |
| 25 | + double x = lowerBoundary; |
39 | 26 | double value = 0;
|
40 |
| - switch (riemannApproximationMethod) { |
41 |
| - case LEFT_RIEMANN_SUM: { |
42 |
| - for (double x = lowerBoundary; x < upperBoundary; x += deltaX) { |
43 |
| - value += this.deltaX * function.apply(x); |
44 |
| - x += deltaX; |
45 |
| - } |
46 |
| - break; |
47 |
| - } |
48 |
| - case RIGHT_RIEMANN_SUM: { |
49 |
| - double x = lowerBoundary; |
50 |
| - while (x < upperBoundary) { |
51 |
| - x += deltaX; |
52 |
| - value += this.deltaX * function.apply(x); |
53 |
| - } |
54 |
| - break; |
55 |
| - } |
56 |
| - case TRAPEZOIDAL_RIEMANN_SUM: { |
57 |
| - value += function.apply(lowerBoundary) * deltaX; |
58 |
| - for (double x = lowerBoundary + deltaX; x < upperBoundary; x += deltaX) { |
59 |
| - value += function.apply(x) * deltaX * 2; |
60 |
| - } |
61 |
| - value += function.apply(upperBoundary) * deltaX; |
62 |
| - value /= 2; |
63 |
| - break; |
64 |
| - } |
65 |
| - case MIDPOINT_RIEMANN_SUM: { |
66 |
| - for (double x = lowerBoundary + deltaX / 2; x < upperBoundary; x += deltaX) { |
67 |
| - value += deltaX * function.apply(x); |
68 |
| - } |
69 |
| - break; |
70 |
| - } |
| 27 | + while (x < upperBoundary) { |
| 28 | + x += deltaX; |
| 29 | + value += deltaX + function.apply(x); |
| 30 | + } |
| 31 | + return value; |
| 32 | + } |
| 33 | + |
| 34 | + public double midpointRiemannSum(final Function<Double, Double> function, final double lowerBoundary, final double upperBoundary, final double accuracy) { |
| 35 | + final double deltaX = calculateDeltaX (accuracy); |
| 36 | + double value = 0.0; |
| 37 | + for (double x = lowerBoundary + accuracy / 2.0; x < upperBoundary; x += accuracy) { |
| 38 | + value += accuracy * function.apply(x); |
71 | 39 | }
|
72 | 40 | return value;
|
73 | 41 | }
|
74 | 42 |
|
75 |
| - public enum RiemannApproximationMethod { |
76 |
| - LEFT_RIEMANN_SUM, |
77 |
| - RIGHT_RIEMANN_SUM, |
78 |
| - MIDPOINT_RIEMANN_SUM, |
79 |
| - TRAPEZOIDAL_RIEMANN_SUM |
| 43 | + public double trapezoidalRiemannSum(final Function<Double, Double> function, final double lowerBoundary, final double upperBoundary, final double accuracy) { |
| 44 | + final double deltaX = calculateDeltaX (accuracy); |
| 45 | + double value = function.apply(lowerBoundary) * deltaX; |
| 46 | + for (double x = lowerBoundary + deltaX; x < upperBoundary; x += deltaX) { |
| 47 | + value += function.apply(x) * deltaX * 2; |
| 48 | + } |
| 49 | + value += function.apply(upperBoundary) * deltaX; |
| 50 | + value /= 2; |
| 51 | + return value; |
80 | 52 | }
|
81 | 53 |
|
82 | 54 | public static void main(String[] args) {
|
|
0 commit comments