C Program for Bisection Method

6 Min Read
C Program for Bisection Method

Bisection method is an iterative implementation of the ‘Intermediate Value Theorem‘ to find the real roots of a nonlinear function. According to the theorem “If a function f(x)=0 is continuous in an interval (a,b), such that f(a) and f(b) are of opposite nature or opposite signs, then there exists at least one or an odd number of roots between a and b.”

Using C program for bisection method is one of the simplest computer programming approach to find the solution of nonlinear equations. It requires two initial guesses and is a closed bracket method. this method never fails!

The programming effort for Bisection Method in C language is simple and easy. The convergence is linear, slow but steady. The overall accuracy obtained is very good, so this method is more reliable in comparison to the Newton Raphson method or the Regula-Falsi method.

C Program for Bisection Method

Features of Bisection Method:

  • Type – closed bracket
  • No. of initial guesses – 2
  • Convergence – linear
  • Rate of convergence – slow but steady
  • Accuracy – good
  • Programming effort – easy
  • Approach – middle point

Below is a source code in C program for bisection method to find a root of the nonlinear function x^3 – 4*x – 9. The initial guesses taken are a and b. The calculation is done until the following condition is satisfied:

|a-b| < 0.0005  OR  If (a+b)/2 < 0.0005 (or both equal to zero)
where, (a+b)/2 is the middle point value.

Variables:

  • itr – a counter variable which keeps track of the no. of iterations performed
  • maxmitr – maximum number of iterations to be performed
  • x – the value of root at the nth iteration
  • a, b – the limits within which the root lies
  • allerr – allowed error
  • x1 – the value of root at (n+1)th iteration

f(x) = x^3 – 4*x – 9

Source Code for Bisection Method in C:

#include<stdio.h>
#include<math.h>
float fun (float x)
{
    return (x*x*x - 4*x - 9);
}
void bisection (float *x, float a, float b, int *itr)
/* this function performs and prints the result of one iteration */
{
    *x=(a+b)/2;
    ++(*itr);
    printf("Iteration no. %3d X = %7.5f\n", *itr, *x);
}
void main ()
{
    int itr = 0, maxmitr;
    float x, a, b, allerr, x1;
    printf("\nEnter the values of a, b, allowed error and maximum iterations:\n");
    scanf("%f %f %f %d", &a, &b, &allerr, &maxmitr);
    bisection (&x, a, b, &itr);
    do
    {
        if (fun(a)*fun(x) < 0)
            b=x;
        else
            a=x;
        bisection (&x1, a, b, &itr);
        if (fabs(x1-x) < allerr)
        {
            printf("After %d iterations, root = %6.4f\n", itr, x1);
            return 0;
        }
        x=x1;
    }
    while (itr < maxmitr);
    printf("The solution does not converge or iterations are not sufficient");
    return 1;
}

Input/Output:

Also see,
Bisection Method MATLAB Program
Bisection Method Algorithm/Flowchart
Numerical Methods Tutorial Compilation

This code was designed to perform this method in an easy-to-read manner. The code also contains two methods; one to find a number within a specified range, and another to perform a binary search. I hope you found this useful and that you enjoy this article.

The method is used in computer science to solve a linear equation. The method involves dividing the given number into two parts. If the equation has a positive number, then the smaller part will be negative and the larger part will be positive. If the equation has a negative number, then the smaller part will be positive and the larger part will be negative.

The method is also known as the half-and-half method because you need to divide the number by 2 to get the smaller part and the larger part of the number. The method is also known as the midpoint method because it is used to find the midpoint of an interval.

Steps To Find the Root of an Equation Using Bisection Method

  1. Define a function that finds the root of an equation.
  2. Write a loop to find the root of an equation.
  3. Use the bisection method to find the root of an equation.
  4. Print the root of an equation using printf().

The following program will help you to find the root of an equation. Let us go through the code step by step.

First, we will define the function to find the root of an equation.


// Function to find root of an equation
float find_root( float x)
{
if (x > 0.0)
{
return 1.0 / sqrt(x);
}
else if (x < 0.0)
{
return (-1.0) / sqrt(x);
}
else
{
return x;
}
}

Hope you liked this post about the Bisection Method in C Program. The method is one of the easiest methods to find the midpoint of the interval. The method will be a good choice for those who are new to the C programming language.

Share This Article
25 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version