org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction Java Examples

The following examples show how to use org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Deprecated
public void testDeprecated() throws MathException {
    DifferentiableUnivariateRealFunction f = new SinFunction();
    double result;
    
    UnivariateRealSolver solver = new NewtonSolver(f);
    result = solver.solve(3, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    result = solver.solve(1, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
    
    //TODO:  create abstract solver test class, move these there
    assertEquals(result, solver.getResult(), 0);
    assertTrue(solver.getIterationCount() > 0);
}
 
Example #2
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 *
 */
public void testSinZero() throws MathException {
    DifferentiableUnivariateRealFunction f = new SinFunction();
    double result;
    
    UnivariateRealSolver solver = new NewtonSolver();
    result = solver.solve(f, 3, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 1, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
    
    //TODO:  create abstract solver test class, move these there
    assertEquals(result, solver.getResult(), 0);
    assertTrue(solver.getIterationCount() > 0);
}
 
Example #3
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 *
 */
public void testSinZero() throws MathException {
    DifferentiableUnivariateRealFunction f = new SinFunction();
    double result;
    
    UnivariateRealSolver solver = new NewtonSolver();
    result = solver.solve(f, 3, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 1, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
    
    //TODO:  create abstract solver test class, move these there
    assertEquals(result, solver.getResult(), 0);
    assertTrue(solver.getIterationCount() > 0);
}
 
Example #4
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Deprecated
public void testDeprecated() throws MathException {
    DifferentiableUnivariateRealFunction f = new SinFunction();
    double result;
    
    UnivariateRealSolver solver = new NewtonSolver(f);
    result = solver.solve(3, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    result = solver.solve(1, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
    
    //TODO:  create abstract solver test class, move these there
    assertEquals(result, solver.getResult(), 0);
    assertTrue(solver.getIterationCount() > 0);
}
 
Example #5
Source File: NewtonSolver.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find a zero near the value <code>startValue</code>.
 * 
 * @param f the function to solve
 * @param min the lower bound for the interval (ignored).
 * @param max the upper bound for the interval (ignored).
 * @param startValue the start value to use.
 * @return the value where the function is zero
 * @throws MaxIterationsExceededException if the maximum iteration count is exceeded 
 * @throws FunctionEvaluationException if an error occurs evaluating the
 * function or derivative
 * @throws IllegalArgumentException if startValue is not between min and max or
 * if function is not a {@link DifferentiableUnivariateRealFunction} instance
 */
public double solve(final UnivariateRealFunction f,
                    final double min, final double max, final double startValue)
    throws MaxIterationsExceededException, FunctionEvaluationException {

    try {

        final UnivariateRealFunction derivative =
            ((DifferentiableUnivariateRealFunction) f).derivative();
        clearResult();
        verifySequence(min, startValue, max);

        double x0 = startValue;
        double x1;

        int i = 0;
        while (i < maximalIterationCount) {

            x1 = x0 - (f.value(x0) / derivative.value(x0));
            if (Math.abs(x1 - x0) <= absoluteAccuracy) {
                setResult(x1, i);
                return x1;
            }

            x0 = x1;
            ++i;
        }

        throw new MaxIterationsExceededException(maximalIterationCount);
    } catch (ClassCastException cce) {
        throw MathRuntimeException.createIllegalArgumentException("function is not differentiable");
    }
}
 
Example #6
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 */
public void testQuinticZero() throws MathException {
    DifferentiableUnivariateRealFunction f = new QuinticFunction();
    double result;

    UnivariateRealSolver solver = new NewtonSolver();
    result = solver.solve(f, -0.2, 0.2);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, -0.1, 0.3);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, -0.3, 0.45);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.3, 0.7);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.2, 0.6);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.05, 0.95);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 1.25);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.8, 1.2);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 1.75);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.55, 1.45);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 5);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
}
 
