thespeedracer38

Bisection Method by Me (Updated) *Does not work

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