Newton Raphson for finding square roots

I am trying to use the newton raphson method for finding square roots. Considering the function f(x)= c-x^2 if we solve for f(x) =0 then x = square root of c which is what we want to find

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

using namespace std;


float newtonroot ( float raph);


int main()


{
float alpha;
float sqrt;

cout << "Enter the number you wish to square root" << endl;
cin >> alpha;

sqrt= newtonroot (alpha); //Call the function

cout << "The square root is" << sqrt <<  endl;

return 0;

}





//Functions
float newtonroot (float raph) //Function definition
{

  float root2=50;

  for (int q=1; q<=1000;)
    {


      root2=(root2-(((root2*root2)-raph)/(2*root2)));
      q++;
        return root2;

    }
}
That isn't the Newton-Raphson formula for finding roots.

Step 1: take a guess.

        x
   x1 = ─    
        2


Step 2: improve the guess.

          1        x
   xn+1 = ──( xn + ─── )    
          2        xn


Step 3: stop when the difference between the previous guess and this guess is less than some small number (usually called "epsilon"). Pay attention to that second term there, the numerator is the original input to the function!

Hope this helps.
Topic archived. No new replies allowed.