Author Topic: Finding square root using Newton-Raphson Method  (Read 9786 times)

0 Members and 1 Guest are viewing this topic.

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Finding square root using Newton-Raphson Method
« on: March 10, 2013, 09:35:01 am »
Here's source code, just wrote it.
Code: (C) [Select]
#include <stdio.h>
// Function to get absolute value of the number given by user.
float absolute(float num)
{
if(num < 0){
 num = -num;
}
return num;
}
// Function to calculate square root of the number using Newton-Raphson method
float square_root(int x)
{
const float    difference = 0.00001;
float          guess = 1.0;
while(absolute(guess * guess - x) >= difference){
 guess = (x/guess + guess)/2.0;
}

return guess;
}

int main()
{
int     number;
float  root;
printf("Enter a number: ");
scanf("%i", &number);
root = square_root(number);
printf("The square root of %i is: %f", number, root);
return 0;
}
« Last Edit: March 17, 2013, 07:14:53 am by parad0x »

Offline pune3t

  • Serf
  • *
  • Posts: 20
  • Cookies: -6
  • Not an Engineer but a Socialist in my way
    • View Profile
Re: Finding square root using Newton-Raphson Method
« Reply #1 on: March 10, 2013, 10:11:02 am »
add some comments for better understanding.
:)
If you think you can do a thing or think you can't do a thing, you're right.

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: Finding square root using Newton-Raphson Method
« Reply #2 on: March 10, 2013, 10:22:08 am »
add some comments for better understanding.
:)
puneet, you know C??

Offline pune3t

  • Serf
  • *
  • Posts: 20
  • Cookies: -6
  • Not an Engineer but a Socialist in my way
    • View Profile
Re: Finding square root using Newton-Raphson Method
« Reply #3 on: March 10, 2013, 10:25:38 am »
yes, actually I'm learning it too.
:)
If you think you can do a thing or think you can't do a thing, you're right.

Offline DaNePaLI

  • Peasant
  • *
  • Posts: 55
  • Cookies: 12
  • Forever n00b
    • View Profile
Re: Finding square root using Newton-Raphson Method
« Reply #4 on: March 10, 2013, 02:24:25 pm »
Hi parad0x, you made a small flaw at line 15.

