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

1. ## 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. 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.

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`
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```

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