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 Summary
Constructors Constructor Description Roots()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static double
aitken(Evaluatable1D eval, double x, double tol)
Find a root of a 1D equation using the Aitken method.static int
bracket(Evaluatable1D func, double x1, double x2, int n, double[] xb1, double[] xb2)
Given a function,func
, defined on the intervalx1
tox2
, this routine subdivides the interval inton
equally spaced segments, and searches for zero crossings of the function.static double
findRoot1D(Evaluatable1D eval, double x1, double x2, double tol)
Find the root of a general 1D functionf(x) = 0
known to lie betweenx1
andx2
.static double
findRoot1D(Evaluatable1D eval, BracketRoot1D bracket, double tol)
Find the root of a general 1D functionf(x) = 0
known to lie betweenx1
andx2
.static boolean
findRootsND(VectorFunction vecfunc, double[] x, int n)
Find the roots of a set ofN
non-linear equations inN
variables.static void
main(java.lang.String[] args)
Used to test out the methods in this class.
-
-
-
Constructor Detail
-
Roots
public Roots()
-
-
Method Detail
-
bracket
public static int bracket(Evaluatable1D func, double x1, double x2, int n, double[] xb1, double[] xb2) throws RootException
Given a function,func
, defined on the intervalx1
tox2
, this routine subdivides the interval inton
equally 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 ofxb1
.- Returns:
- The number of bracketing pairs found.
- Throws:
RootException
- if the supplied function can not accept the interval provided.
-
findRoot1D
public static double findRoot1D(Evaluatable1D eval, BracketRoot1D bracket, double tol) throws RootException
Find the root of a general 1D functionf(x) = 0
known to lie betweenx1
andx2
. The root will be refined until it's accuracy istol
.Have your Evaluatable1D
derivative()
function returnDouble.NaN
when 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.
-
findRoot1D
public static double findRoot1D(Evaluatable1D eval, double x1, double x2, double tol) throws RootException
Find the root of a general 1D functionf(x) = 0
known to lie betweenx1
andx2
. The root will be refined until it's accuracy istol
.Have your Evaluatable1D
derivative()
function returnDouble.NaN
when 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.
-
aitken
public 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.
-
findRootsND
public static boolean findRootsND(VectorFunction vecfunc, double[] x, int n) throws RootException
Find the roots of a set ofN
non-linear equations inN
variables.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:
false
if the routine completed normally ortrue
if 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.
-
main
public static void main(java.lang.String[] args)
Used to test out the methods in this class.- Parameters:
args
- Command line arguments (not used).
-
-