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.Assert;
30 import org.junit.Test;
31
32
33
34
35
36
37
38
39 public final class MidPointIntegratorTest {
40
41
42
43
44 @Test
45 public void testLowAccuracy() {
46 UnivariateFunction f = new QuinticFunction();
47 UnivariateIntegrator integrator = new MidPointIntegrator(0.01, 1.0e-10, 2, 4);
48
49 double min = -10;
50 double max = -9;
51 double expected = -3697001.0 / 48.0;
52 double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
53 double result = integrator.integrate(Integer.MAX_VALUE, f, min, max);
54 Assert.assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
55 Assert.assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
56 Assert.assertEquals(expected, result, tolerance);
57
58 }
59
60
61
62
63 @Test
64 public void testSinFunction() {
65 UnivariateFunction f = new Sin();
66 UnivariateIntegrator integrator = new MidPointIntegrator();
67
68 double min = 0;
69 double max = FastMath.PI;
70 double expected = 2;
71 double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
72 double result = integrator.integrate(Integer.MAX_VALUE, f, min, max);
73 Assert.assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
74 Assert.assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
75 Assert.assertEquals(expected, result, tolerance);
76
77 min = -FastMath.PI/3;
78 max = 0;
79 expected = -0.5;
80 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
81 result = integrator.integrate(Integer.MAX_VALUE, f, min, max);
82 Assert.assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
83 Assert.assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
84 Assert.assertEquals(expected, result, tolerance);
85
86 }
87
88
89
90
91 @Test
92 public void testQuinticFunction() {
93 UnivariateFunction f = new QuinticFunction();
94 UnivariateIntegrator integrator = new MidPointIntegrator();
95
96 double min = 0;
97 double max = 1;
98 double expected = -1.0 / 48;
99 double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
100 double result = integrator.integrate(Integer.MAX_VALUE, f, min, max);
101 Assert.assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
102 Assert.assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
103 Assert.assertEquals(expected, result, tolerance);
104
105 min = 0;
106 max = 0.5;
107 expected = 11.0 / 768;
108 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
109 result = integrator.integrate(Integer.MAX_VALUE, f, min, max);
110 Assert.assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
111 Assert.assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
112 Assert.assertEquals(expected, result, tolerance);
113
114 min = -1;
115 max = 4;
116 expected = 2048 / 3.0 - 78 + 1.0 / 48;
117 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
118 result = integrator.integrate(Integer.MAX_VALUE, f, min, max);
119 Assert.assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2);
120 Assert.assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2);
121 Assert.assertEquals(expected, result, tolerance);
122
123 }
124
125
126
127
128 @Test
129 public void testParameters() {
130 UnivariateFunction f = new Sin();
131
132 try {
133
134 new MidPointIntegrator().integrate(1000, f, 1, -1);
135 Assert.fail("Expecting MathIllegalArgumentException - bad interval");
136 } catch (MathIllegalArgumentException ex) {
137
138 }
139 try {
140
141 new MidPointIntegrator(5, 4);
142 Assert.fail("Expecting MathIllegalArgumentException - bad iteration limits");
143 } catch (MathIllegalArgumentException ex) {
144
145 }
146 try {
147
148 new MidPointIntegrator(10, 99);
149 Assert.fail("Expecting MathIllegalArgumentException - bad iteration limits");
150 } catch (MathIllegalArgumentException ex) {
151
152 }
153 }
154 }