Class RootFinder

java.lang.Object
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 contains 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.
  • Field Details

    • function

      private Function function
      The equation whose zeroes are desired
    • x1

      private double x1
    • x2

      private double x2
  • 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,10)
    • 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, 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.
  • Method Details

    • setX1

      public void setX1(double x1)
    • getX1

      public double getX1()
    • setX2

      public void setX2(double x2)
    • getX2

      public double getX2()
    • parseFunction

      public Function parseFunction(String expression)
      Method that processes the format that this software will recognize for user input of an integral expression. The general format is: expression,lowerLimit,upperLimit,iterations(optional)
      Actually, the lower-limit and upper-limit values specified does not guarantee that the root returned will be between the values specified. In fact, the root could be far away from the values specified.



      Example... Using anonymous functions:
      Parameters:
      expression - The expression containing the function to integrate and the lower and upper boundaries of integration.
      Returns:
      an array containing:: At index 0.....the expression to expand. At index 1.....the lower limit of expansion. At index 2.....the upper limit of expansion. At index 3(optional)...the degree of the polynomial up to which the function should be expanded.
    • 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 intg(funcName,x1,x2) or intg(funcName,x1,x2,iterations)
    • isValid

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

      public void setFunction(Function function)
    • getFunction

      public Function getFunction()
    • getVariable

      public String getVariable()
    • findRoots

      public String 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 2000 counts, it switches to another form of the same algorithm which will run for another 2000 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)
    • main

      public static void main(String[] args)