0% found this document useful (0 votes)
7 views2 pages

C++ Numerical Methods for Root Finding

The document contains C++ implementations of three numerical methods: Bisection Method, Regular Falsi Method, and Fixed Point Iteration Method. Each method is designed to find the root of the function f(x) = x^3 - x - 2, with specific input and output handling. The methods utilize iterative approaches to converge to a solution with a specified tolerance level.

Uploaded by

drkspyder.089
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

C++ Numerical Methods for Root Finding

The document contains C++ implementations of three numerical methods: Bisection Method, Regular Falsi Method, and Fixed Point Iteration Method. Each method is designed to find the root of the function f(x) = x^3 - x - 2, with specific input and output handling. The methods utilize iterative approaches to converge to a solution with a specified tolerance level.

Uploaded by

drkspyder.089
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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;
}

You might also like