thespeedracer38

Bisection Method by Me

Jan 31st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4.  
  5. #define POSITIVE 1
  6. #define NEGATIVE -1
  7.  
  8. using namespace std;
  9.  
  10. class BisectionMethod{
  11.         int next_i, limit;
  12.         int lower_bound, upper_bound;
  13.         double step;
  14.         double eps;
  15.     public:
  16.         BisectionMethod(int, int, double, double);
  17.         double f(double);
  18.         double* givePoints();
  19.         double calcRoots(double, double);
  20.         void displayRoots();
  21.         int signOf(double);
  22. };
  23.  
  24. inline double BisectionMethod::f(double x){
  25.     return (x-1.11)*(x-1.12)*(x-1.13)*(x + 1.14);  
  26. }
  27.  
  28. inline int BisectionMethod::signOf(double val){
  29.     if(val < 0){
  30.         return NEGATIVE;
  31.     }
  32.     else{
  33.         return POSITIVE;
  34.     }
  35. }
  36.  
  37. BisectionMethod::BisectionMethod(int lower_bound, int upper_bound, double step, double eps){
  38.     BisectionMethod::upper_bound = upper_bound;
  39.     BisectionMethod::lower_bound = lower_bound;
  40.     BisectionMethod::step = step;
  41.     BisectionMethod::next_i = 0;
  42.     BisectionMethod::eps = eps;
  43.     limit = (int)((BisectionMethod::upper_bound - BisectionMethod::lower_bound) / BisectionMethod::step);
  44. }
  45.  
  46. double* BisectionMethod::givePoints(){
  47.     double curr_x, next_x;
  48.     double *arr = new double[2];
  49.     for(int i = next_i; i < limit; i++){
  50.         curr_x = lower_bound + (i * step);
  51.         next_x = lower_bound + ((i + 1) * step);
  52.         // Check if f(curr_x) and f(next_x) are of opposite signs
  53.         double f_curr_x = f(curr_x);
  54.         double f_next_x = f(next_x);
  55.         if((f_curr_x * f_next_x) < 0){
  56.             next_i = i + 2;
  57.             arr[0] = curr_x;
  58.             arr[1] = next_x;
  59.             break;
  60.         }
  61.     }
  62.     return arr;
  63. }
  64.  
  65. double BisectionMethod::calcRoots(double a, double b){
  66.     int count = 1;
  67.     //cout << "SNO\t\ta(-ve)\t\tb(+ve)\t\tc\t\tf(c)\t\tSign of f(c)\n";
  68.     double c;
  69.     double fc;
  70.     double last_fc;
  71.     do{
  72.         //cout << count << "\t\t" << a << "\t\t" << b << "\t\t";
  73.         c = (a + b) / 2;
  74.         fc = f(c);
  75.         if(signOf(fc) == POSITIVE){
  76.             b = c;
  77.         }
  78.         else {
  79.             a = c;
  80.         }
  81.         //cout << c << "\t\t" << fc << "\t\t" << signOf(fc) << endl;
  82.         //cin.get();
  83.         if(count != 1){
  84.             if((last_fc - fc) < eps){
  85.                 break;
  86.             }
  87.         }
  88.         last_fc = fc;
  89.         count++;
  90.     }
  91.     while(fabs(fc) > eps); 
  92.     return c;
  93. }
  94.  
  95. void BisectionMethod::displayRoots(){
  96.     double *arr = NULL;
  97.     double root;
  98.     do{
  99.         arr = givePoints();
  100.         root = calcRoots(arr[0], arr[1]);
  101.         cout << "Root between " << arr[0] << " and " << arr[1] << " is " << root << endl;
  102.         cin.get();
  103.         delete arr;
  104.         arr = NULL;
  105.     }while(next_i != limit);
  106. }
  107.  
  108. int main() {
  109.     system("cls");
  110.     BisectionMethod obj(-10, 10, 1e-2, 1e-6);
  111.     //double *arr = obj.givePoints();
  112.     //cout << "arr[0] = " << arr[0] << endl;
  113.     //cout << "arr[1] = " << arr[1] << endl;
  114.     //double root = obj.calcRoots(1, 2);
  115.     //cout << "Root = " << root << endl;
  116.     //delete arr;
  117.     obj.displayRoots();
  118.     cin.get();
  119.     return 0;
  120. }
Add Comment
Please, Sign In to add comment