Numerical Methods Assignment
1. Bisection Method (C++)
#include <iostream>
#include <cmath>
using namespace std;
double f(double x) {
return x*x*x - x - 2;
}
int main() {
double a, b, c;
int iterations = 0;
cin >> a >> b;
if (f(a) * f(b) >= 0) {
return 0;
}
do {
c = (a + b) / 2.0;
iterations++;
if (f(a) * f(c) < 0) {
b = c;
}
else {
a = c;
}
} while (fabs(f(c)) > 0.0001);
cout << c << endl;
return 0;
}
2. Regular Falsi Method (C++)
#include <iostream>
#include <cmath>
using namespace std;
double f(double x) {
return x*x*x - x - 2;
}
int main() {
double a, b, c;
int iterations = 0;
cin >> a >> b;
if (f(a) * f(b) >= 0) {
return 0;
}
do {
c = (a*f(b) - b*f(a)) / (f(b) - f(a));
iterations++;
if (f(a) * f(c) < 0) {
b = c;
}
else {
a = c;
}
} while (fabs(f(c)) > 0.0001);
cout << c << endl;
return 0;
}
3. Fixed Point Iteration Method (C++)
#include <iostream>
#include <cmath>
using namespace std;
double g(double x) {
return cbrt(x + 2);
}
int main() {
double x0, x1;
int iterations = 0;
cin >> x0;
do {
x1 = g(x0);
iterations++;
x0 = x1;
} while (fabs(g(x0) - x0) > 0.0001);
cout << x1 << endl;
return 0;
}