Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- float func(float x)
- {
- float z;
- z=x*x*x+x*x-1;
- return z;
- }
- main()
- {
- float a,b,sub,fx1,fx2,fxn,x1,x2,xn,E;
- int i;
- E=0.009;
- scanf("%f %f",&x1,&x2);
- fx1=func(x1);
- fx2=func(x2);
- if((fx1>0&&fx2>0)||(fx1<0&&fx2<0))
- {
- printf("no root");
- }
- else
- {
- if(fx1<0)
- {
- a=x1;
- b=x2;
- }
- else
- {
- a=x2;
- b=x1;
- }
- for(i=0;;i++)
- {
- xn=(a+b)/2;
- fxn=func(xn);
- printf("%d app\n x1= %.3f x2=%.3f x4=%.3f x4=%.3f\n",i+1,a,b,xn,fxn);
- if(fxn>0)
- {
- b=xn;
- }
- else
- {
- a=xn;
- }
- sub=abs((b-a)/b);
- if (sub<=E)
- {
- printf("final root=%.3f",a);
- return 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement