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 public final class TrapezoidIntegratorTest extends TestCase {
31
32
33
34
35 public void testSinFunction() throws MathException {
36 UnivariateRealFunction f = new SinFunction();
37 UnivariateRealIntegrator integrator = new TrapezoidIntegrator(f);
38 double min, max, expected, result, tolerance;
39
40 min = 0; max = Math.PI; expected = 2;
41 tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
42 result = integrator.integrate(min, max);
43 assertEquals(expected, result, tolerance);
44
45 min = -Math.PI/3; max = 0; expected = -0.5;
46 tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
47 result = integrator.integrate(min, max);
48 assertEquals(expected, result, tolerance);
49 }
50
51
52
53
54 public void testQuinticFunction() throws MathException {
55 UnivariateRealFunction f = new QuinticFunction();
56 UnivariateRealIntegrator integrator = new TrapezoidIntegrator(f);
57 double min, max, expected, result, tolerance;
58
59 min = 0; max = 1; expected = -1.0/48;
60 tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
61 result = integrator.integrate(min, max);
62 assertEquals(expected, result, tolerance);
63
64 min = 0; max = 0.5; expected = 11.0/768;
65 tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
66 result = integrator.integrate(min, max);
67 assertEquals(expected, result, tolerance);
68
69 min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48;
70 tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
71 result = integrator.integrate(min, max);
72 assertEquals(expected, result, tolerance);
73 }
74
75
76
77
78 public void testParameters() throws Exception {
79 UnivariateRealFunction f = new SinFunction();
80 UnivariateRealIntegrator integrator = new TrapezoidIntegrator(f);
81
82 try {
83
84 integrator.integrate(1, -1);
85 fail("Expecting IllegalArgumentException - bad interval");
86 } catch (IllegalArgumentException ex) {
87
88 }
89 try {
90
91 integrator.setMinimalIterationCount(5);
92 integrator.setMaximalIterationCount(4);
93 integrator.integrate(-1, 1);
94 fail("Expecting IllegalArgumentException - bad iteration limits");
95 } catch (IllegalArgumentException ex) {
96
97 }
98 try {
99
100 integrator.setMinimalIterationCount(10);
101 integrator.setMaximalIterationCount(99);
102 integrator.integrate(-1, 1);
103 fail("Expecting IllegalArgumentException - bad iteration limits");
104 } catch (IllegalArgumentException ex) {
105
106 }
107 }
108 }