# Thread: Binary Search for Finding Roots

1. Member Join Date
Oct 2010
Posts
1
Rep Power
0

## Binary Search for Finding Roots

Hi there,

I'm new to this forum and java programming. I've been trying to write a code that can find the roots of a function via Binary Search.

The idea is to pick two numbers that I know surround the root, because the function is positive in one end and negative in the other. Then I repeatedly divide the area in half and move either the lower or upper bound appropriately, until the difference becomes smaller than Epsilon (a constant I define).
The function I'm experimenting with is: f(x) = x^3 - 14*x^2 + 59*x - 65.

I've come down to the following code, however now I'm stuck and I don't get the correct result. I've calculated it manually and the root is ~1.72

The Code:

public class BSearchRoot {

static double Eplison = 1;

public static void main( String args[] ) {
double f, a, b, c, d, high, low, x;;
int flag;

x = 10; // Calculate the function with x = 10
a = x*x*x;
b = 14*(x*x);
c = 59*x;
d = 65;
f = a - b + c - d;

while (f == 0) { // Check if the root is 10
System.out.println("Root is: "+x);
return;
}

while (f < 0) { // Find the upper bound by adding 10 to x until f becomes higher than 0
x = x + 10;
a = x*x*x;
b = 14*(x*x);
c = 59*x;
d = 65;
f = a - b + c - d;
}
high = x; // Set the upper bound
System.out.println("Original high is: "+high); //Print the upper bound

while (f > 0) { // Find the lower bound by subtracting 10 from x until f becomes lower than 0
x = x - 10;
a = x*x*x;
b = 14*(x*x);
c = 59*x;
d = 65;
f = a - b + c - d;
}
low = x; // Set the lower bound
System.out.println("Original low is: "+low);

flag = -1; // Control whether or not to use the upper or lower bound

while ((high-low) > Eplison) { // Do while the difference is > Eplison
if (flag == -1) { // Calculate the lower bound
x = low;
a = x*x*x;
b = 14*(x*x);
c = 59*x;
d = 65;
f = a - b + c - d;
low = x/2.0; // Divide the area in half
flag = -flag;
}
else {
x = high; // Upper bound
a = x*x*x;
b = 14*(x*x);
c = 59*x;
d = 65;
f = a - b + c - d;
high = x/2; // Divide the area in half
flag = -flag;
}
}
// Print the results
System.out.println("high is: "+high);
System.out.println("low is: "+low);
System.out.println("x is: "+x);
System.out.println("f is: "+f);

}
}

When I run the code I get the output:

Original high is: 10.0
Original low is: 0.0
high is: 0.625
low is: 0.0
x is: 1.25
f is: -11.171875

I want to get the output where f is as low as possible.

Any help/criticism is greatly appreciated.  Reply With Quote

2. ## Try debugging your code by printing out all of the intermediate variable values to see where the calculations are going wrong.

Please put your poste code in code tags to make for easier reading. Info here: Java Forums - BB Code List  Reply With Quote

3. ##  Originally Posted by hag1990 The function I'm experimenting with is: f(x) = x^3 - 14*x^2 + 59*x - 65.
x = 10; // Calculate the function with x = 10
a = x*x*x;
b = 14*(x*x);
c = 59*x;
d = 65;
f = a - b + c - d;
That implementation is ever so repetative and yucky; why don't you define a little method for that function?

Java Code:
```private static double function(double x) {
return x*x*x-14*x*x+59*x-65;
}```
... so you can get rid of all that inline code in your main( ... ) method ... b.t.w. read the API documentation for the Math.signum( ... ) method as well.

kind regards,

Jos  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•