double findaroot(double x1, double x2){ //finds the root between two values
double gap = Math.abs(x1 - x2); //find the initial interval
while(gap > INTERVAL) { //check for precision
gap = gap / 2; //halve the interval
double x3 = x1 + gap;
if (f(x3) == 0) { //check for symmetry
return x3;
} else if (Math.signum(f(x1)) == Math.signum(f(x3))){
x1 = x3; //redefine the interval
} else {
x2 = x3; //redefine the interval
}
findaroot(x1, x2); //call again
}
return (x1 + x2) / 2; //return the mean
}
I am trying to find a solution for f(x)=-21x^2+10x-1 in the intervals (-143, 0.222222). The guidelines state that I should implement a bisection method to solve this. Currently this method works fine for 8 of the 10 test cases that I must pass, but it gives a "Time-limit exceeded" error for the aforementioned values. It takes 15 seconds to approximate the root given a precision level of at least "0.000001" between the intervals.
I'm not sure how I can make this more efficient without changing the method. I have already implemented Horner's method to calculate the function because Math.pow(x1, x2)
was taking too long.