C++ Program for Bisection Method to find the roots of an Equation

//bisection method
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
double f(double x);    //declare the function for the given equation
double f(double x)    //define the function here, ie give the equation
{
    double a=pow(x,3)-x-11.0;    //write the equation whose roots are to be determined
    return a;
}
int main()
{    
    cout.precision(4);        //set the precision
    cout.setf(ios::fixed);
    double a,b,c,e,fa,fb,fc;    //declare some needed variables
    a:cout<<"Enter the initial guesses:\na=";    //Enter the value of a(set a label('a:') for later use with goto)
    cin>>a;
    cout<<"\nb=";            //Enter the value of b
    cin>>b;
    cout<<"\nEnter the degree of accuracy desired"<<endl;    //Enter the accuracy
    cin>>e;                //e stands for  accuracy
    if (f(a)*f(b)>0)        //Check if a root exists between a and b
    {                //If f(a)*f(b)>0 then the root does not exist between a and b
        cout<<"Please enter a different intial guess"<<endl;
        goto a;            //go back to 'a' ie 17 and ask for different values of a and b
    }
    else                //else a root exists between a and b
    {
    while (fabs(a-b)>=e)        /*if the mod of a-b is greater than the accuracy desired keep                         bisecting the interval*/
    {
        c=(a+b)/2.0;        //bisect the interval and find the value of c
        fa=f(a);        
        fb=f(b);
        fc=f(c);
        cout<<"a="<<a<<"     "<<"b="<<b<<"     "<<"c="<<c<<"      fc="<<fc<<endl;/*print the                             values of a,b,c and fc  after each iteration*/        
        if (fc==0)        //if f(c)=0, that means we have found the root of the equation
        {
            cout<<"The root of the equation is "<<c;    /*print the root of the equation                                         and break out of the loop*/
            break;
        }

        if (fa*fc>0)    //if f(a)xf(c)>0, that means no root exist between a and c 
        {
            a=c;    /*hence make a=c, ie make c the starting point of the interval and b the                     end point*/
        }
        else if (fa*fc<0)
        {    
            b=c;    /*this means that a root exist between a and c therfore make c the end                     point of the interval*/
        }    
        
    
    }
    }            //The loop ends when the difference between a and b becomes less than the desired accuracy ie now the value stored in 'c' can be called the approximate root of the equation         
    cout<<"The root of the equation is "<<c;    //print the root    
    return 0;    
}
//output attached as jpg

output_bisection

Explanation of the above code:

I'm a physicist specializing in theoretical, computational and experimental condensed matter physics. I like to develop Physics related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.



2 thoughts on “C++ Program for Bisection Method to find the roots of an Equation

  1. tnx i like the code but i want to know what did you mean when you say degree of accuracy desired

Leave a Reply

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