Program 1: Bisection Method (Algebraic Equation)
Finds the root of a polynomial function using the Bisection Method.
#include <iostream>
#include <cmath>
using namespace std;
double f(double x) { return x * x * x - x - 2; }
int main() {
double a = 1, b = 2, c;
while (fabs(a - b) >= 0.0001) {
c = (a + b) / 2;
if (f(a) * f(c) < 0) b = c;
else a = c;
}
cout << "Root = " << c << endl;
return 0;
}
Program 2: Bisection Method (Transcendental Equation)
Solves equations like 3x - cos(x) - 1 = 0 using the Bisection Method.
#include <iostream>
#include <cmath>
using namespace std;
double f(double x) { return 3*x - cos(x) - 1; }
int main() {
double a = 0, b = 1, c;
while (fabs(a - b) >= 0.0001) {
c = (a + b) / 2;
if (f(a) * f(c) < 0) b = c;
else a = c;
}
cout << "Root = " << c << endl;
return 0;
}
Program 3: Regula Falsi Method
Uses linear interpolation between two points to find a root.
#include <iostream>
#include <cmath>
using namespace std;
double f(double x) { return x * x * x - x - 1; }
int main() {
double a = 1, b = 2, c;
for (int i = 0; i < 20; i++) {
c = (a * f(b) - b * f(a)) / (f(b) - f(a));
if (f(a) * f(c) < 0) b = c;
else a = c;
}
cout << "Root = " << c << endl;
return 0;
}