Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By ozzyman

Thread: Creating recursion method to use Newton's method for square roots

  1. #1
    bdl1127 is offline Member
    Join Date
    Jan 2012
    Posts
    26
    Rep Power
    0

    Default Creating recursion method to use Newton's method for square roots

    Im really just not quite understanding recursion. I have created an iterative method to do this and it is working successfully. My iterative method is as follows:

    public static double squareRootIter (double num, double guess) {

    double x = guess;
    double epsilon = 1E-9;
    while(Math.abs(num - (x*x)) > epsilon) {
    double b = x - (((x*x) - num)/ (2*x));
    x = b;
    }
    return x;
    }

    I just need to turn this into a recursive method. I dont really even know where to start. Below is the method that must be used. Can someone please explain recursion to me or give me a starting point?

    public static double squareRootRec (double num, double guess) {

    if(num == 1) return 1;


    }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Creating recursion method to use Newton's method for square roots

    Instead of using a while loop, you need to allow the method to call itself to repeat the part of code you are repeating in your loop. Get your logic right, or you could get a StackOverflow error - The recursion method has to know when to stop. That's not so hard though, you just need to give it a condition whereby it can decide if it needs to recurse again or stop.

    you need something like this:

    Java Code:
    public static double squareRoot(double num, double guess) {
        boolean continue = ... // the condition to continue
        if (continue) {
            ... // make one step
            squareRoot(value1, value2); // recurse
        }
        // otherwise let it end
        return resultValue;
    }
    It doesn't have to look exactly like that, but the idea is that you have a method which calls itself instead of using a while loop.
    sunde887 likes this.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Creating recursion method to use Newton's method for square roots

    I'd like to add two things. Generally when doing recursion, you should not have loops (this is not to say you can't, but excessive loops should lead you to consider a different approach).

    Also, check this out: Example: Square Roots by Newton's Method

    It uses lisp, which may be confusing but the idea here should be quite clear.

Similar Threads

  1. Bisection Method to find Square and Cube Roots
    By tecnicamente in forum New To Java
    Replies: 2
    Last Post: 05-07-2011, 02:03 AM
  2. Solution for Newton-Raphson method
    By kienph2004 in forum Advanced Java
    Replies: 1
    Last Post: 10-29-2009, 07:49 AM
  3. Display Square Roots
    By hypes057 in forum New To Java
    Replies: 8
    Last Post: 08-25-2009, 11:34 AM
  4. Creating a New Method for Square Root Loop
    By SapphireSpark in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 02:21 PM
  5. Pls Help me for Newton-Raphson method by Java
    By kienph2004 in forum Advanced Java
    Replies: 3
    Last Post: 08-13-2008, 06:07 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
  •