org.apache.commons.math.analysis
Class BrentSolver

java.lang.Object
  extended by org.apache.commons.math.analysis.UnivariateRealSolverImpl
      extended by org.apache.commons.math.analysis.BrentSolver
All Implemented Interfaces:
java.io.Serializable, UnivariateRealSolver

public class BrentSolver
extends UnivariateRealSolverImpl

Implements the Brent algorithm for finding zeros of real univariate functions.

The function should be continuous but not necessarily smooth.

Version:
$Revision: 617826 $ $Date: 2008-02-02 09:39:18 -0700 (Sat, 02 Feb 2008) $
See Also:
Serialized Form

Field Summary
private static long serialVersionUID
          Serializable version identifier
 
Fields inherited from class org.apache.commons.math.analysis.UnivariateRealSolverImpl
absoluteAccuracy, defaultAbsoluteAccuracy, defaultFunctionValueAccuracy, defaultMaximalIterationCount, defaultRelativeAccuracy, f, functionValueAccuracy, iterationCount, maximalIterationCount, relativeAccuracy, result, resultComputed
 
Constructor Summary
BrentSolver(UnivariateRealFunction f)
          Construct a solver for the given function.
 
Method Summary
 double solve(double min, double max)
          Find a zero in the given interval.
 double solve(double min, double max, double initial)
          Find a zero in the given interval with an initial guess.
private  double solve(double x0, double y0, double x1, double y1, double x2, double y2)
          Find a zero starting search according to the three provided points.
 
Methods inherited from class org.apache.commons.math.analysis.UnivariateRealSolverImpl
clearResult, getAbsoluteAccuracy, getFunctionValueAccuracy, getIterationCount, getMaximalIterationCount, getRelativeAccuracy, getResult, isBracketing, isSequence, resetAbsoluteAccuracy, resetFunctionValueAccuracy, resetMaximalIterationCount, resetRelativeAccuracy, setAbsoluteAccuracy, setFunctionValueAccuracy, setMaximalIterationCount, setRelativeAccuracy, setResult, verifyBracketing, verifyInterval, verifySequence
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
Serializable version identifier

See Also:
Constant Field Values
Constructor Detail

BrentSolver

public BrentSolver(UnivariateRealFunction f)
Construct a solver for the given function.

Parameters:
f - function to solve.
Method Detail

solve

public double solve(double min,
                    double max,
                    double initial)
             throws MaxIterationsExceededException,
                    FunctionEvaluationException
Find a zero in the given interval with an initial guess.

Throws IllegalArgumentException if the values of the function at the three points have the same sign (note that it is allowed to have endpoints with the same sign if the initial point has opposite sign function-wise).

Parameters:
min - the lower bound for the interval.
max - the upper bound for the interval.
initial - the start value to use (must be set to min if no initial point is known).
Returns:
the value where the function is zero
Throws:
MaxIterationsExceededException - the maximum iteration count is exceeded
FunctionEvaluationException - if an error occurs evaluating the function
java.lang.IllegalArgumentException - if initial is not between min and max (even if it is a root)

solve

public double solve(double min,
                    double max)
             throws MaxIterationsExceededException,
                    FunctionEvaluationException
Find a zero in the given interval.

Requires that the values of the function at the endpoints have opposite signs. An IllegalArgumentException is thrown if this is not the case.

Parameters:
min - the lower bound for the interval.
max - the upper bound for the interval.
Returns:
the value where the function is zero
Throws:
MaxIterationsExceededException - if the maximum iteration count is exceeded
FunctionEvaluationException - if an error occurs evaluating the function
java.lang.IllegalArgumentException - if min is not less than max or the signs of the values of the function at the endpoints are not opposites

solve

private double solve(double x0,
                     double y0,
                     double x1,
                     double y1,
                     double x2,
                     double y2)
              throws MaxIterationsExceededException,
                     FunctionEvaluationException
Find a zero starting search according to the three provided points.

Parameters:
x0 - old approximation for the root
y0 - function value at the approximation for the root
x1 - last calculated approximation for the root
y1 - function value at the last calculated approximation for the root
x2 - bracket point (must be set to x0 if no bracket point is known, this will force starting with linear interpolation)
y2 - function value at the bracket point.
Returns:
the value where the function is zero
Throws:
MaxIterationsExceededException - if the maximum iteration count is exceeded
FunctionEvaluationException - if an error occurs evaluating the function