Newton method to find roots

This is a discussion on Newton method to find roots within the C Programming forums, part of the General Programming Boards category; I'm having some problems with this program , it hangs during operation : Code: #include<stdio.h> #include<math.h> int main(void) { // ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    104

    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);
    	}
    Any advice please ?
    Last edited by ICool; 11-17-2007 at 08:43 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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!)

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    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

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    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.

    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!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    temp_root=(1 / 2.0 * ( temp_root + ( 2 / temp_root) ) );
    ssharish

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    Quote Originally Posted by ssharish2005 View Post
    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'.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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).

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    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);
    	}

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    scanf("&#37;f",&number);
    ssharish

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Also, this code doesn't need <math.h>.

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    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 ?

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    I dont understand that formula you posted , I need to find square root of a number not a root of a function

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by ICool View Post
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-07-2008, 11:30 PM
  2. C# method
    By siten0308 in forum C# Programming
    Replies: 6
    Last Post: 07-15-2008, 07:01 AM
  3. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 07:51 AM
  4. Delegate Calling a method that Calls a delegate.
    By xddxogm3 in forum C# Programming
    Replies: 2
    Last Post: 05-05-2008, 12:59 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 03:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21