0 Replies - 1484 Views - Last Post: 04 November 2011 - 09:03 AM

#1 Ryano121   User is offline

  • D.I.C Lover
  • member icon

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

False Position Root Finder

Posted 04 November 2011 - 09:03 AM

Description: Create an instance of the FalsePositionRootFinder class, passing in the maximum number of iterations and tolerance. Then Call the .FindRoot method, passing in a Function object with the lower and upper bounds. If a root could not be found within the specified number of iterations, an exception will be thrown. See the Example class for full implementation.Use the False Position method to find the roots of an equation.
// 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 False Position method
public class FalsePositionRootFinder
{
    // The maximum of iterations of the
    // False Position method to use
    private int maxIterations;

    // Accuracy of returned roots
    private double tolerance;

    // Create new object, setting values for
    // iterations and tolerance
    public FalsePositionRootFinder(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 fa = f.eval(a);

	int iteration = 1;

	while (iteration <= maxIterations)
	{
	    iteration++;

	    double x = a - (b - a) * fa / (f.eval(b) - fa);
	    double fx = f.eval(x);

	    if (fa * fx > 0)
	    {
		a = x;
	    }
	    else
	    {
		b = x;
	    }

	    if (Math.abs(fx) < tolerance)
	    {
		return x;
	    }
	}

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

// EXAMPLE USAGE

public class Example
{
    public static void main(String[] args)
    {
	FalsePositionRootFinder finder = new FalsePositionRootFinder(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
//
// -5.999796789270474
// Unable to find a root within specified tolerance after 5 iterations





Is This A Good Question/Topic? 0
  • +

Page 1 of 1