Bisection Method in C and C++

In this instructional exercise, you will get the program for bisection technique in C and C++.

To discover a root precisely Bisection Method is utilized in Mathematics. Bisection strategy calculation is anything but

difficult to program and it generally combines which means it generally discovers root.

Bisection Method more than once divides an interim and afterwards chooses a subinterval in which root lies.

It is a basic and strong technique however more slow than different strategies.

It is additionally called Interval splitting, double search technique and division strategy.

Bisection Method ascertains the root by first figuring the mid purpose of the given interim end focuses.

Bisection Method Procedure

The contribution for the strategy is a nonstop capacity f, an interim [a, b], and the capacity esteems f(a) and f(b). The capacity esteems are of inverse sign (there is in any event one zero intersection inside the interim). Every cycle plays out this means:

  1. Compute the midpoint c = (a + b)/2
  2. Compute the capacity esteem at the midpoint, function(c).
  3. On the off chance that union is good (that is, a – c is adequately little, or f(c) is adequately little), return c and quit repeating.
  4. Inspect the indication of f(c) and supplant either (a, f(a)) or (b, f(b)) with (c, f(c)) so that there is a zero intersection inside the new interim.

Upsides and downsides

Favourable position of the bisection technique is that it is destined to be united and very simple to actualize.

The inconvenience of bisection technique is that it can’t distinguish various roots and is slower contrasted with different strategies for ascertaining the roots.

Program for Bisection Method in C

#include<stdio.h>
 
//function used is x^3-2x^2+3
double func(double x)
{
    return x*x*x - 2*x*x + 3;
}
 
double e=0.01;
double c;
 
void bisection(double a,double b)
{
    if(func(a) * func(b) >= 0)
    {
        printf("Incorrect a and b");
        return;
    }
 
    c = a;
 
    while ((b-a) >= e)
    {
        c = (a+b)/2;
        if (func(c) == 0.0){
            printf("Root = %lf\n",c);
            break;
        }
        else if (func(c)*func(a) < 0){
                printf("Root = %lf\n",c);
                b = c;
        }
        else{
                printf("Root = %lf\n",c);
                a = c;
        }
    }
}
 
int main()
{
    double a,b;
    a=-10;
    b=20;
 
    printf("The function used is x^3-2x^2+3\n");
    printf("a = %lf\n",a);
    printf("b = %lf\n",b);
    bisection(a,b);
    printf("\n");
    printf("Accurate Root calculated is = %lf\n",c);
 
    return 0;
}

Output

a = -10.000000
b = 20.000000
Root = 5.000000
Root = -2.500000
Root = 1.250000
Root = -0.625000
Root = -1.562500
Root = -1.093750
Root = -0.859375
Root = -0.976563
Root = -1.035156
Root = -1.005859
Root = -0.991211
Root = -0.998535

Accurate Root calculated is = -0.998535

Program for Bisection Method in C++

#include<iostream>
 
using namespace std;
 
//function used is x^3-2x^2+3
double func(double x)
{
    return x*x*x - 2*x*x + 3;
}
 
double e=0.01;
double c;
 
void bisection(double a,double b)
{
    if(func(a) * func(b) >= 0)
    {
        cout<<"Incorrect a and b";
        return;
    }
 
    c = a;
 
    while ((b-a) >= e)
    {
        c = (a+b)/2;
        if (func(c) == 0.0){
            cout << "Root = " << c<<endl;
            break;
        }
        else if (func(c)*func(a) < 0){
                cout << "Root = " << c<<endl;
                b = c;
        }
        else{
                cout << "Root = " << c<<endl;
                a = c;
        }
    }
}
 
int main()
{
    double a,b;
    a=-10;
    b=20;
 
    cout<<"The function used is x^3-2x^2+3\n";
    cout<<"a = "<<a<<endl;
    cout<<"b = "<<b<<endl;
    bisection(a,b);
    cout<<"\n";
    cout<<"Accurate Root calculated is = "<<c<<endl;
 
    return 0;
}

Remark beneath in the event that you have any questions with respect to the above program for bisection strategy in C and C++.

Leave a Comment

error: Alert: Content is protected!!