Class RootFinder

java.lang.Object
com.github.gbenroscience.math.numericalmethods.RootFinder

public class RootFinder extends Object
Objects of this class are used to solve for the roots of non-implicit equations. They combine a variety of algorithms in an if-fail-switch-algorithm fashion to iteratively deduce the roots.
  1. The Secant Algorithm.

    The first algorithm is the secant algorithm. It requires 2 values of x between which it seeks out the root. If it does not find it in the specified range however, it may search outside the range. If the secant fails, the object automatically switches over to another of its methods...

  2. The Bisection Algorithm.

    This algorithm searches for a root ONLY within the specified range and returns one if it exists. However if it fails, the object again automatically switches over to an highly unpredictable algorithm called here:

  3. The Self-Evaluating Algorithm.

    Two variants of these are used here and due to its unstable nature,it is the algorithm of last resort. It searches for a root starting at the first limit specified for x but the direction of search is not guaranteed due to its instability.

If both flavors of this algorithm fail, then an error report is generated. The search for the root is intensive and if no root is found, it is usually because no real root exists for the function in the specified range.

Usage:
The input that initializes objects of this class is a String value that containsAlgebraicFunction information about the function whose roots we seek and the range in which we need to search for the function. Always specify 2 values for the range,please. If the variable has been initialized before in the workspace and is visible to the currently evaluating object of this class then an example could be:
2x^3-5x+sin(x)-1=0,-3,5
This will try to seek out the zeroes of 2x^3-5x+sin(x)-1 between x = -3 and x = 5 depending on the algorithm in use. If however, the variable has not been initialized before in the workspace or has been initialized but is not visible to the currently evaluating object of this class then an example could be:
x=0;2x^3-5x+sin(x)-1=0,-3,5
CAUTION!!!!! Always end your equations with "=0"
Objects of this class assume that you do and make calculations based on that.
Author:
JIBOYE Oluwagbemiro Olaoluwa
  • Field Details

  • Constructor Details

    • RootFinder

      public RootFinder(String expression)
      Parameters:
      expression - An input expression containing information about the function whose roots are needed. e.g. var x=0;//initialization root(@(x)3sin(x)-4x+1,2) root(@(x)3sin(x)-4x+1,2,8) root(@(x)3sin(x)-4x+1,2,8,2000), x1, x2 and iterations
    • RootFinder

      public RootFinder(String function, double x1)
      Parameters:
      function - A String to be used to initialize the Function attribute of this class. It could be anonymous e.g @(x)sin(x+1) or be the name of a pre-defined function.
      x1 - A starting value for iteration...will automatically be set to the smaller value of the boundaries.
    • RootFinder

      public RootFinder(Function function)
    • RootFinder

      public RootFinder(Function function, double x1)
      Parameters:
      function - A String to be used to initialize the Function attribute of this class. It could be anonymous e.g @(x)sin(x+1) or be the name of a pre-defined function.
      x1 - A starting value for iteration...will automatically be set to the smaller value of the boundaries.
    • RootFinder

      public RootFinder(Function function, double x1, double x2)
      Parameters:
      function - A String to be used to initialize the Function attribute of this class. It could be anonymous e.g @(x)sin(x+1) or be the name of a pre-defined function.
      x1 - A starting value for iteration...will automatically be set to the smaller value of the boundaries.
      x2 -
    • RootFinder

      public RootFinder(Function function, double x1, double x2, int iterations)
      Parameters:
      function - A Function to be used to initialize the Function attribute of this class.
      x1 - A starting value for iteration...will automatically be set to the smaller value of the boundaries.
      x2 - A bounding value for the iteration. The roots are not bound to lie between x1 and x2.
      iterations - The number of times a particular root finding method of this class will run before it switches to another method
  • Method Details

    • setX1

      public void setX1(double x1)
    • getX1

      public double getX1()
    • setX2

      public void setX2(double x2)
    • getX2

      public double getX2()
    • extractFunctionStringFromExpression

      public static void extractFunctionStringFromExpression(List<String> list)
      Analyzes the list and extracts the Function string from it.
      Parameters:
      list - The list to be analyzed. Direct examples would be: root(@sin(x+1),4,7) root(F,4,7) where F is a function that has been defined before in the workspace.. and so on. Simplifies the list to the form root(funcName,x1,x2) or root(funcName,x1,x2,iterations)
    • isValid

      public boolean isValid()
      Returns:
      true if the equation is valid.
    • setFunction

      public final void setFunction(Function function)
    • getFunction

      public Function getFunction()
    • getVariable

      public String getVariable()
    • findRoots

      public double findRoots()
      This method starts with the secant algorithm and if this fails, switches to the bisection algorithm, which if it does not succeed switches to a form of the self-evaluating algorithm. If this final attempt does not return a result after about iterations counts, it switches to another form of the same algorithm which will run for another iterations counts and if it fails, will deliver an error report or switch to another iterative method when this becomes available.
      Returns:
      the root of the equation.
    • approxEqualsZero

      public boolean approxEqualsZero(double number)
    • lenientApproxEqualsZero

      public boolean lenientApproxEqualsZero(double number)
    • setIterations

      public void setIterations(int iterations)
      Sets the number of iterations before switching to another method
      Parameters:
      iterations - The number of iterations before switching to another method
    • getIterations

      public int getIterations()
      Returns:
      The number of iterations before switching to another method
    • main

      public static void main(String[] args)