fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. // ফাংশন সংজ্ঞা: f(x) = x^3 - x - 3
  8. double f(double x) {
  9. return pow(x, 3) - x - 3;
  10. }
  11.  
  12. int main() {
  13. double x1, x2, x3, f1, f2;
  14. const double tolerance = 1e-6;
  15. const int max_iterations = 100;
  16. int iteration = 1;
  17.  
  18. // প্রাথমিক অনুমান গ্রহণ
  19. cout << "x1 এর মান প্রবেশ করুন: ";
  20. cin >> x1;
  21. cout << "x2 এর মান প্রবেশ করুন: ";
  22. cin >> x2;
  23.  
  24. cout << fixed << setprecision(6);
  25. cout << "\nIteration\tx1\t\tx2\t\tx3\t\tf(x1)\t\tf(x2)\n";
  26.  
  27. while (iteration <= max_iterations) {
  28. f1 = f(x1);
  29. f2 = f(x2);
  30.  
  31. // বিভাজকের মান পরীক্ষা
  32. if (fabs(f2 - f1) < 1e-12) {
  33. cout << "বিভাজকের মান খুব ছোট। শূন্য দ্বারা বিভাজনের ঝুঁকি।\n";
  34. break;
  35. }
  36.  
  37. // পরবর্তী আনুমানিক মান গণনা
  38. x3 = (f2 * x1 - f1 * x2) / (f2 - f1);
  39.  
  40. // বর্তমান পুনরাবৃত্তির ফলাফল প্রদর্শন
  41. cout << iteration << "\t\t" << x1 << "\t" << x2 << "\t" << x3 << "\t" << f1 << "\t" << f2 << "\n";
  42.  
  43. // সঙ্গতি পরীক্ষা
  44. if (fabs((x3 - x2) / x3) < tolerance) {
  45. cout << "\nআনুমানিক মূল = " << x3 << endl;
  46. break;
  47. }
  48.  
  49. // পরবর্তী পুনরাবৃত্তির জন্য মান হালনাগাদ
  50. x1 = x2;
  51. x2 = x3;
  52. iteration++;
  53. }
  54.  
  55. if (iteration > max_iterations) {
  56. cout << "সর্বাধিক পুনরাবৃত্তির মধ্যে সমাধানে পৌঁছানো যায়নি।\n";
  57. }
  58.  
  59. return 0;
  60. }
  61.  
Success #stdin #stdout 0.01s 5308KB
stdin
Enter the value of x1: 10
Enter the value of x2: 8
stdout
x1 এর মান প্রবেশ করুন: x2 এর মান প্রবেশ করুন: 
Iteration	x1		x2		x3		f(x1)		f(x2)
বিভাজকের মান খুব ছোট। শূন্য দ্বারা বিভাজনের ঝুঁকি।