#include <iostream> #include <iomanip> #include <cmath> using namespace std; // The function f(x) = 2x^3 + 3x - 1 double function(double x) { return 2.0 * pow(x, 3) + 3.0 * x - 1.0; } // Bisection Method Implementation void bisection_method(double a, double b, double tolerance) { // Check if the initial interval [a, b] brackets a root if (function(a) * function(b) >= 0) { cout << "Error: The Bisection Method requires f(a) and f(b) to have opposite signs." << endl; return; } double x_m; // Midpoint int iteration = 0; cout << setprecision(6) << fixed; // --- Displaying Input --- cout << "--- Bisection Method for f(x) = 2x^3 + 3x - 1 ---" << endl; cout << "Initial Interval [a, b]: [" << a << ", " << b << "]" << endl; cout << "Tolerance (E): " << scientific << tolerance << fixed << endl; cout << "\n"; // --- Displaying Iteration Table Header --- cout << setw(10) << "Iter" << setw(15) << "a (x0)" << setw(15) << "b (x1)" << setw(15) << "f(a)" << setw(15) << "f(b)" << setw(15) << "xm" << setw(15) << "f(xm)" << setw(15) << "|b-a|" << endl; cout << string(125, '-') << endl; // --- Logic Building Phase (Iteration) --- while (abs(b - a) >= tolerance) { iteration++; // Calculate the midpoint x_m = (a + b) / 2.0; double f_a = function(a); double f_b = function(b); double f_m = function(x_m); // Print the current iteration step cout << setw(10) << iteration << setw(15) << a << setw(15) << b << setw(15) << f_a << setw(15) << f_b << setw(15) << x_m << setw(15) << f_m << setw(15) << abs(b - a) << endl; // Check if x_m is the root if (f_m == 0.0) { break; } // Update the interval [a, b] if (f_a * f_m < 0) { b = x_m; // Root is in [a, x_m] } else { a = x_m; // Root is in [x_m, b] } } // --- Displaying Output (Result) --- cout << string(125, '-') << endl; cout << "The final root is approximately: " << x_m << endl; cout << "Found after " << iteration << " iterations." << endl; } int main() { // Set the initial parameters based on the problem task: // Starting interval [0, 1] and tolerance 10^-4 double a = 0.0; double b = 1.0; double tolerance = 0.0001; bisection_method(a, b, tolerance); return 0; }
Standard input is empty
--- 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.