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