Thread: How to define epsilon in newton raphson square root program?

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    8

    How to define epsilon in newton raphson square root program?

    Hi there, I'm working on a c program that generates approximations of square roots based on newton raphson method. I worked out the approximations with another method I learnt and I know this program does fine. But I don't get the concept of epsilon. Below is the code for the program
    Code:
    #include<stdio.h>
    
    // function to compute absolute value
    
    float absoluteValue(float x)
    {
      if(x < 0)
      x = -x;
      
      return (x);
    }
    
    // function to compute the square root of a number
    
    float squareRoot (float x)
    {
      const float epsilon = .00001; //why is this value chosen?
      float       guess   = 1.0;
      
      while(absoluteValue(guess * guess - x) >= epsilon)
            guess = (x/guess + guess) / 2.0;
            
            return guess;
    }
    
    int main (void)
    {
       printf("squareRoot(2.0) = %f\n", squareRoot(2.0));
       printf("squareRoot(144.0) = %f\n", squareRoot(144.0));
       printf("squareRoot(17.5) = %f\n", squareRoot(17.5));
      return 0;
    }
    The text I worked this code from uses the epsilon value arbitrarily. So can someone help me understand how is the epsilon value selected? I tried using this formula to derive the square root of 45. On paper I can go as far as 6.8 something. But once this value is reached it's still bigger than epsilon, so shouldn't the code keep running? The program generates 1.41 for 2, but since it could go further why does the while loop in squareRoot function terminate?
    Thanks in advance and sorry for being verbose too.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,302
    Quote Originally Posted by avidrutham
    The text I worked this code from uses the epsilon value arbitrarily. So can someone help me understand how is the epsilon value selected?
    It probably really is an arbitrarily chosen small value, i.e., chosen such that the difference between the square of the guess and the given value is sufficiently small, where "sufficiently small" depends on what one desires.

    Quote Originally Posted by avidrutham
    I tried using this formula to derive the square root of 45. On paper I can go as far as 6.8 something. But once this value is reached it's still bigger than epsilon, so shouldn't the code keep running?
    Yes, it should, and plugging in 45.0 into your program I get:
    Code:
    squareRoot(45.0) = 6.708204
    Quote Originally Posted by avidrutham
    The program generates 1.41 for 2, but since it could go further why does the while loop in squareRoot function terminate?
    I compiled your program using gcc 4.8.2 on Ubuntu 14.04, ran it and received this output:
    Code:
    squareRoot(2.0) = 1.414216
    squareRoot(144.0) = 12.000000
    squareRoot(17.5) = 4.183300
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    laserlight is correct as usual. The epsilon determines when you want your program to stop and the accuracy of your solution. Your solution is accurate down to 10^5 in the x^2 space, but probably only 10^2 or 10^3 in the square root space. Newton Raphson is quadratically convergent when it actually converges so it's not that expensive running it to machine epsilon. Each iteration will approximately double the number of accurate digits.

    However it only does so if your initial guess is close enough, otherwise it is possible for it to diverge.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding square root by Newton-Raphson program
    By Majorpain2587 in forum C Programming
    Replies: 11
    Last Post: 05-31-2013, 03:38 PM
  2. Newton raphson method - C
    By arazki1yes in forum C Programming
    Replies: 8
    Last Post: 04-04-2011, 01:11 PM
  3. Newton raphson
    By kariisa in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2011, 08:13 AM
  4. using newton raphson method
    By sanskaar in forum C Programming
    Replies: 21
    Last Post: 09-27-2010, 12:02 AM
  5. Newton Raphson Method
    By zeb1d1ah in forum C Programming
    Replies: 2
    Last Post: 12-07-2009, 06:26 AM

Tags for this Thread