Advertisement
a4ary4n

Bisection

Oct 10th, 2020 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. //Author: Aryan Surana
  2. //ID    : 2018B4A70937H
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. float f(float x) {
  8.     float result = 3*x - cos(x) - 1;
  9.     return result;
  10. }
  11.  
  12. int main() {
  13.     float xl, xu, xm;
  14.     cout << "Enter xl and xu: ";
  15.     cin >> xl >> xu;
  16.     char yn;
  17.  
  18.     do {
  19.         cout << "Press y to start or n to exit: ";
  20.         cin >> yn;
  21.         float fxl = f(xl), fxu = f(xu), fxm = 0;
  22.         xm = (xl + xu)/2;
  23.         cout << "xm = " << xm;
  24.         fxm = f(xm);
  25.         cout << " f(xm) = " << fxm << "\n";
  26.         if (fxm*fxl < 0) {
  27.             xu = xm;
  28.             continue;
  29.         }
  30.         else if (fxm*fxl > 0) {
  31.             xl = xm;
  32.         }
  33.  
  34.     }
  35.     while (yn == 'y');
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement