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
40 public final class TrapezoidIntegratorTest {
41
42
43
44
45 @Test
46 public void testSinFunction() {
47 UnivariateFunction f = new Sin();
48 UnivariateIntegrator integrator = new TrapezoidIntegrator();
49 double min, max, expected, result, tolerance;
50
51 min = 0; max = FastMath.PI; expected = 2;
52 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
53 result = integrator.integrate(10000, f, min, max);
54 Assert.assertTrue(integrator.getEvaluations() < 2500);
55 Assert.assertTrue(integrator.getIterations() < 15);
56 Assert.assertEquals(expected, result, tolerance);
57
58 min = -FastMath.PI/3; max = 0; expected = -0.5;
59 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
60 result = integrator.integrate(10000, f, min, max);
61 Assert.assertTrue(integrator.getEvaluations() < 2500);
62 Assert.assertTrue(integrator.getIterations() < 15);
63 Assert.assertEquals(expected, result, tolerance);
64 }
65
66
67
68
69 @Test
70 public void testQuinticFunction() {
71 UnivariateFunction f = new QuinticFunction();
72 UnivariateIntegrator integrator = new TrapezoidIntegrator();
73 double min, max, expected, result, tolerance;
74
75 min = 0; max = 1; expected = -1.0/48;
76 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
77 result = integrator.integrate(10000, f, min, max);
78 Assert.assertTrue(integrator.getEvaluations() < 5000);
79 Assert.assertTrue(integrator.getIterations() < 15);
80 Assert.assertEquals(expected, result, tolerance);
81
82 min = 0; max = 0.5; expected = 11.0/768;
83 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
84 result = integrator.integrate(10000, f, min, max);
85 Assert.assertTrue(integrator.getEvaluations() < 2500);
86 Assert.assertTrue(integrator.getIterations() < 15);
87 Assert.assertEquals(expected, result, tolerance);
88
89 min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48;
90 tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy());
91 result = integrator.integrate(10000, f, min, max);
92 Assert.assertTrue(integrator.getEvaluations() < 5000);
93 Assert.assertTrue(integrator.getIterations() < 15);
94 Assert.assertEquals(expected, result, tolerance);
95
96 }
97
98
99
100
101 @Test
102 public void testParameters() {
103 UnivariateFunction f = new Sin();
104
105 try {
106
107 new TrapezoidIntegrator().integrate(1000, f, 1, -1);
108 Assert.fail("Expecting MathIllegalArgumentException - bad interval");
109 } catch (MathIllegalArgumentException ex) {
110
111 }
112 try {
113
114 new TrapezoidIntegrator(5, 4);
115 Assert.fail("Expecting MathIllegalArgumentException - bad iteration limits");
116 } catch (MathIllegalArgumentException ex) {
117
118 }
119 try {
120
121 new TrapezoidIntegrator(10,99);
122 Assert.fail("Expecting MathIllegalArgumentException - bad iteration limits");
123 } catch (MathIllegalArgumentException ex) {
124
125 }
126 }
127 }