1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.analysis.integration;
23
24 import org.hipparchus.analysis.QuinticFunction;
25 import org.hipparchus.analysis.UnivariateFunction;
26 import org.hipparchus.analysis.function.Sin;
27 import org.hipparchus.exception.MathIllegalArgumentException;
28 import org.hipparchus.util.FastMath;
29 import org.junit.jupiter.api.Test;
30
31 import static org.junit.jupiter.api.Assertions.assertEquals;
32 import static org.junit.jupiter.api.Assertions.assertTrue;
33 import static org.junit.jupiter.api.Assertions.fail;
34
35
36
37
38
39
40
41
42
43 final class TrapezoidIntegratorTest {
44
45
46
47
48 @Test
49 void testSinFunction() {
50 UnivariateFunction f = new Sin();
51 UnivariateIntegrator integrator = new TrapezoidIntegrator();
52 double min, max, expected, result, tolerance;
53
54 min = 0; max = FastMath.PI; expected = 2;
55 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
56 result = integrator.integrate(10000, f, min, max);
57 assertTrue(integrator.getEvaluations() < 2500);
58 assertTrue(integrator.getIterations() < 15);
59 assertEquals(expected, result, tolerance);
60
61 min = -FastMath.PI/3; max = 0; expected = -0.5;
62 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
63 result = integrator.integrate(10000, f, min, max);
64 assertTrue(integrator.getEvaluations() < 2500);
65 assertTrue(integrator.getIterations() < 15);
66 assertEquals(expected, result, tolerance);
67 }
68
69
70
71
72 @Test
73 void testQuinticFunction() {
74 UnivariateFunction f = new QuinticFunction();
75 UnivariateIntegrator integrator = new TrapezoidIntegrator();
76 double min, max, expected, result, tolerance;
77
78 min = 0; max = 1; expected = -1.0/48;
79 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
80 result = integrator.integrate(10000, f, min, max);
81 assertTrue(integrator.getEvaluations() < 5000);
82 assertTrue(integrator.getIterations() < 15);
83 assertEquals(expected, result, tolerance);
84
85 min = 0; max = 0.5; expected = 11.0/768;
86 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
87 result = integrator.integrate(10000, f, min, max);
88 assertTrue(integrator.getEvaluations() < 2500);
89 assertTrue(integrator.getIterations() < 15);
90 assertEquals(expected, result, tolerance);
91
92 min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48;
93 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
94 result = integrator.integrate(10000, f, min, max);
95 assertTrue(integrator.getEvaluations() < 5000);
96 assertTrue(integrator.getIterations() < 15);
97 assertEquals(expected, result, tolerance);
98
99 }
100
101
102
103
104 @Test
105 void testParameters() {
106 UnivariateFunction f = new Sin();
107
108 try {
109
110 new TrapezoidIntegrator().integrate(1000, f, 1, -1);
111 fail("Expecting MathIllegalArgumentException - bad interval");
112 } catch (MathIllegalArgumentException ex) {
113
114 }
115 try {
116
117 new TrapezoidIntegrator(5, 4);
118 fail("Expecting MathIllegalArgumentException - bad iteration limits");
119 } catch (MathIllegalArgumentException ex) {
120
121 }
122 try {
123
124 new TrapezoidIntegrator(10,99);
125 fail("Expecting MathIllegalArgumentException - bad iteration limits");
126 } catch (MathIllegalArgumentException ex) {
127
128 }
129 }
130 }