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,e,x2,y0,y1,y2;
- int i=0;
- printf("Enter the value of x0,x1,e\n");
- 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;
- }
- do{
- x2=(x0*y1-x1*y0)/(y1-y0);
- y2=f(x2);
- i++;
- if(y2*y0>0){
- x0 = x2;
- y0 = y2;
- }
- else{
- x1 = x2;
- y1 = y2;
- }
- printf("\nx%d = %f",i,x2);
- }while(fabs(y2)>e);
- printf("\nHence the root is %f",x2);
- printf("\nTotal number of interations are %d",i);
- return 0;
- }
- /*
- Output
- Enter the value of x0,x1,e
- 2 3
- 0.001
- x1 = 2.900000
- x2 = 2.941555
- x3 = 2.942783
- Hence the root is 2.942783
- Total number of interations are 3
- */
Add Comment
Please, Sign In to add comment