Class RootFinder
-
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...
-
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:
-
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.
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 Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionRootFinder(Function function) RootFinder(Function function, double x1) RootFinder(Function function, double x1, double x2) RootFinder(Function function, double x1, double x2, int iterations) RootFinder(String expression) RootFinder(String function, double x1) -
Method Summary
Modifier and TypeMethodDescriptionbooleanapproxEqualsZero(double number) static voidAnalyzes the list and extracts the Function string from it.doubleThis 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.intdoublegetX1()doublegetX2()booleanisValid()booleanlenientApproxEqualsZero(double number) static voidfinal voidsetFunction(Function function) voidsetIterations(int iterations) Sets the number of iterations before switching to another methodvoidsetX1(double x1) voidsetX2(double x2)
-
Field Details
-
DEFAULT_ITERATIONS
public static final int DEFAULT_ITERATIONS- See Also:
-
-
Constructor Details
-
RootFinder
- 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
- 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
-
RootFinder
- 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
- 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
- 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
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
-
getFunction
-
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 aboutiterationscounts, it switches to another form of the same algorithm which will run for anotheriterationscounts 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
-