Advertisement
yloplopy

Bisection with Rate of Convergence

Apr 27th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. private static void bisectionMethod(double a, double b) {
  2.         double tol = 0.0000001;
  3.        
  4.         List<Double> pastValues = new ArrayList<Double>();
  5.        
  6.         double fa = funct(a);
  7.         int counter = 1;
  8.         while(Math.abs(b-a) >= (tol)) {
  9.            
  10.             double m = (a+b)/2;
  11.             pastValues.add(m);
  12.             double fm = funct(m);
  13.             if((fa*fm)<=0) {
  14.                 b=m;
  15.             }else {
  16.                 a=m;
  17.             }
  18.             if(pastValues.size() < 4) {
  19.                 System.out.println(counter + ": Interval = [" + a + ", " + b + "] x = " + m);
  20.             }else {
  21.                 double y0 = Math.log(Math.abs(pastValues.get(pastValues.size()-1) - pastValues.get(pastValues.size()-2)));
  22.                 double x0 = Math.log(Math.abs(pastValues.get(pastValues.size()-2) - pastValues.get(pastValues.size()-3)));
  23.                 double y1 = x0;
  24.                 double x1 = Math.log(Math.abs(pastValues.get(pastValues.size()-3) - pastValues.get(pastValues.size()-4)));
  25.                 double roc = (y1-y0)/(x1-x0);
  26.                 System.out.println(counter + ": Interval = [" + a + ", " + b + "] x = " + m);
  27.                 System.out.println("Rate of Convergence = " + roc);
  28.             }
  29.             counter++;
  30.         }
  31.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement