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 package ffx.numerics.integrate;
39
40 import static org.apache.commons.math3.util.FastMath.abs;
41 import static org.apache.commons.math3.util.FastMath.cos;
42 import static org.apache.commons.math3.util.FastMath.sin;
43
44 import java.text.DecimalFormat;
45
46
47
48
49
50
51
52 public class Integration {
53
54 private static final double[] x = new double[202];
55
56 static {
57 x[0] = 0;
58 for (int i = 1; i < 201; i++) {
59 x[i] = .0025 + .005 * (i - 1);
60 }
61 x[201] = 1;
62 }
63
64
65
66
67
68
69
70
71 public static double averageIntegral(double leftInt, double rightInt) {
72 return (leftInt + rightInt) / 2.0;
73 }
74
75
76
77
78
79
80 public static double[] generateTestData_v1() {
81 double[] y = new double[202];
82
83 for (int i = 0; i < 202; i++) {
84 y[i] = 10 * sin(6 * x[i]) - 7 * cos(5 * x[i]) + 11 * sin(8 * x[i]);
85 }
86
87 return y;
88 }
89
90
91
92
93
94
95
96
97
98 public static double halfBinComposite(double[] inputData, int mode, String side) {
99 int n = inputData.length;
100 double halfBinComposite = 0;
101
102 if (side.equalsIgnoreCase("left")) {
103
104 double lowerHalfBin = (inputData[1] + inputData[0]) / 2.0 * (x[1] - x[0]);
105 halfBinComposite += lowerHalfBin;
106 switch (mode) {
107
108 case 1 -> {
109 double upperTrapArea = (inputData[n - 3] + inputData[n - 2]) / 2.0 * (x[n - 2] - x[n - 3]);
110 halfBinComposite += upperTrapArea;
111 }
112
113
114 case 2 -> {
115 double upperSimpson =
116 (1.0 / 3.0)
117 * (x[n - 4] - x[n - 5])
118 * (inputData[n - 5] + 4 * inputData[n - 4] + inputData[n - 3]);
119 halfBinComposite += upperSimpson;
120 double upperTrapArea = (inputData[n - 3] + inputData[n - 2]) / 2.0 * (x[n - 2] - x[n - 3]);
121 halfBinComposite += upperTrapArea;
122 }
123 }
124 } else if (side.equalsIgnoreCase("right")) {
125
126 double upperHalfBin = (inputData[n - 1] + inputData[n - 2]) / 2.0 * (x[n - 1] - x[n - 2]);
127 halfBinComposite += upperHalfBin;
128 switch (mode) {
129
130 case 1:
131 double lowerTrapArea = (inputData[1] + inputData[2]) / 2.0 * (x[2] - x[1]);
132 halfBinComposite += lowerTrapArea;
133 break;
134
135
136 case 2:
137 lowerTrapArea = (inputData[1] + inputData[2]) / 2.0 * (x[2] - x[1]);
138 halfBinComposite += lowerTrapArea;
139 double lowerSimpson =
140 (1.0 / 3.0) * (x[3] - x[2]) * (inputData[2] + 4 * inputData[3] + inputData[4]);
141 halfBinComposite += lowerSimpson;
142 break;
143 }
144 }
145 return halfBinComposite;
146 }
147
148
149
150
151
152
153
154 public static double leftBoole(double[] inputData) {
155 int n = inputData.length;
156 double normalBoole = 0;
157 for (int a = 1; a < n - 5; a += 4) {
158 double area =
159 (2.0 / 45.0)
160 * (x[a + 1] - x[a])
161 * (7 * inputData[a]
162 + 32 * inputData[a + 1]
163 + 12 * inputData[a + 2]
164 + 32 * inputData[a + 3]
165 + 7 * inputData[a + 4]);
166 normalBoole += area;
167 }
168
169 return normalBoole;
170 }
171
172
173
174
175
176
177
178 public static double leftRectangularMethod(double[] inputData) {
179 int n = inputData.length;
180 double[] y = generateTestData_v1();
181 double rectangularIntegral = 0;
182 for (int a = 0; a < n - 2; a++) {
183 double area = (x[a + 1] - x[a]) * y[a];
184 rectangularIntegral += area;
185 }
186 return rectangularIntegral;
187 }
188
189
190
191
192
193
194
195 public static double leftSimpsons(double[] inputData) {
196 int n = inputData.length;
197 double normalSimpsons = 0;
198 for (int a = 1; a < n - 4; a += 2) {
199 double area =
200 (1.0 / 3.0)
201 * (x[a + 1] - x[a])
202 * (inputData[a] + 4 * inputData[a + 1] + inputData[a + 2]);
203 normalSimpsons += area;
204 }
205 return normalSimpsons;
206 }
207
208
209
210
211
212
213
214 public static double leftTrapInput(double[] inputData) {
215 int n = x.length;
216 double sum = 0;
217 double total = 0;
218 double trapIntegral = 0;
219 for (int a = 0; a < n - 2; a++) {
220 if (a > 0) {
221 double area = (inputData[a + 1] + inputData[a]) / (double) 2 * (x[a + 1] - x[a]);
222 sum = area + total;
223 total = sum;
224 }
225 if (a == n - 3) {
226 trapIntegral = sum;
227 }
228 if (a == 0) {
229 total = (inputData[a + 1] + inputData[a]) / (double) 2 * (x[a + 1] - x[a]);
230 }
231 }
232
233 return trapIntegral;
234 }
235
236
237
238
239
240
241 public static void main(String[] args) {
242 double testAnswer;
243 double testTrap, testTrapRight, avgTrap, avgTrapError;
244 double testSimp, testSimpRight, avgSimp, avgSimpError;
245 double testBoole, testBooleRight, avgBoole, avgBooleError;
246 double testRect, testRectRight, avgRect, avgRectError;
247 DecimalFormat decimalFormat = new DecimalFormat("#.00");
248
249 testAnswer = 2.98393938659796;
250 System.out.print(" The test case answer is " + testAnswer + "\n\n");
251
252 testRect = leftRectangularMethod(generateTestData_v1());
253 testRectRight = rightRectangularMethod(generateTestData_v1());
254 avgRect = (testRect + testRectRight) / 2.0;
255 avgRectError = abs(testAnswer - avgRect) / testAnswer * 100;
256
257 testTrap = leftTrapInput(generateTestData_v1());
258 testTrapRight = rightTrapInput(generateTestData_v1());
259 avgTrap = (testTrap + testTrapRight) / 2.0;
260 avgTrapError = abs(avgTrap - testAnswer) / testAnswer * 100;
261
262 testSimp =
263 leftSimpsons(generateTestData_v1()) + halfBinComposite(generateTestData_v1(), 1, "left");
264 testSimpRight =
265 rightSimpsons(generateTestData_v1()) + halfBinComposite(generateTestData_v1(), 1, "right");
266 avgSimp = (testSimp + testSimpRight) / 2.0;
267 avgSimpError = abs(testAnswer - avgSimp) / testAnswer * 100;
268
269 testBoole =
270 leftBoole(generateTestData_v1()) + halfBinComposite(generateTestData_v1(), 2, "left");
271 testBooleRight =
272 rightBoole(generateTestData_v1()) + halfBinComposite(generateTestData_v1(), 2, "right");
273 avgBoole = (testBoole + testBooleRight) / 2.0;
274 avgBooleError = abs(testAnswer - avgBoole) / testAnswer * 100;
275
276
277 System.out.print(" Average integrals \n\n");
278 System.out.print(" Average rectangular " + avgRect + "\n");
279 System.out.print(" Average trap " + avgTrap + "\n");
280 System.out.print(" Average Simpsons " + avgSimp + "\n");
281 System.out.print(" Average Boole " + avgBoole + "\n");
282
283
284 System.out.print("\n Average integral error \n\n");
285 System.out.print(" Average rectangular error " + decimalFormat.format(avgRectError) + "%\n");
286 System.out.print(" Average Trapezoidal error " + decimalFormat.format(avgTrapError) + "%\n");
287 System.out.print(" Average Simpsons error " + decimalFormat.format(avgSimpError) + "%\n");
288 System.out.print(" Average Boole error " + decimalFormat.format(avgBooleError) + "%\n");
289 }
290
291
292
293
294
295
296
297 public static double rightBoole(double[] inputData) {
298 int n = inputData.length;
299 double normalBoole = 0;
300 for (int a = 4; a < n - 5; a += 4) {
301
302 double area =
303 (2.0 / 45.0)
304 * (x[a + 1] - x[a])
305 * (7 * inputData[a]
306 + 32 * inputData[a + 1]
307 + 12 * inputData[a + 2]
308 + 32 * inputData[a + 3]
309 + 7 * inputData[a + 4]);
310 normalBoole += area;
311 }
312
313 return normalBoole;
314 }
315
316
317
318
319
320
321
322 public static double rightRectangularMethod(double[] inputData) {
323 int n = inputData.length;
324 double rectangularIntegral = 0;
325 double[] y = generateTestData_v1();
326 for (int a = 1; a < n - 1; a++) {
327 double area = (x[a + 1] - x[a]) * y[a];
328 rectangularIntegral += area;
329 }
330 return rectangularIntegral;
331 }
332
333
334
335
336
337
338
339 public static double rightSimpsons(double[] inputData) {
340 int n = inputData.length;
341 double normalSimpsons = 0;
342 for (int a = 2; a < n - 3; a += 2) {
343
344 double area =
345 (1.0 / 3.0)
346 * (x[a + 1] - x[a])
347 * (inputData[a] + 4 * inputData[a + 1] + inputData[a + 2]);
348 normalSimpsons += area;
349 }
350 return normalSimpsons;
351 }
352
353
354
355
356
357
358
359 public static double rightTrapInput(double[] inputData) {
360 int n = x.length;
361 double sum = 0;
362 double total = 0;
363 double trapIntegral = 0;
364 for (int a = 1; a < n - 1; a++) {
365 if (a > 1) {
366 double area = (inputData[a + 1] + inputData[a]) / 2.0 * (x[a + 1] - x[a]);
367 sum = area + total;
368 total = sum;
369 }
370 if (a == n - 2) {
371 trapIntegral = sum;
372 }
373 if (a == 1) {
374 total = (inputData[a + 1] + inputData[a]) / 2.0 * (x[a + 1] - x[a]);
375 }
376 }
377
378 return trapIntegral;
379 }
380 }