Sample C program that uses Newton Raphson Method in mathematics
/*Newton Raphson Method*/
#include
#include
float f1(x)
float x;
{
return(x*x*x-6*x+4);
}
float f2(x)
float x;
{
return(3*x*x-6);
}
main()
{
int i;
float v1, v2, x0, x1, w, z, e, k;
clrscr();
printf("\n\t Newton - Raphson Method");
printf("\n\t************************");
v1 =0.0; v2 =1.0;
do
{
w = f1(v1);
z = f1(v2);
v2 = v2+1.0;
}
while(((w>0.0) && (z<0.0)) || ((w<0.0) && (z==0.0)));
x0 = v1;
printf("\n\n\t The given function is: x^3 - 6x +4");
printf("\n\n\t The differential function: 3x^2 - 6");
printf("\n\n\t x0 value \t\t x1 value ");
do
{
x1= x0 - (f1(x0)/f2(x0));
printf("\n\n\t %f \t\t %f",x0,x1);
k = x0;
x0 = x1;
}
while(k != x0);
printf("\n\n\t\t root = %f\n", x1);
getch ();
}