Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // one more way to get the answer written in this code by comment where changes has to be made
- #include <stdio.h>
- #include <math.h>
- #define f(x) ((x*x*x) - (2*x) - 5)
- int main()
- {
- float x0,x1,e,x2,y0,y1,y2;
- int i=0;
- printf("Enter the value of x0,x1,e");
- scanf("%f %f %f",&x0,&x1,&e);
- y0 = f(x0);
- y1 = f(x1);
- if(y1*y0>0){
- printf("\nIntial values are not correct");
- return 0;
- }
- while(fabs((x1-x0)/x1)>e){ // only do while
- x2=(x0+x1)/2;
- y2=f(x2);
- i++;
- if(y2*y0>0){
- x0 = x2;
- y0 = y2;
- }
- else{
- x1 = x2;
- y1 = y2;
- }
- } // condition (fabs(y2)>e);
- printf("\nHence the root is %f",x2);
- printf("\nTotal number of interations are %d",i);
- return 0;
- }
- /*
- Ouput
- Enter the value of x0,x1,e
- -1 3 0.0001
- Hence the root is 2.094604
- Total number of iterations are 15
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement