1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.analysis;
18
19 import org.apache.commons.math.MathException;
20 import junit.framework.TestCase;
21
22
23
24
25
26
27
28
29
30
31 public final class RombergIntegratorTest extends TestCase {
32
33
34
35
36 public void testSinFunction() throws MathException {
37 UnivariateRealFunction f = new SinFunction();
38 UnivariateRealIntegrator integrator = new RombergIntegrator(f);
39 double min, max, expected, result, tolerance;
40
41 min = 0; max = Math.PI; expected = 2;
42 tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
43 result = integrator.integrate(min, max);
44 assertEquals(expected, result, tolerance);
45
46 min = -Math.PI/3; max = 0; expected = -0.5;
47 tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
48 result = integrator.integrate(min, max);
49 assertEquals(expected, result, tolerance);
50 }
51
52
53
54
55 public void testQuinticFunction() throws MathException {
56 UnivariateRealFunction f = new QuinticFunction();
57 UnivariateRealIntegrator integrator = new RombergIntegrator(f);
58 double min, max, expected, result, tolerance;
59
60 min = 0; max = 1; expected = -1.0/48;
61 tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
62 result = integrator.integrate(min, max);
63 assertEquals(expected, result, tolerance);
64
65 min = 0; max = 0.5; expected = 11.0/768;
66 tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
67 result = integrator.integrate(min, max);
68 assertEquals(expected, result, tolerance);
69
70 min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48;
71 tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
72 result = integrator.integrate(min, max);
73 assertEquals(expected, result, tolerance);
74 }
75
76
77
78
79 public void testParameters() throws Exception {
80 UnivariateRealFunction f = new SinFunction();
81 UnivariateRealIntegrator integrator = new RombergIntegrator(f);
82
83 try {
84
85 integrator.integrate(1, -1);
86 fail("Expecting IllegalArgumentException - bad interval");
87 } catch (IllegalArgumentException ex) {
88
89 }
90 try {
91
92 integrator.setMinimalIterationCount(5);
93 integrator.setMaximalIterationCount(4);
94 integrator.integrate(-1, 1);
95 fail("Expecting IllegalArgumentException - bad iteration limits");
96 } catch (IllegalArgumentException ex) {
97
98 }
99 try {
100
101 integrator.setMinimalIterationCount(10);
102 integrator.setMaximalIterationCount(50);
103 integrator.integrate(-1, 1);
104 fail("Expecting IllegalArgumentException - bad iteration limits");
105 } catch (IllegalArgumentException ex) {
106
107 }
108 }
109 }