Example #7
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 */
public void testQuinticZero() throws MathException {
    DifferentiableUnivariateRealFunction f = new QuinticFunction();
    double result;

    UnivariateRealSolver solver = new NewtonSolver();
    result = solver.solve(f, -0.2, 0.2);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, -0.1, 0.3);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, -0.3, 0.45);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.3, 0.7);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.2, 0.6);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.05, 0.95);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 1.25);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.8, 1.2);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 1.75);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.55, 1.45);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 5);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
}
 
Example #8
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Deprecated
public void testDeprecated() throws MathException {
    DifferentiableUnivariateRealFunction f = new SinFunction();
    double result;

    UnivariateRealSolver solver = new NewtonSolver(f);
    result = solver.solve(3, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    result = solver.solve(1, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    assertEquals(result, solver.getResult(), 0);
    assertTrue(solver.getIterationCount() > 0);
}
 
Example #9
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 */
public void testSinZero() throws MathException {
    DifferentiableUnivariateRealFunction f = new SinFunction();
    double result;

    UnivariateRealSolver solver = new NewtonSolver();
    result = solver.solve(f, 3, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 1, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    assertEquals(result, solver.getResult(), 0);
    assertTrue(solver.getIterationCount() > 0);
}
 
Example #10
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 */
public void testQuinticZero() throws MathException {
    DifferentiableUnivariateRealFunction f = new QuinticFunction();
    double result;

    UnivariateRealSolver solver = new NewtonSolver();
    result = solver.solve(f, -0.2, 0.2);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, -0.1, 0.3);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, -0.3, 0.45);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.3, 0.7);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.2, 0.6);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.05, 0.95);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 1.25);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.8, 1.2);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 1.75);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.55, 1.45);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 5);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
}
 
Example #11
Source File: NewtonSolver.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find a zero near the value <code>startValue</code>.
 *
 * @param f the function to solve
 * @param min the lower bound for the interval (ignored).
 * @param max the upper bound for the interval (ignored).
 * @param startValue the start value to use.
 * @return the value where the function is zero
 * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
 * @throws FunctionEvaluationException if an error occurs evaluating the
 * function or derivative
 * @throws IllegalArgumentException if startValue is not between min and max or
 * if function is not a {@link DifferentiableUnivariateRealFunction} instance
 */
public double solve(final UnivariateRealFunction f,
                    final double min, final double max, final double startValue)
    throws MaxIterationsExceededException, FunctionEvaluationException {

    try {

        final UnivariateRealFunction derivative =
            ((DifferentiableUnivariateRealFunction) f).derivative();
        clearResult();
        verifySequence(min, startValue, max);

        double x0 = startValue;
        double x1;

        int i = 0;
        while (i < maximalIterationCount) {

            x1 = x0 - (f.value(x0) / derivative.value(x0));
            if (Math.abs(x1 - x0) <= absoluteAccuracy) {
                setResult(x1, i);
                return x1;
            }

            x0 = x1;
            ++i;
        }

        throw new MaxIterationsExceededException(maximalIterationCount);
    } catch (ClassCastException cce) {
        throw MathRuntimeException.createIllegalArgumentException("function is not differentiable");
    }
}
 
Example #12
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Deprecated
public void testDeprecated() throws MathException {
    DifferentiableUnivariateRealFunction f = new SinFunction();
    double result;

    UnivariateRealSolver solver = new NewtonSolver(f);
    result = solver.solve(3, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    result = solver.solve(1, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    assertEquals(result, solver.getResult(), 0);
    assertTrue(solver.getIterationCount() > 0);
}
 
Example #13
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 */
public void testSinZero() throws MathException {
    DifferentiableUnivariateRealFunction f = new SinFunction();
    double result;

    UnivariateRealSolver solver = new NewtonSolver();
    result = solver.solve(f, 3, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 1, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    assertEquals(result, solver.getResult(), 0);
    assertTrue(solver.getIterationCount() > 0);
}
 
Example #14
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 */
public void testQuinticZero() throws MathException {
    DifferentiableUnivariateRealFunction f = new QuinticFunction();
    double result;

    UnivariateRealSolver solver = new NewtonSolver();
    result = solver.solve(f, -0.2, 0.2);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, -0.1, 0.3);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, -0.3, 0.45);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.3, 0.7);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.2, 0.6);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.05, 0.95);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 1.25);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.8, 1.2);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 1.75);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.55, 1.45);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 5);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
}
 
