/* * MyMath.java * * Created: January 29, 2005 * By: Stan Sclaroff * Description: Math class with methods pow, n-th root * For CS111 Spring 05 Programming Assignment 2 * * Modified: * By: * Description of changes: * */ package program2; import java.math.*; public class MyMath { // Constants // Double floating point accuracy private static final double TOLERANCE = 1E-11; // debug mode. If on, then debugging messages are printed private boolean debug=false; // Maximum number of iterations in computing root private static final int MAX_ITERATIONS = 200; // Constructors /* Contructs a new instance of MyMath */ public MyMath() { // do nothing } // Mutators // Set debug mode off/on public void debug(boolean debugState){ debug = debugState; } // THE METHODS YOU WRITE // compute the value of x raised to the power of n public double power(double x, int n){ // You write this method // Use a "for" loop to compute x raised to the power of n // The line below invokes the standard Math power function. // It is only a place holder until your write your own // Once you have written the code for this method // You MUST comment this next line out return(Math.pow(x,n)); } // This method computes the n-th root of y // using the method of bisection up to the tolerance public double nRootBisection(double y, int n){ // You write this method... // Remove the next line once you write your method return(0); } // EXAMPLE METHOD: nRootNewton // This method computes the n-th root of y // using the Newton-Raphson method up to the tolerance // Returns -1 if y < 0, or n is not positive, or // failed to converge // Author: Stan Sclaroff // Date: January 29, 2005 public double nRootNewton(double y, int n){ int iter; double root = y/n; // initial guess is root = y/n double change; // change in estimate if(debug) System.out.println("Entering MyMath.nRootNewton"); // Check for error cases. If error, return -1 if(y < 0){ System.out.println("Error: y is negative. \nLeaving MyMath.nRootNewton"); return -1; } if(n < 1){ System.out.println("Error: n is not positive. \nLeaving MyMath.nRootNewton"); return -1; } if(debug) System.out.println("Initial estimate: " + root); // Loop until converged or exceed allowed number of iterations for(iter=0;iter < MAX_ITERATIONS; ++iter){ change = (power(root,n) - y)/(n*power(root,n-1)); root -= change; // If debug mode is "true" then print out info each iteration // This helps to show us how the method is converging if(debug) System.out.println("Iteration: " + iter + " Estimate: " + root); // If change in estimated root is within tolerance // interval of zero [-TOLERANCE,TOLERANCE] // then we have converged within desired tolerance if((change > -TOLERANCE) && (change < TOLERANCE)) { if(debug) System.out.println("Leaving MyMath.nRootNewton"); return root; } } // We have left the main loop // So maximum iterations met! return -1 if(debug){ System.out.println("Did not converge after " + MAX_ITERATIONS + " iterations."); System.out.println("Leaving MyMath.nRootNewton"); } return -1; } }