waltermilner

Untitled

Apr 11th, 2016
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. public class Test {
  2.  
  3.   public static double func(double x)
  4.   {
  5.     return Math.pow(x,x)-2.5;
  6.   }
  7.  
  8.   public static double findRoot(double low, double high)
  9.   {
  10.     double middle =(low+high)/2.0;
  11.     double midVal=func(middle);
  12.     if (Math.abs(midVal)<1e-12) return middle;
  13.     double lowVal=func(low);
  14.     double highVal=func(high);
  15.     if (Math.signum(lowVal)==Math.signum(midVal))
  16.       return findRoot(middle, high);
  17.     else return findRoot(low, middle);
  18.   }
  19.   public static void main(String[] args) {
  20.     System.out.println(findRoot(1.0, 2.0)); // 1.709275722503662
  21.  
  22.   }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment