=========================================================================
Program to find solutions to equations of the form f(x)=0 using
Newton-Raphson Method
LANGUAGE :: C
Compiler: GNU GCC Compiler
=========================================================================
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double f(double x){
double y;
y=(x*x*x)-(2*x)-5;
return y;
}
double df(double x){
double y;
y=3*(x*x)-2;
return y;
}
int main()
{
int i,n;
double x,x0,e;
printf("Enter the maximum number of iterations:\n");
scanf("%d",&n);
printf("\nEnter the trial solution:\n");
scanf("%lf",&x0);
printf("\nEnter the tolerance:\n");
scanf("%lf",&e);
printf("\nIterations:\n");
for(i=1;i<=n;i++){
x=x0-f(x0)/df(x0);
printf("Iteration:%2d x=%10.6lf f(x)=%10.6lf \n",i,x,f(x));
if(fabs(x-x0)<=e) break;
x0=x;
}
printf("\nTHE APPROXIMATE SOLUTION IS: %lf",x);
return 0;
}