Newton Raphson Method in C Programming
See & Learn 😏
#include <stdio.h>
#include<math.h>
#define ans 0.001
#define f(x) pow(x,3)-4*x+1
#define df(x) 3*x*x-4
int main()
{ float x0,x1;
float f0,f1,df0;
int i = 0;
printf("Enter the value of x(0): ");
scanf("%f",&x0);
printf("Enter the function value f0: ");
scanf("%d",&f0);
do
{
f0 = f(x0);
df0 = df(x0);
x1 = x0-(f0/df0);
f1 = f(x1);
x0 = x1;
i++;
printf("No of Iteration %d ",i);
printf("Root %.4f",x1);
printf("\n");
printf("Value of function %.4f\n",f1);
printf("\n");
} while (fabs(f1)>ans);
return 0;
}
Comments
Post a Comment