MATH-1204 bracket function gives up too early

In UnivariateSolverUtils.bracket(...) the search ends prematurely if a =
lowerBound, which ignores some roots in the interval. Fixed by changing the
loop condition so the search continues while b < upperBound. Also added a test
case.
This commit is contained in:
Evan Ward 2015-02-19 15:16:28 -05:00
parent d746a54c2e
commit a56d4998cf
2 changed files with 9 additions and 1 deletions

View File

@ -314,7 +314,7 @@ public class UnivariateSolverUtils {
double delta = 0;
for (int numIterations = 0;
(numIterations < maximumIterations) && (a > lowerBound || b > upperBound);
(numIterations < maximumIterations) && (a > lowerBound || b < upperBound);
++numIterations) {
final double previousA = a;

View File

@ -176,6 +176,14 @@ public class UnivariateSolverUtilsTest {
UnivariateSolverUtils.bracket(sin, 1.5, 0, 2.0, 0);
}
/** check the search continues when a = lowerBound and b < upperBound. */
@Test
public void testBracketLoopConditionForB() {
double[] result = UnivariateSolverUtils.bracket(sin, -0.9, -1, 1, 0.1, 1, 100);
Assert.assertTrue(result[0] <= 0);
Assert.assertTrue(result[1] >= 0);
}
@Test
public void testMisc() {
UnivariateFunction f = new QuinticFunction();