Advertisement
kdaud

Bisection Method

Mar 8th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. package NumericalComputing.BisectionMethod;
  2.  
  3. import java.util.Scanner;
  4.  
  5. @interface DaudKakumirizi
  6. {
  7.     String name() default " ";
  8.     String ID() default " ";
  9. }
  10. @DaudKakumirizi(name = "Kakumirizi Daud", ID = "17/BSS/BU/R/0010")
  11.  
  12. @SuppressWarnings("ALL")
  13. public class Bisection {
  14.     public static void main(String[] args) {
  15.         double a, b, c; /** a, b and c have the usual meaning */
  16.         double f_of_a, f_of_b; /** f_of_a, f_of_b store values of f(a) and f(b)
  17.         // respectively */
  18.         int highest_degree;
  19.         System.out.println("What is the highest degree of your polynomial? ");
  20.         Scanner input = new Scanner(System.in);
  21.         highest_degree = input.nextInt();
  22.         for (int i = highest_degree; i >= 0; i--) {
  23.             int coeff_deg_i;
  24.             coeff_deg_i = poly_input(i);
  25.             /**System.out.println(coeff_deg_i); */
  26.         }
  27.         /** The following do-while loop keeps asking the user for a and b until
  28.         // f(a)f(b) does not become negative */
  29.         do {
  30.             a = input();
  31.             b = input();
  32.             if (f(a) * f(b) >= 0) {
  33.                 System.out
  34.                         .println("Sorry the two numbers are not bracketing the root.  Please try again ");
  35.             }
  36.         } while (f(a) * f(b) >= 0);
  37.         f_of_a = f(a);
  38.         f_of_b = f(b);
  39.         double root = bisectionMethod(f_of_a, f_of_b, a, b);
  40.         System.out.println("Root is : " + root);
  41.     }
  42.  
  43.     public static double input() { /** Reads in the bracketing number i.e a and b */
  44.         Scanner input = new Scanner(System.in);
  45.         System.out.println("Enter a a bracket number  ");
  46.         return (input.nextDouble());
  47.     }
  48.  
  49.     public static double f(double num) {/** Calculates f(x) given x and returns
  50.         // f(x) */
  51.         final int COEFF_DEG_3 = 1; /** Coefficient of x^3 */
  52.         final int COEFF_DEG_2 = 4; /** Coefficient of x^2 */
  53.         final int COEFF_DEG_0 = -10; /** Coefficient of x^0 */
  54.         return (COEFF_DEG_3 * Math.pow(num, 3) + COEFF_DEG_2 * Math.pow(num, 2) + COEFF_DEG_0
  55.                 * Math.pow(num, 0));
  56.     }
  57.  
  58.     public static double bisectionMethod(double f_of_a, double f_of_b, double a,
  59.             double b) { /** Does the actual work of evaluating */
  60.         double c; /**the root using the method of bisection. */
  61.         double f_of_c;
  62.         final double TOLERANCE = 0.0001;
  63.         while (Math.abs(a - b) > TOLERANCE) {
  64.             c = (a + b) / 2;
  65.             f_of_c = f(c);
  66.             if (f_of_c * f(a) == 0 || f_of_c * f(b) == 0) {
  67.                 return c;
  68.             } else if (f_of_c * f(a) > 0) {
  69.                 a = c;
  70.             } else {
  71.                 b = c;
  72.             }
  73.         }
  74.         return (a + b) / 2;
  75.     }
  76.  
  77.     public static int poly_input(int degree) {
  78.  
  79.         System.out.println("Please enter coefficient for degree " + degree);
  80.  
  81.         Scanner input = new Scanner(System.in);
  82.         int coefficient;
  83.         coefficient = input.nextInt();
  84.         return coefficient;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement