Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #include<string.h>
- using namespace std;
- const int MAX_ITERATION = 1e4;
- double root; /// Answer
- double f(double x)
- {
- return x*x - 4*x - 10; // -1.742 , 5.742
- }
- void FLASE_POSITION(double a,double b)
- {
- if(f(a)==0 || f(b)==0)
- {
- f(a)==0?root=a:root=b;
- return;
- }
- if(f(a)*f(b) >0)
- {
- puts("WRONG INITIAL GUESS");
- exit(0);
- }
- for(int i = 1;i <= MAX_ITERATION; i++)
- {
- root = (a*f(b) - b*f(a)) / (f(b) - f(a));
- if(f(root) == 0)
- {
- break;
- }
- if(f(root)*f(a) < 0)
- {
- b = root;
- }
- else
- {
- a = root;
- }
- }
- }
- int main()
- {
- double a,b;
- scanf("%lf %lf",&a,&b);
- FLASE_POSITION(a,b);
- printf("One root of the equation is : %.06lf\n",root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement