0 Replies - 1544 Views - Last Post: 13 November 2011 - 01:53 PM

#1 Ryano121   User is offline

  • D.I.C Lover
  • member icon

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

Newton Raphson Root Finder

Posted 13 November 2011 - 01:53 PM

Description: Create an instance of the NewtonRaphsonRootFinder class, passing in the maximum number of iterations and tolerance. Then Call the .FindRoot method, passing in a Function object representing the original function, a second representing the derivative of that function and an initial guess. 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 Newton Raphson 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 Newton Raphson method
public class NewtonRaphsonRootFinder
{
    // The maximum of iterations of the
    // Newton Raphson method to use
    private int maxIterations;

    // Accuracy of returned roots
    private double tolerance;

    // Create new object, setting values for
    // iterations and tolerance
    public NewtonRaphsonRootFinder(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 fx, Function derivative, double initialGuess)
    {
	int iteration = 1;
	double xm = initialGuess;

	while (iteration <= maxIterations)
	{
	    // Perform the iteration
	    xm = xm - fx.eval(xm) / derivative.eval(xm);

	    // If within specified tolerance, return the root.
	    if (Math.abs(fx.eval(xm)) < tolerance)
	    {
		return xm;
	    }

	    iteration++;
	}

	// 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)
    {
	NewtonRaphsonRootFinder finder = new NewtonRaphsonRootFinder(15, 1E-3);

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

	// Derivative function
	Function diffFunc = new Function()
	{
	    public double eval(double x)
	    {
		// 2x + 8
		return 2 * x + 8;
	    }
	};

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

	    finder.setMaxIterations(5);

	    root = finder.FindRoot(func, diffFunc, 10);
	    System.out.println(root);
	}
	catch (ArithmeticException e)
	{
	    System.out.println(e.getMessage());
	}

    }
}

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





Is This A Good Question/Topic? 0
  • +

Page 1 of 1