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 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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate class
private class
private class
private class
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionRootFinder
(String expression) RootFinder
(String function, double x1) RootFinder
(Function function, double x1) -
Method Summary
Modifier and TypeMethodDescriptionboolean
approxEqualsZero
(double number) static void
Analyzes the list and extracts the Function string from it.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.double
getX1()
double
getX2()
boolean
isValid()
boolean
lenientApproxEqualsZero
(double number) static void
parseFunction
(String expression) Method that processes the format that this software will recognize for user input of an integral expression.void
setFunction
(Function function) void
setX1
(double x1) void
setX2
(double x2)
-
Field Details
-
function
The equation whose zeroes are desired -
x1
private double x1 -
x2
private double x2
-
-
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,10)
-
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.
-
-
Method Details
-
setX1
public void setX1(double x1) -
getX1
public double getX1() -
setX2
public void setX2(double x2) -
getX2
public double getX2() -
parseFunction
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
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
-
getFunction
-
getVariable
-
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
-