Results 1 to 3 of 3
  1. #1
    hag1990 is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default 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.

    Thank you in advanced.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    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

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by hag1990 View Post
    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

Similar Threads

  1. Binary Search Help
    By Plissken in forum New To Java
    Replies: 2
    Last Post: 03-13-2010, 11:34 AM
  2. Binary search tree search method
    By chopo1980 in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 02:42 AM
  3. Binary Search Tree
    By anmadie in forum New To Java
    Replies: 5
    Last Post: 11-17-2009, 03:39 AM
  4. Binary Search Tree
    By michael_mke in forum New To Java
    Replies: 3
    Last Post: 12-04-2008, 03:03 AM
  5. binary search
    By tranceluv in forum New To Java
    Replies: 10
    Last Post: 01-14-2008, 08:13 PM

Posting Permissions

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