how to solve newton raphson method in c++.


newton raphson method is a method to solve equations in numerical techniques.
#include<iostream.h>
#include<conio.h>
#include<math.h>
const double epsilon=0.00001;


float f(float x)
{
float fx1;

fx1=pow(x,4)+pow(x,2)-80;

return fx1;
}
float f_dash(float x)
{
float fx2;

fx2=4*pow(x,3)+2*x;
return fx2;
}
void main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
clrscr();
float x0,c;

cout<<"Enter initial approximation\n";
cout<<"x0=";
cin>>x0;
cout<<"\nx0\t\tf(x0)\t\tf'(x0)\t\tc\n";
cout<<endl;
c=x0-(f(x0)/f_dash(x0));

                while(fabs(x0-c)>=epsilon)
                {
                cout<<x0<<"\t"<<f(x0)<<"\t"<<f_dash(x0)<<"\t"<<c<<endl;
                x0=c;
                c=x0-(f(x0)/f_dash(x0));

                }


cout<<"\n\nThe final root is:";
cout<<c;
getch();
}

OUTPUT:

Enter initial approximation
x0=2.5

     x0                        f(x0)               f'(x0)                  c
2.500000        -34.687500      67.500000       3.013889
3.013889        11.593969       115.534729      2.913538
2.913538        0.546817        104.755745      2.908318
2.908318        0.001412        104.214531      2.908305


The final root is:  2.908305




Comments :

Post a Comment