fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. // ফাংশন f(x) = 2x² - x - 8
  7. double f(double x) {
  8. return 2 * x * x - x - 8;
  9. }
  10.  
  11. int main() {
  12. double x1, x2, x0, f1, f2, f0, error;
  13. int iteration = 1;
  14. const double EPSILON = 0.0001;
  15.  
  16. cout << "Enter the value of x1: ";
  17. cin >> x1;
  18. cout << "Enter the value of x2: ";
  19. cin >> x2;
  20.  
  21. f1 = f(x1);
  22. f2 = f(x2);
  23.  
  24. // প্রাথমিক অনুমানগুলোর মধ্যে ফাংশনের মানের চিহ্ন বিপরীত কিনা পরীক্ষা
  25. if (f1 * f2 > 0) {
  26. cout << "The initial guesses do not bracket any root." << endl;
  27. return 0;
  28. }
  29.  
  30. cout << "\nIteration x1 x2 x0 f(x1) f(x2) f(x0)\n";
  31.  
  32. do {
  33. // মধ্যবিন্দু নির্ণয়
  34. x0 = (x1 + x2) / 2.0;
  35. f0 = f(x0);
  36.  
  37. // ফলাফল প্রদর্শন
  38. cout << fixed << setprecision(6);
  39. cout << iteration << " "
  40. << x1 << " " << x2 << " "
  41. << x0 << " "
  42. << f1 << " "
  43. << f2 << " "
  44. << f0 << endl;
  45.  
  46. // নতুন সীমা নির্ধারণ
  47. if (f1 * f0 < 0) {
  48. x2 = x0;
  49. f2 = f0;
  50. } else {
  51. x1 = x0;
  52. f1 = f0;
  53. }
  54.  
  55. // ত্রুটি নির্ণয়
  56. error = fabs((x2 - x1) / x2);
  57. iteration++;
  58. } while (error > EPSILON);
  59.  
  60. cout << "\nApproximate root = " << x0 << endl;
  61. return 0;
  62. }
  63.  
  64.  
Success #stdin #stdout 0s 5320KB
stdin
stdout
Enter the value of x1: Enter the value of x2: The initial guesses do not bracket any root.