Advertisement
Nur_Mohammed_Mehedy

FALSE POSITION

Apr 29th, 2021
1,185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1.  
  2. #include<bits/stdc++.h>
  3. #include<string.h>
  4. using namespace std;
  5. const int MAX_ITERATION = 1e4;
  6.  
  7. double root; /// Answer
  8.  
  9. double f(double x)
  10. {
  11.     return x*x - 4*x - 10;  // -1.742 , 5.742
  12. }
  13.  
  14.  
  15. void FLASE_POSITION(double a,double b)
  16. {
  17.     if(f(a)==0 || f(b)==0)
  18.     {
  19.         f(a)==0?root=a:root=b;
  20.         return;
  21.     }
  22.  
  23.     if(f(a)*f(b) >0)
  24.     {
  25.         puts("WRONG INITIAL GUESS");
  26.         exit(0);
  27.     }
  28.  
  29.     for(int i = 1;i <= MAX_ITERATION; i++)
  30.     {
  31.         root = (a*f(b) - b*f(a)) / (f(b) - f(a));
  32.  
  33.         if(f(root) == 0)
  34.         {
  35.             break;
  36.         }
  37.         if(f(root)*f(a) < 0)
  38.         {
  39.             b = root;
  40.         }
  41.         else
  42.         {
  43.             a = root;
  44.         }
  45.     }
  46.  
  47. }
  48.  
  49. int main()
  50. {
  51.  
  52.     double a,b;
  53.  
  54.     scanf("%lf %lf",&a,&b);
  55.  
  56.     FLASE_POSITION(a,b);
  57.  
  58.     printf("One root of the equation is : %.06lf\n",root);
  59.  
  60.     return 0;
  61. }
  62.  
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement