Example #1
0
  public static void askUser() {
    Scanner input = new Scanner(System.in);

    System.out.println("Enter a value>");
    double value = input.nextDouble();
    System.out.println("The value is " + myPoly.evaluate(value));
    boolean cont = true;

    while (cont) {
      System.out.println("Continue?>");
      String valueYesNo = input.next();
      if (valueYesNo.equalsIgnoreCase("Yes") || valueYesNo.equals("y")) {
        input = new Scanner(System.in);
        System.out.println("Enter a value>");
        value = input.nextDouble();
        System.out.println("The value is " + myPoly.evaluate(value));
      } else {
        cont = false;
      }
    }
  }
Example #2
0
  // TODO try using a linear search alg here
  public static double refineRoot(Polynomial poly, double root, int maxIterations) {

    //		for( int i = 0; i < maxIterations; i++ ) {
    //
    //			double v = poly.c[poly.size-1];
    //			double d = v*(poly.size-1);
    //
    //			for( int j = poly.size-1; j > 0; j-- ) {
    //				v = poly.c[j] + v*root;
    //				d = poly.c[j]*j + d*root;
    //			}
    //			v = poly.c[0] + v*root;
    //
    //			if( d == 0 )
    //				return root;
    //
    //			root -= v/d;
    //		}
    //
    //		return root;

    Polynomial deriv = new Polynomial(poly.size());
    derivative(poly, deriv);

    for (int i = 0; i < maxIterations; i++) {

      double v = poly.evaluate(root);
      double d = deriv.evaluate(root);

      if (d == 0) return root;

      root -= v / d;
    }

    return root;
  }