# Newton method to find roots

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-17-2007
ICool
Program hangs
I'm having some problems with this program , it hangs during operation :
Code:

```#include<stdio.h> #include<math.h> int main(void)         {                         // Calculates the square root of number using the newton's method                 float temp_root;                 int number;                 printf("Please Enter the number whose sqrt is to be found\n");                 scanf("&#37;d",&number);                                         if (number<0)                         {                                printf("invalid number entered");                         return 0;                         }                 temp_root=(number+1);        // To ensure the next statement is true initially                 while (((temp_root*temp_root)>number)||((((temp_root)*(temp_root))-number)<0.001))                         {                         temp_root=(1/2*(number+(2/number)));                         }                 printf("The square root of the number is %f",temp_root);         }```
• 11-17-2007
when your program hangs or is acting weird, i would recommend to use printf statements to see whats being executed, what isnt, what the values of your variables are, etc.

if you print temp_root in your while loop you will see its value is always 0. thats because your not modifying temp_root after each iteration, so it wont change, so it will loop continuously. also, rather than doing many arithmetic operations in one statement, create other temp. variables then compose them together. check the value of each one separately to ensure your calculations are correct.

as i dont know the algorithm for this, i cant help. (have to study for my own data structures exam!)
• 11-17-2007
ICool
Thanks for that nadroj , I tried changing it to :
Code:

`temp_root=(1/2*(temp_root+(2/temp_root)));`
, ( within while loop ) , but this makes my program crash . Is this undefined behaviour or am I doing something else wrong ? Thanks again
• 11-17-2007
again you should print out what each variable is--maybe your trying to divide by zero? do not use one statement to do all of this. use at least 2 other statements on 2 other variables, and print each one to make sure everything is what it should be.
• 11-17-2007
iMalc
Quote:

Code:

`temp_root=(1/2*(number+(2/number)));`

This line will give the same answer every time because 'number' doesn't change. The forumla you're using looks almost right for something that would be hard-coded to calculate the square root of 2 ONLY. Take another look at the formula and try again.

Also, 1/2 equals 0. When doing integer division the result is an integer and it always rounds down. You could use 1.0/2.0 for example, or you could simply use 0.5.

Quote:

Code:

`temp_root=(number+1);        // To ensure the next statement is true initially`

Don't use a while loop when you have to do something special to make it even enter the loop. Use a do..while instead!
• 11-17-2007
ssharish2005
Code:

`temp_root=(1 / 2.0 * ( temp_root + ( 2 / temp_root) ) );`
ssharish
• 11-17-2007
iMalc
Quote:

Originally Posted by ssharish2005
Code:

`temp_root=(1 / 2.0 * ( temp_root + ( 2 / temp_root) ) );`
ssharish

Well that fixes one problem, but it's still hard-coded to find the sqrt of 2 instead of 'number'.
• 11-17-2007
robatino
Since the technique works perfectly well for computing the square root of any positive number, not just an integer, number should be a floating-point type, not an int (and temp_root should be the same type).
• 11-17-2007
ICool
Ok thanks guys , I changed it to a do while loop , and I aded a variable x to do 10 repetitions , to achieve accuracy . However it still crashes.
Code:

```#include<stdio.h> #include<math.h> int main(void)         {                        float temp_root;                 float number;                 int x=0;                 printf("Please Enter the number whose sqrt is to be found\n");                 scanf("&#37;f",number);                                         if (number<0)                         {                                printf("invalid number entered");                         return 0;                         }                         temp_root=number;                                        do                         {                         temp_root=(1 / 2.0 * ( temp_root + ( 2 / temp_root) ) );                         x=x+1;                         }                         while(x<10);                 printf("The square root of the number is %f",temp_root);         }```
• 11-17-2007
ssharish2005
Code:

`scanf("&#37;f",&number);`
ssharish
• 11-17-2007
robatino
Also, this code doesn't need <math.h>.
• 11-17-2007
ICool
Thanks for that ssharish2005 now when I changed it it works BUT

0.082106 , I always get that as the result . No matter which number I enter . Any other ideas ?
• 11-17-2007
robatino
Your iteration formula has a sign error. The general formula for Newton's method is

x_{n+1} = x_n - f(x_n)/f'(x_n)

and if you plug in the appropriate function f, you'll find it.

http://en.wikipedia.org/wiki/Newton's_method
• 11-17-2007
ICool
I dont understand that formula you posted , I need to find square root of a number not a root of a function
• 11-17-2007
robatino
Quote:

Originally Posted by ICool
I dont understand that formula you posted , I need to find square root of a number not a root of a function

You need to understand how Newton's method is being used to derive the (faulty) iteration formula that you're using in your code. Once you do that, you'll be able to find the sign error you made. First step: decide what function f should be set to to compute the square root of a number by this method.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last