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