/* Example of the use of the class RealRoot demonstrating the use of the method, RealRoot.bisectNewtonRaphson in finding a root of the function, y = a.x^b - 2 */ import flanagan.roots.*; // Class to evaluate the function y = a.x^b-2 and its derivative, a.b.x^(b-1) class Funct implements RealRootDerivFunction{ private double a = 1.0D; private double b = 2.0D; public double[ ] function(double x){ double[ ] y = new double[2]; y[0] = a*Math.pow(x,b) - 2.0D; y[1] = a*b*Math.pow(x,b-1.0D); return y; } public void setA(double a){ this.a = a; } public void setB(double b){ this.b = b; } } // Class to demonstrate method, RealRoot.bisectNewtonRaphson. public class RealRootExample{ public static void main(String[] arg){ // Create instance of the class holding the function, y = ax^b - 2, and its derivative Funct func = new Funct(); // change value of constant a in the function func.setA(2.0D); // lower bound double lowerBound = 0.0D; // upper bound double upperBound = 2.0D; // required tolerance double tolerance = 1e-6; // Create instance of RealRoot RealRoot realR = new RealRoot(); // Set tolerance realR.setTolerance(tolerance); // call root searching method, bisectNewtonRaphson double root = realR.bisectNewtonRaphson(func, lowerBound, upperBound); // Print root System.out.println("root = "+root); } }