Newton's Method

Mar 1, 2013 at 1:51am
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;
}
Mar 1, 2013 at 4:28am
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
Mar 1, 2013 at 4:36am
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.
Mar 1, 2013 at 3:39pm
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 Mar 1, 2013 at 3:41pm
Mar 2, 2013 at 3:30am
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 Mar 2, 2013 at 3:32am
Mar 4, 2013 at 12:44am
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;
}
Mar 4, 2013 at 4:56am
while ((y/x1)+0.0001 < x0); produced a better approximation faster
Topic archived. No new replies allowed.