Bisection Method C++ Program: 2x^2 - 38 = 0
// Bisection Method to find root of 2x^2 - 38 = 0
#include <iostream>
#include <cmath>
using namespace std;
// Define the function: f(x) = 2x^2 - 38
float f(float x) {
return 2 * x * x - 38;
}
int main() {
float a, b, c, maxerr;
int i = 0;
char choice;
do {
cout << "\nEnter the lower limit, upper limit, and max error: ";
cin >> a >> b >> maxerr;
if (f(a) * f(b) >= 0) {
cout << "\nThe chosen interval does not bracket the root.\n";
} else {
i = 0;
do {
c = (a + b) / 2;
if (f(a) * f(c) < 0)
b = c;
else
a = c;
i++;
} while (fabs((b - a) / a) > maxerr);
cout << "\nRoot of the equation after " << i << " iterations = " << c <<
endl;
}
cout << "\nDo you want to continue? (y/n): ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
return 0;
}