Advertisement
skb50bd

CSE225(1)Lab1 [Bisection & False-Position]

May 13th, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 KB | None | 0 0
  1. // Bisection and False Position Method
  2. // Author   : Shakib Haris
  3. // Date     : 17 May 2017
  4.  
  5. #include <iomanip>
  6. #include <iostream>
  7. #include <cstdio>
  8. #include <cmath>
  9. #include <cstdlib>
  10. #define eps 0.01
  11. #define MAX 5 // maximum degree 4
  12. #define MAX_LOOP 100
  13. using namespace std;
  14.  
  15. int degreeOfEquation;
  16. int numberOfRoots;
  17. int aN[MAX]; // array to hold the coefficients
  18. double ranges[MAX][2]; // array to hold the ranges for the roots
  19. double roots[MAX]; // array to hold the roots;
  20.  
  21. bool confirm () {
  22.     char ch;
  23.     while (true) {
  24.         cin.sync();
  25.         ch = getchar();
  26.         if (ch == 'y' || ch == 'Y' || ch == '\0')
  27.             return true;
  28.         else if (ch == 'n' || ch == 'N')
  29.             return false;
  30.         else
  31.             cout << "Invalid input" << endl << "Try again!" << endl << "(Y/n): ";
  32.     }
  33. }
  34.  
  35. double f (double x) { //
  36.     double total = 0.0;
  37.     for (int i = 0; i <= degreeOfEquation; i++)
  38.         total += aN[i] * pow (x, degreeOfEquation - i);
  39.     return total;
  40. }
  41.  
  42. void getRanges () {
  43.     cout << "Find Ranges automatically? (Y/n): ";
  44.     if (confirm()) {
  45.         int xMax = abs ((int) ceil (sqrt (pow ((((double) aN[1]) / aN[0]), 2) - 2 * (((double) aN[2]) / aN[0]))));
  46.         cout << "Xmax is " << xMax << endl;
  47.         for (int i = (-1) * (xMax); i < xMax; i++) {
  48.             if (f (i) == 0) {
  49.                 ranges[numberOfRoots][0] = i;
  50.                 ranges[numberOfRoots][1] = i;
  51.                 numberOfRoots++;
  52.             }
  53.             else if ((f (i) * f (i + 1)) < 0) {
  54.                 ranges[numberOfRoots][0] = i;
  55.                 ranges[numberOfRoots][1] = i + 1;
  56.                 numberOfRoots++;
  57.             }
  58.         }
  59.         cout << "Found " << numberOfRoots << " Range(s)" << endl;
  60.     }
  61.     else {
  62.         cout << "Number of ranges: ";
  63.         cin >> numberOfRoots;
  64.         for (int i = 0; i < numberOfRoots; i++) {
  65.             cout << "Range " << i + 1 << " Lower Bound: ";
  66.             cin >> ranges[i][0];
  67.             cout << "Range " << i + 1 << " Upper Bound: ";
  68.             cin >> ranges[i][1];
  69.         }
  70.     }
  71.  
  72.     // print ranges
  73.     for (int i = 0; i < numberOfRoots; i++)
  74.         cout << "Range " << i + 1 << ": " << ranges[i][0] << ", " << ranges[i][1] << endl;
  75.     cout << endl << endl;
  76. }
  77.  
  78. void getCoefficients () {
  79.     cout << "Enter the Coefficients for all the degrees of X" << endl;
  80.     for (int i = 0; i <= degreeOfEquation; i++) {
  81.         cout << "An-" << i << ": ";
  82.         cin >> aN[i];
  83.     }
  84. }
  85.  
  86. void bisect () {
  87.     double x0, x1, x2;
  88.     double f0, f1, f2;
  89.     int loopCount;
  90.  
  91.     for (int i = 0; i < numberOfRoots; i++) {
  92.         x1 = ranges[i][0];
  93.         x2 = ranges[i][1];
  94.         x0 = (x1 + x2) / 2;
  95.        
  96.         f1 = f(x1);
  97.         f2 = f(x2);
  98.         f0 = f(x0);
  99.  
  100.         loopCount = 0;
  101.         while (fabs (f0) >= eps && loopCount < MAX_LOOP) {
  102.             if (f0 * f1 < 0) {
  103.                 x2 = x0;
  104.                 f2 = f0;
  105.             }
  106.             else {
  107.                 x1 = x0;
  108.                 f1 = f0;
  109.             }
  110.             x0 = (x1 + x2) / 2;
  111.             f0 = f(x0);
  112.             loopCount++;   
  113.         }
  114.         if (loopCount != MAX_LOOP) {
  115.             roots[i] = x0;
  116.             cout << "Found Root " << x0 << endl;
  117.         }
  118.         else {
  119.             cout << "Root not Found for Range: " << ranges[i][0] << ", " << ranges[i][1] << endl;
  120.             roots[i] = INT_MAX;
  121.         }
  122.     }
  123. }
  124.  
  125. void falsePosition () {
  126.     double x0, x1, x2;
  127.     double f0, f1, f2;
  128.     int loopCount;
  129.  
  130.     for (int i = 0; i < numberOfRoots; i++) {
  131.         x1 = ranges[i][0];
  132.         x2 = ranges[i][1];
  133.        
  134.         f1 = f(x1);
  135.         f2 = f(x2);
  136.        
  137.         if (x1 != x2)
  138.             x0 = x1 - (f1 * ((x2 - x1) / (f2 - f1)));
  139.         else
  140.             x0 = x1;
  141.            
  142.         f0 = f(x0);
  143.        
  144.         loopCount = 0;
  145.         while (fabs (f0) >= eps && loopCount < MAX_LOOP) {
  146.             if (f0 * f1 < 0) {
  147.                 x2 = x0;
  148.                 f2 = f0;
  149.             }
  150.             else {
  151.                 x1 = x0;
  152.                 f1 = f0;
  153.             }
  154.             x0 = x1 - (f1 * ((x2 - x1) / (f2 - f1)));
  155.             f0 = f(x0);
  156.             loopCount++;
  157.         }
  158.         if (loopCount != MAX_LOOP) {
  159.             roots[i] = x0;
  160.             cout << "Found Root " << x0 << endl;
  161.         }
  162.         else {
  163.             cout << "Root not Found for Range: " << ranges[i][0] << ", " << ranges[i][1] << endl;
  164.             roots[i] = INT_MAX;
  165.         }
  166.     }
  167. }
  168.  
  169.  
  170. int main () {
  171.     cout << "Degree of the Equation: ";
  172.     cin >> degreeOfEquation;
  173.  
  174.     // user inputs the coefficients
  175.     getCoefficients ();
  176.     system("cls");
  177.  
  178.     // getting ranges
  179.     getRanges();
  180.  
  181.     if (numberOfRoots <= 0) {
  182.         cout << "No Root Found" << endl;
  183.         return 0;
  184.     }
  185.  
  186.     // system("cls");
  187.     cout << "running BISECTION Method" << endl;
  188.     bisect ();
  189.     cout << endl << endl;
  190.     cout << "running FALSE-POSITION Method" << endl;   
  191.     falsePosition ();
  192.     //system("cls");
  193.  
  194.     // printing the roots
  195.     // cout << "Root(s): ";
  196.     // for (int i = 0; i < numberOfRoots; i++)
  197.     //  cout << fixed << setprecision(4) << roots[i] << " ";
  198.     // cout << endl;
  199.  
  200.     return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement