Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<math.h>
- #define E 0.0001
- #define f(x) x*x*x*x+2*x*x-16*x+5
- void main()
- {
- int i=0,n;
- float x1,x2,x3,f1,f2,f3;
- printf("enter number of iterations and initial values x1 and x2\n");
- scanf("%d%f%f",&n,&x1,&x2);
- printf("ITRATION\t x1\t x2\t x3\n");
- while(i<n)
- {
- f1=f(x1);
- f2=f(x2);
- x3=x2-((f2*(x2-x1))/(f2-f1));
- f3=f(x3);
- printf("\n%d\t %.4f\t %.4f\t %.4f ",i+1,x1,x2,x3);
- if(fabs((x2-x1)/x2)<=E)
- {
- printf("\nthe root is %f\n",x3);
- break;
- }
- else
- {
- x1=x2;
- f1=f2;
- x2=x3;
- f2=f3;
- }
- i++;
- }
- }
- OUTPUT:
- enter number of iterations and initial values x1 and x2
- 100
- 0
- 1
- ITRATION x1 x2 x3
- 1 0.0000 1.0000 0.3846
- 2 1.0000 0.3846 0.3128
- 3 0.3846 0.3128 0.3267
- 4 0.3128 0.3267 0.3265
- 5 0.3267 0.3265 0.3265
- 6 0.3265 0.3265 0.3265
- the root is 0.326539
- Press any key to continue
Advertisement
Add Comment
Please, Sign In to add comment