C Program For Bisection Method With Output

The Bisection Method is a numerical technique used to find the root of a nonlinear equation within a given interval. It is one of the simplest and most reliable methods for root-finding, provided that the function is continuous and has a sign change in the given interval.

This topic will explain how the Bisection Method works, provide a C program implementation, and include sample output for better understanding.

How the Bisection Method Works

The Bisection Method follows these steps:

  1. Select an interval [a, b] where the function changes sign, meaning f(a) * f(b) < 0.

  2. Compute the midpoint (c) using the formula:

    c = frac{a + b}{2}

  3. Evaluate f(c):

    • If f(c) = 0, then c is the root.
    • If f(a) * f(c) < 0, set b = c (the root is in the left subinterval).
    • If f(b) * f(c) < 0, set a = c (the root is in the right subinterval).
  4. Repeat steps 2 and 3 until the desired accuracy (error tolerance) is reached.

C Program for Bisection Method

Here is a C program that implements the Bisection Method:

#include <stdio.h>
#include <math.h>

// Function for which we want to find the root
double f(double x) {
return x * x * x - 4 * x - 9; // Example: f(x) = x^3 - 4x - 9
}

// Bisection Method function
void bisection(double a, double b, double tol) {
if (f(a) * f(b) >= 0) {
printf('Invalid interval. The function must have opposite signs at a and b.n');
return;
}

double c;
int iteration = 1;

printf('Iterationt att btt ctt f(c)n');

do {
c = (a + b) / 2; // Midpoint
double fc = f(c);

printf('%dtt %.6ft %.6ft %.6ft %.6fn', iteration, a, b, c, fc);

if (fc == 0.0 || fabs(b - a) < tol) {
break; // Root found or interval is sufficiently small
}

// Update the interval
if (f(a) * fc < 0) {
b = c;
} else {
a = c;
}

iteration++;

} while (fabs(b - a) >= tol);

printf('nApproximate root: %.6fn', c);
}

// Main function
int main() {
double a, b, tol;

// Input section
printf('Enter the lower bound (a): ');
scanf('%lf', anda);
printf('Enter the upper bound (b): ');
scanf('%lf', andb);
printf('Enter the tolerance (e.g., 0.0001): ');
scanf('%lf', andtol);

// Call Bisection Method
bisection(a, b, tol);

return 0;
}

Explanation of the Code

  1. Function Definition:

    • f(x) defines the mathematical function whose root we want to find.
    • In this example, f(x) = x³ - 4x - 9.
  2. Bisection Function:

    • The function bisection(a, b, tol) takes three parameters:
      • a: lower bound
      • b: upper bound
      • tol: error tolerance
    • It checks whether f(a) * f(b) < 0 to ensure a valid interval.
    • The midpoint c is calculated in each iteration.
    • If f(c) == 0 or the interval is smaller than tol, the loop stops.
    • The updated interval is determined based on the sign of f(c).
  3. Main Function:

    • Takes user input for a, b, and tol.
    • Calls the bisection() function to find the root.

Example Input and Output

Input

Enter the lower bound (a): 2
Enter the upper bound (b): 3
Enter the tolerance (e.g., 0.0001): 0.0001

Output

Iteration     a         b         c         f(c)
1           2.000000   3.000000   2.500000   -2.375000
2           2.500000   3.000000   2.750000   2.796875
3           2.500000   2.750000   2.625000   0.666016
4           2.500000   2.625000   2.562500   -0.841553
5           2.562500   2.625000   2.593750   -0.093781
6           2.593750   2.625000   2.609375   0.285984
7           2.593750   2.609375   2.601563   0.096084
8           2.593750   2.601563   2.597656   0.001129
9           2.593750   2.597656   2.595703   -0.046349
10          2.595703   2.597656   2.596680   -0.022608
11          2.596680   2.597656   2.597168   -0.010740
12          2.597168   2.597656   2.597412   -0.004805
13          2.597412   2.597656   2.597534   -0.001838
14          2.597534   2.597656   2.597595   -0.000354
15          2.597595   2.597656   2.597626   0.000388

Approximate root: 2.597626

Advantages of the Bisection Method

  1. Guaranteed Convergence:

    • As long as the function is continuous and the interval contains a root, the method will always converge.
  2. Simple Implementation:

    • The algorithm is easy to understand and implement in any programming language.
  3. Works for Any Continuous Function:

    • Can be applied to any function where a sign change exists in the interval.

Limitations of the Bisection Method

  1. Slow Convergence:

    • The method requires multiple iterations to reach high accuracy.
    • More efficient methods like Newton-Raphson can converge faster.
  2. Requires a Sign Change:

    • The method only works if f(a) * f(b) < 0.
    • If no sign change exists, the method fails.
  3. Not Suitable for Multiple Roots in One Interval:

    • If an interval contains more than one root, the method may not find all of them.

The Bisection Method is a fundamental numerical technique for finding roots of equations. It is easy to implement, guarantees convergence, and works well for continuous functions where a sign change occurs in the given interval.

Key Takeaways:

  • The method repeatedly halves the interval to find the root.
  • The C program provided calculates the root with user-defined tolerance.
  • While reliable, the method can be slow and requires an initial interval with a sign change.

Understanding and implementing the Bisection Method is valuable in engineering, physics, and computational mathematics, making it a great tool for problem-solving.

You cannot copy content of this page