0 Replies - 1410 Views - Last Post: 24 October 2011 - 05:52 AM

#1 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Secant Root Finder

Posted 24 October 2011 - 05:52 AM

Description: Create an object of SecantRootFinder, passing the maximum number of iterations and the accuracy in which the result must be within. Then call the FindRoot method, with a Function object, along with upper and lower bounds. If the root is not within the specified accuracy after the maximum number of iterations has been exceeded, an exception will be thrown.Find the roots of a function using the Secant method of root finding.
// Represents a function for use
// in root finding
public interface Function
{
    // Evaluate the function at 'x'
    double eval(double x);
}


// Find the roots of a function using
// the Secant method
public class SecantRootFinder
{
    // The maximum of iterations of the
    // Secant method to use
    private int maxIterations;

    // Accuracy of returned roots
    private double tolerance;

    // Create new object, setting values for
    // iterations and tolerance
    public SecantRootFinder(int maxIterations, double accuracy)
    {
	this.maxIterations = maxIterations;
	this.tolerance = accuracy;
    }

    // Return maximum number of iterations
    public int getMaxIterations()
    {
	return maxIterations;
    }

    // Set the maximum number of iterations
    public void setMaxIterations(int maxIterations)
    {
	this.maxIterations = maxIterations;
    }

    // Return root accuracy
    public double getTolerance()
    {
	return tolerance;
    }

    // Set the maximum number of iterations
    public void setTolerance(double accuracy)
    {
	this.tolerance = accuracy;
    }

    // Find a root of the Function f, with lower and upper bounds
    public double FindRoot(Function f, double lower, double upper)
    {
	double a = lower;
	double b = upper;

	double fb = f.eval(b);

	int iteration = 0;

	while (iteration < maxIterations)
	{
	    double x = b - (b - a) * fb / (fb - f.eval(a));
	    a = b;
	    b = x;
	    fb = f.eval(b);

	    if (Math.abs(fb) < tolerance)
	    {
		// Return root if within tolerance
		return x;
	    }

            iteration++;
	}

	// Throw exception if root is not found within tolerance
	throw new ArithmeticException("Unable to find a root within specified tolerance after " + maxIterations + " iterations");
    }
}

public class Example
{
    public static void main(String[] args)
    {
	SecantRootFinder finder = new SecantRootFinder(15, 1E-3);

	Function func = new Function()
	{
	    public double eval(double x)
	    {
		// Roots at -2 and -6
		return x * x - 8 * x + 12;
	    }
	};

	try
	{
	    double root = finder.FindRoot(func, -8, -4);
	    System.out.println(root);

	    finder.setMaxIterations(5);

	    root = finder.FindRoot(func, -5, 4);
	    System.out.println(root);
	}
	catch (ArithmeticException e)
	{
	    System.out.println(e.getMessage());
	}

    }
}

// OUTPUT
//
// 1.9999988944665041
// Unable to find a root within specified tolerance after 5 iterations





Is This A Good Question/Topic? 0
  • +

Page 1 of 1