468,512 Members | 1,147 Online

# newtons method 2
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 emaghero
85 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 5 posts views Thread by Chris | 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 18 posts views Thread by JohnR | last post: by 10 posts views Thread by Mihai Osian | last post: by 9 posts views Thread by Steve Richter | last post: by 3 posts views Thread by allendowney | last post: by reply views Thread by ravipankaj | last post: by reply views Thread by slotstar | last post: by reply views Thread by captainhaddock | last post: by 1 post views Thread by fmendoza | last post: by 1 post views Thread by sonnet | last post: by reply views Thread by jimatqsi | last post: by reply views Thread by ajay sahare | last post: by 1 post views Thread by hwsilver | last post: by reply views Thread by yuyenews | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.