Replace
Code: [Select]
while(absolute(guess * guess - x*x) >= difference){with
Code: [Select]
while(absolute(guess * guess - x) >= difference){
Since x is the number whose square root you need to find, guess * guess has to be subtracted from x not from x * x.

Then your program will run fine.

Offline pune3t

  • Serf
  • *
  • Posts: 20
  • Cookies: -6
  • Not an Engineer but a Socialist in my way
    • View Profile
Re: Finding square root using Newton-Raphson Method
« Reply #5 on: March 10, 2013, 03:59:23 pm »
and please for god sake use #include<conio.h> its easy to understand with the help of this header file, & the main reason behind using this is it uses a [ getch() function ] which holds the screen, after your program has been executed.
e.g: see the uploaded file.
« Last Edit: March 10, 2013, 04:01:19 pm by pune3t »
If you think you can do a thing or think you can't do a thing, you're right.

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: Finding square root using Newton-Raphson Method
« Reply #6 on: March 10, 2013, 04:27:49 pm »
and please for god sake use #include<conio.h> its easy to understand with the help of this header file, & the main reason behind using this is it uses a [ getch() function ] which holds the screen, after your program has been executed.
e.g: see the uploaded file.
conio.h header file slows down the program and for holding the screen, I am sure you are on windows. To hold the screen, use getchar() which is present in stdio.h.

Offline p_2001

  • Royal Highness
  • ****
  • Posts: 684
  • Cookies: -64
    • View Profile
Re: Finding square root using Newton-Raphson Method
« Reply #7 on: March 10, 2013, 06:00:45 pm »
Speed up the code... The square root of a number is always less than half of the number.


the code is slow because of the number of decimals and the divisions done.
"Always have a plan"

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: Finding square root using Newton-Raphson Method
« Reply #8 on: March 10, 2013, 06:24:06 pm »
well to be blunt your code is a complete mess but that just fine since your new. allow me to tidy it up a bit


Code: (c) [Select]

#include <stdio.h>
 
float absolute(float num); //Prototype or function declaration.
float square_root(int x);  // definition is below main
 
int main()
{
    int number;
    float root;


    printf("Enter a number: ");
    scanf("%i", &number);


    root = square_root(number);


    printf("The square root of %i is: %f", number, root);
    return 0;   
}


float absolute(float num) // Function to get absolute value of the number given by user.
{
     if(num < 0)
       num = -num;
   
     return num;
}
 


float square_root(int x)  // Function to calculate square root of the number using Newton-Raphson method
{
     const float difference = 0.00001;
     float guess = 1.0;
     while(absolute((guess*guess) - x) >= difference) //DaNePaLI's addition
     {
         guess = ((x/guess) + guess)/2.0;
     }
 
     return guess;
}
« Last Edit: March 10, 2013, 06:28:45 pm by Super_mario666 »
The Bigger they are...The more likely you'll get your ass kicked

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
Re: Finding square root using Newton-Raphson Method
« Reply #9 on: March 10, 2013, 06:25:21 pm »
Speed up the code... The square root of a number is always less than half of the number.


the code is slow because of the number of decimals and the divisions done.

Less than or equal to half of the number: sqrt(4) = 2 = 4/2
except for 1: sqrt(1) = 1 = 1/1

It's important to not forget about these two cases too!
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
Re: Finding square root using Newton-Raphson Method
« Reply #10 on: March 17, 2013, 07:13:04 am »
well to be blunt your code is a complete mess but that just fine since your new. allow me to tidy it up a bit


Code: (c) [Select]

#include <stdio.h>
 
float absolute(float num); //Prototype or function declaration.
float square_root(int x);  // definition is below main
 
int main()
{
    int number;
    float root;


    printf("Enter a number: ");
    scanf("%i", &number);


    root = square_root(number);


    printf("The square root of %i is: %f", number, root);
    return 0;   
}


float absolute(float num) // Function to get absolute value of the number given by user.
{
     if(num < 0)
       num = -num;
   
     return num;
}
 


float square_root(int x)  // Function to calculate square root of the number using Newton-Raphson method
{
     const float difference = 0.00001;
     float guess = 1.0;
     while(absolute((guess*guess) - x) >= difference) //DaNePaLI's addition
     {
         guess = ((x/guess) + guess)/2.0;
     }
 
     return guess;
}
mario, what you are saying are standard C++ while c99 standards say that you should write the function before it is used in main().

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: Finding square root using Newton-Raphson Method
« Reply #11 on: March 19, 2013, 06:15:33 pm »
mario, what you are saying are standard C++ while c99 standards say that you should write the function before it is used in main().

you don't have to include a function prototype just so long as the function is declared before the function is called. i personally included it because i find it easier to read and understand. but the main thing i was trying to fix was was indentation. all of your code was hugging the left side of the page which is a total pain to go though.


« Last Edit: March 19, 2013, 06:52:27 pm by Super_mario666 »
The Bigger they are...The more likely you'll get your ass kicked

Offline Axon

  • VIP
  • King
  • *
  • Posts: 2047
  • Cookies: 319
    • View Profile
Re: Finding square root using Newton-Raphson Method
« Reply #12 on: March 22, 2013, 12:42:14 am »
This brings back bad memories when I took a course in numerical analysis.
« Last Edit: March 22, 2013, 12:51:19 am by Axon »

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: Finding square root using Newton-Raphson Method
« Reply #13 on: March 25, 2013, 06:49:33 pm »
@parad0x; prototypes are just forward-declarations of functions.  Indeed, the ANSI standard indicates their preferred use because all the information for function calls is then available before any function is actually run.  Using 'prototypes' avoids issues with inter-dependent functions and having to choose which function comes first and finally redesigning code entirely to avoid interdependence(sometimes it's not avoidable).  This is the best method and C99 has not changed that.  Use of prototypes is preferred...always.

@Deque; yes, I'm sorry.  I had like 15 minutes to do something of my own choice so I came here to check out what havoc I could wreak.  Not having time to actually respond to everything, I left placeholders to help me remember what was awaiting my comment(I do this somewhat often whilst working).  Unfortunately, work took my attention away for multiple days when I thought I could respond within a sane period of time.
« Last Edit: March 27, 2013, 04:49:46 pm by Xires »
-Xires

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Finding square root using Newton-Raphson Method
« Reply #14 on: March 25, 2013, 08:45:04 pm »
-- Placeholder --

Xires! I am opening all these new answers to threads just to see a placeholder everwhere. That's annoying.