Bisection Method In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Bisection Method In C++

Bisection Method In C++

BLUF: Mastering Bisection Method In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Bisection Method In C++

C++ is renowned for its efficiency. Learn how Bisection Method In C++ enables low-level control and high-performance computing in the tutorial below.

An essential aspect of numerical computation involves finding the roots of continuous functions within a specified interval. In such cases, the bisection method offers a straightforward way to determine these roots, also known as the interval halving technique, binary search algorithm, or dichotomy method. While not the quickest method, its reliability and simplicity render it a valuable asset for numerical computations.

When dealing with continuous functions within the range [a, b], where the function values at the boundaries, f(a) and f(b), exhibit different signs, the bisection technique proves to be particularly advantageous. The intermediate value theorem ensures the presence of a root within this interval. The bisection algorithm involves iteratively dividing the interval in half and assessing the function at the midpoint to approach the target root.

Implementations of the Bisection method in C++:

The process of implementing the bisection technique in C++ involves the sequential execution of the following stages:

  1. Begin by defining the target function that necessitates root determination. Let's consider the function f(x) = x3 - 2x2 + 3 for illustration purposes.
Example

double func(double x)
{
    return x*x*x - 2*x*x + 3;
}
  1. Choose a starting range [a, b] where the signs of the function values at the endpoints are opposite. Its need is essential for the procedure to work.
  2. The main concept of the bisection method is to iteratively reduce the interval. Establish a loop that keeps going as long as the interval width (b - a) is wide enough (in this case, 01 ). Inside the loop:
  • Use the formula c = (a + b) / 2 to determine the current interval's midpoint .
  • Calculate the value of the function at c.
  • Depending on the function value at the midpoint, three choices emerge:
  • If func(c) is equal to 0 , then c is the root already, and the process can end.
  • If func(c) and func(a) have opposing signs, update b to c to effectively shrink the interval.
  • Update a to c if func(c) and func(b) have opposing signs.
  • Up until the interval width is narrow enough, the loop keeps adjusting the interval. Now, the value of c comes close to representing the root.
  1. In the end, the value of c of the loop represents the function's approximate root.
  2. Example:

The complete C++ code for implementing the bisection algorithm is presented below:

Example

#include<iostream>

using namespace std;

double func(double x)
{
    return x*x*x - 2*x*x + 3;
}

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) >= 0.01)
    {
        c = (a+b)/2;
        if (func(c) == 0.0){
            break;
        }
        else if (func(c)*func(a) <0){
            b=c;
        }
else{
            a=c;
        }
    }
}

int main()
{
    double a,b;
    a=-10;
    b=20;
    bisection(a,b); 
cout<<"\n";
cout<<"The value of root is = "<<c<<endl;

    return 0;
}

Output:

Output

The value of root is = -0.998535

Explanation:

  • The initial interval is specified by the program to be a = -10 and b = 20 .
  • The midpoint c of the present interval (a, b) is determined by the bisection method, which iteratively refines the interval.
  • (A + B) / 2 is used to compute the value of c .
  • At the halfway point , the program examines the value of the function func(c):
  • Since c is the root, the loop ends if func(c) equals 0 .
  • If func(c) * func(a)= 0 , then changing b to c will shorten the interval because func(c) and func(a) have opposite signs.
  • If none of the conditions are true , then the interval is shortened by updating a to c because func(c) and func(b) have opposing signs.
  • Until the interval width (b - a) drops below 01 , the loop is repeated.
  • In this example, the approximated root c's value is about -0.998535 . After that, it is printed by the program.
  • Conclusion:

In summary, the bisection technique stands as a reliable approach for estimating the roots of continuous functions within defined intervals. While it may not be the most rapid method available currently, its straightforward nature and robustness render it a valuable asset in numerical computations. This method efficiently narrows down the search area for roots by repetitively dividing intervals and assessing function values. The provided C++ implementation's sample result demonstrates its proficiency in root determination. Despite the existence of more intricate algorithms, the bisection method's simplicity and adaptability ensure its enduring relevance in the realm of numerical problem-solving strategies.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience