Tuesday, October 21, 2014

Newton's raphson method

#include<iostream.h>
#include<math.h>
double f(double x)
{
    return(4*sin(x)-exp(x));
}
double f1(double x)
{
    return(4*cos(x)-exp(x));
}

void main()
{
    double x[100];
    int k;
    cout<<"\nType the Initial guess x0=";
    cin>>x[0];
    x[1]=x[0]-f(x[0])/f1(x[0]);
    k=0;
    cout<<"k\t x[k]\t\t f(x[k])\n";
    while(fabs(x[k+1]-x[k])>=0.00005)
    {
        k++;
        x[k+1]=x[k]-f(x[k])/f1(x[k]);
        cout<<k+1<<"\t"<<x[k+1]<<"\t"<<f(x[k+1])<<endl;
    }
    cout<<"\nThe final root is="<<x[k+1];
}

No comments:

Post a Comment