fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. // The function f(x) = 2x^3 + 3x - 1
  8. double function(double x) {
  9. return 2.0 * pow(x, 3) + 3.0 * x - 1.0;
  10. }
  11.  
  12. // Bisection Method Implementation
  13. void bisection_method(double a, double b, double tolerance) {
  14. // Check if the initial interval [a, b] brackets a root
  15. if (function(a) * function(b) >= 0) {
  16. cout << "Error: The Bisection Method requires f(a) and f(b) to have opposite signs." << endl;
  17. return;
  18. }
  19.  
  20. double x_m; // Midpoint
  21. int iteration = 0;
  22.  
  23. cout << setprecision(6) << fixed;
  24.  
  25. // --- Displaying Input ---
  26. cout << "--- Bisection Method for f(x) = 2x^3 + 3x - 1 ---" << endl;
  27. cout << "Initial Interval [a, b]: [" << a << ", " << b << "]" << endl;
  28. cout << "Tolerance (E): " << scientific << tolerance << fixed << endl;
  29. cout << "\n";
  30.  
  31. // --- Displaying Iteration Table Header ---
  32. cout << setw(10) << "Iter"
  33. << setw(15) << "a (x0)"
  34. << setw(15) << "b (x1)"
  35. << setw(15) << "f(a)"
  36. << setw(15) << "f(b)"
  37. << setw(15) << "xm"
  38. << setw(15) << "f(xm)"
  39. << setw(15) << "|b-a|" << endl;
  40. cout << string(125, '-') << endl;
  41.  
  42. // --- Logic Building Phase (Iteration) ---
  43. while (abs(b - a) >= tolerance) {
  44. iteration++;
  45.  
  46. // Calculate the midpoint
  47. x_m = (a + b) / 2.0;
  48.  
  49. double f_a = function(a);
  50. double f_b = function(b);
  51. double f_m = function(x_m);
  52.  
  53. // Print the current iteration step
  54. cout << setw(10) << iteration
  55. << setw(15) << a
  56. << setw(15) << b
  57. << setw(15) << f_a
  58. << setw(15) << f_b
  59. << setw(15) << x_m
  60. << setw(15) << f_m
  61. << setw(15) << abs(b - a) << endl;
  62.  
  63. // Check if x_m is the root
  64. if (f_m == 0.0) {
  65. break;
  66. }
  67.  
  68. // Update the interval [a, b]
  69. if (f_a * f_m < 0) {
  70. b = x_m; // Root is in [a, x_m]
  71. } else {
  72. a = x_m; // Root is in [x_m, b]
  73. }
  74. }
  75.  
  76. // --- Displaying Output (Result) ---
  77. cout << string(125, '-') << endl;
  78. cout << "The final root is approximately: " << x_m << endl;
  79. cout << "Found after " << iteration << " iterations." << endl;
  80. }
  81.  
  82. int main() {
  83. // Set the initial parameters based on the problem task:
  84. // Starting interval [0, 1] and tolerance 10^-4
  85. double a = 0.0;
  86. double b = 1.0;
  87. double tolerance = 0.0001;
  88.  
  89. bisection_method(a, b, tolerance);
  90.  
  91. return 0;
  92. }
Success #stdin #stdout 0.01s 5332KB
stdin
Standard input is empty
stdout
--- Bisection Method for f(x) = 2x^3 + 3x - 1 ---
Initial Interval [a, b]: [0.000000, 1.000000]
Tolerance (E): 1.000000e-04

      Iter         a (x0)         b (x1)           f(a)           f(b)             xm          f(xm)          |b-a|
-----------------------------------------------------------------------------------------------------------------------------
         1       0.000000       1.000000      -1.000000       4.000000       0.500000       0.750000       1.000000
         2       0.000000       0.500000      -1.000000       0.750000       0.250000      -0.218750       0.500000
         3       0.250000       0.500000      -0.218750       0.750000       0.375000       0.230469       0.250000
         4       0.250000       0.375000      -0.218750       0.230469       0.312500      -0.001465       0.125000
         5       0.312500       0.375000      -0.001465       0.230469       0.343750       0.112488       0.062500
         6       0.312500       0.343750      -0.001465       0.112488       0.328125       0.055031       0.031250
         7       0.312500       0.328125      -0.001465       0.055031       0.320312       0.026666       0.015625
         8       0.312500       0.320312      -0.001465       0.026666       0.316406       0.012571       0.007812
         9       0.312500       0.316406      -0.001465       0.012571       0.314453       0.005546       0.003906
        10       0.312500       0.314453      -0.001465       0.005546       0.313477       0.002039       0.001953
        11       0.312500       0.313477      -0.001465       0.002039       0.312988       0.000287       0.000977
        12       0.312500       0.312988      -0.001465       0.000287       0.312744      -0.000589       0.000488
        13       0.312744       0.312988      -0.000589       0.000287       0.312866      -0.000151       0.000244
        14       0.312866       0.312988      -0.000151       0.000287       0.312927       0.000068       0.000122
-----------------------------------------------------------------------------------------------------------------------------
The final root is approximately: 0.312927
Found after 14 iterations.