Results 1 to 3 of 3
Thread: Binary Search for Finding Roots
 10012010, 03:09 PM #1Member
 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 ((highlow) > 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.
Thank you in advanced.
 10012010, 03:41 PM #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
 10012010, 04:10 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 29
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*x14*x*x+59*x65; }
kind regards,
Jos
Similar Threads

Binary Search Help
By Plissken in forum New To JavaReplies: 2Last Post: 03132010, 11:34 AM 
Binary search tree search method
By chopo1980 in forum New To JavaReplies: 2Last Post: 12102009, 02:42 AM 
Binary Search Tree
By anmadie in forum New To JavaReplies: 5Last Post: 11172009, 03:39 AM 
Binary Search Tree
By michael_mke in forum New To JavaReplies: 3Last Post: 12042008, 03:03 AM 
binary search
By tranceluv in forum New To JavaReplies: 10Last Post: 01142008, 08:13 PM
Bookmarks