## org.opensourcephysics.numerics Class Root

```java.lang.Object
org.opensourcephysics.numerics.Root
```

`public class Rootextends java.lang.Object`

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

Author:
Wolfgang Christian

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[][]` ```getJacobian(VectorFunction feqs, int n, double[] xx, double tol)```
Computes the Jacobian using a finite difference approximation.
`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` ```newtonBisection(Function f, double xleft, double xright, double tol, int icmax)```
Implements Newton's method for finding the root but switches to the bisection method if the the estimate is not between xleft and xright.
`static double` ```newtonMultivar(VectorFunction feqs, double[] xx, int max, double tol)```

`static double[][]` `polynomial(double[] c)`
Solves for the roots of the polynomial with the given coefficients c: c[0] + c[1] * x + c[2] * x^2 + ....
`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.

### polynomial

`public static double[][] polynomial(double[] c)`
Solves for the roots of the polynomial with the given coefficients c: c[0] + c[1] * x + c[2] * x^2 + .... The roots are the complex eigenvalues of the companion matrix.

Parameters:
`double[]` - c coefficients
Returns:
double[][] complex 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 the central difference approximation.

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

### newtonBisection

```public static double newtonBisection(Function f,
double xleft,
double xright,
double tol,
int icmax)```
Implements Newton's method for finding the root but switches to the bisection method if the the estimate is not between xleft and xright. Method contributed by: J E Hasbun A Newton Raphson result is accepted if it is within the known bounds, else a bisection step is taken. Ref: Computational Physics by P. L. Devries (J. Wiley, 1993) input: [xleft,xright] is the interval wherein fx() has a root, icmax is the maximum iteration number, and tol is the tolerance level output: returns xbest as the value of the function Reasonable values of icmax and tol are 25, 5e-3. Returns the root or NaN if root not found.

Parameters:
`xleft` - double
`xright` - double
`tol` - double tolerance
`icmax` - int number of trials
Returns:
double the root

### newtonMultivar

```public static double newtonMultivar(VectorFunction feqs,
double[] xx,
int max,
double tol)```

### getJacobian

```public static double[][] getJacobian(VectorFunction feqs,
int n,
double[] xx,
double tol)```
Computes the Jacobian using a finite difference approximation. Contributed to OSP by J E Hasbun 2007.

Parameters:
`feqs` - VectorFunction - the function containing n equations
`n` - int - number of equations
`xx` - double[] - the variable array at which the Jacobian is calculated
`tol` - double - the small change to find the derivatives
Returns:
double[][] J - the square matrix containing the Jacobian