newtons method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • radhika88
    New Member
    • Oct 2006
    • 2

    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
  • emaghero
    New Member
    • Oct 2006
    • 85

    #2
    Originally posted by radhika88
    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"<<end l;
    cout<<" "<<endl;
    cout<<"Newton's method converged in "<<istep<<" steps"<<endl;
    cout<<" "<<endl;
    cout<<"The square root is "<<endl;
    cout<<" "<<endl;
    printf("%16.24l f\n",x0);//For outoputting more digits of precision
    cout<<" "<<endl;

    system("PAUSE") ;
    return 0;
    }

    Comment

    Working...