Example #15
Source File: NewtonSolver.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find a zero near the value <code>startValue</code>.
 *
 * @param f the function to solve
 * @param min the lower bound for the interval (ignored).
 * @param max the upper bound for the interval (ignored).
 * @param startValue the start value to use.
 * @return the value where the function is zero
 * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
 * @throws FunctionEvaluationException if an error occurs evaluating the
 * function or derivative
 * @throws IllegalArgumentException if startValue is not between min and max or
 * if function is not a {@link DifferentiableUnivariateRealFunction} instance
 */
public double solve(final UnivariateRealFunction f,
                    final double min, final double max, final double startValue)
    throws MaxIterationsExceededException, FunctionEvaluationException {

    try {

        final UnivariateRealFunction derivative =
            ((DifferentiableUnivariateRealFunction) f).derivative();
        clearResult();
        verifySequence(min, startValue, max);

        double x0 = startValue;
        double x1;

        int i = 0;
        while (i < maximalIterationCount) {

            x1 = x0 - (f.value(x0) / derivative.value(x0));
            if (Math.abs(x1 - x0) <= absoluteAccuracy) {
                setResult(x1, i);
                return x1;
            }

            x0 = x1;
            ++i;
        }

        throw new MaxIterationsExceededException(maximalIterationCount);
    } catch (ClassCastException cce) {
        throw MathRuntimeException.createIllegalArgumentException("function is not differentiable");
    }
}
 
Example #16
Source File: NewtonSolver.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find a zero near the value <code>startValue</code>.
 * 
 * @param f the function to solve
 * @param min the lower bound for the interval (ignored).
 * @param max the upper bound for the interval (ignored).
 * @param startValue the start value to use.
 * @return the value where the function is zero
 * @throws MaxIterationsExceededException if the maximum iteration count is exceeded 
 * @throws FunctionEvaluationException if an error occurs evaluating the
 * function or derivative
 * @throws IllegalArgumentException if startValue is not between min and max or
 * if function is not a {@link DifferentiableUnivariateRealFunction} instance
 */
public double solve(final UnivariateRealFunction f,
                    final double min, final double max, final double startValue)
    throws MaxIterationsExceededException, FunctionEvaluationException {

    try {

        final UnivariateRealFunction derivative =
            ((DifferentiableUnivariateRealFunction) f).derivative();
        clearResult();
        verifySequence(min, startValue, max);

        double x0 = startValue;
        double x1;

        int i = 0;
        while (i < maximalIterationCount) {

            x1 = x0 - (f.value(x0) / derivative.value(x0));
            if (Math.abs(x1 - x0) <= absoluteAccuracy) {
                setResult(x1, i);
                return x1;
            }

            x0 = x1;
            ++i;
        }

        throw new MaxIterationsExceededException(maximalIterationCount);
    } catch (ClassCastException cce) {
        throw MathRuntimeException.createIllegalArgumentException("function is not differentiable");
    }
}
 
Example #17
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 */
@Test
public void testQuinticZero() {
    DifferentiableUnivariateRealFunction f = new QuinticFunction();
    double result;

    NewtonSolver solver = new NewtonSolver();
    result = solver.solve(100, f, -0.2, 0.2);
    Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(100, f, -0.1, 0.3);
    Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(100, f, -0.3, 0.45);
    Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(100, f, 0.3, 0.7);
    Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(100, f, 0.2, 0.6);
    Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(100, f, 0.05, 0.95);
    Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(100, f, 0.85, 1.25);
    Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(100, f, 0.8, 1.2);
    Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(100, f, 0.85, 1.75);
    Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(100, f, 0.55, 1.45);
    Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(100, f, 0.85, 5);
    Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
}
 
