Bisection Method in C
This section will discuss the bisection method in the C programming language. The bisection method is a simple and convergence method used to get the real roots of non-linear equations. The Bisection method repeatedly bisects or separates the interval and selects a subinterval in which the root of the given equation is found.
It means if a function f(x) is continuous in the closed interval [a, b] and the f(a) and f(b) are two real numbers of opposite signs that contain at least one real root of f(x) = 0, between a and b. This method is also known as the Bolzano or Half Interval or Binary search method.
Bisection Method Algorithm:
Following is the algorithm of the Bisection Method in C.
- Start the program.
- Input two initial guesses x1 and x2. The ‘e’ is the absolute error to get the desired degree of accuracy.
- Compute the function value: f1 = f(x1) and f2 = f(x2)
- Now compare the product of f1 and f2 with 0, as
If (f1 * f2) > 0, it displays the initial guesses are wrong and transfer control to step 11.
- Get the mid value: x = (x1 + x2) / 2
- If ( [ (x1 – x2) / x] < e), then print the value x and jump to (11).
- Else, it shows: f = f(x)
- If (( f * f1) > 0), then assign x1 = x and f1 = f.
- Else, assign x2 = x and f2 = f.
- Jump to 5. And the loop continues with new values.
- Terminate the program.
Example 1: Program to find the root of the given equation using the Bisection method
Let’s consider an example to get the approximation root of an equation using the Bisection method and for loop in the C programming language.
Enter the first starting point: 5 Enter the second ending point: 9 Enter the maximum iteration to be allowed: 8 Input the no. of allowed error point: 0.02 Iteration1:7.000000 Iteration1: 8.000000 Iteration2: 8.500000 Iteration3: 8.750000 Iteration4: 8.875000 Iteration5: 8.937500 Iteration6: 8.968750 Iteration7: 8.984375 The approximation root is: 8.984375
Example 2: Program to find the real root of the (x3 + 3x – 5 = 0) equation using the Bisection method
Let’s consider an example to print the real roots using the Bisection method in the C programming language.
Display the real roots of the given equation using the Bisection method: X ^ 3 + 3 * x - 5 = 0 Enter the first approximation of the root: 1 Enter the second approximation of the root: 5 Input the number of iteration you want to perform: 7 The root after 1 iterations is 3.000000 The root after 2 iterations is 2.000000. The root after 3 iterations is 1.500000. The root after 4 iterations is 1.250000. The root after 5 iterations is 1.125000. The root after 6 iterations is 1.187500. The root after 7 iterations is 1.156250. The approximation root is 1.156250
Example 3: Program to find the approximation root of the non-algebraic function using the Bisection method
Let’s create a simple program to calculate the approximation root using the Bisection method and do while loop in C programming language.
Input the initial approximation for x: 1 Input the initial approximation for y: 3 Define the input accuracy: .002 Input the maximum number of iteration: 10 Iteration x y z f(z) |x - y| 1 1.000000 3.000000 2.000000 -0.479759 2.000000 2 1.000000 2.000000 1.500000 1.015806 1.000000 3 1.500000 2.000000 1.750000 0.479383 0.500000 4 1.750000 2.000000 1.875000 0.058267 0.250000 5 1.875000 2.000000 1.937500 -0.195362 0.125000 6 1.875000 1.937500 1.906250 -0.064801 0.062500 7 1.875000 1.906250 1.890625 -0.002343 0.031250 8 1.875000 1.890625 1.882812 0.028192 0.015625 9 1.882812 1.890625 1.886719 0.012982 0.007812 10 1.886719 1.890625 1.888672 0.005334 0.003906 The root of the given equation is: 1.888672
Advantages of the Bisection Method:
- The Bisection method is guaranteed to the convergence of real roots.
- It reduces the chances of error in the non-linear equation.
- It evaluates one function at each iteration.
- It usually convergence in a linear fashion.
- We can control the error by increasing the number of iterations that return a more accurate root in the bisection method.
- It does not involve complex calculations to get the root.
- The bisection method is faster in the case of multiple roots.
Disadvantages of the Bisection Method
- In the Bisection method, the convergence is very slow as compared to other iterative methods.
- The rate of approximation of convergence in the bisection method is 0.5.
- It is a linear rate of convergence.
- It fails to get the complex root.
- It cannot be applied if there are any discontinuity occurrs in the guess interval.
- It is unable to find the root for some equations. For example, f(x) = x2 as there are no bracketing or bisect values.
- It cannot be used over an interval when the function takes the same sign values.