468,512 Members | 1,147 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

newtons method

hi everyone can u pls help me to explain newtons method to find suare of a number and pls post its program also
Oct 19 '06 #1
1 6358
hi everyone can u pls help me to explain newtons method to find suare of a number and pls post its program also
If you mean the square root of a number you simply apply the newton method to the function f(x)=x*x-a, where a is the number whose square root will be computed

The following implementation is basic but should do the trick

#include <stdlib.h>
#include <math.h>

#define f(x) (x*x-2)//The function
#define d(x) (2*x)//Its derivative
//The square root of the number in the above number is computed here

//This program computes an approximation to the root of the
//function defined above via Newton's Method

using namespace std;

int main(int argc, char *argv[])
{
int istep;//loop control
double dl = 1e-14;//lower bound
double a, b, x0,dx;//variables
double x,xnew;//variables

a = 1; b =10;//This defines the range on which the root is saught
dx = b-a;
x0 = (a+b)/2;//This defines an initial approximation to the root
istep = 0;

//Newton's Method Algorithm
while (fabs(dx) > dl)
{
dx = f(x0)/d(x0);
x0 -= dx;
istep++;
}

cout<<"The Square Root Algorithm"<<endl;
cout<<" "<<endl;
cout<<"Newton's method converged in "<<istep<<" steps"<<endl;
cout<<" "<<endl;
cout<<"The square root is "<<endl;
cout<<" "<<endl;
printf("%16.24lf\n",x0);//For outoputting more digits of precision
cout<<" "<<endl;

system("PAUSE");
return 0;
}
Oct 19 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Dave Rahardja | last post: by
4 posts views Thread by daniel.w.gelder | last post: by
7 posts views Thread by greenflame | last post: by
5 posts views Thread by Nick Flandry | last post: by
10 posts views Thread by Mihai Osian | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.