Bisection Method
AIM:
Write and execute a C++ program for finding roots of a non-linear equation using the
Bisection method.
PRINCIPLE:
Mathematical methods for a wide variety of problems in science and engineering can be
formulated in equations of the form f(x) = 0. The solution which satisfies the equation is
called the root of the equation.
A non-linear function can be defined as the one with one variable which does not have a
straight line form.
Example: f(x) = x² + 1
Methods for solving a non-linear equation:
1. Direct method
2. Graphical method
3. Trial and error method
4. Iterative method
5. Analytical method
Among these, iterative methods are more popular and less prone to error.
Types of Iterative Methods:
- Bracketing method (Interpolation)
- Open-end method
Bracketing method starts with two initial guess values that bracket the root and then
reduces the bracket width.
THEORY:
The Bisection method (or binary chopping method) is applicable when f(x) is continuous
and changes sign in the interval [a, b]. If f(a) × f(b) < 0, then there exists at least one root in
(a, b).
Let a = x₁, b = x₂, and x₀ be the midpoint:
x₀ = (x₁ + x₂) / 2
There are 3 possibilities:
1. f(x₀) = 0 → root found
2. f(x₀) × f(x₁) < 0 → root lies between x₀ and x₁
3. f(x₀) × f(x₂) < 0 → root lies between x₀ and x₂
By checking the sign of f at the midpoint, the interval is halved to locate the root. Repeat
until desired precision.
PROCEDURE:
1. Formulate the problem
2. Write algorithm or flow chart
3. Develop source code in C++
4. Compile the code and check for errors
5. Run the program
6. Repeat the experiment with different inputs
7. Verify results
ALGORITHM:
1. Start
2. Decide x₁ and x₂
3. Compute f₁ = f(x₁), f₂ = f(x₂)
4. If f₁ × f₂ > 0, stop
5. Compute x₀ = (x₁ + x₂) / 2, f₀ = f(x₀)
6. If f₁ × f₀ < 0 → x₂ = x₀, else x₁ = x₀
7. If |x₁ − x₂| < 𝜀, then root = (x₁ + x₂)/2, stop
8. Else go to Step 5
SOURCE CODE:
#include<iostream.h>
#include<conio.h>
#include<math.h>
const double eps = 0.001;
class bisect {
private:
double x1, x2, f1, f2, root, x0, f0;
int status, count;
double function(double x);
void bisect1();
public:
void getdata();
};
double bisect::function(double x) {
return ((x * x) - (4 * x) - 10);
}
void bisect::bisect1() {
f1 = function(x1);
f2 = function(x2);
if ((f1 * f2) > 0.0) {
status = 0;
return;
} else {
count = 0;
while (1) {
x0 = (x1 + x2) / 2.0;
f0 = function(x0);
if (f0 == 0) {
status = 1;
root = x0;
return;
}
if ((f1 * f0) < 0) {
x2 = x0;
} else {
x1 = x0;
f1 = f0;
}
if (fabs((x2 - x1) / x2) < eps) {
status = 1;
root = (x1 + x2) / 2.0;
return;
} else {
count++;
}
}
}
}
void bisect::getdata() {
cout << "\n\nEnter the value of x1: ";
cin >> x1;
cout << "Enter the value of x2: ";
cin >> x2;
f1 = 0; f2 = 0; root = 0; x0 = 0; f0 = 0; status = 0; count = 0;
bisect1();
if (status == 0) {
cout << "\n\tStarting points do not bisect even roots\n";
} else {
cout << "\n\tRoots = " << root;
cout << "\n\tFunction(root) = " << function(root);
cout << "\n\tIterations = " << count;
}
}
void main() {
bisect b;
clrscr();
cout << "\n\tFinding a root of a non-linear equation:";
cout << "\n\tby bisection method\n";
b.getdata();
getch();
}
OUTPUT:
Finding a root of a non-linear equation: by bisection method
Enter the value of x1: 5
Enter the value of x2: 7
Roots = 5.740234
Function(root) = -0.010647
Iterations = [some integer]
Finding a root of a non-linear equation: by bisection method
Enter the value of x1: 5.5
Enter the value of x2: 6