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