Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #define EPS 0.005
- double func(double x)
- {
- return x*x*x-2*x-5;
- }
- void bisection(double a, double b)
- {
- double e;
- if (func(a) * func(b) > 0)
- {
- e=func(a)*func(b);
- printf("%f\n",e);
- printf("You have not assumed right xn and xp as func(xn) * func(xp)>0 \n");
- return;
- }
- double c;
- printf("a\tb\tc\tfm\te\n");
- while ((b-a) >= EPS)
- {
- // Find middle point
- double e;
- double d;
- c = (a+b)/2;
- d=((b-a)/(a));
- e=func(c);
- printf("%.4f\t %.4f\t %.4f\t %.4f\t %.4f\t",a,b,c,e,d);
- // Check if middle point is root
- if (func(c) == 0.0)
- break;
- // Decide the side to repeat the steps
- else if (func(c) < 0)
- a = c;
- else
- b= c;
- printf("\n");
- }
- printf("\nThe value of root is : %.4f\n",c );
- }
- int main()
- {
- // Initial values assumed
- int x1,x2;
- scanf("%d %d",&x1,&x2);
- bisection(x1, x2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement