org.opensourcephysics.numerics
Class Root

java.lang.Object
  extended byorg.opensourcephysics.numerics.Root

public class Root
extends java.lang.Object

Class Root defines various root finding algorithms. This class cannot be subclassed or instantiated because all methods are static.


Method Summary
static double bisection(Function f, double x1, double x2, double tol)
          Implements the bisection method for finding the root of a function.
static double[][] cubic(double a, double b, double c, double d)
          Solves for the roots of the cubic equation ax3+bx2+cx+d=0.
static double newton(Function f, double x, double tol)
          Implements Newton's method for finding the root of a function.
static double newton(Function f, Function df, double x, double tol)
          Implements Newton's method for finding the root of a function.
static double[][] quadratic(double a, double b, double c)
          Solves for the complex roots of the quadratic equation ax2+bx+c=0.
static double[] quadraticReal(double a, double b, double c)
          Solves for the real roots of the quadratic equation ax2+bx+c=0.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

quadraticReal

public static double[] quadraticReal(double a,
                                     double b,
                                     double c)
Solves for the real roots of the quadratic equation ax2+bx+c=0.

Parameters:
a - double quadratic term coefficient
b - double linear term coefficient
c - double constant term
Returns:
double[] an array containing the two roots.

quadratic

public static double[][] quadratic(double a,
                                   double b,
                                   double c)
Solves for the complex roots of the quadratic equation ax2+bx+c=0.

Parameters:
a - double quadratic term coefficient
b - double linear term coefficient
c - double constant term
Returns:
double[] an array containing the two roots.

cubic

public static double[][] cubic(double a,
                               double b,
                               double c,
                               double d)
Solves for the roots of the cubic equation ax3+bx2+cx+d=0.

Parameters:
a - double cubic term coefficient
b - double quadratic term coefficient
c - double linear term coefficient
d - double constant term
Returns:
double[] an array containing the two roots.

newton

public static double newton(Function f,
                            double x,
                            double tol)
Implements Newton's method for finding the root of a function. The derivative is calculated numerically using Romberg's method.

Parameters:
f - Function the function
x - double guess the root
tol - double computation tolerance
Returns:
double the root or NaN if root not found.

newton

public static double newton(Function f,
                            Function df,
                            double x,
                            double tol)
Implements Newton's method for finding the root of a function.

Parameters:
f - Function the function
df - Function the derivative of the function
x - double guess the root
tol - double computation tolerance
Returns:
double the root or NaN if root not found.

bisection

public static double bisection(Function f,
                               double x1,
                               double x2,
                               double tol)
Implements the bisection method for finding the root of a function.

Parameters:
f - Function the function
x1 - double lower
x2 - double upper
tol - double computation tolerance
Returns:
double the root or NaN if root not found