Example #18
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 */
@Test
public void testSinZero() {
    DifferentiableUnivariateRealFunction f = new SinFunction();
    double result;

    NewtonSolver solver = new NewtonSolver();
    result = solver.solve(100, f, 3, 4);
    Assert.assertEquals(result, FastMath.PI, solver.getAbsoluteAccuracy());

    result = solver.solve(100, f, 1, 4);
    Assert.assertEquals(result, FastMath.PI, solver.getAbsoluteAccuracy());

    Assert.assertTrue(solver.getEvaluations() > 0);
}
 
Example #19
Source File: AbstractDifferentiableUnivariateRealSolver.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected void setup(int maxEval, DifferentiableUnivariateRealFunction f,
                     double min, double max, double startValue) {
    super.setup(maxEval, f, min, max, startValue);
    functionDerivative = f.derivative();
}
 
Example #20
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 */
public void testQuinticZero() throws MathException {
    DifferentiableUnivariateRealFunction f = new QuinticFunction();
    double result;

    UnivariateRealSolver solver = new NewtonSolver();
    result = solver.solve(f, -0.2, 0.2);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, -0.1, 0.3);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, -0.3, 0.45);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.3, 0.7);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.2, 0.6);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.05, 0.95);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 1.25);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.8, 1.2);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 1.75);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.55, 1.45);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 5);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
}
 
Example #21
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 */
public void testSinZero() throws MathException {
    DifferentiableUnivariateRealFunction f = new SinFunction();
    double result;

    UnivariateRealSolver solver = new NewtonSolver();
    result = solver.solve(f, 3, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 1, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    assertEquals(result, solver.getResult(), 0);
    assertTrue(solver.getIterationCount() > 0);
}
 
Example #22
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Deprecated
public void testDeprecated() throws MathException {
    DifferentiableUnivariateRealFunction f = new SinFunction();
    double result;

    UnivariateRealSolver solver = new NewtonSolver(f);
    result = solver.solve(3, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    result = solver.solve(1, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    assertEquals(result, solver.getResult(), 0);
    assertTrue(solver.getIterationCount() > 0);
}
 
Example #23
Source File: NewtonSolver.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find a zero near the value <code>startValue</code>.
 *
 * @param f the function to solve
 * @param min the lower bound for the interval (ignored).
 * @param max the upper bound for the interval (ignored).
 * @param startValue the start value to use.
 * @return the value where the function is zero
 * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
 * @throws FunctionEvaluationException if an error occurs evaluating the
 * function or derivative
 * @throws IllegalArgumentException if startValue is not between min and max or
 * if function is not a {@link DifferentiableUnivariateRealFunction} instance
 */
public double solve(final UnivariateRealFunction f,
                    final double min, final double max, final double startValue)
    throws MaxIterationsExceededException, FunctionEvaluationException {

    try {

        final UnivariateRealFunction derivative =
            ((DifferentiableUnivariateRealFunction) f).derivative();
        clearResult();
        verifySequence(min, startValue, max);

        double x0 = startValue;
        double x1;

        int i = 0;
        while (i < maximalIterationCount) {

            x1 = x0 - (f.value(x0) / derivative.value(x0));
            if (Math.abs(x1 - x0) <= absoluteAccuracy) {
                setResult(x1, i);
                return x1;
            }

            x0 = x1;
            ++i;
        }

        throw new MaxIterationsExceededException(maximalIterationCount);
    } catch (ClassCastException cce) {
        throw MathRuntimeException.createIllegalArgumentException("function is not differentiable");
    }
}
 
Example #24
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 */
public void testQuinticZero() throws MathException {
    DifferentiableUnivariateRealFunction f = new QuinticFunction();
    double result;

    UnivariateRealSolver solver = new NewtonSolver();
    result = solver.solve(f, -0.2, 0.2);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, -0.1, 0.3);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, -0.3, 0.45);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.3, 0.7);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.2, 0.6);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.05, 0.95);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 1.25);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.8, 1.2);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 1.75);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.55, 1.45);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 5);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
}
 
