Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- float l,u;
- int j,i=0;
- float arr[20];
- float f(float x)
- {
- return (x*x*x-x-1);
- }
- float itr(float a, float b)
- {
- float x=(a+b)/2;
- i++;
- arr[i-1]=x;
- return x;
- }
- float limit()
- {
- if(f(l)*f(arr[i-1])>0)
- {
- l=arr[i-1];
- }
- else
- u=arr[i-1];
- }
- void main()
- {
- printf("Enter the lower value\n");
- scanf("%f",&l);
- printf("Enter the upper value\n");
- scanf("%f",&u);
- printf("Enter the Iteration\n");
- scanf("%d",&j);
- if(f(l)*f(u)>0)
- {
- printf("Here is no root");
- }
- else
- {
- while(i<=j)
- {
- printf("Iteration %d root: %0.3f\t",i,itr(l,u));
- if(i>=2)
- {
- int err=(fabs(arr[i-1]-arr[i-2])/arr[i-1])*100;
- printf("error %d\n",err);
- }
- else
- {
- printf("No Error\n");
- }
- limit();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement