/** * @(#)NewtonRaphsonTester.java * * NewtonRaphsonTester application * * @author Michael McClelland, Leo Gorokhovsky * @version 1.00 2007/5/16 */import javax.swing.JOptionPane;public class NewtonRaphsonTester { public static void main(String[] args) { System.out.println("This program will solve for zeros of equations of the from of: f(x)= X^n-a");//prompt display menu();//initiate menu to display }public static void test(){ int n=0;//init n to 0 double precision=0.00001,a=0;//precision set to 0 and a init. String userAnswer; try{ System.out.println("Please Enter values for the equation with respect to:\nf(x) = x^n - a"); //prompt display precision = Math.abs(Double.parseDouble(JOptionPane.showInputDialog("Value for precision? - Enter a decimal value")));//JOptionPane Dialogue n = Integer.parseInt(JOptionPane.showInputDialog("Value for n? - Enter an Integer greater than 0 - input will be treated as signless")); System.out.println("Value for a? - Enter an Integer greater than 0 - input will be treated as signless"); a = Integer.parseInt(JOptionPane.showInputDialog("Value for a? - Enter an Integer greater than 0 - input will be treated as signless")); System.out.println("Summery:\nprecision=" + precision + "\nn=" + n + "\na=" + a); userAnswer = JOptionPane.showInputDialog("Is this correct?\nEnter yes or y - all other inputs will restart the test"); if ((userAnswer.equalsIgnoreCase("yes")) || (userAnswer.equalsIgnoreCase("y"))) System.out.println("Starting approximation"); else { System.out.println("Summery not approved or bad input - restarting"); test(); } } catch (NumberFormatException e) { System.out.println("Bad input - restarting"); test(); } NRaphson g = new NRaphson(n,a,precision); do { g.nextApproximation(); System.out.println(g.getApprox()); } while (g.closeEnough()); } public static void autoTest() { int qq=0,correct=0,a=0,n=0; double u,nth,oracle,precision=0.00001; try{ precision = Math.abs(Double.parseDouble(JOptionPane.showInputDialog("Value for precision? - Enter a decimal value"))); int tests; tests = Math.abs(Integer.parseInt(JOptionPane.showInputDialog("Perform how many tests??"))); while (qq<tests) { n = 1 + (int)(Math.random() * 20); // random number casted to int between 1-20 a = 1 + (int)(Math.random() * 20); // random number casted to int between 1-20 NRaphson g = new NRaphson(n,a,precision); do { g.nextApproximation(); u=g.getApprox(); } while (g.closeEnough()); nth = Math.pow(n,-1); oracle = Math.pow(a,nth); Numeric tolerance = new Numeric(); if ((u==oracle) || tolerance.approxEqual(u,oracle,precision)) { correct++; // System.out.println("My method " + u + " Math.pow() oracle " + oracle + " n " + n + " a " + a + " howclose " + Math.abs(oracle - u)); } else { // System.out.println(tolerance.approxEqual(u,oracle,precision) + " XXXXXXXX ERROR My method " + u + " Math.pow() oracle " + oracle + " n " + n + " a " + a + " howclose " + Math.abs(oracle - u)); } qq++; } System.out.println("Out of " + qq + " operations the oracle and the Newton-Raphson method had identical or very close values " + correct + " times"); } catch (NumberFormatException e) { System.out.println("Bad input - restarting"); autoTest(); } }public static void menu() { int pick =1; int pk=6; try{ while (pk==6) { pick = Integer.parseInt(JOptionPane.showInputDialog("what would you like to do?\n1.Run a single test\n2.Run a user defined number of tests with random inputs against an oracle\n3.View Boundary Cases\n4.Quit")); if (pick == 1) test(); if (pick == 2) autoTest(); if (pick == 3) boundryAndNegative(); if (pick == 4) { System.out.println("Quitting"); System.exit(0); } } } catch (NumberFormatException e) { System.out.println("Bad input - restarting"); menu(); } }public static void boundryAndNegative(){ int a=0,n=0; double u,nth,oracle,precision=0.00001; NRaphson g = new NRaphson(n,a,precision);//---------------------------------------------------------- do { g.nextApproximation(); u=g.getApprox(); } while (g.closeEnough()); System.out.println("n=" + n + " a=" + a); System.out.println(g.getApprox());//---------------------------------------------------------- a=0;n=4; g = new NRaphson(n,a,precision); do { g.nextApproximation(); u=g.getApprox(); } while (g.closeEnough()); System.out.println("n=" + n + " a=" + a); System.out.println(g.getApprox());//---------------------------------------------------------- a=7;n=0; g = new NRaphson(n,a,precision); do { g.nextApproximation(); u=g.getApprox(); } while (g.closeEnough()); System.out.println("n=" + n + " a=" + a); System.out.println(g.getApprox());//---------------------------------------------------------- a=7;n=4; g = new NRaphson(n,a,precision); do { g.nextApproximation(); u=g.getApprox(); } while (g.closeEnough()); System.out.println("n=" + n + " a=" + a); System.out.println(g.getApprox());//---------------------------------------------------------- a=-7;n=-4; g = new NRaphson(n,a,precision); do { g.nextApproximation(); u=g.getApprox(); } while (g.closeEnough()); System.out.println("n=" + n + " a=" + a); System.out.println(g.getApprox());//---------------------------------------------------------- a=7;n=-4; g = new NRaphson(n,a,precision); do { g.nextApproximation(); u=g.getApprox(); } while (g.closeEnough()); System.out.println("n=" + n + " a=" + a); System.out.println(g.getApprox());//---------------------------------------------------------- a=-7;n=4; g = new NRaphson(n,a,precision); do { g.nextApproximation(); u=g.getApprox(); } while (g.closeEnough()); System.out.println("n=" + n + " a=" + a); System.out.println(g.getApprox());//---------------------------------------------------------- nth = Math.pow(n,-1); oracle = Math.pow(a,nth);}}