Results 1 to 7 of 7

Thread: close to zero

  1. #1
    nokicky is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default close to zero

    Hey guys.
    I've written a little program that finds the root of an equation using either the bisection or newtons method and it works great.
    At the moment it is set up to do 25 iterations of each equation by which time an answer if often found to 6 decimal places.
    I would like to stop the iterations if an answer if found before that.

    So the question is, how do I compare a double to zero but to only 6 decimal places?

    So that 0.0000001 == 0.000000 is true

    Thanks

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    with doubles and floats you should always use "<" and ">" and never use direct "=="

    But if you insist, i would take a look at NumberFormat.
    My Hobby Project: LegacyClone

  3. #3
    nokicky is offline Member
    Join Date
    Oct 2009
    Posts
    2
    Rep Power
    0

    Default

    Thanks for the advise. Went for this option:

    Java Code:
     if((f.apply(x) < 0.0000001) && (f.apply(x) > -0.0000001)) {
                    flag = false;
                }

  4. #4
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    Pretty standard to stop an iterative computation when a certain precision is met. May be interesting to examine a series of iterations to see the convergence, but the algorithm is usually stopped at some level of accuracy (which may be some small multiple) of machine accuracy).

    Simply compare that iteration(n) - itneration(n-1) < required accuracy.
    abs(thisX - lastX) <= somePrecision

    be careful with this statement. It might allow extra iterations if each iteration alternates between (0 + epsilon) and (0 - epsilon). But not fatal, just doubles the accuracy.

    A (very) quick look at NumberFormat suggests that it formats numbers to strings. Absolutely irrelevant to a numerical computation.

    Edit: OP reposted while I was replying. He found a correct solution.
    Edit2: It bothered me that the OP was comparing error range of the function evaluation to a precision value. While this may be what you want, the error in the root will be a polynomial mirroring in some way the original polynomial.

    Most likely, you will have more accuracy than you are really looking for at the expense of extra iterations. You also won't really know what the accuracy of the final answer is.
    Last edited by rdtindsm; 10-23-2009 at 03:15 PM.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,450
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by nokicky View Post
    Thanks for the advise. Went for this option:

    Java Code:
     if((f.apply(x) < 0.0000001) && (f.apply(x) > -0.0000001)) {
                    flag = false;
                }
    Use the Math.abs( ... ) method to avoid double evaluatation of f.apply( ... )

    Java Code:
    if (Math.abs(f.apply(x)) < 0.0000001) ...
    kind regards,

    Jos

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Would you mind sharing your code showing how you calculated the derivative? I attempted to write this program last year but other things came up and I never finished it.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,450
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by xcallmejudasx View Post
    Would you mind sharing your code showing how you calculated the derivative? I attempted to write this program last year but other things came up and I never finished it.
    This will do fine for very small values of h:

    f'(x)== (f(x+h)-f(x-h))/2h

    kind regards,

    Jos

Similar Threads

  1. Close but help would be grateful
    By reader34 in forum New To Java
    Replies: 8
    Last Post: 12-21-2008, 05:42 PM
  2. [SOLVED] Close Exe
    By smartsubroto in forum New To Java
    Replies: 11
    Last Post: 10-14-2008, 08:04 AM
  3. Close Active Window (?)
    By dcnorman07 in forum New To Java
    Replies: 2
    Last Post: 10-03-2008, 05:55 PM
  4. close a frame..
    By tajinvillage in forum New To Java
    Replies: 5
    Last Post: 04-27-2008, 10:22 PM
  5. How to close a JFrame
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:33 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
  •