Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test {
- public static double func(double x)
- {
- return Math.pow(x,x)-2.5;
- }
- public static double findRoot(double low, double high)
- {
- double middle =(low+high)/2.0;
- double midVal=func(middle);
- if (Math.abs(midVal)<1e-12) return middle;
- double lowVal=func(low);
- double highVal=func(high);
- if (Math.signum(lowVal)==Math.signum(midVal))
- return findRoot(middle, high);
- else return findRoot(low, middle);
- }
- public static void main(String[] args) {
- System.out.println(findRoot(1.0, 2.0)); // 1.709275722503662
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment