diff --git a/src/main/java/org/apache/commons/math3/analysis/integration/MidPointIntegrator.java b/src/main/java/org/apache/commons/math3/analysis/integration/MidPointIntegrator.java index 059af63eb..8058b6aa3 100644 --- a/src/main/java/org/apache/commons/math3/analysis/integration/MidPointIntegrator.java +++ b/src/main/java/org/apache/commons/math3/analysis/integration/MidPointIntegrator.java @@ -120,7 +120,7 @@ public class MidPointIntegrator extends BaseAbstractUnivariateIntegrator { final double diff = getMax() - min; if (n == 0) { - final double midPoint = 0.5 * diff; + final double midPoint = min + 0.5 * diff; return diff * computeObjectiveValue(midPoint); } else { final long np = 1L << (n - 1); // number of new points in this stage diff --git a/src/test/java/org/apache/commons/math3/analysis/integration/MidPointIntegratorTest.java b/src/test/java/org/apache/commons/math3/analysis/integration/MidPointIntegratorTest.java index d0a5fa9c8..928817681 100644 --- a/src/test/java/org/apache/commons/math3/analysis/integration/MidPointIntegratorTest.java +++ b/src/test/java/org/apache/commons/math3/analysis/integration/MidPointIntegratorTest.java @@ -35,6 +35,25 @@ import org.junit.Test; */ public final class MidPointIntegratorTest { + /** + * Test of integrator for the sine function. + */ + @Test + public void testLowAccuracy() { + UnivariateFunction f = new QuinticFunction(); + UnivariateIntegrator integrator = new MidPointIntegrator(0.01, 1.0e-10, 2, 4); + + double min = -10; + double max = -9; + double expected = -3697001.0 / 48.0; + double tolerance = FastMath.abs(expected * integrator.getRelativeAccuracy()); + double result = integrator.integrate(Integer.MAX_VALUE, f, min, max); + Assert.assertTrue(integrator.getEvaluations() < Integer.MAX_VALUE / 2); + Assert.assertTrue(integrator.getIterations() < MidPointIntegrator.MIDPOINT_MAX_ITERATIONS_COUNT / 2); + Assert.assertEquals(expected, result, tolerance); + + } + /** * Test of integrator for the sine function. */