Zeros of Functions
Bisection Method Explained
Graphically
Main Classes Involved
Notes on the Classes
OneVariableFunction is an interface class
PolynomialFunction is a concrete class that implements class OneVariableFunction.
It accepts the function in the form c[0] + c[1] * x + c[2] * x^2 + ....
IterativeProcess is an abstract class. It allows you to
Set the maximum number of iterations
Set the desired precision
FunctionalIterator is an abstract class that extends IterativeProcess. It is an iterator based on a
one-variable function having a single numerical result
BisectionZeroFinder is a concrete class that extends class FunctionalIterator.
The class is listed below
//CLASS BISECTIONZEROFINDER: PARTIAL LISTING
import OneVariableFunction;
public class BisectionZeroFinder extends FunctionalIterator
{
private double xNeg; //Value at which the function's value is negative.
private double xPos; //Value at which the function's value is positive
public BisectionZeroFinder(DhbInterfaces.OneVariableFunction func) {super(func);}
//NOTE: super above is calling the constructor of the class above BisectioZeroFinder which is
//FunctionalIterator. The constructor being called is
// public FunctionalIterator(OneVariableFunction func)
Note that there is a typo in code example 5.2.3. The corrected driver is given below
/**********************************************************************************************
Finding the root of the equation f(x) = erf(x) - 0.9. Note that this function involves an
integral and is defined on page 57 of the text. EXAMPLE 2 IS A BETTER EXAMPLE
**********************************************************************************************/
import alpcentauri.*;
class FindingZero
{
public static void main(String[] args)
{
BisectionZeroFinder zeroFinder = new BisectionZeroFinder(new
OneVariableFunction() {public double value(double x)
{return NormalDistribution.errorFunction(x)-0.9;}});
try {
zeroFinder.setNegativeX(0);
zeroFinder.setPositiveX(5);
}
catch(IllegalArgumentException e)
{
return;
}
zeroFinder.evaluate();
double result = zeroFinder.getResult();
System.out.println("The root is at: "+ result);
}
}
Bisection Method Example 2
//the following evaluates the function f(x) = x2 – 4 between 1 (function is negative) and //3 (function is positive)
import alpcentauri.*;
class Bisection
{
public static void main (String [] args)
{
double [] coefficients = {-4, 0, 1};
BisectionZeroFinder zeroFinder = new BisectionZeroFinder(new
PolynomialFunction (coefficients));
try
{
zeroFinder.setNegativeX(1);
zeroFinder.setPositiveX(3);
}
catch ( IllegalArgumentException e)
{
return;
}
zeroFinder.evaluate();
double result = zeroFinder.getResult();
System.out.println("The root is at x = : "+result);
}
}
Bisection Method Example 3
There is a public method named getIterations in the IterativeProcess abstract class. Since it is abstract, an object of the class cannot be made.
The FunctionIterator class inherits from IterativeProcess but FunctionIterator is also abstract.
BisectionZeroFinder is a concrete class that inherits from FunctionIterator.
An object of BisectionZeroFinder can therefore be made in order to access the number of iterations performed.
The following 2 lines of code can be added to example 3 in order to obtain the number of iterations performed.
double iterations = zeroFinder.getIterations();
System.out.println(iterations + " iterations were performed");
Output
import alpcentauri.*;
class Newton
{
public static void main (String [] args)
{
double [] coefficients = {-4, 0, 1};
NewtonZeroFinder zeroFinder = new NewtonZeroFinder(new
PolynomialFunction (coefficients),1);
zeroFinder.evaluate();
double result = zeroFinder.getResult();
System.out.println("The root is at x = : "+result);
}
}
¢ In comparison, the bisection algorithm is slow, Newton's algorithm is faster.
¢ Newton is better for most functions if you can provide a close enough value to the root.
¢ Bisection is rock solid and will always converge over an interval in which there are no singularities.
Exercise 1
¢ Use the Bisection method to find the root of f(x) = x2 - 9 that lies between -2 and -4
¢ Repeat the above problem using Newton's method.
¢ For each method, find the number of iterations.
Exercise 2
¢ In a large number of problems (if not most) it is a good idea to develop a rough schetch of the function being evaluated.
Provide a rough sketch of the function f(x) = x2|sin x| - 4 in the region for x between 0 and 5 including the end points
¢ Use the Bisection method to find the smallest positive root of the function f(x) = x2|sin x| - 4 in the above region.
¢ Repeat the above problem using Newton's method.
¢ For each method, find the number of iterations.
¢ State which method you consider to be the best for this problem and why.
¢ Research.
The following methods for finding roots are not discussed in your text. Describe these methods and provide a comparison, in terms of strengths and weaknesses, when compared with the bisection and Newton methods. This problem is discussion only - it does not involve solving the above problem using the methods.
Ø Secant Method
Ø False Position Method
Ø Ridder's Method