001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.math.analysis.integration;
018
019 import org.apache.commons.math.MathException;
020 import org.apache.commons.math.analysis.QuinticFunction;
021 import org.apache.commons.math.analysis.SinFunction;
022 import org.apache.commons.math.analysis.UnivariateRealFunction;
023
024 import junit.framework.TestCase;
025
026 /**
027 * Testcase for Romberg integrator.
028 * <p>
029 * Romberg algorithm is very fast for good behavior integrand. Test runs
030 * show that for a default relative accuracy of 1E-6, it generally takes
031 * takes less than 5 iterations for the integral to converge.
032 *
033 * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
034 */
035 public final class RombergIntegratorTest extends TestCase {
036
037 /**
038 * Test of integrator for the sine function.
039 */
040 public void testSinFunction() throws MathException {
041 UnivariateRealFunction f = new SinFunction();
042 UnivariateRealIntegrator integrator = new RombergIntegrator();
043 double min, max, expected, result, tolerance;
044
045 min = 0; max = Math.PI; expected = 2;
046 tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
047 result = integrator.integrate(f, min, max);
048 assertEquals(expected, result, tolerance);
049
050 min = -Math.PI/3; max = 0; expected = -0.5;
051 tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
052 result = integrator.integrate(f, min, max);
053 assertEquals(expected, result, tolerance);
054 }
055
056 /**
057 * Test of integrator for the quintic function.
058 */
059 public void testQuinticFunction() throws MathException {
060 UnivariateRealFunction f = new QuinticFunction();
061 UnivariateRealIntegrator integrator = new RombergIntegrator();
062 double min, max, expected, result, tolerance;
063
064 min = 0; max = 1; expected = -1.0/48;
065 tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
066 result = integrator.integrate(f, min, max);
067 assertEquals(expected, result, tolerance);
068
069 min = 0; max = 0.5; expected = 11.0/768;
070 tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
071 result = integrator.integrate(f, min, max);
072 assertEquals(expected, result, tolerance);
073
074 min = -1; max = 4; expected = 2048/3.0 - 78 + 1.0/48;
075 tolerance = Math.abs(expected * integrator.getRelativeAccuracy());
076 result = integrator.integrate(f, min, max);
077 assertEquals(expected, result, tolerance);
078 }
079
080 /**
081 * Test of parameters for the integrator.
082 */
083 public void testParameters() throws Exception {
084 UnivariateRealFunction f = new SinFunction();
085 UnivariateRealIntegrator integrator = new RombergIntegrator();
086
087 try {
088 // bad interval
089 integrator.integrate(f, 1, -1);
090 fail("Expecting IllegalArgumentException - bad interval");
091 } catch (IllegalArgumentException ex) {
092 // expected
093 }
094 try {
095 // bad iteration limits
096 integrator.setMinimalIterationCount(5);
097 integrator.setMaximalIterationCount(4);
098 integrator.integrate(f, -1, 1);
099 fail("Expecting IllegalArgumentException - bad iteration limits");
100 } catch (IllegalArgumentException ex) {
101 // expected
102 }
103 try {
104 // bad iteration limits
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 // expected
111 }
112 }
113 }