Newton's Method

I have been trying to write a Newton's Method program for the square root of a number and have been failing miserably... can anyone help me i have this so far?


#include <iostream>
#include <cmath>
using namespace std;

int main()
{
double x1;
double y;
double x0;

cout << "Enter a value to be square rooted: " << endl;
cin >> y;
x0 = (y/4);
cout << x0 << endl;

do {
x0 = x1;
x1 = ((x0 + (y/x0))/2);

cout << "x1=" << x1 << endl;

} while ((x1 + 0.0001) <= x0);

return 0;
}
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
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
  double x1;
  double y;
  double x0;
  
  cout << "Enter a value to be square rooted: " << endl;
  cin >> y;
  x0 = log(y);
  cout << x0 << endl;
  
  do 
  {
    x1 = (x0 - (((x0 * x0) - y)/(2* x0)));
    x0 = x1;
    
    cout << "x1= " << x1 << endl;
    
  } while ((x1 * x1) > y);
  
  return 0;
}



http://en.wikipedia.org/wiki/Newton's_method#Square_root_of_a_number
I'm new to c++ and programming in general, but it seems to me you would want to switch the order of x0 = x1; and x1 = ((x0 + (y/x0))/2);.

If and when you try this you'll haveto change your while condition, because once you switch those x1+0.0001 will never be less than x0.

I'd suggest something like:
while ((y/x0) <= 0.0001);

Check the math on that, I dunno if the constant in there is appropriate, or if what I'm saying is of any value at all.

Good Luck.
Your while loop condition did not return the required result, however, using

while ((y/x1)+0.0001 < x0); produced a better approximation faster

Enter a value to be square rooted: 
612
6.41673
x1= 50.8962
x1= 31.4603
x1= 25.4567
x1= 24.7488
x1= 24.7386


Compared to:
Enter a value to be square rooted: 
612
6.41673
x1= 50.8962
x1= 31.4603
x1= 25.4567
x1= 24.7488
x1= 24.7386
x1= 24.7386
x1= 24.7386


Might just have to do with decimal places
Last edited on
Compare your code with this function. I think the problem is in the first approximation e.g. x0=(y/4).
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
const double ACCURACY=0.001;
double lower, upper, guess;

 if (number < 1)
 {
  lower = number;
  upper = 1;
 }
 else
 {
  lower = 1;
  upper = number;
 }

 while ((upper-lower) > ACCURACY)
 {
  guess = (lower + upper)/2;
  if(guess*guess > number)
   upper =guess;
  else
   lower = guess;
 }
 return (lower + upper)/2;

}
/*enter a number to take the sqrt of
612
24.7387 is the square root of 612

Process returned 0 (0x0)   execution time : 4.766 s
Press any key to continue.
*/
Last edited on
I think i figured the code out but any last minute tweaks to make it better would help.

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;
int main()
{
double x1;
double y;
double x0;

cout << "Enter a value to be square rooted: " << endl;
cin >> y;
x0 = y/4;
cout <<"x0 is equall to " << x0 << setprecision(10) << endl;

do
{
x1 = (x0 - (((x0 * x0) - y)/(2* x0)));
x0 = x1;
cout << "x0= " << x0 << setprecision(10) << endl;
cout << "x1= " << x1 << setprecision(10) << endl;
cout << endl;

} while ((x1 * x0) > y);

cout << "The square root of "<< y << " is " << x1 << "." << endl;

return 0;
}
while ((y/x1)+0.0001 < x0); produced a better approximation faster
Topic archived. No new replies allowed.