Example #25
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 */
public void testSinZero() throws MathException {
    DifferentiableUnivariateRealFunction f = new SinFunction();
    double result;

    UnivariateRealSolver solver = new NewtonSolver();
    result = solver.solve(f, 3, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 1, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    assertEquals(result, solver.getResult(), 0);
    assertTrue(solver.getIterationCount() > 0);
}
 
Example #26
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Deprecated
public void testDeprecated() throws MathException {
    DifferentiableUnivariateRealFunction f = new SinFunction();
    double result;

    UnivariateRealSolver solver = new NewtonSolver(f);
    result = solver.solve(3, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    result = solver.solve(1, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    assertEquals(result, solver.getResult(), 0);
    assertTrue(solver.getIterationCount() > 0);
}
 
Example #27
Source File: NewtonSolver.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Find a zero near the value <code>startValue</code>.
 *
 * @param f the function to solve
 * @param min the lower bound for the interval (ignored).
 * @param max the upper bound for the interval (ignored).
 * @param startValue the start value to use.
 * @return the value where the function is zero
 * @throws MaxIterationsExceededException if the maximum iteration count is exceeded
 * @throws FunctionEvaluationException if an error occurs evaluating the
 * function or derivative
 * @throws IllegalArgumentException if startValue is not between min and max or
 * if function is not a {@link DifferentiableUnivariateRealFunction} instance
 */
public double solve(final UnivariateRealFunction f,
                    final double min, final double max, final double startValue)
    throws MaxIterationsExceededException, FunctionEvaluationException {

    try {

        final UnivariateRealFunction derivative =
            ((DifferentiableUnivariateRealFunction) f).derivative();
        clearResult();
        verifySequence(min, startValue, max);

        double x0 = startValue;
        double x1;

        int i = 0;
        while (i < maximalIterationCount) {

            x1 = x0 - (f.value(x0) / derivative.value(x0));
            if (Math.abs(x1 - x0) <= absoluteAccuracy) {
                setResult(x1, i);
                return x1;
            }

            x0 = x1;
            ++i;
        }

        throw new MaxIterationsExceededException(maximalIterationCount);
    } catch (ClassCastException cce) {
        throw MathRuntimeException.createIllegalArgumentException("function is not differentiable");
    }
}
 
Example #28
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 */
public void testQuinticZero() throws MathException {
    DifferentiableUnivariateRealFunction f = new QuinticFunction();
    double result;

    UnivariateRealSolver solver = new NewtonSolver();
    result = solver.solve(f, -0.2, 0.2);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, -0.1, 0.3);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, -0.3, 0.45);
    assertEquals(result, 0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.3, 0.7);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.2, 0.6);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.05, 0.95);
    assertEquals(result, 0.5, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 1.25);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.8, 1.2);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 1.75);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.55, 1.45);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 0.85, 5);
    assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
}
 
Example #29
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 *
 */
public void testSinZero() throws MathException {
    DifferentiableUnivariateRealFunction f = new SinFunction();
    double result;

    UnivariateRealSolver solver = new NewtonSolver();
    result = solver.solve(f, 3, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    result = solver.solve(f, 1, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    assertEquals(result, solver.getResult(), 0);
    assertTrue(solver.getIterationCount() > 0);
}
 
Example #30
Source File: NewtonSolverTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Deprecated
public void testDeprecated() throws MathException {
    DifferentiableUnivariateRealFunction f = new SinFunction();
    double result;

    UnivariateRealSolver solver = new NewtonSolver(f);
    result = solver.solve(3, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    result = solver.solve(1, 4);
    assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());

    assertEquals(result, solver.getResult(), 0);
    assertTrue(solver.getIterationCount() > 0);
}