Advertisement
Guest User

Bisection_Yana

a guest
May 26th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. double func(double x)
  6. {
  7.     return (-x*x + 6 * x + 5);
  8. }
  9.  
  10.  
  11. double c;
  12.  
  13. void bisection(double a, double b,double e)
  14. {
  15.     c = a; int i = 0;
  16.     while (abs(b - a) >= e)
  17.     {
  18.         cout << "Iteration #" << i << endl;
  19.         c = (a + b) / 2;
  20.         if (func(c) > func(a)) {
  21.             cout << "Midpoint = " << c << endl;
  22.             cout << "f(" << c << ")=" << func(c) << endl;
  23.             a = c;
  24.         }
  25.         else {
  26.             cout << "Midpoint = " << c << endl;
  27.             cout << "f(" << c << ")=" << func(c) << endl;
  28.             b = c;
  29.         }
  30.         i++;
  31.     }
  32. }
  33.  
  34. int main()
  35. {
  36.     double a, b, e;
  37.     a = 0;
  38.     b = 5;
  39.     cout << "a = " << a << endl;
  40.     cout << "b = " << b << endl;
  41.     cout << "Enter e:";
  42.     cin >> e;
  43.     bisection(a, b,e);
  44.     cout << "\n";
  45.     cout << "Point of maximum is = " << c << endl;
  46.     cout << "Extr is = " << func(c) << endl;
  47.  
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement