2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.commons.math.analysis;
19
import org.apache.commons.math.MathException;
20
import junit.framework.TestCase;
23
* Testcase for Romberg integrator.
25
* Romberg algorithm is very fast for good behavior integrand. Test runs
26
* show that for a default relative accuracy of 1E-6, it generally takes
27
* takes less than 5 iterations for the integral to converge.
29
* @version $Revision: 480442 $ $Date: 2006-11-29 00:21:22 -0700 (Wed, 29 Nov 2006) $
31
public final class RombergIntegratorTest extends TestCase {
34
* Test of integrator for the sine function.
36
public void testSinFunction() throws MathException {
37
UnivariateRealFunction f = new SinFunction();
38
UnivariateRealIntegrator integrator = new RombergIntegrator(f);
39
double min, max, expected, result, tolerance;
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);
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);
53
* Test of integrator for the quintic function.
55
public void testQuinticFunction() throws MathException {
56
UnivariateRealFunction f = new QuinticFunction();
57
UnivariateRealIntegrator integrator = new RombergIntegrator(f);
58
double min, max, expected, result, tolerance;
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);
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);
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);
77
* Test of parameters for the integrator.
79
public void testParameters() throws Exception {
80
UnivariateRealFunction f = new SinFunction();
81
UnivariateRealIntegrator integrator = new RombergIntegrator(f);
85
integrator.integrate(1, -1);
86
fail("Expecting IllegalArgumentException - bad interval");
87
} catch (IllegalArgumentException ex) {
91
// bad iteration limits
92
integrator.setMinimalIterationCount(5);
93
integrator.setMaximalIterationCount(4);
94
integrator.integrate(-1, 1);
95
fail("Expecting IllegalArgumentException - bad iteration limits");
96
} catch (IllegalArgumentException ex) {
100
// bad iteration limits
101
integrator.setMinimalIterationCount(10);
102
integrator.setMaximalIterationCount(50);
103
integrator.integrate(-1, 1);
104
fail("Expecting IllegalArgumentException - bad iteration limits");
105
} catch (IllegalArgumentException ex) {