Class Roots
- java.lang.Object
- 
- jahuwaldt.tools.math.Roots
 
- 
 public class Roots extends java.lang.Object A collection of static routines to find the roots of functions or sets of functions.Modified by: Joseph A. Huwaldt - Version:
- September 11, 2015
- Author:
- Joseph A. Huwaldt, Date: October 8, 1997
 
- 
- 
Constructor SummaryConstructors Constructor Description Roots()
 - 
Method SummaryAll Methods Static Methods Concrete Methods Modifier and Type Method Description static doubleaitken(Evaluatable1D eval, double x, double tol)Find a root of a 1D equation using the Aitken method.static intbracket(Evaluatable1D func, double x1, double x2, int n, double[] xb1, double[] xb2)Given a function,func, defined on the intervalx1tox2, this routine subdivides the interval intonequally spaced segments, and searches for zero crossings of the function.static doublefindRoot1D(Evaluatable1D eval, double x1, double x2, double tol)Find the root of a general 1D functionf(x) = 0known to lie betweenx1andx2.static doublefindRoot1D(Evaluatable1D eval, BracketRoot1D bracket, double tol)Find the root of a general 1D functionf(x) = 0known to lie betweenx1andx2.static booleanfindRootsND(VectorFunction vecfunc, double[] x, int n)Find the roots of a set ofNnon-linear equations inNvariables.static voidmain(java.lang.String[] args)Used to test out the methods in this class.
 
- 
- 
- 
Constructor Detail- 
Rootspublic Roots() 
 
- 
 - 
Method Detail- 
bracketpublic static int bracket(Evaluatable1D func, double x1, double x2, int n, double[] xb1, double[] xb2) throws RootException Given a function,func, defined on the intervalx1tox2, this routine subdivides the interval intonequally spaced segments, and searches for zero crossings of the function. Brackets around any zero crossings found are returned.- Parameters:
- func- The function that is being search for zero crossings.
- x1- The start of the interval to be searched.
- x2- The end of the interval to be searched.
- n- The number segments to divide the interval into.
- xb1- Lower value of each bracketing pair found (number of pairs is returned by function). The size of the array determines the maximum number of bracketing pairs that will be found.
- xb2- Upper value of each bracketing pair found (number of pairs is returned by function). The size of this array must match the size of- xb1.
- Returns:
- The number of bracketing pairs found.
- Throws:
- RootException- if the supplied function can not accept the interval provided.
 
 - 
findRoot1Dpublic static double findRoot1D(Evaluatable1D eval, BracketRoot1D bracket, double tol) throws RootException Find the root of a general 1D functionf(x) = 0known to lie betweenx1andx2. The root will be refined until it's accuracy istol.Have your Evaluatable1D derivative()function returnDouble.NaNwhen you want this routine to use Brent's method. Newton-Raphson will be used if a derivative function is provided that returns something other thanDouble.NaN. Thefunction()method will always be called before thederivative()method. The value returned from the 1st call to thefunction()method is ignored.- Parameters:
- eval- An evaluatable 1D function that returns the function value at x and optionally returns the function derivative value (d(fx)/dx) at x. It is guaranteed that "function()" will always be called before "derivative()".
- bracket- The upper and lower bracket surrounding the root (assumed that root lies inside the bracket).
- tol- The root will be refined until it's accuracy is better than this.
- Returns:
- The value of x that solves the equation f(x) = 0.
- Throws:
- RootException- if unable to find a root of the function.
 
 - 
findRoot1Dpublic static double findRoot1D(Evaluatable1D eval, double x1, double x2, double tol) throws RootException Find the root of a general 1D functionf(x) = 0known to lie betweenx1andx2. The root will be refined until it's accuracy istol.Have your Evaluatable1D derivative()function returnDouble.NaNwhen you want this routine to use Brent's method. Newton-Raphson will be used if a derivative function is provided that returns something other thanDouble.NaN. Thefunction()method will always be called before thederivative()method. The value returned from the 1st call to thefunction()method is ignored.- Parameters:
- eval- An evaluatable 1D function that returns the function value at x and optionally returns the function derivative value (d(fx)/dx) at x. It is guaranteed that "function()" will always be called before "derivative()".
- x1- The lower bracket surrounding the root (assumed that root lies between x1 and x2).
- x2- The upper bracket surrounding the root (assumed that root lies between x1 and x2).
- tol- The root will be refined until it's accuracy is better than this.
- Returns:
- The value of x that solves the equation f(x) = 0.
- Throws:
- RootException- Unable to find a root of the function.
 
 - 
aitkenpublic static double aitken(Evaluatable1D eval, double x, double tol) throws RootException Find a root of a 1D equation using the Aitken method.- Parameters:
- eval- An evaluatable 1D function that returns the function value at x and optionally returns the function derivative value (d(fx)/dx) at x. This method never calls "derivative()".
- x- A starting guess at the root value.
- tol- The root will be refined until it's accuracy is better than this.
- Returns:
- The value of x that solves the equation f(x) = 0 to within the specified tolerance.
- Throws:
- RootException- if unable to find a root of the function.
 
 - 
findRootsNDpublic static boolean findRootsND(VectorFunction vecfunc, double[] x, int n) throws RootException Find the roots of a set ofNnon-linear equations inNvariables.Uses a globally convergent Newton's method with either a Jacobian supplied by the VectorFunction or one computed by differencing. Reference: Numerical Recipes in C, 2nd Edition, pg 386. - Parameters:
- vecfunc- A VectorFunction that computes the values for each equation using the input values in x[] and optionally the Jacobian.
- x- A pre-existing array that contains the initial guesses at the x values. On completion, it will contain the roots of the equations.
- n- The number of equations to be solved (and the number of elements in x.
- Returns:
- falseif the routine completed normally or- trueif the routine has converged to a local minimum of the "fmin" function. In this case, try restarting from a different initial guess.
- Throws:
- RootException- Unable to find the roots of the functions.
 
 - 
mainpublic static void main(java.lang.String[] args) Used to test out the methods in this class.- Parameters:
- args- Command line arguments (not used).
 
 
- 
 
-