Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #define f(x) ((x * x * x) - (9*x) + 1)
- int main()
- {
- float x0,x1,x2,xm,c,E;
- int i = 0;
- printf("Enter the initial values\n");
- scanf("%f %f %f",&x0,&x1,&E);
- if (f(x0) * f(x1) > 0){
- printf("Can not find a root in the given interval");
- return 0;
- }
- do
- {
- x2 = (x0 * f(x1) - x1 * f(x0)) / (f(x1) - f(x0));
- c = f(x0) * f(x2);
- x0 = x1;
- x1 = x2;
- i++;
- if (c == 0) break;
- xm = (x0 * f(x1) - x1 * f(x0)) / (f(x1) - f(x0));
- } while (fabs(xm - x2) >= E);
- printf("Root of the given equation= %f\n", x2);
- printf("No. of iterations = %d\n", i);
- return 0;
- }
- /*
- Output
- Enter the initial values
- 2 3 0.001
- Root of the given equation= 2.942849
- No. of iterations = 3
- */
Add Comment
Please, Sign In to add comment