Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<math.h>
- #define E 0.0001
- float f(float x)
- {
- return (x*x+x-2);
- }
- float df(float x)
- {
- return (2*x+1);
- }
- main()
- {
- float x0,x1,f0,df0;
- int n,i=0;
- printf("enter the value of x and max itrations");
- scanf("%f %d",&x0,&n);
- while(i<n)
- {
- f0=f(x0);
- df0=df(x0);
- x1= x0- f0/df0;
- printf("\n itration %d \t x0= %f",(i+1),x0);
- if(fabs((x1-x0)/x1) < E)
- {
- printf("\n the root is %f in %d iterations",x0,(i+1));
- exit(0);
- }
- x0=x1;
- i++;
- }
- printf("the roots do not converge in %d itrations",n);
- return 0;
- }
- OUTPUT:
- enter the value of x and max itrations-10
- 50
- itration 1 x0= -10.000000
- itration 2 x0= -5.368421
- itration 3 x0= -3.165292
- itration 4 x0= -2.254739
- itration 5 x0= -2.018490
- itration 6 x0= -2.000113
- the root is -2.000113 in 6 iterations
Advertisement
Add Comment
Please, Sign In to add comment